0

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.

Brenton K
  • 7
  • 2
  • what is your question ? – Ravi Feb 03 '18 at 17:20
  • Add a method "addAccount(BankAccount)" to your Bank class and do the array work there. – Sascha Feb 03 '18 at 17:22
  • How to make an array of objects, like the one I tried to make here. I am not sure how. – Brenton K Feb 03 '18 at 17:23
  • Don't name your variables with uppercase, that is very confusing. Don't name a variable for BankAccounts Bank, that's super confusing, especially in a class named Bank. An Array behaves in many parts the same, as other types, so if you declare b1 in the constructor, it's visibility is restricted to to the constructor. Have you ever written a class without an array member? – user unknown Feb 03 '18 at 19:27

2 Answers2

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
       }
   }
Daniel Diment
  • 110
  • 1
  • 8
  • 1
    This will not solve the problem. accounts.lenght!=10 always will be false.So it never enters the in the if condition. Your code will not compile also as there is no size available over Array. – Amit Bera Feb 03 '18 at 17:48
  • @AmitBera You are right, I didn't realize that as the array is already initialized it would always be 10, thank you. – Daniel Diment Feb 03 '18 at 17:55
  • 1
    Still, there is some error. What will happens when this "accounts[accounts.size]=account;" line executes. This is not aligned with the requirements and also it will throw ArrayIndexOutOfBoundsException. – Amit Bera Feb 03 '18 at 18:11
  • @AmitBera You are correct again, I forgot to correct that, thanks again. – Daniel Diment Feb 03 '18 at 18:14
  • 1
    There is still some issue. What will happen when we add the first account to a bank object? It will try to add the account to the -1 index of the array. So, accounts[accountsPointer-1]=account should be accounts[accountsPointer]=account; – Amit Bera Feb 03 '18 at 18:19
  • True the pointer should be increased before, thank you, I'm more used to work with lists, so this certainly helps. – Daniel Diment Feb 03 '18 at 18:22
  • Still, you need to improve the logic as in present code. It will skip the 10th element and will add 11 th element. – Amit Bera Feb 03 '18 at 18:38
0

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.

Amit Bera
  • 7,075
  • 1
  • 19
  • 42