|
@@ -2,18 +2,106 @@ 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.Iterator;
|
|
8
|
+import java.util.Map;
|
|
9
|
+
|
5
|
10
|
|
6
|
11
|
public class Main {
|
7
|
12
|
|
8
|
|
- public String readRawDataToString() throws Exception{
|
|
13
|
+ public String readRawDataToString() throws Exception {
|
9
|
14
|
ClassLoader classLoader = getClass().getClassLoader();
|
10
|
15
|
String result = IOUtils.toString(classLoader.getResourceAsStream("RawData.txt"));
|
11
|
16
|
return result;
|
12
|
17
|
}
|
13
|
18
|
|
14
|
|
- public static void main(String[] args) throws Exception{
|
|
19
|
+ public static void main(String[] args) throws Exception {
|
15
|
20
|
String output = (new Main()).readRawDataToString();
|
16
|
|
- System.out.println(output);
|
17
|
|
- // TODO: parse the data in output into items, and display to console.
|
|
21
|
+
|
|
22
|
+ ItemParser parser = new ItemParser();
|
|
23
|
+
|
|
24
|
+ ArrayList<String> dataStringArray = parser.parseRawDataIntoStringArray(output);
|
|
25
|
+
|
|
26
|
+ ArrayList<Item> items = new ArrayList<>();
|
|
27
|
+ for (String item : dataStringArray) {
|
|
28
|
+ items.add(parser.parseStringIntoItem(item));
|
|
29
|
+ }
|
|
30
|
+
|
|
31
|
+ int errorCounter = 0;
|
|
32
|
+ Integer milkCounter = 0;
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+ Iterator<Item> iterator = items.iterator();
|
|
36
|
+ while (iterator.hasNext()) {
|
|
37
|
+ Item item = iterator.next();
|
|
38
|
+ if ("N/A".equals(item.getName()) || item.getPrice() == 0.0) {
|
|
39
|
+ errorCounter++;
|
|
40
|
+ iterator.remove();
|
|
41
|
+ }
|
|
42
|
+ }
|
|
43
|
+
|
|
44
|
+ HashMap<String, Integer> names = getNameMap(items);
|
|
45
|
+
|
|
46
|
+ for (Map.Entry<String, Integer> name : names.entrySet()) {
|
|
47
|
+ HashMap<String, HashMap<Double, Integer>> namePriceMap = getNamePriceMap(items, name.getKey());
|
|
48
|
+ System.out.println("==============================");
|
|
49
|
+ System.out.println("Name: " + name.getKey() + " Appears: " + name.getValue() + " times");
|
|
50
|
+ System.out.println("------------------------------");
|
|
51
|
+ for(Map.Entry<String, HashMap<Double, Integer>> namePrice : namePriceMap.entrySet()){
|
|
52
|
+ String key = namePrice.getKey();
|
|
53
|
+ for(Map.Entry<Double,Integer> priceMap : namePrice.getValue().entrySet()){
|
|
54
|
+ System.out.println("Price: " + priceMap.getKey() + " Appears: " + priceMap.getValue());
|
|
55
|
+ }
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+ }
|
|
59
|
+ }
|
|
60
|
+ System.out.println("------------------------------");
|
|
61
|
+ System.out.println("Errors: " + errorCounter);
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+ }
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+ public static HashMap<String, HashMap<Double, Integer>> getNamePriceMap(ArrayList<Item> items, String itemName) {
|
|
69
|
+ HashMap<String, HashMap<Double, Integer>> namePriceMap = new HashMap<>();
|
|
70
|
+ ArrayList <Item> nameList = new ArrayList<>();
|
|
71
|
+ for (Item item : items) {
|
|
72
|
+ if(item.getName().equals(itemName)){
|
|
73
|
+ nameList.add(item);
|
|
74
|
+ }
|
|
75
|
+ }
|
|
76
|
+ namePriceMap.put(itemName, getPriceMap(nameList));
|
|
77
|
+ return namePriceMap;
|
|
78
|
+ }
|
|
79
|
+
|
|
80
|
+ public static HashMap<Double, Integer> getPriceMap(ArrayList<Item> items) {
|
|
81
|
+ HashMap<Double, Integer> map = new HashMap<>();
|
|
82
|
+ for (Item item : items) {
|
|
83
|
+ if (map.containsKey(item.getPrice())) {
|
|
84
|
+ Integer newItemCount = map.get(item.getPrice());
|
|
85
|
+ newItemCount++;
|
|
86
|
+ map.put(item.getPrice(), newItemCount);
|
|
87
|
+ } else {
|
|
88
|
+ map.put(item.getPrice(), 1);
|
|
89
|
+ }
|
|
90
|
+ }
|
|
91
|
+ return map;
|
|
92
|
+ }
|
|
93
|
+
|
|
94
|
+ public static HashMap<String, Integer> getNameMap(ArrayList<Item> items) {
|
|
95
|
+ HashMap<String, Integer> map = new HashMap<>();
|
|
96
|
+ for (Item item : items) {
|
|
97
|
+ if (map.containsKey(item.getName())) {
|
|
98
|
+ Integer newItemCount = map.get(item.getName());
|
|
99
|
+ newItemCount++;
|
|
100
|
+ map.put(item.getName(), newItemCount);
|
|
101
|
+ } else {
|
|
102
|
+ map.put(item.getName(), 1);
|
|
103
|
+ }
|
|
104
|
+ }
|
|
105
|
+ return map;
|
18
|
106
|
}
|
19
|
|
-}
|
|
107
|
+}
|