import java.util.Random; import java.util.List; import java.util.Set; import java.util.Iterator; /** Tests the bank classes of Assignment 9, CS 49J, SJSU, Spring 2009 @author Jeff Smith @version final version for the assigment */ public class A9 { // the number of accounts per bank that will // be used in certain tests. private static int numberOfAccounts = 1000; // for recording of the start time of a test // sequence private static long startTime; // a list of names for account holders private static String[] holderNames = new String[numberOfAccounts]; /** Initialize the timer */ public static void startTimer() { startTime = System.nanoTime(); } /** Get the elapsed time in microseconds since the timer was last initialized @return this elapsed time */ public static long getElapsedTime() { return (System.nanoTime()-startTime)/1000; } /** Construct a random string of length 5 with vowels in the 2nd and 4th positions. Not all letters are allowed to occur. @param r a Random object to use for the random selection @return the randomly generated string */ private static String getRandomName(Random r) { String vowels = "aeiou"; String cons = "bcdfghklmnprstvz"; String consUpper = "BCDFGHKLMNPRSTVZ"; StringBuffer buffer = new StringBuffer(); buffer.append( consUpper.charAt(r.nextInt(consUpper.length()))); buffer.append(vowels.charAt(r.nextInt(vowels.length()))); buffer.append(cons.charAt(r.nextInt(cons.length()))); buffer.append(vowels.charAt(r.nextInt(vowels.length()))); buffer.append(cons.charAt(r.nextInt(cons.length()))); return new String(buffer); } /** Tests the methods of the Bank class on a given bank. Prints various intermediate total balances. @param bank the given bank */ public static void test1(Bank bank) { try { System.out.println(bank.getTotalBalance()); bank.addCheckingAccount( "James", 0); } catch (IllegalArgumentException e) { System.out.println(e.getMessage()); } try { bank.addSavingsAccount( "Madison", 0, 2.0); } catch (IllegalArgumentException e) { System.out.println(e.getMessage()); } try { bank.addCheckingAccount( "Jackson", -1); } catch (IllegalArgumentException e) { System.out.println(e.getMessage()); } try { bank.addSavingsAccount( "Tyler", -100, 3.0); } catch (IllegalArgumentException e) { System.out.println(e.getMessage()); } try { bank.addSavingsAccount( "Taylor", 0, -2.0); } catch (IllegalArgumentException e) { System.out.println(e.getMessage()); } try { bank.addSavingsAccount( null, -100, 3.0); } catch (IllegalArgumentException e) { System.out.println(e.getMessage()); } try { bank.addSavingsAccount( null, 0, -2.0); } catch (IllegalArgumentException e) { System.out.println(e.getMessage()); } bank.deposit(100, new AccountHolder("James")); bank.deposit(1000, new AccountHolder("Madison")); System.out.println(bank.getTotalBalance()); bank.addInterestToAll(); System.out.println(bank.getTotalBalance()); bank.deductFeesFromAll(); System.out.println(bank.getTotalBalance()); bank.withdraw(1, new AccountHolder("James")); bank.withdraw(1, new AccountHolder("Madison")); bank.withdraw(1, new AccountHolder("Jeff")); bank.deposit(4, new AccountHolder("Jeff")); System.out.println(bank.getTotalBalance()); System.out.println( bank.getBalance(new AccountHolder("Jeff"))); if (bank.removeAccount(new AccountHolder("Jeff"))) System.out.println("account removed"); else System.out.println("can't remove account"); try { bank.withdraw(1000, new AccountHolder("James")); } catch (IllegalArgumentException e) { System.out.println(e.getMessage()); } System.out.println(bank.getTotalBalance()); } /** Tests the methods of the Bank class on a given bank by creating many accounts @param bank the given bank @param isReportWanted tells whether attempts at duplicate entry of names are to be reported */ public static void test2(Bank bank, boolean isReportWanted) { boolean wasAdded; for (int i=0; i holders = bank.getAccountHolders(); if (isReportWanted) { Iterator it = holders.iterator(); for (int i=0; i holders = bank.getSortedAccountHolders(); if (isReportWanted) { Iterator it = holders.iterator(); for (int i=0; i