#31 tkhong102

Open
tkhong102 wants to merge 1 commits from tkhong102/CalcSkin:master into master

+ 20
- 6
src/main/java/rocks/zipcode/calcskin/CalcEngine.java View File

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

+ 52
- 2
src/main/java/rocks/zipcode/calcskin/CalcSkin.java View File

@@ -24,6 +24,8 @@ public class CalcSkin extends Application {
24 24
         launch(args);
25 25
     }
26 26
     private static final String[][] template = {
27
+            {"sin", "cos", "tan", "-x"},
28
+            { "x²", "√", "xⁿ", "1/x"},
27 29
             { "7", "8", "9", "/" },
28 30
             { "4", "5", "6", "*" },
29 31
             { "1", "2", "3", "-" },
@@ -36,7 +38,8 @@ public class CalcSkin extends Application {
36 38
     private DoubleProperty currentValue = new SimpleDoubleProperty();
37 39
     private CalcEngine calcEngine = new CalcEngine();
38 40
 
39
-    private enum Op { NOOP, ADD, SUBTRACT, MULTIPLY, DIVIDE }
41
+    private enum Op { NOOP, ADD, SUBTRACT, MULTIPLY, DIVIDE, SQUARE, SQUAREROOT, EXPONENT,
42
+    INVERSE, INVERT, FINDSIN, FINDCOS, FINDTAN}
40 43
 
41 44
     private Op curOp   = Op.NOOP;
42 45
     private Op stackOp = Op.NOOP;
@@ -127,6 +130,15 @@ public class CalcSkin extends Application {
127 130
             case "-": triggerOp.set(Op.SUBTRACT); break;
128 131
             case "*": triggerOp.set(Op.MULTIPLY); break;
129 132
             case "/": triggerOp.set(Op.DIVIDE);   break;
133
+            case "x²": triggerOp.set(Op.SQUARE);   break;
134
+            case "√": triggerOp.set(Op.SQUAREROOT); break;
135
+            case "xⁿ": triggerOp.set(Op.EXPONENT); break;
136
+            case "1/x": triggerOp.set(Op.INVERSE); break;
137
+            case "sin": triggerOp.set(Op.FINDSIN); break;
138
+            case "cos": triggerOp.set(Op.FINDCOS); break;
139
+            case "tan": triggerOp.set(Op.FINDTAN); break;
140
+            case "-x": triggerOp.set(Op.INVERT); break;
141
+
130 142
         }
131 143
         return triggerOp;
132 144
     }
@@ -136,7 +148,37 @@ public class CalcSkin extends Application {
136 148
         button.setOnAction(new EventHandler<ActionEvent>() {
137 149
             @Override
138 150
             public void handle(ActionEvent actionEvent) {
139
-                curOp = triggerOp.get();
151
+                if (triggerOp.get() == Op.SQUARE) {
152
+                    currentValue.set(calcEngine.square(currentValue.get()));
153
+                    stackOp = curOp;
154
+                    curOp = Op.NOOP;
155
+                }else if(triggerOp.get() ==  Op.SQUAREROOT){
156
+                    currentValue.set(calcEngine.squareroot(currentValue.get()));
157
+                    stackOp = curOp;
158
+                    curOp = Op.NOOP;
159
+                }else if(triggerOp.get() ==  Op.FINDCOS){
160
+                    currentValue.set(calcEngine.findCos(currentValue.get()));
161
+                    stackOp = curOp;
162
+                    curOp = Op.NOOP;
163
+                }else if(triggerOp.get() ==  Op.FINDSIN){
164
+                    currentValue.set(calcEngine.findSin(currentValue.get()));
165
+                    stackOp = curOp;
166
+                    curOp = Op.NOOP;
167
+                }else if(triggerOp.get() ==  Op.FINDTAN){
168
+                    currentValue.set(calcEngine.findTan(currentValue.get()));
169
+                    stackOp = curOp;
170
+                    curOp = Op.NOOP;
171
+                }else if(triggerOp.get() ==  Op.INVERSE){
172
+                    currentValue.set(calcEngine.inverse(currentValue.get()));
173
+                    stackOp = curOp;
174
+                    curOp = Op.NOOP;
175
+                }else if(triggerOp.get() ==  Op.INVERT){
176
+                    currentValue.set(calcEngine.invert(currentValue.get()));
177
+                    stackOp = curOp;
178
+                    curOp = Op.NOOP;
179
+                } else {
180
+                    curOp = triggerOp.get();
181
+                }
140 182
             }
141 183
         });
142 184
     }
@@ -185,6 +227,14 @@ public class CalcSkin extends Application {
185 227
                     case SUBTRACT: currentValue.set(calcEngine.subtract(previousValue.get(), currentValue.get())); break;
186 228
                     case MULTIPLY: currentValue.set(calcEngine.multiply(previousValue.get(), currentValue.get())); break;
187 229
                     case DIVIDE:   currentValue.set(calcEngine.divide(previousValue.get(), currentValue.get())); break;
230
+                    case SQUARE:   currentValue.set(calcEngine.square(currentValue.get())); break;
231
+                    case SQUAREROOT: currentValue.set(calcEngine.squareroot(previousValue.get())); break;
232
+                    case INVERSE: currentValue.set(calcEngine.inverse(currentValue.get())); break;
233
+                    case INVERT: currentValue.set(calcEngine.invert(currentValue.get())); break;
234
+                    case FINDSIN: currentValue.set(calcEngine.findSin(currentValue.get())); break;
235
+                    case FINDCOS: currentValue.set(calcEngine.findCos(currentValue.get())); break;
236
+                    case FINDTAN: currentValue.set(calcEngine.findTan(currentValue.get())); break;
237
+                    case EXPONENT: currentValue.set(calcEngine.exponent(previousValue.get(), currentValue.get())); break;
188 238
                 }
189 239
             }
190 240
         });