# HG changeset patch # User Jaroslav Tulach # Date 1353539914 -3600 # Node ID e7bb314eec325400d1493a848c7b3debb1199e13 # Parent e7ea061e669c9bf3bd97198ed076fb2837b1f2ac Less verbose. Concentrating all operations into single method. Using string switch to determine what operation to run. diff -r e7ea061e669c -r e7bb314eec32 javaquery/demo-calculator/pom.xml --- a/javaquery/demo-calculator/pom.xml Thu Nov 22 00:08:57 2012 +0100 +++ b/javaquery/demo-calculator/pom.xml Thu Nov 22 00:18:34 2012 +0100 @@ -51,8 +51,8 @@ maven-compiler-plugin 2.3.2 - 1.6 - 1.6 + 1.7 + 1.7 diff -r e7ea061e669c -r e7bb314eec32 javaquery/demo-calculator/src/main/java/org/apidesign/bck2brwsr/mavenhtml/App.java --- a/javaquery/demo-calculator/src/main/java/org/apidesign/bck2brwsr/mavenhtml/App.java Thu Nov 22 00:08:57 2012 +0100 +++ b/javaquery/demo-calculator/src/main/java/org/apidesign/bck2brwsr/mavenhtml/App.java Thu Nov 22 00:18:34 2012 +0100 @@ -22,19 +22,43 @@ @Page(xhtml="Calculator.xhtml") public class App { - private static final int OP_PLUS = 1; - private static final int OP_MINUS = 2; - private static final int OP_MUL = 3; - private static final int OP_DIV = 4; - - static double memory = 0; - static int operation = 0; - - + private static double memory; + private static String operation; @OnClick(id="clear") static void clear() { - setValue(0.0); + memory = 0; + operation = null; + Calculator.DISPLAY.setValue("0"); + } + + @OnClick(id= { "plus", "minus", "mul", "div" }) + static void applyOp(String op) { + memory = getValue(); + operation = op; + Calculator.DISPLAY.setValue("0"); + } + + @OnClick(id="result") + static void computeTheValue() { + switch (operation) { + case "plus": setValue(memory + getValue()); break; + case "minus": setValue(memory - getValue()); break; + case "mul": setValue(memory * getValue()); break; + case "div": setValue(memory / getValue()); break; + default: throw new IllegalStateException(operation); + } + } + + @OnClick(id={"n0", "n1", "n2", "n3", "n4", "n5", "n6", "n7", "n8", "n9"}) + static void addDigit(String digit) { + digit = digit.substring(1); + String v = Calculator.DISPLAY.getValue(); + if (getValue() == 0.0) { + Calculator.DISPLAY.setValue(digit); + } else { + Calculator.DISPLAY.setValue(v + digit); + } } private static void setValue(double v) { @@ -42,50 +66,13 @@ sb.append(v); Calculator.DISPLAY.setValue(sb.toString()); } - + private static double getValue() { - return Double.parseDouble(Calculator.DISPLAY.getValue()); - } - - @OnClick(id="plus") - static void plus() { - memory = getValue(); - operation = OP_PLUS; - setValue(0.0); - } - - @OnClick(id="minus") - static void minus() { - memory = getValue(); - operation = OP_MINUS; - setValue(0.0); - } - - @OnClick(id="mul") - static void mul() { - memory = getValue(); - operation = OP_MUL; - setValue(0.0); - } - - @OnClick(id="result") - static void computeTheValue() { - switch (operation) { - case 0: break; - case OP_PLUS: setValue(memory + getValue()); break; - case OP_MINUS: setValue(memory - getValue()); break; - case OP_MUL: setValue(memory * getValue()); break; - } - } - - @OnClick(id={"n0", "n1", "n2", "n3", "n4", "n5", "n6", "n7", "n8", "n9"}) - static void addDigit(String digit) { - digit = digit.substring(1); - String v = Calculator.DISPLAY.getValue(); - if ("0".equals(v) || v == null) { - Calculator.DISPLAY.setValue(digit); - } else { - Calculator.DISPLAY.setValue(v + digit); + try { + return Double.parseDouble(Calculator.DISPLAY.getValue()); + } catch (NumberFormatException ex) { + Calculator.DISPLAY.setValue("err"); + return 0.0; } } } diff -r e7ea061e669c -r e7bb314eec32 javaquery/demo-calculator/src/main/resources/org/apidesign/bck2brwsr/mavenhtml/Calculator.xhtml --- a/javaquery/demo-calculator/src/main/resources/org/apidesign/bck2brwsr/mavenhtml/Calculator.xhtml Thu Nov 22 00:08:57 2012 +0100 +++ b/javaquery/demo-calculator/src/main/resources/org/apidesign/bck2brwsr/mavenhtml/Calculator.xhtml Thu Nov 22 00:18:34 2012 +0100 @@ -22,12 +22,32 @@ Simple Calculator in HTML5 and Java - + + + +

Java & HTML5

- + @@ -57,5 +77,12 @@
+