-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStockDataBase.java
78 lines (61 loc) · 2 KB
/
StockDataBase.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
import java.io.*;
import java.util.*;
class StockDataBase {
private Map<String, StockItem> stockList;
private String [] fields;
public StockDataBase(String csvFile, String key, String val1,String val2) {
FileReader fileRd=null;
BufferedReader reader=null;
try {
fileRd = new FileReader(csvFile);
reader = new BufferedReader(fileRd);
/* read the CSV file's first line which has
* the names of fields.
*/
String header = reader.readLine();
fields = header.split(",");// keep field names
// find where the key and the value are
int keyIndex = findIndexOf(key);
int val1Index = findIndexOf(val1);
int val2Index = findIndexOf(val2);
if(keyIndex == -1 || val1Index == -1 || val2Index == -1)
{
throw new IOException("CSV file does not have data");
}
// note how you can throw a new exception
// get a new hash map
stockList = new HashMap<String, StockItem>();
/* read each line, getting it split by ,
* use the indexes to get the key and value
*/
String [] tokens;
for(String line = reader.readLine();
line != null;
line = reader.readLine()) {
tokens = line.split(",");
StockItem entree = new StockItem(tokens[val1Index], Double.parseDouble(tokens[val2Index]));
stockList.put(tokens[keyIndex], entree);
}
if(fileRd != null) fileRd.close();
if(reader != null) reader.close();
// I can catch more than one exceptions
} catch (IOException e) {
System.out.println(e);
System.exit(-1);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Malformed CSV file");
System.out.println(e);
}
}
private int findIndexOf(String key) {
for(int i=0; i < fields.length; i++)
if(fields[i].equals(key)) return i;
return -1;
}
public boolean findSymbol(String key){
return stockList.containsKey(key);
}
public double findPrice(String key){
return stockList.get(key).price;
}
}