KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > flow > java > test > 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.components.flow.java.test;
17
18 import org.apache.cocoon.components.flow.java.*;
19
20 public class CalculatorFlow extends AbstractContinuable {
21
22     public void run() {
23
24         float a, b;
25         String JavaDoc op;
26         String JavaDoc uri = "page/";
27
28         sendPageAndWait(uri + "getNumberA");
29         a = Float.parseFloat(getRequest().getParameter("a"));
30         System.out.println("a=" + a);
31
32         sendPageAndWait(uri + "getNumberB", new VarMap().add("a", a));
33         b = Float.parseFloat(getRequest().getParameter("b"));
34         System.out.println("b=" + b);
35
36         sendPageAndWait(uri + "getOperator", new VarMap().add("a", a).add("b", b));
37         op = getRequest().getParameter("operator");
38         System.out.println("operator=" + op);
39
40         if ("plus".equals(op)) {
41             System.out.println("result=" + (a + b));
42             sendPage(uri + "displayResult", new VarMap().add("a", a).add("b", b).add("operator", op).add("result", a + b));
43         }
44         else if ("minus".equals(op)) {
45             System.out.println("result=" + (a - b));
46             sendPage(uri + "displayResult", new VarMap().add("a", a).add("b", b).add("operator", op).add("result", a - b));
47         }
48         else if ("multiply".equals(op)) {
49             System.out.println("result=" + (a * b));
50             sendPage(uri + "displayResult", new VarMap().add("a", a).add("b", b).add("operator", op).add("result", a * b));
51         }
52         else if ("divide".equals(op)) {
53             if (b == 0) {
54                 //sendPage("Error: Division by zero!");
55
}
56             else {
57                 System.out.println("result=" + (a / b));
58                 sendPage(uri + "displayResult", new VarMap().add("a", a).add("b", b).add("operator", op).add("result", a / b));
59             }
60         }
61         else {
62             //sendPage("Error: Unkown operator!");
63
}
64     }
65 }
66
Popular Tags