Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 28 additions & 6 deletions src/main/java/rocks/zipcode/atm/CashMachine.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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),
Expand All @@ -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
);
Expand All @@ -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 <T> void tryCall(Supplier<ActionResult<T> > action, Consumer<T> postAction) {

msg = "";

try {
ActionResult<T> result = action.get();
if (result.isSuccess()) {
Expand All @@ -69,7 +91,7 @@ private <T> void tryCall(Supplier<ActionResult<T> > action, Consumer<T> postActi
throw new RuntimeException(errorMessage);
}
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
msg = "Error: " + e.getMessage();
}
}
}
132 changes: 112 additions & 20 deletions src/main/java/rocks/zipcode/atm/CashMachineApp.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
Expand All @@ -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();

}



}
20 changes: 12 additions & 8 deletions src/main/java/rocks/zipcode/atm/bank/Account.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
/**
* @author ZipCodeWilmington
*/


public abstract class Account {

private AccountData accountData;
Expand All @@ -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);
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/rocks/zipcode/atm/bank/AccountData.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -30,7 +30,7 @@ public String getEmail() {
return email;
}

public int getBalance() {
public float getBalance() {
return balance;
}

Expand Down
29 changes: 21 additions & 8 deletions src/main/java/rocks/zipcode/atm/bank/Bank.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,23 @@
*/
public class Bank {

private Map<Integer, Account> accounts = new HashMap<>();
private Map<Integer, Account> 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
)));
}

Expand All @@ -27,26 +35,31 @@ public ActionResult<AccountData> 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<AccountData> deposit(AccountData accountData, int amount) {

public ActionResult<AccountData> deposit(AccountData accountData, Float amount) {

Account account = accounts.get(accountData.getId());
account.deposit(amount);

return ActionResult.success(account.getAccountData());
}

public ActionResult<AccountData> withdraw(AccountData accountData, int amount) {

public ActionResult<AccountData> 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());
}
}
}
Loading