如何循环 ObservableArrayList 以查看两个属性是否匹配?

How to cycle through ObservableArrayList to see if two properties match?

我有一个循环访问所有客户的登录系统,以查看特定的用户名和密码是否匹配,但它似乎不起作用。客户都在 ObservableArrayList 中。我参考了 this SO post 来制作它,但它仍然没有用。任何帮助将不胜感激。谢谢。

其他信息

TextField username,password;
Customer checkCustomer;
ObservableList<Customer> customers;

customers = FXCollections.observableArrayList();

登录系统(GUI)

if(t.getSource() == logIn){
            //Owner login


            if(username.getText().equals("admin") && password.getText().equals("admin")){
                System.out.println("success, owner logged in");
                window.setScene(primaryOwner);
            }
            else {

                  if(findRegisteredCustomer(username.getText(),password.getText()) != null){
                      System.out.println("Success, customer logged in");
                      if(checkCustomer.getPoints() < 1000){
                        status = "Silver";
                       }
                      else if(checkCustomer.getPoints() > 1000){
                        status = "Gold";
                       }
                      welcome.setText("Welcome " + checkCustomer.getUsername() + ". " + "You have " + checkCustomer.getPoints() + " points. " + "Your status is " + status + ".");
                      System.out.println("success, " + checkCustomer.getUsername() + " has logged in");
                      window.setScene(customerStartScreen);
                  }
                  else {
                      System.out.println("Username: " + username.getText() + ", Password: " + password.getText());
                      System.out.println("failed, incorrect login details");
                  }
            }
        }

查找客户是否注册方法

public Customer findRegisteredCustomer(String username, String password){
          for(Customer customerTest: customers){
              if(customerTest.getUsername().equals(username) && customerTest.getPassword().equals(password)){
                      System.out.println("Registered user");
                      checkCustomer = customerTest;
                      return customerTest;
                  }
              return null;
          }
          return null;
}

客户class

public class Customer extends User {
    private int points;
    
    public Customer(String username, String password){
        super(username,password);
        points = 0;
        
    }
    
    public Customer(String username, String password, int points){
        super(username,password);
        this.points = points;
    }

    public int getPoints() {
        return points;
    }

    public void setPoints(int points) {
        this.points = points;
    }
    
}

你的 if 块后 return null,删除 findRegisterdCustomer 方法中的第一个 return null,你应该没问题。 此外,您可能还想查看散列、等号方法和默认密码,以及这些可能存在的问题。