方法无法正确打印ArrayList内容

时间:2017-05-07 14:02:46

标签: java arraylist

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

这是我的代码:

班级帐户

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

}

Class CustomerRegister

public class CustomerRegister {

private ArrayList<Customer> customers = new ArrayList<Customer>();

public void setCustomers(ArrayList<Customer> customers){
    this.customers = customers;
}

public ArrayList<Customer> getCustomers(){
    return customers;
}

public void add(Customer k){
    customers.add(k);
}

public Customer find(String nr){
    for (Customer b : customers){
        if (nr == b.getNbr()){
            return b;
        }
    }
    return null;
}

public ArrayList<Account> printAccounts(String customerNbr){
    Customer l = find(customerNbr);
    if (l != null){
        return l.getAccounts();
    }
    return null;
}

// CAN'T SOLVE THIS
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 == x.getNbr()) {
            balance += x.getSaldo();
        }
        return balance;
    }
    }
    return null;
}

}

课程演示:

public class Demo {
    public static void main(String[] args){

    CustomerRegister r1 = new CustomerRegister();
    ArrayList<Account> listOfAccounts = new ArrayList<Account>();
    ArrayList<Customer> listOfCustomers = new ArrayList<Customer>();

    Customer c = new Customer();
    c.setNbr("1");
    c.setNamn("Adam Schinn");

    Account a = new Account();
    a.setNbr("Konto 1");
    a.setSaldo(2200);
    a.setCustomer(c);

    Account b = new Account();
    b.setNbr("Konto 2");
    b.setSaldo(2000);
    b.setCustomer(c);

    listOfCustomers.add(c);
    listOfAccounts.add(a);
    listOfAccounts.add(b);

    c.setAccounts(listOfAccounts);
    r1.add(c);

    r1.setCustomers(listOfCustomers);

    for(Account temp : r1.printAccounts("1")){
        System.out.println(temp.getNbr() + " Balance: " + temp.getSaldo() + "kr ");
    }

    for(Customer temp : r1.getCustomers()){
        System.out.println("Nr: " + temp.getNbr() + " Namn: " + temp.getNamn());
    }

    // CAN'T SOLVE THIS
    // If I write "Konto 2" instead of "Konto 1" it prints "0.0"
    System.out.println(r1.getAccountSaldo("1", "Konto 1"));


}
}

打印

Konto 1 Balance: 2200.0kr 
Konto 2 Balance: 2000.0kr 
Nr: 1 Namn: Adam Schinn
2200.0

1 个答案:

答案 0 :(得分: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;
}