intellij version of CashMachineBlueJ

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package rocks.zipcode.atm.bank;
  2. import rocks.zipcode.atm.ActionResult;
  3. import java.util.HashMap;
  4. import java.util.Map;
  5. /**
  6. * @author ZipCodeWilmington
  7. */
  8. public class Bank {
  9. private Map<Integer, Account> accounts = new HashMap<>();
  10. public Bank() {
  11. accounts.put(1000, new BasicAccount(new AccountData(
  12. 1000, "Example 1", "example1@gmail.com", 500
  13. )));
  14. accounts.put(2000, new PremiumAccount(new AccountData(
  15. 2000, "Example 2", "example2@gmail.com", 200
  16. )));
  17. accounts.put(3000, new PremiumAccount(new AccountData(
  18. 2000, "Example 3", "example2@gmail.com", 100
  19. )));
  20. }
  21. public ActionResult<AccountData> getAccountById(int id) {
  22. Account account = accounts.get(id);
  23. if (account != null) {
  24. return ActionResult.success(account.getAccountData());
  25. } else {
  26. return ActionResult.fail("No account with id: " + id + "\nTry account 1000 or 2000");
  27. }
  28. }
  29. public ActionResult<AccountData> deposit(AccountData accountData, int amount) {
  30. Account account = accounts.get(accountData.getId());
  31. account.deposit(amount);
  32. return ActionResult.success(account.getAccountData());
  33. }
  34. public ActionResult<AccountData> withdraw(AccountData accountData, int amount) {
  35. Account account = accounts.get(accountData.getId());
  36. boolean ok = account.withdraw(amount);
  37. if (ok) {
  38. return ActionResult.success(account.getAccountData());
  39. } else {
  40. return ActionResult.fail("Withdraw failed: " + amount + ". Account has: " + account.getBalance());
  41. }
  42. }
  43. }