Reputation: 1767
hi i am a totally new guy on JAVA OO concepts ,and this is my first time programming,i am currently stuck on how to create a registration class and a class which will store all the registration info, this is what i have done, 4 classes, a Account class, a Staff class extend Account,a Student class extend Account, and a dataStorage class to store the informations and to extract them when needed.
public class Account {
private String name;
private String department;
private String username;
private String password;
public Account()
{
}
public Account(String nm,String dep,String user,String pass)
{
name = nm;
department = dep;
username = user;
password = pass;
}
public void setName(String nm)
{
name = nm;
}
public String getName()
{
..... other accessors
}
/**/
public class Student extends Account {
private String studentNRIC;
private String studentID;
public Student(String n, String nr, String id, String dep, String user, String pass)
{
super(n, dep, user, pass);
studentNRIC = nr;
studentID = id;
}
public void setStudentNRIC(String nr)
{
studentNRIC = nr;
}
public String getStudentNRIC()
{
return studentNRIC;
}
....accessors
}
/**/
public class Staff extends Account {
private String staffID;
public Staff(String n, String id, String dep, String user, String pass)
{
super(n, dep, user, pass);
staffID = id;
}
public void setStaffID(String id)
{
staffID = id;
}
public String getStaffID()
{
return staffID;
}
}
/****/ import java.util.*;
public class DataStorage
{
ArrayList<Account> staff = new ArrayList<Account>();
ArrayList<Account> student = new ArrayList<Account>();
public DataStorage(Staff aAcc)
{
staff.add(aAcc);
}
public DataStorage(Student aAcc)
{
student.add(aAcc);
}
public String msg()
{
Staff sf = staff.get(0);
return staff;
}
}
Upvotes: 1
Views: 5350
Reputation: 4034
Here some test class, as it would have been written with JUnit.
// Tests related to the Staff class
public class StaffTest {
// This test should be successful, as all argument are correct
@Test
public void testCreateStaffMember() {
String name = "John";
String staffId = "0900477A";
String department = "ENG";
String username = "john";
String password = "secret";
new Staff(name, staffId, department, username, password);
}
// This test should throw an IllegalArgumentException, as the name is required.
@Test(expected=IllegalArgumentException.class)
public void testCreateStaffMemberNameNull() {
String name = null;
String staffId = "0900477A";
String department = "ENG";
String username = "john";
String password = "secret";
new Staff(name, staffId, department, username, password);
}
// This test should throw an IllegalArgumentException, as the name is required.
@Test(expected=IllegalArgumentException.class)
public void testCreateStaffMemberNameEmpty() {
String name = "";
String staffId = "0900477A";
String department = "ENG";
String username = "john";
String password = "secret";
new Staff(name, staffId, department, username, password);
}
// More constructor test follow
// This test should be successful, as all getters, should return the given input values.
@Test
public void testValidateGetterMethods() {
String name = "";
String staffId = "0900477A";
String department = "ENG";
String username = "john";
String password = "secret";
Staff staff = new Staff(name, staffId, department, username, password);
Asset.assertEquals(name, staff.getName());
Asset.assertEquals(staffId, staff.getStaffId());
Asset.assertEquals(department, staff.getDepartment());
Asset.assertEquals(username, staff.getUsername());
Asset.assertEquals(password, staff.getPassword());
}
// More tests...
}
// Tests related to the DataStorage class
public class DataStorageTest {
@Test
public void testAddStaffMembers() {
DataStorage ds = new DataStorage();
ds.addStaff(createJohn());
ds.addStaff(createAlan());
}
// Here you deside, as the designer off the application, what should happen!
// Will the second John, override the first John? As they have the same ID?
// Or will an exception be thrown, because the staff member with that id
// already exists?
@Test
public void testAddStaffMembersSameName() {
DataStorage ds = new DataStorage();
ds.addStaff(createJohn());
ds.addStaff(createJohn());
}
Staff createJohn() {
String name = "John";
String staffId = "0900477A";
String department = "ENG";
String username = "john";
String password = "secret";
return new Staff(name, staffId, department, username, password);
}
Staff createAlan() {
...
}
}
Upvotes: 0
Reputation: 1767
thanks to the answers i have come up with a new DataStorage class with HashMap object, note that i have not full grasp how HashMap works and this is what i have partially done, and would like to ask am i going in the right direction? A creation of a test class works, i am able to get the a value.
import java.util.*;
public class DataStorage
{
HashMap<String, Student> students = new HashMap<String, Student>();
HashMap<String, Staff> staffMembers = new HashMap<String, Staff>();
//Default constructor
public DataStorage(){
}
public void addStaffMember(Staff aAcc)
{
staffMembers.put(aAcc.getAccID(),aAcc);
}
public void addStudentMember(Student aAcc)
{
students.put(aAcc.getAccID(),aAcc);
}
public Staff getStaffMember(Staff aAcc)
{
return staffMembers.get(aAcc.getAccID());
}
}
/* test*/
public class Test {
public static void main(String[] args) {
DataStorage ds = new DataStorage();
String name = "John";
String ID = "0900477A";
String Department = "ENG";
String username = "ky";
String pass = "ky";
Staff sf2 = new Staff("Tim","Tim","Tim","Tim","Tim");
Staff sf = new Staff(name, ID, Department, username, pass);
ds.addStaffMember(sf2);
ds.addStaffMember(sf);
Staff temp = ds.getStaffMember(sf2);
System.out.println(temp.getName());
}
}
Upvotes: 0
Reputation: 39495
It looks good so far. One thing that you could do is to move the ID (staffID
and studentID
) up to the Account
class, calling it something like accountID
. It seems that all accounts that you create need an ID, so moving this up makes sense.
One benefit of this would be to simplify your DataStorage
class by having a single Collection
. Even if you would like to keep your Staff
and Students
separate, you could also use Map
instead of a List
in DataStorage
, as this would make lookups faster and easier.
Upvotes: 1
Reputation: 104188
Your DataStorage class needs some corrections:
public class DataStorage
{
ArrayList<Account> staffMembers = new ArrayList<Account>();
ArrayList<Account> students = new ArrayList<Account>();
// Default constructor
public DataStorage() {
}
public void addStaffMember(Staff aAcc)
{
staffMembers.add(aAcc);
}
public void addStudentMember(Student aAcc)
{
students.add(aAcc);
}
public Student getStudentById(String id)
{
}
// More retrieval methods
}
You will usually want to hava more retrieval methods (get by department, name, id). Some will return a single item and some a list. If you are only interested in retrieving accounts by id, you could use a Map instead of a list. The truth is that to support all retrieval methods, you would need a relational database of some kind. If you are looking for a naive solution, keep the ArrayList and iterate through all members to find the ones you are looking for.
Upvotes: 0