|
| 1 | +package ch_10; |
| 2 | + |
| 3 | +import java.util.Scanner; |
| 4 | + |
| 5 | +import ch_09.exercise09_07.Account; |
| 6 | + |
| 7 | +/** |
| 8 | + * 10.7 (Game: ATM machine) Use the Account class created in |
| 9 | + * Programming Exercise 9.7 to simulate an ATM machine. |
| 10 | + * Create ten accounts in an array with id 0, 1, . . . , 9, |
| 11 | + * and initial balance $100. The system prompts the user to |
| 12 | + * enter an id. If the id is entered incorrectly, ask the user |
| 13 | + * to enter a correct id. Once an id is accepted, the main menu |
| 14 | + * is displayed as shown in the sample run. You can enter a choice |
| 15 | + * 1 for viewing the current balance, 2 for withdrawing money, 3 for |
| 16 | + * depositing money, and 4 for exiting the main menu. Once you exit, |
| 17 | + * the system will prompt for an id again. Thus, once the system |
| 18 | + * starts, it will not stop. |
| 19 | + * |
| 20 | + * @author Harry D. |
| 21 | + */ |
| 22 | +public class Exercise10_07 { |
| 23 | + public static void main(String[] args) { |
| 24 | + |
| 25 | + Scanner input = new Scanner(System.in); |
| 26 | + |
| 27 | + Account[] atmAccounts = new Account[10]; |
| 28 | + |
| 29 | + for (int i = 0; i < atmAccounts.length; i++) { |
| 30 | + |
| 31 | + atmAccounts[i] = new Account(i, 100.00); |
| 32 | + |
| 33 | + } |
| 34 | + LOOP: |
| 35 | + for (; ; ) { // Enter 1234 to end the program |
| 36 | + |
| 37 | + System.out.print("Please enter an account ID#: "); |
| 38 | + |
| 39 | + |
| 40 | + int idOfAcc = input.nextInt(); |
| 41 | + if (idOfAcc == 1234) { |
| 42 | + break; |
| 43 | + } |
| 44 | + |
| 45 | + while (idOfAcc < 0 | idOfAcc > 9) { |
| 46 | + |
| 47 | + System.out.println("Incorrect ID# please enter a correct ID# "); |
| 48 | + |
| 49 | + idOfAcc = input.nextInt(); |
| 50 | + if (idOfAcc == 1234) { |
| 51 | + break LOOP; |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + |
| 56 | + int userInput = 0; |
| 57 | + |
| 58 | + while (userInput != 4) { |
| 59 | + |
| 60 | + |
| 61 | + System.out.println("Main Menu"); |
| 62 | + System.out.println("1: Check Balance"); |
| 63 | + System.out.println("2: Withdraw"); |
| 64 | + System.out.println("3: Deposit"); |
| 65 | + System.out.println("4: Exit"); |
| 66 | + System.out.print("Enter a choice now:"); |
| 67 | + |
| 68 | + userInput = input.nextInt(); |
| 69 | + executeCommand(userInput, idOfAcc, input, atmAccounts); |
| 70 | + |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + public static void executeCommand(int userCommand, int accId, |
| 76 | + Scanner j, Account[] arr) { |
| 77 | + |
| 78 | + switch (userCommand) { |
| 79 | + |
| 80 | + case 1: |
| 81 | + System.out.printf("The balance in account " + accId + " is $%.2f \n", |
| 82 | + arr[accId].getBalance()); |
| 83 | + break; |
| 84 | + |
| 85 | + case 2: |
| 86 | + System.out.print("Enter the amount you want to withdraw:"); |
| 87 | + arr[accId].withdraw(j.nextDouble()); |
| 88 | + break; |
| 89 | + |
| 90 | + case 3: |
| 91 | + System.out.print("Enter the amount you you want to deposit:"); |
| 92 | + arr[accId].deposit(j.nextDouble()); |
| 93 | + |
| 94 | + |
| 95 | + } |
| 96 | + |
| 97 | + } |
| 98 | + |
| 99 | +} |
0 commit comments