|
@@ -2,10 +2,22 @@ package io.zipcoder;
|
2
|
2
|
|
3
|
3
|
import java.util.ArrayList;
|
4
|
4
|
import java.util.Arrays;
|
|
5
|
+import java.util.Map;
|
|
6
|
+import java.util.TreeMap;
|
|
7
|
+import java.util.regex.Matcher;
|
|
8
|
+import java.util.regex.Pattern;
|
5
|
9
|
|
6
|
10
|
public class ItemParser {
|
|
11
|
+ //to organize my Key/Value pairs
|
|
12
|
+ private int exceptions = 0;
|
|
13
|
+ private TreeMap<String, ArrayList<Item>> groceryMap;
|
7
|
14
|
|
|
15
|
+ public ItemParser(){
|
|
16
|
+ //initializing the TreeMap
|
|
17
|
+ groceryMap = new TreeMap<String, ArrayList<Item>>();
|
|
18
|
+ }
|
8
|
19
|
|
|
20
|
+//splits a string of rawData by ## and puts it into an ArrayList called reponse
|
9
|
21
|
public ArrayList<String> parseRawDataIntoStringArray(String rawData){
|
10
|
22
|
String stringPattern = "##";
|
11
|
23
|
ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawData);
|
|
@@ -13,6 +25,64 @@ public class ItemParser {
|
13
|
25
|
}
|
14
|
26
|
|
15
|
27
|
public Item parseStringIntoItem(String rawItem) throws ItemParseException{
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+ if(findName(rawItem) == null || findPrice(rawItem) == null){
|
|
31
|
+ throw new ItemParseException();
|
|
32
|
+ }
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+ String name = findName(rawItem);
|
|
36
|
+ Double price = Double.parseDouble(findPrice(rawItem));
|
|
37
|
+ String type = findType(rawItem);
|
|
38
|
+ String expirationDate = findExpirationDate(rawItem);
|
|
39
|
+
|
|
40
|
+ Item item = new Item(name, price, type, expirationDate);
|
|
41
|
+ return item;
|
|
42
|
+ }
|
|
43
|
+
|
|
44
|
+ public String findExpirationDate(String rawItem)throws ItemParseException {
|
|
45
|
+ Pattern checkExpDateRegex = Pattern.compile("\\d\\/\\d+\\/\\d+");
|
|
46
|
+ Matcher regexExpDateMatcher = checkExpDateRegex.matcher(rawItem);
|
|
47
|
+
|
|
48
|
+ if(regexExpDateMatcher.find()){
|
|
49
|
+ return regexExpDateMatcher.group();
|
|
50
|
+ }
|
|
51
|
+ return null;
|
|
52
|
+ }
|
|
53
|
+
|
|
54
|
+ public String findType(String rawItem)throws ItemParseException {
|
|
55
|
+ Pattern checkTypeRegex = Pattern.compile("(?<=([Tt][Yy][Pp][Ee][^A-Za-z])).*?(?=[^A-Za-z0])");
|
|
56
|
+ Matcher regexTypeMatcher = checkTypeRegex.matcher(rawItem);
|
|
57
|
+
|
|
58
|
+ if(regexTypeMatcher.find()){
|
|
59
|
+ String fixtype = regexTypeMatcher.group().replace("\\d", "f");
|
|
60
|
+ return fixtype.toLowerCase();
|
|
61
|
+ }
|
|
62
|
+
|
|
63
|
+ return null;
|
|
64
|
+ }
|
|
65
|
+
|
|
66
|
+ public String findName(String rawItem)throws ItemParseException{
|
|
67
|
+ Pattern checkNameRegex = Pattern.compile("(?<=([Nn][Aa][Mm][Ee][^A-Za-z])).*?(?=[^A-Za-z0])");
|
|
68
|
+ Matcher regexNameMatcher = checkNameRegex.matcher(rawItem);
|
|
69
|
+
|
|
70
|
+ if(regexNameMatcher.find()){
|
|
71
|
+ String fixName = regexNameMatcher.group().replace("\\d", "o");
|
|
72
|
+ return fixName.toLowerCase();
|
|
73
|
+ }
|
|
74
|
+ return null;
|
|
75
|
+ }
|
|
76
|
+
|
|
77
|
+ public String findPrice(String rawItem) throws NumberFormatException{
|
|
78
|
+ Pattern checkPriceRegex = Pattern.compile("\\d\\.\\d*");
|
|
79
|
+ Matcher regexPriceMatcher = checkPriceRegex.matcher(rawItem);
|
|
80
|
+
|
|
81
|
+ if(regexPriceMatcher.find()){
|
|
82
|
+ if(!regexPriceMatcher.group().equals("")){
|
|
83
|
+ return regexPriceMatcher.group();
|
|
84
|
+ }
|
|
85
|
+ }
|
16
|
86
|
return null;
|
17
|
87
|
}
|
18
|
88
|
|
|
@@ -26,6 +96,89 @@ public class ItemParser {
|
26
|
96
|
return new ArrayList<String>(Arrays.asList(inputString.split(stringPattern)));
|
27
|
97
|
}
|
28
|
98
|
|
|
99
|
+ public TreeMap<String, ArrayList<Item>> getGroceryMap()throws Exception {
|
|
100
|
+ Main main = new Main();
|
|
101
|
+
|
|
102
|
+ ArrayList<String> items = parseRawDataIntoStringArray(main.readRawDataToString());
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+ for(String s: items){
|
|
106
|
+ try {
|
|
107
|
+ Item newItem = parseStringIntoItem(s);
|
|
108
|
+ if (!groceryMap.containsKey(newItem.getName())){
|
|
109
|
+ ArrayList<Item> myItemArrayList = new ArrayList<Item>();
|
|
110
|
+ myItemArrayList.add(newItem);
|
|
111
|
+ groceryMap.put(newItem.getName(), myItemArrayList);
|
|
112
|
+ } else {
|
|
113
|
+ groceryMap.get(newItem.getName()).add(newItem);
|
|
114
|
+ }
|
|
115
|
+ } catch (ItemParseException e){
|
|
116
|
+ exceptions++;
|
|
117
|
+ }
|
|
118
|
+ }
|
|
119
|
+ return groceryMap;
|
|
120
|
+ }
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+ public String printGroceries() throws Exception{
|
|
124
|
+
|
|
125
|
+ groceryMap = getGroceryMap();
|
|
126
|
+
|
|
127
|
+ StringBuilder sb = new StringBuilder();
|
|
128
|
+
|
|
129
|
+ //how do I print grocery list
|
|
130
|
+
|
|
131
|
+ for(Map.Entry<String, ArrayList<Item>> namesAndItems : groceryMap.entrySet()){
|
|
132
|
+ String makeUpperCase = namesAndItems.getKey().substring(0,1).toUpperCase() + namesAndItems.getKey().substring(1);
|
|
133
|
+
|
|
134
|
+ sb.append("\n" + "name: " + makeUpperCase + "\t\t\t\t" + "seen: " + namesAndItems.getValue().size() + " times");
|
|
135
|
+ sb.append("\n" + "------------------------------------------");
|
|
136
|
+
|
|
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
|
+ }
|
|
147
|
+
|
|
148
|
+ }
|
|
149
|
+ sb.append("\n\n" + "Errors: " + exceptions + " times\n\n");
|
|
150
|
+ sb.append("\n" + "------------------------------------------");
|
|
151
|
+ return sb.toString();
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+ }
|
|
155
|
+
|
|
156
|
+ private ArrayList<Double> getDifferentPrices(Map.Entry<String, ArrayList<Item>> namesAndItems) {
|
|
157
|
+
|
|
158
|
+ return null;
|
|
159
|
+ }
|
|
160
|
+
|
|
161
|
+ private boolean getPriceOccurrences(ArrayList<Item> value, Double aDouble) {
|
|
162
|
+ //retrieve the prices
|
|
163
|
+
|
|
164
|
+ return Boolean.parseBoolean(null);
|
|
165
|
+ }
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+ //PseudoCode
|
|
169
|
+ //I need to print the grocery list
|
|
170
|
+ //I need to be able to return prices - done
|
|
171
|
+ //I need to be able to count the exceptions - done
|
|
172
|
+ //need a replace method for the C00kies - done
|
|
173
|
+ //get key - done
|
|
174
|
+ //get value - done
|
|
175
|
+ //how to handle if the price field is empty - done
|
|
176
|
+ //patterns & matches - done
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
|
29
|
182
|
|
30
|
183
|
|
31
|
184
|
}
|