Reputation: 201
I am trying to make a user login system at the moment and i need help on reading objecs and their fields of an existing arraylist in another class in order to authenticate.Belowis the code for the object constructor,
public class User {
public String UserName ;
public String UserSurname ;
public String UserUsername ;
public String UserPassword ;
public int UserType ; // 0 for cashier
// 1 for manager
public User() {}
public User(String UserName, String UserSurname, String UserUsername, String UserPassword, int UserType) {
this.UserName = UserName;
this.UserSurname = UserSurname;
this.UserUsername = UserUsername;
this.UserPassword = UserPassword;
this.UserType = UserType;
}
/*
Getters and Setters
*/
Here is my "database " of users and itemlist (Note-I have a constructor for these obj)
public class DataBase {
ArrayList<User> userlist;
ArrayList<Item> itemlist;
public DataBase(){
User user1 = new User("example","example","example","example",1) ;
User user2 = new User("sda","fas","gdf","vcx",0) ;
User user3 = new User("htr","ytr","vxc","xaxxzx",0) ;
User user4 = new User("dag","gdf","dfgfgd","thrhf",0) ;
ArrayList<User> userlist = new ArrayList<User>();
userlist.add(user1);
userlist.add(user2);
userlist.add(user3);
userlist.add(user4);
ArrayList<Item> itemlist = new ArrayList<Item>() {};
Item item1 = new Item(1,"sadas",25.0) ;
Item item2 = new Item(1,"dcxz",25.0) ;
Item item3 = new Item(1,"czx",25.0) ;
Item item4 = new Item(1,"zxccxz",25.0) ;
Item item5 = new Item(1,"czx",25.0) ;
Item item6 = new Item(1,"tertgdf",25.0) ;
Item item7 = new Item(1,"zxcfes",25.0) ;
Item item8 = new Item(1,"erwt",25.0) ;
Item item9 = new Item(1,"gfdvcx",25.0) ;
Item item10 = new Item(1,"hjfgh",25.0) ;
itemlist.add(item1);
itemlist.add(item2);
itemlist.add(item3);
itemlist.add(item4);
itemlist.add(item5);
itemlist.add(item6);
itemlist.add(item7);
itemlist.add(item8);
itemlist.add(item9);
itemlist.add(item10);
}
public DataBase(ArrayList<User> userlist, ArrayList<Item> itemlist) {
this.userlist = userlist;
this.itemlist = itemlist;
}
public ArrayList<User> getUsers() {
return userlist;
} //end get Users
public ArrayList<Item> getItems(){
return itemlist;
} //end get Items
} // end class
And here is what i have done to read from the ArrayList i showed above .. :
String username = UsernameField.getText();
String pass = PasswordField.getText();
try
{
users = new DataBase();
ArrayList<User> userlist = users.getUsers();
for(User d : userlist){
if(d.getUserUsername() != null && d.getUserUsername() == username && d.getUserPassword() != null && d.getUserPassword() == pass){
Succed success = new Succed();
success.setVisible(true);
}
else {
failure fail = new failure() ;
fail.setVisible(true);
}
}// end for
}/* end try */ catch (NullPointerException ex) {
}
I would appriciate if someone could help me to solvet this.I've spent a lot of time trying to solve this problem.
The Action Listener code :
LoginButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
LoginButtonActionPerformed(evt);
}
});
Upvotes: 3
Views: 128
Reputation: 438
One should never compare strings for equality using ==
, instead make use of .equals()
. ==
compares the references not the contents of the string variables. For comparison of contents .equals()
or .equalsIgnoreCase()
should be used based on the requirement.
For reference see this
Update:
Right now you are running a loop on userlist and checking if the entered username is in the list. Instead do it like this:
Succed success = new Succed();
failure fail = new failure();
for(User d : userlist){
if(d.getUserUsername().equals(username) && d.getUserPassword().equals(pass)){
success.setVisible(true);
break;
}
}
if(!success.isVisible()){
fail.setVisible(true);
}
assuming isVisible()
is defined on class Succed
which return true
if the success is set to visible and return false
in other case.
Upvotes: 1