1 import java.awt.FlowLayout;
2 import java.awt.GridLayout;
3 import java.awt.event.ActionEvent;
4 import java.awt.event.ActionListener;
5 import javax.swing.JButton;
6 import javax.swing.JFrame;
7 import javax.swing.JPanel;
8 import javax.swing.JTextArea;
9
10 /**
11 A frame displaying the components of an ATM.
12 */
13 public class ATMFrame extends JFrame
14 {
15 private static final int FRAME_WIDTH = 300;
16 private static final int FRAME_HEIGHT = 300;
17
18 private JButton aButton;
19 private JButton bButton;
20 private JButton cButton;
21
22 private KeyPad pad;
23 private JTextArea display;
24
25 private ATM theATM;
26
27 /**
28 Constructs the user interface of the ATM frame.
29 */
30 public ATMFrame(ATM anATM)
31 {
32 theATM = anATM;
33
34 // Construct components
35 pad = new KeyPad();
36
37 display = new JTextArea(4, 20);
38
39 aButton = new JButton(" A ");
40 aButton.addActionListener(new AButtonListener());
41
42 bButton = new JButton(" B ");
43 bButton.addActionListener(new BButtonListener());
44
45 cButton = new JButton(" C ");
46 cButton.addActionListener(new CButtonListener());
47
48 // Add components
49
50 JPanel buttonPanel = new JPanel();
51 buttonPanel.add(aButton);
52 buttonPanel.add(bButton);
53 buttonPanel.add(cButton);
54
55 setLayout(new FlowLayout());
56 add(pad);
57 add(display);
58 add(buttonPanel);
59 showState();
60
61 setSize(FRAME_WIDTH, FRAME_HEIGHT);
62 }
63
64 /**
65 Updates display message.
66 */
67 public void showState()
68 {
69 int state = theATM.getState();
70 pad.clear();
71 if (state == ATM.START)
72 {
73 display.setText("Enter customer number\nA = OK");
74 }
75 else if (state == ATM.PIN)
76 {
77 display.setText("Enter PIN\nA = OK");
78 }
79 else if (state == ATM.ACCOUNT)
80 {
81 display.setText("Select Account\n"
82 + "A = Checking\nB = Savings\nC = Exit");
83 }
84 else if (state == ATM.TRANSACT)
85 {
86 display.setText("Balance = "
87 + theATM.getBalance()
88 + "\nEnter amount and select transaction\n"
89 + "A = Withdraw\nB = Deposit\nC = Cancel");
90 }
91 }
92
93 class AButtonListener implements ActionListener
94 {
95 public void actionPerformed(ActionEvent event)
96 {
97 int state = theATM.getState();
98 if (state == ATM.START)
99 {
100 theATM.setCustomerNumber((int) pad.getValue());
101 }
102 else if (state == ATM.PIN)
103 {
104 theATM.selectCustomer((int) pad.getValue());
105 }
106 else if (state == ATM.ACCOUNT)
107 {
108 theATM.selectAccount(ATM.CHECKING);
109 }
110 else if (state == ATM.TRANSACT)
111 {
112 theATM.withdraw(pad.getValue());
113 theATM.back();
114 }
115 showState();
116 }
117 }
118
119 class BButtonListener implements ActionListener
120 {
121 public void actionPerformed(ActionEvent event)
122 {
123 int state = theATM.getState();
124 if (state == ATM.ACCOUNT)
125 {
126 theATM.selectAccount(ATM.SAVINGS);
127 }
128 else if (state == ATM.TRANSACT)
129 {
130 theATM.deposit(pad.getValue());
131 theATM.back();
132 }
133 showState();
134 }
135 }
136
137 class CButtonListener implements ActionListener
138 {
139 public void actionPerformed(ActionEvent event)
140 {
141 int state = theATM.getState();
142 if (state == ATM.ACCOUNT)
143 {
144 theATM.reset();
145 }
146 else if (state == ATM.TRANSACT)
147 {
148 theATM.back();
149 }
150 showState();
151 }
152 }
153 }