Ver código fonte

buttons kind of work

Seth 6 anos atrás
pai
commit
c1498b8919

+ 1
- 4
README.md Ver arquivo

@@ -16,10 +16,7 @@ CalcEngine is a "fails all tests" class, and you should use the one from your pr
16 16
 See the MainApplication class to see how to call it from your __main__ method.
17 17
 
18 18
 Making a simple 4-function calculator should be trivial for you at this stage.
19
-
20
-*BUT*, how about adding a column of buttons for x-squared, square-root, inverse (1/x), and
21
-exponent (x^y).
22
-
19
+ 
23 20
 Then you might add a column of buttons that do the Memory functions.
24 21
 (What should you add to the UI to make this easier??)
25 22
 

+ 33
- 4
src/main/java/rocks/zipcode/calcskin/CalcEngine.java Ver arquivo

@@ -6,18 +6,47 @@ public class CalcEngine {
6 6
     }
7 7
 
8 8
     public double add(double v, double v1) {
9
-        return Double.NaN;
9
+
10
+        return v + v1;
10 11
     }
11 12
 
12 13
     public double subtract(double v, double v1) {
13
-        return Double.NaN;
14
+
15
+        return v - v1;
14 16
     }
15 17
 
16 18
     public double multiply(double v, double v1) {
17
-        return Double.NaN;
19
+
20
+           return v * v1;
18 21
     }
19 22
 
20 23
     public double divide(double v, double v1) {
21
-        return Double.NaN;
24
+
25
+        return v / v1;
26
+    }
27
+
28
+    public double exponent(double v, double v1){
29
+
30
+        return (Math.pow(v, v1));
31
+    }
32
+
33
+    public double squareRoot(double v) {
34
+
35
+        return (Math.sqrt(v));
36
+
22 37
     }
38
+
39
+    public double inverse(double v) {
40
+
41
+        return 1 / v;
42
+    }
43
+
44
+    public double square(double v) {
45
+
46
+        return Math.pow(v, 2);
47
+    }
48
+
49
+
50
+
51
+
23 52
 }

+ 25
- 11
src/main/java/rocks/zipcode/calcskin/CalcSkin.java Ver arquivo

@@ -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", "/", "x2" },
28
+            { "4", "5", "6", "*", "√x"},
29
+            { "1", "2", "3", "-", "1/x" },
30
+            { "0", "c", "=", "+", "x^y"}
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, ROOT, INVERSE, EXPO }
40 40
 
41 41
     private Op curOp   = Op.NOOP;
42 42
     private Op stackOp = Op.NOOP;
@@ -60,7 +60,7 @@ public class CalcSkin extends Application {
60 60
     private VBox createLayout(TextField screen, TilePane buttons) {
61 61
         final VBox layout = new VBox(20);
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: orange; -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());
@@ -81,7 +81,7 @@ public class CalcSkin extends Application {
81 81
 
82 82
     private TextField createScreen() {
83 83
         final TextField screen = new TextField();
84
-        screen.setStyle("-fx-background-color: aquamarine;");
84
+        screen.setStyle("-fx-background-color: red;");
85 85
         screen.setAlignment(Pos.CENTER_RIGHT);
86 86
         screen.setEditable(false);
87 87
         screen.textProperty().bind(Bindings.format("%.0f", currentValue));
@@ -114,12 +114,15 @@ public class CalcSkin extends Application {
114 114
                 makeClearButton(button);
115 115
             } else if ("=".equals(s)) {
116 116
                 makeEqualsButton(button);
117
+            } else if ("x2".equals(s)) {
118
+
117 119
             }
118 120
         }
119 121
 
120 122
         return button;
121 123
     }
122 124
 
125
+
123 126
     private ObjectProperty<Op> determineOperand(String s) {
124 127
         final ObjectProperty<Op> triggerOp = new SimpleObjectProperty<>(Op.NOOP);
125 128
         switch (s) {
@@ -127,12 +130,17 @@ 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 "x2":triggerOp.set(Op.SQUARE);   break;
134
+            case "√x":triggerOp.set(Op.ROOT);     break;
135
+            case "1/x":triggerOp.set(Op.INVERSE); break;
136
+            case "x^y":triggerOp.set(Op.EXPO);    break;
137
+
130 138
         }
131 139
         return triggerOp;
132 140
     }
133 141
 
134 142
     private void makeOperandButton(Button button, final ObjectProperty<Op> triggerOp) {
135
-        button.setStyle("-fx-base: lightgray;");
143
+        button.setStyle("-fx-base: #fdff00;");
136 144
         button.setOnAction(new EventHandler<ActionEvent>() {
137 145
             @Override
138 146
             public void handle(ActionEvent actionEvent) {
@@ -143,7 +151,7 @@ public class CalcSkin extends Application {
143 151
 
144 152
     private Button makeStandardButton(String s) {
145 153
         Button button = new Button(s);
146
-        button.setStyle("-fx-base: beige;");
154
+        button.setStyle("-fx-base: #b88cf5;");
147 155
         accelerators.put(s, button);
148 156
         button.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
149 157
         return button;
@@ -166,7 +174,7 @@ public class CalcSkin extends Application {
166 174
     }
167 175
 
168 176
     private void makeClearButton(Button button) {
169
-        button.setStyle("-fx-base: mistyrose;");
177
+        button.setStyle("-fx-base: rgba(169,255,131,1);");
170 178
         button.setOnAction(new EventHandler<ActionEvent>() {
171 179
             @Override
172 180
             public void handle(ActionEvent actionEvent) {
@@ -175,8 +183,10 @@ public class CalcSkin extends Application {
175 183
         });
176 184
     }
177 185
 
186
+
187
+
178 188
     private void makeEqualsButton(Button button) {
179
-        button.setStyle("-fx-base: ghostwhite;");
189
+        button.setStyle("-fx-base: #ff03e8;");
180 190
         button.setOnAction(new EventHandler<ActionEvent>() {
181 191
             @Override
182 192
             public void handle(ActionEvent actionEvent) {
@@ -185,6 +195,10 @@ public class CalcSkin extends Application {
185 195
                     case SUBTRACT: currentValue.set(calcEngine.subtract(previousValue.get(), currentValue.get())); break;
186 196
                     case MULTIPLY: currentValue.set(calcEngine.multiply(previousValue.get(), currentValue.get())); break;
187 197
                     case DIVIDE:   currentValue.set(calcEngine.divide(previousValue.get(), currentValue.get())); break;
198
+                    //case SQUARE:   currentValue.set(calcEngine.square(currentValue.get())); break;
199
+                    case ROOT:     currentValue.set(calcEngine.squareRoot(currentValue.get())); break;
200
+                    case INVERSE:  currentValue.set(calcEngine.inverse(currentValue.get())); break;
201
+                    case EXPO:     currentValue.set(calcEngine.exponent(previousValue.get(), currentValue.get())); break;
188 202
                 }
189 203
             }
190 204
         });