|
@@ -2,30 +2,158 @@ package io.zipcoder;
|
2
|
2
|
|
3
|
3
|
import java.util.ArrayList;
|
4
|
4
|
import java.util.Arrays;
|
|
5
|
+import java.util.HashMap;
|
|
6
|
+import java.util.Map;
|
|
7
|
+import java.util.regex.Matcher;
|
|
8
|
+import java.util.regex.Pattern;
|
5
|
9
|
|
6
|
10
|
public class ItemParser {
|
7
|
11
|
|
|
12
|
+ public static int exceptionCount = 0;
|
|
13
|
+ private HashMap<String, ArrayList<Item>> groceryList = new HashMap<String, ArrayList<Item>>();
|
8
|
14
|
|
9
|
|
- public ArrayList<String> parseRawDataIntoStringArray(String rawData){
|
|
15
|
+
|
|
16
|
+ public ArrayList<String> parseRawDataIntoStringArray(String rawData){ // entries split by ## with name,price,type and exp Example: [naMe:Milk;price:3.23;type:Food;expiration:1/25/2016, naME:BreaD;price:1.23;type:Food;expiration:1/02/2016 ... etc.]
|
10
|
17
|
String stringPattern = "##";
|
11
|
18
|
ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawData);
|
12
|
19
|
return response;
|
13
|
20
|
}
|
|
21
|
+ public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){ // entries split by crazy characters Example: [naMe:Milk, price:3.23, type:Food, expiration:1/25/2016] [] [] etc.
|
|
22
|
+ String stringPattern = "[;|^]";
|
|
23
|
+ ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawItem);
|
|
24
|
+ return response;
|
|
25
|
+ }
|
|
26
|
+
|
|
27
|
+ private ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString){ // helper method that splits strings at characters above into ArrayLists
|
|
28
|
+ return new ArrayList<String>(Arrays.asList(inputString.split(stringPattern)));
|
|
29
|
+ }
|
14
|
30
|
|
15
|
31
|
public Item parseStringIntoItem(String rawItem) throws ItemParseException{
|
|
32
|
+ if (findName(rawItem) == null | findPrice(rawItem)== null) {
|
|
33
|
+ throw new ItemParseException();
|
|
34
|
+ }
|
|
35
|
+
|
|
36
|
+ String name = findName(rawItem);
|
|
37
|
+ Double price = Double.parseDouble(findPrice(rawItem));
|
|
38
|
+ String type = findType(rawItem);
|
|
39
|
+ String expDate = findExpiration(rawItem);
|
|
40
|
+
|
|
41
|
+ Item foundAnItem = new Item(name, price, type, expDate);
|
|
42
|
+ return foundAnItem;
|
|
43
|
+ }
|
|
44
|
+
|
|
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"
|
|
47
|
+ Matcher matcherName = patternName.matcher(rawItem);
|
|
48
|
+ if (matcherName.find()){
|
|
49
|
+ if(!matcherName.group().equals("")){ // if the name does not equal null --> bc some blank spaces and semicolons were being counted as names
|
|
50
|
+ String fixedName = matcherName.group().replaceAll("\\d", "o"); // will fix C00kie mistake - once you hit a number replace it with an o
|
|
51
|
+ return fixedName.toLowerCase(); // changed to lowercase bc tests require lowercase
|
|
52
|
+ }
|
|
53
|
+ }
|
16
|
54
|
return null;
|
17
|
55
|
}
|
18
|
56
|
|
19
|
|
- public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){
|
20
|
|
- String stringPattern = "[;|^]";
|
21
|
|
- ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawItem);
|
22
|
|
- return response;
|
|
57
|
+ //group() --> Matcher method
|
|
58
|
+ //Returns the input subsequent matched by the previous match.
|
|
59
|
+
|
|
60
|
+ public String findPrice(String rawItem) {
|
|
61
|
+ Pattern patternPrice = Pattern.compile("\\d(\\.)\\d\\d"); // matches "3.23"
|
|
62
|
+ Matcher matcherPrice = patternPrice.matcher(rawItem);
|
|
63
|
+ if(matcherPrice.find()){
|
|
64
|
+ if (!matcherPrice.group().equals("")){ // some blank prices were getting returned so make sure they're not blank
|
|
65
|
+ return matcherPrice.group();
|
|
66
|
+ }
|
|
67
|
+ }
|
|
68
|
+ return null;
|
23
|
69
|
}
|
24
|
70
|
|
25
|
|
- private ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString){
|
26
|
|
- return new ArrayList<String>(Arrays.asList(inputString.split(stringPattern)));
|
|
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"
|
|
73
|
+ Matcher matcherExp = patternExp.matcher(rawItem);
|
|
74
|
+ if (matcherExp.find()){
|
|
75
|
+ return matcherExp.group();
|
|
76
|
+ }
|
|
77
|
+ else return null;
|
|
78
|
+ }
|
|
79
|
+
|
|
80
|
+ public String findType(String rawItem){
|
|
81
|
+ Pattern patternType = Pattern.compile("(?<=([Tt][Yy][Pp][Ee][^A-Za-z])).*?(?=[^A-Za-z0])"); // matches "type: Food"
|
|
82
|
+ Matcher matcherType = patternType.matcher(rawItem);
|
|
83
|
+ if (matcherType.find()){
|
|
84
|
+ return matcherType.group().toLowerCase();
|
|
85
|
+ }
|
|
86
|
+ else return null;
|
27
|
87
|
}
|
28
|
88
|
|
|
89
|
+ public HashMap<String, ArrayList<Item>> getGroceryList() throws Exception {
|
|
90
|
+ Main main = new Main();
|
|
91
|
+
|
|
92
|
+ ArrayList<String> listOfItems = parseRawDataIntoStringArray(main.readRawDataToString());
|
|
93
|
+
|
|
94
|
+ for(String anItem : listOfItems){
|
|
95
|
+ try {
|
|
96
|
+ Item newItem = parseStringIntoItem(anItem);
|
|
97
|
+ if (!groceryList.containsKey(newItem.getName())){ // if we do not have this key
|
|
98
|
+ ArrayList<Item> myItemArrayList = new ArrayList<Item>(); // we will have to create a new arrayList to hold our items
|
|
99
|
+ myItemArrayList.add(newItem); // then we can add our item to our arrayList
|
|
100
|
+ groceryList.put(newItem.getName(), myItemArrayList); // we will add it to our map next
|
|
101
|
+ } else {
|
|
102
|
+ groceryList.get(newItem.getName()).add(newItem); // if we already have the item, add the value
|
|
103
|
+ }
|
|
104
|
+ } catch (ItemParseException e){
|
|
105
|
+ exceptionCount++;
|
|
106
|
+ }
|
|
107
|
+ }
|
|
108
|
+ return groceryList;
|
|
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" + "------------------------------------------");
|
29
|
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
|
+ }
|
30
|
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
|
+ }
|
31
|
159
|
}
|