|
@@ -1,31 +1,247 @@
|
1
|
1
|
package io.zipcoder;
|
2
|
2
|
|
3
|
|
-import java.util.ArrayList;
|
4
|
|
-import java.util.Arrays;
|
|
3
|
+import apple.laf.JRSUIUtils;
|
|
4
|
+
|
|
5
|
+import java.rmi.activation.ActivationGroup_Stub;
|
|
6
|
+import java.util.*;
|
|
7
|
+import java.util.regex.Pattern;
|
|
8
|
+import java.util.regex.Matcher;
|
|
9
|
+import java.util.Map.Entry;
|
5
|
10
|
|
6
|
11
|
public class ItemParser {
|
7
|
12
|
|
|
13
|
+ private ItemParseException ipe;
|
|
14
|
+ private List<String> itemStrings;
|
|
15
|
+ private List<Item> items;
|
|
16
|
+ private TreeMap<String, ArrayList<Item>> itemOrganizer;
|
|
17
|
+
|
|
18
|
+ public ItemParser() {
|
|
19
|
+ this.ipe = new ItemParseException();
|
|
20
|
+ this.itemStrings = new ArrayList<>();
|
|
21
|
+ this.items = new ArrayList<>();
|
|
22
|
+ this.itemOrganizer = new TreeMap<>();
|
|
23
|
+ }
|
|
24
|
+
|
|
25
|
+ public ItemParseException getIpe() {
|
|
26
|
+ return ipe;
|
|
27
|
+ }
|
8
|
28
|
|
9
|
|
- public ArrayList<String> parseRawDataIntoStringArray(String rawData){
|
|
29
|
+ public ArrayList<String> parseRawDataIntoItemStringArray(String rawData){
|
10
|
30
|
String stringPattern = "##";
|
11
|
|
- ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawData);
|
|
31
|
+ ArrayList<String> response = splitStringWithRegexPattern(rawData, stringPattern);
|
|
32
|
+ itemStrings = response;
|
12
|
33
|
return response;
|
13
|
34
|
}
|
14
|
35
|
|
15
|
|
- public Item parseStringIntoItem(String rawItem) throws ItemParseException{
|
16
|
|
- return null;
|
|
36
|
+ public List<String> getItemStrings() {
|
|
37
|
+ return itemStrings;
|
|
38
|
+ }
|
|
39
|
+
|
|
40
|
+ public void createItems() throws ItemParseException {
|
|
41
|
+ for (String itemString : itemStrings) {
|
|
42
|
+ Item item = parseStringIntoItem(itemString);
|
|
43
|
+ items.add(item);
|
|
44
|
+ }
|
|
45
|
+ }
|
|
46
|
+
|
|
47
|
+ public List<Item> getItems() {
|
|
48
|
+ return items;
|
|
49
|
+ }
|
|
50
|
+
|
|
51
|
+ public String itemsAsString() {
|
|
52
|
+ StringBuilder sb = new StringBuilder();
|
|
53
|
+ int count = 1;
|
|
54
|
+ for (Item item : items) {
|
|
55
|
+ sb.append(count + " " + item.toString() + "\n");
|
|
56
|
+ count++;
|
|
57
|
+ }
|
|
58
|
+ sb.deleteCharAt(sb.length() - 1);
|
|
59
|
+ return sb.toString();
|
|
60
|
+ }
|
|
61
|
+
|
|
62
|
+ public ArrayList<Item> getItemsOfSameName(String name) {
|
|
63
|
+ ArrayList<Item> itemsOfSameName = new ArrayList<>();
|
|
64
|
+ for (Item item : items) {
|
|
65
|
+ if (item.getName().equals(name)) {
|
|
66
|
+ itemsOfSameName.add(item);
|
|
67
|
+ }
|
|
68
|
+ }
|
|
69
|
+ return itemsOfSameName;
|
|
70
|
+ }
|
|
71
|
+
|
|
72
|
+ public void addNameAndItemsOfSameNameToItemOrganizer() {
|
|
73
|
+ for (Item item : items) {
|
|
74
|
+ String itemName = item.getName();
|
|
75
|
+ if (itemOrganizer.get(itemName) == null) {
|
|
76
|
+ itemOrganizer.put(itemName, getItemsOfSameName(itemName));
|
|
77
|
+ }
|
|
78
|
+ }
|
|
79
|
+ }
|
|
80
|
+
|
|
81
|
+ public TreeMap<String, ArrayList<Item>> getItemOrganizer() {
|
|
82
|
+ return itemOrganizer;
|
|
83
|
+ }
|
|
84
|
+
|
|
85
|
+ public String itemOrganizerAsString() {
|
|
86
|
+ StringBuilder sb = new StringBuilder();
|
|
87
|
+ for (Entry entry : itemOrganizer.entrySet()) {
|
|
88
|
+ sb.append("name: " + entry.getKey() + " " + "nameCount: " + itemOrganizer.get(entry.getKey()).size() + "\n"
|
|
89
|
+ + entry.getValue() + "\n");
|
|
90
|
+ }
|
|
91
|
+ sb.deleteCharAt(sb.length() - 1);
|
|
92
|
+ return sb.toString();
|
|
93
|
+ }
|
|
94
|
+
|
|
95
|
+ public String itemNameAndCountAsString(String name) {
|
|
96
|
+ String timeOrTimes = "times";
|
|
97
|
+ StringBuilder sb = new StringBuilder();
|
|
98
|
+ for (String keyName : itemOrganizer.keySet()) {
|
|
99
|
+ if (itemOrganizer.get(keyName).size() == 1) {
|
|
100
|
+ timeOrTimes = "time";
|
|
101
|
+ }
|
|
102
|
+ if (keyName.equals(name)) {
|
|
103
|
+ int numberOfEmptyPriceFields = countNumberOfEmptyPriceFields(name);
|
|
104
|
+ int actualCount = itemOrganizer.get(name).size() - numberOfEmptyPriceFields;
|
|
105
|
+ String s = String.format("%-5s%8s", "name:", capitalizeFirstLetterOnly(name));
|
|
106
|
+ sb.append(s);
|
|
107
|
+ sb.append("\t\tseen: " + actualCount + " " + timeOrTimes);
|
|
108
|
+ }
|
|
109
|
+ }
|
|
110
|
+ return sb.toString();
|
|
111
|
+ }
|
|
112
|
+
|
|
113
|
+ public int countNumberOfEmptyPriceFields(String name) {
|
|
114
|
+ int count = 0;
|
|
115
|
+ for (String keyName : itemOrganizer.keySet()) {
|
|
116
|
+ if (keyName.equals(name)) {
|
|
117
|
+ for (Item item : itemOrganizer.get(keyName)) {
|
|
118
|
+ if (item.getPrice() == 0) {
|
|
119
|
+ count++;
|
|
120
|
+ }
|
|
121
|
+ }
|
|
122
|
+ }
|
|
123
|
+ }
|
|
124
|
+ return count;
|
|
125
|
+ }
|
|
126
|
+
|
|
127
|
+ public String capitalizeFirstLetterOnly(String word) {
|
|
128
|
+ StringBuilder sb = new StringBuilder();
|
|
129
|
+ String firstLetterOfWord = word.substring(0,1);
|
|
130
|
+ sb.append(firstLetterOfWord.toUpperCase() + word.substring(1, word.length()));
|
|
131
|
+ return sb.toString();
|
|
132
|
+ }
|
|
133
|
+
|
|
134
|
+ public Map<Double, Integer> getPricesAndTheirCount(String name) {
|
|
135
|
+ Map<Double, Integer> pricesAndTheirCount = new TreeMap<>(Collections.reverseOrder());
|
|
136
|
+ for (String nameKey : itemOrganizer.keySet()) {
|
|
137
|
+ if (nameKey.equals(name)) {
|
|
138
|
+ for (int i = 0; i < itemOrganizer.get(nameKey).size(); i++) {
|
|
139
|
+ double price = itemOrganizer.get(nameKey).get(i).getPrice();
|
|
140
|
+ if (pricesAndTheirCount.get(price) == null) {
|
|
141
|
+ pricesAndTheirCount.put(price, 1);
|
|
142
|
+ } else {
|
|
143
|
+ pricesAndTheirCount.put(price, pricesAndTheirCount.get(price) + 1);
|
|
144
|
+ }
|
|
145
|
+ }
|
|
146
|
+ }
|
|
147
|
+ }
|
|
148
|
+ return pricesAndTheirCount;
|
|
149
|
+ }
|
|
150
|
+
|
|
151
|
+ public String pricesAndTheirCountAsString(Map<Double, Integer> pricesAndTheirCount) {
|
|
152
|
+
|
|
153
|
+ int count = 0;
|
|
154
|
+ StringBuilder sb = new StringBuilder();
|
|
155
|
+ sb.append("=============\t\t=============\n");
|
|
156
|
+ for (Double price : pricesAndTheirCount.keySet()) {
|
|
157
|
+ String timeOrTimes = "times";
|
|
158
|
+ if (pricesAndTheirCount.get(price) == 1) {
|
|
159
|
+ timeOrTimes = "time";
|
|
160
|
+ }
|
|
161
|
+ if (price != 0) {
|
|
162
|
+ sb.append("Price: " + price + "\t\tseen: " + pricesAndTheirCount.get(price) + " " + timeOrTimes + "\n");
|
|
163
|
+ sb.append("-------------\t\t-------------\n");
|
|
164
|
+ }
|
|
165
|
+ count++;
|
|
166
|
+ }
|
|
167
|
+ if (count > 1) {
|
|
168
|
+ sb.delete(sb.length() - 29, sb.length());
|
|
169
|
+ }
|
|
170
|
+// sb.deleteCharAt(sb.length() - 1);
|
|
171
|
+ return sb.toString();
|
|
172
|
+ }
|
|
173
|
+
|
|
174
|
+ public String getReportAsString() {
|
|
175
|
+ StringBuilder sb = new StringBuilder();
|
|
176
|
+ for (String name : itemOrganizer.keySet()) {
|
|
177
|
+ if (! name.equals("EMPTY")) {
|
|
178
|
+ sb.append(itemNameAndCountAsString(name) + "\n");
|
|
179
|
+ sb.append(pricesAndTheirCountAsString(getPricesAndTheirCount(name)) + "\n");
|
|
180
|
+ }
|
|
181
|
+ }
|
|
182
|
+ sb.append(getIpe().errorCountAsString());
|
|
183
|
+ return sb.toString();
|
|
184
|
+ }
|
|
185
|
+
|
|
186
|
+ public Item parseStringIntoItem(String rawItem) throws ItemParseException {
|
|
187
|
+
|
|
188
|
+ String emptyFieldName = "";
|
|
189
|
+ List<String> keyValuePairsArrayList = findKeyValuePairsInRawItemData(rawItem);
|
|
190
|
+
|
|
191
|
+ String upToAndIncludingColonString = "\\w+:";
|
|
192
|
+ Pattern upToAndIncludingColonPattern = Pattern.compile(upToAndIncludingColonString);
|
|
193
|
+
|
|
194
|
+ Matcher nameMatcher = upToAndIncludingColonPattern.matcher(keyValuePairsArrayList.get(0));
|
|
195
|
+ String name = nameMatcher.replaceAll("").toLowerCase();
|
|
196
|
+ String cookiesString = "(c|C)\\w+(s|S)";
|
|
197
|
+ Pattern cookiesPattern = Pattern.compile(cookiesString);
|
|
198
|
+ Matcher cookiesMatcher = cookiesPattern.matcher(name);
|
|
199
|
+ if (cookiesMatcher.find()) {
|
|
200
|
+ name = cookiesMatcher.replaceAll("cookies");
|
|
201
|
+ }
|
|
202
|
+ if (name.equals("")) {
|
|
203
|
+ name = "EMPTY";
|
|
204
|
+ emptyFieldName = "name";
|
|
205
|
+ ipe.logErrorCount(emptyFieldName, rawItem);
|
|
206
|
+ }
|
|
207
|
+
|
|
208
|
+ Matcher priceMatcher = upToAndIncludingColonPattern.matcher(keyValuePairsArrayList.get(1));
|
|
209
|
+ String priceString = priceMatcher.replaceAll("");
|
|
210
|
+ if (priceString.equals("")) {
|
|
211
|
+ priceString = "0.00";
|
|
212
|
+ emptyFieldName = "price";
|
|
213
|
+ ipe.logErrorCount(emptyFieldName, rawItem);
|
|
214
|
+ }
|
|
215
|
+ double price = Double.parseDouble(priceString);
|
|
216
|
+
|
|
217
|
+ Matcher typeMatcher = upToAndIncludingColonPattern.matcher(keyValuePairsArrayList.get(2));
|
|
218
|
+ String type = typeMatcher.replaceAll("").toLowerCase();
|
|
219
|
+ if (type.equals("")) {
|
|
220
|
+ type = "EMPTY";
|
|
221
|
+ emptyFieldName = "type";
|
|
222
|
+ ipe.logErrorCount(emptyFieldName, rawItem);
|
|
223
|
+ }
|
|
224
|
+
|
|
225
|
+ Matcher expirationMatcher = upToAndIncludingColonPattern.matcher(keyValuePairsArrayList.get(3));
|
|
226
|
+ String expiration = expirationMatcher.replaceAll("").toLowerCase();
|
|
227
|
+ if (expiration.equals("")) {
|
|
228
|
+ expiration = "EMPTY";
|
|
229
|
+ emptyFieldName = "expiration";
|
|
230
|
+ ipe.logErrorCount(emptyFieldName, rawItem);
|
|
231
|
+ }
|
|
232
|
+
|
|
233
|
+ Item item = new Item(name, price, type, expiration);
|
|
234
|
+ return item;
|
17
|
235
|
}
|
18
|
236
|
|
19
|
237
|
public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){
|
20
|
|
- String stringPattern = "[;|^]";
|
21
|
|
- ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawItem);
|
|
238
|
+ String stringPattern = "[^\\w:.\\/]";
|
|
239
|
+ ArrayList<String> response = splitStringWithRegexPattern(rawItem, stringPattern);
|
22
|
240
|
return response;
|
23
|
241
|
}
|
24
|
242
|
|
25
|
|
- private ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString){
|
|
243
|
+ private ArrayList<String> splitStringWithRegexPattern(String inputString, String stringPattern){
|
26
|
244
|
return new ArrayList<String>(Arrays.asList(inputString.split(stringPattern)));
|
27
|
245
|
}
|
28
|
246
|
|
29
|
|
-
|
30
|
|
-
|
31
|
247
|
}
|