-
Notifications
You must be signed in to change notification settings - Fork 0
/
CalculatorGUI.java
168 lines (145 loc) · 4.43 KB
/
CalculatorGUI.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
package module3.gui.calculator;
import javafx.application.*;
import javafx.geometry.Insets;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.*;
/**
* Calculator
*/
public class CalculatorGUI extends Application {
public static void main(String[] args) {
launch(args);
}
Calculator calculator = new Calculator();
TextField display = new TextField();
GridPane buttonGrid = new GridPane();
String[][] buttonLabels = {
{"7", "8", "9", "/"},
{"4", "5", "6", "*"},
{"1", "2", "3", "-"},
{"0", ".", "=", "+"},
{"AC"}
};
public void start(Stage stage) {
Pane root = new VBox(10);
root.setPadding(new Insets(10, 10, 10, 10));
Scene myScene = new Scene(root, 300, 300);
stage.setScene(myScene);
stage.setTitle("Basic Sum Application");
root.getChildren().add(display);
root.getChildren().add(buttonGrid);
buttonGrid.setVgap(10);
buttonGrid.setHgap(10);
for (int row = 0; row < buttonLabels.length; row++) {
for(int col = 0; col < buttonLabels[row].length; col++) {
String label = buttonLabels[row][col];
Button btn = new Button(label);
btn.setPrefWidth(60);
if (label.equals("AC")) {
btn.setOnAction(e -> {
calculator.clear();
display();
});
} else if (label.equals("+") || label.equals("-") || label.equals("*") || label.equals("/")) {
btn.setOnAction(e -> {
pushOperatorButton(label);
display();
});
} else if (label.equals("=")) {
btn.setOnAction(e -> {
calculator.submit();
display();
});
}
else {
btn.setOnAction(e -> {
pushNumberButton(label);
display();
});
}
buttonGrid.add(btn, col, row);
}
}
stage.show();
}
public void pushOperatorButton(String label) {
calculator.setOperator(label);
display();
}
public void pushNumberButton(String label) {
calculator.addDigit(label);
display();
System.out.println("PUSH: " + label);
}
public void display() {
this.display.setText(calculator.display());
}
}
class Calculator {
private String[] inputNums = {"", ""};
private int activeIndex = 0;
private String operator = "";
private boolean showResult = false;
public String display() {
String out = inputNums[0];
if (!operator.isEmpty()) {
out += " " + operator + " " + inputNums[1];
}
if (showResult) {
out += " = " + getResult();
}
return out;
}
private String getNumString() {
return inputNums[this.activeIndex];
}
private void setNumString(String numString) {
inputNums[this.activeIndex] = numString;
}
public void switchActiveIndex() {
int len = inputNums.length;
activeIndex = (++activeIndex) % len;
}
public void addDigit(String digit) {
if (showResult) {
clear();
}
setNumString(getNumString() + digit);
}
public void setOperator(String op) {
this.operator = op;
switchActiveIndex();
}
public void clear() {
inputNums = new String[]{"", ""};
activeIndex = 0;
operator = "";
showResult = false;
}
public void submit() {
try {
getResult();
showResult = true;
} catch (NumberFormatException e) {
clear();
}
}
public Double getResult() throws NumberFormatException {
Double num1 = Double.parseDouble(inputNums[0]);
Double num2 = Double.parseDouble(inputNums[1]);
switch(operator) {
case "+":
return num1 + num2;
case "-":
return num1 - num2;
case "*":
return num1 * num2;
case "/":
return num1 / num2;
default:
return -99999.99;
}
}
}