|
@@ -1,31 +1,118 @@
|
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;
|
|
6
|
+
|
|
7
|
+import com.google.common.base.CharMatcher;
|
|
8
|
+import com.google.common.base.Splitter;
|
|
9
|
+
|
|
10
|
+import static java.util.stream.Collectors.groupingBy;
|
|
11
|
+
|
5
|
12
|
|
6
|
13
|
public class ItemParser {
|
7
|
14
|
|
|
15
|
+ int errorCounter = 0;
|
|
16
|
+
|
|
17
|
+ public int getErrorCounter() {
|
|
18
|
+ return errorCounter;
|
|
19
|
+ }
|
|
20
|
+
|
|
21
|
+ public Map<String, Map<Double, List<Item>>> getItemsMap(ArrayList<Item> items) {
|
|
22
|
+ return items
|
|
23
|
+ .stream()
|
|
24
|
+ .collect(
|
|
25
|
+ groupingBy(
|
|
26
|
+ Item::getName,
|
|
27
|
+ groupingBy(
|
|
28
|
+ Item::getPrice
|
|
29
|
+ )
|
|
30
|
+ )
|
|
31
|
+ );
|
|
32
|
+ }
|
8
|
33
|
|
9
|
34
|
public ArrayList<String> parseRawDataIntoStringArray(String rawData){
|
|
35
|
+ String fixedString = fixString(rawData);
|
10
|
36
|
String stringPattern = "##";
|
11
|
|
- ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawData);
|
|
37
|
+ ArrayList<String> response = splitStringWithRegexPattern(stringPattern , fixedString);
|
12
|
38
|
return response;
|
13
|
39
|
}
|
14
|
40
|
|
15
|
|
- public Item parseStringIntoItem(String rawItem) throws ItemParseException{
|
16
|
|
- return null;
|
|
41
|
+ public Item parseStringIntoItem(String rawItem) {
|
|
42
|
+// try {
|
|
43
|
+// validateItem(rawItem);
|
|
44
|
+// } catch (ItemParseException e) {
|
|
45
|
+// //NO OP
|
|
46
|
+// }
|
|
47
|
+ Map<String, String> itemMap = null;
|
|
48
|
+ try {
|
|
49
|
+ itemMap = itemStringToHashMap(rawItem);
|
|
50
|
+ } catch (ItemParseException e) {
|
|
51
|
+ errorCounter++;
|
|
52
|
+ return null;
|
|
53
|
+ }
|
|
54
|
+ return createItem(itemMap);
|
|
55
|
+ }
|
|
56
|
+
|
|
57
|
+ private Item createItem(Map<String, String> itemMap) {
|
|
58
|
+ return new Item(itemMap.get("name"), Double.parseDouble(itemMap.get("price")), itemMap.get("type"), itemMap.get("expiration"));
|
|
59
|
+ }
|
|
60
|
+
|
|
61
|
+// public boolean validateItem(String rawItem) throws ItemParseException{
|
|
62
|
+// boolean isValid = false;
|
|
63
|
+// if (rawItem.contains("[:\\W]")) {
|
|
64
|
+// throw new ItemParseException();
|
|
65
|
+// } else isValid = true;
|
|
66
|
+// return isValid;
|
|
67
|
+// }
|
|
68
|
+
|
|
69
|
+ public String toLowerCase(String rawString) {
|
|
70
|
+ Matcher m = Pattern.compile("\\w").matcher(rawString);
|
|
71
|
+ StringBuilder sb = new StringBuilder();
|
|
72
|
+
|
|
73
|
+ int last = 0;
|
|
74
|
+ while (m.find()) {
|
|
75
|
+ sb.append(rawString, last, m.start());
|
|
76
|
+ sb.append(m.group(0).toLowerCase());
|
|
77
|
+ last = m.end();
|
|
78
|
+ }
|
|
79
|
+ sb.append(rawString.substring(last));
|
|
80
|
+
|
|
81
|
+ return sb.toString();
|
|
82
|
+ }
|
|
83
|
+
|
|
84
|
+ public String fixCo0kies(String rawString) {
|
|
85
|
+ Matcher m = Pattern.compile("co0kies").matcher(rawString);
|
|
86
|
+ String fixedString = m.replaceAll("cookies");
|
|
87
|
+ return fixedString;
|
|
88
|
+ }
|
|
89
|
+
|
|
90
|
+ public String fixString(String rawString) {
|
|
91
|
+ String lowerCased = toLowerCase(rawString);
|
|
92
|
+ String fixedString = fixCo0kies(lowerCased);
|
|
93
|
+ return fixedString;
|
17
|
94
|
}
|
18
|
95
|
|
19
|
96
|
public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){
|
20
|
|
- String stringPattern = "[;|^]";
|
|
97
|
+ String stringPattern = "[;|\\^]";
|
21
|
98
|
ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawItem);
|
|
99
|
+ //TODO
|
22
|
100
|
return response;
|
23
|
101
|
}
|
24
|
102
|
|
25
|
103
|
private ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString){
|
26
|
|
- return new ArrayList<String>(Arrays.asList(inputString.split(stringPattern)));
|
|
104
|
+ return new ArrayList<>(Arrays.asList(inputString.split(stringPattern)));
|
27
|
105
|
}
|
28
|
106
|
|
29
|
|
-
|
|
107
|
+ private Map<String, String> itemStringToHashMap(String rawItem) throws ItemParseException {
|
|
108
|
+ CharMatcher matcher = CharMatcher.anyOf(";^%*!@");
|
|
109
|
+ Map<String,String> itemMap = Splitter.on(matcher).withKeyValueSeparator(':').split(rawItem);
|
|
110
|
+ for (Map.Entry<String,String> entry : itemMap.entrySet()) {
|
|
111
|
+ if ( entry.getValue().equals("")) {
|
|
112
|
+ throw new ItemParseException();
|
|
113
|
+ }
|
|
114
|
+ }
|
|
115
|
+ return itemMap;
|
|
116
|
+ }
|
30
|
117
|
|
31
|
118
|
}
|