|
@@ -1,11 +1,13 @@
|
1
|
1
|
package io.zipcoder;
|
2
|
2
|
|
3
|
|
-import java.util.ArrayList;
|
4
|
|
-import java.util.Arrays;
|
|
3
|
+import java.lang.reflect.Field;
|
|
4
|
+import java.lang.reflect.Method;
|
|
5
|
+import java.util.*;
|
|
6
|
+import java.util.regex.Matcher;
|
|
7
|
+import java.util.regex.Pattern;
|
5
|
8
|
|
6
|
9
|
public class ItemParser {
|
7
|
10
|
|
8
|
|
-
|
9
|
11
|
public ArrayList<String> parseRawDataIntoStringArray(String rawData){
|
10
|
12
|
String stringPattern = "##";
|
11
|
13
|
ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawData);
|
|
@@ -13,11 +15,18 @@ public class ItemParser {
|
13
|
15
|
}
|
14
|
16
|
|
15
|
17
|
public Item parseStringIntoItem(String rawItem) throws ItemParseException{
|
16
|
|
- return null;
|
|
18
|
+ ArrayList<String> values = getValues(rawItem);
|
|
19
|
+
|
|
20
|
+ String name = uppercase(correctSpelling(values.get(0)));
|
|
21
|
+ Double price = Double.valueOf(values.get(1));
|
|
22
|
+ String type = uppercase(values.get(2));
|
|
23
|
+ String expiration = values.get(3);
|
|
24
|
+
|
|
25
|
+ return new Item(name, price, type, expiration);
|
17
|
26
|
}
|
18
|
27
|
|
19
|
28
|
public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){
|
20
|
|
- String stringPattern = "[;|^]";
|
|
29
|
+ String stringPattern = "[^a-zA-Z0-9\\:\\.\\/]";
|
21
|
30
|
ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawItem);
|
22
|
31
|
return response;
|
23
|
32
|
}
|
|
@@ -26,6 +35,229 @@ public class ItemParser {
|
26
|
35
|
return new ArrayList<String>(Arrays.asList(inputString.split(stringPattern)));
|
27
|
36
|
}
|
28
|
37
|
|
|
38
|
+ //keys - (.+)(?=:)
|
|
39
|
+ //values - (?<=:)(.+)
|
|
40
|
+
|
|
41
|
+ public String getValue(String input)throws ItemParseException{
|
|
42
|
+ ArrayList<String> kv = splitStringWithRegexPattern(":", input);
|
|
43
|
+ if(kv.size() < 2){
|
|
44
|
+ throw new ItemParseException();
|
|
45
|
+ } else if(kv.size() == 2) {
|
|
46
|
+ return kv.get(1);
|
|
47
|
+ }
|
|
48
|
+ return null;
|
|
49
|
+ }
|
|
50
|
+
|
|
51
|
+ public ArrayList<String> getValues(String rawItem) throws ItemParseException{
|
|
52
|
+ ArrayList<String> KVPairs = findKeyValuePairsInRawItemData(rawItem);
|
|
53
|
+ ArrayList<String> values = new ArrayList<String>();
|
|
54
|
+ for(String pair : KVPairs){
|
|
55
|
+ String value = toLowerCase(getValue(pair));
|
|
56
|
+ if(value.equals("")){
|
|
57
|
+ throw new ItemParseException();
|
|
58
|
+ } else{
|
|
59
|
+ values.add(value);
|
|
60
|
+ }
|
|
61
|
+ }
|
|
62
|
+ return values;
|
|
63
|
+ }
|
|
64
|
+
|
|
65
|
+ public String toLowerCase(String input) {
|
|
66
|
+ String[] lower = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
|
|
67
|
+ String[] upper = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
|
|
68
|
+
|
|
69
|
+ StringBuilder lowercase = new StringBuilder();
|
|
70
|
+
|
|
71
|
+ Pattern pattern = Pattern.compile(".");
|
|
72
|
+ Matcher matcher = pattern.matcher(input);
|
29
|
73
|
|
|
74
|
+ while(matcher.find()){
|
|
75
|
+ String character = matcher.group();
|
|
76
|
+ if(isUpper(character)){
|
|
77
|
+ int i = indexOf(upper, character);
|
|
78
|
+ lowercase.append(lower[i]);
|
|
79
|
+ }else {
|
|
80
|
+ lowercase.append(character);
|
|
81
|
+ }
|
|
82
|
+ }
|
|
83
|
+ return lowercase.toString();
|
|
84
|
+ }
|
|
85
|
+
|
|
86
|
+ public boolean isUpper(String c){
|
|
87
|
+ Pattern pattern = Pattern.compile("[A-Z]");
|
|
88
|
+ Matcher matcher = pattern.matcher(c);
|
|
89
|
+ return matcher.find();
|
|
90
|
+ }
|
30
|
91
|
|
|
92
|
+ public int indexOf(String[] array, String c){
|
|
93
|
+ for(int i = 0; i < array.length; i++){
|
|
94
|
+ if(array[i].equals(c)){
|
|
95
|
+ return i;
|
|
96
|
+ }
|
|
97
|
+ }
|
|
98
|
+ return -1;
|
|
99
|
+ }
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+ public int findNumberOfErrors(String input){
|
|
115
|
+ ArrayList<String> badFormat = new ArrayList<String>();
|
|
116
|
+ ArrayList<String> rawData = parseRawDataIntoStringArray(input);
|
|
117
|
+ for(String data : rawData){
|
|
118
|
+ try {
|
|
119
|
+ Item item = parseStringIntoItem(data);
|
|
120
|
+ }catch(ItemParseException e){
|
|
121
|
+ badFormat.add(data);
|
|
122
|
+ }
|
|
123
|
+ }
|
|
124
|
+ return badFormat.size();
|
|
125
|
+ }
|
|
126
|
+
|
|
127
|
+ public ArrayList<Item> findItems(String input){
|
|
128
|
+ ArrayList<Item> items = new ArrayList<Item>();
|
|
129
|
+ ArrayList<String> rawData = parseRawDataIntoStringArray(input);
|
|
130
|
+ for(String data : rawData){
|
|
131
|
+ try {
|
|
132
|
+ Item item = parseStringIntoItem(data);
|
|
133
|
+ items.add(item);
|
|
134
|
+ }catch(ItemParseException e){
|
|
135
|
+ e.printStackTrace();
|
|
136
|
+ }
|
|
137
|
+ }
|
|
138
|
+ return items;
|
|
139
|
+ }
|
|
140
|
+
|
|
141
|
+ public Map<Object, Integer> getUniqueItemNames(ArrayList<Item> items, String methodname){
|
|
142
|
+
|
|
143
|
+ try {
|
|
144
|
+ Map<Object, Integer> uniqueNames = new HashMap<>();
|
|
145
|
+ for (Item item : items) {
|
|
146
|
+ Class<?> e = item.getClass();
|
|
147
|
+ Method method = e.getMethod(methodname);
|
|
148
|
+ if (!uniqueNames.containsKey(method.invoke(item))) {
|
|
149
|
+ uniqueNames.put(method.invoke(item), new Integer(1));
|
|
150
|
+ } else {
|
|
151
|
+ int amount = uniqueNames.get(method.invoke(item));
|
|
152
|
+ amount = amount + 1;
|
|
153
|
+ uniqueNames.put(method.invoke(item), amount);
|
|
154
|
+ }
|
|
155
|
+ }
|
|
156
|
+ return uniqueNames;
|
|
157
|
+
|
|
158
|
+ }catch(Exception e){
|
|
159
|
+ e.printStackTrace();
|
|
160
|
+ }
|
|
161
|
+ return null;
|
|
162
|
+ }
|
|
163
|
+
|
|
164
|
+ public String correctSpelling(String test) {
|
|
165
|
+ String[] nums = {"0", "1", "3"};
|
|
166
|
+ String[] alpha = {"o", "l", "e"};
|
|
167
|
+
|
|
168
|
+ StringBuilder lowercase = new StringBuilder();
|
|
169
|
+
|
|
170
|
+ Pattern pattern = Pattern.compile(".");
|
|
171
|
+ Matcher matcher = pattern.matcher(test);
|
|
172
|
+
|
|
173
|
+ while(matcher.find()){
|
|
174
|
+ String character = matcher.group();
|
|
175
|
+ int index = indexOf(nums, character);
|
|
176
|
+ if(index != -1){
|
|
177
|
+ lowercase.append(alpha[index]);
|
|
178
|
+ } else{
|
|
179
|
+ lowercase.append(character);
|
|
180
|
+ }
|
|
181
|
+ }
|
|
182
|
+ return lowercase.toString();
|
|
183
|
+ }
|
|
184
|
+
|
|
185
|
+ public Map<String, Map<Double, Integer>> sortItems(ArrayList<Item> items){
|
|
186
|
+ Map<String, Map<Double, Integer>> uniqueItem = new HashMap<>();
|
|
187
|
+ Map<Object, Integer> names = getUniqueItemNames(items, "getName");
|
|
188
|
+
|
|
189
|
+ for(Object key : names.keySet()){
|
|
190
|
+ Map<Double, Integer> prices = new HashMap<>();
|
|
191
|
+ for(Item item : items){
|
|
192
|
+ if(item.getName().equals(key)){
|
|
193
|
+ if(prices.containsKey(item.getPrice())){
|
|
194
|
+ int amount = prices.get(item.getPrice());
|
|
195
|
+ amount = amount + 1;
|
|
196
|
+ prices.put(item.getPrice(), amount);
|
|
197
|
+ } else {
|
|
198
|
+ prices.put(item.getPrice(), 1);
|
|
199
|
+ }
|
|
200
|
+ }
|
|
201
|
+ uniqueItem.put((String)key, prices);
|
|
202
|
+ }
|
|
203
|
+ }
|
|
204
|
+
|
|
205
|
+ return uniqueItem;
|
|
206
|
+ }
|
|
207
|
+
|
|
208
|
+ public String uppercase(String test) {
|
|
209
|
+ String[] lower = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
|
|
210
|
+ String[] upper = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
|
|
211
|
+
|
|
212
|
+ ArrayList<String> answer = getCharList(test);
|
|
213
|
+
|
|
214
|
+ if(answer.size() > 0){
|
|
215
|
+ if(isUpper(answer.get(0))){
|
|
216
|
+ return listToString(answer);
|
|
217
|
+ } else {
|
|
218
|
+ int index = indexOf(lower, answer.get(0));
|
|
219
|
+ answer.remove(0);
|
|
220
|
+ answer.add(0, upper[index]);
|
|
221
|
+ }
|
|
222
|
+ }
|
|
223
|
+ return listToString(answer);
|
|
224
|
+ }
|
|
225
|
+
|
|
226
|
+ public ArrayList<String> getCharList(String input){
|
|
227
|
+ ArrayList<String> answer = new ArrayList<>();
|
|
228
|
+
|
|
229
|
+ Pattern pattern = Pattern.compile(".");
|
|
230
|
+ Matcher matcher = pattern.matcher(input);
|
|
231
|
+
|
|
232
|
+ while(matcher.find()){
|
|
233
|
+ answer.add(matcher.group());
|
|
234
|
+ }
|
|
235
|
+ return answer;
|
|
236
|
+ }
|
|
237
|
+
|
|
238
|
+ public String listToString(List<String> list){
|
|
239
|
+ StringBuilder stringBuilder = new StringBuilder();
|
|
240
|
+ for(String string : list){
|
|
241
|
+ stringBuilder.append(string);
|
|
242
|
+ }
|
|
243
|
+ return stringBuilder.toString();
|
|
244
|
+ }
|
|
245
|
+
|
|
246
|
+ public void print(String input){
|
|
247
|
+ int errs = findNumberOfErrors(input);
|
|
248
|
+ ArrayList<Item> items = findItems(input);
|
|
249
|
+ Map<Object, Integer> names = getUniqueItemNames(items, "getName");
|
|
250
|
+ Map<String, Map<Double, Integer>> uniqueItem = sortItems(items);
|
|
251
|
+
|
|
252
|
+ for(String name : uniqueItem.keySet()){
|
|
253
|
+ System.out.printf("name: %7s %20s \n", name, "seen: " + names.get(name) + " times");
|
|
254
|
+ System.out.println("============= \t =============");
|
|
255
|
+ for(Double price : uniqueItem.get(name).keySet()){
|
|
256
|
+ System.out.printf("Price: %6s %20s \n", price, "seen: " + uniqueItem.get(name).get(price) + " times");
|
|
257
|
+ }
|
|
258
|
+ System.out.println("============= \t =============");
|
|
259
|
+
|
|
260
|
+ }
|
|
261
|
+ System.out.printf("\nErrors %26s", "seen " + errs + " times" );
|
|
262
|
+ }
|
31
|
263
|
}
|