|
@@ -2,30 +2,196 @@ package io.zipcoder;
|
2
|
2
|
|
3
|
3
|
import java.util.ArrayList;
|
4
|
4
|
import java.util.Arrays;
|
|
5
|
+import java.util.HashMap;
|
|
6
|
+import java.util.Map;
|
|
7
|
+import java.util.regex.Matcher;
|
|
8
|
+import java.util.regex.Pattern;
|
5
|
9
|
|
6
|
10
|
public class ItemParser {
|
7
|
11
|
|
|
12
|
+ private Pattern pattern;
|
|
13
|
+ private Matcher matcher;
|
|
14
|
+ private int exceptionCount = 0;
|
|
15
|
+ private HashMap<String, ArrayList<Item>> groceries;
|
8
|
16
|
|
9
|
|
- public ArrayList<String> parseRawDataIntoStringArray(String rawData){
|
|
17
|
+ public ItemParser() {
|
|
18
|
+ groceries = new HashMap<String, ArrayList<Item>>();
|
|
19
|
+ }
|
|
20
|
+
|
|
21
|
+ public String parse(String aString) {
|
|
22
|
+ ArrayList<String> groceries = parseRawDataIntoStringArray(aString);
|
|
23
|
+ addGroceriesToList(groceries);
|
|
24
|
+ return printGroceries();
|
|
25
|
+ }
|
|
26
|
+
|
|
27
|
+ protected ArrayList<String> parseRawDataIntoStringArray(String rawData) {
|
10
|
28
|
String stringPattern = "##";
|
11
|
|
- ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawData);
|
|
29
|
+ ArrayList<String> response = splitStringWithRegexPattern(stringPattern, rawData);
|
12
|
30
|
return response;
|
13
|
31
|
}
|
14
|
32
|
|
15
|
|
- public Item parseStringIntoItem(String rawItem) throws ItemParseException{
|
16
|
|
- return null;
|
|
33
|
+ protected void addGroceriesToList(ArrayList<String> jerkList) {
|
|
34
|
+ for (int i = 0; i < jerkList.size(); i++) {
|
|
35
|
+ try {
|
|
36
|
+ Item temp = (parseStringIntoItem(jerkList.get(i)));
|
|
37
|
+ incrementItem(this.groceries, temp);
|
|
38
|
+ } catch (ItemParseException ipe) {
|
|
39
|
+ exceptionCount++;
|
|
40
|
+ }
|
|
41
|
+ }
|
|
42
|
+ }
|
|
43
|
+
|
|
44
|
+ protected Item parseStringIntoItem(String rawItem) throws ItemParseException {
|
|
45
|
+ String aName = findName(rawItem);
|
|
46
|
+ Double aPrice = findPrice(rawItem);
|
|
47
|
+ String aType = findType(rawItem);
|
|
48
|
+ String anExpiration = findExpiration(rawItem);
|
|
49
|
+
|
|
50
|
+ if (findName(rawItem) == null || findPrice(rawItem) == null) {
|
|
51
|
+ throw new ItemParseException();
|
|
52
|
+ }
|
|
53
|
+
|
|
54
|
+ return new Item(aName, aPrice, aType, anExpiration);
|
17
|
55
|
}
|
18
|
56
|
|
19
|
|
- public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){
|
|
57
|
+ protected ArrayList<String> findKeyValuePairsInRawItemData(String rawItem) {
|
20
|
58
|
String stringPattern = "[;|^]";
|
21
|
|
- ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawItem);
|
22
|
|
- return response;
|
|
59
|
+ return splitStringWithRegexPattern(stringPattern, rawItem);
|
23
|
60
|
}
|
24
|
61
|
|
25
|
|
- private ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString){
|
|
62
|
+ public HashMap<String, ArrayList<Item>> getGroceries() {
|
|
63
|
+ return groceries;
|
|
64
|
+ }
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+ // private methods
|
|
68
|
+
|
|
69
|
+ private ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString) {
|
26
|
70
|
return new ArrayList<String>(Arrays.asList(inputString.split(stringPattern)));
|
27
|
71
|
}
|
28
|
72
|
|
|
73
|
+ private String printGroceries() {
|
|
74
|
+ StringBuilder display = new StringBuilder();
|
|
75
|
+ for (Map.Entry<String, ArrayList<Item>> entry : groceries.entrySet()) {
|
|
76
|
+ displayItemInfo(display, entry);
|
|
77
|
+ displayPriceInfo(display, entry);
|
|
78
|
+ }
|
|
79
|
+ display.append("\n\n").append(exceptionCount).append(" errors\n");
|
|
80
|
+ return display.toString();
|
|
81
|
+ }
|
|
82
|
+
|
|
83
|
+ private void displayItemInfo(StringBuilder aBuilder, Map.Entry<String, ArrayList<Item>> anEntry) {
|
|
84
|
+ aBuilder.append("\nname:");
|
|
85
|
+ aBuilder.append(String.format("%9s", anEntry.getKey().substring(0, 1).toUpperCase() + anEntry.getKey().substring(1)));
|
|
86
|
+ aBuilder.append(" seen: " + anEntry.getValue().size() + " times\n");
|
|
87
|
+ aBuilder.append("==============" + "\t\t\t" + "===============\n");
|
|
88
|
+ }
|
|
89
|
+
|
|
90
|
+ private void displayPriceInfo(StringBuilder aBuilder, Map.Entry<String, ArrayList<Item>> anEntry) {
|
|
91
|
+ ArrayList<Double> uniquePriceArrayList = getUniquePrices(anEntry);
|
|
92
|
+ for (Double aPrice : uniquePriceArrayList) {
|
|
93
|
+ aBuilder.append("Price:").append(String.format("%8s", aPrice));
|
|
94
|
+ aBuilder.append(" seen: " + priceOccurences(anEntry.getValue(), aPrice) + " times\n");
|
|
95
|
+ aBuilder.append("--------------" + "\t\t\t" + "---------------\n");
|
|
96
|
+ }
|
|
97
|
+ }
|
|
98
|
+
|
|
99
|
+ private ArrayList<Double> getUniquePrices(Map.Entry<String, ArrayList<Item>> entry) {
|
|
100
|
+ ArrayList<Double> prices = new ArrayList<Double>();
|
29
|
101
|
|
|
102
|
+ for (int i = 0; i < entry.getValue().size(); i++) {
|
|
103
|
+ if (!prices.contains(entry.getValue().get(i).getPrice())) {
|
|
104
|
+ prices.add(entry.getValue().get(i).getPrice());
|
|
105
|
+ }
|
|
106
|
+ }
|
|
107
|
+ return prices;
|
|
108
|
+ }
|
|
109
|
+
|
|
110
|
+ private int priceOccurences(ArrayList<Item> aList, Double aPrice) {
|
|
111
|
+ int count = 0;
|
|
112
|
+ for (Item anAList : aList) {
|
|
113
|
+ if (anAList.getPrice().equals(aPrice)) {
|
|
114
|
+ count++;
|
|
115
|
+ }
|
|
116
|
+ }
|
|
117
|
+ return count;
|
|
118
|
+ }
|
|
119
|
+
|
|
120
|
+ private void incrementItem(Map<String, ArrayList<Item>> aMap, Item anItem) {
|
|
121
|
+ if (aMap.keySet().contains(anItem.getName())) {
|
|
122
|
+ aMap.get(anItem.getName()).add(anItem);
|
|
123
|
+ } else {
|
|
124
|
+ aMap.put(anItem.getName(), new ArrayList<Item>());
|
|
125
|
+ aMap.get(anItem.getName()).add(anItem);
|
|
126
|
+ }
|
|
127
|
+ }
|
|
128
|
+
|
|
129
|
+ private String findName(String aString) {
|
|
130
|
+ String search = "(?<=([n|N][a|A][m|M][e|E][^a-zA-z])).*?(?=[^a-zA-z0])";
|
|
131
|
+ pattern = Pattern.compile(search);
|
|
132
|
+ matcher = pattern.matcher(aString);
|
|
133
|
+
|
|
134
|
+ if (matcher.find()) {
|
|
135
|
+ if (matcher.group().length() > 0) {
|
|
136
|
+ return replaceZeros(matcher.group().toLowerCase());
|
|
137
|
+ } else {
|
|
138
|
+ return null;
|
|
139
|
+ }
|
|
140
|
+ } else {
|
|
141
|
+ return null;
|
|
142
|
+ }
|
|
143
|
+ }
|
|
144
|
+
|
|
145
|
+ private String replaceZeros(String aString) {
|
|
146
|
+ pattern = Pattern.compile("[0]");
|
|
147
|
+ matcher = pattern.matcher(aString);
|
|
148
|
+ return matcher.replaceAll("o");
|
|
149
|
+ }
|
|
150
|
+
|
|
151
|
+ private Double findPrice(String aString) {
|
|
152
|
+ pattern = Pattern.compile("(?<=([p|P][r|R][i|I][c|C][e|E][^a-zA-Z])).*?(?=[^0-9.])");
|
|
153
|
+ matcher = pattern.matcher(aString);
|
|
154
|
+
|
|
155
|
+ if (matcher.find()) {
|
|
156
|
+ if (matcher.group().length() > 0) {
|
|
157
|
+ return Double.parseDouble(matcher.group());
|
|
158
|
+ } else {
|
|
159
|
+ return null;
|
|
160
|
+ }
|
|
161
|
+ } else {
|
|
162
|
+ return null;
|
|
163
|
+ }
|
|
164
|
+ }
|
|
165
|
+
|
|
166
|
+ private String findType(String aString) {
|
|
167
|
+ pattern = Pattern.compile("(?<=([t|T][y|Y][p|P][e|E][^a-zA-z])).*?(?=[^a-zA-Z0])");
|
|
168
|
+ matcher = pattern.matcher(aString);
|
|
169
|
+
|
|
170
|
+ if (matcher.find()) {
|
|
171
|
+ if (matcher.group().length() > 0) {
|
|
172
|
+ return matcher.group().toLowerCase();
|
|
173
|
+ } else {
|
|
174
|
+ return null;
|
|
175
|
+ }
|
|
176
|
+ } else {
|
|
177
|
+ return null;
|
|
178
|
+ }
|
|
179
|
+ }
|
|
180
|
+
|
|
181
|
+ private String findExpiration(String aString) {
|
|
182
|
+ String search = "(?<=([e|E][x|X][p|P][i|I][r|R][a|A][t|T][i|I][o|O][n|N][^a-zA-z]))(.)+[^#]";
|
|
183
|
+ pattern = Pattern.compile(search);
|
|
184
|
+ matcher = pattern.matcher(aString);
|
|
185
|
+
|
|
186
|
+ if (matcher.find()) {
|
|
187
|
+ if (matcher.group().length() > 0) {
|
|
188
|
+ return matcher.group();
|
|
189
|
+ } else {
|
|
190
|
+ return null;
|
|
191
|
+ }
|
|
192
|
+ } else {
|
|
193
|
+ return null;
|
|
194
|
+ }
|
|
195
|
+ }
|
30
|
196
|
|
31
|
197
|
}
|