-
Notifications
You must be signed in to change notification settings - Fork 1
/
StockDB.java
64 lines (49 loc) · 1.75 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
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
/**
*
* @author Hishan Indrajith
*/
public class StockDB {
private static HashMap<String,StockItem> stockItemMap;
private String csvFile = "stocks.csv";
private FileReader fileRd=null;
private BufferedReader reader=null;
public StockDB() {
stockItemMap =new HashMap<String,StockItem>();
try {
fileRd = new FileReader(csvFile);
reader = new BufferedReader(fileRd);
String [] tokens;
reader.readLine();
for(String line = reader.readLine(); line != null; line = reader.readLine()) {
tokens = line.split(",");
String price = tokens[tokens.length-1];
StockItem item = new StockItem(tokens[0],Double.parseDouble(price),tokens[1]);
stockItemMap.put(tokens[0],item);
}
if(fileRd != null) fileRd.close();
if(reader != null) reader.close();
} catch (FileNotFoundException ex) {
}catch (IOException ex) {
}
}
public static StockItem getStockItem(String symbol){
return stockItemMap.get(symbol);
}
public static void setPrice(String symbol,double price){
StockItem item = stockItemMap.get(symbol);
item.setPrice(price);
stockItemMap.put(symbol, item);
}
public static boolean isExist(String symbol){
return stockItemMap.containsKey(symbol);
}
public static Collection<StockItem> getValues(){
return stockItemMap.values();
}
}