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