Java 方法无法正确打印ArrayList内容

Java 方法无法正确打印ArrayList内容,java,arraylist,Java,Arraylist,我需要打印出我的ArrayList的内容,但是打印不正确。更具体地说,是CustomerRegister中的getAccountSaldo没有返回正确的值(如果我想打印出一个不同的帐户) 这是我的密码: 类别帐户 public class Account { private Customer customer; private String nbr; private double saldo; public void setNbr(String nbr){ this.nbr = nb

我需要打印出我的
ArrayList
的内容,但是打印不正确。更具体地说,是
CustomerRegister
中的
getAccountSaldo
没有返回正确的值(如果我想打印出一个不同的帐户)

这是我的密码:

类别帐户

public class Account {

private Customer customer;
private String nbr;
private double saldo;

public void setNbr(String nbr){
    this.nbr = nbr;
}

public String getNbr(){
    return nbr;
}

public void setSaldo(double saldo){
    this.saldo = saldo;
}

public double getSaldo(){
    return saldo;
}

public void setCustomer(Customer customer){
    this.customer = customer;
}

public Customer getCustomer(){
    return customer;
}

public void withdraw(double amount){
    saldo -= amount;
}

public void deposit(double amount){
    saldo += amount;
}



}
类客户

public class Customer {

private String nbr;
private String namn;

private ArrayList<Account> accounts = new ArrayList<Account>();

public void setNbr(String nbr){
    this.nbr = nbr;
}

public String getNbr(){
    return nbr;
}

public void setNamn(String namn){
    this.namn = namn;
}

public String getNamn(){
    return namn;
}

public void setAccounts(ArrayList<Account> accounts){
    this.accounts = accounts;
}

public ArrayList<Account> getAccounts(){
    return accounts;
}

public void add(Account account){
    accounts.add(account);
}

public Account find(String nbr){
    for(Account a : accounts){
        if(nbr == a.getNbr()){
            return a;
        }
    }
    return null;
}

}

不能使用=运算符比较字符串,这不会检查内容是否相等,而是检查两个字符串的地址。 返回语句的位置也不正确。 我对您的功能做了一些更改,请尝试一下:

public Double getAccountSaldo(String customerNbr, String accountNbr) {
    double balance = 0;
    Customer custNbr = find(customerNbr);
    // Shouldn't it be "Account acctNbr = find(accountNbr);" here? 
    // Problem is I can't access that method in this class...

    if(custNbr != null){
    for (Account x : printAccounts(customerNbr)) {
        if (accountNbr.equals(x.getNbr())) {
            balance += x.getSaldo();

        }        
    }
    }
    return balance;
}

不能使用=运算符比较字符串,这不会检查内容是否相等,而是检查两个字符串的地址。 返回语句的位置也不正确。 我对您的功能做了一些更改,请尝试一下:

public Double getAccountSaldo(String customerNbr, String accountNbr) {
    double balance = 0;
    Customer custNbr = find(customerNbr);
    // Shouldn't it be "Account acctNbr = find(accountNbr);" here? 
    // Problem is I can't access that method in this class...

    if(custNbr != null){
    for (Account x : printAccounts(customerNbr)) {
        if (accountNbr.equals(x.getNbr())) {
            balance += x.getSaldo();

        }        
    }
    }
    return balance;
}

你对我的代码(特别是演示类)有什么改进建议吗?详细说明一下:我不需要在demo类中创建列表,只需使用add方法?(我完全是个新手,只是在学习ArrayList)有一个建议,你的类中仍然有错误的字符串比较,只要做得正确,你的应用程序就会按要求工作。若你们想玩得更多,那个么改变你们的演示类,从用户那个里获取一些输入,然后相应地继续。你们对我的代码(特别是演示类)有什么改进建议吗?详细说明一下:我不需要在demo类中创建列表,只需使用add方法?(我完全是个新手,只是在学习ArrayList)有一个建议,你的类中仍然有错误的字符串比较,只要做得正确,你的应用程序就会按要求工作。若你们想玩得更多,那个么改变你们的演示类,从用户那个里获取一些输入,然后进行相应的操作。
Konto 1 Balance: 2200.0kr 
Konto 2 Balance: 2000.0kr 
Nr: 1 Namn: Adam Schinn
2200.0
public Double getAccountSaldo(String customerNbr, String accountNbr) {
    double balance = 0;
    Customer custNbr = find(customerNbr);
    // Shouldn't it be "Account acctNbr = find(accountNbr);" here? 
    // Problem is I can't access that method in this class...

    if(custNbr != null){
    for (Account x : printAccounts(customerNbr)) {
        if (accountNbr.equals(x.getNbr())) {
            balance += x.getSaldo();

        }        
    }
    }
    return balance;
}