-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_calculator.java
281 lines (238 loc) · 9.15 KB
/
example_calculator.java
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
public class Calculator extends JFrame {
private static final String NUMBER_PROPERTY = "NUMBER_PROPERTY";
private static final String OPERATOR_PROPERTY = "OPERATOR_PROPERTY";
private static final String FIRST = "FIRST";
private static final String VALID = "VALID";
// These would be much better if placed in an enum,
// but enums are only available starting in Java 5.
// Code using them isn't back portable.
private static interface Operator{
static final int EQUALS = 0;
static final int PLUS = 1;
static final int MINUS = 2;
static final int MULTIPLY = 3;
static final int DIVIDE = 4;
}
private String status;
private int previousOperation;
private double lastValue;
private JTextArea lcdDisplay;
private JLabel errorDisplay;
public static void main(String[] args) {
// Remember, all swing components must be accessed from
// the event dispatch thread.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Calculator calc = new Calculator();
calc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
calc.setVisible(true);
}
});
}
public Calculator() {
super("Calculator");
JPanel mainPanel = new JPanel(new BorderLayout());
JPanel numberPanel = buildNumberPanel();
JPanel operatorPanel = buildOperatorPanel();
JPanel clearPanel = buildClearPanel();
lcdDisplay = new JTextArea();
lcdDisplay.setFont(new Font("Dialog", Font.BOLD, 18));
mainPanel.add(clearPanel, BorderLayout.SOUTH);
mainPanel.add(numberPanel, BorderLayout.CENTER);
mainPanel.add(operatorPanel, BorderLayout.EAST);
mainPanel.add(lcdDisplay, BorderLayout.NORTH);
errorDisplay = new JLabel(" ");
errorDisplay.setFont(new Font("Dialog", Font.BOLD, 12));
getContentPane().setLayout(new BorderLayout());
getContentPane().add(mainPanel, BorderLayout.CENTER);
getContentPane().add(errorDisplay, BorderLayout.SOUTH);
pack();
resetState();
}
private final ActionListener numberListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
JComponent source = (JComponent)e.getSource();
Integer number = (Integer) source.getClientProperty(NUMBER_PROPERTY);
if(number == null){
throw new IllegalStateException("No NUMBER_PROPERTY on component");
}
numberButtonPressed(number.intValue());
}
};
private final ActionListener decimalListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
decimalButtonPressed();
}
};
private final ActionListener operatorListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
JComponent source = (JComponent) e.getSource();
Integer opCode = (Integer) source.getClientProperty(OPERATOR_PROPERTY);
if (opCode == null) {
throw new IllegalStateException("No OPERATOR_PROPERTY on component");
}
operatorButtonPressed(opCode);
}
};
private final ActionListener clearListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
resetState();
}
};
private JButton buildNumberButton(int number) {
JButton button = new JButton(Integer.toString(number));
button.putClientProperty(NUMBER_PROPERTY, Integer.valueOf(number));
button.addActionListener(numberListener);
return button;
}
private JButton buildOperatorButton(String symbol, int opType) {
JButton plus = new JButton(symbol);
plus.putClientProperty(OPERATOR_PROPERTY, Integer.valueOf(opType));
plus.addActionListener(operatorListener);
return plus;
}
public JPanel buildNumberPanel() {
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(4, 3));
panel.add(buildNumberButton(7));
panel.add(buildNumberButton(8));
panel.add(buildNumberButton(9));
panel.add(buildNumberButton(4));
panel.add(buildNumberButton(5));
panel.add(buildNumberButton(6));
panel.add(buildNumberButton(1));
panel.add(buildNumberButton(2));
panel.add(buildNumberButton(3));
JButton buttonDec = new JButton(".");
buttonDec.addActionListener(decimalListener);
panel.add(buttonDec);
panel.add(buildNumberButton(0));
// Exit button is to close the calculator and terminate the program.
JButton buttonExit = new JButton("EXIT");
buttonExit.setMnemonic(KeyEvent.VK_C);
buttonExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
panel.add(buttonExit);
return panel;
}
public JPanel buildOperatorPanel() {
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(4, 1));
panel.add(buildOperatorButton("+", Operator.PLUS));
panel.add(buildOperatorButton("-", Operator.MINUS));
panel.add(buildOperatorButton("*", Operator.MULTIPLY));
panel.add(buildOperatorButton("/", Operator.DIVIDE));
return panel;
}
public JPanel buildClearPanel() {
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(1, 3));
JButton clear = new JButton("C");
clear.addActionListener(clearListener);
panel.add(clear);
JButton CEntry = new JButton("CE");
CEntry.addActionListener(clearListener);
panel.add(CEntry);
panel.add(buildOperatorButton("=", Operator.EQUALS));
return panel;
}
public void numberButtonPressed(int i) {
String displayText = lcdDisplay.getText();
String valueString = Integer.toString(i);
if (("0".equals(displayText)) || (FIRST.equals(status))) {
displayText = "";
}
int maxLength = (displayText.indexOf(".") >= 0) ? 21 : 20;
if(displayText.length() + valueString.length() <= maxLength){
displayText += valueString;
clearError();
} else {
setError("Reached the 20 digit max");
}
lcdDisplay.setText(displayText);
status = VALID;
}
public void operatorButtonPressed(int newOperation) {
Double displayValue = Double.valueOf(lcdDisplay.getText());
// if(newOperation == Operator.EQUALS && previousOperation != //Operator.EQUALS){
// operatorButtonPressed(previousOperation);
// } else {
switch (previousOperation) {
case Operator.PLUS:
displayValue = lastValue + displayValue;
commitOperation(newOperation, displayValue);
break;
case Operator.MINUS:
displayValue = lastValue - displayValue;
commitOperation(newOperation, displayValue);
break;
case Operator.MULTIPLY:
displayValue = lastValue * displayValue;
commitOperation(newOperation, displayValue);
break;
case Operator.DIVIDE:
if (displayValue == 0) {
setError("ERROR: Division by Zero");
} else {
displayValue = lastValue / displayValue;
commitOperation(newOperation, displayValue);
}
break;
case Operator.EQUALS:
commitOperation(newOperation, displayValue);
// }
}
}
public void decimalButtonPressed() {
String displayText = lcdDisplay.getText();
if (FIRST.equals(status)) {
displayText = "0";
}
if(!displayText.contains(".")){
displayText = displayText + ".";
}
lcdDisplay.setText(displayText);
status = VALID;
}
private void setError(String errorMessage) {
if(errorMessage.trim().equals("")){
errorMessage = " ";
}
errorDisplay.setText(errorMessage);
}
private void clearError(){
status = FIRST;
errorDisplay.setText(" ");
}
private void commitOperation(int operation, double result) {
status = FIRST;
lastValue = result;
previousOperation = operation;
lcdDisplay.setText(String.valueOf(result));
}
/**
* Resets the program state.
*/
void resetState() {
clearError();
lastValue = 0;
previousOperation = Operator.EQUALS;
lcdDisplay.setText("0");
}
}