KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > getahead > dwrdemo > gidemo > Corporation


1 /*
2  * Copyright 2005 Joe Walker
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.getahead.dwrdemo.gidemo;
17
18 import java.math.BigDecimal JavaDoc;
19 import java.text.SimpleDateFormat JavaDoc;
20 import java.util.Date JavaDoc;
21 import java.util.Random JavaDoc;
22
23 /**
24  * An object representing the stock price of a corporation.
25  * @author Joe Walker [joe at getahead dot ltd dot uk]
26  */

27 public class Corporation
28 {
29     /**
30      * @param jsxid
31      * @param name
32      * @param last
33      * @param change
34      */

35     public Corporation(String JavaDoc jsxid, String JavaDoc name)
36     {
37         this.jsxid = jsxid;
38         this.name = name;
39
40         float temp = random.nextFloat() * 100.0F;
41         last = new BigDecimal JavaDoc(Math.round(temp * 100.0F) / 100.0F);
42         last = last.setScale(2, BigDecimal.ROUND_UP);
43         time = new Date JavaDoc();
44         change = new BigDecimal JavaDoc(0.0F);
45         change = change.setScale(2, BigDecimal.ROUND_UP);
46         max = last;
47         min = last;
48     }
49
50     private String JavaDoc jsxid;
51
52     private String JavaDoc name;
53
54     private BigDecimal JavaDoc last;
55
56     private Date JavaDoc time;
57
58     private BigDecimal JavaDoc change;
59
60     private BigDecimal JavaDoc max;
61
62     private BigDecimal JavaDoc min;
63
64     private Random JavaDoc random = new Random JavaDoc();
65
66     /**
67      * @return the jsxid
68      */

69     public String JavaDoc getJsxid()
70     {
71         return jsxid;
72     }
73
74     /**
75      * @return the name
76      */

77     public String JavaDoc getName()
78     {
79         return name;
80     }
81
82     /**
83      * @return the last
84      */

85     public BigDecimal JavaDoc getLast()
86     {
87         return last;
88     }
89
90     /**
91      * @return the time
92      */

93     public String JavaDoc getTime()
94     {
95         return FORMAT.format(time);
96     }
97
98     /**
99      * @return the change
100      */

101     public BigDecimal JavaDoc getChange()
102     {
103         return change;
104     }
105
106     /**
107      * @return the max
108      */

109     public BigDecimal JavaDoc getMax()
110     {
111         return max;
112     }
113
114     /**
115      * @return the min
116      */

117     public BigDecimal JavaDoc getMin()
118     {
119         return min;
120     }
121
122     /**
123      * Alter the stock price
124      */

125     public void change()
126     {
127         float newChange = (random.nextFloat() * 4) - 2;
128         newChange = Math.round(newChange * 100.0F) / 100.0F;
129
130         if (last.floatValue() + newChange < 9)
131         {
132             newChange = -newChange;
133         }
134
135         change = new BigDecimal JavaDoc(newChange);
136         change = change.setScale(2, BigDecimal.ROUND_UP);
137
138         last = last.add(change);
139
140         if (last.compareTo(max) > 0)
141         {
142             max = last;
143         }
144
145         if (last.compareTo(min) < 0)
146         {
147             min = last;
148         }
149
150         time = new Date JavaDoc();
151     }
152
153     private static final SimpleDateFormat JavaDoc FORMAT = new SimpleDateFormat JavaDoc("hh:MM:ss");
154 }
155
Popular Tags