KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > Jt > examples > patterns > Calculator3


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 /**
11  * Calculator implementation based on Command, Memento, Strategy and Factory Method.
12  * Creating a subclass of JtCommand and implementing the processMessage
13  * method is basically all that is needed. Command requests are logged via
14  * the inherited logMessage() method.
15  */

16
17 public class Calculator3 extends JtCommand {
18
19   transient Object JavaDoc state = null; // Object state
20
private int total = 0; // Calculator Total
21
private JtMemento memento = null; // Contains the state of the object
22
// needed to implement the undo operation
23

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   // Save calculator state
37

38   private void saveState () {
39
40     JtMessage msg = new JtMessage ("JtENCODE_OBJECT");
41     JtObject tmp = new JtObject();
42     Object JavaDoc 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     // Use an instance of JtMemento to store the state
53

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 = (Calculator1) xmlHelper.processMessage (msg);
88
tmp = (Calculator3) copier.processMessage (msg);
89
90     msg = new JtMessage ("JtENCODE_OBJECT");
91     msg.setMsgContent (tmp);
92
93     //System.out.println ("...\n" + copier.processMessage (msg));
94
//System.out.println ("src...\n" + tmp.getMessageLog ());
95

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   /**
117     * Process object messages (Command requests).
118     * <ul>
119     * <li>ADD - Add a number (msgContent) to the total
120     * <li>MULTIPLY - Multiply a number (msgContent)
121     * </ul>
122     */

123
124   public Object JavaDoc processMessage (Object JavaDoc message) {
125
126    String JavaDoc msgid = null;
127    JtMessage msg = (JtMessage) message;
128    Object JavaDoc content;
129    JtMessage tmp;
130    Integer JavaDoc mres;
131
132      if (msg == null)
133     return null;
134
135      // Retrieve Message ID and content
136

137      msgid = (String JavaDoc) msg.getMsgId ();
138
139      if (msgid == null)
140     return null;
141
142      content = msg.getMsgContent();
143
144      // Add a number to the total
145

146      if (msgid.equals ("ADD")) {
147
148         // Saves the state of the object
149
saveState ();
150
151         // Log the request (message). This method is inherited from JtCommand
152

153         logMessage (msg);
154
155         total += ((Integer JavaDoc) content).intValue ();
156
157         return (new Integer JavaDoc (total));
158      }
159
160
161      if (msgid.equals ("MULTIPLY")) {
162
163
164         // Saves the state of the object
165
saveState ();
166
167         // Log the message
168
logMessage (msg);
169
170         // Put together the MULTIPLY message
171

172         tmp = new JtMessage ("MULTIPLY");
173         tmp.setMsgContent (new Integer JavaDoc (total));
174         tmp.setMsgData (content);
175
176         // the multiplication object (child of this object) is used
177
// to process multiplications.
178

179         mres = (Integer JavaDoc) sendMessage ("multiplication", tmp);
180
181         total = mres.intValue ();
182
183         return (new Integer JavaDoc (total));
184      }
185
186
187      if (msgid.equals ("UNDO")) {
188
189  
190         restoreState ();
191         
192         return (new Integer JavaDoc (total));
193      }
194
195      // JtRemove message (Remove Object)
196

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   /**
208    * Calculator implementation (main)
209    */

210
211   public static void main(String JavaDoc[] args) {
212
213     JtObject main = new JtFactory ();
214     JtMessage msg, msg1;
215     Object JavaDoc total;
216     JtKeyboard keyboard;
217     String JavaDoc input;
218     int num = 0;
219     Calculator3 calculator;
220     Multiplication multiplication;
221
222
223     // Create calculator, keyboard and multiplication
224

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 JavaDoc) 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 JavaDoc) 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       // Read input (number) from the keyboard (JtACTIVATE message)
288

289       input = (String JavaDoc) 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 JavaDoc e) {
301
302         System.err.println (e);
303         continue;
304       }
305
306
307       // Add the number to the Total (ADD Message)
308
//msg = new JtMessage ("ADD");
309
msg.setMsgContent (new Integer JavaDoc (num));
310       total = main.sendMessage ("calculator", msg);
311
312       System.out.println (">>> Total:" + total + " <<<");
313
314     }
315
316     // Print the log (list of requests executed by the calculator)
317
// Use main to convert the log information (messageLog)
318
// into XML.
319

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     // Remove object
328

329     main.removeObject ("calculator");
330
331   }
332
333 }
334
335
336
337
Popular Tags