001: /*
002: * Copyright 2005 Joe Walker
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.getahead.dwrdemo.gidemo;
017:
018: import java.math.BigDecimal;
019: import java.text.SimpleDateFormat;
020: import java.util.Date;
021: import java.util.Random;
022:
023: /**
024: * An object representing the stock price of a corporation.
025: * @author Joe Walker [joe at getahead dot ltd dot uk]
026: */
027: public class Corporation {
028: /**
029: * @param jsxid
030: * @param name
031: */
032: public Corporation(String jsxid, String name) {
033: this .jsxid = jsxid;
034: this .name = name;
035:
036: float temp = random.nextFloat() * 100.0F;
037: last = new BigDecimal(Math.round(temp * 100.0F) / 100.0F);
038: last = last.setScale(2, BigDecimal.ROUND_UP);
039: time = new Date();
040: change = new BigDecimal(0.0F);
041: change = change.setScale(2, BigDecimal.ROUND_UP);
042: max = last;
043: min = last;
044: }
045:
046: private String jsxid;
047:
048: private String name;
049:
050: private BigDecimal last;
051:
052: private Date time;
053:
054: private BigDecimal change;
055:
056: private BigDecimal max;
057:
058: private BigDecimal min;
059:
060: private static final Random random = new Random();
061:
062: /**
063: * @return the jsxid
064: */
065: public String getJsxid() {
066: return jsxid;
067: }
068:
069: /**
070: * @return the name
071: */
072: public String getName() {
073: return name;
074: }
075:
076: /**
077: * @return the last
078: */
079: public BigDecimal getLast() {
080: return last;
081: }
082:
083: /**
084: * @return the time
085: */
086: public String getTime() {
087: return FORMAT.format(time);
088: }
089:
090: /**
091: * @return the change
092: */
093: public BigDecimal getChange() {
094: return change;
095: }
096:
097: /**
098: * @return the max
099: */
100: public BigDecimal getMax() {
101: return max;
102: }
103:
104: /**
105: * @return the min
106: */
107: public BigDecimal getMin() {
108: return min;
109: }
110:
111: /**
112: * Alter the stock price
113: */
114: public void change() {
115: float newChange = (random.nextFloat() * 4) - 2;
116: newChange = Math.round(newChange * 100.0F) / 100.0F;
117:
118: if (last.floatValue() + newChange < 9) {
119: newChange = -newChange;
120: }
121:
122: change = new BigDecimal(newChange);
123: change = change.setScale(2, BigDecimal.ROUND_UP);
124:
125: last = last.add(change);
126:
127: if (last.compareTo(max) > 0) {
128: max = last;
129: }
130:
131: if (last.compareTo(min) < 0) {
132: min = last;
133: }
134:
135: time = new Date();
136: }
137:
138: private static final SimpleDateFormat FORMAT = new SimpleDateFormat(
139: "hh:MM:ss");
140: }
|