1 import java.io.IOException;
2 import java.sql.SQLException;
3
4 /**
5 An ATM that accesses a bank.
6 */
7 public class ATM
8 {
9 public static final int CHECKING = 1;
10 public static final int SAVINGS = 2;
11
12 private int state;
13 private int customerNumber;
14 private Customer currentCustomer;
15 private BankAccount currentAccount;
16 private Bank theBank;
17
18 public static final int START = 1;
19 public static final int PIN = 2;
20 public static final int ACCOUNT = 3;
21 public static final int TRANSACT = 4;
22
23 /**
24 Constructs an ATM for a given bank.
25 @param aBank the bank to which this ATM connects
26 */
27 public ATM(Bank aBank)
28 {
29 theBank = aBank;
30 reset();
31 }
32
33 /**
34 Resets the ATM to the initial state.
35 */
36 public void reset()
37 {
38 customerNumber = -1;
39 currentAccount = null;
40 state = START;
41 }
42
43 /**
44 Sets the current customer number
45 and sets state to PIN.
46 (Precondition: state is START)
47 @param number the customer number.
48 */
49 public void setCustomerNumber(int number)
50 {
51 customerNumber = number;
52 state = PIN;
53 }
54
55 /**
56 Finds customer in bank.
57 If found sets state to ACCOUNT, else to START.
58 (Precondition: state is PIN)
59 @param pin the PIN of the current customer
60 */
61 public void selectCustomer(int pin)
62 throws SQLException
63 {
64 currentCustomer
65 = theBank.findCustomer(customerNumber, pin);
66 if (currentCustomer == null)
67 {
68 state = START;
69 }
70 else
71 {
72 state = ACCOUNT;
73 }
74 }
75
76 /**
77 Sets current account to checking or savings. Sets
78 state to TRANSACT.
79 (Precondition: state is ACCOUNT or TRANSACT)
80 @param account one of CHECKING or SAVINGS
81 */
82 public void selectAccount(int account)
83 {
84 if (account == CHECKING)
85 {
86 currentAccount = currentCustomer.getCheckingAccount();
87 }
88 else
89 {
90 currentAccount = currentCustomer.getSavingsAccount();
91 }
92 state = TRANSACT;
93 }
94
95 /**
96 Withdraws amount from current account.
97 (Precondition: state is TRANSACT)
98 @param value the amount to withdraw
99 */
100 public void withdraw(double value)
101 throws SQLException
102 {
103 currentAccount.withdraw(value);
104 }
105
106 /**
107 Deposits amount to current account.
108 (Precondition: state is TRANSACT)
109 @param value the amount to deposit
110 */
111 public void deposit(double value)
112 throws SQLException
113 {
114 currentAccount.deposit(value);
115 }
116
117 /**
118 Gets the balance of the current account.
119 (Precondition: state is TRANSACT)
120 @return the balance
121 */
122 public double getBalance()
123 throws SQLException
124 {
125 return currentAccount.getBalance();
126 }
127
128 /**
129 Moves back to the previous state.
130 */
131 public void back()
132 {
133 if (state == TRANSACT)
134 {
135 state = ACCOUNT;
136 }
137 else if (state == ACCOUNT)
138 {
139 state = PIN;
140 }
141 else if (state == PIN)
142 {
143 state = START;
144 }
145 }
146
147 /**
148 Gets the current state of this ATM.
149 @return the current state
150 */
151 public int getState()
152 {
153 return state;
154 }
155 }