Kate Moore 6 anni fa
parent
commit
0491741193

+ 21
- 4
src/main/java/rocks/zipcode/calcskin/CalcEngine.java Vedi File

@@ -6,18 +6,35 @@ 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
+    }
23
+
24
+    public double squareRoot(double v1, double v) {
25
+        return Math.sqrt(v);
26
+    }
27
+
28
+    public double exponent(double v, double v1){
29
+        return Math.pow(v, v1);
30
+    }
31
+
32
+    public double invert(double v, double v1) {
33
+        return v * -1.0;
34
+    }
35
+
36
+    public double square(double v, double v1) {
37
+        return v * v;
22 38
     }
23 39
 }
40
+

+ 17
- 9
src/main/java/rocks/zipcode/calcskin/CalcSkin.java Vedi File

@@ -18,16 +18,16 @@ import java.util.HashMap;
18 18
 import java.util.Map;
19 19
 
20 20
 // a simple JavaFX calculator.
21
-public class CalcSkin extends Application {
21
+public class CalcSkin extends Application{
22 22
 
23 23
     public static void main(String[] args){
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", "/", "S" },
28
+            { "4", "5", "6", "*", "SR" },
29
+            { "1", "2", "3", "-", "INV" },
30
+            { "0", "c", "=", "+", "EXP" }
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, SQUARE_ROOT, EXPONENT, INVERT }
40 40
 
41 41
     private Op curOp   = Op.NOOP;
42 42
     private Op stackOp = Op.NOOP;
@@ -51,16 +51,16 @@ public class CalcSkin extends Application {
51 51
         final TilePane  buttons = createButtons();
52 52
 
53 53
         stage.setTitle("Calc");
54
-        stage.initStyle(StageStyle.UTILITY);
54
+        stage.initStyle(StageStyle.DECORATED);
55 55
         stage.setResizable(false);
56 56
         stage.setScene(new Scene(createLayout(screen, buttons)));
57 57
         stage.show();
58 58
     }
59 59
 
60 60
     private VBox createLayout(TextField screen, TilePane buttons) {
61
-        final VBox layout = new VBox(20);
61
+        final VBox layout = new VBox(70);
62 62
         layout.setAlignment(Pos.CENTER);
63
-        layout.setStyle("-fx-background-color: silver; -fx-padding: 20; -fx-font-size: 20;");
63
+        layout.setStyle("-fx-background-color: lightBlue; -fx-padding: 20; -fx-font-size: 20;");
64 64
         layout.getChildren().setAll(screen, buttons);
65 65
         handleAccelerators(layout);
66 66
         screen.prefWidthProperty().bind(buttons.widthProperty());
@@ -127,6 +127,10 @@ 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 "S": triggerOp.set(Op.SQUARE);   break;
131
+            case "SR" : triggerOp.set(Op.SQUARE_ROOT); break;
132
+            case "EXP" : triggerOp.set(Op.EXPONENT); break;
133
+            case "INV" : triggerOp.set(Op.INVERT);  break;
130 134
         }
131 135
         return triggerOp;
132 136
     }
@@ -185,6 +189,10 @@ public class CalcSkin extends Application {
185 189
                     case SUBTRACT: currentValue.set(calcEngine.subtract(previousValue.get(), currentValue.get())); break;
186 190
                     case MULTIPLY: currentValue.set(calcEngine.multiply(previousValue.get(), currentValue.get())); break;
187 191
                     case DIVIDE:   currentValue.set(calcEngine.divide(previousValue.get(), currentValue.get())); break;
192
+                    case SQUARE:   currentValue.set(calcEngine.square(previousValue.get(), currentValue.get())); break;
193
+                    case SQUARE_ROOT: currentValue.set(calcEngine.squareRoot(previousValue.get(), currentValue.get())); break;
194
+                    case EXPONENT: currentValue.set(calcEngine.exponent(previousValue.get(), currentValue.get())); break;
195
+                    case INVERT:   currentValue.set(calcEngine.invert(previousValue.get(), currentValue.get())); break;
188 196
                 }
189 197
             }
190 198
         });