KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > calculator > business > CalculatorManagerImpl


1 /*
2  * calculator
3  *
4  * Enhydra super-servlet specification object
5  *
6  */

7
8 package calculator.business;
9
10
11 import calculator.spec.CalculatorManager;
12
13 import java.math.BigDecimal JavaDoc;
14 import com.lutris.appserver.server.session.*;
15
16 import java.io.IOException JavaDoc;
17
18 // Enhydra SuperServlet imports
19

20 import com.lutris.util.KeywordValueException;
21
22 public class CalculatorManagerImpl implements CalculatorManager{
23
24
25  /**
26    * Add a digit to the current display. We might be looking at a
27    * "stale" number (if overwrite is set). If so, then clear the
28    * display and save the current number for later use.
29    */

30   public void addDigit(SessionData sd, String JavaDoc button) throws KeywordValueException {
31     
32     String JavaDoc digits = (String JavaDoc) sd.get("digits");
33     if (digits == null){
34       digits = "0";
35     }
36     if (sd.get("overwrite") != null) {
37       try {
38         sd.set("pending_num", new Float JavaDoc(digits));
39       } catch (NumberFormatException JavaDoc e) {
40       }
41       digits = "";
42       sd.remove("overwrite");
43     }
44     digits += button;
45     sd.set("digits", digits);
46   }
47
48  /**
49    * Add a decimal point to the current display. If it already has a
50    * decimal point, then do nothing.
51    */

52   public void addPoint(SessionData sd)
53       throws KeywordValueException {
54     String JavaDoc digits = (String JavaDoc) sd.get("digits");
55     if (digits == null)
56       digits = "0";
57     if (sd.get("overwrite") != null) {
58       try {
59         sd.set("pending_num", new Float JavaDoc(digits));
60       } catch (NumberFormatException JavaDoc e) {
61       }
62       digits = "";
63       sd.remove("overwrite");
64     }
65     if (digits.indexOf(".") != -1)
66       return;
67     digits += ".";
68     sd.set("digits", digits);
69   }
70
71   /**
72    * Flip the sign of the number currently in the display.
73    */

74   public void negate(SessionData sd)
75       throws KeywordValueException {
76     String JavaDoc digits = (String JavaDoc) sd.get("digits");
77     if (digits == null)
78       return;
79     if (digits.startsWith("-"))
80       digits = digits.substring(1);
81     else
82       digits = "-" + digits;
83     sd.set("digits", digits);
84   }
85
86   /**
87    * If there is a "pending number" (not in the current display),
88    * and there is a "pending operation" (add, subtract etc...),
89    * then use them and the current contents of the display and do some
90    * math. Leave the result in the current display.
91    */

92   public void doEquals(SessionData sd)
93       throws KeywordValueException {
94     String JavaDoc op = (String JavaDoc) sd.get("pending_op");
95     if (op == null)
96       return;
97     Float JavaDoc f = (Float JavaDoc) sd.get("pending_num");
98     if (f == null)
99       f = new Float JavaDoc("0.0");
100     float f2 = f.floatValue();
101
102     String JavaDoc digits = (String JavaDoc) sd.get("digits");
103     if (digits == null)
104       digits = "0";
105     Float JavaDoc f3 = new Float JavaDoc(0);
106     try {
107       f3 = new Float JavaDoc(digits);
108     } catch (NumberFormatException JavaDoc e) {
109     }
110     float f4 = f3.floatValue();
111     double result = 0.0;
112
113     if (op.equals("+")) {
114       result = f2 + f4;
115     } else if (op.equals("-")) {
116       result = f2 - f4;
117     } else if (op.equals("*")) {
118       result = f2 * f4;
119     } else if (op.equals("/")) {
120       result = f2 / f4;
121     }
122
123     BigDecimal JavaDoc bd = new BigDecimal JavaDoc(result);
124     sd.set("digits", bd.toString());
125     sd.remove("pending_op");
126     sd.remove("pending_num");
127     sd.set("overwrite", Boolean.TRUE);
128   }
129
130   /**
131    * Remember that a math function was pushed.
132    */

133   public void doFunction(SessionData sd, String JavaDoc op)
134       throws KeywordValueException {
135     if (sd.get("pending_num") != null) {
136       doEquals(sd);
137     } else {
138       String JavaDoc digits = (String JavaDoc) sd.get("digits");
139       if (digits == null)
140         digits = "0";
141     }
142     sd.set("pending_op", op);
143     sd.set("overwrite", Boolean.TRUE);
144   }
145
146 public void clear(SessionData sd) throws KeywordValueException{
147
148     sd.set("digits", "0");
149     sd.remove("pending_op");
150     sd.set("pending_num", new Float JavaDoc(0.0));
151     sd.set("overwrite", Boolean.TRUE);
152
153    }
154
155 }
Popular Tags