|
@@ -1,5 +1,4 @@
|
1
|
1
|
package rocks.zipcode.calcskin;
|
2
|
|
-
|
3
|
2
|
import javafx.application.Application;
|
4
|
3
|
import javafx.beans.binding.Bindings;
|
5
|
4
|
import javafx.beans.property.*;
|
|
@@ -12,12 +11,9 @@ import javafx.scene.input.KeyEvent;
|
12
|
11
|
import javafx.scene.layout.*;
|
13
|
12
|
import javafx.stage.Stage;
|
14
|
13
|
import javafx.stage.StageStyle;
|
15
|
|
-import rocks.zipcode.calcskin.CalcEngine;
|
16
|
|
-
|
17
|
14
|
import java.util.HashMap;
|
18
|
15
|
import java.util.Map;
|
19
|
16
|
|
20
|
|
-// a simple JavaFX calculator.
|
21
|
17
|
public class CalcSkin extends Application {
|
22
|
18
|
|
23
|
19
|
public static void main(String[] args){
|
|
@@ -27,19 +23,19 @@ public class CalcSkin extends Application {
|
27
|
23
|
{ "7", "8", "9", "/" },
|
28
|
24
|
{ "4", "5", "6", "*" },
|
29
|
25
|
{ "1", "2", "3", "-" },
|
30
|
|
- { "0", "c", "=", "+" }
|
|
26
|
+ { "0", "c", "=", "+" },
|
|
27
|
+ { "√", "^", "~", "²"}
|
31
|
28
|
};
|
32
|
29
|
|
33
|
30
|
private final Map<String, Button> accelerators = new HashMap<>();
|
34
|
|
-
|
35
|
31
|
private DoubleProperty previousValue = new SimpleDoubleProperty();
|
36
|
32
|
private DoubleProperty currentValue = new SimpleDoubleProperty();
|
37
|
33
|
private CalcEngine calcEngine = new CalcEngine();
|
38
|
34
|
|
39
|
|
- private enum Op { NOOP, ADD, SUBTRACT, MULTIPLY, DIVIDE }
|
|
35
|
+ private enum Cal { operation, add, subtract, multiply, divide, squareroot, square, exponent, invert}
|
40
|
36
|
|
41
|
|
- private Op curOp = Op.NOOP;
|
42
|
|
- private Op stackOp = Op.NOOP;
|
|
37
|
+ private Cal first = Cal.operation;
|
|
38
|
+ private Cal second = Cal.operation;
|
43
|
39
|
|
44
|
40
|
public static void launchCalc(String... args) {
|
45
|
41
|
launch(args);
|
|
@@ -107,9 +103,9 @@ public class CalcSkin extends Application {
|
107
|
103
|
if (s.matches("[0-9]")) {
|
108
|
104
|
makeNumericButton(s, button);
|
109
|
105
|
} else {
|
110
|
|
- final ObjectProperty<Op> triggerOp = determineOperand(s);
|
111
|
|
- if (triggerOp.get() != Op.NOOP) {
|
112
|
|
- makeOperandButton(button, triggerOp);
|
|
106
|
+ final ObjectProperty<Cal> triggerCal = determineOperand(s);
|
|
107
|
+ if (triggerCal.get() != Cal.operation) {
|
|
108
|
+ makeOperandButton(button, triggerCal);
|
113
|
109
|
} else if ("c".equals(s)) {
|
114
|
110
|
makeClearButton(button);
|
115
|
111
|
} else if ("=".equals(s)) {
|
|
@@ -120,30 +116,34 @@ public class CalcSkin extends Application {
|
120
|
116
|
return button;
|
121
|
117
|
}
|
122
|
118
|
|
123
|
|
- private ObjectProperty<Op> determineOperand(String s) {
|
124
|
|
- final ObjectProperty<Op> triggerOp = new SimpleObjectProperty<>(Op.NOOP);
|
|
119
|
+ private ObjectProperty<Cal> determineOperand(String s) {
|
|
120
|
+ final ObjectProperty<Cal> triggerOp = new SimpleObjectProperty<>(Cal.operation);
|
125
|
121
|
switch (s) {
|
126
|
|
- case "+": triggerOp.set(Op.ADD); break;
|
127
|
|
- case "-": triggerOp.set(Op.SUBTRACT); break;
|
128
|
|
- case "*": triggerOp.set(Op.MULTIPLY); break;
|
129
|
|
- case "/": triggerOp.set(Op.DIVIDE); break;
|
|
122
|
+ case "+": triggerOp.set(Cal.add); break;
|
|
123
|
+ case "-": triggerOp.set(Cal.subtract); break;
|
|
124
|
+ case "*": triggerOp.set(Cal.multiply); break;
|
|
125
|
+ case "/": triggerOp.set(Cal.divide); break;
|
|
126
|
+ case "²": triggerOp.set(Cal.square); break;
|
|
127
|
+ case "√": triggerOp.set(Cal.squareroot); break;
|
|
128
|
+ case "^": triggerOp.set(Cal.exponent); break;
|
|
129
|
+ case "~": triggerOp.set(Cal.invert); break;
|
130
|
130
|
}
|
131
|
131
|
return triggerOp;
|
132
|
132
|
}
|
133
|
133
|
|
134
|
|
- private void makeOperandButton(Button button, final ObjectProperty<Op> triggerOp) {
|
135
|
|
- button.setStyle("-fx-base: lightgray;");
|
|
134
|
+ private void makeOperandButton(Button button, final ObjectProperty<Cal> triggerCal) {
|
|
135
|
+ button.setStyle("-fx-base: black;");
|
136
|
136
|
button.setOnAction(new EventHandler<ActionEvent>() {
|
137
|
137
|
@Override
|
138
|
138
|
public void handle(ActionEvent actionEvent) {
|
139
|
|
- curOp = triggerOp.get();
|
|
139
|
+ first = triggerCal.get();
|
140
|
140
|
}
|
141
|
141
|
});
|
142
|
142
|
}
|
143
|
143
|
|
144
|
144
|
private Button makeStandardButton(String s) {
|
145
|
145
|
Button button = new Button(s);
|
146
|
|
- button.setStyle("-fx-base: beige;");
|
|
146
|
+ button.setStyle("-fx-base: pink;");
|
147
|
147
|
accelerators.put(s, button);
|
148
|
148
|
button.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
|
149
|
149
|
return button;
|
|
@@ -153,20 +153,20 @@ public class CalcSkin extends Application {
|
153
|
153
|
button.setOnAction(new EventHandler<ActionEvent>() {
|
154
|
154
|
@Override
|
155
|
155
|
public void handle(ActionEvent actionEvent) {
|
156
|
|
- if (curOp == Op.NOOP) {
|
|
156
|
+ if (first == Cal.operation) {
|
157
|
157
|
currentValue.set(currentValue.get() * 10 + Integer.parseInt(s));
|
158
|
158
|
} else {
|
159
|
159
|
previousValue.set(currentValue.get());
|
160
|
160
|
currentValue.set(Integer.parseInt(s));
|
161
|
|
- stackOp = curOp;
|
162
|
|
- curOp = Op.NOOP;
|
|
161
|
+ second = first;
|
|
162
|
+ second = Cal.operation;
|
163
|
163
|
}
|
164
|
164
|
}
|
165
|
165
|
});
|
166
|
166
|
}
|
167
|
167
|
|
168
|
168
|
private void makeClearButton(Button button) {
|
169
|
|
- button.setStyle("-fx-base: mistyrose;");
|
|
169
|
+ button.setStyle("-fx-base: grey;");
|
170
|
170
|
button.setOnAction(new EventHandler<ActionEvent>() {
|
171
|
171
|
@Override
|
172
|
172
|
public void handle(ActionEvent actionEvent) {
|
|
@@ -176,19 +176,23 @@ public class CalcSkin extends Application {
|
176
|
176
|
}
|
177
|
177
|
|
178
|
178
|
private void makeEqualsButton(Button button) {
|
179
|
|
- button.setStyle("-fx-base: ghostwhite;");
|
|
179
|
+ button.setStyle("-fx-base: green;");
|
180
|
180
|
button.setOnAction(new EventHandler<ActionEvent>() {
|
181
|
181
|
@Override
|
182
|
182
|
public void handle(ActionEvent actionEvent) {
|
183
|
|
- switch (stackOp) {
|
184
|
|
- case ADD: currentValue.set(calcEngine.add(previousValue.get(), currentValue.get())); break;
|
185
|
|
- case SUBTRACT: currentValue.set(calcEngine.subtract(previousValue.get(), currentValue.get())); break;
|
186
|
|
- case MULTIPLY: currentValue.set(calcEngine.multiply(previousValue.get(), currentValue.get())); break;
|
187
|
|
- case DIVIDE: currentValue.set(calcEngine.divide(previousValue.get(), currentValue.get())); break;
|
|
183
|
+ switch (second) {
|
|
184
|
+ case add: currentValue.set(calcEngine.addition(previousValue.get(), currentValue.get())); break;
|
|
185
|
+ case subtract: currentValue.set(calcEngine.subtraction(previousValue.get(), currentValue.get())); break;
|
|
186
|
+ case multiply: currentValue.set(calcEngine.multiplication(previousValue.get(), currentValue.get())); break;
|
|
187
|
+ case divide: currentValue.set(calcEngine.division(previousValue.get(), currentValue.get())); break;
|
|
188
|
+ case square: currentValue.set(calcEngine.square(previousValue.get())); break;
|
|
189
|
+ case squareroot: currentValue.set(calcEngine.squareroot(previousValue.get())); break;
|
|
190
|
+ case exponent: currentValue.set(calcEngine.exponent(previousValue.get(), currentValue.get())); break;
|
|
191
|
+ case invert: currentValue.set(calcEngine.invert(previousValue.get())); break;
|
|
192
|
+
|
188
|
193
|
}
|
189
|
194
|
}
|
190
|
195
|
});
|
191
|
196
|
}
|
192
|
197
|
}
|
193
|
198
|
|
194
|
|
-
|