ATM.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package com.zipcodewilmington.bankaccountlab;
  2. public class ATM {
  3. Console console = new Console();
  4. BankAccount account1;
  5. BankAccount account2;
  6. BankAccount account3;
  7. BankAccount[] accounts = new BankAccount[]{account1,account2,account3};
  8. public ATM(){
  9. }
  10. public BankAccount[] getAccounts() {
  11. return accounts;
  12. }
  13. public void setAccounts(BankAccount[] accounts) {
  14. this.accounts = accounts;
  15. }
  16. public void runATM(){
  17. console.println("Welcome to the ATM! You may type \"leave\" at any word entry to close the application");
  18. createAccounts();
  19. boolean run =true;
  20. while(run) {
  21. console.println("Here are your current accounts:");
  22. for (BankAccount account : accounts) {
  23. console.println("\nOwner Name: " + account.getOwnerName()+"\nAccount Number: "+ account.getAccountNumber() + "\nCurrent Balance: " + account.getCurrentBalance());
  24. }
  25. int currentAccount = console.getIntInput("please enter your account number");
  26. switch(console.getIntInput("What would you like to do?\n1.Transfer Funds\n2.Change An Account Owner\n")){
  27. case (1) : transferFunds(currentAccount);
  28. break;
  29. case (2) : changeAccountOwner(currentAccount,console.getStringInput("What is the new name on the account?"));
  30. break;
  31. default:
  32. console.println("Please enter the number associated with your choice");
  33. }
  34. }
  35. }
  36. public void transferFunds(int account){
  37. switch (console.getIntInput("What would you like to do?\n1.Deposit\n2.Withdraw\n3.Transfer to a different account")){
  38. case (1) : deposit(account, console.getDoubleInput("How much would you like to deposit?"));
  39. break;
  40. case (2) : withdraw(account,console.getDoubleInput("How much would you like to withdraw?"));
  41. break;
  42. case (3) : transfer(account,console.getDoubleInput("How much would you like to transfer?"),console.getIntInput("Enter the account number you would like to transfer to"));
  43. break;
  44. default:
  45. console.println("Please enter the number associated with your choice");
  46. }
  47. }
  48. public void withdraw(int currentAccount, double amount) {
  49. for (BankAccount account : accounts) {
  50. if (currentAccount == account.getAccountNumber()){
  51. account.setCurrentBalance(account.getCurrentBalance()-amount);
  52. }
  53. }
  54. }
  55. public void deposit(int currentAccount, double amount) {
  56. for (BankAccount account : accounts) {
  57. if (currentAccount == account.getAccountNumber()){
  58. account.setCurrentBalance(account.getCurrentBalance()+amount);
  59. }
  60. }
  61. }
  62. public void transfer(int currentAccount, double amount, int transferTo) {
  63. for (BankAccount account : accounts) {
  64. if (currentAccount == account.getAccountNumber()){
  65. account.setCurrentBalance(account.getCurrentBalance()-amount);
  66. }
  67. if (transferTo == account.getAccountNumber()){
  68. account.setCurrentBalance(account.getCurrentBalance()+amount);
  69. }
  70. }
  71. }
  72. public void changeAccountOwner(int currentAccount, String newName){
  73. for (BankAccount account : accounts) {
  74. if (currentAccount == account.getAccountNumber()){
  75. account.setOwnerName(newName);
  76. }
  77. }
  78. }
  79. public void createAccounts() {
  80. for (BankAccount account : accounts) {
  81. boolean creating = true;
  82. while (creating) {
  83. switch (console.getIntInput("What kind of account would you like to open?\n1.Business\n2.Checking\n3.Savings\n4.Skip")) {
  84. case (1):
  85. account = new BusinessAccount(console.getDoubleInput("How much would you like to deposit?"), console.getStringInput("What is your name?"));
  86. creating = false;
  87. break;
  88. case (2):
  89. account = new CheckingAccount(console.getDoubleInput("How much would you like to deposit?"), console.getStringInput("What is your name?"));
  90. creating = false;
  91. break;
  92. case (3):
  93. account = new SavingsAccount(console.getDoubleInput("How much would you like to deposit?"), console.getStringInput("What is your name?"));
  94. creating = false;
  95. break;
  96. case (4):
  97. account = null;
  98. creating = false;
  99. break;
  100. default:
  101. console.println("Please enter the number associated with your choice");
  102. }
  103. }
  104. }
  105. }
  106. }