12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
-
-
- //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(int amount) {
- if (accountData != null) {
- tryCall(
- () -> bank.deposit(accountData, amount),
- update
- );
- }
- }
-
- public void withdraw(int amount) {
- if (accountData != null) {
- tryCall(
- () -> bank.withdraw(accountData, amount),
- update
- );
- }
- }
-
- 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();
- throw new RuntimeException(errorMessage);
- }
- } catch (Exception e) {
- System.out.println("Error: " + e.getMessage());
- }
- }
- }
|