|
@@ -1,21 +1,193 @@
|
1
|
1
|
package io.zipcoder;
|
2
|
2
|
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+import jdk.nashorn.internal.objects.Global;
|
|
6
|
+
|
|
7
|
+import java.io.FileNotFoundException;
|
|
8
|
+import java.io.PrintWriter;
|
3
|
9
|
import java.util.ArrayList;
|
4
|
10
|
import java.util.Arrays;
|
|
11
|
+import java.util.HashMap;
|
|
12
|
+import java.util.Map;
|
|
13
|
+import java.util.logging.Logger;
|
|
14
|
+import java.util.regex.Matcher;
|
|
15
|
+import java.util.regex.Pattern;
|
|
16
|
+import java.util.stream.Collectors;
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+public final class ItemParser {
|
|
20
|
+
|
|
21
|
+ private PrintWriter writer ;
|
|
22
|
+ private Map<String,ArrayList<Item>> groceryMap;
|
|
23
|
+
|
|
24
|
+ //counts exceptions
|
|
25
|
+ int exception = 0;
|
|
26
|
+
|
|
27
|
+ public ItemParser() {
|
|
28
|
+
|
|
29
|
+ groceryMap = new HashMap<>();
|
|
30
|
+ //add printWriter in constructor so that there is only ONE
|
|
31
|
+ try {
|
|
32
|
+ writer= new PrintWriter("/Users/karoushafennimore/Dev/PainfulAfternoon/src/main/resources/errors.txt");
|
|
33
|
+ } catch (FileNotFoundException e) {
|
|
34
|
+ e.printStackTrace();
|
|
35
|
+ }
|
|
36
|
+ }
|
|
37
|
+
|
|
38
|
+ public String printGroceryList(){
|
|
39
|
+
|
|
40
|
+ StringBuilder sb = new StringBuilder();
|
|
41
|
+ //for every new entry - put the Key and how many times you saw it
|
|
42
|
+ for (Map.Entry<String , ArrayList<Item> > item : groceryMap.entrySet()) {
|
|
43
|
+ sb.append(String.format("\nname: %9s", item.getKey().substring(0, 1).toUpperCase() + item.getKey().substring(1)));
|
|
44
|
+ sb.append("\t\t\tseen: " + item.getValue().size() + " times\n" + "===============" + "\t\t\t" + "===============\n");
|
5
|
45
|
|
6
|
|
-public class ItemParser {
|
|
46
|
+ ArrayList<Double> tempList = uniquePrices(item);
|
7
|
47
|
|
|
48
|
+ for(int i = 0; i < tempList.size(); i++) {
|
|
49
|
+ sb.append(String.format("Price: %8s", tempList.get(i)));
|
|
50
|
+ sb.append("\t\t\tseen: " + priceCount(item.getValue(), tempList.get(i)) + " times\n---------------" + "\t\t\t" + "---------------\n");
|
|
51
|
+ }
|
|
52
|
+ }
|
|
53
|
+ sb.append("\nErrors" + "\t\t\t\t\tseen: " + exception + " times\n");
|
|
54
|
+
|
|
55
|
+ return sb.toString();
|
|
56
|
+ }
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+ public ArrayList<Double> uniquePrices(Map.Entry<String , ArrayList<Item> > item) {
|
|
61
|
+
|
|
62
|
+ //returning unique Prices from the array list
|
|
63
|
+ ArrayList<Double> uniquePrice = new ArrayList<>();
|
|
64
|
+ //loop through and check if the price has been repeated...if it hasn't then add it
|
|
65
|
+ for (int i = 0; i < item.getValue().size(); i++) {
|
|
66
|
+ if (!uniquePrice.contains(item.getValue().get(i).getPrice())) {
|
|
67
|
+ uniquePrice.add(item.getValue().get(i).getPrice());
|
|
68
|
+ }
|
|
69
|
+ }
|
|
70
|
+ //output ArrayList of Doubles
|
|
71
|
+ return uniquePrice;
|
|
72
|
+
|
|
73
|
+ }
|
|
74
|
+
|
|
75
|
+ public int priceCount(ArrayList<Item> item, Double price) {
|
|
76
|
+ int count = 0;
|
|
77
|
+ //loop through items and look for the price to see how many times it is there
|
|
78
|
+ for(int i = 0; i < item.size(); i++) {
|
|
79
|
+ if(item.get(i).getPrice().equals(price)) {
|
|
80
|
+ count++;
|
|
81
|
+ }
|
|
82
|
+ }
|
|
83
|
+ return count;
|
|
84
|
+ }
|
|
85
|
+
|
|
86
|
+ public Map<String, ArrayList<Item>> getMap() {
|
|
87
|
+ return this.groceryMap;
|
|
88
|
+ }
|
8
|
89
|
|
9
|
90
|
public ArrayList<String> parseRawDataIntoStringArray(String rawData){
|
10
|
|
- String stringPattern = "##";
|
11
|
|
- ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawData);
|
12
|
|
- return response;
|
|
91
|
+ return splitStringWithRegexPattern("##" , rawData);
|
|
92
|
+ }
|
|
93
|
+
|
|
94
|
+ private void incrementList(Map<String, ArrayList<Item>> myMap, Item myItem) {
|
|
95
|
+ //if the Key is already in the Map
|
|
96
|
+ if(myMap.keySet().contains(myItem.getName())) {
|
|
97
|
+ //giving the map the item name and then adding the value to THAT key
|
|
98
|
+ myMap.get(myItem.getName()).add(myItem);
|
|
99
|
+ }else {
|
|
100
|
+ //if the key is not in the map then add it
|
|
101
|
+ myMap.put(myItem.getName(), new ArrayList<Item>());
|
|
102
|
+ //then add the value to that added KEY!
|
|
103
|
+ myMap.get(myItem.getName()).add(myItem);
|
|
104
|
+ }
|
|
105
|
+
|
|
106
|
+ }
|
|
107
|
+
|
|
108
|
+ public void addItemToList(ArrayList<String> groceryList) {
|
|
109
|
+
|
|
110
|
+ for(int i = 0; i < groceryList.size(); i++) {
|
|
111
|
+ try {
|
|
112
|
+ Item tryItem = (parseStringIntoItem(groceryList.get(i)));
|
|
113
|
+ incrementList(groceryMap, tryItem);
|
|
114
|
+ } catch (ItemParseException e) {
|
|
115
|
+ exception++;
|
|
116
|
+ try {
|
|
117
|
+ printErrorToFile(e);
|
|
118
|
+ } catch (FileNotFoundException e1) {
|
|
119
|
+ e1.printStackTrace();
|
|
120
|
+ }
|
|
121
|
+ }
|
|
122
|
+ }
|
|
123
|
+ }
|
|
124
|
+
|
|
125
|
+ public Item parseStringIntoItem(String rawItem) throws ItemParseException {
|
|
126
|
+ String itemName = findName(rawItem);
|
|
127
|
+ Double itemPrice = itemPrice(rawItem);
|
|
128
|
+ String itemType = itemType(rawItem);
|
|
129
|
+ String itemExpiration = itemExpiration(rawItem);
|
|
130
|
+
|
|
131
|
+ //if item does not have a name or item Price - dont create it bc its irrelevant data.
|
|
132
|
+ if(findName(rawItem) == null || itemPrice(rawItem) == null) {
|
|
133
|
+ throw new ItemParseException();
|
|
134
|
+ }
|
|
135
|
+ return new Item (itemName, itemPrice, itemType, itemExpiration);
|
|
136
|
+ }
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+ //getting all the values from string for
|
|
140
|
+ private String findName(String rawItem) {
|
|
141
|
+ Pattern namePattern = Pattern.compile("(?<=([Nn][Aa][Mm][Ee][^A-Za-z])).*?(?=[^A-Za-z0])");
|
|
142
|
+ Matcher regex = namePattern.matcher(rawItem);
|
|
143
|
+
|
|
144
|
+ if (regex.find()) {
|
|
145
|
+ if (regex.group().length() > 0) {
|
|
146
|
+ return replaceZeros(regex.group().toLowerCase());
|
|
147
|
+ } else {
|
|
148
|
+ return null;
|
|
149
|
+ }
|
|
150
|
+ } else {
|
|
151
|
+ return null;
|
|
152
|
+ }
|
|
153
|
+ }
|
|
154
|
+
|
|
155
|
+ public Double itemPrice(String rawItem) {
|
|
156
|
+ Pattern pricePattern = Pattern.compile("(?<=([Pp][Rr][Ii][Cc][Ee][^A-Za-z])).*?(?=[^0-9.])");
|
|
157
|
+ Matcher regex1 = pricePattern.matcher(rawItem);
|
|
158
|
+ if (regex1.find()) {
|
|
159
|
+ if (regex1.group().length() > 0) {
|
|
160
|
+ return Double.parseDouble(regex1.group().toLowerCase());
|
|
161
|
+ }
|
|
162
|
+ }
|
|
163
|
+ return null;
|
13
|
164
|
}
|
14
|
165
|
|
15
|
|
- public Item parseStringIntoItem(String rawItem) throws ItemParseException{
|
|
166
|
+ public String itemType(String rawItem) {
|
|
167
|
+ Pattern typePattern = Pattern.compile("(?<=([Tt][Yy][Pp][Ee][^A-Za-z])).*?(?=[^A-Za-z0])");
|
|
168
|
+ Matcher regex2 = typePattern.matcher(rawItem);
|
|
169
|
+ if (regex2.find()) {
|
|
170
|
+ return regex2.group().toLowerCase();
|
|
171
|
+ }
|
|
172
|
+ return null;
|
|
173
|
+ }
|
|
174
|
+ public String itemExpiration(String rawItem) {
|
|
175
|
+ Pattern expirationPattern = Pattern.compile("(?<=([Ee][Xx][Pp][Ii][Rr][Aa][Tt][Ii][Oo][Nn][^A-Za-z]))(.)*[^#]");
|
|
176
|
+ Matcher regex3 = expirationPattern.matcher(rawItem);
|
|
177
|
+ if (regex3.find()) {
|
|
178
|
+ return regex3.group();
|
|
179
|
+ }
|
16
|
180
|
return null;
|
17
|
181
|
}
|
18
|
182
|
|
|
183
|
+ //this is to catch the 0's in cookie and replace with a o instead
|
|
184
|
+ private String replaceZeros(String rawItem) {
|
|
185
|
+
|
|
186
|
+ Pattern pattern = Pattern.compile("[0]");
|
|
187
|
+ Matcher regexName2 = pattern.matcher(rawItem);
|
|
188
|
+ return regexName2.replaceAll("o");
|
|
189
|
+ }
|
|
190
|
+
|
19
|
191
|
public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){
|
20
|
192
|
String stringPattern = "[;|^]";
|
21
|
193
|
ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawItem);
|
|
@@ -23,9 +195,16 @@ public class ItemParser {
|
23
|
195
|
}
|
24
|
196
|
|
25
|
197
|
private ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString){
|
26
|
|
- return new ArrayList<String>(Arrays.asList(inputString.split(stringPattern)));
|
|
198
|
+ return new ArrayList<>(Arrays.asList(inputString.split(stringPattern)));
|
27
|
199
|
}
|
28
|
200
|
|
|
201
|
+ public void printErrorToFile(ItemParseException e) throws FileNotFoundException {
|
|
202
|
+ //getting entire stack report on the errors that I am getting.
|
|
203
|
+ writer.write(Arrays.stream(e.getStackTrace()).map(Object::toString).collect(Collectors.joining("\n")));
|
29
|
204
|
|
|
205
|
+ }
|
30
|
206
|
|
|
207
|
+ public void flushExceptionsToFile() {
|
|
208
|
+ writer.close();
|
|
209
|
+ }
|
31
|
210
|
}
|