home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / xampp / xampp-cocoon-addon-1.4.9-installer.exe / CalculatorFlow.class (.txt) < prev    next >
Encoding:
Java Class File  |  2004-07-12  |  2.8 KB  |  59 lines

  1. package org.apache.cocoon.samples.flow.java;
  2.  
  3. import org.apache.cocoon.components.flow.java.AbstractContinuable;
  4. import org.apache.cocoon.components.flow.java.VarMap;
  5.  
  6. public class CalculatorFlow extends AbstractContinuable {
  7.    private int count = 1;
  8.  
  9.    public void doCalculator() {
  10.       float a = this.getNumber("a", 0.0F, 0.0F);
  11.       float b = this.getNumber("b", a, 0.0F);
  12.       String op = this.getOperator(a, b);
  13.       if (op.equals("plus")) {
  14.          this.sendResult(a, b, op, a + b);
  15.       } else if (op.equals("minus")) {
  16.          this.sendResult(a, b, op, a - b);
  17.       } else if (op.equals("multiply")) {
  18.          this.sendResult(a, b, op, a * b);
  19.       } else if (op.equals("divide")) {
  20.          if (b == 0.0F) {
  21.             this.sendMessage("Error: Cannot divide by zero!");
  22.          }
  23.  
  24.          this.sendResult(a, b, op, a / b);
  25.       } else {
  26.          this.sendMessage("Error: Unkown operator!");
  27.       }
  28.  
  29.       ++this.count;
  30.    }
  31.  
  32.    private float getNumber(String name, float a, float b) {
  33.       String uri = "page/calculator-" + name.toLowerCase();
  34.       this.sendPageAndWait(uri, (new VarMap()).add("a", a).add("b", b).add("count", this.count));
  35.       float value = 0.0F;
  36.  
  37.       try {
  38.          value = Float.parseFloat(this.getRequest().getParameter(name));
  39.       } catch (Exception var7) {
  40.          this.sendMessage("Error: \"" + this.getRequest().getParameter(name) + "\" is not a correct number!");
  41.       }
  42.  
  43.       return value;
  44.    }
  45.  
  46.    private String getOperator(float a, float b) {
  47.       this.sendPageAndWait("page/calculator-operator", (new VarMap()).add("a", a).add("b", b).add("count", this.count));
  48.       return this.getRequest().getParameter("operator");
  49.    }
  50.  
  51.    private void sendResult(float a, float b, String op, float result) {
  52.       this.sendPage("page/calculator-result", (new VarMap()).add("a", a).add("b", b).add("operator", op).add("result", result).add("count", this.count));
  53.    }
  54.  
  55.    private void sendMessage(String message) {
  56.       this.sendPageAndWait("page/calculator-message", (new VarMap()).add("message", message).add("count", this.count));
  57.    }
  58. }
  59.