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