javaquery/demo-calculator/src/main/java/org/apidesign/bck2brwsr/mavenhtml/App.java
Less verbose. Concentrating all operations into single method. Using string switch to determine what operation to run.
2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 2 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. Look for COPYING file in the top folder.
16 * If not, see http://opensource.org/licenses/GPL-2.0.
18 package org.apidesign.bck2brwsr.mavenhtml;
20 import org.apidesign.bck2brwsr.htmlpage.api.OnClick;
21 import org.apidesign.bck2brwsr.htmlpage.api.Page;
23 @Page(xhtml="Calculator.xhtml")
25 private static double memory;
26 private static String operation;
32 Calculator.DISPLAY.setValue("0");
35 @OnClick(id= { "plus", "minus", "mul", "div" })
36 static void applyOp(String op) {
39 Calculator.DISPLAY.setValue("0");
43 static void computeTheValue() {
45 case "plus": setValue(memory + getValue()); break;
46 case "minus": setValue(memory - getValue()); break;
47 case "mul": setValue(memory * getValue()); break;
48 case "div": setValue(memory / getValue()); break;
49 default: throw new IllegalStateException(operation);
53 @OnClick(id={"n0", "n1", "n2", "n3", "n4", "n5", "n6", "n7", "n8", "n9"})
54 static void addDigit(String digit) {
55 digit = digit.substring(1);
56 String v = Calculator.DISPLAY.getValue();
57 if (getValue() == 0.0) {
58 Calculator.DISPLAY.setValue(digit);
60 Calculator.DISPLAY.setValue(v + digit);
64 private static void setValue(double v) {
65 StringBuilder sb = new StringBuilder();
67 Calculator.DISPLAY.setValue(sb.toString());
70 private static double getValue() {
72 return Double.parseDouble(Calculator.DISPLAY.getValue());
73 } catch (NumberFormatException ex) {
74 Calculator.DISPLAY.setValue("err");