/** * This class represents a bank. * * I noticed that in 3 separate methods, the user passes in an account id * and we need to search through the array to find the corresponding account. * As a result, I created a private method called idToAccount to do just this. */ public class ArrayBank { private final int INITIAL_LENGTH = 10; // These are the actual bank accounts private BankAccount[] accounts; // This is the account id for the next account to be opened private int nextAvailableAccountId; // To make this class more general, and to allow us to change how we // represent account ids in the future if we want, I have decided to // create a separate integer variable to keep track of the next available // spot in the array. private int nextOpenSpot; /** * Constructs a new bank */ public ArrayBank(){ nextOpenSpot = 0; nextAvailableAccountId = 1; accounts = new BankAccount[INITIAL_LENGTH]; } /** * Opens bank account with specified owner name and initial balance * @param owner The name of the owner of the account * @param initialBalance The initial balance in the account * @return The account id of the new account */ public int openAccount(String owner, double initialBalance){ BankAccount account1 = new BankAccount(owner, initialBalance, nextAvailableAccountId); nextAvailableAccountId++; // Note: there are many ways to test if the array is full. Here, we check if the last element // in the array is null or not. We could also have used the variable nextOpenSpot // to determine if the array is full. I.e., we could have asked if nextOpenSpot is equal // to the size of the array if(accounts[accounts.length-1] == null){ accounts[nextOpenSpot] = account1; } else{ expandArray(); accounts[nextOpenSpot] = account1; } nextOpenSpot++; return account1.getAccountId(); } /** * Returns the balance of the account or -1 if account is invalid * @param id The account id * @return The balance of the account */ public double getBalance(int id){ BankAccount theAccount = idToAccount(id); if(theAccount != null){ return theAccount.getBalance(); } return -1; } /** * Deposits money into specified account * @param id The account id * @param amount The amount to be deposited * @return The new balance on the account or -1 if account id is invalid */ public double deposit(int id, double amount){ BankAccount theAccount = idToAccount(id); if(theAccount != null){ return theAccount.deposit(amount); } return -1; } /** * Withdraws money from the account if sufficient funds are available * @param id The account id * @param amount The amount to be deposited * @return The new balance on the account or -1 if account id is invalid */ public double withdraw(int id, double amount){ BankAccount theAccount = idToAccount(id); if(theAccount != null){ return theAccount.withdraw(amount); } return -1; } /** * Returns a string representation of all of the * accounts opened with the bank * @param String A string representation of the bank */ public String toString(){ String str = ""; for(int i = 0; i < nextOpenSpot; i++){ str += accounts[i].toString() + "\n"; } return str; } /*---------------------------------- * PRIVATE HELPER METHODS *----------------------------------*/ /** * This takes an account id and returns the corresponding bank account * or returns null if no such bank account exists * @param id The id of the account * @return The bank account or null if no such account exists */ private BankAccount idToAccount(int id){ for(int i = 0; i < nextOpenSpot; i++){ if(accounts[i].getAccountId() == id){ return accounts[i]; } } return null; } /** * This method doubles the size of the array. Copies over all the existing accounts. * Then updates the instance variables as necessary */ private void expandArray(){ BankAccount[] temp = new BankAccount[accounts.length*2]; for(int i = 0; i < accounts.length; i++){ temp[i] = accounts[i]; } accounts = temp; } }