2017-02-06 140 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方法,并提供费用金额。