|
@@ -2,30 +2,220 @@ package io.zipcoder;
|
2
|
2
|
|
3
|
3
|
import java.util.ArrayList;
|
4
|
4
|
import java.util.Arrays;
|
|
5
|
+import java.util.Date;
|
|
6
|
+import java.util.HashMap;
|
|
7
|
+import java.util.regex.Matcher;
|
|
8
|
+import java.util.regex.Pattern;
|
|
9
|
+import java.util.Map;
|
5
|
10
|
|
6
|
|
-public class ItemParser {
|
|
11
|
+public class ItemParser
|
|
12
|
+{
|
|
13
|
+ private Pattern pattern;
|
|
14
|
+ private Matcher matcher;
|
|
15
|
+ private Integer countExceptions = 0;
|
|
16
|
+ private Main main = new Main();
|
|
17
|
+ private Map<String, ArrayList<Item>> groceryMap = new HashMap<String, ArrayList<Item>>();
|
|
18
|
+ private String setTimes;
|
7
|
19
|
|
|
20
|
+ //
|
|
21
|
+ private ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString)
|
|
22
|
+ {
|
|
23
|
+ return new ArrayList<String>(Arrays.asList(inputString.split(stringPattern)));
|
|
24
|
+ }
|
8
|
25
|
|
|
26
|
+ //This method calls the above method
|
9
|
27
|
public ArrayList<String> parseRawDataIntoStringArray(String rawData){
|
10
|
28
|
String stringPattern = "##";
|
11
|
29
|
ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawData);
|
12
|
30
|
return response;
|
13
|
31
|
}
|
14
|
32
|
|
15
|
|
- public Item parseStringIntoItem(String rawItem) throws ItemParseException{
|
|
33
|
+ //not used really, but tested
|
|
34
|
+ public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem)
|
|
35
|
+ {
|
|
36
|
+ String stringPattern = "[@|^|*|%|!|;]";
|
|
37
|
+ ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawItem);
|
|
38
|
+ return response;
|
|
39
|
+ }
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+ //Uses the helper methods created below named checkName, checkPrice, checkType, checkExpiration
|
|
43
|
+ public Item parseStringIntoItem(String rawItem) throws ItemParseException
|
|
44
|
+ {
|
|
45
|
+
|
|
46
|
+ if (checkName(rawItem) == null || checkPrice(rawItem) == null) {
|
|
47
|
+ throw new ItemParseException();
|
|
48
|
+ }
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+ String name = checkName(rawItem);
|
|
52
|
+ Double price = Double.parseDouble(checkPrice(rawItem));
|
|
53
|
+ String type = checkType(rawItem);
|
|
54
|
+ String expiration = checkExpiration(rawItem);
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+ return new Item(name, price, type, expiration);
|
|
58
|
+
|
|
59
|
+ }
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+ public String checkName(String rawItem)
|
|
65
|
+ {
|
|
66
|
+
|
|
67
|
+ String search = "(?<=([Nn][Aa][Mm][Ee][^A-Za-z])).*?(?=[^A-Za-z0])";
|
|
68
|
+ pattern = Pattern.compile(search);
|
|
69
|
+ matcher = pattern.matcher(rawItem);
|
|
70
|
+
|
|
71
|
+ if (matcher.find())
|
|
72
|
+ {
|
|
73
|
+ if(!matcher.group().equals(""))
|
|
74
|
+ {
|
|
75
|
+ String name = matcher.group().replaceAll("\\d", "o");
|
|
76
|
+ return name.toLowerCase();
|
|
77
|
+ }
|
|
78
|
+ }
|
|
79
|
+
|
16
|
80
|
return null;
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+ }
|
|
85
|
+
|
|
86
|
+ public String checkPrice(String rawItem)
|
|
87
|
+ {
|
|
88
|
+
|
|
89
|
+ pattern = Pattern.compile("(?<=([Pp][Rr][Ii][Cc][Ee][^A-Za-z])).*?(?=[^0-9.])");
|
|
90
|
+ matcher = pattern.matcher(rawItem);
|
|
91
|
+
|
|
92
|
+ if (matcher.find()) {
|
|
93
|
+ if (!matcher.group().equals("")) {
|
|
94
|
+ return matcher.group();
|
|
95
|
+ }
|
|
96
|
+ }
|
|
97
|
+ return null;
|
|
98
|
+
|
17
|
99
|
}
|
18
|
100
|
|
19
|
|
- public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){
|
20
|
|
- String stringPattern = "[;|^]";
|
21
|
|
- ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawItem);
|
22
|
|
- return response;
|
|
101
|
+ public String checkType(String rawItem) //throws itemParseException
|
|
102
|
+ {
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+ pattern = Pattern.compile("(?<=([Tt][Yy][Pp][Ee][^A-Za-z])).*?(?=[^A-Za-z0])");
|
|
106
|
+ matcher = pattern.matcher(rawItem);
|
|
107
|
+
|
|
108
|
+ if (matcher.find()) {
|
|
109
|
+ return matcher.group().toLowerCase();
|
|
110
|
+ }
|
|
111
|
+ return null;
|
23
|
112
|
}
|
24
|
113
|
|
25
|
|
- private ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString){
|
26
|
|
- return new ArrayList<String>(Arrays.asList(inputString.split(stringPattern)));
|
|
114
|
+ public String checkExpiration(String rawItem)
|
|
115
|
+ {
|
|
116
|
+ pattern = Pattern.compile("(?<=([Ee][Xx][Pp][Ii][Rr][Aa][Tt][Ii][Oo][Nn][^A-Za-z]))(.)+[^#]");
|
|
117
|
+ matcher = pattern.matcher(rawItem);
|
|
118
|
+ if (matcher.find()) {
|
|
119
|
+ return matcher.group();
|
|
120
|
+ }
|
|
121
|
+ return null;
|
|
122
|
+ }
|
|
123
|
+
|
|
124
|
+ public Map<String, ArrayList<Item>> getMap() throws Exception
|
|
125
|
+ {
|
|
126
|
+ ArrayList<String> allItems = parseRawDataIntoStringArray(main.readRawDataToString());
|
|
127
|
+
|
|
128
|
+ for (String eachItem : allItems)
|
|
129
|
+ {
|
|
130
|
+ try {
|
|
131
|
+ Item newItem = parseStringIntoItem(eachItem);
|
|
132
|
+ if (!groceryMap.containsKey(newItem.getName()))
|
|
133
|
+ {
|
|
134
|
+ ArrayList<Item> thisItem = new ArrayList<Item>();
|
|
135
|
+ thisItem.add(newItem);
|
|
136
|
+ groceryMap.put(newItem.getName(), thisItem);
|
|
137
|
+ }
|
|
138
|
+ else
|
|
139
|
+ {
|
|
140
|
+ groceryMap.get(newItem.getName()).add(newItem);
|
|
141
|
+ }
|
|
142
|
+ }
|
|
143
|
+
|
|
144
|
+ catch (ItemParseException e)
|
|
145
|
+ {
|
|
146
|
+ countExceptions++;
|
|
147
|
+ }
|
|
148
|
+ }
|
|
149
|
+
|
|
150
|
+ return groceryMap;
|
|
151
|
+ }
|
|
152
|
+
|
|
153
|
+ public String displayOutput() throws Exception{
|
|
154
|
+ groceryMap = getMap();
|
|
155
|
+
|
|
156
|
+ StringBuilder sb = new StringBuilder();
|
|
157
|
+
|
|
158
|
+ for (Map.Entry<String, ArrayList<Item>> item: groceryMap.entrySet())
|
|
159
|
+ {
|
|
160
|
+ String upperCase = item.getKey().substring(0, 1).toUpperCase()+ item.getKey().substring(1);
|
|
161
|
+
|
|
162
|
+ sb.append("\n" +
|
|
163
|
+ String.format("%-5s%10s%15s%2d%5s", "name:", upperCase
|
|
164
|
+ , "seen: ", item.getValue().size(), " times"));
|
|
165
|
+
|
|
166
|
+ sb.append("\n" + String.format("%15s%3s%5s", "===============", "\t\t\t", "===============") + "\n");
|
|
167
|
+
|
|
168
|
+ ArrayList<Double> uniquePriceList = getUniquePrices(item);
|
|
169
|
+
|
|
170
|
+ for (int i = 0; i <uniquePriceList.size(); i++)
|
|
171
|
+ {
|
|
172
|
+ if(priceOccurencesSeen(item.getValue(), uniquePriceList.get(i)) == 1){
|
|
173
|
+ setTimes = " time";
|
|
174
|
+ } else setTimes = " times";
|
|
175
|
+
|
|
176
|
+ sb.append(String.format("%-11s%.2f%15s%2d%5s", "Price:", uniquePriceList.get(i)
|
|
177
|
+ , "seen: ", priceOccurencesSeen(item.getValue(), uniquePriceList.get(i))
|
|
178
|
+ , setTimes));
|
|
179
|
+ sb.append("\n" +
|
|
180
|
+ String.format("%15s%3s%5s", "---------------", "\t\t\t", "---------------") + "\n");
|
|
181
|
+ }
|
|
182
|
+
|
|
183
|
+ }
|
|
184
|
+
|
|
185
|
+ sb.append("\n"+ String.format("%-20s%10s%2d%5s", "Errors", "seen: ", countExceptions, " times"));
|
|
186
|
+
|
|
187
|
+ return sb.toString();
|
27
|
188
|
}
|
28
|
189
|
|
|
190
|
+ public int priceOccurencesSeen(ArrayList<Item>list, Double price)
|
|
191
|
+ {
|
|
192
|
+ int priceCounter = 0;
|
|
193
|
+
|
|
194
|
+ for (int i = 0; i < list.size(); i++)
|
|
195
|
+ {
|
|
196
|
+ if(list.get(i).getPrice().equals(price))
|
|
197
|
+ {
|
|
198
|
+ priceCounter++;
|
|
199
|
+ }
|
|
200
|
+ }
|
|
201
|
+
|
|
202
|
+ return priceCounter;
|
|
203
|
+ }
|
|
204
|
+
|
|
205
|
+ public ArrayList<Double> getUniquePrices(Map.Entry<String,ArrayList<Item>> item)
|
|
206
|
+ {
|
|
207
|
+ ArrayList<Double> uniquePrices = new ArrayList<Double>();
|
|
208
|
+
|
|
209
|
+ for (int i = 0; i < item.getValue().size(); i++)
|
|
210
|
+ {
|
|
211
|
+ if(!uniquePrices.contains(item.getValue().get(i).getPrice()))
|
|
212
|
+ {
|
|
213
|
+ uniquePrices.add(item.getValue().get(i).getPrice());
|
|
214
|
+ }
|
|
215
|
+ }
|
|
216
|
+
|
|
217
|
+ return uniquePrices;
|
|
218
|
+ }
|
29
|
219
|
|
30
|
220
|
|
31
|
221
|
}
|