// Bank.java /** * Bank represents a single Bank containing a number of BankAccounts. */ public class Bank { // Member variables: /** The array of BankAccount objects contained in this bank. */ private BankAccount[] myAccounts = new BankAccount[ 2000]; /** The number of BankAccount objects stored in the array in this bank. */ private int numberOfAccounts = 0; // Constructors: /** * Creates a Bank. */ public Bank() {} // Methods: /** * Creates an account with the name and balance, and adds it to * the bank's list of accounts. * If the name already exists, no account will be created. * @param aName The name for the new account. * @param aBalance The initial balance for the new account. */ public void createAccount( String aName, double aBalance) { BankAccount existing = this.findAccount( aName); if( existing != null) return; BankAccount anAccount = new BankAccount( aName, aBalance); this.myAccounts[ numberOfAccounts] = anAccount; this.numberOfAccounts++; } /** * Finds an account in the bank's list of accounts by name. * If no account is found, this method returns null. * @param aName The name of the BankAccount to search for. * @return The BankAccount bearing the given name, if found. */ public BankAccount findAccount( String aName) { if( aName == null) return( null); for( int index = 0; index < numberOfAccounts; index++) { BankAccount anAccount = this.myAccounts[ index]; if( aName.equals( anAccount.getName())) { return( anAccount); } } return( null); } /** * Gives a total of all the BankAccounts in the bank. * @return The sum of all the deposits in the Bank's BankAccounts */ public double totalInBank() { double total = 0.0; for( int index = 0; index < numberOfAccounts; index++) { BankAccount anAccount = this.myAccounts[ index]; total += anAccount.getBalance(); } return( total); } /** * Returns a String which represents a short summary of * all the accounts in the bank. */ public String getSummaryString() { String answer = ""; for( int index = 0; index < numberOfAccounts; index++) { BankAccount anAccount = this.myAccounts[ index]; answer += anAccount.getName() + " " + anAccount.getBalance() + "\n"; } return( answer); } }