intellij version of CashMachineBlueJ

Bank.java 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package rocks.zipcode.atm.bank;
  2. import rocks.zipcode.atm.ActionResult;
  3. import rocks.zipcode.atm.CashMachine;
  4. import rocks.zipcode.atm.CashMachineApp;
  5. import java.util.HashMap;
  6. import java.util.Map;
  7. /**
  8. * @author ZipCodeWilmington
  9. */
  10. public class Bank {
  11. private Map<Integer, Account> accounts = new HashMap<>();
  12. public Bank() {
  13. accounts.put(1000, new BasicAccount(new AccountData(
  14. 1000, "Example 1", "example1@gmail.com", 500
  15. )));
  16. accounts.put(2000, new PremiumAccount(new AccountData(
  17. 2000, "Example 2", "example2@gmail.com", 200
  18. )));
  19. accounts.put(6666, new PremiumAccount(new AccountData(
  20. 6666, "Nick Satinover", "nsatinover@gmail.com", 20000
  21. )));
  22. accounts.put(7337, new BasicAccount(new AccountData(
  23. 7337, "Kris HaHaYourBroke", "kris@onedollar.com", 1
  24. )));
  25. }
  26. public ActionResult<AccountData> getAccountById(int id) {
  27. Account account = accounts.get(id);
  28. if (account != null) {
  29. return ActionResult.success(account.getAccountData());
  30. } else {
  31. return ActionResult.fail("No account with id: " + id + "\nTry account 1000, 2000, 6666, 7337");
  32. }
  33. }
  34. public ActionResult<AccountData> deposit(AccountData accountData, float amount) {
  35. Account account = accounts.get(accountData.getId());
  36. account.deposit(amount);
  37. return ActionResult.success(account.getAccountData());
  38. }
  39. public ActionResult<AccountData> withdraw(AccountData accountData, float amount) {
  40. Account account = accounts.get(accountData.getId());
  41. boolean ok = account.withdraw(amount);
  42. if (ok) {
  43. if (account.getBalance() < 0) {
  44. Overdraft.popupMessage("Overdraft", "Warning, your account is now in overdraft");
  45. }
  46. return ActionResult.success(account.getAccountData());
  47. } else {
  48. Overdraft.popupMessage("Declined", "Declined, account contains insufficient funds");
  49. return ActionResult.fail("Withdraw failed: " + amount + ". Account has: " + account.getBalance());
  50. }
  51. }
  52. public boolean isValidId(int id){
  53. Account account = accounts.get(id);
  54. if (account != null) {
  55. return true;
  56. } else {
  57. return false;
  58. }
  59. }
  60. }