|
@@ -2,18 +2,95 @@ package io.zipcoder;
|
2
|
2
|
|
3
|
3
|
import org.apache.commons.io.IOUtils;
|
4
|
4
|
|
|
5
|
+import java.util.ArrayList;
|
|
6
|
+import java.util.HashMap;
|
|
7
|
+import java.util.List;
|
|
8
|
+import java.util.Map;
|
|
9
|
+import java.util.stream.Collectors;
|
|
10
|
+
|
5
|
11
|
|
6
|
12
|
public class Main {
|
|
13
|
+ private static int count;
|
|
14
|
+ Map<String, List<Double>> map = new HashMap<>();
|
7
|
15
|
|
8
|
|
- public String readRawDataToString() throws Exception{
|
|
16
|
+ private String readRawDataToString() throws Exception{
|
9
|
17
|
ClassLoader classLoader = getClass().getClassLoader();
|
10
|
18
|
String result = IOUtils.toString(classLoader.getResourceAsStream("RawData.txt"));
|
11
|
19
|
return result;
|
12
|
20
|
}
|
13
|
21
|
|
|
22
|
+ private void addToMap(Item item){
|
|
23
|
+ String key = item.getName();
|
|
24
|
+ List<Double> list = new ArrayList<>();
|
|
25
|
+
|
|
26
|
+ if (!map.containsKey(key)) {
|
|
27
|
+ list.add(item.getPrice());
|
|
28
|
+ map.put(key, list);
|
|
29
|
+ }
|
|
30
|
+ else {
|
|
31
|
+ list = map.get(key);
|
|
32
|
+ list.add(item.getPrice());
|
|
33
|
+ map.put(key, list);
|
|
34
|
+ }
|
|
35
|
+ }
|
|
36
|
+
|
|
37
|
+ private int getCount(List<Double> list, double d){
|
|
38
|
+ list.removeIf(e -> e != d);
|
|
39
|
+ return list.size();
|
|
40
|
+ }
|
|
41
|
+
|
|
42
|
+ private String getCountPhrase(int num){
|
|
43
|
+ return num == 1 ? num + " time": num + " times";
|
|
44
|
+ }
|
|
45
|
+
|
|
46
|
+ public String writeOutput(){
|
|
47
|
+ StringBuilder sb = new StringBuilder();
|
|
48
|
+ String column1; String column2;
|
|
49
|
+
|
|
50
|
+ for (String s : map.keySet()){
|
|
51
|
+ List<Double> prices = map.get(s);
|
|
52
|
+ List<Double> uniquePrices = prices.stream().distinct().collect(Collectors.toList());
|
|
53
|
+
|
|
54
|
+ column1 = String.format("%-7s%7s", "Name: ", s);
|
|
55
|
+ column2 = String.format("%-7s%7s","Seen: ", getCountPhrase(prices.size()));
|
|
56
|
+
|
|
57
|
+ sb.append(String.format("%14s %8s %14s\n",column1, " ",column2));
|
|
58
|
+ sb.append(String.format("%14s %8s %14s\n","==============", " ", "=============="));
|
|
59
|
+
|
|
60
|
+ for (Double d : uniquePrices) {
|
|
61
|
+ column1 = String.format("%-7s%7s", "Price: ", d);
|
|
62
|
+ column2 = String.format("%-7s%7s","Seen: ", getCountPhrase(getCount(prices,d)));
|
|
63
|
+
|
|
64
|
+ sb.append(String.format("%14s %8s %14s\n",column1, " ",column2));
|
|
65
|
+ sb.append(String.format("%14s %8s %14s\n","--------------", " ", "--------------"));
|
|
66
|
+ }
|
|
67
|
+ sb.append("\n");
|
|
68
|
+ }
|
|
69
|
+
|
|
70
|
+ column1 = "Errors";
|
|
71
|
+ column2 = String.format("%-7s%7s","Seen: ", getCountPhrase(count));
|
|
72
|
+ sb.append(String.format("%-14s %8s %14s",column1, " ",column2));
|
|
73
|
+ return sb.toString();
|
|
74
|
+ }
|
|
75
|
+
|
14
|
76
|
public static void main(String[] args) throws Exception{
|
15
|
|
- String output = (new Main()).readRawDataToString();
|
16
|
|
- System.out.println(output);
|
|
77
|
+ Main main = new Main();
|
|
78
|
+ String output = main.readRawDataToString();
|
|
79
|
+
|
|
80
|
+ ItemParser ip = new ItemParser();
|
|
81
|
+ List<String> list = ip.parseRawDataIntoStringArray(output);
|
|
82
|
+
|
|
83
|
+ for (String str : list) {
|
|
84
|
+ try {
|
|
85
|
+ Item newItem = ip.parseStringIntoItem(str);
|
|
86
|
+ main.addToMap(newItem);
|
|
87
|
+ } catch (ItemParseException e1) {
|
|
88
|
+ count++;
|
|
89
|
+ }
|
|
90
|
+ }
|
|
91
|
+
|
|
92
|
+ System.out.println(main.writeOutput());
|
|
93
|
+
|
17
|
94
|
// TODO: parse the data in output into items, and display to console.
|
18
|
95
|
}
|
19
|
96
|
}
|