home *** CD-ROM | disk | FTP | other *** search
- package org.apache.cocoon.samples.flow.java;
-
- import org.apache.cocoon.components.flow.java.AbstractContinuable;
- import org.apache.cocoon.components.flow.java.VarMap;
-
- public class CalculatorFlow extends AbstractContinuable {
- private int count = 1;
-
- public void doCalculator() {
- float a = this.getNumber("a", 0.0F, 0.0F);
- float b = this.getNumber("b", a, 0.0F);
- String op = this.getOperator(a, b);
- if (op.equals("plus")) {
- this.sendResult(a, b, op, a + b);
- } else if (op.equals("minus")) {
- this.sendResult(a, b, op, a - b);
- } else if (op.equals("multiply")) {
- this.sendResult(a, b, op, a * b);
- } else if (op.equals("divide")) {
- if (b == 0.0F) {
- this.sendMessage("Error: Cannot divide by zero!");
- }
-
- this.sendResult(a, b, op, a / b);
- } else {
- this.sendMessage("Error: Unkown operator!");
- }
-
- ++this.count;
- }
-
- private float getNumber(String name, float a, float b) {
- String uri = "page/calculator-" + name.toLowerCase();
- this.sendPageAndWait(uri, (new VarMap()).add("a", a).add("b", b).add("count", this.count));
- float value = 0.0F;
-
- try {
- value = Float.parseFloat(this.getRequest().getParameter(name));
- } catch (Exception var7) {
- this.sendMessage("Error: \"" + this.getRequest().getParameter(name) + "\" is not a correct number!");
- }
-
- return value;
- }
-
- private String getOperator(float a, float b) {
- this.sendPageAndWait("page/calculator-operator", (new VarMap()).add("a", a).add("b", b).add("count", this.count));
- return this.getRequest().getParameter("operator");
- }
-
- private void sendResult(float a, float b, String op, float result) {
- this.sendPage("page/calculator-result", (new VarMap()).add("a", a).add("b", b).add("operator", op).add("result", result).add("count", this.count));
- }
-
- private void sendMessage(String message) {
- this.sendPageAndWait("page/calculator-message", (new VarMap()).add("message", message).add("count", this.count));
- }
- }
-