Explorar el Código

Completed CalcSkin

Chaitali Patel hace 6 años
padre
commit
9bc4768d56

+ 21
- 4
src/main/java/rocks/zipcode/calcskin/CalcEngine.java Ver fichero

@@ -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 22
     }
23
+
24
+    public double sqrt(double v) {
25
+        return Math.sqrt(v);
26
+    }
27
+
28
+    public double average(double v, double v1) {
29
+        return (v + v1) /2;
30
+    }
31
+
32
+    public double exponent(double v) {
33
+        return Math.exp(v);
34
+    }
35
+
36
+    public static double log(double V) {
37
+       return Math.pow(10, V);
38
+    }
39
+
23 40
 }

+ 15
- 6
src/main/java/rocks/zipcode/calcskin/CalcSkin.java Ver fichero

@@ -12,7 +12,6 @@ import javafx.scene.input.KeyEvent;
12 12
 import javafx.scene.layout.*;
13 13
 import javafx.stage.Stage;
14 14
 import javafx.stage.StageStyle;
15
-import rocks.zipcode.calcskin.CalcEngine;
16 15
 
17 16
 import java.util.HashMap;
18 17
 import java.util.Map;
@@ -24,10 +23,12 @@ public class CalcSkin extends Application {
24 23
         launch(args);
25 24
     }
26 25
     private static final String[][] template = {
26
+            {"sqrt","exp","log","c"},
27 27
             { "7", "8", "9", "/" },
28 28
             { "4", "5", "6", "*" },
29 29
             { "1", "2", "3", "-" },
30
-            { "0", "c", "=", "+" }
30
+            {"0","avg", "=", "+" }
31
+
31 32
     };
32 33
 
33 34
     private final Map<String, Button> accelerators = new HashMap<>();
@@ -36,7 +37,7 @@ public class CalcSkin extends Application {
36 37
     private DoubleProperty currentValue = new SimpleDoubleProperty();
37 38
     private CalcEngine calcEngine = new CalcEngine();
38 39
 
39
-    private enum Op { NOOP, ADD, SUBTRACT, MULTIPLY, DIVIDE }
40
+    private enum Op { NOOP, ADD, SUBTRACT, MULTIPLY, DIVIDE, ROOT, AVERAGE, EXPONENT, LOG}
40 41
 
41 42
     private Op curOp   = Op.NOOP;
42 43
     private Op stackOp = Op.NOOP;
@@ -50,7 +51,7 @@ public class CalcSkin extends Application {
50 51
         final TextField screen  = createScreen();
51 52
         final TilePane  buttons = createButtons();
52 53
 
53
-        stage.setTitle("Calc");
54
+        stage.setTitle("<> Calculator <>");
54 55
         stage.initStyle(StageStyle.UTILITY);
55 56
         stage.setResizable(false);
56 57
         stage.setScene(new Scene(createLayout(screen, buttons)));
@@ -58,7 +59,7 @@ public class CalcSkin extends Application {
58 59
     }
59 60
 
60 61
     private VBox createLayout(TextField screen, TilePane buttons) {
61
-        final VBox layout = new VBox(20);
62
+        final VBox layout = new VBox(30);
62 63
         layout.setAlignment(Pos.CENTER);
63 64
         layout.setStyle("-fx-background-color: silver; -fx-padding: 20; -fx-font-size: 20;");
64 65
         layout.getChildren().setAll(screen, buttons);
@@ -82,7 +83,7 @@ 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 88
         screen.textProperty().bind(Bindings.format("%.0f", currentValue));
88 89
         return screen;
@@ -127,6 +128,10 @@ public class CalcSkin extends Application {
127 128
             case "-": triggerOp.set(Op.SUBTRACT); break;
128 129
             case "*": triggerOp.set(Op.MULTIPLY); break;
129 130
             case "/": triggerOp.set(Op.DIVIDE);   break;
131
+            case "Sqrt": triggerOp.set(Op.ROOT); break;
132
+            case "log": triggerOp.set(Op.LOG); break;
133
+            case "exp": triggerOp.set(Op.EXPONENT); break;
134
+
130 135
         }
131 136
         return triggerOp;
132 137
     }
@@ -185,6 +190,10 @@ public class CalcSkin extends Application {
185 190
                     case SUBTRACT: currentValue.set(calcEngine.subtract(previousValue.get(), currentValue.get())); break;
186 191
                     case MULTIPLY: currentValue.set(calcEngine.multiply(previousValue.get(), currentValue.get())); break;
187 192
                     case DIVIDE:   currentValue.set(calcEngine.divide(previousValue.get(), currentValue.get())); break;
193
+                    case ROOT:   currentValue.set(calcEngine.sqrt(previousValue.get())); break;
194
+                    case AVERAGE:  currentValue.set(calcEngine.average(previousValue.get(), currentValue.get())); break;
195
+                    case EXPONENT:   currentValue.set(calcEngine.exponent(previousValue.get())); break;
196
+                    case LOG:   currentValue.set(calcEngine.log(previousValue.get())); break;
188 197
                 }
189 198
             }
190 199
         });

+ 21
- 0
src/test/java/rocks/zipcode/calcskin/CalcEngineTest.java Ver fichero

@@ -38,4 +38,25 @@ 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 sqrt() {
44
+        Assert.assertTrue("", (testCalc.sqrt(25.0 ) == 5.0));
45
+    }
46
+
47
+    @Test
48
+    public void average() {
49
+        Assert.assertTrue("", ((testCalc.average(25.0, 5.0)/2) != 5.0));
50
+    }
51
+
52
+    @Test
53
+    public void exponent() {
54
+        Assert.assertTrue("", (testCalc.sqrt(25.0 ) == 5.0));
55
+    }
56
+
57
+    @Test
58
+    public void log() {
59
+        Assert.assertTrue("", (testCalc.sqrt(25.0 ) == 5.0));
60
+    }
61
+
41 62
 }