javaquery/demo-calculator/src/main/java/org/apidesign/bck2brwsr/mavenhtml/App.java
changeset 197 e7bb314eec32
parent 142 74c37d9cfdc9
child 198 5c1604c5ca9a
     1.1 --- a/javaquery/demo-calculator/src/main/java/org/apidesign/bck2brwsr/mavenhtml/App.java	Fri Nov 09 12:09:40 2012 +0100
     1.2 +++ b/javaquery/demo-calculator/src/main/java/org/apidesign/bck2brwsr/mavenhtml/App.java	Thu Nov 22 00:18:34 2012 +0100
     1.3 @@ -22,19 +22,43 @@
     1.4  
     1.5  @Page(xhtml="Calculator.xhtml")
     1.6  public class App {
     1.7 -    private static final int OP_PLUS = 1;
     1.8 -    private static final int OP_MINUS = 2;
     1.9 -    private static final int OP_MUL = 3;
    1.10 -    private static final int OP_DIV = 4;
    1.11 -    
    1.12 -    static double memory = 0;
    1.13 -    static int operation = 0;
    1.14 -    
    1.15 -    
    1.16 +    private static double memory;
    1.17 +    private static String operation;
    1.18      
    1.19      @OnClick(id="clear")
    1.20      static void clear() {
    1.21 -        setValue(0.0);
    1.22 +        memory = 0;
    1.23 +        operation = null;
    1.24 +        Calculator.DISPLAY.setValue("0");
    1.25 +    }
    1.26 +    
    1.27 +    @OnClick(id= { "plus", "minus", "mul", "div" })
    1.28 +    static void applyOp(String op) {
    1.29 +        memory = getValue();
    1.30 +        operation = op;
    1.31 +        Calculator.DISPLAY.setValue("0");
    1.32 +    }
    1.33 +    
    1.34 +    @OnClick(id="result")
    1.35 +    static void computeTheValue() {
    1.36 +        switch (operation) {
    1.37 +            case "plus": setValue(memory + getValue()); break;
    1.38 +            case "minus": setValue(memory - getValue()); break;
    1.39 +            case "mul": setValue(memory * getValue()); break;
    1.40 +            case "div": setValue(memory / getValue()); break;
    1.41 +            default: throw new IllegalStateException(operation);
    1.42 +        }
    1.43 +    }
    1.44 +    
    1.45 +    @OnClick(id={"n0", "n1", "n2", "n3", "n4", "n5", "n6", "n7", "n8", "n9"}) 
    1.46 +    static void addDigit(String digit) {
    1.47 +        digit = digit.substring(1);
    1.48 +        String v = Calculator.DISPLAY.getValue();
    1.49 +        if (getValue() == 0.0) {
    1.50 +            Calculator.DISPLAY.setValue(digit);
    1.51 +        } else {
    1.52 +            Calculator.DISPLAY.setValue(v + digit);
    1.53 +        }
    1.54      }
    1.55      
    1.56      private static void setValue(double v) {
    1.57 @@ -42,50 +66,13 @@
    1.58          sb.append(v);
    1.59          Calculator.DISPLAY.setValue(sb.toString());
    1.60      }
    1.61 -    
    1.62 +
    1.63      private static double getValue() {
    1.64 -        return Double.parseDouble(Calculator.DISPLAY.getValue());
    1.65 -    }
    1.66 -    
    1.67 -    @OnClick(id="plus")
    1.68 -    static void plus() {
    1.69 -        memory = getValue();
    1.70 -        operation = OP_PLUS;
    1.71 -        setValue(0.0);
    1.72 -    }
    1.73 -    
    1.74 -    @OnClick(id="minus")
    1.75 -    static void minus() {
    1.76 -        memory = getValue();
    1.77 -        operation = OP_MINUS;
    1.78 -        setValue(0.0);
    1.79 -    }
    1.80 -    
    1.81 -    @OnClick(id="mul")
    1.82 -    static void mul() {
    1.83 -        memory = getValue();
    1.84 -        operation = OP_MUL;
    1.85 -        setValue(0.0);
    1.86 -    }
    1.87 -    
    1.88 -    @OnClick(id="result")
    1.89 -    static void computeTheValue() {
    1.90 -        switch (operation) {
    1.91 -            case 0: break;
    1.92 -            case OP_PLUS: setValue(memory + getValue()); break;
    1.93 -            case OP_MINUS: setValue(memory - getValue()); break;
    1.94 -            case OP_MUL: setValue(memory * getValue()); break;
    1.95 -        }
    1.96 -    }
    1.97 -    
    1.98 -    @OnClick(id={"n0", "n1", "n2", "n3", "n4", "n5", "n6", "n7", "n8", "n9"}) 
    1.99 -    static void addDigit(String digit) {
   1.100 -        digit = digit.substring(1);
   1.101 -        String v = Calculator.DISPLAY.getValue();
   1.102 -        if ("0".equals(v) || v == null) {
   1.103 -            Calculator.DISPLAY.setValue(digit);
   1.104 -        } else {
   1.105 -            Calculator.DISPLAY.setValue(v + digit);
   1.106 +        try {
   1.107 +            return Double.parseDouble(Calculator.DISPLAY.getValue());
   1.108 +        } catch (NumberFormatException ex) {
   1.109 +            Calculator.DISPLAY.setValue("err");
   1.110 +            return 0.0;
   1.111          }
   1.112      }
   1.113  }