java bankaccount程序问题与方法

时间:2017-02-06 08:46:39

标签: java class methods bank

因此,对于HW分配,我创建了BankAccount类和Bank类。它基本上是完整的,除了我在创建添加月费的方法时遇到问题。我一直在寻找,虽然我已经找到了大量类似的例子,但没有任何与我的相关的东西。

这是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方法。任何帮助表示赞赏!提前谢谢。

2 个答案:

答案 0 :(得分: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);
            }
        }
    }

在您使用它时删除了monthlyFee方法的返回值,并且它不清楚您希望它返回的内容。

顺便说一句,如果我是你,我会使用ListArrayList代替数组,这样可以省去一些不必要的麻烦。

答案 1 :(得分:2)

您可以在循环中的每个帐户中使用adjust方法,并提供费用金额。