从调用Java中的两个类创建方法 [英] Creating a method from calling two classes in java

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

问题描述

我不知道其中的我的方法是不工作的。当我创建addSavingsAccount账户的一切工作正常,但是当我想从两个类打印出来的信息它打印的姓名和PNR,但显示了第一ACCOUNTNUMBER到所有客户端。这有什么错呢?

  //这工作正常
公众诠释addSavingsAccount(长PNR){    的for(int i = 0; I< customerlist.size();我++)
    {
        如果(customerlist.get(I).getP code()== PNR)
        {
            客户客户= customerlist.get(I)
            customer.addAccount(PNR);
            返回account.getAccountId();
        }
    }
    返回-1;
}
//但是这不
公共字符串infoAccount(PNR长,诠释帐户ID)
{
    字符串信息=;
    对于(客户的客户:customerlist){
          如果(customer.getP code()== PNR){
            this.customer =客户;
            ArrayList的< SavingsAccount>帐户= customer.getAccount();
            对于(SavingsAccount账户:账户){
              this.account =账户;
              如果(account.getAccountId()==帐户ID){
                信息= account.toString();
            }
            }
        }
     }
    返回信息;
}//这是我打印出的第一次尝试:
公共字符串infoAccount(PNR长,诠释帐户ID)
{
    字符串信息=;
    对于(客户的客户:customerlist)
    {
        如果(PNR == customer.getP code())
        {
            对于(SavingsAccount账户:账户)
            {
                如果(帐户== account.getAccountId())
                {
                    信息=Personnummer:+ PNR +\\ nKontonummer:+帐户ID
                    +\\ nSaldo:+量+\
Ränta:+ SavingsAccount.RATE;
                }
            }
        }
    }
    返回信息;
}
//在Customerclass方法
公共无效addAccount(长PNR){
    accounts.add(新SavingsAccount());
}公众的ArrayList< SavingsAccount>是getAccount(){
    返回账户;
}
//从SavingsAccount方法
公众诠释getAccountId(){
    返回accountCount ++;
}公共字符串的toString(){
    字符串infoAccount =\\ tKontonr:+帐户ID +\\ tKontotyp:+ ACCOUNTTYPE +
    \\ nSaldo:+余额+\\tRäntesats:+率;
    返回infoAccount;
}


解决方案

我不知道我看到足够code,但它看起来像你想设置的东西,让每个帐户有一个帐户ID。然而,你的 getAccountId 方法是要在每次被称为时间返回不同的值,即使在呼吁同 SavingsAccount ,因为它每次都会增加一个计数器,它被称为

您需要做的是声明实例成员,如帐户ID SavingsAccount ,并设置它当一个新的 SavingsAccount 创建

 帐户ID = accountCount ++;

是您要使用 ++ 的唯一地方。然后, getAccountId()将返回实例成员,不增加任何东西。

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

解决方案

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.

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

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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