//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 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 void tryCall(Supplier > action, Consumer postAction) { try { ActionResult 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()); } } }