1 |
import java.awt.Container; |
2 |
import java.awt.FlowLayout; |
3 |
import java.awt.GridLayout; |
4 |
import java.awt.event.ActionEvent; |
5 |
import java.awt.event.ActionListener; |
6 |
import java.io.IOException; |
7 |
import javax.swing.JButton; |
8 |
import javax.swing.JFrame; |
9 |
import javax.swing.JOptionPane; |
10 |
import javax.swing.JPanel; |
11 |
import javax.swing.JTextArea; |
12 |
|
13 |
/** |
14 |
A frame displaying the components of an ATM |
15 |
*/ |
16 |
class ATM extends JFrame |
17 |
{ |
18 |
/** |
19 |
Constructs the user interface of the ATM application. |
20 |
*/ |
21 |
public ATM() |
22 |
{ |
23 |
// initialize bank and customers |
24 |
|
25 |
theBank = new Bank(); |
26 |
try |
27 |
{ |
28 |
theBank.readCustomers("customers.txt"); |
29 |
} |
30 |
catch(IOException e) |
31 |
{ |
32 |
JOptionPane.showMessageDialog(null, |
33 |
"Error opening accounts file."); |
34 |
} |
35 |
|
36 |
// construct components |
37 |
|
38 |
pad = new Keypad(); |
39 |
|
40 |
display = new JTextArea(4, 20); |
41 |
|
42 |
aButton = new JButton(" A "); |
43 |
aButton.addActionListener(new AButtonListener()); |
44 |
|
45 |
bButton = new JButton(" B "); |
46 |
bButton.addActionListener(new BButtonListener()); |
47 |
|
48 |
cButton = new JButton(" C "); |
49 |
cButton.addActionListener(new CButtonListener()); |
50 |
|
51 |
// add components to content pane |
52 |
|
53 |
JPanel buttonPanel = new JPanel(); |
54 |
buttonPanel.setLayout(new GridLayout(3, 1)); |
55 |
buttonPanel.add(aButton); |
56 |
buttonPanel.add(bButton); |
57 |
buttonPanel.add(cButton); |
58 |
|
59 |
Container contentPane = getContentPane(); |
60 |
contentPane.setLayout(new FlowLayout()); |
61 |
contentPane.add(pad); |
62 |
contentPane.add(display); |
63 |
contentPane.add(buttonPanel); |
64 |
|
65 |
setState(START_STATE); |
66 |
} |
67 |
|
68 |
/** |
69 |
Sets the current customer number to the keypad value |
70 |
and sets state to PIN. |
71 |
*/ |
72 |
public void setCustomerNumber() |
73 |
{ |
74 |
customerNumber = (int)pad.getValue(); |
75 |
setState(PIN_STATE); |
76 |
} |
77 |
|
78 |
/** |
79 |
Gets PIN from keypad, finds customer in bank. |
80 |
If found sets state to ACCOUNT, else to START. |
81 |
*/ |
82 |
public void selectCustomer() |
83 |
{ |
84 |
int pin = (int)pad.getValue(); |
85 |
currentCustomer |
86 |
= theBank.findCustomer(customerNumber, pin); |
87 |
if (currentCustomer == null) |
88 |
setState(START_STATE); |
89 |
else |
90 |
setState(ACCOUNT_STATE); |
91 |
} |
92 |
|
93 |
/** |
94 |
Sets current account to checking or savings. Sets |
95 |
state to TRANSACT |
96 |
@param account one of CHECKING_ACCOUNT or SAVINGS_ACCOUNT |
97 |
*/ |
98 |
public void selectAccount(int account) |
99 |
{ |
100 |
if (account == CHECKING_ACCOUNT) |
101 |
currentAccount = currentCustomer.getCheckingAccount(); |
102 |
else |
103 |
currentAccount = currentCustomer.getSavingsAccount(); |
104 |
setState(TRANSACT_STATE); |
105 |
} |
106 |
|
107 |
/** |
108 |
Withdraws amount typed in keypad from current account. |
109 |
Sets state to ACCOUNT. |
110 |
*/ |
111 |
public void withdraw() |
112 |
{ |
113 |
currentAccount.withdraw(pad.getValue()); |
114 |
setState(ACCOUNT_STATE); |
115 |
} |
116 |
|
117 |
/** |
118 |
Deposits amount typed in keypad to current account. |
119 |
Sets state to ACCOUNT. |
120 |
*/ |
121 |
public void deposit() |
122 |
{ |
123 |
currentAccount.deposit(pad.getValue()); |
124 |
setState(ACCOUNT_STATE); |
125 |
} |
126 |
|
127 |
/** |
128 |
Sets state and updates display message. |
129 |
@param state the next state |
130 |
*/ |
131 |
public void setState(int newState) |
132 |
{ |
133 |
state = newState; |
134 |
pad.clear(); |
135 |
if (state == START_STATE) |
136 |
display.setText("Enter customer number\nA = OK"); |
137 |
else if (state == PIN_STATE) |
138 |
display.setText("Enter PIN\nA = OK"); |
139 |
else if (state == ACCOUNT_STATE) |
140 |
display.setText("Select Account\n" |
141 |
+ "A = Checking\nB = Savings\nC = Exit"); |
142 |
else if (state == TRANSACT_STATE) |
143 |
display.setText("Balance = " |
144 |
+ currentAccount.getBalance() |
145 |
+ "\nEnter amount and select transaction\n" |
146 |
+ "A = Withdraw\nB = Deposit\nC = Cancel"); |
147 |
} |
148 |
|
149 |
private class AButtonListener implements ActionListener |
150 |
{ |
151 |
public void actionPerformed(ActionEvent event) |
152 |
{ |
153 |
if (state == START_STATE) |
154 |
setCustomerNumber(); |
155 |
else if (state == PIN_STATE) |
156 |
selectCustomer(); |
157 |
else if (state == ACCOUNT_STATE) |
158 |
selectAccount(CHECKING_ACCOUNT); |
159 |
else if (state == TRANSACT_STATE) |
160 |
withdraw(); |
161 |
} |
162 |
} |
163 |
|
164 |
private class BButtonListener implements ActionListener |
165 |
{ |
166 |
public void actionPerformed(ActionEvent event) |
167 |
{ |
168 |
if (state == ACCOUNT_STATE) |
169 |
selectAccount(SAVINGS_ACCOUNT); |
170 |
else if (state == TRANSACT_STATE) |
171 |
deposit(); |
172 |
} |
173 |
} |
174 |
|
175 |
private class CButtonListener implements ActionListener |
176 |
{ |
177 |
public void actionPerformed(ActionEvent event) |
178 |
{ |
179 |
if (state == ACCOUNT_STATE) |
180 |
setState(START_STATE); |
181 |
else if (state == TRANSACT_STATE) |
182 |
setState(ACCOUNT_STATE); |
183 |
} |
184 |
} |
185 |
|
186 |
private int state; |
187 |
private int customerNumber; |
188 |
private Customer currentCustomer; |
189 |
private BankAccount currentAccount; |
190 |
private Bank theBank; |
191 |
|
192 |
private JButton aButton; |
193 |
private JButton bButton; |
194 |
private JButton cButton; |
195 |
|
196 |
private KeyPad pad; |
197 |
private JTextArea display; |
198 |
|
199 |
private static final int START_STATE = 1; |
200 |
private static final int PIN_STATE = 2; |
201 |
private static final int ACCOUNT_STATE = 3; |
202 |
private static final int TRANSACT_STATE = 4; |
203 |
|
204 |
private static final int CHECKING_ACCOUNT = 1; |
205 |
private static final int SAVINGS_ACCOUNT = 2; |
206 |
} |