1 2 package Jt.examples.patterns; 3 4 import Jt.*; 5 import java.io.*; 6 import java.util.*; 7 import Jt.xml.*; 8 import java.beans.*; 9 10 16 17 public class Calculator3 extends JtCommand { 18 19 transient Object state = null; private int total = 0; private JtMemento memento = null; 24 25 public Calculator3 () { 26 } 27 28 public void setTotal (int total) { 29 this.total = total; 30 } 31 32 public int getTotal () { 33 return (total); 34 } 35 36 38 private void saveState () { 39 40 JtMessage msg = new JtMessage ("JtENCODE_OBJECT"); 41 JtObject tmp = new JtObject(); 42 Object aux; 43 44 msg.setMsgContent (this); 45 aux = tmp.processMessage (msg); 46 47 if (aux == null) { 48 handleWarning ("saveSate: Unable to save the current state"); 49 return; 50 } 51 52 54 if (memento == null) 55 memento = new JtMemento (); 56 57 memento.setState (aux); 58 59 handleTrace ("saveState: saving state ...\n" + aux); 60 61 } 62 63 64 private void restoreState () { 65 66 JtMessage msg = new JtMessage ("JtDECODE_OBJECT"); 67 Calculator3 tmp; 68 JtObject copier = new JtObject (); 69 70 71 if (memento == null) { 72 handleWarning ("Warning: nothing to undo ...."); 73 return; 74 } 75 76 state = memento.getState (); 77 memento = null; 78 79 if (state == null) { 80 handleWarning ("Warning: nothing to undo ...."); 81 return; 82 } 83 84 handleTrace ("restoreState (state) ...\n" + state); 85 86 msg.setMsgContent (state); 87 tmp = (Calculator3) copier.processMessage (msg); 89 90 msg = new JtMessage ("JtENCODE_OBJECT"); 91 msg.setMsgContent (tmp); 92 93 96 97 msg = new JtMessage ("JtCOPY_OBJECT"); 98 99 if (tmp == null) { 100 handleTrace ("restoreState failed: unable to convert object from XML"); 101 return; 102 } 103 104 msg.setMsgContent (tmp); 105 msg.setMsgData (this); 106 sendMessage (copier, msg); 107 108 msg = new JtMessage ("JtENCODE_OBJECT"); 109 msg.setMsgContent (this); 110 111 112 } 113 114 115 116 123 124 public Object processMessage (Object message) { 125 126 String msgid = null; 127 JtMessage msg = (JtMessage) message; 128 Object content; 129 JtMessage tmp; 130 Integer mres; 131 132 if (msg == null) 133 return null; 134 135 137 msgid = (String ) msg.getMsgId (); 138 139 if (msgid == null) 140 return null; 141 142 content = msg.getMsgContent(); 143 144 146 if (msgid.equals ("ADD")) { 147 148 saveState (); 150 151 153 logMessage (msg); 154 155 total += ((Integer ) content).intValue (); 156 157 return (new Integer (total)); 158 } 159 160 161 if (msgid.equals ("MULTIPLY")) { 162 163 164 saveState (); 166 167 logMessage (msg); 169 170 172 tmp = new JtMessage ("MULTIPLY"); 173 tmp.setMsgContent (new Integer (total)); 174 tmp.setMsgData (content); 175 176 179 mres = (Integer ) sendMessage ("multiplication", tmp); 180 181 total = mres.intValue (); 182 183 return (new Integer (total)); 184 } 185 186 187 if (msgid.equals ("UNDO")) { 188 189 190 restoreState (); 191 192 return (new Integer (total)); 193 } 194 195 197 if (msgid.equals ("JtREMOVE")) { 198 return (null); 199 } 200 201 handleError ("Calculator3.processMessage: invalid message id:" + msgid); 202 return (null); 203 } 204 205 206 207 210 211 public static void main(String [] args) { 212 213 JtObject main = new JtFactory (); 214 JtMessage msg, msg1; 215 Object total; 216 JtKeyboard keyboard; 217 String input; 218 int num = 0; 219 Calculator3 calculator; 220 Multiplication multiplication; 221 222 223 225 calculator = (Calculator3) main.createObject ("Jt.examples.patterns.Calculator3", "calculator"); 226 keyboard = (JtKeyboard) main.createObject ("Jt.JtKeyboard", "keyboard"); 227 multiplication = (Multiplication) calculator.createObject 228 ("Jt.examples.patterns.Multiplication", "multiplication"); 229 230 do { 231 System.out.println ("Please select a multiplication strategy ..."); 232 System.out.println ("1. Standard multiplication"); 233 System.out.println ("2. Repetitive addition"); 234 235 input = (String ) main.sendMessage (keyboard, new JtMessage ("JtACTIVATE")); 236 237 if (input == null) 238 continue; 239 240 input = input.trim (); 241 242 if (input.equals ("1")) { 243 main.setValue (multiplication, "concreteStrategy", new MultiplicationA ()); 244 break; 245 } else if (input.equals ("2")) { 246 main.setValue (multiplication, "concreteStrategy", new MultiplicationB ()); 247 break; 248 } 249 } while (true); 250 251 for (;;) { 252 253 System.out.println ("Please select an operation ..."); 254 System.out.println ("1. Addition"); 255 System.out.println ("2. Multiplication"); 256 System.out.println ("3. Undo the last operation"); 257 System.out.println ("4. Exit"); 258 259 260 input = (String ) main.sendMessage (keyboard, new JtMessage ("JtACTIVATE")); 261 if (input == null) 262 continue; 263 264 input = input.trim (); 265 266 if (input.equals ("4")) 267 break; 268 269 if ("3".equals (input)) { 270 msg = new JtMessage ("UNDO"); 271 main.sendMessage ("calculator", msg); 272 System.out.println 273 (">>> Total:" + main.getValue ("calculator", "total") + " <<<"); 274 continue; 275 } 276 277 if (input.equals ("1")) { 278 msg = new JtMessage ("ADD"); 279 } else if (input.equals ("2")) { 280 msg = new JtMessage ("MULTIPLY"); 281 } else 282 continue; 283 284 System.out.print ("Enter a number --> "); 285 286 287 289 input = (String ) main.sendMessage (keyboard, new JtMessage ("JtACTIVATE")); 290 291 if (input != null) 292 input = input.trim (); 293 294 if (input.equals ("")) 295 continue; 296 297 298 try { 299 num = Integer.parseInt (input); 300 } catch (Exception e) { 301 302 System.err.println (e); 303 continue; 304 } 305 306 307 msg.setMsgContent (new Integer (num)); 310 total = main.sendMessage ("calculator", msg); 311 312 System.out.println (">>> Total:" + total + " <<<"); 313 314 } 315 316 320 msg = new JtMessage ("JtENCODE_OBJECT"); 321 msg.setMsgContent (main.getValue ("calculator", "messageLog")); 322 323 System.out.println ("Log:\n" + 324 main.sendMessage (main, msg)); 325 326 327 329 main.removeObject ("calculator"); 330 331 } 332 333 } 334 335 336 337 | Popular Tags |