1 7 8 package calculator.business; 9 10 11 import calculator.spec.CalculatorManager; 12 13 import java.math.BigDecimal ; 14 import com.lutris.appserver.server.session.*; 15 16 import java.io.IOException ; 17 18 20 import com.lutris.util.KeywordValueException; 21 22 public class CalculatorManagerImpl implements CalculatorManager{ 23 24 25 30 public void addDigit(SessionData sd, String button) throws KeywordValueException { 31 32 String digits = (String ) 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 (digits)); 39 } catch (NumberFormatException e) { 40 } 41 digits = ""; 42 sd.remove("overwrite"); 43 } 44 digits += button; 45 sd.set("digits", digits); 46 } 47 48 52 public void addPoint(SessionData sd) 53 throws KeywordValueException { 54 String digits = (String ) 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 (digits)); 60 } catch (NumberFormatException 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 74 public void negate(SessionData sd) 75 throws KeywordValueException { 76 String digits = (String ) 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 92 public void doEquals(SessionData sd) 93 throws KeywordValueException { 94 String op = (String ) sd.get("pending_op"); 95 if (op == null) 96 return; 97 Float f = (Float ) sd.get("pending_num"); 98 if (f == null) 99 f = new Float ("0.0"); 100 float f2 = f.floatValue(); 101 102 String digits = (String ) sd.get("digits"); 103 if (digits == null) 104 digits = "0"; 105 Float f3 = new Float (0); 106 try { 107 f3 = new Float (digits); 108 } catch (NumberFormatException 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 bd = new BigDecimal (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 133 public void doFunction(SessionData sd, String op) 134 throws KeywordValueException { 135 if (sd.get("pending_num") != null) { 136 doEquals(sd); 137 } else { 138 String digits = (String ) 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 (0.0)); 151 sd.set("overwrite", Boolean.TRUE); 152 153 } 154 155 } | Popular Tags |