-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCsvParser.java
More file actions
165 lines (126 loc) · 5.1 KB
/
CsvParser.java
File metadata and controls
165 lines (126 loc) · 5.1 KB
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
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.HashMap;
public class CsvParser {
private String stockCsvPath = "StockInventory.csv";
private String productCsvPath = "ProductType.csv";
private String configCsvPath = "config.csv";
private String delimiter = ";";
public int getNextProductId(){
int nextProductId = 1;
try(BufferedReader br = new BufferedReader(new FileReader(configCsvPath))){
br.readLine();
nextProductId = Integer.parseInt(br.readLine());
return nextProductId;
} catch(Exception e){
e.printStackTrace();
System.out.println("Config error, defaulting ID to 1");
}
return nextProductId;
}
public int getNextStockId(){
int nextStockId = 1;
try(BufferedReader br = new BufferedReader(new FileReader(configCsvPath))){
br.readLine();
br.readLine();
nextStockId = Integer.parseInt(br.readLine());
return nextStockId;
} catch(Exception e){
e.printStackTrace();
System.out.println("Config error, defaulting ID to 1");
}
return nextStockId;
}
public void saveConfig(int nextProductId, int nextStockId) {
StringBuilder sb = new StringBuilder();
sb.append("NextAvailableId - Product (Row 2), Stock (Row 3)\n");
sb.append(nextProductId).append("\n");
sb.append(nextStockId);
try (BufferedWriter bw = new BufferedWriter(new FileWriter(configCsvPath))) {
bw.write(sb.toString());
} catch (IOException e) {
e.printStackTrace();
}
}
//return hashmap of product types
public HashMap<Integer, Product> returnProducts(){
HashMap<Integer, Product> productMap = new HashMap<>();
try(BufferedReader br = new BufferedReader(new FileReader(productCsvPath))){
String row, brand, model;
int productId;
Product newProduct;
String[] rowParts;
br.readLine(); //skip first line (header)
while((row = br.readLine()) != null){
rowParts = row.split(delimiter);
productId = Integer.parseInt(rowParts[0]);
brand = rowParts[1];
model = rowParts[2];
newProduct = new Product(productId,brand,model);
productMap.put(productId, newProduct);
}
} catch(IOException e){
e.printStackTrace();
}
return productMap;
}
//return hashmap of stocks
public HashMap<Integer, Stock> returnStocks(HashMap<Integer, Product> productMap){
HashMap<Integer, Stock> stockMap = new HashMap<>();
try(BufferedReader br = new BufferedReader(new FileReader(stockCsvPath))){
String row, engineNumber;
int stockId,productId;
Product product;
LocalDateTime entryDateTime, purchaseDateTime;
String[] rowParts;
Stock newStock;
br.readLine(); //skip first line (header)
while((row = br.readLine()) != null){
rowParts = row.split(delimiter);
stockId = Integer.parseInt(rowParts[0]);
productId = Integer.parseInt(rowParts[1]);
product = productMap.get(productId); //use hashmap for O(1) lookup of product
engineNumber = rowParts[2];
entryDateTime = LocalDateTime.parse(rowParts[3]);
if (rowParts[4].equals("null")){
purchaseDateTime = null;
} else {
purchaseDateTime = LocalDateTime.parse(rowParts[4]);
}
newStock = new Stock(stockId, product, engineNumber, entryDateTime, purchaseDateTime);
stockMap.put(stockId,newStock);
}
} catch(IOException e){
e.printStackTrace();
}
return stockMap;
}
public void saveProducts(HashMap<Integer, Product> productMap){
StringBuilder newCsvData = new StringBuilder();
newCsvData.append("ProductId;Brand;Model\n");
for (Product p : productMap.values()){
newCsvData.append(p.toCsvRow());
}
try(BufferedWriter bw = new BufferedWriter(new FileWriter(productCsvPath))){
bw.write(newCsvData.toString());
} catch(IOException e){
e.printStackTrace();
}
}
public void saveStocks(HashMap<Integer, Stock> stockMap){
StringBuilder newCsvData = new StringBuilder();
newCsvData.append("StockId;ProductId;EngineNumber;DateTimeEntered;DateTimePurchased\n");
for (Stock s : stockMap.values()){
newCsvData.append(s.toCsvRow());
}
try(BufferedWriter bw = new BufferedWriter(new FileWriter(stockCsvPath))){
bw.write(newCsvData.toString());
} catch(IOException e){
e.printStackTrace();
}
}
}