|
@@ -7,6 +7,7 @@ import java.util.regex.Pattern;
|
7
|
7
|
public class ItemParser {
|
8
|
8
|
|
9
|
9
|
private Integer countExceptionsThrown = 0;
|
|
10
|
+ private Integer count = 0;
|
10
|
11
|
|
11
|
12
|
private ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString) {
|
12
|
13
|
return new ArrayList<String>(Arrays.asList(inputString.split(stringPattern)));
|
|
@@ -25,20 +26,29 @@ public class ItemParser {
|
25
|
26
|
}
|
26
|
27
|
|
27
|
28
|
public Item parseStringIntoItem(String rawItem) throws ItemParseException {
|
28
|
|
- String name = checkName(rawItem);
|
29
|
|
- Double price = Double.valueOf(checkPrice(rawItem));
|
30
|
|
- String type = checkType(rawItem);
|
31
|
|
- String expiration = checkExpiration(rawItem);
|
|
29
|
+ if (checkName(rawItem) == null || checkPrice(rawItem) == null || checkType(rawItem) == null
|
|
30
|
+ || checkExpiration(rawItem) == null) {
|
|
31
|
+ throw new ItemParseException();
|
|
32
|
+ }
|
|
33
|
+
|
|
34
|
+ String name = checkName(rawItem);
|
|
35
|
+ Double price = Double.valueOf(checkPrice(rawItem));
|
|
36
|
+ String type = checkType(rawItem);
|
|
37
|
+ String expiration = checkExpiration(rawItem);
|
32
|
38
|
|
33
|
|
- return new Item(name, price, type, expiration);
|
|
39
|
+ return new Item(name, price, type, expiration);
|
34
|
40
|
}
|
35
|
41
|
|
36
|
|
- public ArrayList<Item> createItemArrayList(String rawData) throws ItemParseException{
|
|
42
|
+ public ArrayList<Item> createItemArrayList(String rawData) {
|
37
|
43
|
ArrayList<String> temp = parseRawDataIntoStringArray(rawData);
|
38
|
44
|
ArrayList<Item> itemArrayList = new ArrayList<Item>();
|
39
|
45
|
|
40
|
46
|
for (int i = 0; i <temp.size() ; i++) {
|
41
|
|
- itemArrayList.add(parseStringIntoItem(temp.get(i)));
|
|
47
|
+ try {
|
|
48
|
+ itemArrayList.add(parseStringIntoItem(temp.get(i)));
|
|
49
|
+ } catch (ItemParseException e) {
|
|
50
|
+ count++;
|
|
51
|
+ }
|
42
|
52
|
}
|
43
|
53
|
return itemArrayList;
|
44
|
54
|
}
|
|
@@ -66,20 +76,20 @@ public class ItemParser {
|
66
|
76
|
return priceTotals;
|
67
|
77
|
}
|
68
|
78
|
|
69
|
|
- public Map<Double, Integer> itemTypeMapWithCounts(String rawData, String filterType) throws ItemParseException {
|
|
79
|
+ public Map<Double, Integer> itemTypeMapWithCounts(String rawData, String filterType) {
|
70
|
80
|
ArrayList<Item> itemArrayList = createItemArrayList(rawData);
|
71
|
81
|
ArrayList<Item> filterItemArrayList = filterItemArrayList(itemArrayList, filterType);
|
72
|
82
|
Map<Double, Integer> priceTotals = individualItemCount(filterItemArrayList);
|
73
|
83
|
return priceTotals;
|
74
|
84
|
}
|
75
|
85
|
|
76
|
|
- public Integer totalTimesItemSeen(String rawData, String filterType) throws ItemParseException {
|
|
86
|
+ public Integer totalTimesItemSeen(String rawData, String filterType) {
|
77
|
87
|
ArrayList<Item> itemArrayList = createItemArrayList(rawData);
|
78
|
88
|
ArrayList<Item> filterItemArrayList = filterItemArrayList(itemArrayList, filterType);
|
79
|
89
|
return filterItemArrayList.size();
|
80
|
90
|
}
|
81
|
91
|
|
82
|
|
- public ArrayList<String> filterTypeArrayList(String rawData) throws ItemParseException {
|
|
92
|
+ public ArrayList<String> filterTypeArrayList(String rawData) {
|
83
|
93
|
ArrayList<Item> itemArrayList = createItemArrayList(rawData);
|
84
|
94
|
ArrayList<String> filterType = new ArrayList<String>();
|
85
|
95
|
|
|
@@ -112,7 +122,7 @@ public class ItemParser {
|
112
|
122
|
return sb.toString();
|
113
|
123
|
}
|
114
|
124
|
|
115
|
|
- public String formatPriceField(String rawData, String filterType) throws ItemParseException {
|
|
125
|
+ public String formatPriceField(String rawData, String filterType) {
|
116
|
126
|
Map<Double, Integer> priceTotals = itemTypeMapWithCounts(rawData, filterType);
|
117
|
127
|
StringBuilder sb = new StringBuilder();
|
118
|
128
|
Set mapSet = (Set) priceTotals.entrySet();
|
|
@@ -123,20 +133,27 @@ public class ItemParser {
|
123
|
133
|
Object keyValue = mapEntry.getKey();
|
124
|
134
|
//getValue method returns corresponding key's value
|
125
|
135
|
Object value = mapEntry.getValue();
|
126
|
|
- sb.append("Price: " + keyValue + "\t\t seen: " + value + " times" + "\n");
|
|
136
|
+ String time;
|
|
137
|
+ if(value.equals(1)){
|
|
138
|
+ time = " time";
|
|
139
|
+ }
|
|
140
|
+ else{
|
|
141
|
+ time = " times";
|
|
142
|
+ }
|
|
143
|
+ sb.append("Price: " + keyValue + "\t\t seen: " + value + time + "\n");
|
127
|
144
|
sb.append("-------------\t\t -------------\n");
|
128
|
145
|
}
|
129
|
146
|
return sb.toString();
|
130
|
147
|
}
|
131
|
148
|
|
132
|
|
- public String checkName(String input) throws ItemParseException {
|
|
149
|
+ public String checkName(String input) {
|
133
|
150
|
String newInput = fixCookie(input);
|
134
|
151
|
Pattern patternName = Pattern.compile("([Nn]..[Ee]:)(\\w+)");
|
135
|
152
|
Matcher matcherName= patternName.matcher(newInput);
|
136
|
153
|
|
137
|
154
|
if (matcherName.find())
|
138
|
155
|
return matcherName.group(2).toLowerCase();
|
139
|
|
- else throw new ItemParseException();
|
|
156
|
+ else return null;
|
140
|
157
|
}
|
141
|
158
|
|
142
|
159
|
public String fixCookie(String input){
|
|
@@ -145,31 +162,31 @@ public class ItemParser {
|
145
|
162
|
return matcherCookie.replaceAll("cookies");
|
146
|
163
|
}
|
147
|
164
|
|
148
|
|
- public String checkPrice(String input) throws ItemParseException{
|
|
165
|
+ public String checkPrice(String input) {
|
149
|
166
|
Pattern patternPrice = Pattern.compile("([Pp]...[Ee]:)(\\d\\.\\d{2})");
|
150
|
167
|
Matcher matcherPrice= patternPrice.matcher(input);
|
151
|
168
|
|
152
|
169
|
if (matcherPrice.find())
|
153
|
170
|
return matcherPrice.group(2);
|
154
|
|
- else throw new ItemParseException();
|
|
171
|
+ else return null;
|
155
|
172
|
}
|
156
|
173
|
|
157
|
|
- public String checkType(String input) throws ItemParseException{
|
|
174
|
+ public String checkType(String input) {
|
158
|
175
|
Pattern patternType = Pattern.compile("([Tt]..[Ee]:)(\\w+)");
|
159
|
176
|
Matcher matcherType = patternType.matcher(input);
|
160
|
177
|
|
161
|
178
|
if (matcherType.find())
|
162
|
179
|
return matcherType.group(2).toLowerCase();
|
163
|
|
- else throw new ItemParseException();
|
|
180
|
+ else return null;
|
164
|
181
|
}
|
165
|
182
|
|
166
|
|
- public String checkExpiration(String input) throws ItemParseException{
|
|
183
|
+ public String checkExpiration(String input) {
|
167
|
184
|
Pattern patternExpiration = Pattern.compile("([Ee]........[Nn]:)(\\d\\/\\d{2}\\/\\d{4})");
|
168
|
185
|
Matcher matcherExpiration = patternExpiration.matcher(input);
|
169
|
186
|
|
170
|
187
|
if (matcherExpiration.find())
|
171
|
188
|
return matcherExpiration.group(2);
|
172
|
|
- else throw new ItemParseException();
|
|
189
|
+ else return null;
|
173
|
190
|
}
|
174
|
191
|
|
175
|
192
|
public Integer getExceptionsThrown(String rawData) {
|