|
@@ -10,6 +10,14 @@ import java.util.regex.Pattern;
|
10
|
10
|
|
11
|
11
|
public class ItemParser {
|
12
|
12
|
|
|
13
|
+ private ErrorLogger errorLogger;
|
|
14
|
+
|
|
15
|
+ public ItemParser(ErrorLogger errorLogger) {
|
|
16
|
+ this.errorLogger = errorLogger;
|
|
17
|
+ }
|
|
18
|
+ public ItemParser(){
|
|
19
|
+
|
|
20
|
+ }
|
13
|
21
|
|
14
|
22
|
private ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString) {
|
15
|
23
|
return new ArrayList<String>(Arrays.asList(inputString.split(stringPattern)));
|
|
@@ -42,95 +50,23 @@ public class ItemParser {
|
42
|
50
|
ArrayList<String> temp = parseRawDataIntoStringArray(rawData);
|
43
|
51
|
ArrayList<Item> itemArrayList = new ArrayList<Item>();
|
44
|
52
|
for (int i = 0; i < temp.size(); i++) {
|
45
|
|
- itemArrayList.add(parseStringIntoItem(temp.get(i)));
|
46
|
|
- }
|
47
|
|
- return itemArrayList;
|
48
|
|
- }
|
|
53
|
+ Item item;
|
|
54
|
+ try {
|
|
55
|
+ item = parseStringIntoItem(temp.get(i));
|
|
56
|
+ itemArrayList.add(item);
|
49
|
57
|
|
50
|
|
-
|
51
|
|
- public HashMap<String, Integer> findBasedOnName(String rawData, String whatToSearch) throws ItemParseException {
|
52
|
|
- HashMap<String, Integer> hashMap = new HashMap<String, Integer>();
|
53
|
|
- ArrayList<Item> tempItems = findRawItemToArrayList(rawData);
|
54
|
|
- for (int i = 0; i < tempItems.size(); i++) {
|
55
|
|
- if (tempItems.get(i).getName().equalsIgnoreCase(whatToSearch)) {
|
56
|
|
- incrementValue(hashMap, String.valueOf(tempItems.get(i).getPrice()));
|
|
58
|
+ } catch (ItemParseException e) {
|
|
59
|
+ errorLogger.incrementError();
|
57
|
60
|
}
|
58
|
|
- }
|
59
|
|
- return hashMap;
|
60
|
|
- }
|
61
|
|
-
|
62
|
|
- public void incrementValue(Map<String, Integer> map, String key) {
|
63
|
|
- Integer count = map.get(key);
|
64
|
|
- if (count == null) {
|
65
|
|
- count = 0;
|
66
|
|
- }
|
67
|
|
- map.put(key, count + 1);
|
68
|
|
- }
|
69
|
61
|
|
70
|
|
- public Integer getTotalCount (String rawData, String whatToSearch) throws ItemParseException {
|
71
|
|
- HashMap<String, Integer> hashMap = findBasedOnName(rawData,whatToSearch);
|
72
|
|
- Integer totalCount = 0;
|
73
|
|
- for (Map.Entry<String, Integer> entry: hashMap.entrySet()){
|
74
|
|
- totalCount += entry.getValue();
|
75
|
|
- }
|
76
|
|
- return totalCount;
|
77
|
|
-
|
78
|
|
- }
|
79
|
|
-
|
80
|
|
- public String printOut (String rawData) throws ItemParseException {
|
81
|
|
- ArrayList<Item> tempItems = findRawItemToArrayList(rawData);
|
82
|
|
- HashMap<String, Integer> milk = findBasedOnName(rawData, "milk");
|
83
|
|
- HashMap<String, Integer> bread = findBasedOnName(rawData, "bread");
|
84
|
|
- HashMap<String, Integer> cookies = findBasedOnName(rawData, "cookies");
|
85
|
|
- HashMap<String, Integer> apples = findBasedOnName(rawData, "apples");
|
86
|
|
-
|
87
|
|
- StringBuilder sb = new StringBuilder();
|
88
|
|
- sb.append("name:\tMilk\t\tseen: "+ )
|
89
|
|
-//
|
90
|
|
-// name: Milk seen: 6 times
|
91
|
|
-// ============= =============
|
92
|
|
-// Price: 3.23 seen: 5 times
|
93
|
|
-// ------------- -------------
|
94
|
|
-// Price: 1.23 seen: 1 time
|
95
|
|
-//
|
96
|
|
-// name: Bread seen: 6 times
|
97
|
|
-// ============= =============
|
98
|
|
-// Price: 1.23 seen: 6 times
|
99
|
|
-// ------------- -------------
|
100
|
|
-//
|
101
|
|
-// name: Cookies seen: 8 times
|
102
|
|
-// ============= =============
|
103
|
|
-// Price: 2.25 seen: 8 times
|
104
|
|
-// ------------- -------------
|
105
|
|
-//
|
106
|
|
-// name: Apples seen: 4 times
|
107
|
|
-// ============= =============
|
108
|
|
-// Price: 0.25 seen: 2 times
|
109
|
|
-// ------------- -------------
|
110
|
|
-// Price: 0.23 seen: 2 times
|
111
|
|
-//
|
112
|
|
-// Errors seen: 4 times
|
113
|
|
-
|
114
|
|
-
|
115
|
|
- return rawData;
|
116
|
|
- }
|
117
|
62
|
|
118
|
63
|
|
119
|
|
- public static void main(String[] args) throws ItemParseException {
|
120
|
|
- String rawMultipleItems = "naMe:Milk;price:3.23;type:Food;expiration:1/25/2016##"
|
121
|
|
- + "naME:BreaD;price:1.23;type:Food;expiration:1/02/2016##"
|
122
|
|
- + "NAMe:BrEAD;price:1.23;type:Food;expiration:2/25/2016##"
|
123
|
|
- + "NAMe:BrEAD;price:1.25;type:Food;expiration:2/25/2016##"
|
124
|
|
- + "NAMe:chocolate;price:1.25;type:Food;expiration:2/25/2016##";
|
125
|
|
- ;
|
126
|
|
- ItemParser itemParser = new ItemParser();
|
127
|
|
- System.out.println(itemParser.findBasedOnName(rawMultipleItems, "bread"));
|
128
|
|
- System.out.println(itemParser.getTotalCount(rawMultipleItems, "bread"));
|
129
|
64
|
|
|
65
|
+ }
|
|
66
|
+ return itemArrayList;
|
130
|
67
|
}
|
131
|
68
|
|
132
|
69
|
|
133
|
|
-
|
134
|
70
|
public String fixCookie(String input) {
|
135
|
71
|
Pattern patternCookies = Pattern.compile("[Cc][Oo0][Oo0][Kk][Ii][Ee][Ss]");
|
136
|
72
|
Matcher matcherCookies = patternCookies.matcher(input);
|
|
@@ -144,35 +80,105 @@ public class ItemParser {
|
144
|
80
|
|
145
|
81
|
if (matcherName.find())
|
146
|
82
|
return matcherName.group(2).toLowerCase();
|
147
|
|
- else throw new ItemParseException();
|
148
|
|
- }
|
|
83
|
+ else {
|
|
84
|
+ throw new ItemParseException();
|
|
85
|
+ } }
|
149
|
86
|
|
150
|
87
|
public String checkPrice(String input) throws ItemParseException {
|
151
|
88
|
Pattern patternPrice = Pattern.compile("([Pp]...[Ee]:)(\\d\\.\\d{2})");
|
152
|
89
|
Matcher matcherPrice = patternPrice.matcher(input);
|
153
|
|
-
|
154
|
|
- if (matcherPrice.find())
|
|
90
|
+ if (matcherPrice.find()){
|
155
|
91
|
return matcherPrice.group(2).toLowerCase();
|
156
|
|
- else throw new ItemParseException();
|
|
92
|
+ }
|
|
93
|
+ else {
|
|
94
|
+ throw new ItemParseException();
|
|
95
|
+ }
|
157
|
96
|
}
|
158
|
97
|
|
159
|
98
|
public String checkType(String input) throws ItemParseException {
|
160
|
99
|
Pattern patternType = Pattern.compile("([Tt]..[Ee]:)(\\w+)");
|
161
|
100
|
Matcher matcherType = patternType.matcher(input);
|
162
|
101
|
|
163
|
|
- if (matcherType.find())
|
|
102
|
+ if (matcherType.find()) {
|
164
|
103
|
return matcherType.group(2).toLowerCase();
|
165
|
|
- else throw new ItemParseException();
|
|
104
|
+ }
|
|
105
|
+ else {
|
|
106
|
+ throw new ItemParseException();
|
|
107
|
+ }
|
166
|
108
|
}
|
167
|
109
|
|
168
|
110
|
public String checkExpiration(String input) throws ItemParseException {
|
169
|
111
|
Pattern patternExpiration = Pattern.compile("([Ee]........[Nn]:)(\\d\\/\\d{2}\\/\\d{4})");
|
170
|
112
|
Matcher matcherExpiration = patternExpiration.matcher(input);
|
171
|
113
|
|
172
|
|
- if (matcherExpiration.find())
|
|
114
|
+ if (matcherExpiration.find()) {
|
173
|
115
|
return matcherExpiration.group(2).toLowerCase();
|
174
|
|
- else throw new ItemParseException();
|
|
116
|
+ }
|
|
117
|
+ else {
|
|
118
|
+ throw new ItemParseException();
|
|
119
|
+ }
|
175
|
120
|
}
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+// public HashMap<String, Integer> findBasedOnName(String rawData, String whatToSearch) throws ItemParseException {
|
|
124
|
+// HashMap<String, Integer> hashMap = new HashMap<String, Integer>();
|
|
125
|
+// ArrayList<Item> tempItems = findRawItemToArrayList(rawData);
|
|
126
|
+// for (int i = 0; i < tempItems.size(); i++) {
|
|
127
|
+// if (tempItems.get(i).getName().equalsIgnoreCase(whatToSearch)) {
|
|
128
|
+// incrementValue(hashMap, String.valueOf(tempItems.get(i).getPrice()));
|
|
129
|
+// }
|
|
130
|
+// }
|
|
131
|
+// return hashMap;
|
|
132
|
+// }
|
|
133
|
+//
|
|
134
|
+// public void incrementValue(Map<String, Integer> map, String key) {
|
|
135
|
+// Integer count = map.get(key);
|
|
136
|
+// if (count == null) {
|
|
137
|
+// count = 0;
|
|
138
|
+// }
|
|
139
|
+// map.put(key, count + 1);
|
|
140
|
+// }
|
|
141
|
+//
|
|
142
|
+// public Integer getTotalCount (String rawData, String whatToSearch) throws ItemParseException {
|
|
143
|
+// HashMap<String, Integer> hashMap = findBasedOnName(rawData,whatToSearch);
|
|
144
|
+// Integer totalCount = 0;
|
|
145
|
+// for (Map.Entry<String, Integer> entry: hashMap.entrySet()){
|
|
146
|
+// totalCount += entry.getValue();
|
|
147
|
+// }
|
|
148
|
+// return totalCount;
|
|
149
|
+//
|
|
150
|
+// }
|
|
151
|
+//
|
|
152
|
+// public String printOut (String rawData) throws ItemParseException {
|
|
153
|
+// ArrayList<Item> tempItems = findRawItemToArrayList(rawData);
|
|
154
|
+// HashMap<String, Integer> milk = findBasedOnName(rawData, "milk");
|
|
155
|
+// HashMap<String, Integer> bread = findBasedOnName(rawData, "bread");
|
|
156
|
+// HashMap<String, Integer> cookies = findBasedOnName(rawData, "cookies");
|
|
157
|
+// HashMap<String, Integer> apples = findBasedOnName(rawData, "apples");
|
|
158
|
+//
|
|
159
|
+// StringBuilder sb = new StringBuilder();
|
|
160
|
+// sb.append("name:\tMilk\t\tseen: "+ 1(rawData, "milk") + " times\n")
|
|
161
|
+//
|
|
162
|
+// return sb.toString();
|
|
163
|
+// }
|
|
164
|
+//
|
|
165
|
+//
|
|
166
|
+// public static void main(String[] args) throws ItemParseException {
|
|
167
|
+// String rawMultipleItems = "naMe:Milk;price:3.23;type:Food;expiration:1/25/2016##"
|
|
168
|
+// + "naME:BreaD;price:1.23;type:Food;expiration:1/02/2016##"
|
|
169
|
+// + "NAMe:BrEAD;price:1.23;type:Food;expiration:2/25/2016##"
|
|
170
|
+// + "NAMe:BrEAD;price:1.25;type:Food;expiration:2/25/2016##"
|
|
171
|
+// + "NAMe:chocolate;price:1.25;type:Food;expiration:2/25/2016##";
|
|
172
|
+// ;
|
|
173
|
+// ItemParser itemParser = new ItemParser();
|
|
174
|
+// System.out.println(itemParser.findBasedOnName(rawMultipleItems, "bread"));
|
|
175
|
+// System.out.println(itemParser.getTotalCount(rawMultipleItems, "bread"));
|
|
176
|
+//
|
|
177
|
+// }
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
|
176
|
182
|
}
|
177
|
183
|
|
178
|
184
|
|