1 import java.io.IOException;
2 import java.util.Scanner;
3
4 /**
5 A text-based simulation of an automatic teller machine.
6 */
7 public class ATMSimulator
8 {
9 public static void main(String[] args)
10 {
11 ATM theATM;
12 try
13 {
14 Bank theBank = new Bank();
15 theBank.readCustomers("customers.txt");
16 theATM = new ATM(theBank);
17 }
18 catch(IOException e)
19 {
20 System.out.println("Error opening accounts file.");
21 return;
22 }
23
24 Scanner in = new Scanner(System.in);
25
26 while (true)
27 {
28 int state = theATM.getState();
29 if (state == ATM.START)
30 {
31 System.out.print("Enter customer number: ");
32 int number = in.nextInt();
33 theATM.setCustomerNumber(number);
34 }
35 else if (state == ATM.PIN)
36 {
37 System.out.print("Enter PIN: ");
38 int pin = in.nextInt();
39 theATM.selectCustomer(pin);
40 }
41 else if (state == ATM.ACCOUNT)
42 {
43 System.out.print("A=Checking, B=Savings, C=Quit: ");
44 String command = in.next();
45 if (command.equalsIgnoreCase("A"))
46 {
47 theATM.selectAccount(ATM.CHECKING);
48 }
49 else if (command.equalsIgnoreCase("B"))
50 {
51 theATM.selectAccount(ATM.SAVINGS);
52 }
53 else if (command.equalsIgnoreCase("C"))
54 {
55 theATM.reset();
56 }
57 else
58 {
59 System.out.println("Illegal input!");
60 }
61 }
62 else if (state == ATM.TRANSACT)
63 {
64 System.out.println("Balance=" + theATM.getBalance());
65 System.out.print("A=Deposit, B=Withdrawal, C=Cancel: ");
66 String command = in.next();
67 if (command.equalsIgnoreCase("A"))
68 {
69 System.out.print("Amount: ");
70 double amount = in.nextDouble();
71 theATM.deposit(amount);
72 theATM.back();
73 }
74 else if (command.equalsIgnoreCase("B"))
75 {
76 System.out.print("Amount: ");
77 double amount = in.nextDouble();
78 theATM.withdraw(amount);
79 theATM.back();
80 }
81 else if (command.equalsIgnoreCase("C"))
82 {
83 theATM.back();
84 }
85 else
86 {
87 System.out.println("Illegal input!");
88 }
89 }
90 }
91 }
92 }
93