我的getAverageBalance方法总是返回0

时间:2014-04-12 03:31:51

标签: java class object

所以我有一个Account类和一个TestAccount类。在TestAccount类中,我需要调用我的getAverageBalance方法来查找我创建的所有帐户的平均余额。以下是我的平均余额法的代码:

public static double getAverageBalance()
{
if (numberOfAccounts > 0)
    return totalValueOfAccounts / numberOfAccounts;
else
    return 0;
}

问题是,if语句总是返回false,我不知道为什么。以下是我的其他帐户类代码

public class Account
{
  private int id;
  private double balance;
  private static double annualInterestRate;
  private java.util.Date dateCreated;
  private static int numberOfAccounts;
  private static double totalValueOfAccounts;

  public Account() 
  {
    dateCreated = new java.util.Date();
  }
  public Account(int newId, double newBalance, double newAnnualInterestRate) 
  {
    this.id = newId;
    this.balance = newBalance;
    dateCreated = new java.util.Date();
    this.annualInterestRate = newAnnualInterestRate;
  }
  public int getId() 
  {
  return this.id;
  }
  public double getBalance() 
  {
   return balance;
  }
  public double getAnnualInterestRate() 
  {
    return annualInterestRate;
  }
  public static double getNumberOfAccounts()
  {
    return numberOfAccounts;
  }
  public static double getTotalValueOfAccounts()
  {
    return totalValueOfAccounts;
  }
  public void setId(int newId) 
  {
    this.id = newId;
  }
  public void setAnnualInterestRate(int newAnnualInterestRate) 
  {
    this.annualInterestRate = newAnnualInterestRate;
  }
  public void setBalance(double newBalance) 
  {
    this.balance = newBalance;
  }
  public static void setAnnualInterestRate(double newAnnualInterestRate) 
  {
    annualInterestRate = newAnnualInterestRate;
  }
  public double getMonthlyInterest() 
  {
    return this.balance * (annualInterestRate / 1200);
  }
  public java.util.Date getDateCreated() 
  {
   return this.dateCreated;
  }
  public void withdraw(double amount)
  {
    this.balance -= amount;
  }
  public void deposit(double amount) 
  {
    this.balance += amount;
  }
  public void awardMonthlyInterestRate()
  {
    this.balance += getMonthlyInterest();
  }
  public void closeAccount()
  {
    System.out.println("Closing account " + id);
    this.numberOfAccounts -= 1;
    this.totalValueOfAccounts -= balance;
  }

以下是testaccount代码:

public class TestAccount
{
public static void printAccount(Account acct)
{
System.out.printf("%5d $%9.2f %5.2f%% %29s\n\n", acct.getId(), acct.getBalance(), acct.getAnnualInterestRate(), acct.getDateCreated());
}
public static void main(String[] args)
{
    System.out.println("The average account balance is: " + Account.getAverageBalance());
    System.out.println();
    Account a = new Account();
    System.out.println("Default account: ");
    printAccount(a);
    a.setId(1122);
    a.setBalance(20000);
    a.setAnnualInterestRate(4.5);
    System.out.println("Modified account: ");
    printAccount(a);
    a.withdraw(2500);
    a.deposit(3000);
    System.out.println("After withdraw and deposit: ");
    printAccount(a);
    a.awardMonthlyInterestRate();
    a.awardMonthlyInterestRate();
    a.awardMonthlyInterestRate();
    a.awardMonthlyInterestRate();
    a.awardMonthlyInterestRate();
    a.awardMonthlyInterestRate();
    System.out.println("After 6 months of interest: ");
    printAccount(a);
    System.out.println("The average account balance is: " + Account.getAverageBalance());
    a.closeAccount();
    System.out.println();
    Account[] accts = new Account[5];
    for (int i = 0; i < accts.length; i++)
    {
        accts[i] = new Account(i+1, (double) Math.floor(Math.random()*100000), 3.0 );
    }
    System.out.println("Array of five accounts with random balances:");
    for (int i = 0; i < accts.length; i++)
    {
        printAccount(accts[i]); 
    }
    System.out.println("The average account balance is: " + Account.getAverageBalance());
    System.out.println();
    System.out.println("Array after awarding 6 months of interest: ");
    for (int i = 0; i < accts.length; i++)
    {
        Account b = accts[i];
        for (int j = 0; j < 6; j++)
        {
            b.awardMonthlyInterestRate();
        }
        printAccount(accts[i]);
    }
    System.out.println("The average account balance is: " + Account.getAverageBalance());
}

}

1 个答案:

答案 0 :(得分:0)

我想也许你在构造函数中缺少一行:

public Account() {
    dateCreated = new java.util.Date();
    numberOfAccounts++;
}

如果您永远不会增加numberOfAccounts,我不确定您希望得到的值高于0。这特别令人遗憾,因为该变量是私有的,所以只有Account的方法才有可能设置它。

您需要向第二个构造函数添加相同的行,但您可以通过调用第一个构造函数来简化。您还需要添加到totalValueOfAccounts

public Account(int newId, double newBalance, double newAnnualInterestRate) 
{
    this(); // call the first constructor
    this.id = newId;
    this.balance = newBalance;
    this.annualInterestRate = newAnnualInterestRate;
    totalValueOfAccounts += newBalance;
}

您还需要在修改单个余额的每个方法中更新变量totalValueOfAccounts,例如:

public void setBalance(double newBalance) 
  {
     totalValueOfAccounts -= this.balance;
     totalValueOfAccounts += newBalance;
     this.balance = newBalance;

  }