| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- package rocks.zipcode.atm;
-
- import javafx.scene.control.Alert;
- import rocks.zipcode.atm.bank.AccountData;
- import rocks.zipcode.atm.bank.Bank;
-
- import java.util.function.Consumer;
- import java.util.function.Supplier;
-
- /**
- * @author ZipCodeWilmington
- */
- public class CashMachine {
-
- private final Bank bank;
- private AccountData accountData = null;
-
- public CashMachine(Bank bank) {
-
- this.bank = bank;
- }
-
- private Consumer<AccountData> update = data -> {
- accountData = data;
- };
-
- public void login(int id) {
- tryCall(
- () -> bank.getAccountById(id),
- update
- );
- }
-
- public void deposit(float amount) {
- if (accountData != null) {
- tryCall(
- () -> bank.deposit(accountData, amount),
- update
- );
-
- }
- }
-
- public void withdraw(float amount) {
- if (accountData != null) {
- tryCall(
- () -> bank.withdraw(accountData, amount),
- update
- );
-
- // Over draft check for alert message?
-
- }
- }
-
- 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.";
- }
-
- private <T> void tryCall(Supplier<ActionResult<T> > action, Consumer<T> postAction) {
- try {
- ActionResult<T> result = action.get();
- if (result.isSuccess()) {
- T data = result.getData();
- postAction.accept(data);
- } else {
- String errorMessage = result.getErrorMessage();
- // Create alert here, rather than throwing an exception
-
- throw new RuntimeException(errorMessage);
- }
- } catch (Exception e) {
- System.out.println("Error: " + e.getMessage());
- }
- }
- }
|