Browse Source

I broked it

NedRedmond 6 years ago
parent
commit
086c1a4663

+ 16
- 9
src/main/java/rocks/zipcode/calcskin/CalcEngine.java View File

@@ -5,19 +5,26 @@ public class CalcEngine {
5 5
     CalcEngine() {
6 6
     }
7 7
 
8
-    public double add(double v, double v1) {
9
-        return Double.NaN;
8
+    public double add(double x, double y) {
9
+        return x+y;
10 10
     }
11 11
 
12
-    public double subtract(double v, double v1) {
13
-        return Double.NaN;
12
+    public double subtract(double x, double y) {
13
+        return x-y;
14 14
     }
15 15
 
16
-    public double multiply(double v, double v1) {
17
-        return Double.NaN;
18
-    }
16
+    public double multiply(double x, double y) { return x*y; }
19 17
 
20
-    public double divide(double v, double v1) {
21
-        return Double.NaN;
18
+    public double divide(double x, double y) {
19
+        return x/y;
22 20
     }
21
+
22
+    public double squ(double x){ return Math.pow(x,2); }
23
+
24
+    public double sqrt(double x){ return Math.sqrt(x); }
25
+
26
+    public double exp(double x, double y){ return Math.pow(x,y); }
27
+
28
+    public double inv(double x){ return (1/x); }
29
+
23 30
 }

+ 30
- 6
src/main/java/rocks/zipcode/calcskin/CalcSkin.java View File

@@ -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²" },
28
+            { "4", "5", "6", "*", "√x" },
29
+            { "1", "2", "3", "-", "x⁻¹" },
30
+            { "0", "c", "=", "+", "xⁿ" }
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, EXPONENT }
40 40
 
41 41
     private Op curOp   = Op.NOOP;
42 42
     private Op stackOp = Op.NOOP;
@@ -114,6 +114,12 @@ public class CalcSkin extends Application {
114 114
                 makeClearButton(button);
115 115
             } else if ("=".equals(s)) {
116 116
                 makeEqualsButton(button);
117
+            } else if ("x²".equals(s)) {
118
+                makeSingleIntegerButton(button);
119
+            } else if ("x⁻¹".equals(s)) {
120
+                makeSingleIntegerButton(button);
121
+            } else if ("√x".equals(s)) {
122
+                makeSingleIntegerButton(button);
117 123
             }
118 124
         }
119 125
 
@@ -127,12 +133,16 @@ public class CalcSkin extends Application {
127 133
             case "-": triggerOp.set(Op.SUBTRACT); break;
128 134
             case "*": triggerOp.set(Op.MULTIPLY); break;
129 135
             case "/": triggerOp.set(Op.DIVIDE);   break;
136
+            case "x²": triggerOp.set(Op.SQUARE);  break;
137
+            case "√x": triggerOp.set(Op.SQRROOT);  break;
138
+            case "x⁻¹": triggerOp.set(Op.INVERSE);  break;
139
+            case "xⁿ": triggerOp.set(Op.EXPONENT);  break;
130 140
         }
131 141
         return triggerOp;
132 142
     }
133 143
 
134 144
     private void makeOperandButton(Button button, final ObjectProperty<Op> triggerOp) {
135
-        button.setStyle("-fx-base: lightgray;");
145
+        button.setStyle("-fx-base: azure;");
136 146
         button.setOnAction(new EventHandler<ActionEvent>() {
137 147
             @Override
138 148
             public void handle(ActionEvent actionEvent) {
@@ -175,6 +185,19 @@ public class CalcSkin extends Application {
175 185
         });
176 186
     }
177 187
 
188
+    private void makeSingleIntegerButton(Button button) {
189
+        button.setStyle("-fx-base: azure");
190
+        button.setOnAction(new EventHandler<ActionEvent>() {
191
+            @Override
192
+            public void handle(ActionEvent actionEvent) {
193
+            switch (stackOp) {
194
+                case SQUARE:    currentValue.set(calcEngine.squ(currentValue.get())); break;
195
+                case INVERSE:   currentValue.set(calcEngine.inv(currentValue.get())); break;
196
+                case SQRROOT:   currentValue.set(calcEngine.sqrt(currentValue.get())); break;
197
+            }
198
+        }});
199
+    }
200
+
178 201
     private void makeEqualsButton(Button button) {
179 202
         button.setStyle("-fx-base: ghostwhite;");
180 203
         button.setOnAction(new EventHandler<ActionEvent>() {
@@ -185,6 +208,7 @@ public class CalcSkin extends Application {
185 208
                     case SUBTRACT: currentValue.set(calcEngine.subtract(previousValue.get(), currentValue.get())); break;
186 209
                     case MULTIPLY: currentValue.set(calcEngine.multiply(previousValue.get(), currentValue.get())); break;
187 210
                     case DIVIDE:   currentValue.set(calcEngine.divide(previousValue.get(), currentValue.get())); break;
211
+                    case EXPONENT: currentValue.set(calcEngine.exp(previousValue.get(), currentValue.get())); break;
188 212
                 }
189 213
             }
190 214
         });

+ 0
- 1
src/main/java/rocks/zipcode/calcskin/MainApplication.java View File

@@ -5,7 +5,6 @@ import rocks.zipcode.calcskin.CalcSkin;
5 5
 // You probably should make NO changes here. Keep it simple.
6 6
 public class MainApplication {
7 7
     public static void main(String[] args){
8
-
9 8
         CalcSkin.launchCalc(args);
10 9
     }
11 10