import chn.util.*; class CheckingAccount { final double CHECK_CHARGE = 0.15; double myBalance; double myAmount; /** Constructs a bank account with a zero balance */ public CheckingAccount() { myBalance = 0; } /** Constructs a bank account with a given balance @param initialBalance the initial balance */ public CheckingAccount(double initialBalance) { myBalance = initialBalance; if(myBalance < 0) throw new IllegalArgumentException(); } /** Deposits money into the bank account. @param amount the amount to deposit */ public void deposit(double amount) { if(amount < 0) throw new IllegalArgumentException(); myBalance += amount; } /** Withdraws money from the bank account. @param amount the amount to withdraw */ public void withdraw(double amount2) { myAmount = amount2; if(amount2 < 0 || myAmount > myBalance) throw new IllegalArgumentException(); myBalance = myBalance - myAmount - CHECK_CHARGE; } /** Gets the current balance of the bank account. @return the current balance */ public double getBalance() { return myBalance; } } public class ErrorCheck { public static void main(String[] args) { //variables CheckingAccount errorCheck = new CheckingAccount(); ConsoleIO console = new ConsoleIO(); double balance = 0; double deposit = 0; double withdraw = 0; int count = 0; int count1 = 0; int count2 = 0; //starting balance while(count == 0) { System.out.print("Please enter the starting balance: "); balance = console.readDouble(); //try-catch statement try { errorCheck = new CheckingAccount(balance); count++; System.out.println("Account opened with balance of " + balance); } catch(IllegalArgumentException e) { System.out.println("Error CheckingAccount: negative initial balance"); } } //deposit while(count1 == 0) { System.out.print("Please enter the amount to deposit: "); deposit = console.readDouble(); //try-catch statement try { errorCheck.deposit(deposit); count1++; System.out.println("Deposit made. Current account balance = " + errorCheck.getBalance()); } catch(IllegalArgumentException e) { System.out.println("Error deposit: negative amount"); } } //withdraw while(count2 == 0) { System.out.print("Please enter the amount to withdraw: "); withdraw = console.readDouble(); //try catch statement try { errorCheck.withdraw(withdraw); count2++; System.out.println("Withdrawal made. Current account balance = " + errorCheck.getBalance()); } catch(IllegalArgumentException e) { System.out.println("Error Withdraw: Illegal Amount"); } } System.out.println("Thank you for using ErrorFreeChecking...goodbye!"); } }