import java.util.Random; import java.util.Iterator; import java.util.ArrayList; import java.util.Collections; /** A class to test the NumberedAccount class. */ public class A10 { /** Traverses the elements of an ArrayList of Numbered Accounts, by printing each of them to System.out, using the toString method of the Numbered Account class. @param list the ArrayList @throws ClassCastException if there is an element of the ArrayList that is not a NumberedAccount */ public static void traverse(ArrayList list) { Iterator it = list.iterator(); while (it.hasNext()) System.out.println((NumberedAccount) it.next()); System.out.println(); } /** Tests the methods of the NumberedAccount class. @param args not used */ public static void main(String[] args) { Random r = new Random(11); ArrayList accounts = new ArrayList(); accounts.add(new NumberedAccount( "Harry", 1234, 2000)); accounts.add(new NumberedAccount( "Mary", 2345, 3000)); accounts.add(new NumberedAccount( "Barry", 3456, 4000)); System.out.println("sorting the " + "collection of accounts so far gives:"); ArrayList cloneOfAccounts = (ArrayList) accounts.clone(); Collections.sort(cloneOfAccounts); traverse(cloneOfAccounts); System.out.println(); for (int i=1; i<= 10; i++) accounts.add(new NumberedAccount( "Customer"+i,i,r.nextInt(10000))); System.out.println("the unsorted " + "collection of accounts is now:"); traverse(accounts); System.out.println(); System.out.println("and the sorted " + "collection of accounts is now:"); Collections.sort(accounts); traverse(accounts); System.out.println(); NumberedAccount firstAccount = (NumberedAccount) accounts.get(0); System.out.println("The first customer" + " in alphabetical order"); System.out.print(" is " + firstAccount.getAccountHolder()); System.out.print(" with account #" + firstAccount.getAccountNumber()); System.out.println(" and balance $" + firstAccount.getBalance()); firstAccount.deposit(2000); System.out.print("After depositing " + "$2000, this customer's balance is $"); System.out.println(firstAccount.getBalance()); firstAccount.withdraw(1000); System.out.print("After withdrawing " + "$1000, this customer's balance is $"); System.out.println(firstAccount.getBalance()); } }