3

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);
        }
    });
10
  • as a sidenote, i´d suggest to use a Enum instead of an int to identify if the user is a cashier or a manager. Commented Jan 15, 2016 at 11:07
  • 3
    Possible duplicate of How do I compare strings in Java? Commented Jan 15, 2016 at 11:09
  • When do you populate the two lists in DataBase ? Commented Jan 15, 2016 at 11:12
  • Hello @Berger thank you for comment , checck the code again , i added the list population Commented Jan 15, 2016 at 11:20
  • @KevinEsche thank you for your support , latter when i implement other classes i might change that , but at the moment need help with this :/ Commented Jan 15, 2016 at 11:20

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

26 Comments

Right on. In his case, something like this: if(d.getUsername() != null && d.getUsername().equals(username) && d.getPassword() != null && d.getPassword().equals(pass)) ...
Just if(username.equals(d.getUsername()) && pass.equals(d.getPassword())) would do, As .equals() method implicitly does the "not null" validation.
@VamshiKrishnaAlladi I changed that part of code but the part of code when i read the DataBase objects is part of an action listener button (login ) and when i click it , it does nothing ... maybe the code when i try to read the objecs is wrong ??...
@HasS can you post the code of the action listener?
@VamshiKrishnaAlladi you are presuming username is not null.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.