001: /*
002: * calculator
003: *
004: * Enhydra super-servlet specification object
005: *
006: */
007:
008: package calculator.business;
009:
010: import calculator.spec.CalculatorManager;
011:
012: import java.math.BigDecimal;
013: import com.lutris.appserver.server.session.*;
014:
015: import java.io.IOException;
016:
017: // Enhydra SuperServlet imports
018:
019: import com.lutris.util.KeywordValueException;
020:
021: public class CalculatorManagerImpl implements CalculatorManager {
022:
023: /**
024: * Add a digit to the current display. We might be looking at a
025: * "stale" number (if overwrite is set). If so, then clear the
026: * display and save the current number for later use.
027: */
028: public void addDigit(SessionData sd, String button)
029: throws KeywordValueException {
030:
031: String digits = (String) sd.get("digits");
032: if (digits == null) {
033: digits = "0";
034: }
035: if (sd.get("overwrite") != null) {
036: try {
037: sd.set("pending_num", new Float(digits));
038: } catch (NumberFormatException e) {
039: }
040: digits = "";
041: sd.remove("overwrite");
042: }
043: digits += button;
044: sd.set("digits", digits);
045: }
046:
047: /**
048: * Add a decimal point to the current display. If it already has a
049: * decimal point, then do nothing.
050: */
051: public void addPoint(SessionData sd) throws KeywordValueException {
052: String digits = (String) sd.get("digits");
053: if (digits == null)
054: digits = "0";
055: if (sd.get("overwrite") != null) {
056: try {
057: sd.set("pending_num", new Float(digits));
058: } catch (NumberFormatException e) {
059: }
060: digits = "";
061: sd.remove("overwrite");
062: }
063: if (digits.indexOf(".") != -1)
064: return;
065: digits += ".";
066: sd.set("digits", digits);
067: }
068:
069: /**
070: * Flip the sign of the number currently in the display.
071: */
072: public void negate(SessionData sd) throws KeywordValueException {
073: String digits = (String) sd.get("digits");
074: if (digits == null)
075: return;
076: if (digits.startsWith("-"))
077: digits = digits.substring(1);
078: else
079: digits = "-" + digits;
080: sd.set("digits", digits);
081: }
082:
083: /**
084: * If there is a "pending number" (not in the current display),
085: * and there is a "pending operation" (add, subtract etc...),
086: * then use them and the current contents of the display and do some
087: * math. Leave the result in the current display.
088: */
089: public void doEquals(SessionData sd) throws KeywordValueException {
090: String op = (String) sd.get("pending_op");
091: if (op == null)
092: return;
093: Float f = (Float) sd.get("pending_num");
094: if (f == null)
095: f = new Float("0.0");
096: float f2 = f.floatValue();
097:
098: String digits = (String) sd.get("digits");
099: if (digits == null)
100: digits = "0";
101: Float f3 = new Float(0);
102: try {
103: f3 = new Float(digits);
104: } catch (NumberFormatException e) {
105: }
106: float f4 = f3.floatValue();
107: double result = 0.0;
108:
109: if (op.equals("+")) {
110: result = f2 + f4;
111: } else if (op.equals("-")) {
112: result = f2 - f4;
113: } else if (op.equals("*")) {
114: result = f2 * f4;
115: } else if (op.equals("/")) {
116: result = f2 / f4;
117: }
118:
119: BigDecimal bd = new BigDecimal(result);
120: sd.set("digits", bd.toString());
121: sd.remove("pending_op");
122: sd.remove("pending_num");
123: sd.set("overwrite", Boolean.TRUE);
124: }
125:
126: /**
127: * Remember that a math function was pushed.
128: */
129: public void doFunction(SessionData sd, String op)
130: throws KeywordValueException {
131: if (sd.get("pending_num") != null) {
132: doEquals(sd);
133: } else {
134: String digits = (String) sd.get("digits");
135: if (digits == null)
136: digits = "0";
137: }
138: sd.set("pending_op", op);
139: sd.set("overwrite", Boolean.TRUE);
140: }
141:
142: public void clear(SessionData sd) throws KeywordValueException {
143:
144: sd.set("digits", "0");
145: sd.remove("pending_op");
146: sd.set("pending_num", new Float(0.0));
147: sd.set("overwrite", Boolean.TRUE);
148:
149: }
150:
151: }
|