diff --git a/src/main/java/seedu/bankwithus/Parser.java b/src/main/java/seedu/bankwithus/Parser.java index c65e6fdd52..4e0405426b 100644 --- a/src/main/java/seedu/bankwithus/Parser.java +++ b/src/main/java/seedu/bankwithus/Parser.java @@ -1,6 +1,7 @@ package seedu.bankwithus; import seedu.bankwithus.exceptions.CommandNotFoundException; +import seedu.bankwithus.exceptions.NegativeAmountException; import java.io.File; import java.io.IOException; @@ -13,11 +14,14 @@ public Parser(BankWithUs bwu) { this.bwu = bwu; } - public float parseWithdrawAmt(String args) { + public float parseWithdrawAmt(String args) throws NegativeAmountException { float withdrawAmt = Float.parseFloat(args); + if (withdrawAmt < 0) { + throw new NegativeAmountException(); + } float currBal = bwu.accounts.accounts.get(0).balance; - float final_bal = currBal-withdrawAmt; - return final_bal; + float finalBal = currBal-withdrawAmt; + return finalBal; } /** @@ -31,24 +35,30 @@ public void parseUserInput(String input) throws CommandNotFoundException { String args = split.length == 2 ? split[1] : ""; Ui screen = new Ui(); switch (command) { - case "exit": - bwu.isExitEntered = true; - break; - case "view-account": - String accDetails = bwu.accounts.getAllAccountDetails(); - screen.viewAccount(accDetails); - break; - case "withdraw": - float final_bal = parseWithdrawAmt(args); - if(final_bal > -1) { - bwu.accounts.accounts.get(0).setBalance(final_bal); - screen.showBal(final_bal); + case "exit": + bwu.isExitEntered = true; + break; + case "view-account": + String accDetails = bwu.accounts.getAllAccountDetails(); + screen.viewAccount(accDetails); + break; + case "withdraw": + try { + float finalBal = parseWithdrawAmt(args); + if(finalBal >= 0) { + bwu.accounts.accounts.get(0).setBalance(finalBal); + screen.showBal(finalBal); } else { - System.out.println("You do not have sufficient Balance"); + screen.showInsufficientBalanceMessage(); } - break; - default: - throw new CommandNotFoundException(); + } catch (NumberFormatException e) { + screen.showNumberFormatError(); + } catch (NegativeAmountException e) { + screen.showNegativeAmountError(); + } + break; + default: + throw new CommandNotFoundException(); } } @@ -71,5 +81,6 @@ public void parseSavedFile(AccountList list) throws IOException { String balance = splitDetails[1]; list.addAccount(name, balance); } + myReader.close(); } } diff --git a/src/main/java/seedu/bankwithus/Ui.java b/src/main/java/seedu/bankwithus/Ui.java index 3b7782bcb6..4aefc40ab6 100644 --- a/src/main/java/seedu/bankwithus/Ui.java +++ b/src/main/java/seedu/bankwithus/Ui.java @@ -72,4 +72,12 @@ public void viewAccount(String accDetails) { public void showBal(float finalBal) { System.out.println("You have $" + String.valueOf(finalBal) + " remaining!"); } -} \ No newline at end of file + + public void showNegativeAmountError() { + System.out.println("Negative number entered!"); + } + + public void showInsufficientBalanceMessage() { + System.out.println("You do not have sufficient Balance"); + } +} diff --git a/src/main/java/seedu/bankwithus/exceptions/NegativeAmountException.java b/src/main/java/seedu/bankwithus/exceptions/NegativeAmountException.java new file mode 100644 index 0000000000..277cd19097 --- /dev/null +++ b/src/main/java/seedu/bankwithus/exceptions/NegativeAmountException.java @@ -0,0 +1,8 @@ +package seedu.bankwithus.exceptions; + +/** + * Thrown when negative number is entered when not supposed to + */ +public class NegativeAmountException extends Exception { + +} diff --git a/text-ui-test/data/save.txt b/text-ui-test/data/save.txt deleted file mode 100644 index eed34ab1ea..0000000000 --- a/text-ui-test/data/save.txt +++ /dev/null @@ -1 +0,0 @@ -june;1000.0