KickJava   Java API By Example, From Geeks To Geeks.

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


1 package Jt.examples.patterns;
2
3 import Jt.*;
4 import java.io.*;
5 import java.util.*;
6 import Jt.xml.*;
7
8 /**
9  * Calculator implementation based on the Jt Command pattern.
10  * Creating a subclass of JtCommand and implementing the processMessage
11  * method is basically all that is needed. Command requests are logged via
12  * the inherited LogMessage method.
13  */

14
15 public class Calculator extends JtCommand {
16
17
18   private int total = 0; // Calculator Total
19

20   public Calculator () {
21   }
22
23
24
25
26   /**
27     * Process object messages (Command requests).
28     * <ul>
29     * <li>JtADD - Add a number (msgContent) to the total
30     * </ul>
31     */

32
33   public Object JavaDoc processMessage (Object JavaDoc message) {
34
35    String JavaDoc msgid = null;
36    JtMessage msg = (JtMessage) message;
37    Object JavaDoc content;
38
39      if (msg == null)
40     return null;
41
42      // Retrieve Message ID and content
43

44      msgid = (String JavaDoc) msg.getMsgId ();
45
46      if (msgid == null)
47     return null;
48
49      content = msg.getMsgContent();
50
51      // Add a number to the total
52

53      if (msgid.equals ("ADD")) {
54
55         total += ((Integer JavaDoc) content).intValue ();
56         
57         // Log the message
58
logMessage (msg);
59         return (new Integer JavaDoc (total));
60      }
61
62      // JtRemove message (Remove Object)
63

64      if (msgid.equals ("JtREMOVE")) {
65        return (null);
66      }
67
68      handleError ("Calculator.processMessage: invalid message id:" + msgid);
69      return (null);
70   }
71
72
73  
74   /**
75    * Calculator implementation (main)
76    */

77
78   public static void main(String JavaDoc[] args) {
79
80     JtObject main = new JtFactory ();
81     JtMessage msg, msg1;
82     JtXMLHelper xmlHelper = new JtXMLHelper ();
83     Object JavaDoc total;
84     JtKeyboard keyboard;
85     String JavaDoc input;
86     int num = 0;
87
88     // Create calculator and Keyboard
89

90     main.createObject ("Jt.examples.patterns.Calculator", "calculator");
91     keyboard = (JtKeyboard) main.createObject ("Jt.JtKeyboard", "keyboard");
92
93     System.out.println ("Enter a number to be added to the total (or <CR> to exit):");
94
95     for (;;) {
96
97       // Read input (number) from the keyboard (JtACTIVATE message)
98

99       input = (String JavaDoc) main.sendMessage (keyboard, new JtMessage ("JtACTIVATE"));
100
101       input = input.trim ();
102
103       if ("".equals (input))
104         break;
105
106
107       try {
108         num = Integer.parseInt (input);
109       } catch (Exception JavaDoc e) {
110
111         System.err.println (e);
112       }
113
114
115       // Add the number to the Total (ADD Message)
116
msg = new JtMessage ("ADD");
117       msg.setMsgContent (new Integer JavaDoc (num));
118       total = main.sendMessage ("calculator", msg);
119
120       System.out.println ("Total:" + total);
121
122     }
123
124
125     // Print the log (list of requests executed by the Calculator)
126
// Use the xmlHelper to convert the log information (messageLog)
127
// into XML.
128

129     msg = new JtMessage ("JtCONVERT_OBJECT_TO_XML");
130     msg.setMsgContent (main.getValue ("calculator", "messageLog"));
131
132     System.out.println ("Log:\n" +
133       main.sendMessage (xmlHelper, msg));
134
135
136
137     // Remove object
138

139     main.removeObject ("calculator");
140
141   }
142
143 }
144
145
146
147
Popular Tags