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 theATM.selectAccount(ATM.CHECKING);
47 else if (command.equalsIgnoreCase("B"))
48 theATM.selectAccount(ATM.SAVINGS);
49 else if (command.equalsIgnoreCase("C"))
50 theATM.reset();
51 else
52 System.out.println("Illegal input!");
53 }
54 else if (state == ATM.TRANSACT)
55 {
56 System.out.println("Balance=" + theATM.getBalance());
57 System.out.print("A=Deposit, B=Withdrawal, C=Cancel: ");
58 String command = in.next();
59 if (command.equalsIgnoreCase("A"))
60 {
61 System.out.print("Amount: ");
62 double amount = in.nextDouble();
63 theATM.deposit(amount);
64 theATM.back();
65 }
66 else if (command.equalsIgnoreCase("B"))
67 {
68 System.out.print("Amount: ");
69 double amount = in.nextDouble();
70 theATM.withdraw(amount);
71 theATM.back();
72 }
73 else if (command.equalsIgnoreCase("C"))
74 theATM.back();
75 else
76 System.out.println("Illegal input!");
77 }
78 }
79 }
80 }
81