Ver código fonte

completed calcskin

Christian Sheridan 6 anos atrás
pai
commit
e48f7f3651

+ 17
- 4
src/main/java/rocks/zipcode/calcskin/CalcEngine.java Ver arquivo

@@ -6,18 +6,31 @@ public class CalcEngine {
6 6
     }
7 7
 
8 8
     public double add(double v, double v1) {
9
-        return Double.NaN;
9
+        return v+v1;
10 10
     }
11 11
 
12 12
     public double subtract(double v, double v1) {
13
-        return Double.NaN;
13
+        return v-v1;
14 14
     }
15 15
 
16 16
     public double multiply(double v, double v1) {
17
-        return Double.NaN;
17
+        return v*v1;
18 18
     }
19 19
 
20 20
     public double divide(double v, double v1) {
21
-        return Double.NaN;
21
+        return v/v1;
22 22
     }
23
+
24
+    public double squared(double v) {return v*v;}
25
+
26
+    public double sqrt(double v) {return Math.sqrt(v);}
27
+
28
+    public double inverse(double v) {return 1/v;}
29
+
30
+    public double exponent(double v, double v1) {return Math.pow(v, v1);}
31
+
32
+    public double memoryAdd(double v) {return v;}
33
+
34
+    public double memoryRecall(double v) {return v;}
35
+
23 36
 }

+ 131
- 7
src/main/java/rocks/zipcode/calcskin/CalcSkin.java Ver arquivo

@@ -24,10 +24,10 @@ public class CalcSkin extends Application {
24 24
         launch(args);
25 25
     }
26 26
     private static final String[][] template = {
27
-            { "7", "8", "9", "/" },
28
-            { "4", "5", "6", "*" },
29
-            { "1", "2", "3", "-" },
30
-            { "0", "c", "=", "+" }
27
+            { "7", "8", "9", "/", "x^2", "MC", "sin" },
28
+            { "4", "5", "6", "*", "SQRT", "MR", "cos" },
29
+            { "1", "2", "3", "-", "1/x", "M+", "tan" },
30
+            { "0", "c", "=", "+", "x^y" }
31 31
     };
32 32
 
33 33
     private final Map<String, Button> accelerators = new HashMap<>();
@@ -35,8 +35,9 @@ public class CalcSkin extends Application {
35 35
     private DoubleProperty previousValue = new SimpleDoubleProperty();
36 36
     private DoubleProperty currentValue = new SimpleDoubleProperty();
37 37
     private CalcEngine calcEngine = new CalcEngine();
38
+    private DoubleProperty memoryValue = new SimpleDoubleProperty();
38 39
 
39
-    private enum Op { NOOP, ADD, SUBTRACT, MULTIPLY, DIVIDE }
40
+    private enum Op { NOOP, ADD, SUBTRACT, MULTIPLY, DIVIDE, SQUARED, SQUAREROOT, INVERSE, EXPONENT }
40 41
 
41 42
     private Op curOp   = Op.NOOP;
42 43
     private Op stackOp = Op.NOOP;
@@ -82,9 +83,9 @@ public class CalcSkin extends Application {
82 83
     private TextField createScreen() {
83 84
         final TextField screen = new TextField();
84 85
         screen.setStyle("-fx-background-color: aquamarine;");
85
-        screen.setAlignment(Pos.CENTER_RIGHT);
86
+        screen.setAlignment(Pos.CENTER_LEFT);
86 87
         screen.setEditable(false);
87
-        screen.textProperty().bind(Bindings.format("%.0f", currentValue));
88
+        screen.textProperty().bind(Bindings.format("%.4f", currentValue));
88 89
         return screen;
89 90
     }
90 91
 
@@ -114,7 +115,27 @@ public class CalcSkin extends Application {
114 115
                 makeClearButton(button);
115 116
             } else if ("=".equals(s)) {
116 117
                 makeEqualsButton(button);
118
+            } else if ("x^2".equals(s)) {
119
+                makeSquaredButton(button);
120
+            } else if ("SQRT".equals(s)){
121
+                makeSqrtButton(button);
122
+            } else if ("1/x".equals(s)){
123
+                makeInverseButton(button);
124
+            } else if ("M+".equals(s)){
125
+                makeMemoryAddButton(button);
126
+            } else if ("MR".equals(s)){
127
+                makeMemoryRecallButton(button);
128
+            } else if ("MC".equals(s)){
129
+                makeMemoryClearButton(button);
130
+            } else if ("sin".equals(s)){
131
+                makeSinButton(button);
132
+            } else if ("cos".equals(s)) {
133
+                makeCosButton(button);
134
+            } else if ("tan".equals(s)) {
135
+                makeTanButton(button);
117 136
             }
137
+            //make else ifs for the single numeric functions
138
+            //call the new method for those functions
118 139
         }
119 140
 
120 141
         return button;
@@ -127,6 +148,10 @@ public class CalcSkin extends Application {
127 148
             case "-": triggerOp.set(Op.SUBTRACT); break;
128 149
             case "*": triggerOp.set(Op.MULTIPLY); break;
129 150
             case "/": triggerOp.set(Op.DIVIDE);   break;
151
+            //case "x^2": triggerOp.set(Op.SQUARED); break;
152
+            //case "SQRT": triggerOp.set(Op.SQUAREROOT); break;
153
+            //case "1/x": triggerOp.set(Op.INVERSE); break;
154
+            case "x^y": triggerOp.set(Op.EXPONENT); break;
130 155
         }
131 156
         return triggerOp;
132 157
     }
@@ -175,6 +200,96 @@ public class CalcSkin extends Application {
175 200
         });
