|
@@ -1,22 +1,17 @@
|
1
|
1
|
package io.zipcoder;
|
2
|
2
|
|
3
|
|
-import java.util.ArrayList;
|
4
|
|
-import java.util.Arrays;
|
5
|
|
-import java.util.HashMap;
|
6
|
|
-import java.util.Map;
|
|
3
|
+import java.util.*;
|
7
|
4
|
import java.util.regex.Matcher;
|
8
|
5
|
import java.util.regex.Pattern;
|
9
|
6
|
|
10
|
7
|
public class ItemParser {
|
11
|
8
|
|
12
|
|
- Pattern pattern;
|
13
|
|
- Matcher matcher;
|
14
|
|
- public int counter = 0;
|
15
|
|
-
|
|
9
|
+ private Pattern pattern;
|
|
10
|
+ private Matcher matcher;
|
|
11
|
+ private int counter = 0;
|
16
|
12
|
private Main main = new Main();
|
17
|
13
|
private Map<String, ArrayList<Item>> groceryMap = new HashMap<String, ArrayList<Item>>();
|
18
|
14
|
|
19
|
|
-
|
20
|
15
|
public ArrayList<String> parseRawDataIntoStringArray(String rawData) {
|
21
|
16
|
String stringPattern = "##";
|
22
|
17
|
ArrayList<String> response = splitStringWithRegexPattern(stringPattern, rawData);
|
|
@@ -47,7 +42,7 @@ public class ItemParser {
|
47
|
42
|
return new ArrayList<String>(Arrays.asList(inputString.split(stringPattern)));
|
48
|
43
|
}
|
49
|
44
|
|
50
|
|
- private String findName(String rawItem){
|
|
45
|
+ public String findName(String rawItem) {
|
51
|
46
|
String search = "(?<=([Nn][Aa][Mm][Ee][^A-Za-z])).*?(?=[^A-Za-z0])";
|
52
|
47
|
pattern = Pattern.compile(search);
|
53
|
48
|
matcher = pattern.matcher(rawItem);
|
|
@@ -56,13 +51,12 @@ public class ItemParser {
|
56
|
51
|
if (!matcher.group().equals("")) {
|
57
|
52
|
String name = matcher.group().replaceAll("\\d", "o");
|
58
|
53
|
return name.toLowerCase();
|
59
|
|
- //return name.substring(0, 1).toUpperCase() + name.substring(1).toLowerCase();
|
60
|
54
|
}
|
61
|
55
|
}
|
62
|
56
|
return null;
|
63
|
57
|
}
|
64
|
58
|
|
65
|
|
- private String findPrice(String rawItem){
|
|
59
|
+ public String findPrice(String rawItem) {
|
66
|
60
|
pattern = Pattern.compile("(?<=([Pp][Rr][Ii][Cc][Ee][^A-Za-z])).*?(?=[^0-9.])");
|
67
|
61
|
matcher = pattern.matcher(rawItem);
|
68
|
62
|
|
|
@@ -96,7 +90,9 @@ public class ItemParser {
|
96
|
90
|
public Map<String, ArrayList<Item>> getMap() throws Exception {
|
97
|
91
|
|
98
|
92
|
ArrayList<String> listOfItems = parseRawDataIntoStringArray(main.readRawDataToString());
|
|
93
|
+
|
99
|
94
|
for (String anItem : listOfItems) {
|
|
95
|
+
|
100
|
96
|
try {
|
101
|
97
|
Item newItem = parseStringIntoItem(anItem);
|
102
|
98
|
if (!groceryMap.containsKey(newItem.getName())) {
|
|
@@ -109,21 +105,62 @@ public class ItemParser {
|
109
|
105
|
} catch (ItemParseException e) {
|
110
|
106
|
counter++;
|
111
|
107
|
}
|
|
108
|
+
|
112
|
109
|
}
|
113
|
110
|
return groceryMap;
|
114
|
111
|
}
|
115
|
112
|
|
116
|
|
- public String display(){
|
117
|
|
- for(Map.Entry<String, ArrayList<Item>> mapKey : groceryMap.entrySet()) {
|
118
|
|
- System.out.println(mapKey.getKey());
|
119
|
|
- for (Item item : mapKey.getValue()) {
|
120
|
|
- System.out.println(item.getPrice());
|
|
113
|
+ public String display() throws Exception {
|
|
114
|
+ groceryMap = getMap();
|
|
115
|
+ StringBuilder displayBuild = new StringBuilder();
|
|
116
|
+
|
|
117
|
+ for (Map.Entry<String, ArrayList<Item>> item : groceryMap.entrySet()) {
|
|
118
|
+ String upperCaseString = item.getKey().substring(0, 1).toUpperCase() + item.getKey().substring(1);
|
|
119
|
+
|
|
120
|
+ displayBuild.append("\n" +
|
|
121
|
+ String.format("%-5s%10s%15s%2d%5s", "name:", upperCaseString
|
|
122
|
+ , "seen: ", item.getValue().size(), " times"));
|
|
123
|
+ displayBuild.append("\n" +
|
|
124
|
+ String.format("%15s%3s%5s", "===============", "\t\t\t", "===============") + "\n");
|
|
125
|
+
|
|
126
|
+ ArrayList<Double> uniquePriceList = getUniquePrices(item);
|
|
127
|
+
|
|
128
|
+ for (int i = 0; i < uniquePriceList.size(); i++) {
|
|
129
|
+ displayBuild.append(String.format("%-11s%.2f%15s%2d%5s", "Price:", uniquePriceList.get(i)
|
|
130
|
+ , "seen: ", seenPriceOccurences(item.getValue(), uniquePriceList.get(i))
|
|
131
|
+ , " times"));
|
|
132
|
+ displayBuild.append("\n" +
|
|
133
|
+ String.format("%15s%3s%5s", "---------------", "\t\t\t", "---------------") + "\n");
|
121
|
134
|
}
|
122
|
135
|
}
|
123
|
|
- System.out.println("Errors seen: " + counter + " times");
|
124
|
|
- return null;
|
|
136
|
+ displayBuild.append("\n" +
|
|
137
|
+ String.format("%-20s%10s%2d%5s", "Errors", "seen: ", counter, " times"));
|
|
138
|
+
|
|
139
|
+ return displayBuild.toString();
|
|
140
|
+ }
|
|
141
|
+
|
|
142
|
+ public int seenPriceOccurences(ArrayList<Item> list, Double price) {
|
|
143
|
+ int countPrice = 0;
|
|
144
|
+
|
|
145
|
+ for (int i = 0; i < list.size(); i++) {
|
|
146
|
+ if (list.get(i).getPrice().equals(price)) {
|
|
147
|
+ countPrice++;
|
|
148
|
+ }
|
125
|
149
|
}
|
|
150
|
+ return countPrice;
|
|
151
|
+ }
|
126
|
152
|
|
127
|
|
- public
|
|
153
|
+ public ArrayList<Double> getUniquePrices(Map.Entry<String, ArrayList<Item>> item) {
|
|
154
|
+ ArrayList<Double> uniquePrices = new ArrayList<Double>();
|
|
155
|
+
|
|
156
|
+ for (int i = 0; i < item.getValue().size(); i++) {
|
|
157
|
+ if (!uniquePrices.contains(item.getValue().get(i).getPrice())) {
|
|
158
|
+ uniquePrices.add(item.getValue().get(i).getPrice());
|
|
159
|
+ }
|
|
160
|
+ }
|
|
161
|
+ return uniquePrices;
|
128
|
162
|
}
|
|
163
|
+}
|
|
164
|
+
|
|
165
|
+
|
129
|
166
|
|