123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package com.zipcodewilmington.bankaccountlab;
  2. import java.util.ArrayList;
  3. import java.util.InputMismatchException;
  4. import java.util.Scanner;
  5. public class Atm {
  6. BankAccount account;
  7. ArrayList<BankAccount> accounts;
  8. Scanner in = new Scanner(System.in);
  9. public String getNameFromUser (){
  10. System.out.println("Enter your name");
  11. String name = in.nextLine();
  12. return name;
  13. }
  14. public ArrayList<BankAccount> accountsArray(int num, String name){
  15. accounts = new ArrayList<>();
  16. for (int i=0; i<num; i++){
  17. System.out.println("Enter account name");
  18. String str = in.next();
  19. System.out.println("Enter amount");
  20. double balance = in.nextDouble();
  21. accounts.add(Account.accessAccount(str, name, balance));
  22. }
  23. return accounts;
  24. }
  25. public double getAmountFromUser(){
  26. System.out.println("Enter amount");
  27. double amount = in.nextDouble();
  28. return amount;
  29. }
  30. public BankAccount AccountIfo (String str) throws InputMismatchException{
  31. Class checkFor = str.equals("checkings")
  32. ? CheckingAccount.class
  33. : str.equals("savings")
  34. ? SavingsAccount.class
  35. : str.equals("business")
  36. ? BusinessAccount.class
  37. : null;
  38. BankAccount korrektBankerinoAkkount = null;
  39. for (BankAccount acc : accounts) {
  40. if (acc.getClass() == checkFor) korrektBankerinoAkkount = acc;
  41. }
  42. return korrektBankerinoAkkount;
  43. }
  44. public void container(){
  45. boolean welcome = true;
  46. String selectedAccount = " ";
  47. int num = 0 ;
  48. Scanner in = new Scanner(System.in);
  49. String name = getNameFromUser();
  50. while(welcome){
  51. System.out.println("enter choice:\n 1 to open account\n 2 to deposit\n 3 to withdraw\n 4 to check balance\n 5 to exit");
  52. int response = in.nextInt();
  53. switch (response){
  54. case 1:
  55. System.out.println("how many accounts to open");
  56. num = in.nextInt();
  57. accountsArray(num, name);
  58. break;
  59. case 2:
  60. System.out.println("choose account to deposit to");
  61. selectedAccount = in.next();
  62. AccountIfo(selectedAccount).depositMoney(getAmountFromUser());
  63. break;
  64. case 3:
  65. System.out.println("choose account to withdraw from");
  66. selectedAccount = in.next();
  67. AccountIfo(selectedAccount).withDraw(getAmountFromUser());
  68. break;
  69. case 4:
  70. if(accounts.size() == 1){
  71. System.out.println("Your "+ accounts.get(0).accountName()+" account balance is " +
  72. AccountIfo(accounts.get(0).accountName()).getAmount());
  73. }
  74. else if (accounts.size()>1) {
  75. System.out.println("Enter account name");
  76. selectedAccount = in.next();
  77. System.out.println("Your " + selectedAccount + " balance is " +
  78. AccountIfo(selectedAccount).getAmount());
  79. }
  80. break;
  81. case 5:
  82. welcome = false;
  83. break;
  84. default:
  85. System.out.println("please check your response");
  86. break;
  87. }
  88. }
  89. }
  90. }