|
@@ -27,12 +27,12 @@ public class ItemParser {
|
27
|
27
|
return response;
|
28
|
28
|
}
|
29
|
29
|
|
30
|
|
- //split string with regex
|
|
30
|
+ //general method for splitting arraylists with regex
|
31
|
31
|
private ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString) {
|
32
|
32
|
return new ArrayList<>(Arrays.asList(inputString.split(stringPattern)));
|
33
|
33
|
}
|
34
|
34
|
|
35
|
|
-
|
|
35
|
+ //Splits arraylist that has chunks into the name:cake arraylist which gets used in testing
|
36
|
36
|
public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem) {
|
37
|
37
|
String stringPattern = "[;|^|!|%|*|@]";
|
38
|
38
|
ArrayList<String> response = splitStringWithRegexPattern(stringPattern, rawItem);
|
|
@@ -41,6 +41,19 @@ public class ItemParser {
|
41
|
41
|
|
42
|
42
|
}
|
43
|
43
|
|
|
44
|
+ //takes the raw data arraylist(item chunks) parses them into items and then adds the items to the map
|
|
45
|
+ public void addItemToList(ArrayList<String> itemList){
|
|
46
|
+ for (int i = 0; i < itemList.size(); i++) {
|
|
47
|
+ try{
|
|
48
|
+ Item newItem = (parseStringIntoItem(itemList.get(i)));
|
|
49
|
+ addNewItemToMap(realFoodList, newItem);
|
|
50
|
+ }catch (ItemParseException e){
|
|
51
|
+ continue;
|
|
52
|
+ }
|
|
53
|
+
|
|
54
|
+ }
|
|
55
|
+ }
|
|
56
|
+
|
44
|
57
|
public void addNewItemToMap(Map<String, ArrayList<Item>> map, Item newItem) {
|
45
|
58
|
if (!map.keySet().contains(newItem.getName())) {
|
46
|
59
|
map.put(newItem.getName(), new ArrayList<>());
|
|
@@ -50,17 +63,7 @@ public class ItemParser {
|
50
|
63
|
}
|
51
|
64
|
}
|
52
|
65
|
|
53
|
|
- public void addItemToList(ArrayList<String> itemList) {
|
54
|
|
- try {
|
55
|
|
- for (int i = 0; i < itemList.size(); i++) {
|
56
|
|
- Item newItem = (parseStringIntoItem(itemList.get(i)));
|
57
|
|
- addNewItemToMap(realFoodList, newItem);
|
58
|
|
- }
|
59
|
|
- } catch (ItemParseException e) {
|
60
|
|
- new ItemParseException();
|
61
|
|
- }
|
62
|
|
- }
|
63
|
|
-
|
|
66
|
+ //gets all the prices for the items
|
64
|
67
|
public ArrayList<Double> getPrices(Map.Entry<String, ArrayList<Item>> itemMap) {
|
65
|
68
|
ArrayList<Double> prices = new ArrayList<>();
|
66
|
69
|
for (int i = 0; i < itemMap.getValue().size(); i++) {
|
|
@@ -81,33 +84,39 @@ public class ItemParser {
|
81
|
84
|
return counter;
|
82
|
85
|
}
|
83
|
86
|
|
84
|
|
-
|
|
87
|
+ //Create new item
|
85
|
88
|
public Item parseStringIntoItem(String rawItem) throws ItemParseException {
|
86
|
89
|
String name = findName(rawItem);
|
87
|
90
|
Double price = Double.valueOf(findPrice(rawItem));
|
88
|
91
|
String type = findType(rawItem);
|
89
|
92
|
String expiration = findExpiration(rawItem);
|
|
93
|
+
|
90
|
94
|
return new Item(name, price, type, expiration);
|
91
|
95
|
}
|
92
|
96
|
|
93
|
|
-
|
94
|
|
- public String fixCookie(String input) {
|
95
|
|
- String regexCookie = "(C|c).....(S|s)";
|
96
|
|
- Pattern pattern = Pattern.compile(regexCookie);
|
|
97
|
+ //Cookie has a 0 in it so we fix it before parsing into the item
|
|
98
|
+ public String fixCo0kie(String input) {
|
|
99
|
+ String regexCo0kie = "[0]";
|
|
100
|
+ Pattern pattern = Pattern.compile(regexCo0kie);
|
97
|
101
|
Matcher matcher = pattern.matcher(input);
|
98
|
|
- return matcher.replaceAll("cookies");
|
|
102
|
+ return matcher.replaceAll("o");
|
99
|
103
|
}
|
100
|
104
|
|
|
105
|
+ //We find the item name by searching for the name:item pair then return the group that has the item
|
101
|
106
|
public String findName(String name) throws ItemParseException {
|
102
|
|
- String ifCookieFix = fixCookie(name);
|
103
|
107
|
String regexName = "([Nn]..[Ee]:)(\\w+)";
|
104
|
108
|
Pattern pattern = Pattern.compile(regexName);
|
105
|
109
|
Matcher matcher = pattern.matcher(name);
|
106
|
110
|
if (matcher.find()) {
|
107
|
|
- return matcher.group(2).toLowerCase();
|
|
111
|
+ if(matcher.group(2).contains("0")) {
|
|
112
|
+ return fixCo0kie(matcher.group(2).toLowerCase());
|
|
113
|
+ }else {
|
|
114
|
+ return matcher.group(2).toLowerCase();
|
|
115
|
+ }
|
108
|
116
|
} else {
|
109
|
117
|
throw new ItemParseException();
|
110
|
118
|
}
|
|
119
|
+
|
111
|
120
|
}
|
112
|
121
|
|
113
|
122
|
public String findPrice(String price) throws ItemParseException {
|
|
@@ -122,6 +131,7 @@ public class ItemParser {
|
122
|
131
|
}
|
123
|
132
|
}
|
124
|
133
|
|
|
134
|
+
|
125
|
135
|
public String findType(String type) throws ItemParseException {
|
126
|
136
|
String regexType = "([Tt]..[Ee]:)(\\w+)";
|
127
|
137
|
Pattern pattern = Pattern.compile(regexType);
|
|
@@ -134,6 +144,7 @@ public class ItemParser {
|
134
|
144
|
}
|
135
|
145
|
}
|
136
|
146
|
|
|
147
|
+
|
137
|
148
|
public String findExpiration(String expiration) throws ItemParseException {
|
138
|
149
|
String regexExpiration = "([E|e]........[N|n]:)(\\d\\/\\d{2}\\/\\d{4})";
|
139
|
150
|
Pattern pattern = Pattern.compile(regexExpiration);
|
|
@@ -145,7 +156,6 @@ public class ItemParser {
|
145
|
156
|
}
|
146
|
157
|
}
|
147
|
158
|
|
148
|
|
-
|
149
|
159
|
public String printParsedJerkSON() {
|
150
|
160
|
StringBuilder print = new StringBuilder();
|
151
|
161
|
for (Map.Entry<String, ArrayList<Item>> entry : realFoodList.entrySet()) {
|