2014-08-31 55 views
0

我是一個學生剛剛完成我的第一年學習Java,並有一個問題「創建一個方法,返回在銀行總資金?」在考試中。我已經包括下面的類。我得到了大部分正確的方法,包括addAccount(),getBalance(),withdraw()等,但不確定這個答案是什麼。這可能比我想象的簡單並且更簡單(我必須使用for循環或某種類型的兩個循環),但只是爲了澄清合計總和的正確方法。這也出現在一家雜貨店的C#作業中,客戶從不同的產品(例如水果蔬菜等)購買了貨物,並且必須計算總計。找到一個銀行賬戶的總數

預先感謝您...

保羅

代碼: 超類:

/** 
    A bank account has a balance that can be changed by 
    deposits and withdrawals. 
*/ 
public class BankAccount 
{ 
    //Declare balance field 
    private double balance; 

    /** 
     Constructs a bank account with a zero balance. 
    */ 
    public BankAccount() 
    { 
     balance = 0; 
    } 

    /** 
     Constructs a bank account with a given balance. 
     @param initialBalance the initial balance 
    */ 
    public BankAccount(double initialBalance) 
    { 
     balance = initialBalance; 
    } 

    /** 
     Deposits money into the bank account. 
     @param amount the amount to deposit 
    */ 
    public void deposit(double amount) 
    { 
     balance = balance + amount; 
    } 

    /** 
     Withdraws money from the bank account. 
     @param amount the amount to withdraw 
    */ 
    public void withdraw(double amount) 
    { 
     if (balance >= amount) 
     { 
     balance = balance - amount; 
     } 
     else 
     { 
      System.out.println("Withdrawal error: insufficent funds"); 
     } 
    } 

    /** 
     Gets the current balance of the bank account. 
     @return the current balance 
    */ 
    public double getBalance() 
    { 
     return balance; 
    } 

    /** 
     Transfers money from the bank account to another account 
     @param amount the amount to transfer 
     @param other the other account 
    */ 
    public void transfer(double amount, BankAccount other) 
    { 
     withdraw(amount); 
     other.deposit(amount); 
    } 

    public String toString() 
    { 
     return "Your Balance: "+ balance; 
    } 
} 

子類的支票帳戶:

/** 
    A checking account that charges transaction fees. 
*/ 
public class CheckingAccount extends BankAccount 
{ 
    private int transactionCount; 
    private int transaction; 

    private static final int FREE_TRANSACTIONS = 0; 
    private static final double TRANSACTION_FEE = 2.0; 

    /** 
     Constructs a checking account with a given balance. 
     @param initialBalance the initial balance 
    */ 
    public CheckingAccount(double initialBalance) 
    { 
     // Construct superclass 
     super(initialBalance); 

     // Initialize transaction count 
     transactionCount = 0; 
    } 

    public void deposit(double amount) 
    { 
     transactionCount++; 
     // Now add amount to balance 
     super.deposit(amount); 
    } 

    public void withdraw(double amount) 
    { 
     transactionCount++; 
     // Now subtract amount from balance 
     super.withdraw(amount); 
    } 

    /** 
     Deducts the accumulated fees and resets the 
     transaction count. 
    */ 
    public void deductFees() 
    { 
     if (transactionCount > FREE_TRANSACTIONS) 
     { 
     double fees = TRANSACTION_FEE * 
       (transactionCount - FREE_TRANSACTIONS); 
     super.withdraw(fees); 
     } 
     transaction = transactionCount; 
    } 

    public String toString() 
    { 

     return super.toString() + "\t Your Transactions: "+ transaction; 
    } 

} 

子類儲蓄帳戶:

/** 
    An account that earns interest at a fixed rate. 
*/ 
public class SavingsAccount extends BankAccount 
{ 
    private double interestRate; 

    /** 
     Constructs a bank account with a given interest rate. 
     @param rate the interest rate 
    */ 
    public SavingsAccount(double rate) 
    { 
     interestRate = rate; 
    } 

    /** 
     Constructs a bank account with a given interest rate. 
     @param rate the interest rate 
    */ 
    public SavingsAccount(double rate, double initBalance) 
    { 
     super(initBalance); 
     interestRate = rate; 
    } 

    /** 
     Adds the earned interest to the account balance. 
    */ 
    public void addInterest() 
    { 
     double interest = getBalance() * interestRate + 100; 
     deposit(interest); 
    } 

     public String toString() 
    { 

     return super.toString() + "\t Your Interest rate: "+ interestRate; 
    } 

} 

回答

0

你說得對。

如果您擁有BankAccounts或其子類的集合,那麼可以在for循環中將它們簡單地相加。

可以說,你有這樣的事情(我假設有包含一個函數,給了我所有賬戶以某種方式銀行對象):

Collection<BankAccount> accounts = Bank.getAccounts(); 
Double sum = 0.0; 
for (BankAccount account : accounts) { 
    sum += account.getBalance(); 
} 
+0

感謝那些人,並給我一些示例代碼混亂,我很高興知道我在正確的軌道上,但很高興有第二個意見。 – Paul66 2014-08-31 18:45:49

+0

然後請接受答案;-) – mcdikki 2014-08-31 19:53:30

0

您正在思考一個循環的正確軌道。只需總結餘額。

public static double totalBalance(Collection<BankAccount> accounts){ 
    double sum = 0.0; 
    for(BankAccount b : accounts){ 
     sum += b.getBalance(); 
    } 
    return sum; 
} 
+0

嗯,我期望爲getBalance()方法總是會導致帳戶的正確balnce,以及由Bankcontroler等經常調用的addInterest()和dectutFees()方法。如果您每次檢查餘額時都使用它們,則會改變餘額,餘額將取決於您檢查餘額的次數。這對我來說似乎是錯誤的... – mcdikki 2014-08-31 15:30:07

+0

你可能是對的。調用deductFees()通常不會受到傷害,因爲如果沒有更多事務,它不會更改餘額。但是,我現在看到addInterest始終增加了相同的興趣量,而不考慮自上次調用後已經過去的「時間」。我會擺脫那部分。 – Mshnik 2014-08-31 16:03:16

相關問題