2014-04-12 77 views
0

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

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

}

回答

0

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

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

如果你永远不会增加numberOfAccounts,我不知道你期望如何得到一个值abov e 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; 

    } 
+0

我试过了,它给了我一个平均余额的第3和第4次我打电话给它,但它是错的。仍然0.0前两次我称之为 – user1054757

+0

它为一个家庭作业,教授说帐户类文件是正确的,所以我真的迷路了。 – user1054757

+0

@ user1054757,教授并不总是写出正确的代码。我要更新。 – merlin2011