1 16 package org.apache.cocoon.samples.flow.java; 17 18 import org.apache.cocoon.components.flow.java.AbstractContinuable; 19 import org.apache.cocoon.components.flow.java.VarMap; 20 21 public class CalculatorFlow extends AbstractContinuable { 22 23 private int count = 1; 24 25 public void doCalculator() { 26 float a = getNumber("a", 0f, 0f); 27 float b = getNumber("b", a, 0f); 28 String op = getOperator(a, b); 29 30 if (op.equals("plus")) { 31 sendResult(a, b, op, a + b); 32 } else if (op.equals("minus")) { 33 sendResult(a, b, op, a - b); 34 } else if (op.equals("multiply")) { 35 sendResult(a, b, op, a * b); 36 } else if (op.equals("divide")) { 37 if (b==0f) 38 sendMessage("Error: Cannot divide by zero!"); 39 sendResult(a, b, op, a / b); 40 } else { 41 sendMessage("Error: Unkown operator!"); 42 } 43 44 count++; 45 } 46 47 private float getNumber(String name, float a, float b) { 48 String uri = "page/calculator-" + name.toLowerCase(); 49 sendPageAndWait(uri, new VarMap().add("a", a).add("b", b).add("count", count)); 50 51 float value = 0f; 52 try { 53 value = Float.parseFloat(getRequest().getParameter(name)); 54 } catch (Exception e) { 55 sendMessage("Error: \""+getRequest().getParameter(name)+"\" is not a correct number!"); 56 } 57 return value; 58 } 59 60 private String getOperator(float a, float b) { 61 sendPageAndWait("page/calculator-operator", new VarMap().add("a", a).add("b", b).add("count", count)); 62 return getRequest().getParameter("operator"); 63 } 64 65 private void sendResult(float a, float b, String op, float result) { 66 sendPage("page/calculator-result", new VarMap().add("a", a).add("b", b).add("operator", op).add("result", result).add("count", count)); 67 } 68 69 private void sendMessage(String message) { 70 sendPageAndWait("page/calculator-message", new VarMap().add("message", message).add("count", count)); 71 } 72 } 73 | Popular Tags |