Skip to content

Commit

Permalink
added a check to withdraw only whats available in balance
Browse files Browse the repository at this point in the history
  • Loading branch information
manushridiv committed Mar 15, 2023
2 parents 2ba8c05 + bf6b481 commit f7867d3
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 14 deletions.
2 changes: 1 addition & 1 deletion data/save.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
jen;900.0
jen;0.0
1 change: 1 addition & 0 deletions src/main/java/seedu/bankwithus/BankWithUs.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ public static void main(String[] args) {
try {
new BankWithUs(FILE_PATH).run();
} catch (IOException e) {
return;
}
}
}
21 changes: 16 additions & 5 deletions src/main/java/seedu/bankwithus/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ public Parser(BankWithUs bwu) {
this.bwu = bwu;
}

public float parseWithdrawAmt(String args) {
float withdrawAmt = Float.parseFloat(args);
float currBal = bwu.accounts.accounts.get(0).balance;
float final_bal = currBal-withdrawAmt;
return final_bal;
}

/**
* Parses the user input into command and arguments.
*/
Expand All @@ -32,18 +39,22 @@ public void parseUserInput(String input) throws CommandNotFoundException {
screen.viewAccount(accDetails);
break;
case "withdraw":
float withdrawAmt = Float.parseFloat(args);
float currBal = bwu.accounts.accounts.get(0).balance;
float final_bal = currBal-withdrawAmt;
bwu.accounts.accounts.get(0).setBalance(final_bal);
System.out.println("u have $" + String.valueOf(final_bal) + " remaining!");
float final_bal = parseWithdrawAmt(args);
if(final_bal > -1) {
bwu.accounts.accounts.get(0).setBalance(final_bal);
screen.showBal(final_bal);
} else {
System.out.println("You do not have sufficient Balance");
}
break;
default:
throw new CommandNotFoundException();
}

}



/**
* This method reads any existing file and add the saved data
* into current programme
Expand Down
18 changes: 12 additions & 6 deletions src/main/java/seedu/bankwithus/Ui.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void createScanner() {

/**
* Gets the next line of user input
*
*
* @return the next of user input
*/

Expand All @@ -56,14 +56,20 @@ public void closeScanner() {
this.scanner.close();
}

public void printLine() {
System.out.println("----------------------------");
}

public void viewAccount(String accDetails) {
String name = accDetails.split(";")[0];
String bal = accDetails.split(";")[1];
System.out.println("----------------------------");
printLine();
System.out.println("Name: " + name);
System.out.println("Balance: $"+bal);
System.out.println("----------------------------");
System.out.println("Balance: $" + bal);
printLine();
}
}


public void showBal(float finalBal) {
System.out.println("You have $" + String.valueOf(finalBal) + " remaining!");
}
}
2 changes: 0 additions & 2 deletions src/test/java/seedu/bankwithus/StorageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import org.junit.jupiter.api.Test;

import java.util.Scanner;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;

public class StorageTest {
Expand Down
1 change: 1 addition & 0 deletions text-ui-test/data/save.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
june;1000.0

0 comments on commit f7867d3

Please sign in to comment.