Pārlūkot izejas kodu

Elliott Calc Skin

Elliott Stansbury 6 gadus atpakaļ
vecāks
revīzija
e6e3a3981f

Binārs
.DS_Store Parādīt failu


Binārs
src/.DS_Store Parādīt failu


Binārs
src/main/.DS_Store Parādīt failu


Binārs
src/main/java/.DS_Store Parādīt failu


Binārs
src/main/java/rocks/.DS_Store Parādīt failu


Binārs
src/main/java/rocks/zipcode/.DS_Store Parādīt failu


+ 31
- 4
src/main/java/rocks/zipcode/calcskin/CalcEngine.java Parādīt failu

@@ -6,18 +6,45 @@ public class CalcEngine {
6 6
     }
7 7
 
8 8
     public double add(double v, double v1) {
9
-        return Double.NaN;
9
+
10
+        return v + v1;
10 11
     }
11 12
 
12 13
     public double subtract(double v, double v1) {
13
-        return Double.NaN;
14
+
15
+        return v - v1;
14 16
     }
15 17
 
16 18
     public double multiply(double v, double v1) {
17
-        return Double.NaN;
19
+
20
+        return v * v1;
18 21
     }
19 22
 
20 23
     public double divide(double v, double v1) {
21
-        return Double.NaN;
24
+
25
+        return v / v1;
26
+    }
27
+
28
+    public double square(double v){
29
+
30
+
31
+
32
+        return v * v;
33
+    }
34
+
35
+    public double squareRoot(double v){
36
+
37
+        return Math.sqrt(v);
38
+    }
39
+
40
+    public double invertNumber(double v){
41
+
42
+        return 1/v;
43
+    }
44
+
45
+    public double exponent(double v, double v1){
46
+
47
+        return Math.pow(v,v1);
48
+
22 49
     }
23 50
 }

+ 34
- 6
src/main/java/rocks/zipcode/calcskin/CalcSkin.java Parādīt failu

@@ -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" },
28
+            { "4", "5", "6", "*", "1/x" },
29
+            { "1", "2", "3", "-", "SqRt" },
30
+            { "0", "c", "=", "+", "x^y" }
31 31
     };
32 32
 
33 33
     private final Map<String, Button> accelerators = new HashMap<>();
@@ -36,7 +36,7 @@ public class CalcSkin extends Application {
36 36
     private DoubleProperty currentValue = new SimpleDoubleProperty();
37 37
     private CalcEngine calcEngine = new CalcEngine();
38 38
 
39
-    private enum Op { NOOP, ADD, SUBTRACT, MULTIPLY, DIVIDE }
39
+    private enum Op { NOOP, ADD, SUBTRACT, MULTIPLY, DIVIDE, SQUARE, SQUAREROOT, INVERT, EXPONENT}
40 40
 
41 41
     private Op curOp   = Op.NOOP;
42 42
     private Op stackOp = Op.NOOP;
@@ -84,7 +84,7 @@ public class CalcSkin extends Application {
84 84
         screen.setStyle("-fx-background-color: aquamarine;");
85 85
         screen.setAlignment(Pos.CENTER_RIGHT);
86 86
         screen.setEditable(false);
87
-        screen.textProperty().bind(Bindings.format("%.0f", currentValue));
87
+        screen.textProperty().bind(Bindings.format("%.3f", currentValue));
88 88
         return screen;
89 89
     }
90 90
 
@@ -127,10 +127,17 @@ public class CalcSkin extends Application {
127 127
             case "-": triggerOp.set(Op.SUBTRACT); break;
128 128
             case "*": triggerOp.set(Op.MULTIPLY); break;
129 129
             case "/": triggerOp.set(Op.DIVIDE);   break;
130
+            case "x^2": triggerOp.set(Op.SQUARE); break;
131
+            case "SqRt": triggerOp.set(Op.SQUAREROOT); break;
132
+            case "1/x": triggerOp.set(Op.INVERT); break;
133
+            case "x^y": triggerOp.set(Op.EXPONENT); break;
134
+
130 135
         }
131 136
         return triggerOp;
132 137
     }
133 138
 
139
+    //private void squareRoot(Button button)
140
+
134 141
     private void makeOperandButton(Button button, final ObjectProperty<Op> triggerOp) {
135 142
         button.setStyle("-fx-base: lightgray;");
136 143
         button.setOnAction(new EventHandler<ActionEvent>() {
@@ -185,10 +192,31 @@ public class CalcSkin extends Application {
185 192
                     case SUBTRACT: currentValue.set(calcEngine.subtract(previousValue.get(), currentValue.get())); break;
186 193
                     case MULTIPLY: currentValue.set(calcEngine.multiply(previousValue.get(), currentValue.get())); break;
187 194
                     case DIVIDE:   currentValue.set(calcEngine.divide(previousValue.get(), currentValue.get())); break;
195
+                    case SQUARE:   currentValue.set(calcEngine.square(currentValue.get())); break;
196
+                    case SQUAREROOT:   currentValue.set(calcEngine.squareRoot(currentValue.get())); break;
197
+                    case INVERT:   currentValue.set(calcEngine.invertNumber(currentValue.get())); break;
198
+                    case EXPONENT:   currentValue.set(calcEngine.exponent(previousValue.get(), currentValue.get())); break;
199
+
188 200
                 }
189 201
             }
190 202
         });
191 203
     }
204
+
205
+//    private void makeSquareButton(Button button) {
206
+//        button.setStyle("-fx-base: mistyrose;");
207
+//        button.setOnAction(new EventHandler<ActionEvent>() {
208
+//            @Override
209
+//            public void handle(ActionEvent actionEvent) {
210
+//                switch (stackOp) {
211
+//
212
+//                    case SQUARE:
213
+//                        currentValue.set(calcEngine.square(currentValue.get()));
214
+//                }
215
+//            }
216
+//        });
217
+//    }
218
+
219
+
192 220
 }
193 221
 
194 222
 

Binārs
src/test/.DS_Store Parādīt failu


Binārs
src/test/java/.DS_Store Parādīt failu


Binārs
src/test/java/rocks/.DS_Store Parādīt failu


Binārs
src/test/java/rocks/zipcode/.DS_Store Parādīt failu


+ 8
- 0
src/test/java/rocks/zipcode/calcskin/CalcEngineTest.java Parādīt failu

@@ -38,4 +38,12 @@ 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 square() {
44
+
45
+
46
+        Assert.assertEquals(25.0, testCalc.square(5.0));
47
+
48
+    }
41 49
 }