소스 검색

Completed CalcSkin

Chaitali Patel 6 년 전
부모
커밋
9bc4768d56

+ 21
- 4
src/main/java/rocks/zipcode/calcskin/CalcEngine.java 파일 보기

6
     }
6
     }
7
 
7
 
8
     public double add(double v, double v1) {
8
     public double add(double v, double v1) {
9
-        return Double.NaN;
9
+        return (v + v1);
10
     }
10
     }
11
 
11
 
12
     public double subtract(double v, double v1) {
12
     public double subtract(double v, double v1) {
13
-        return Double.NaN;
13
+        return (v - v1);
14
     }
14
     }
15
 
15
 
16
     public double multiply(double v, double v1) {
16
     public double multiply(double v, double v1) {
17
-        return Double.NaN;
17
+        return (v * v1);
18
     }
18
     }
19
 
19
 
20
     public double divide(double v, double v1) {
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 파일 보기

12
 import javafx.scene.layout.*;
12
 import javafx.scene.layout.*;
13
 import javafx.stage.Stage;
13
 import javafx.stage.Stage;
14
 import javafx.stage.StageStyle;
14
 import javafx.stage.StageStyle;
15
-import rocks.zipcode.calcskin.CalcEngine;
16
 
15
 
17
 import java.util.HashMap;
16
 import java.util.HashMap;
18
 import java.util.Map;
17
 import java.util.Map;
24
         launch(args);
23
         launch(args);
25
     }
24
     }
26
     private static final String[][] template = {
25
     private static final String[][] template = {
26
+            {"sqrt","exp","log","c"},
27
             { "7", "8", "9", "/" },
27
             { "7", "8", "9", "/" },
28
             { "4", "5", "6", "*" },
28
             { "4", "5", "6", "*" },
29
             { "1", "2", "3", "-" },
29
             { "1", "2", "3", "-" },
30
-            { "0", "c", "=", "+" }
30
+            {"0","avg", "=", "+" }
31
+
31
     };
32
     };
32
 
33
 
33
     private final Map<String, Button> accelerators = new HashMap<>();
34
     private final Map<String, Button> accelerators = new HashMap<>();
36
     private DoubleProperty currentValue = new SimpleDoubleProperty();
37
     private DoubleProperty currentValue = new SimpleDoubleProperty();
37
     private CalcEngine calcEngine = new CalcEngine();
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
     private Op curOp   = Op.NOOP;
42
     private Op curOp   = Op.NOOP;
42
     private Op stackOp = Op.NOOP;
43
     private Op stackOp = Op.NOOP;
50
         final TextField screen  = createScreen();
51
         final TextField screen  = createScreen();
51
         final TilePane  buttons = createButtons();
52
         final TilePane  buttons = createButtons();
52
 
53
 
53
-        stage.setTitle("Calc");
54
+        stage.setTitle("<> Calculator <>");
54
         stage.initStyle(StageStyle.UTILITY);
55
         stage.initStyle(StageStyle.UTILITY);
55
         stage.setResizable(false);
56
         stage.setResizable(false);
56
         stage.setScene(new Scene(createLayout(screen, buttons)));
57
         stage.setScene(new Scene(createLayout(screen, buttons)));
58
     }
59
     }
59
 
60
 
60
     private VBox createLayout(TextField screen, TilePane buttons) {
61
     private VBox createLayout(TextField screen, TilePane buttons) {
61
-        final VBox layout = new VBox(20);
62
+        final VBox layout = new VBox(30);
62
         layout.setAlignment(Pos.CENTER);
63
         layout.setAlignment(Pos.CENTER);
63
         layout.setStyle("-fx-background-color: silver; -fx-padding: 20; -fx-font-size: 20;");
64
         layout.setStyle("-fx-background-color: silver; -fx-padding: 20; -fx-font-size: 20;");
64
         layout.getChildren().setAll(screen, buttons);
65
         layout.getChildren().setAll(screen, buttons);
82
     private TextField createScreen() {
83
     private TextField createScreen() {
83
         final TextField screen = new TextField();
84
         final TextField screen = new TextField();
84
         screen.setStyle("-fx-background-color: aquamarine;");
85
         screen.setStyle("-fx-background-color: aquamarine;");
85
-        screen.setAlignment(Pos.CENTER_RIGHT);
86
+        screen.setAlignment(Pos.CENTER_LEFT);
86
         screen.setEditable(false);
87
         screen.setEditable(false);
87
         screen.textProperty().bind(Bindings.format("%.0f", currentValue));
88
         screen.textProperty().bind(Bindings.format("%.0f", currentValue));
88
         return screen;
89
         return screen;
127
             case "-": triggerOp.set(Op.SUBTRACT); break;
128
             case "-": triggerOp.set(Op.SUBTRACT); break;
128
             case "*": triggerOp.set(Op.MULTIPLY); break;
129
             case "*": triggerOp.set(Op.MULTIPLY); break;
129
             case "/": triggerOp.set(Op.DIVIDE);   break;
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
         return triggerOp;
136
         return triggerOp;
132
     }
137
     }
185
                     case SUBTRACT: currentValue.set(calcEngine.subtract(previousValue.get(), currentValue.get())); break;
190
                     case SUBTRACT: currentValue.set(calcEngine.subtract(previousValue.get(), currentValue.get())); break;
186
                     case MULTIPLY: currentValue.set(calcEngine.multiply(previousValue.get(), currentValue.get())); break;
191
                     case MULTIPLY: currentValue.set(calcEngine.multiply(previousValue.get(), currentValue.get())); break;
187
                     case DIVIDE:   currentValue.set(calcEngine.divide(previousValue.get(), currentValue.get())); break;
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 파일 보기

38
     public void divide() {
38
     public void divide() {
39
         Assert.assertTrue("", (testCalc.divide(10.0, 2.0) == 5.0));
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
 }