KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > calculator > business > StateImpl


1 /*
2  * calculator
3  *
4  * Enhydra super-servlet specification object
5  *
6  */

7
8 package calculator.business;
9
10
11 import calculator.spec.State;
12
13 // Enhydra SuperServlet imports
14
import com.lutris.util.KeywordValueException;
15 import com.lutris.appserver.server.session.*;
16
17
18 public class StateImpl implements State{
19 //DigitsChack
20
public String JavaDoc checkDigits(SessionData sd)throws KeywordValueException
21   {
22     String JavaDoc digits = (String JavaDoc) sd.get("digits");
23
24     if (digits == null)
25       digits = "0";
26  
27     // If it's an integer, don't show the decimal point.
28
if (digits.endsWith(".0"))
29         digits = digits.substring(0, digits.length()-2);
30     
31     
32     // If it's too long to show, trim it or just show 0.
33
// An error indication would be nice to add (hint hint... :)
34

35     if (digits.indexOf(".") != -1) {
36       if (digits.length() > 6)
37          {
38       digits=digits.substring(0,7);
39        
40          int dot= digits.indexOf(".");
41       
42       if(dot==6)
43          digits = digits.substring(0,6);
44           
45        }
46     } else {
47       if (digits.length() > 7)
48         digits = "0"; // Overflow.
49
}
50     
51     return digits;
52   }
53
54
55 public String JavaDoc convertDigitsToHTML(String JavaDoc digits)
56   {
57    // Convert the string of digits to HTML.
58
String JavaDoc digitHtml = "";
59     for (int i=0; i<digits.length(); i++) {
60       char c = digits.charAt(i);
61       if (c == '0')
62         digitHtml += "<img SRC=media/zero.jpeg>";
63       else if (c == '1')
64         digitHtml += "<img SRC=media/one.jpeg>";
65       else if (c == '2')
66         digitHtml += "<img SRC=media/two.jpeg>";
67       else if (c == '3')
68         digitHtml += "<img SRC=media/three.jpeg>";
69       else if (c == '4')
70         digitHtml += "<img SRC=media/four.jpeg>";
71       else if (c == '5')
72         digitHtml += "<img SRC=media/five.jpeg>";
73       else if (c == '6')
74         digitHtml += "<img SRC=media/six.jpeg>";
75       else if (c == '7')
76         digitHtml += "<img SRC=media/seven.jpeg>";
77       else if (c == '8')
78         digitHtml += "<img SRC=media/eight.jpeg>";
79       else if (c == '9')
80         digitHtml += "<img SRC=media/nine.jpeg>";
81       else if (c == '-')
82         digitHtml += "<img SRC=media/dash.jpeg>";
83       else if (c == '.')
84         digitHtml += "<img SRC=media/point.jpeg>";
85     }
86     if (digitHtml.length() == 0)
87       digitHtml = "<img SRC=media/zero.jpeg>";
88
89     
90      return digitHtml;
91   }
92
93
94 }
Popular Tags