-->

How to make an array of objects that creates new a

2019-08-24 20:32发布

问题:

I need help on a programming assignment. The class of BankAccount already exists and works as it should. I have never been asked to place objects into arrays before, and am having trouble doing so.

I have started with the following:

   public class Bank
   {
   private BankAccount[] Bank;

   public Bank(BankAccount[] Bank)
   {
      BankAccount[] b1 = new BankAccount[10];
   }

Although it compiles, it is wrong. I am not sure where to go.

The following are the requirements of the code that I am currently stuck on.

  • An object of class Bank can hold up to 10 BankAccount objects.
  • The constructor for the Bank object will create an array that can hold up to 10 BankAccount objects.

The following code is the test program that our professor included with the assignment that we must use:

System.out.println("\nCreate bank1");
Bank bank1 = new Bank();
System.out.println("\nOne account");
BankAccount b1 = new BankAccount("Joe Mac", 1234);
b1.adjust(1000.0);
bank1.addAccount(b1);
bank1.printAccounts();
System.out.println("\nTwo accounts");
BankAccount b2 = new BankAccount("Sally Ride", 2345);
b2.adjust(2000.0);
bank1.addAccount(b2);
bank1.printAccounts();
System.out.println("\nThree accounts");
BankAccount b3 = new BankAccount("Pat Armstrong", 3456);
b3.adjust(3000.0);
bank1.addAccount(b3);
bank1.printAccounts();
System.out.println("\nMonthly Fee");
bank1.monthlyFee(fee);
bank1.printAccounts();
System.out.println("\nErrors:");

Help would be immensely appreciated. Thank you.

回答1:

You just have to add a method to add a new account and test that it doesn't go more bigger than 10:

 public class Bank
   {
   private BankAccount[] accounts; //Don't name variables with uppercase
   private int accountsPointer; //This is going to keep track of how many accounts are there in the array

   public Bank() //The constructor doesn't need to accept any bank account since it'll start as empty
   {
    accounts = new BankAccount[10]; //Here we initialize the array
    accountsPointer = 0; //Here the pointer starts as 0 since the array is empty
   }
   public void addAccount(BankAccount account){
       accountsPointer++;
       if(accountsPointer<10){ //We test it here so it won't throw an out of bounds exeption
           accounts[accountsPointer-1]=account;//It assigns the account to the next empty space on the array
       }
   }


回答2:

You can use below code to solve your problem.

class Bank {
    private static final int MAX_NO_OF_ACCOUNT = 10;
    private int pointer = 0;
    private BankAccount[] bank;

    public Bank() {
        bank = new BankAccount[MAX_NO_OF_ACCOUNT];
    }

    public void addAccount(BankAccount bankAccount) throws Exception {
        if (pointer == MAX_NO_OF_ACCOUNT) {
            throw new Exception("No of bank account reached max no"); // you should own custom exception
        } else {
            bank[pointer] = bankAccount;
            pointer++;
        }
    }

}

Explanation : As per requirements, one Bank object can hold max 10 account. I have put it in available name MAX_NO_OF_ACCOUNT as I know it is constant. static final variables in Java known as constant. Also, it gives the flexibility to change the max no of account easily (as that may use in multiple places). I have to make it private as I don't want to expose that value outside the class. However, I believe that should be more configuration driven. The pointer variable will always point to the last added element index so that I can check before add. Let come to the constructor. When I carate a Bank object I have initialized the BankAccount[] array which can hold MAX_NO_OF_ACCOUNT of account.

Now addAccount method: I made the addAccount method as the checked exception as I think the user of that API/Class can add more than max no of account. If such case happens we force to handle such situation.