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.
You just have to add a method to add a new account and test that it doesn't go more bigger than 10:
You can use below code to solve your problem.
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.