|
@@ -1,31 +1,198 @@
|
1
|
1
|
package io.zipcoder;
|
2
|
2
|
|
3
|
|
-import java.util.ArrayList;
|
4
|
|
-import java.util.Arrays;
|
|
3
|
+import java.util.*;
|
|
4
|
+import java.util.regex.Matcher;
|
|
5
|
+import java.util.regex.Pattern;
|
5
|
6
|
|
6
|
7
|
public class ItemParser {
|
|
8
|
+ public HashMap<String, ArrayList<Item>> completeList = new HashMap<String, ArrayList<Item>>();
|
|
9
|
+ int exceptionCounter = 0;
|
7
|
10
|
|
8
|
11
|
|
9
|
|
- public ArrayList<String> parseRawDataIntoStringArray(String rawData){
|
|
12
|
+ public ArrayList<String> parseRawDataIntoStringArray(String rawData) {
|
10
|
13
|
String stringPattern = "##";
|
11
|
|
- ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawData);
|
|
14
|
+ ArrayList<String> response = splitStringWithRegexPattern(stringPattern, rawData);
|
12
|
15
|
return response;
|
13
|
16
|
}
|
14
|
17
|
|
15
|
|
- public Item parseStringIntoItem(String rawItem) throws ItemParseException{
|
16
|
|
- return null;
|
|
18
|
+ public Item parseStringIntoItem(String rawItem) throws ItemParseException {
|
|
19
|
+ if (nameFinder(rawItem) == null || priceFinder(rawItem) == null) {
|
|
20
|
+ throw new ItemParseException();
|
|
21
|
+ }
|
|
22
|
+
|
|
23
|
+ String name = nameFinder(rawItem);
|
|
24
|
+ Double price = Double.parseDouble(priceFinder(rawItem));
|
|
25
|
+ String type = "food";
|
|
26
|
+ String expiration = expFinder(rawItem);
|
|
27
|
+
|
|
28
|
+ return new Item(name, price, type, expiration);
|
17
|
29
|
}
|
18
|
30
|
|
19
|
|
- public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){
|
|
31
|
+ public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem) {
|
20
|
32
|
String stringPattern = "[;|^]";
|
21
|
|
- ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawItem);
|
|
33
|
+ ArrayList<String> response = splitStringWithRegexPattern(stringPattern, rawItem);
|
22
|
34
|
return response;
|
23
|
35
|
}
|
24
|
36
|
|
25
|
|
- private ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString){
|
|
37
|
+ private ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString) {
|
26
|
38
|
return new ArrayList<String>(Arrays.asList(inputString.split(stringPattern)));
|
27
|
39
|
}
|
28
|
40
|
|
|
41
|
+ public String nameFinder(String rawNames) {
|
|
42
|
+ StringBuilder found = new StringBuilder();
|
|
43
|
+
|
|
44
|
+ String itemSearch = rawNames;
|
|
45
|
+ Pattern foodNames = Pattern.compile("\\w+(?=\\Wprice)", Pattern.CASE_INSENSITIVE);
|
|
46
|
+ Matcher m = foodNames.matcher(itemSearch);
|
|
47
|
+ int lastMatchPos = 0;
|
|
48
|
+ while (m.find()) {
|
|
49
|
+ if (!m.group().equals("")) {
|
|
50
|
+ found.append(m.group().replaceAll("\\d", "o"));
|
|
51
|
+ lastMatchPos = m.end();
|
|
52
|
+ }
|
|
53
|
+ return found.toString().toLowerCase();
|
|
54
|
+ }
|
|
55
|
+ return null;
|
|
56
|
+ }
|
|
57
|
+
|
|
58
|
+ public String priceFinder(String rawPrices) {
|
|
59
|
+ StringBuilder found = new StringBuilder();
|
|
60
|
+
|
|
61
|
+ String itemSearch = rawPrices;
|
|
62
|
+ Pattern foodNames = Pattern.compile("\\d+\\.\\d\\d", Pattern.CASE_INSENSITIVE);
|
|
63
|
+ Matcher m = foodNames.matcher(itemSearch);
|
|
64
|
+ int lastMatchPos = 0;
|
|
65
|
+ while (m.find()) {
|
|
66
|
+ if (!m.group().equals("")) {
|
|
67
|
+ found.append(m.group() + "");
|
|
68
|
+ lastMatchPos = m.end();
|
|
69
|
+ }
|
|
70
|
+ return found.toString();
|
|
71
|
+ }
|
|
72
|
+ return null;
|
|
73
|
+ }
|
|
74
|
+
|
|
75
|
+ public String expFinder(String rawExp) {
|
|
76
|
+ StringBuilder found = new StringBuilder();
|
|
77
|
+
|
|
78
|
+ String itemSearch = rawExp;
|
|
79
|
+ Pattern foodNames = Pattern.compile("\\d{1,2}\\/\\d\\d\\/\\d{4}", Pattern.CASE_INSENSITIVE);
|
|
80
|
+ Matcher m = foodNames.matcher(itemSearch);
|
|
81
|
+ int lastMatchPos = 0;
|
|
82
|
+ while (m.find()) {
|
|
83
|
+ found.append(m.group() + "");
|
|
84
|
+ lastMatchPos = m.end();
|
|
85
|
+ }
|
|
86
|
+ return found.toString();
|
|
87
|
+ }
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+ public HashMap<String, ArrayList<Item>> getCompleteList() throws Exception {
|
|
91
|
+ Main mainObject = new Main();
|
|
92
|
+ ArrayList<String> rawinputdata = parseRawDataIntoStringArray(mainObject.readRawDataToString());
|
|
93
|
+ for (String item : rawinputdata) {
|
|
94
|
+ try {
|
|
95
|
+ Item myItem = parseStringIntoItem(item);
|
|
96
|
+ if (!completeList.containsKey(myItem.getName())) {
|
|
97
|
+ ArrayList<Item> myItemList = new ArrayList<Item>();
|
|
98
|
+ myItemList.add(myItem);
|
|
99
|
+ completeList.put(myItem.getName(), myItemList);
|
|
100
|
+ } else {
|
|
101
|
+ completeList.get(myItem.getName()).add(myItem);
|
|
102
|
+ }
|
|
103
|
+
|
|
104
|
+ } catch (ItemParseException except) {
|
|
105
|
+ exceptionCounter++;
|
|
106
|
+ }
|
|
107
|
+ }
|
|
108
|
+ return completeList;
|
|
109
|
+ }
|
|
110
|
+
|
|
111
|
+ public String printFinalList() throws Exception {
|
|
112
|
+ completeList = getCompleteList();
|
|
113
|
+ StringBuilder listBuilder = new StringBuilder();
|
|
114
|
+
|
|
115
|
+ for(Map.Entry<String,ArrayList<Item>>groceryItems:completeList.entrySet()){
|
|
116
|
+ listBuilder.append("\nname: ");
|
|
117
|
+ listBuilder.append(String.format("%8s",captitalizeFirstLetter(groceryItems.getKey())));
|
|
118
|
+ listBuilder.append("\t\t\t\tseen: "+getOccurencesOfItems(groceryItems.getValue())+" times\n");
|
|
119
|
+ listBuilder.append("==============="+"\t\t\t\t===============\n");
|
|
120
|
+ String priceReport = generatePriceReport(groceryItems);
|
|
121
|
+ listBuilder.append(priceReport);
|
|
122
|
+ listBuilder.append("---------------"+"\t\t\t\t---------------\n");
|
|
123
|
+ }
|
|
124
|
+
|
|
125
|
+ listBuilder.append("\nErrors\t\t\t\t\t\tseen: "+exceptionCounter+" times\n");
|
|
126
|
+
|
|
127
|
+ return listBuilder.toString();
|
|
128
|
+ }
|
|
129
|
+
|
|
130
|
+ public int getOccurencesOfItems(ArrayList list) {
|
|
131
|
+ return list.size();
|
|
132
|
+ }
|
|
133
|
+
|
|
134
|
+ public int getOccurencesOfPrices(ArrayList<Item> list, Double price) {
|
|
135
|
+ int priceCounter = 0;
|
|
136
|
+ for (int i = 0; i < list.size(); i++) {
|
|
137
|
+ if (list.get(i).getPrice().equals(price) ) {
|
|
138
|
+ priceCounter++;
|
|
139
|
+ }
|
|
140
|
+ }
|
|
141
|
+ return priceCounter;
|
|
142
|
+ }
|
|
143
|
+
|
|
144
|
+ public String generatePriceReport(Map.Entry<String, ArrayList<Item>> input) {
|
|
145
|
+ String priceReport = "";
|
|
146
|
+ ArrayList<Double> nonDupPrices = getUniquePrices(input);
|
|
147
|
+ for(int i=0;i<nonDupPrices.size();i++){
|
|
148
|
+ priceReport+="Price";
|
|
149
|
+ priceReport+=(String.format("%10s",nonDupPrices.get(i)));
|
|
150
|
+ priceReport+=("\t\t\t\tseen: "+ getOccurencesOfPrices(input.getValue(),nonDupPrices.get(i))+
|
|
151
|
+ " times\n");
|
|
152
|
+ }
|
|
153
|
+ return priceReport;
|
|
154
|
+
|
|
155
|
+ }
|
|
156
|
+
|
|
157
|
+ public ArrayList<Double> getUniquePrices(Map.Entry<String, ArrayList<Item>> input) {
|
|
158
|
+ ArrayList<Double> uniquePrices = new ArrayList<>();
|
|
159
|
+ for (int i=0;i<input.getValue().size();i++) {
|
|
160
|
+ if (!uniquePrices.contains(input.getValue().get(i).getPrice())) {
|
|
161
|
+ uniquePrices.add(input.getValue().get(i).getPrice());
|
|
162
|
+ }
|
|
163
|
+ }
|
|
164
|
+ return uniquePrices;
|
|
165
|
+ }
|
|
166
|
+
|
|
167
|
+ public String captitalizeFirstLetter(String input) {
|
|
168
|
+ return input.substring(0, 1).toUpperCase() + input.substring(1).toLowerCase();
|
|
169
|
+ }
|
|
170
|
+
|
|
171
|
+ }
|
|
172
|
+
|
29
|
173
|
|
|
174
|
+// public int counts() throws Exception {
|
|
175
|
+// String input = getCompleteList().toString();
|
|
176
|
+// String[] inputs = input.split("([\\W\\s+])");
|
|
177
|
+// Map<String, Integer> listCounter = new HashMap<String, Integer>();
|
|
178
|
+// for (String searchWord : inputs) {
|
|
179
|
+// if (listCounter.containsKey(searchWord)) {
|
|
180
|
+// listCounter.put(searchWord, listCounter.get(searchWord) + 1);
|
|
181
|
+// } else {
|
|
182
|
+// listCounter.put(searchWord, 1);
|
|
183
|
+// }
|
|
184
|
+// }
|
|
185
|
+// return listCounter.get();
|
|
186
|
+// }
|
|
187
|
+// public String printList() throws Exception{
|
|
188
|
+// ItemParser itemParser = new ItemParser();
|
|
189
|
+// StringBuilder finalList = new StringBuilder();
|
|
190
|
+// HashMap<String, ArrayList<Item>> testMap = itemParser.getCompleteList();
|
|
191
|
+// for (String key : testMap.keySet()) {
|
|
192
|
+// for (int i = 0; i < testMap.get(key).size(); i++) {
|
|
193
|
+// finalList.append(("name: "+ key + " seen:"+testMap.get(key).size()+"\n" + testMap.get(key).get(i).getPrice()));
|
|
194
|
+// }
|
|
195
|
+// }
|
|
196
|
+// return finalList.toString();
|
|
197
|
+// }
|
30
|
198
|
|
31
|
|
-}
|