views:

67

answers:

1

i am currently stucked on how to create a login algorithm which will login a user based on 2 HashMap objects namely Students and StaffMembers from a class DataStorage, i do not know after getting the texts input from LoginHandler() what do i do with them to compare it with my DataStorage .

/* Class DataStorage*/

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());
   }

   public Student getStudent(Student aAcc)
   {
    return students.get(aAcc.getAccID());
   }

   public Account authUser(String user, String pass)
   {

   }  
}

/* Class Account*/

public class Account {

 private String name;
 private String department;
 private String username;
 private String password;
 private String accountID;
 public Account()
 {
 } 
    public Account(String nm,String dep,String user,String pass, String accID) 
    {
     name = nm;
       department = dep;
     username = user;
     password = pass;
     accountID = accID;

    }

    public void setName(String nm)
    {
     name = nm;
    }

    public String getName()
    {
     return name;
    }

    public void setDep(String d)
    {
     department = d;
    }

    public String getDep()
    {
     return department;
    }

    public void setUser(String u)
    {
     username = u;
    }
    public String getUser()
    {
     return username;
    }

    public void setPass(String p)
    {
     password = p;
    }

    public String getPass()
    {
     return password;
    }

    public void setAccID(String a)
    {
     accountID = a;
    }

    public String getAccID()
    {
     return accountID;
    }
}

/* Class Staff extends account*/

public class Staff extends Account {

 public Staff(String n, String id, String dep, String user, String pass)
 {
  super(n, dep, user, pass, id);

 }

}

/** Class Student extends Account**/

public class Student extends Account {

 private String studentNRIC;

 public Student(String n, String nr, String id, String dep, String user, String pass)
 {
  super(n, dep, user, pass, id);
  studentNRIC = nr;
 }

    public void setStudentNRIC(String nr)
    {
     studentNRIC = nr;
    }

    public String getStudentNRIC()
    {
     return studentNRIC;
    }

}

/* class loginHandler which will handle a login button/

class LoginHandler implements ActionListener
 {
  public void actionPerformed(ActionEvent e)
  {
   String tempUser;
   String tempPass;
   tempUser = txfUser.getText();
   tempPass = txfPass.getText();

  }

 }
+1  A: 

First of all you should change the getters in DataStorage to have String parameters, so that you can look up a student (or staff member) based on her account ID:

public Student getStudent(String aAcc)
{
    return students.get(aAcc);
}

You can then lookup the username in actionPerformed, like

Student student = dataStorage.getStudent(tempUser);
if (student != null && student.getPass().equals(tempPass))
{
    // login successful
    ...
} else {
    // Login failed - display error message
}

Similarly for staff members.

Péter Török
thanks, for the reply, does this mean i need to use a iterator to go through the collection since my HashMap keys are based on accountIDs and not the username?so the actionPerformed have to pass in a accountID instead of a username for it to check
kyrogue
@kyrogue in general it is best to use that property as the map key, by which you need to lookup users most often. I assumed that username and account id are the same, sorry. If you use the username for login, update the map to use that property as key.
Péter Török