2017-02-06 180 views
-1

因此,對於HW分配,我創建了一個BankAccount類和一個Bank類。它基本上是完整的,除了我在創建一個方法來增加每月費用有問題。我已經搜索過,雖然我發現了很多類似的例子,但沒有什麼與我有的一樣。java bankaccount程序問題的方法

這裏是BankAccount類:

public class BankAccount { 

    String owner; 
    int accountNumber; 
    double balance; 

    public BankAccount (String name, int acct){ 
     owner = name; 
     accountNumber = acct; 
    } 

    public String toString() { 
     String str = owner + " owns the account " + accountNumber + " with the balance of " + String.format("$%,.2f", balance); 
     return str; 
    } 

    public double adjust(double amt) { 
     balance = balance + amt; 
     return balance; 
    } 

    public double getBalance() { 
     return balance; 
    } 
} 

這裏是Bank.java類:

import java.util.Arrays; 

public class Bank { 

    BankAccount[] bAcct; 

    public Bank() { 
     //Constructor - will create an array that can hold up to 10 BankAccount objects. 
     bAcct = new BankAccount[10]; 
    } 

    public void addAccount(BankAccount a) { 
     //This method will take a BankAccount object as a param and place it in the next avail entry in array. 
     for(int i = 0; i < bAcct.length; i++) { 
      if (bAcct[i] == null){ 
       bAcct[i] = a; 
       break; 
      } 
     } 
    } 

    public BankAccount getAccount(int index) { 
     //will return a BankAccount object given an integer index value as a param 
     return bAcct[index]; 
    } 

    public void printAccounts() { 
     //will display all of the BankAccount objects 
     for (int i = 0; i < bAcct.length; i++) { 
     if(bAcct[i] != null) { 
      System.out.println(bAcct[i]); 
      } 
     } 
    } 

    public double monthlyFee(double f) { 
     //will take a double value as a param and apply that value to every BankAccount object 
     for (int i = 0; i < bAcct.length; i++){ 
      if(bAcct[i] !=null) { 


      } 
     } 

    } 
} 

和測試類:

public class BankTest 
{ 
    /* 
    * test - set up a bank and add accounts 
    */ 
    public static void main(String[] args) 
    { 
    // Code to test Bank and BankAccount classes 
    int errors = 0; 
    double fee = -2.95; 

    System.out.println("\nCreate bank1"); 
    Bank bank1 = new Bank(); 
    System.out.println("\nOne account"); 
    BankAccount b1 = new BankAccount("Peter Chang", 3021); 
    b1.adjust(1000.0); 
    bank1.addAccount(b1); 
    bank1.printAccounts(); 
    System.out.println("\nTwo accounts"); 
    BankAccount b2 = new BankAccount("Roddy Piper", 3049); 
    b2.adjust(2000.0); 
    bank1.addAccount(b2); 
    bank1.printAccounts(); 
    System.out.println("\nThree accounts"); 
    BankAccount b3 = new BankAccount("Leeroy Jenkins", 4028); 
    b3.adjust(3000.0); 
    bank1.addAccount(b3); 
    bank1.printAccounts(); 
    System.out.println("\nMonthly Fee"); 
    bank1.monthlyFee(fee); 
    bank1.printAccounts(); 
    System.out.println("\nErrors:"); 

    if (bank1.getAccount(0).getBalance() != 997.05) 
    { 
     errors += 1; 
     System.out.println("Balance for account at index 0 does not match $997.05"); 
    } 
    if (bank1.getAccount(1).getBalance() != 1997.05) 
    { 
     errors += 1; 
     System.out.println("Balance for account at index 1 does not match $1997.05"); 
    } 
    if (bank1.getAccount(2).getBalance() != 2997.05) 
    { 
     errors += 1; 
     System.out.println("Balance for account at index 2 does not match $2997.05"); 
    } 
    if (errors == 0) 
     System.out.println("No errors found!!!"); 
    } 
} 

我卡上Bank.java類中的monthlyFee方法。任何幫助表示讚賞!提前致謝。

+0

您希望作爲月費申請的具體操作是什麼?只是一個減法? – jlumietu

回答

2

類似的東西:

public void monthlyFee(double f) { 
     //will take a double value as a param and apply that value to every BankAccount object 
     for (int i = 0; i < bAcct.length; i++){ 
      if(bAcct[i] !=null) { 
       bAcct[i].adjust(f); 
      } 
     } 
    } 

刪除你不使用它,它是不明確你希望它返回的內容從每月費用方法的返回。順便說一句,如果我是你,我會用ListArrayList而不是Array,這將節省你一些不必要的麻煩。

+0

哦拍,這是我的問題,我太專注於我沒有想到的方法發生了什麼!我沒有想到它會返回任何東西,只是我的一個愚蠢的疏忽。謝謝! – PFKrang

2

您可以簡單地在循環中的每個帳戶上使用您的adjust方法,並提供費用金額。