|
@@ -1,31 +1,178 @@
|
1
|
1
|
package io.zipcoder;
|
2
|
2
|
|
3
|
|
-import java.util.ArrayList;
|
4
|
|
-import java.util.Arrays;
|
|
3
|
+import com.sun.org.apache.xerces.internal.impl.xpath.regex.Match;
|
|
4
|
+import com.sun.xml.internal.bind.v2.runtime.reflect.Lister;
|
|
5
|
+
|
|
6
|
+import java.lang.reflect.Array;
|
|
7
|
+import java.util.*;
|
|
8
|
+import java.util.regex.Matcher;
|
|
9
|
+import java.util.regex.Pattern;
|
5
|
10
|
|
6
|
11
|
public class ItemParser {
|
7
|
12
|
|
8
|
13
|
|
9
|
|
- public ArrayList<String> parseRawDataIntoStringArray(String rawData){
|
|
14
|
+ private ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString) {
|
|
15
|
+ return new ArrayList<String>(Arrays.asList(inputString.split(stringPattern)));
|
|
16
|
+ }
|
|
17
|
+
|
|
18
|
+ public ArrayList<String> parseRawDataIntoStringArray(String rawData) {
|
10
|
19
|
String stringPattern = "##";
|
11
|
|
- ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawData);
|
|
20
|
+ ArrayList<String> response = splitStringWithRegexPattern(stringPattern, rawData);
|
12
|
21
|
return response;
|
13
|
22
|
}
|
14
|
23
|
|
15
|
|
- public Item parseStringIntoItem(String rawItem) throws ItemParseException{
|
16
|
|
- return null;
|
|
24
|
+ public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem) {
|
|
25
|
+ String stringPattern = "[@|^|*|%|!|;]";
|
|
26
|
+ ArrayList<String> response = splitStringWithRegexPattern(stringPattern, rawItem);
|
|
27
|
+ return response;
|
17
|
28
|
}
|
18
|
29
|
|
19
|
|
- public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){
|
20
|
|
- String stringPattern = "[;|^]";
|
21
|
|
- ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawItem);
|
22
|
|
- return response;
|
|
30
|
+ public Item parseStringIntoItem(String rawItem) throws ItemParseException {
|
|
31
|
+
|
|
32
|
+ String name = checkName(rawItem);
|
|
33
|
+ Double price = Double.valueOf(checkPrice(rawItem));
|
|
34
|
+ String type = checkType(rawItem);
|
|
35
|
+ String expiration = checkExpiration(rawItem);
|
|
36
|
+
|
|
37
|
+ return new Item(name, price, type, expiration);
|
|
38
|
+
|
23
|
39
|
}
|
24
|
40
|
|
25
|
|
- private ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString){
|
26
|
|
- return new ArrayList<String>(Arrays.asList(inputString.split(stringPattern)));
|
|
41
|
+ public ArrayList<Item> findRawItemToArrayList(String rawData) throws ItemParseException {
|
|
42
|
+ ArrayList<String> temp = parseRawDataIntoStringArray(rawData);
|
|
43
|
+ ArrayList<Item> itemArrayList = new ArrayList<Item>();
|
|
44
|
+ for (int i = 0; i < temp.size(); i++) {
|
|
45
|
+ itemArrayList.add(parseStringIntoItem(temp.get(i)));
|
|
46
|
+ }
|
|
47
|
+ return itemArrayList;
|
|
48
|
+ }
|
|
49
|
+
|
|
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()));
|
|
57
|
+ }
|
|
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
|
+
|
|
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
|
+
|
|
118
|
+
|
|
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
|
+
|
27
|
130
|
}
|
28
|
131
|
|
29
|
132
|
|
30
|
133
|
|
|
134
|
+ public String fixCookie(String input) {
|
|
135
|
+ Pattern patternCookies = Pattern.compile("[Cc][Oo0][Oo0][Kk][Ii][Ee][Ss]");
|
|
136
|
+ Matcher matcherCookies = patternCookies.matcher(input);
|
|
137
|
+ return matcherCookies.replaceAll("cookies");
|
|
138
|
+ }
|
|
139
|
+
|
|
140
|
+ public String checkName(String input) throws ItemParseException {
|
|
141
|
+ String newInput = fixCookie(input);
|
|
142
|
+ Pattern patternName = Pattern.compile("([Nn]..[Ee]:)(\\w+)");
|
|
143
|
+ Matcher matcherName = patternName.matcher(newInput);
|
|
144
|
+
|
|
145
|
+ if (matcherName.find())
|
|
146
|
+ return matcherName.group(2).toLowerCase();
|
|
147
|
+ else throw new ItemParseException();
|
|
148
|
+ }
|
|
149
|
+
|
|
150
|
+ public String checkPrice(String input) throws ItemParseException {
|
|
151
|
+ Pattern patternPrice = Pattern.compile("([Pp]...[Ee]:)(\\d\\.\\d{2})");
|
|
152
|
+ Matcher matcherPrice = patternPrice.matcher(input);
|
|
153
|
+
|
|
154
|
+ if (matcherPrice.find())
|
|
155
|
+ return matcherPrice.group(2).toLowerCase();
|
|
156
|
+ else throw new ItemParseException();
|
|
157
|
+ }
|
|
158
|
+
|
|
159
|
+ public String checkType(String input) throws ItemParseException {
|
|
160
|
+ Pattern patternType = Pattern.compile("([Tt]..[Ee]:)(\\w+)");
|
|
161
|
+ Matcher matcherType = patternType.matcher(input);
|
|
162
|
+
|
|
163
|
+ if (matcherType.find())
|
|
164
|
+ return matcherType.group(2).toLowerCase();
|
|
165
|
+ else throw new ItemParseException();
|
|
166
|
+ }
|
|
167
|
+
|
|
168
|
+ public String checkExpiration(String input) throws ItemParseException {
|
|
169
|
+ Pattern patternExpiration = Pattern.compile("([Ee]........[Nn]:)(\\d\\/\\d{2}\\/\\d{4})");
|
|
170
|
+ Matcher matcherExpiration = patternExpiration.matcher(input);
|
|
171
|
+
|
|
172
|
+ if (matcherExpiration.find())
|
|
173
|
+ return matcherExpiration.group(2).toLowerCase();
|
|
174
|
+ else throw new ItemParseException();
|
|
175
|
+ }
|
31
|
176
|
}
|
|
177
|
+
|
|
178
|
+
|