import java.util.Arrays; // File: Bank.java // Purpose: Demonstrates the basics of array processing // This is a new version of our old friend the Bank class from previous units. // Although the implementation has been changed from an ArrayList to an array, // all software that works with the old version is guaranteed to work // correctly with this one! // This is an excellent example of the OOP principle of "information hiding" // and one of the major reasons why the user of a Java class is forbidden // to access the implementation directly (i.e. why instance variables are // declared private /** This bank contains a collection of bank accounts. */ public class Bank { private BankAccount[] accounts ; // a list of BankAccount objects private int count ; // number of accounts in this Bank private static final int MAX_SIZE = 100 ; // capacity of array /** Constructs a bank with no bank accounts. */ public Bank() { accounts = new BankAccount[MAX_SIZE] ; // accounts can store up to MAX_SIZE BankAccount objects } /** Adds an account to this bank. @param a the account to add */ public void addAccount(BankAccount newAccount) { // first, must check to see if array if full if ( count == accounts.length ) // array is full { this.resize() ; // make it bigger } // add newAccount to the Bank, in element "count" accounts[count] = newAccount ; count++ ; // now we have one more account } // utility method to make the array bigger when full private void resize() { // add another MAX_SIZE elements accounts = Arrays.copyOf(accounts, accounts.length + MAX_SIZE) ; } /** Print data for all accounts of this Bank */ public void printList() { // print headings System.out.printf("%15s%20s%n","Account Number","Account Balance") ; System.out.printf("%15s%20s%n","==============","===============") ; // for each account in this bank for ( int i = 0 ; i < count ; i++ ) { BankAccount current = accounts[i] ; // get next account // print account number and balamce to screen System.out.printf("%15s $%14.2f%n", current.getAccountNumber(), current.getBalance()) ; } } /** Gets the sum of the balances of all accounts in this bank. @return the sum of the balances */ public double getTotalBalance() { double total = 0; // for each account in this bank for ( int index = 0 ; index < count ; index++) { BankAccount current = accounts[index] ; // get the account total = total + current.getBalance() ; // add its balance to accum } return total ; } /** Counts the number of bank accounts whose balance is at least a given value. @param atLeast the balance required to count an account @return the number of accounts having at least the given balance */ public int count(double atLeast) { int matches = 0 ; for ( int i = 0 ; i < count ; i++ ) { BankAccount current = accounts[i] ; // get next account if (current.getBalance() >= atLeast) // found one... matches++ ; // ...increment counter } return matches ; } /** Linear search to find a bank account with a given account number. @param acctNum the number to find @return the account with the given number, or null if there is no such account */ public BankAccount find(String acctNum) { for ( int i = 0 ; i < count ; i++ ) { BankAccount current = accounts[i] ; // get the account String accountNum = current.getAccountNumber() ; // get account number if ( accountNum.equals(acctNum) ) // found a match return current ; // we're done! } return null ; // No match in this Bank } /** Gets the bank account with the largest balance. @return the account with the largest balance, or null if the bank has no accounts */ public BankAccount getMaximum() { if (count == 0) // if no accounts, return null return null; // assume first account is the largest BankAccount largestYet = accounts[0] ; // for all other accounts for ( int i = 1; i < count ; i++ ) { BankAccount current = accounts[i] ; // get next account // is current account > highest so far? if (current.getBalance() > largestYet.getBalance()) largestYet = current ; } return largestYet ; } }