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