forked from asdfghjkai/STS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStockMarket.java
185 lines (139 loc) · 3.91 KB
/
StockMarket.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import java.io.*;
import java.util.*;
public class StockMarket implements Runnable
{
private static StockMarket ref;
private String[][] stockData;
private String filename = "stocks.csv";
private String delims = ",";
private String[] tokens;
private String[] registeredIDs = new String[20];
private Random rnd;
private final long PERIOD = 5000L;
private long lastTime;
private long currentTime;
private StockMarket()
{
stockData = new String[10][4];
populateStockData();
lastTime = System.currentTimeMillis() - PERIOD;
rnd = new Random();
}
public static StockMarket getStockMarket()
{
if(ref == null)
{
ref = new StockMarket();
}
return ref;
}
public boolean registerUser(String aID)
{
int count = 0;
while(registeredIDs[count] != null)
{
count++;
}
if(count < 20)
{
registeredIDs[count] = aID;
}
return true;
}
public String checkSharePrice(String aCompany)
{
String temp = "";
for(int i = 0; i < stockData.length; i++)
{
if(stockData[i][0].equals(aCompany))
{
temp = stockData[i][1];
break;
}
}
return temp;
}
public boolean checkID(String anID)
{
int count = 0;
while(registeredIDs[count] != null)
{
if(registeredIDs[count] == anID)
{
return true;
}
else
{
count++;
}
}
return false;
}
private void populateStockData()
{
String line;
int count = 0;
try
{
FileReader fileReader = new FileReader(filename);
BufferedReader in = new BufferedReader(fileReader);
while((line = in.readLine()) != null)
{
tokens = line.split(delims);
for(int i = 0; i < tokens.length; i++)
{
stockData[count][i] = tokens[i];
}
count++;
}
}
catch(Exception e)
{
System.out.println("Something went wrong: " + e);
}
for(int i = 0; i < 10; i++)
for(int j = 0; j < 3; j++)
System.out.println("populate: "+i+":"+j+" with: "+stockData[i][j]);
}
public String[][] getStockMarketState()
{
return stockData;
}
public void run()
{
while(true)
{
currentTime = System.currentTimeMillis();
if((currentTime - lastTime) >= PERIOD)
{
lastTime = currentTime;
System.out.println("Stock Market updated each 5s.");
updateStockPrice();
}
}
}
private void updateStockPrice()
{
double change = 0.0;
for(int i = 0; i < stockData.length; i++)
{
if(rnd.nextBoolean())
{
change = rnd.nextInt(11)*rnd.nextDouble();
double aVal = Double.parseDouble(stockData[i][1]);
if(rnd.nextBoolean())
{
aVal += change;
stockData[i][3] = ""+change;
}
else
{
aVal -= change;
stockData[i][3] = ""+(change * -1);
}
stockData[i][1] = ""+aVal;
System.out.println("UPD:"+stockData[i][0]+":"+stockData[i][1]+":"+stockData[i][3]);
}
}
}
}