import java.util.Random; /** * 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; private int nextOpenSpot; // next available spot in the array private BankAccount[] accounts; private Random rng; /** * Constructs a new bank */ public ArrayBank(){ nextOpenSpot = 0; rng = new Random(); 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 password The password for the account * @param initialBalance The initial balance in the account * @return The account id of the new account */ public String openAccount(String owner, String password, double initialBalance){ BankAccount account = new BankAccount(owner, password, getAccountId()); account.deposit(initialBalance); // Check if the array is full. If so, expand if(nextOpenSpot == accounts.length){ expandArray(); } // There is now room in the array, so store the new account accounts[nextOpenSpot] = account; nextOpenSpot++; return account.getAccountId(); } /** * Returns the balance of the account * @param id The account id * @return The balance of the account or -1 if account is invalid */ public double getBalance(String id){ BankAccount theAccount = idToAccount(id); if(theAccount != null){ return theAccount.checkBalance(); } 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(String 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(String id, double amount){ BankAccount theAccount = idToAccount(id); if(theAccount != null){ return theAccount.withdraw(amount); } return -1; } /** * Transfers money between two accounts * @param fromId The account id for the sending account * @param toId The account id for the receiving account * @param amount The amount to be transfered */ public void transfer(String fromId, String toId, double amount){ BankAccount fromAccount = idToAccount(fromId); BankAccount toAccount = idToAccount(toId); if(fromAccount != null && toAccount != null){ fromAccount.withdraw(amount); toAccount.deposit(amount); } } /** * 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 *----------------------------------*/ /** * Generates a random account id */ private String getAccountId(){ final int UPPERBOUND = 888888889; final int OFFSET = 111111111; int id = rng.nextInt(UPPERBOUND) + OFFSET; String theStringForm = "" + id; return theStringForm; } /** * 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(String id){ for(int i = 0; i < nextOpenSpot; i++){ String currAccountId = accounts[i].getAccountId(); if(currAccountId.equals(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; } }