import java.io.*; import java.util.*; class DepositAccountMain8 // Version of DepositAccountMain7 with recursive // supplementary static methods { public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); int sum = getPosInt(in,"","Initial Deposit: "); double interest = getDouble(in,"","Interest Rate%: ")/100; DepositAccountB acc = new DepositAccountB(sum,interest); char command; do { StringTokenizer line; String num; do { System.out.print("Command: "); line = new StringTokenizer(in.readLine()); } while(!line.hasMoreTokens()); command = line.nextToken().charAt(0); switch(command) { case 'b': System.out.println("Balance is "+acc.balance()); break; case 'n': acc.update(); System.out.println("Start month with "+acc.balance()); break; case 'd': num=line.hasMoreTokens()?line.nextToken():""; sum=getPosInt(in,num,"Enter Amount: "); acc.deposit(sum); break; case 'w': num=line.hasMoreTokens()?line.nextToken():""; sum=getPosInt(in,num,"Enter amount: "); try { acc.withdraw(sum); } catch(AccountException e) { System.out.print("Withdrawal refused: "); if(e.getMessage().equals("overdraw")) System.out.println("would make account overdrawn"); else System.out.println("previous withdrawal this month"); } break; case 'q': break; default: System.out.println("Invalid command"); } } while(command!='q'); } private static int getPosInt(BufferedReader in,String str,String prompt) throws IOException // If str represents a positive number, return that number. // Otherwise, print prompt then read a string from in, and // repeat doing so until a positive number is read { int n=0; str=getNonNullString(in,str,prompt); try { n=Integer.parseInt(str); if(n<=0) System.out.println("Non-positive amount entered"); } catch(NumberFormatException e) { System.out.println("Not a valid figure"); } if(n>0) return n; else return getPosInt(in,"",prompt); } private static double getDouble(BufferedReader in,String str,String prompt) throws IOException // If str represents a floating point number, return that number. // Otherwise, print prompt then read a string from in, and // repeat doing so until a floating number is read { double x; str=getNonNullString(in,str,prompt); try { return Double.parseDouble(str); } catch(NumberFormatException e) { System.out.println("Not a valid figure"); return getDouble(in,"",prompt); } } private static String getNonNullString(BufferedReader in,String str,String prompt) throws IOException { if(str.equals("")) { System.out.print(prompt); str=in.readLine(); return getNonNullString(in,str,prompt); } else return str; } }