在另一个类中读取arraylist对象及其字段(java)

时间:2016-01-15 11:06:21

标签: java arraylist

我正在尝试创建一个用户登录系统,我需要帮助读取另一个类中现有arraylist的objecs及其字段以进行身份​​验证.Below是对象构造函数的代码,

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
*/

这是我的用户和项目列表的“数据库”(注意 - 我有这些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

以下是我从上面显示的ArrayList中读取的内容..:

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) {
        }

如果有人可以帮我解决这个问题,我会很高兴。我花了很多时间试图解决这个问题。

动作侦听器代码:

LoginButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            LoginButtonActionPerformed(evt);
        }
    });

1 个答案:

答案 0 :(得分:2)

不应该使用==来比较字符串的相等性,而是使用.equals()==比较引用而不是字符串变量的内容。为了比较内容,应根据要求使用.equals().equalsIgnoreCase()

有关参考,请参阅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);
}

假设isVisible()在类Succed上定义,如果成功设置为可见则返回true,而在其他情况下返回false