KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > samples > flow > java > CalculatorFlow


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 JavaDoc 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 JavaDoc name, float a, float b) {
48         String JavaDoc 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 JavaDoc e) {
55             sendMessage("Error: \""+getRequest().getParameter(name)+"\" is not a correct number!");
56         }
57         return value;
58     }
59
60     private String JavaDoc 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 JavaDoc 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 JavaDoc message) {
70         sendPageAndWait("page/calculator-message", new VarMap().add("message", message).add("count", count));
71     }
72 }
73
Popular Tags