diff --git a/src/main/java/rocks/zipcode/atm/CashMachine.java b/src/main/java/rocks/zipcode/atm/CashMachine.java index d6f8f74..f23ca45 100644 --- a/src/main/java/rocks/zipcode/atm/CashMachine.java +++ b/src/main/java/rocks/zipcode/atm/CashMachine.java @@ -13,6 +13,7 @@ public class CashMachine { private final Bank bank; private AccountData accountData = null; + private String msg; public CashMachine(Bank bank) { this.bank = bank; @@ -22,14 +23,29 @@ public CashMachine(Bank bank) { accountData = data; }; + public String getMsg(){ //method to return the error msg on line 92 + return msg; + + } + public void login(int id) { - tryCall( + tryCall( () -> bank.getAccountById(id), update ); } - public void deposit(int amount) { + + public void deposit(Float amount) { + + public boolean userIsLoggedIn() { //method to validate if the log-in id is valid + //if account data is = null; means user is not logged in = false + //if account data is valid; true + if (accountData == null) { + return false; + } else { + return true; + } if (accountData != null) { tryCall( () -> bank.deposit(accountData, amount), @@ -38,9 +54,10 @@ public void deposit(int amount) { } } - public void withdraw(int amount) { + public void withdraw(Float amount) { + if (accountData != null) { - tryCall( + tryCall( () -> bank.withdraw(accountData, amount), update ); @@ -51,14 +68,19 @@ public void exit() { if (accountData != null) { accountData = null; } + } @Override public String toString() { - return accountData != null ? accountData.toString() : "Try account 1000 or 2000 and click submit."; + return accountData != null ? accountData.toString() : "Log-in with your account ID."; } + private void tryCall(Supplier > action, Consumer postAction) { + + msg = ""; + try { ActionResult result = action.get(); if (result.isSuccess()) { @@ -69,7 +91,7 @@ private void tryCall(Supplier > action, Consumer postActi throw new RuntimeException(errorMessage); } } catch (Exception e) { - System.out.println("Error: " + e.getMessage()); + msg = "Error: " + e.getMessage(); } } } diff --git a/src/main/java/rocks/zipcode/atm/CashMachineApp.java b/src/main/java/rocks/zipcode/atm/CashMachineApp.java index c0fada8..3c10376 100644 --- a/src/main/java/rocks/zipcode/atm/CashMachineApp.java +++ b/src/main/java/rocks/zipcode/atm/CashMachineApp.java @@ -1,5 +1,6 @@ package rocks.zipcode.atm; +import javafx.scene.Group; import rocks.zipcode.atm.bank.Bank; import javafx.application.Application; import javafx.scene.Parent; @@ -11,59 +12,140 @@ import javafx.stage.Stage; import javafx.scene.layout.FlowPane; +import javax.xml.soap.Text; + /** * @author ZipCodeWilmington */ + +//TODO + //create a menu that shows all accounts available + //Login screen separate from the main menu + + public class CashMachineApp extends Application { private TextField field = new TextField(); + private TextField txtLogIn = new TextField(); + private TextField txtDeposit = new TextField(); + private TextField txtWithDraw = new TextField(); + private TextField txtSeeAccounts = new TextField(); //creation of a see all accounts? private CashMachine cashMachine = new CashMachine(new Bank()); + + private Parent createContent() { - VBox vbox = new VBox(10); - vbox.setPrefSize(600, 600); + Button btnSubmit = new Button("Log-in"); + Button btnSeeAccounts = new Button("Get Accounts"); + Button btnDeposit = new Button("Deposit"); + Button btnWithdraw = new Button("Withdraw"); + Button btnExit = new Button("Log-out"); + + btnWithdraw.setDisable(true); + btnDeposit.setDisable(true); + btnExit.setDisable(true); + btnSeeAccounts.setDisable(true); + + + VBox vbox = new VBox(20); //spacing between the buttons + vbox.setPrefSize(600, 600); //size of the display box TextArea areaInfo = new TextArea(); - Button btnSubmit = new Button("Set Account ID"); + + btnSubmit.setOnAction(e -> { - int id = Integer.parseInt(field.getText()); + int id = Integer.parseInt(txtLogIn.getText()); cashMachine.login(id); - + if(cashMachine.userIsLoggedIn()) { //reference the method in the cash machine app with the boolean condition + btnExit.setDisable(false); + btnDeposit.setDisable(false); + btnWithdraw.setDisable(false); + btnSubmit.setDisable(true); + } areaInfo.setText(cashMachine.toString()); + clearTxtBoxes(); + }); - Button btnDeposit = new Button("Deposit"); + btnDeposit.setOnAction(e -> { - int amount = Integer.parseInt(field.getText()); - cashMachine.deposit(amount); + Float amount = Float.parseFloat(txtDeposit.getText()); + cashMachine.deposit(amount); areaInfo.setText(cashMachine.toString()); + if(amount < 0) { + areaInfo.setText("this is not a valid number!"); + else { + cashMachine.deposit(amount); + areaInfo.setText(cashMachine.toString()); + + } + clearTxtBoxes(); }); - Button btnWithdraw = new Button("Withdraw"); + + + btnWithdraw.setOnAction(e -> { - int amount = Integer.parseInt(field.getText()); - cashMachine.withdraw(amount); - areaInfo.setText(cashMachine.toString()); + Float amount = Float.parseFloat(txtWithDraw.getText()); + + cashMachine.withdraw(amount); + if(cashMachine.getMsg() != "") { + areaInfo.setText(cashMachine.getMsg()); + + } + if(amount < 0){ //added this to show invalid number for negatives - but it stopped showing error when trying to overdraft??! + areaInfo.setText("This is not a valid number!"); + } + else{ + areaInfo.setText( "This is not a valid number!"); + } + + clearTxtBoxes(); }); - Button btnExit = new Button("Exit"); + + btnExit.setOnAction(e -> { cashMachine.exit(); - areaInfo.setText(cashMachine.toString()); + btnExit.setDisable(true); + btnDeposit.setDisable(true); + btnWithdraw.setDisable(true); + btnSubmit.setDisable(false); + clearTxtBoxes(); }); - FlowPane flowpane = new FlowPane(); - flowpane.getChildren().add(btnSubmit); - flowpane.getChildren().add(btnDeposit); - flowpane.getChildren().add(btnWithdraw); - flowpane.getChildren().add(btnExit); - vbox.getChildren().addAll(field, flowpane, areaInfo); + +// FlowPane flowpane = new FlowPane(); +// flowpane.getChildren().add(btnSubmit); +// flowpane.getChildren().add(btnDeposit); +// flowpane.getChildren().add(btnWithdraw); +// flowpane.getChildren().add(btnExit); + + + FlowPane flowPaneAccountID = new FlowPane(); + flowPaneAccountID.getChildren().add(btnSubmit); + flowPaneAccountID.getChildren().add(txtLogIn); + + FlowPane flowPaneDeposit = new FlowPane(); + flowPaneDeposit.getChildren().add(btnDeposit); + flowPaneDeposit.getChildren().add(txtDeposit); + + FlowPane flowPaneWithDraw = new FlowPane(); + flowPaneWithDraw.getChildren().add(btnWithdraw); + flowPaneWithDraw.getChildren().add(txtWithDraw); + + FlowPane flowPaneLogOut = new FlowPane(); + flowPaneLogOut.getChildren().add(btnExit); + + + vbox.getChildren().addAll(flowPaneAccountID, flowPaneDeposit,flowPaneWithDraw, flowPaneLogOut, areaInfo); return vbox; + } @Override @@ -75,4 +157,14 @@ public void start(Stage stage) throws Exception { public static void main(String[] args) { launch(args); } + + public void clearTxtBoxes(){ + txtDeposit.clear(); + txtWithDraw.clear(); + txtLogIn.clear(); + + } + + + } diff --git a/src/main/java/rocks/zipcode/atm/bank/Account.java b/src/main/java/rocks/zipcode/atm/bank/Account.java index 53a0fa8..c8ff191 100644 --- a/src/main/java/rocks/zipcode/atm/bank/Account.java +++ b/src/main/java/rocks/zipcode/atm/bank/Account.java @@ -3,6 +3,8 @@ /** * @author ZipCodeWilmington */ + + public abstract class Account { private AccountData accountData; @@ -15,28 +17,30 @@ public AccountData getAccountData() { return accountData; } - public void deposit(int amount) { - updateBalance(getBalance() + amount); + + public void deposit(Float amount) { + + accountData = new AccountData (accountData.getId(), accountData.getName(), accountData.getEmail(), accountData.getBalance() + amount); } - public boolean withdraw(int amount) { + public boolean withdraw(Float amount) { if (canWithdraw(amount)) { - updateBalance(getBalance() - amount); + updateBalance(((getBalance() - amount))); return true; } else { return false; } } - protected boolean canWithdraw(int amount) { - return getBalance() >= amount; + protected boolean canWithdraw(float amount) { + return accountData.getBalance() >= amount; } public int getBalance() { - return accountData.getBalance(); + return (int) accountData.getBalance(); } - private void updateBalance(int newBalance) { + private void updateBalance(float newBalance) { accountData = new AccountData(accountData.getId(), accountData.getName(), accountData.getEmail(), newBalance); } diff --git a/src/main/java/rocks/zipcode/atm/bank/AccountData.java b/src/main/java/rocks/zipcode/atm/bank/AccountData.java index 0255385..f5b973c 100644 --- a/src/main/java/rocks/zipcode/atm/bank/AccountData.java +++ b/src/main/java/rocks/zipcode/atm/bank/AccountData.java @@ -9,9 +9,9 @@ public final class AccountData { private final String name; private final String email; - private final int balance; + private final float balance; - AccountData(int id, String name, String email, int balance) { + AccountData(int id, String name, String email, float balance) { this.id = id; this.name = name; this.email = email; @@ -30,7 +30,7 @@ public String getEmail() { return email; } - public int getBalance() { + public float getBalance() { return balance; } diff --git a/src/main/java/rocks/zipcode/atm/bank/Bank.java b/src/main/java/rocks/zipcode/atm/bank/Bank.java index 5bd5bbf..e7ce149 100644 --- a/src/main/java/rocks/zipcode/atm/bank/Bank.java +++ b/src/main/java/rocks/zipcode/atm/bank/Bank.java @@ -10,15 +10,23 @@ */ public class Bank { - private Map accounts = new HashMap<>(); + private Map accounts = new HashMap<>(); //use to create a collection to show all the accounts public Bank() { accounts.put(1000, new BasicAccount(new AccountData( - 1000, "Example 1", "example1@gmail.com", 500 + 1000, "Action Jackson", "blamblam1@gmail.com", 300000f ))); accounts.put(2000, new PremiumAccount(new AccountData( - 2000, "Example 2", "example2@gmail.com", 200 + 2000, "Spawn", "spawny3@gmail.com", 200000f + ))); + + accounts.put(3000, new PremiumAccount(new AccountData( + 3000, "David", "Nice@gmail.com", 1000000f + ))); + + accounts.put(4000, new PremiumAccount(new AccountData( + 4000, "Simba", "woofwoof@gmail.com", 300000f ))); } @@ -27,26 +35,31 @@ public ActionResult getAccountById(int id) { if (account != null) { return ActionResult.success(account.getAccountData()); - } else { - return ActionResult.fail("No account with id: " + id + "\nTry account 1000 or 2000"); + } + else { + return ActionResult.fail("No account with id: " + id + "\nEnter your account ID and then click submit"); } } - public ActionResult deposit(AccountData accountData, int amount) { + + public ActionResult deposit(AccountData accountData, Float amount) { + Account account = accounts.get(accountData.getId()); account.deposit(amount); return ActionResult.success(account.getAccountData()); } - public ActionResult withdraw(AccountData accountData, int amount) { + + public ActionResult withdraw(AccountData accountData, Float amount) { + Account account = accounts.get(accountData.getId()); boolean ok = account.withdraw(amount); if (ok) { return ActionResult.success(account.getAccountData()); } else { - return ActionResult.fail("Withdraw failed: " + amount + ". Account has: " + account.getBalance()); + return ActionResult.fail("Cannot Withdraw Amount: " + amount + ". Account has: " + account.getBalance()); } } } diff --git a/src/main/java/rocks/zipcode/atm/bank/PremiumAccount.java b/src/main/java/rocks/zipcode/atm/bank/PremiumAccount.java index e4c9616..b265d3c 100644 --- a/src/main/java/rocks/zipcode/atm/bank/PremiumAccount.java +++ b/src/main/java/rocks/zipcode/atm/bank/PremiumAccount.java @@ -5,14 +5,14 @@ */ public class PremiumAccount extends Account { - private static final int OVERDRAFT_LIMIT = 100; + private static final float OVERDRAFT_LIMIT = 100f; public PremiumAccount(AccountData accountData) { super(accountData); } @Override - protected boolean canWithdraw(int amount) { + protected boolean canWithdraw(float amount) { return getBalance() + OVERDRAFT_LIMIT >= amount; } }