|
@@ -2,30 +2,188 @@ package io.zipcoder;
|
2
|
2
|
|
3
|
3
|
import java.util.ArrayList;
|
4
|
4
|
import java.util.Arrays;
|
|
5
|
+import java.util.HashMap;
|
|
6
|
+import java.util.Map;
|
|
7
|
+import java.util.regex.Matcher;
|
|
8
|
+import java.util.regex.Pattern;
|
5
|
9
|
|
6
|
10
|
public class ItemParser {
|
7
|
11
|
|
|
12
|
+ Map<String, ArrayList<Item>> realFoodList;
|
8
|
13
|
|
9
|
|
- public ArrayList<String> parseRawDataIntoStringArray(String rawData){
|
|
14
|
+ public ItemParser() {
|
|
15
|
+ realFoodList = new HashMap<>();
|
|
16
|
+ }
|
|
17
|
+
|
|
18
|
+ public Map<String, ArrayList<Item>> getRealFoodList() {
|
|
19
|
+ return realFoodList;
|
|
20
|
+ }
|
|
21
|
+
|
|
22
|
+ //feed data from string split into arrayList
|
|
23
|
+ public ArrayList<String> parseRawDataIntoStringArray(String rawData) {
|
10
|
24
|
String stringPattern = "##";
|
11
|
|
- ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawData);
|
|
25
|
+ ArrayList<String> response = splitStringWithRegexPattern(stringPattern, rawData);
|
|
26
|
+ //naMe:Milk;price:3.23;type:Food;expiration:1/25/2016 these chunks stored at each index
|
12
|
27
|
return response;
|
13
|
28
|
}
|
14
|
29
|
|
15
|
|
- public Item parseStringIntoItem(String rawItem) throws ItemParseException{
|
16
|
|
- return null;
|
|
30
|
+ //general method for splitting arraylists with regex
|
|
31
|
+ private ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString) {
|
|
32
|
+ return new ArrayList<>(Arrays.asList(inputString.split(stringPattern)));
|
17
|
33
|
}
|
18
|
34
|
|
19
|
|
- public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){
|
20
|
|
- String stringPattern = "[;|^]";
|
21
|
|
- ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawItem);
|
|
35
|
+ //Splits arraylist that has chunks into the name:cake arraylist which gets used in testing
|
|
36
|
+ public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem) {
|
|
37
|
+ String stringPattern = "[;|^|!|%|*|@]";
|
|
38
|
+ ArrayList<String> response = splitStringWithRegexPattern(stringPattern, rawItem);
|
|
39
|
+ //ex {name:cake,price:2.50, type:food, expiration:1/4/2018}
|
22
|
40
|
return response;
|
|
41
|
+
|
|
42
|
+ }
|
|
43
|
+
|
|
44
|
+ //takes the raw data arraylist(item chunks) parses them into items and then adds the items to the map
|
|
45
|
+ public void addItemToList(ArrayList<String> itemList){
|
|
46
|
+ for (int i = 0; i < itemList.size(); i++) {
|
|
47
|
+ try{
|
|
48
|
+ Item newItem = (parseStringIntoItem(itemList.get(i)));
|
|
49
|
+ addNewItemToMap(realFoodList, newItem);
|
|
50
|
+ }catch (ItemParseException e){
|
|
51
|
+ continue;
|
|
52
|
+ }
|
|
53
|
+
|
|
54
|
+ }
|
|
55
|
+ }
|
|
56
|
+
|
|
57
|
+ public void addNewItemToMap(Map<String, ArrayList<Item>> map, Item newItem) {
|
|
58
|
+ if (!map.keySet().contains(newItem.getName())) {
|
|
59
|
+ map.put(newItem.getName(), new ArrayList<>());
|
|
60
|
+ map.get(newItem.getName()).add(newItem);
|
|
61
|
+ } else {
|
|
62
|
+ map.get(newItem.getName()).add(newItem);
|
|
63
|
+ }
|
|
64
|
+ }
|
|
65
|
+
|
|
66
|
+ //gets all the prices for the items
|
|
67
|
+ public ArrayList<Double> getPrices(Map.Entry<String, ArrayList<Item>> itemMap) {
|
|
68
|
+ ArrayList<Double> prices = new ArrayList<>();
|
|
69
|
+ for (int i = 0; i < itemMap.getValue().size(); i++) {
|
|
70
|
+ if (!prices.contains(itemMap.getValue().get(i).getPrice())) {
|
|
71
|
+ prices.add(itemMap.getValue().get(i).getPrice());
|
|
72
|
+ }
|
|
73
|
+ }
|
|
74
|
+ return prices;
|
|
75
|
+ }
|
|
76
|
+
|
|
77
|
+ public int countNumberOfDifferentPrices(ArrayList<Item> itemList, Double price) {
|
|
78
|
+ int counter = 0;
|
|
79
|
+ for (int i = 0; i < itemList.size(); i++) {
|
|
80
|
+ if (itemList.get(i).getPrice().equals(price)) {
|
|
81
|
+ counter++;
|
|
82
|
+ }
|
|
83
|
+ }
|
|
84
|
+ return counter;
|
|
85
|
+ }
|
|
86
|
+
|
|
87
|
+ //Create new item
|
|
88
|
+ public Item parseStringIntoItem(String rawItem) throws ItemParseException {
|
|
89
|
+ String name = findName(rawItem);
|
|
90
|
+ Double price = Double.valueOf(findPrice(rawItem));
|
|
91
|
+ String type = findType(rawItem);
|
|
92
|
+ String expiration = findExpiration(rawItem);
|
|
93
|
+
|
|
94
|
+ return new Item(name, price, type, expiration);
|
|
95
|
+ }
|
|
96
|
+
|
|
97
|
+ //Cookie has a 0 in it so we fix it before parsing into the item
|
|
98
|
+ public String fixCo0kie(String input) {
|
|
99
|
+ String regexCo0kie = "[0]";
|
|
100
|
+ Pattern pattern = Pattern.compile(regexCo0kie);
|
|
101
|
+ Matcher matcher = pattern.matcher(input);
|
|
102
|
+ return matcher.replaceAll("o");
|
|
103
|
+ }
|
|
104
|
+
|
|
105
|
+ //We find the item name by searching for the name:item pair then return the group that has the item
|
|
106
|
+ public String findName(String name) throws ItemParseException {
|
|
107
|
+ String regexName = "([Nn]..[Ee]:)(\\w+)";
|
|
108
|
+ Pattern pattern = Pattern.compile(regexName);
|
|
109
|
+ Matcher matcher = pattern.matcher(name);
|
|
110
|
+ if (matcher.find()) {
|
|
111
|
+ if(matcher.group(2).contains("0")) {
|
|
112
|
+ return fixCo0kie(matcher.group(2).toLowerCase());
|
|
113
|
+ }else {
|
|
114
|
+ return matcher.group(2).toLowerCase();
|
|
115
|
+ }
|
|
116
|
+ } else {
|
|
117
|
+ throw new ItemParseException();
|
|
118
|
+ }
|
|
119
|
+
|
|
120
|
+ }
|
|
121
|
+
|
|
122
|
+ public String findPrice(String price) throws ItemParseException {
|
|
123
|
+ String regexPrice = "([Pp]...[Ee]:)(\\d\\.\\d{2})";
|
|
124
|
+ Pattern pattern = Pattern.compile(regexPrice);
|
|
125
|
+ Matcher matcher = pattern.matcher(price);
|
|
126
|
+
|
|
127
|
+ if (matcher.find()) {
|
|
128
|
+ return matcher.group(2);
|
|
129
|
+ } else {
|
|
130
|
+ throw new ItemParseException();
|
|
131
|
+ }
|
23
|
132
|
}
|
24
|
133
|
|
25
|
|
- private ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString){
|
26
|
|
- return new ArrayList<String>(Arrays.asList(inputString.split(stringPattern)));
|
|
134
|
+
|
|
135
|
+ public String findType(String type) throws ItemParseException {
|
|
136
|
+ String regexType = "([Tt]..[Ee]:)(\\w+)";
|
|
137
|
+ Pattern pattern = Pattern.compile(regexType);
|
|
138
|
+ Matcher matcher = pattern.matcher(type);
|
|
139
|
+
|
|
140
|
+ if (matcher.find()) {
|
|
141
|
+ return matcher.group(2).toLowerCase();
|
|
142
|
+ } else {
|
|
143
|
+ throw new ItemParseException();
|
|
144
|
+ }
|
|
145
|
+ }
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+ public String findExpiration(String expiration) throws ItemParseException {
|
|
149
|
+ String regexExpiration = "([E|e]........[N|n]:)(\\d\\/\\d{2}\\/\\d{4})";
|
|
150
|
+ Pattern pattern = Pattern.compile(regexExpiration);
|
|
151
|
+ Matcher matcher = pattern.matcher(expiration);
|
|
152
|
+ if (matcher.find()) {
|
|
153
|
+ return matcher.group(2).toLowerCase();
|
|
154
|
+ } else {
|
|
155
|
+ throw new ItemParseException();
|
|
156
|
+ }
|
27
|
157
|
}
|
28
|
158
|
|
|
159
|
+ public String printParsedJerkSON() {
|
|
160
|
+ StringBuilder print = new StringBuilder();
|
|
161
|
+ for (Map.Entry<String, ArrayList<Item>> entry : realFoodList.entrySet()) {
|
|
162
|
+ print.append("Name: ");
|
|
163
|
+ print.append(String.format("%-10s",entry.getKey()));
|
|
164
|
+ print.append("\t\tseen: " + entry.getValue().size() + " times\n");
|
|
165
|
+ print.append("================" + "\t\t" + "==================\n");
|
29
|
166
|
|
30
|
167
|
|
|
168
|
+
|
|
169
|
+ ArrayList<Double> priceList = getPrices(entry);
|
|
170
|
+
|
|
171
|
+ for (int i = 0; i < priceList.size(); i++) {
|
|
172
|
+ print.append("Price: ");
|
|
173
|
+ print.append(String.format("%-10s",priceList.get(i)));
|
|
174
|
+ print.append("\t\tseen: " + countNumberOfDifferentPrices(entry.getValue(), priceList.get(i)) + " times\n");
|
|
175
|
+ print.append("----------------" + "\t\t" + "------------------\n");
|
|
176
|
+
|
|
177
|
+ }
|
|
178
|
+ print.append("\n");
|
|
179
|
+ }
|
|
180
|
+
|
|
181
|
+ print.append("\nErrors" + "\t\t\t\t\tseen: " + ItemParseException.getCount() + " times\n");
|
|
182
|
+ return print.toString();
|
|
183
|
+ }
|
|
184
|
+
|
31
|
185
|
}
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
|