|
@@ -1,12 +1,9 @@
|
1
|
1
|
package io.zipcoder;
|
2
|
2
|
|
3
|
|
-import com.sun.org.apache.xerces.internal.impl.xpath.regex.Match;
|
4
|
|
-import com.sun.org.apache.xpath.internal.operations.Bool;
|
5
|
|
-
|
6
|
|
-import java.lang.reflect.Array;
|
7
|
3
|
import java.util.ArrayList;
|
8
|
4
|
import java.util.Arrays;
|
9
|
5
|
import java.util.HashMap;
|
|
6
|
+import java.util.Map;
|
10
|
7
|
import java.util.regex.Matcher;
|
11
|
8
|
import java.util.regex.Pattern;
|
12
|
9
|
|
|
@@ -26,18 +23,13 @@ public class ItemParser {
|
26
|
23
|
ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawItem);
|
27
|
24
|
return response;
|
28
|
25
|
}
|
29
|
|
-// public ArrayList<String> splitStringsAtSemiColon(String rawItem){ // entries split by : Example:
|
30
|
|
-// String stringPattern = ":";
|
31
|
|
-// ArrayList<String> response = splitStringWithRegexPattern(stringPattern, rawItem);
|
32
|
|
-// return response;
|
33
|
|
-// }
|
34
|
26
|
|
35
|
27
|
private ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString){ // helper method that splits strings at characters above into ArrayLists
|
36
|
28
|
return new ArrayList<String>(Arrays.asList(inputString.split(stringPattern)));
|
37
|
29
|
}
|
38
|
30
|
|
39
|
31
|
public Item parseStringIntoItem(String rawItem) throws ItemParseException{
|
40
|
|
- if (findName(rawItem) == null || findPrice(rawItem)== null) {
|
|
32
|
+ if (findName(rawItem) == null | findPrice(rawItem)== null) {
|
41
|
33
|
throw new ItemParseException();
|
42
|
34
|
}
|
43
|
35
|
|
|
@@ -50,13 +42,13 @@ public class ItemParser {
|
50
|
42
|
return foundAnItem;
|
51
|
43
|
}
|
52
|
44
|
|
53
|
|
- public String findName(String rawItem) throws ItemParseException {
|
54
|
|
- Pattern patternName = Pattern.compile("(?<=([Nn][Aa][Mm][Ee][^A-Za-z])).*?(?=[^A-Za-z0])"); // matches "nAmE: MiLk"
|
|
45
|
+ public String findName(String rawItem) {
|
|
46
|
+ Pattern patternName = Pattern.compile("(?<=([Nn][Aa][Mm][Ee][^A-Za-z])).*?(?=[^A-Za-z0])"); // matches "MiLk" or "bReaD"
|
55
|
47
|
Matcher matcherName = patternName.matcher(rawItem);
|
56
|
48
|
if (matcherName.find()){
|
57
|
|
- if(!matcherName.group().equals("")){ // if the name does not equal null --> some blank spaces were being counted as names
|
|
49
|
+ if(!matcherName.group().equals("")){ // if the name does not equal null --> bc some blank spaces and semicolons were being counted as names
|
58
|
50
|
String fixedName = matcherName.group().replaceAll("\\d", "o"); // will fix C00kie mistake - once you hit a number replace it with an o
|
59
|
|
- return fixedName.toLowerCase(); // changed to lowercase bc tests requires lowercase
|
|
51
|
+ return fixedName.toLowerCase(); // changed to lowercase bc tests require lowercase
|
60
|
52
|
}
|
61
|
53
|
}
|
62
|
54
|
return null;
|
|
@@ -65,7 +57,7 @@ public class ItemParser {
|
65
|
57
|
//group() --> Matcher method
|
66
|
58
|
//Returns the input subsequent matched by the previous match.
|
67
|
59
|
|
68
|
|
- public String findPrice(String rawItem) throws ItemParseException {
|
|
60
|
+ public String findPrice(String rawItem) {
|
69
|
61
|
Pattern patternPrice = Pattern.compile("\\d(\\.)\\d\\d"); // matches "3.23"
|
70
|
62
|
Matcher matcherPrice = patternPrice.matcher(rawItem);
|
71
|
63
|
if(matcherPrice.find()){
|
|
@@ -76,8 +68,8 @@ public class ItemParser {
|
76
|
68
|
return null;
|
77
|
69
|
}
|
78
|
70
|
|
79
|
|
- public String findExpiration(String rawItem) throws ItemParseException{
|
80
|
|
- Pattern patternExp = Pattern.compile("(?<=([Ee][Xx][Pp][Ii][Rr][Aa][Tt][Ii][Oo][Nn][^A-Za-z]))(.)+[^#]"); // matches "eXpirAtioN: 1/17/2016
|
|
71
|
+ public String findExpiration(String rawItem) {
|
|
72
|
+ Pattern patternExp = Pattern.compile("(?<=([Ee][Xx][Pp][Ii][Rr][Aa][Tt][Ii][Oo][Nn][^A-Za-z]))(.)+[^#]"); // matches "1/17/2016"
|
81
|
73
|
Matcher matcherExp = patternExp.matcher(rawItem);
|
82
|
74
|
if (matcherExp.find()){
|
83
|
75
|
return matcherExp.group();
|
|
@@ -107,7 +99,7 @@ public class ItemParser {
|
107
|
99
|
myItemArrayList.add(newItem); // then we can add our item to our arrayList
|
108
|
100
|
groceryList.put(newItem.getName(), myItemArrayList); // we will add it to our map next
|
109
|
101
|
} else {
|
110
|
|
- groceryList.get(newItem.getName()).add(newItem);
|
|
102
|
+ groceryList.get(newItem.getName()).add(newItem); // if we already have the item, add the value
|
111
|
103
|
}
|
112
|
104
|
} catch (ItemParseException e){
|
113
|
105
|
exceptionCount++;
|
|
@@ -115,4 +107,53 @@ public class ItemParser {
|
115
|
107
|
}
|
116
|
108
|
return groceryList;
|
117
|
109
|
}
|
|
110
|
+
|
|
111
|
+ public String displayGroceryListToString() throws Exception{
|
|
112
|
+ groceryList = getGroceryList();
|
|
113
|
+ StringBuilder displayGroceryList = new StringBuilder();
|
|
114
|
+
|
|
115
|
+ for (Map.Entry<String, ArrayList<Item>> nameAndItem : groceryList.entrySet()){
|
|
116
|
+ String makeUpperCase = nameAndItem.getKey().substring(0,1).toUpperCase() + nameAndItem.getKey().substring(1); // capitalize the first letter of the name ex: M and then add the rest of the work ex: ilk
|
|
117
|
+
|
|
118
|
+ displayGroceryList.append("\n" + "name: " + makeUpperCase + "\t\t\t\t" + "seen: " + nameAndItem.getValue().size() + " times");
|
|
119
|
+ displayGroceryList.append("\n" + "------------------------------------------");
|
|
120
|
+
|
|
121
|
+ ArrayList<Double> getDiffPrices = getDifferentPrices(nameAndItem);
|
|
122
|
+ for (int i = 0; i < getDiffPrices.size(); i++) {
|
|
123
|
+ if (getPriceOccurrences(nameAndItem.getValue(), getDiffPrices.get(i)) == 1) {
|
|
124
|
+ String time = " time";
|
|
125
|
+ } else {
|
|
126
|
+ String time = " times";
|
|
127
|
+ displayGroceryList.append("\n" + "Price: " + getDiffPrices.get(i) + "\t\t\t\t" + " seen: " + getPriceOccurrences(nameAndItem.getValue(), getDiffPrices.get(i)) + " "+time);
|
|
128
|
+ displayGroceryList.append("\n" + "==========================================");
|
|
129
|
+ }
|
|
130
|
+ }
|
|
131
|
+
|
|
132
|
+ }
|
|
133
|
+ displayGroceryList.append("\n\n" + "Errors: " + exceptionCount + " times\n\n");
|
|
134
|
+ displayGroceryList.append("\n" + "------------------------------------------");
|
|
135
|
+ return displayGroceryList.toString();
|
|
136
|
+
|
|
137
|
+ }
|
|
138
|
+
|
|
139
|
+ public Integer getPriceOccurrences(ArrayList<Item> listOfItems, Double price){
|
|
140
|
+ int counter = 0;
|
|
141
|
+
|
|
142
|
+ for (int i = 0; i < listOfItems.size(); i++){
|
|
143
|
+ if (listOfItems.get(i).getPrice().equals(price)) // if our arrayList of items have the value of our price add it to the count
|
|
144
|
+ counter++;
|
|
145
|
+ }
|
|
146
|
+ return counter;
|
|
147
|
+ }
|
|
148
|
+
|
|
149
|
+ public ArrayList<Double> getDifferentPrices(Map.Entry<String, ArrayList<Item>> item){
|
|
150
|
+ ArrayList<Double> diffPrices = new ArrayList<Double>();
|
|
151
|
+
|
|
152
|
+ for (int i = 0; i < item.getValue().size(); i ++){
|
|
153
|
+ if (!diffPrices.contains(item.getValue().get(i).getPrice())){ // get the size of our arrayList of items and if the prices != the other prices add them to our list
|
|
154
|
+ diffPrices.add(item.getValue().get(i).getPrice());
|
|
155
|
+ }
|
|
156
|
+ }
|
|
157
|
+ return diffPrices;
|
|
158
|
+ }
|
118
|
159
|
}
|