|
@@ -1,31 +1,228 @@
|
1
|
1
|
package io.zipcoder;
|
2
|
2
|
|
3
|
|
-import java.util.ArrayList;
|
4
|
|
-import java.util.Arrays;
|
|
3
|
+import java.util.*;
|
|
4
|
+import java.util.regex.Matcher;
|
|
5
|
+import java.util.regex.Pattern;
|
5
|
6
|
|
6
|
7
|
public class ItemParser {
|
7
|
8
|
|
|
9
|
+ private Integer countExceptionsThrown = 0;
|
|
10
|
+ private Integer count = 0;
|
8
|
11
|
|
9
|
|
- public ArrayList<String> parseRawDataIntoStringArray(String rawData){
|
|
12
|
+ private ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString) {
|
|
13
|
+ return new ArrayList<String>(Arrays.asList(inputString.split(stringPattern)));
|
|
14
|
+ }
|
|
15
|
+
|
|
16
|
+ public ArrayList<String> parseRawDataIntoStringArray(String rawData) {
|
10
|
17
|
String stringPattern = "##";
|
11
|
|
- ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawData);
|
|
18
|
+ ArrayList<String> response = splitStringWithRegexPattern(stringPattern, rawData);
|
12
|
19
|
return response;
|
13
|
20
|
}
|
14
|
21
|
|
15
|
|
- public Item parseStringIntoItem(String rawItem) throws ItemParseException{
|
16
|
|
- return null;
|
|
22
|
+ public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem) {
|
|
23
|
+ String stringPattern = "[@|^|*|%|!|;]";
|
|
24
|
+ ArrayList<String> response = splitStringWithRegexPattern(stringPattern, rawItem);
|
|
25
|
+ return response;
|
17
|
26
|
}
|
18
|
27
|
|
19
|
|
- public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){
|
20
|
|
- String stringPattern = "[;|^]";
|
21
|
|
- ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawItem);
|
22
|
|
- return response;
|
|
28
|
+ public Item parseStringIntoItem(String rawItem) throws ItemParseException {
|
|
29
|
+ if (checkName(rawItem) == null || checkPrice(rawItem) == null || checkType(rawItem) == null
|
|
30
|
+ || checkExpiration(rawItem) == null) {
|
|
31
|
+ throw new ItemParseException();
|
|
32
|
+ }
|
|
33
|
+
|
|
34
|
+ String name = checkName(rawItem);
|
|
35
|
+ Double price = Double.valueOf(checkPrice(rawItem));
|
|
36
|
+ String type = checkType(rawItem);
|
|
37
|
+ String expiration = checkExpiration(rawItem);
|
|
38
|
+
|
|
39
|
+ return new Item(name, price, type, expiration);
|
23
|
40
|
}
|
24
|
41
|
|
25
|
|
- private ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString){
|
26
|
|
- return new ArrayList<String>(Arrays.asList(inputString.split(stringPattern)));
|
|
42
|
+ public ArrayList<Item> createItemArrayList(String rawData) {
|
|
43
|
+ ArrayList<String> temp = parseRawDataIntoStringArray(rawData);
|
|
44
|
+ ArrayList<Item> itemArrayList = new ArrayList<Item>();
|
|
45
|
+
|
|
46
|
+ for (int i = 0; i <temp.size() ; i++) {
|
|
47
|
+ try {
|
|
48
|
+ itemArrayList.add(parseStringIntoItem(temp.get(i)));
|
|
49
|
+ } catch (ItemParseException e) {
|
|
50
|
+ count++;
|
|
51
|
+ }
|
|
52
|
+ }
|
|
53
|
+ return itemArrayList;
|
|
54
|
+ }
|
|
55
|
+
|
|
56
|
+ public ArrayList<Item> filterItemArrayList(ArrayList<Item> input, String filterType){
|
|
57
|
+ ArrayList<Item> filterItemArrayList = new ArrayList<Item>();
|
|
58
|
+ for (int i = 0; i <input.size() ; i++) {
|
|
59
|
+ if(input.get(i).getName().equals(filterType))
|
|
60
|
+ filterItemArrayList.add(input.get(i));
|
|
61
|
+ }
|
|
62
|
+ return filterItemArrayList;
|
|
63
|
+ }
|
|
64
|
+
|
|
65
|
+ public Map<Double, Integer> individualItemCount(ArrayList<Item> filteredArrayList) {
|
|
66
|
+ Map<Double, Integer> priceTotals = new TreeMap<Double, Integer>(Collections.reverseOrder());
|
|
67
|
+ for (int i = 0; i < filteredArrayList.size(); i++) {
|
|
68
|
+ Double key = filteredArrayList.get(i).getPrice();
|
|
69
|
+ Integer count = priceTotals.get(key);
|
|
70
|
+ if (count == null) {
|
|
71
|
+ priceTotals.put(key, 1);
|
|
72
|
+ } else {
|
|
73
|
+ priceTotals.put(key, count + 1);
|
|
74
|
+ }
|
|
75
|
+ }
|
|
76
|
+ return priceTotals;
|
|
77
|
+ }
|
|
78
|
+
|
|
79
|
+ public Map<Double, Integer> itemTypeMapWithCounts(String rawData, String filterType) {
|
|
80
|
+ ArrayList<Item> itemArrayList = createItemArrayList(rawData);
|
|
81
|
+ ArrayList<Item> filterItemArrayList = filterItemArrayList(itemArrayList, filterType);
|
|
82
|
+ Map<Double, Integer> priceTotals = individualItemCount(filterItemArrayList);
|
|
83
|
+ return priceTotals;
|
|
84
|
+ }
|
|
85
|
+
|
|
86
|
+ public Integer totalTimesItemSeen(String rawData, String filterType) {
|
|
87
|
+ ArrayList<Item> itemArrayList = createItemArrayList(rawData);
|
|
88
|
+ ArrayList<Item> filterItemArrayList = filterItemArrayList(itemArrayList, filterType);
|
|
89
|
+ return filterItemArrayList.size();
|
|
90
|
+ }
|
|
91
|
+
|
|
92
|
+ public ArrayList<String> filterTypeArrayList(String rawData) {
|
|
93
|
+ ArrayList<Item> itemArrayList = createItemArrayList(rawData);
|
|
94
|
+ ArrayList<String> filterType = new ArrayList<String>();
|
|
95
|
+
|
|
96
|
+ for(int i = 0; i < itemArrayList.size(); i++) {
|
|
97
|
+ if (!filterType.contains(itemArrayList.get(i).getName())) {
|
|
98
|
+ filterType.add(itemArrayList.get(i).getName());
|
|
99
|
+ }
|
|
100
|
+ }
|
|
101
|
+ return filterType;
|
|
102
|
+ }
|
|
103
|
+
|
|
104
|
+ public String formatText(String rawData) throws ItemParseException {
|
|
105
|
+ ArrayList<String> filterType = filterTypeArrayList(rawData);
|
|
106
|
+ StringBuilder sb = new StringBuilder();
|
|
107
|
+
|
|
108
|
+ for (int i = 0; i < filterType.size(); i++) {
|
|
109
|
+ String name = filterType.get(i);
|
|
110
|
+ Integer total = totalTimesItemSeen(rawData, filterType.get(i));
|
|
111
|
+ String priceFormated = formatPriceField(rawData, filterType.get(i));
|
|
112
|
+ sb.append("name: ");
|
|
113
|
+ sb.append(String.format("%7s", name.substring(0, 1).toUpperCase() + name.substring(1)));
|
|
114
|
+ sb.append("\t \t");
|
|
115
|
+ sb.append(" seen: " + total + " times\n");
|
|
116
|
+ sb.append("============= \t \t =============\n");
|
|
117
|
+ sb.append(priceFormated + "\n");
|
|
118
|
+ }
|
|
119
|
+
|
|
120
|
+ sb.append("Errors " + " seen: " + getExceptionsThrown(rawData) + " times");
|
|
121
|
+
|
|
122
|
+ return sb.toString();
|
|
123
|
+ }
|
|
124
|
+
|
|
125
|
+ public String formatPriceField(String rawData, String filterType) {
|
|
126
|
+ Map<Double, Integer> priceTotals = itemTypeMapWithCounts(rawData, filterType);
|
|
127
|
+ StringBuilder sb = new StringBuilder();
|
|
128
|
+ Set mapSet = (Set) priceTotals.entrySet();
|
|
129
|
+ Iterator mapIterator = mapSet.iterator();
|
|
130
|
+ while (mapIterator.hasNext()) {
|
|
131
|
+ Map.Entry mapEntry = (Map.Entry) mapIterator.next();
|
|
132
|
+ // getKey Method of HashMap access a key of map
|
|
133
|
+ Object keyValue = mapEntry.getKey();
|
|
134
|
+ //getValue method returns corresponding key's value
|
|
135
|
+ Object value = mapEntry.getValue();
|
|
136
|
+ String time = (1 < (Integer)value) ? " times": " time";
|
|
137
|
+ sb.append("Price: " + keyValue + "\t\t seen: " + value + time + "\n");
|
|
138
|
+ if (mapIterator.hasNext()) sb.append(String.format("------------- -------------\n"));
|
|
139
|
+ if(priceTotals.size() < 2) sb.append(String.format("------------- -------------\n"));
|
|
140
|
+
|
|
141
|
+ }
|
|
142
|
+ return sb.toString();
|
|
143
|
+ }
|
|
144
|
+
|
|
145
|
+ public String checkName(String input) {
|
|
146
|
+ String newInput = fixCookie(input);
|
|
147
|
+ Pattern patternName = Pattern.compile("([Nn]..[Ee]:)(\\w+)");
|
|
148
|
+ Matcher matcherName= patternName.matcher(newInput);
|
|
149
|
+
|
|
150
|
+ if (matcherName.find())
|
|
151
|
+ return matcherName.group(2).toLowerCase();
|
|
152
|
+ else return null;
|
|
153
|
+ }
|
|
154
|
+
|
|
155
|
+ public String fixCookie(String input){
|
|
156
|
+ Pattern patternCookies = Pattern.compile("([Cc][0Oo][0Oo][Kk][Ii][Ee][Ss])");
|
|
157
|
+ Matcher matcherCookie= patternCookies.matcher(input);
|
|
158
|
+ return matcherCookie.replaceAll("cookies");
|
27
|
159
|
}
|
28
|
160
|
|
|
161
|
+ public String checkPrice(String input) {
|
|
162
|
+ Pattern patternPrice = Pattern.compile("([Pp]...[Ee]:)(\\d\\.\\d{2})");
|
|
163
|
+ Matcher matcherPrice= patternPrice.matcher(input);
|
29
|
164
|
|
|
165
|
+ if (matcherPrice.find())
|
|
166
|
+ return matcherPrice.group(2);
|
|
167
|
+ else return null;
|
|
168
|
+ }
|
|
169
|
+
|
|
170
|
+ public String checkType(String input) {
|
|
171
|
+ Pattern patternType = Pattern.compile("([Tt]..[Ee]:)(\\w+)");
|
|
172
|
+ Matcher matcherType = patternType.matcher(input);
|
|
173
|
+
|
|
174
|
+ if (matcherType.find())
|
|
175
|
+ return matcherType.group(2).toLowerCase();
|
|
176
|
+ else return null;
|
|
177
|
+ }
|
|
178
|
+
|
|
179
|
+ public String checkExpiration(String input) {
|
|
180
|
+ Pattern patternExpiration = Pattern.compile("([Ee]........[Nn]:)(\\d\\/\\d{2}\\/\\d{4})");
|
|
181
|
+ Matcher matcherExpiration = patternExpiration.matcher(input);
|
30
|
182
|
|
|
183
|
+ if (matcherExpiration.find())
|
|
184
|
+ return matcherExpiration.group(2);
|
|
185
|
+ else return null;
|
|
186
|
+ }
|
|
187
|
+
|
|
188
|
+ public Integer getExceptionsThrown(String rawData) {
|
|
189
|
+ ArrayList<String> parsedRawData = parseRawDataIntoStringArray(rawData);
|
|
190
|
+ for (int i = 0; i < parsedRawData.size(); i++)
|
|
191
|
+ try {
|
|
192
|
+ parseStringIntoItem(parsedRawData.get(i));
|
|
193
|
+ } catch (ItemParseException e){
|
|
194
|
+ countExceptionsThrown++;
|
|
195
|
+ }
|
|
196
|
+ return this.countExceptionsThrown;
|
|
197
|
+ }
|
31
|
198
|
}
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+// public String checkName(String input) throws ItemParseException{
|
|
202
|
+// Pattern patternName = Pattern.compile("([Nn]..[Ee]:)(\\w+)");
|
|
203
|
+// Matcher matcherName= patternName.matcher(input);
|
|
204
|
+//
|
|
205
|
+// if (matcherName.find())
|
|
206
|
+// return matcherName.group(2).toLowerCase();
|
|
207
|
+// else throw new ItemParseException();
|
|
208
|
+// }
|
|
209
|
+
|
|
210
|
+// Pattern[] patternName = new Pattern[4];
|
|
211
|
+// patternName[0] = Pattern.compile("([Nn]..[Ee]:)([Mm]..[Kk])");
|
|
212
|
+// patternName[1] = Pattern.compile("([Nn]..[Ee]:)([Bb]...[Dd])");
|
|
213
|
+// patternName[2] = Pattern.compile("([Nn]..[Ee]:)([Cc].....[Ss])");
|
|
214
|
+// patternName[3] = Pattern.compile("([Nn]..[Ee]:)([Aa]....[Ss])");
|
|
215
|
+//
|
|
216
|
+// for (int i = 0; i < patternName.length; i++) {
|
|
217
|
+// Matcher nameMatcher = patternName[i].matcher(input);
|
|
218
|
+// if (nameMatcher.find()) {
|
|
219
|
+// return "milk";
|
|
220
|
+// } else if (nameMatcher.find()) {
|
|
221
|
+// return "bread";
|
|
222
|
+// } else if (nameMatcher.find()) {
|
|
223
|
+// return "cookies";
|
|
224
|
+// } else if (nameMatcher.find()) {
|
|
225
|
+// return "apples";
|
|
226
|
+// }
|
|
227
|
+// }
|
|
228
|
+// throw new ItemParseException();
|