import java.util.Random; import java.util.Iterator; import java.util.ArrayList; import java.util.Collections; /** A class to test the A11NumberedAccount class. */ public class A11 { /** Traverses the elements of an ArrayList of A11NumberedAccounts, by printing each of them to System.out, using the toString method of the A11NumberedAccount class. @param list the ArrayList @throws ClassCastException if there is an element of the ArrayList that is not an A11NumberedAccount */ public static void traverse(ArrayList list) { Iterator it = list.iterator(); while (it.hasNext()) System.out.println((A11NumberedAccount) 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 A11NumberedAccount( "Harry", 1234, 2000)); accounts.add(new A11NumberedAccount( "Mary", 2345, 3000)); accounts.add(new A11NumberedAccount( "Barry", 3456, 4000)); for (int i=1; i<= 10; i++) accounts.add(new A11NumberedAccount( "Customer"+i,i,r.nextInt(10000))); System.out.println("the unsorted " + "collection of accounts is now:"); traverse(accounts); System.out.println(); System.out.println("sorted by account number, " + " the collection is:"); Collections.sort(accounts); traverse(accounts); System.out.println(); System.out.println("sorted by account holder, " + " the collection is:"); Collections.sort(accounts, A11NumberedAccount.getCustomerComparator()); traverse(accounts); System.out.println(); System.out.println("sorted by balance, " + " the collection is:"); Collections.sort(accounts, A11NumberedAccount.getBalanceComparator()); traverse(accounts); System.out.println(); A11NumberedAccount firstAccount = (A11NumberedAccount) accounts.get(0); System.out.println("The customer" + " with the biggest balance"); 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()); System.out.println(); System.out.println("test of equals & clone:"); A11NumberedAccount accountCopy = new A11NumberedAccount( (String) firstAccount.getAccountHolder(), firstAccount.getAccountNumber(), firstAccount.getBalance()); A11NumberedAccount anotherAccount = (A11NumberedAccount) firstAccount.clone(); System.out.println(firstAccount.equals(firstAccount)); System.out.println(firstAccount.equals(anotherAccount)); System.out.println(firstAccount.equals(accountCopy)); anotherAccount.withdraw(5000); System.out.println(firstAccount.getBalance()); System.out.println(anotherAccount.getBalance()); System.out.println(firstAccount.equals(anotherAccount)); System.out.println(firstAccount.equals(accountCopy)); System.out.println(); try { Collections.sort(accounts); traverse(accounts); accounts.add(new String("oops!")); Collections.sort(accounts); traverse(accounts); } catch(ClassCastException e) { System.out.println("ill-formed account found" + " -- can't sort or traverse "); } } }