-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStockDB.java
65 lines (51 loc) · 1.93 KB
/
StockDB.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
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Random;
//database for stock items. details are read from stocks.csv file
public class StockDB {
private static HashMap<String, StockUnit> mapToDB;
private String csvFile = "stocks.csv";
private BufferedReader bRead = null;
private int rangeMin = 1;
private int rangeMax = 10;
Random rand = new Random();
// symbol is used as the key of hash map
public StockDB() {
mapToDB = new HashMap<String, StockUnit>();
try {
bRead = new BufferedReader(new FileReader(csvFile));
String line = null;
while ((line = bRead.readLine()) != null) {
String str[] = line.split(",");
// price value is a random value
double price = Math.round((rangeMin + (rangeMax - rangeMin) * rand.nextDouble()) * 100.0) / 100.0;
StockUnit newItem = new StockUnit(price, str[1], str[0]);
mapToDB.put(str[0], newItem);
}
if (bRead != null)
bRead.close();
} catch (FileNotFoundException e) {
} catch (IOException e) {
}
}
public static StockUnit getStockUnit(String symbol) {
return mapToDB.get(symbol);
}
public static void setPrice(String symbol, double price) {
StockUnit item = mapToDB.get(symbol);
item.setPrice(price);
mapToDB.put(symbol, item); // put updated price in stock database
}
//check if symbol is exists
public static boolean isExist(String symbol) {
return mapToDB.containsKey(symbol);
}
//get the details of one stock item
public static Collection<StockUnit> getValues() {
return mapToDB.values();
}
}