|
@@ -1,31 +1,162 @@
|
1
|
1
|
package io.zipcoder;
|
2
|
2
|
|
|
3
|
+import java.lang.reflect.Field;
|
|
4
|
+import java.lang.reflect.Method;
|
3
|
5
|
import java.util.ArrayList;
|
4
|
6
|
import java.util.Arrays;
|
|
7
|
+import java.util.HashMap;
|
|
8
|
+import java.util.Map;
|
|
9
|
+import java.util.regex.Matcher;
|
|
10
|
+import java.util.regex.Pattern;
|
|
11
|
+
|
|
12
|
+import static jdk.nashorn.internal.objects.NativeString.indexOf;
|
5
|
13
|
|
6
|
14
|
public class ItemParser {
|
7
|
15
|
|
8
|
16
|
|
9
|
|
- public ArrayList<String> parseRawDataIntoStringArray(String rawData){
|
|
17
|
+ public ArrayList<String> parseRawDataIntoStringArray(String rawData) {
|
10
|
18
|
String stringPattern = "##";
|
11
|
|
- ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawData);
|
|
19
|
+ ArrayList<String> response = splitStringWithRegexPattern(stringPattern, rawData);
|
12
|
20
|
return response;
|
13
|
21
|
}
|
14
|
22
|
|
15
|
|
- public Item parseStringIntoItem(String rawItem) throws ItemParseException{
|
16
|
|
- return null;
|
|
23
|
+ public Item parseStringIntoItem(String rawItem) throws ItemParseException {
|
|
24
|
+ ArrayList<String> values = getValues(toLowerCase(rawItem));
|
|
25
|
+
|
|
26
|
+ String name = values.get(0);
|
|
27
|
+ Double price = Double.valueOf(values.get(1));
|
|
28
|
+ String type = values.get(2);
|
|
29
|
+ String expiration = values.get(3);
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+ return new Item(name, price, type, expiration);
|
|
33
|
+
|
17
|
34
|
}
|
18
|
35
|
|
19
|
|
- public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){
|
20
|
|
- String stringPattern = "[;|^]";
|
21
|
|
- ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawItem);
|
|
36
|
+ public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem) {
|
|
37
|
+ String stringPattern = "[^a-zA-Z0-9\\:\\.\\/]+";
|
|
38
|
+ ArrayList<String> response = splitStringWithRegexPattern(stringPattern, rawItem);
|
22
|
39
|
return response;
|
23
|
40
|
}
|
24
|
41
|
|
25
|
|
- private ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString){
|
|
42
|
+ private ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString) {
|
26
|
43
|
return new ArrayList<String>(Arrays.asList(inputString.split(stringPattern)));
|
27
|
44
|
}
|
28
|
45
|
|
|
46
|
+ public String getValue(String input) throws ItemParseException {
|
|
47
|
+ ArrayList<String> kv = splitStringWithRegexPattern(":", input);
|
|
48
|
+ if (kv.size() < 2) {
|
|
49
|
+ throw new ItemParseException();
|
|
50
|
+ } else if (kv.size() == 2) {
|
|
51
|
+ return kv.get(1);
|
|
52
|
+ }
|
|
53
|
+ return null;
|
|
54
|
+ }
|
|
55
|
+
|
|
56
|
+ public ArrayList<String> getValues(String rawItem) throws ItemParseException {
|
29
|
57
|
|
|
58
|
+ ArrayList<String> KVPairs = findKeyValuePairsInRawItemData(rawItem);
|
|
59
|
+ ArrayList<String> values = new ArrayList<String>();
|
|
60
|
+
|
|
61
|
+ for (String pair : KVPairs) {
|
|
62
|
+ String value = getValue(pair);
|
|
63
|
+ if (value.equals("")) {
|
|
64
|
+ throw new ItemParseException();
|
|
65
|
+ } else {
|
|
66
|
+ values.add(value);
|
|
67
|
+ }
|
|
68
|
+ }
|
|
69
|
+ return values;
|
|
70
|
+ }
|
|
71
|
+
|
|
72
|
+ public String toLowerCase(String test) {
|
|
73
|
+ 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"};
|
|
74
|
+ 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"};
|
|
75
|
+
|
|
76
|
+ StringBuilder lowercase = new StringBuilder();
|
|
77
|
+
|
|
78
|
+ Pattern pattern = Pattern.compile(".");
|
|
79
|
+ Matcher matcher = pattern.matcher(test);
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+ while (matcher.find()) {
|
|
83
|
+ String character = (matcher.group());
|
|
84
|
+ if (isUpper(character)) {
|
|
85
|
+ int i = indexOf(upper, character);
|
|
86
|
+ lowercase.append(lower[i]);
|
|
87
|
+ } else {
|
|
88
|
+ lowercase.append(character);
|
|
89
|
+ }
|
|
90
|
+ }
|
|
91
|
+ return lowercase.toString();
|
|
92
|
+ }
|
30
|
93
|
|
31
|
|
-}
|
|
94
|
+
|
|
95
|
+ public boolean isUpper(String c) {
|
|
96
|
+ Pattern pattern = Pattern.compile("[A-Z]");
|
|
97
|
+ Matcher matcher = pattern.matcher(c);
|
|
98
|
+ return matcher.find();
|
|
99
|
+ }
|
|
100
|
+
|
|
101
|
+ public int indexOf(String[] array, String c) {
|
|
102
|
+ for (int i = 0; i < array.length; i++) {
|
|
103
|
+ if (array[i].equals(c)) {
|
|
104
|
+ return i;
|
|
105
|
+ }
|
|
106
|
+ }
|
|
107
|
+ return -1;
|
|
108
|
+ }
|
|
109
|
+
|
|
110
|
+ public int findNumberOfErrors(String input) {
|
|
111
|
+ ArrayList<String> rawData = parseRawDataIntoStringArray(input);
|
|
112
|
+ ArrayList<String> badFormat = new ArrayList<String>();
|
|
113
|
+
|
|
114
|
+ for (String data : rawData) {
|
|
115
|
+ try {
|
|
116
|
+ Item item = parseStringIntoItem(data);
|
|
117
|
+ } catch (ItemParseException e) {
|
|
118
|
+ badFormat.add(data);
|
|
119
|
+ }
|
|
120
|
+ }
|
|
121
|
+ return (badFormat.size());
|
|
122
|
+ }
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+ public ArrayList<Item> findItems(String input) {
|
|
126
|
+ ArrayList<String> rawData = parseRawDataIntoStringArray(input);
|
|
127
|
+ ArrayList<Item> items = new ArrayList<Item>();
|
|
128
|
+
|
|
129
|
+ for (String data : rawData) {
|
|
130
|
+ try {
|
|
131
|
+ Item item = parseStringIntoItem(data);
|
|
132
|
+ items.add(item);
|
|
133
|
+ } catch (ItemParseException e) {
|
|
134
|
+ e.printStackTrace();
|
|
135
|
+ }
|
|
136
|
+ }
|
|
137
|
+ return items;
|
|
138
|
+ }
|
|
139
|
+
|
|
140
|
+ public Map<Object, Integer> getUniqueNames(ArrayList<Item> items, String methodname) {
|
|
141
|
+ try {
|
|
142
|
+ Map<Object, Integer> uniqueNames = new HashMap<>();
|
|
143
|
+
|
|
144
|
+ for (Item item : items) {
|
|
145
|
+ Class<?> e = item.getClass();
|
|
146
|
+ Method method = e.getMethod(methodname);
|
|
147
|
+ if (!uniqueNames.containsKey(method.invoke(item))) {
|
|
148
|
+
|
|
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
|
+ } catch (Exception e) {
|
|
158
|
+ e.printStackTrace();
|
|
159
|
+ }
|
|
160
|
+ return null;
|
|
161
|
+ }
|
|
162
|
+}
|