简体   繁体   English

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

[英]Reading an arraylist Objects and their fields in another class(java)

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, 我正在尝试建立一个用户登录系统,我需要帮助阅读另一个类中现有arraylist的objecs及其字段以进行身份​​验证.Belowis是对象构造函数的代码,

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) 这是我的用户和项目列表的“数据库”( 注意 - 我有这些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 .. : 这是我从上面显示的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) {
        }

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

One should never compare strings for equality using == , instead make use of .equals() . 不应该使用==来比较字符串的相等性,而是使用.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. 为了比较内容,应根据要求使用.equals().equalsIgnoreCase()

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. 假设isVisible()是在类中定义Succed ,其返回true如果成功设置为可见,并返回false的其它情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM