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 display.setText("Enter customer number\nA = OK");
73 else if (state == ATM.PIN)
74 display.setText("Enter PIN\nA = OK");
75 else if (state == ATM.ACCOUNT)
76 display.setText("Select Account\n"
77 + "A = Checking\nB = Savings\nC = Exit");
78 else if (state == ATM.TRANSACT)
79 display.setText("Balance = "
80 + theATM.getBalance()
81 + "\nEnter amount and select transaction\n"
82 + "A = Withdraw\nB = Deposit\nC = Cancel");
83 }
84
85 class AButtonListener implements ActionListener
86 {
87 public void actionPerformed(ActionEvent event)
88 {
89 int state = theATM.getState();
90 if (state == ATM.START)
91 theATM.setCustomerNumber((int) pad.getValue());
92 else if (state == ATM.PIN)
93 theATM.selectCustomer((int) pad.getValue());
94 else if (state == ATM.ACCOUNT)
95 theATM.selectAccount(ATM.CHECKING);
96 else if (state == ATM.TRANSACT)
97 {
98 theATM.withdraw(pad.getValue());
99 theATM.back();
100 }
101 showState();
102 }
103 }
104
105 class BButtonListener implements ActionListener
106 {
107 public void actionPerformed(ActionEvent event)
108 {
109 int state = theATM.getState();
110 if (state == ATM.ACCOUNT)
111 theATM.selectAccount(ATM.SAVINGS);
112 else if (state == ATM.TRANSACT)
113 {
114 theATM.deposit(pad.getValue());
115 theATM.back();
116 }
117 showState();
118 }
119 }
120
121 class CButtonListener implements ActionListener
122 {
123 public void actionPerformed(ActionEvent event)
124 {
125 int state = theATM.getState();
126 if (state == ATM.ACCOUNT)
127 theATM.reset();
128 else if (state == ATM.TRANSACT)
129 theATM.back();
130 showState();
131 }
132 }
133 }