本文介绍了在java中通过调用两个类创建一个方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定我的哪一种方法不起作用.当我从 addSavingsAccount 创建帐户时,一切正常,但是当我想打印两个类的信息时,它会打印名称和 pNr,但会向所有客户端显示第一个帐号.这有什么问题吗?

I'm not sure which one of my methods that's not working. When i create accounts from addSavingsAccount everything is working fine, but when I want to print out the info from two classes it prints name and pNr but shows the first accountnumber to all clients. What's wrong with this?

//this works fine
public int addSavingsAccount(long pNr){ 

    for(int i = 0; i < customerlist.size(); i++)
    {
        if(customerlist.get(i).getPCode() == pNr)
        {
            Customer customer = customerlist.get(i);
            customer.addAccount(pNr);
            return account.getAccountId();
        }
    }
    return -1;
}


//but this don't
public String infoAccount(long pNr, int accountId)
{
    String info = "";
    for(Customer customer : customerlist) {
          if(customer.getPCode() == pNr) {
            this.customer = customer;
            ArrayList<SavingsAccount> accounts = customer.getAccount();
            for (SavingsAccount account : accounts) {
              this.account = account;
              if (account.getAccountId() == accountId){
                info = account.toString();
            }
            }
        }
     } 
    return info;
}

//this was my first try of printing out:
public String infoAccount(long pNr, int accountId)
{
    String info = "";
    for(Customer customer : customerlist)
    {
        if(pNr == customer.getPCode())
        {
            for(SavingsAccount account : accounts)
            {
                if(accountId == account.getAccountId())
                {
                    info = "Personnummer: " + pNr + "\nKontonummer: " + accountId
                    + "\nSaldo: " + amount + "\nRänta: " + SavingsAccount.RATE;
                }
            }
        } 
    }
    return info; 
}


//the methods in Customerclass
public void addAccount(long pNr){
    accounts.add(new SavingsAccount());
}

public ArrayList<SavingsAccount> getAccount(){
    return accounts;
}


//methods from SavingsAccount
public int getAccountId(){
    return accountCount++;
}

public String toString(){
    String infoAccount = "\tKontonr: " + accountId + "\tKontotyp: " + accounttype +
    "\nSaldo: " + balance + "\tRäntesats: " + RATE;
    return infoAccount;
}

推荐答案

我不确定我看到的代码是否足够,但看起来您正在尝试设置以便每个 account 都有一个帐户 ID.但是,您的 getAccountId 方法每次调用都会返回不同的值,即使在同一个 SavingsAccount 上调用也是如此,因为每次调用它都会增加一个计数器.

I am not sure I see enough code, but it looks like you're trying to set things up so that each account has an account ID. However, your getAccountId method is going to return a different value each time it's called, even when called on the same SavingsAccount, because it will increment a counter each time it's called.

你需要做的是在SavingsAccount中声明一个accountId等实例成员,并在有新的SavingsAccount时进行设置已创建:

What you need to do is to declare an instance member such as accountId in the SavingsAccount, and set it when a new SavingsAccount is created:

accountId = accountCount++;

那个是您唯一想使用++的地方.然后,getAccountId() 将返回实例成员,而不增加任何内容.

That is the only place you want to use the ++. Then, getAccountId() would return the instance member, without incrementing anything.

这篇关于在java中通过调用两个类创建一个方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-18 23:43