-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMilkData.java
More file actions
executable file
·381 lines (356 loc) · 11.6 KB
/
MilkData.java
File metadata and controls
executable file
·381 lines (356 loc) · 11.6 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
/**
* MilkData.java created by wenxiyang on MacBook Pro in a2
*
* Author: Wenxi Yang(wyang235@wisc.edu)
* Date: 4/26/2020
*
* Course: CS400
* Semester: Spring 2020
* Lecture: 001
*
* IDE: Eclipse IDE for Java Developers
* Version: 2019-12 (4.14.0)
* Build id: 20191212-1212
*
* Device: Wenxi's MacBook Pro
* OS: MacOS Mojave
* Version: 10.14.4
* OS Build: 18E226
*
* List Collaboratons: Name, email@wisc.edu, lecture number
*
* Other Credits: describe other sources(web sites or people)
*
* Known bugs: describe known unsolved bugs here
*/
/**
* MilkData - Save the milk data in a tree map, including the functions of
* load/write from/to file.
*
* @author Wenxi Yang (2020)
*
*/
public class MilkData {
// first key is month, second key is farmID, third key is date
private TreeMap<String, TreeMap<String, TreeMap<String, Integer>>> milkMap = new TreeMap<>(new MilkComparator());
private String csvDir;
/**
* Gets the milkMap.
*
* @return the milk map TreeMap<month(year-month), TreeMap<farmID,
* TreeMap<date(year-month-day), milk weight>>>
*/
public TreeMap<String, TreeMap<String, TreeMap<String, Integer>>> getMilkMap() {
return milkMap;
}
/**
* Read milk data from csv file directory (folder). For example, if we read csv
* files from the folder "small" which is provided by the professor, we enter
* "csv/small/" as the directory name.
*
* @param csvDir the csv dir
* @throws IOException Signals that an I/O exception has occurred.
*/
public List<File> readMilkData(String csvDir) throws IOException {
milkMap.clear();
this.csvDir = csvDir;
ArrayList<File> flist = new ArrayList<File>();
File file = new File(csvDir);
File[] fileList = file.listFiles();
for (File fileObj : fileList) {
String fileName = fileObj.getName();
if (fileName.toLowerCase().endsWith(".csv")) {
String year_month = fileName.substring(0, fileName.length() - 4);
readTextFile(year_month, fileObj);
flist.add(fileObj);
} else {
System.err.println("Error file: " + fileObj.getName());
}
}
return flist;
}
/**
* Read text file.
*
* @param month the month
* @param csvFile the csv file
* @throws IOException Signals that an I/O exception has occurred.
*/
private void readTextFile(String month, File csvFile) throws IOException {
if (!milkMap.containsKey(month)) {
milkMap.put(month, new TreeMap<String, TreeMap<String, Integer>>(new MilkComparator()));
}
FileInputStream fis = null;
try {
fis = new FileInputStream(csvFile);
InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
BufferedReader br = new BufferedReader(isr);
String line = br.readLine();
if (line != null) {
line = br.readLine();
}
while (line != null) {
processLine(milkMap.get(month), line);
line = br.readLine();
}
} finally {
if (fis != null) {
fis.close();
}
}
}
/**
* Process line.
*
* @param monthMap the month map
* @param line the line
*/
private void processLine(TreeMap<String, TreeMap<String, Integer>> monthMap, String line) {
String[] rs = line.split(",");
if (rs.length != 3) {
System.err.println("Error data. Skip it: " + line);
} else {
String date = rs[0].trim();
String farmID = rs[1].trim();
String weights = rs[2].trim();
Integer weight = null;
try {
weight = Integer.valueOf(weights);
if (CommonMilkTool.verifyDate(date) && farmID.matches("Farm \\d{1,}")) {
if (!monthMap.containsKey(farmID)) {
monthMap.put(farmID, new TreeMap<String, Integer>(new MilkComparator()));
}
monthMap.get(farmID).put(date, weight);
} else {
System.err.println("Error data, date format error: " + line);
}
} catch (NumberFormatException e) {
System.err.println("Error data, weight number format error: " + line);
}
}
}
/**
* Write milk data.
*
* @throws IOException Signals that an I/O exception has occurred.
*/
public void writeMilkData() throws IOException {
for (String key : milkMap.keySet()) {
writeMonthData(key);
}
}
/**
* Write month data.
*
* @param year_month the year-month
* @throws IOException Signals that an I/O exception has occurred.
*/
private void writeMonthData(String year_month) throws IOException {
TreeMap<String, TreeMap<String, Integer>> monthMap = milkMap.get(year_month);
String pathName = csvDir + "/" + year_month + ".csv";
File file = new File(pathName);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
osw.write("date,farm_id,weight\n");
// first key is date, second key is farmID
ArrayList<String> list = new ArrayList<String>();
for (Map.Entry<String, TreeMap<String, Integer>> farmEntry : monthMap.entrySet()) {
String farmID = farmEntry.getKey();
TreeMap<String, Integer> map = farmEntry.getValue();
for (Map.Entry<String, Integer> entry : map.entrySet()) {
String date = entry.getKey();
Integer weight = entry.getValue();
list.add(date + "," + farmID + "," + weight);
}
}
list.forEach((item) -> {
try {
osw.write(item + "\n");
} catch (IOException e) {
e.printStackTrace();
}
});
osw.flush();
} finally {
if (fos != null) {
fos.close();
}
}
}
// TreeMap<month(year-month), TreeMap<farmID, TreeMap<date(year-month-day), milk
// weight>>>
public List<MilkItem> getDataByDay(String day) {
List<MilkItem> list = new ArrayList<>();
String month = day.substring(0, day.lastIndexOf("-"));
TreeMap<String, TreeMap<String, Integer>> monthMap = this.milkMap.get(month);
if (monthMap == null) {
return list;
}
// TreeMap<farmID, TreeMap<date(year-month-day), milk weight>>
for (Map.Entry<String, TreeMap<String, Integer>> farmEntry : monthMap.entrySet()) {
for (Map.Entry<String, Integer> dayEntry : farmEntry.getValue().entrySet()) {
if (dayEntry.getKey().equals(day)) {
list.add(new MilkItem(day, farmEntry.getKey(), dayEntry.getValue()));
}
}
}
return list;
}
// TreeMap<month(year-month), TreeMap<farmID, TreeMap<date(year-month-day), milk
// weight>>>
public void removeData(String day, String farmID) {
List<MilkItem> list = new ArrayList<>();
String month = day.substring(0, day.lastIndexOf("-"));
TreeMap<String, TreeMap<String, Integer>> monthMap = this.milkMap.get(month);
if (monthMap == null) {
return;
}
TreeMap<String, Integer> farmMap = monthMap.get(farmID);
if (farmMap == null) {
return;
}
farmMap.remove(day);
}
public boolean checkData(String day, String farmID) {
List<MilkItem> list = new ArrayList<>();
String month = day.substring(0, day.lastIndexOf("-"));
TreeMap<String, TreeMap<String, Integer>> monthMap = this.milkMap.get(month);
if (monthMap == null) {
return false;
}
TreeMap<String, Integer> farmMap = monthMap.get(farmID);
if (farmMap == null) {
return false;
}
return farmMap.containsKey(day);
}
public void changeData(String day, String farmID, int weight) {
List<MilkItem> list = new ArrayList<>();
String month = day.substring(0, day.lastIndexOf("-"));
TreeMap<String, TreeMap<String, Integer>> monthMap = this.milkMap.get(month);
if (monthMap == null) {
return;
}
TreeMap<String, Integer> farmMap = monthMap.get(farmID);
if (farmMap == null) {
return;
}
farmMap.put(day, weight);
}
public void addData(String day, String farmID, int weight) {
List<MilkItem> list = new ArrayList<>();
String month = day.substring(0, day.lastIndexOf("-"));
if (!milkMap.containsKey(month)) {
milkMap.put(month, new TreeMap<String, TreeMap<String, Integer>>(new MilkComparator()));
}
TreeMap<String, TreeMap<String, Integer>> monthMap = this.milkMap.get(month);
if (!monthMap.containsKey(farmID)) {
monthMap.put(farmID, new TreeMap<String, Integer>(new MilkComparator()));
}
TreeMap<String, Integer> farmMap = monthMap.get(farmID);
farmMap.put(day, weight);
}
public List<MilkItem> getDataByMonth(String month) {
List<MilkItem> list = new ArrayList<>();
TreeMap<String, TreeMap<String, Integer>> monthMap = this.milkMap.get(month);
if (monthMap == null) {
return list;
}
// TreeMap<farmID, TreeMap<date(year-month-day), milk weight>>
for (Map.Entry<String, TreeMap<String, Integer>> farmEntry : monthMap.entrySet()) {
for (Map.Entry<String, Integer> dayEntry : farmEntry.getValue().entrySet()) {
list.add(new MilkItem(dayEntry.getKey(), farmEntry.getKey(), dayEntry.getValue()));
}
}
Collections.sort(list);
return list;
}
/**
* Get all the milk data in the given year
* @param year
* @return a list of this year's milk data
*/
public List<MilkItem> getDataByYear(String year){
List<MilkItem> list = new ArrayList<>();
for (Map.Entry<String, TreeMap<String, TreeMap<String, Integer>>> monthEntry : this.milkMap.entrySet()) {
if (monthEntry.getKey().split("-", 2)[0].equals(year)) {
for (Map.Entry<String, TreeMap<String, Integer>> farmEntry: monthEntry.getValue().entrySet())
for (Map.Entry<String, Integer> dayEntry : farmEntry.getValue().entrySet()) {
list.add(new MilkItem(dayEntry.getKey(), farmEntry.getKey(), dayEntry.getValue()));
}
}
}
Collections.sort(list);
return list;
}
// TreeMap<month(year-month), TreeMap<farmID, TreeMap<date(year-month-day), milk
// weight>>>
public List<MilkItem> getAllData() {
List<MilkItem> list = new ArrayList<>();
for (Map.Entry<String, TreeMap<String, TreeMap<String, Integer>>> monthEntry : this.milkMap.entrySet()) {
for (Map.Entry<String, TreeMap<String, Integer>> farmEntry : monthEntry.getValue().entrySet()) {
for (Map.Entry<String, Integer> dayEntry : farmEntry.getValue().entrySet()) {
list.add(new MilkItem(dayEntry.getKey(), farmEntry.getKey(), dayEntry.getValue()));
}
}
}
Collections.sort(list);
return list;
}
public Set<String> getFarmIDList() {
Set<String> list = new HashSet<>();
for (Map.Entry<String, TreeMap<String, TreeMap<String, Integer>>> monthEntry : this.milkMap.entrySet()) {
list.addAll(monthEntry.getValue().keySet());
}
return list;
}
// TreeMap<month(year-month), TreeMap<farmID, TreeMap<date(year-month-day), milk
// weight>>>
public Set<String> getMonths() {
return this.milkMap.keySet();
}
/**
* Gets the data for data range report.
*
* @param startDate the start date
* @param endDate the end date
* @return the total weight
*/
public int getDataByTimeRange(String startDate, String endDate, List<DataRangeItem> list) {
MilkComparator milkComparator = new MilkComparator();
int sum = 0;
for (Map.Entry<String, TreeMap<String, TreeMap<String, Integer>>> monthEntry : this.milkMap.entrySet()) {
for (Map.Entry<String, TreeMap<String, Integer>> farmEntry : monthEntry.getValue().entrySet()) {
for (Map.Entry<String, Integer> dayEntry : farmEntry.getValue().entrySet()) {
if (milkComparator.compare(startDate, dayEntry.getKey()) <= 0
&& milkComparator.compare(endDate, dayEntry.getKey()) >= 0) {
list.add(new DataRangeItem(dayEntry.getKey(), farmEntry.getKey(), dayEntry.getValue(), 0));
sum += dayEntry.getValue();
}
}
}
}
for (DataRangeItem item : list) {
double percentage = ((item.getWeight() * 0.1) / sum);
item.setPercent((int)(percentage * 1000));
}
Collections.sort(list);
return sum;
}
}