176 201
     }
177 202
 
203
+    private void makeSquaredButton(Button button) {
204
+        button.setStyle("-fx-base: darkgray");
205
+        button.setOnAction(new EventHandler<ActionEvent>() {
206
+            @Override
207
+            public void handle(ActionEvent actionEvent) {
208
+                currentValue.set(calcEngine.squared(currentValue.get()));
209
+            }
210
+        });
211
+    }
212
+
213
+    private void makeSqrtButton(Button button) {
214
+        button.setStyle("-fx-base: darkgray;");
215
+        button.setOnAction(new EventHandler<ActionEvent>() {
216
+            @Override
217
+            public void handle(ActionEvent actionEvent) {
218
+                currentValue.set(calcEngine.sqrt(currentValue.get()));
219
+            }
220
+        });
221
+    }
222
+
223
+    private void makeInverseButton(Button button) {
224
+        button.setStyle("-fx-base: darkgray;");
225
+        button.setOnAction(new EventHandler<ActionEvent>() {
226
+            @Override
227
+            public void handle(ActionEvent actionEvent) {
228
+                currentValue.set(calcEngine.inverse(currentValue.get()));
229
+            }
230
+        });
231
+    }
232
+
233
+    private void makeMemoryAddButton(Button button) {
234
+        button.setStyle("-fx-base: gray");
235
+        button.setOnAction(new EventHandler<ActionEvent>() {
236
+            @Override
237
+            public void handle(ActionEvent actionEvent) {
238
+                memoryValue.set(currentValue.get());
239
+            }
240
+        });
241
+    }
242
+
243
+    private void makeMemoryRecallButton(Button button) {
244
+        button.setStyle("-fx-base: gray");
245
+        button.setOnAction(new EventHandler<ActionEvent>() {
246
+            @Override
247
+            public void handle(ActionEvent actionEvent) {
248
+                currentValue.set(memoryValue.get());
249
+            }
250
+        });
251
+    }
252
+
253
+    private void makeMemoryClearButton(Button button) {
254
+        button.setStyle("-fx-base: gray");
255
+        button.setOnAction(new EventHandler<ActionEvent>() {
256
+            @Override
257
+            public void handle(ActionEvent actionEvent) {
258
+                memoryValue.set(0);
259
+            }
260
+        });
261
+    }
262
+
263
+    private void makeSinButton(Button button) {
264
+        button.setStyle("-fx-base: black");
265
+        button.setOnAction(new EventHandler<ActionEvent>() {
266
+            @Override
267
+            public void handle(ActionEvent actionEvent) {
268
+                currentValue.set(Math.sin(currentValue.get()));
269
+            }
270
+        });
271
+    }
272
+
273
+    private void makeCosButton(Button button) {
274
+        button.setStyle("-fx-base: black");
275
+        button.setOnAction(new EventHandler<ActionEvent>() {
276
+            @Override
277
+            public void handle(ActionEvent actionEvent) {
278
+                currentValue.set(Math.cos(currentValue.get()));
279
+            }
280
+        });
281
+    }
282
+
283
+    private void makeTanButton(Button button) {
284
+        button.setStyle("-fx-base: black");
285
+        button.setOnAction(new EventHandler<ActionEvent>() {
286
+            @Override
287
+            public void handle(ActionEvent actionEvent) {
288
+                currentValue.set(Math.tan(currentValue.get()));
289
+            }
290
+        });
291
+    }
292
+
178 293
     private void makeEqualsButton(Button button) {
179 294
         button.setStyle("-fx-base: ghostwhite;");
180 295
         button.setOnAction(new EventHandler<ActionEvent>() {
@@ -185,10 +300,19 @@ public class CalcSkin extends Application {
185 300
                     case SUBTRACT: currentValue.set(calcEngine.subtract(previousValue.get(), currentValue.get())); break;
186 301
                     case MULTIPLY: currentValue.set(calcEngine.multiply(previousValue.get(), currentValue.get())); break;
187 302
                     case DIVIDE:   currentValue.set(calcEngine.divide(previousValue.get(), currentValue.get())); break;
303
+                    //case SQUARED:   currentValue.set(calcEngine.squared(currentValue.get())); break;
304
+                    //case SQUAREROOT:   currentValue.set(calcEngine.sqrt(currentValue.get())); break;
305
+                    //case INVERSE:   currentValue.set(calcEngine.inverse(currentValue.get())); break;
306
+                    case EXPONENT:   currentValue.set(calcEngine.exponent(previousValue.get(), currentValue.get())); break;
307
+
308
+
309
+
188 310
                 }
189 311
             }
190 312
         });
191 313
     }
314
+
315
+
192 316
 }
193 317
 
194 318
 

+ 12
- 0
src/test/java/rocks/zipcode/calcskin/CalcEngineTest.java Ver arquivo

@@ -38,4 +38,16 @@ public class CalcEngineTest {
38 38
     public void divide() {
39 39
         Assert.assertTrue("", (testCalc.divide(10.0, 2.0) == 5.0));
40 40
     }
41
+
42
+    @Test
43
+    public void squared() {Assert.assertTrue("", (testCalc.squared(5) == 25));}
44
+
45
+    @Test
46
+    public void sqrt() {Assert.assertTrue("", testCalc.sqrt(9)==3);}
47
+
48
+    @Test
49
+    public void inverse() { Assert.assertTrue("", testCalc.inverse(10)==.1);}
50
+
51
+    @Test
52
+    public void exponent() { Assert.assertTrue("", testCalc.exponent(2,3)==8);}
41 53
 }