|
@@ -1,22 +1,27 @@
|
1
|
1
|
package io.zipcoder;
|
2
|
2
|
|
3
|
|
-import java.util.ArrayList;
|
4
|
|
-import java.util.Arrays;
|
5
|
|
-import java.util.Map;
|
6
|
|
-import java.util.TreeMap;
|
|
3
|
+import java.util.*;
|
7
|
4
|
import java.util.regex.Matcher;
|
8
|
5
|
import java.util.regex.Pattern;
|
9
|
6
|
|
10
|
7
|
public class ItemParser {
|
|
8
|
+
|
|
9
|
+
|
11
|
10
|
//to organize my Key/Value pairs
|
12
|
|
- private int exceptions = 0;
|
13
|
|
- private TreeMap<String, ArrayList<Item>> groceryMap;
|
|
11
|
+ public static int exceptions = 0;
|
|
12
|
+ private HashMap<String, ArrayList<Item>> groceryMap;
|
14
|
13
|
|
15
|
14
|
public ItemParser(){
|
16
|
15
|
//initializing the TreeMap
|
17
|
|
- groceryMap = new TreeMap<String, ArrayList<Item>>();
|
|
16
|
+ groceryMap = new HashMap<String, ArrayList<Item>>();
|
18
|
17
|
}
|
19
|
18
|
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
|
20
|
25
|
//splits a string of rawData by ## and puts it into an ArrayList called reponse
|
21
|
26
|
public ArrayList<String> parseRawDataIntoStringArray(String rawData){
|
22
|
27
|
String stringPattern = "##";
|
|
@@ -41,7 +46,7 @@ public class ItemParser {
|
41
|
46
|
return item;
|
42
|
47
|
}
|
43
|
48
|
|
44
|
|
- public String findExpirationDate(String rawItem)throws ItemParseException {
|
|
49
|
+ public String findExpirationDate(String rawItem) {
|
45
|
50
|
Pattern checkExpDateRegex = Pattern.compile("\\d\\/\\d+\\/\\d+");
|
46
|
51
|
Matcher regexExpDateMatcher = checkExpDateRegex.matcher(rawItem);
|
47
|
52
|
|
|
@@ -51,7 +56,7 @@ public class ItemParser {
|
51
|
56
|
return null;
|
52
|
57
|
}
|
53
|
58
|
|
54
|
|
- public String findType(String rawItem)throws ItemParseException {
|
|
59
|
+ public String findType(String rawItem){
|
55
|
60
|
Pattern checkTypeRegex = Pattern.compile("(?<=([Tt][Yy][Pp][Ee][^A-Za-z])).*?(?=[^A-Za-z0])");
|
56
|
61
|
Matcher regexTypeMatcher = checkTypeRegex.matcher(rawItem);
|
57
|
62
|
|
|
@@ -63,7 +68,7 @@ public class ItemParser {
|
63
|
68
|
return null;
|
64
|
69
|
}
|
65
|
70
|
|
66
|
|
- public String findName(String rawItem)throws ItemParseException{
|
|
71
|
+ public String findName(String rawItem){
|
67
|
72
|
Pattern checkNameRegex = Pattern.compile("(?<=([Nn][Aa][Mm][Ee][^A-Za-z])).*?(?=[^A-Za-z0])");
|
68
|
73
|
Matcher regexNameMatcher = checkNameRegex.matcher(rawItem);
|
69
|
74
|
|
|
@@ -74,7 +79,7 @@ public class ItemParser {
|
74
|
79
|
return null;
|
75
|
80
|
}
|
76
|
81
|
|
77
|
|
- public String findPrice(String rawItem) throws NumberFormatException{
|
|
82
|
+ public String findPrice(String rawItem){
|
78
|
83
|
Pattern checkPriceRegex = Pattern.compile("\\d\\.\\d*");
|
79
|
84
|
Matcher regexPriceMatcher = checkPriceRegex.matcher(rawItem);
|
80
|
85
|
|
|
@@ -96,7 +101,11 @@ public class ItemParser {
|
96
|
101
|
return new ArrayList<String>(Arrays.asList(inputString.split(stringPattern)));
|
97
|
102
|
}
|
98
|
103
|
|
99
|
|
- public TreeMap<String, ArrayList<Item>> getGroceryMap()throws Exception {
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+ public HashMap<String, ArrayList<Item>> getGroceryMap() throws Exception {
|
|
108
|
+
|
100
|
109
|
Main main = new Main();
|
101
|
110
|
|
102
|
111
|
ArrayList<String> items = parseRawDataIntoStringArray(main.readRawDataToString());
|
|
@@ -115,53 +124,82 @@ public class ItemParser {
|
115
|
124
|
} catch (ItemParseException e){
|
116
|
125
|
exceptions++;
|
117
|
126
|
}
|
118
|
|
- }
|
|
127
|
+ }
|
119
|
128
|
return groceryMap;
|
120
|
129
|
}
|
121
|
130
|
|
122
|
131
|
|
123
|
132
|
public String printGroceries() throws Exception{
|
124
|
|
-
|
125
|
133
|
groceryMap = getGroceryMap();
|
126
|
134
|
|
127
|
135
|
StringBuilder sb = new StringBuilder();
|
128
|
136
|
|
129
|
|
- //how do I print grocery list
|
130
|
|
-
|
131
|
137
|
for(Map.Entry<String, ArrayList<Item>> namesAndItems : groceryMap.entrySet()){
|
132
|
|
- String makeUpperCase = namesAndItems.getKey().substring(0,1).toUpperCase() + namesAndItems.getKey().substring(1);
|
|
138
|
+ sb.append("\nname: ");
|
|
139
|
+ sb.append(String.format("%8s",captitalizeFirstLetter(namesAndItems.getKey())));
|
|
140
|
+ sb.append("\t\t\t\tseen: "+getOccurencesOfItems(namesAndItems.getValue())+" times\n");
|
|
141
|
+ sb.append("==============="+"\t\t\t\t===============\n");
|
|
142
|
+ String priceList = generatePriceList(namesAndItems);
|
|
143
|
+ sb.append(priceList);
|
|
144
|
+ sb.append("---------------"+"\t\t\t\t---------------\n");
|
|
145
|
+ }
|
133
|
146
|
|
134
|
|
- sb.append("\n" + "name: " + makeUpperCase + "\t\t\t\t" + "seen: " + namesAndItems.getValue().size() + " times");
|
135
|
|
- sb.append("\n" + "------------------------------------------");
|
|
147
|
+ sb.append("\nErrors\t\t\t\t\t\tseen: "+exceptions+" times\n");
|
136
|
148
|
|
137
|
|
- ArrayList<Double> getDiffPrices = getDifferentPrices(namesAndItems);
|
138
|
|
- for (int i = 0; i < getDiffPrices.size(); i++) {
|
139
|
|
- if (getPriceOccurrences(namesAndItems.getValue(), getDiffPrices.get(i)) == 1) {
|
140
|
|
- String time = " time";
|
141
|
|
- } else {
|
142
|
|
- String time = " times";
|
143
|
|
- sb.append("\n" + "Price: " + getDiffPrices.get(i) + "\t\t\t\t" + " seen: " + getPriceOccurrences(namesAndItems.getValue(), getDiffPrices.get(i)) + " "+time);
|
144
|
|
- sb.append("\n" + "==========================================");
|
145
|
|
- }
|
146
|
|
- }
|
|
149
|
+ return sb.toString();
|
147
|
150
|
|
148
|
|
- }
|
149
|
|
- sb.append("\n\n" + "Errors: " + exceptions + " times\n\n");
|
150
|
|
- sb.append("\n" + "------------------------------------------");
|
151
|
|
- return sb.toString();
|
152
|
151
|
|
|
152
|
+ }
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+ public String generatePriceList(Map.Entry<String, ArrayList<Item>> input) {
|
|
156
|
+ String priceList = "";
|
|
157
|
+ ArrayList<Double> nonDupPrices = getDifferentPrices(input);
|
|
158
|
+ for(int i=0;i<nonDupPrices.size();i++){
|
|
159
|
+ priceList+="Price";
|
|
160
|
+ priceList+=(String.format("%10s",nonDupPrices.get(i)));
|
|
161
|
+ priceList+=("\t\t\t\tseen: "+ getPriceOccurrences(input.getValue(),nonDupPrices.get(i))+
|
|
162
|
+ " times\n");
|
|
163
|
+ }
|
|
164
|
+ return priceList;
|
|
165
|
+
|
|
166
|
+ }
|
153
|
167
|
|
|
168
|
+ public String captitalizeFirstLetter(String input) {
|
|
169
|
+ return input.substring(0, 1).toUpperCase() + input.substring(1).toLowerCase();
|
154
|
170
|
}
|
155
|
171
|
|
156
|
|
- private ArrayList<Double> getDifferentPrices(Map.Entry<String, ArrayList<Item>> namesAndItems) {
|
157
|
172
|
|
158
|
|
- return null;
|
|
173
|
+ public int getOccurencesOfItems(ArrayList list) {
|
|
174
|
+ return list.size();
|
|
175
|
+ }
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+ private ArrayList<Double> getDifferentPrices(Map.Entry<String, ArrayList<Item>> items) {
|
|
182
|
+
|
|
183
|
+ ArrayList<Double> differentPrices = new ArrayList<Double>();
|
|
184
|
+
|
|
185
|
+ for(int i =0; i<items.getValue().size(); i++){
|
|
186
|
+ if(!differentPrices.contains(items.getValue().get(i).getPrice())){
|
|
187
|
+ differentPrices.add(items.getValue().get(i).getPrice());
|
|
188
|
+ }
|
|
189
|
+ }
|
|
190
|
+
|
|
191
|
+ return differentPrices;
|
159
|
192
|
}
|
160
|
193
|
|
161
|
|
- private boolean getPriceOccurrences(ArrayList<Item> value, Double aDouble) {
|
162
|
|
- //retrieve the prices
|
|
194
|
+ private int getPriceOccurrences(ArrayList<Item> value, Double aDouble) {
|
|
195
|
+ int price = 0;
|
163
|
196
|
|
164
|
|
- return Boolean.parseBoolean(null);
|
|
197
|
+ for(int i = 0; i<value.size(); i++){
|
|
198
|
+ if(value.get(i).getPrice().equals(aDouble)){
|
|
199
|
+ price++;
|
|
200
|
+ }
|
|
201
|
+ }
|
|
202
|
+ return price;
|
165
|
203
|
}
|
166
|
204
|
|
167
|
205
|
|