/** * This class represents a more realistic bank. * * When a user opens a bank account, we internally store the bank account and return only * the account id back to the user. * * When the user wants to perform a transaction (e.g. withdraw or deposit) they must * provide their account id. * */ 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 account = new BankAccount(owner, initialBalance, nextAvailableAccountId); nextAvailableAccountId++; // Note: there are many ways to test if the array is full. Here, we check if the // nextOpenSpot is equal to the size of the array. We could also have checked if // the last element in the array is null or not. if(nextOpenSpot < accounts.length){ accounts[nextOpenSpot] = account; } else{ expandArray(); accounts[nextOpenSpot] = account; } nextOpenSpot++; return account.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; } }