|
@@ -2,30 +2,205 @@ 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
|
+ private Pattern pattern;
|
|
13
|
+
|
|
14
|
+ private Matcher matcher;
|
|
15
|
+
|
|
16
|
+ public int counter = 0;
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+ private Map<String ,ArrayList<Item>> groceryListMap = new HashMap<String, ArrayList<Item>>();
|
|
21
|
+ // Splits string based on ##
|
8
|
22
|
|
9
|
23
|
public ArrayList<String> parseRawDataIntoStringArray(String rawData){
|
10
|
24
|
String stringPattern = "##";
|
11
|
25
|
ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawData);
|
12
|
26
|
return response;
|
13
|
27
|
}
|
|
28
|
+ public Item parseStringIntoItem(String rawItem) throws ItemParseException {
|
|
29
|
+ if (findName(rawItem) == null || findPrice(rawItem) == null) {
|
|
30
|
+ throw new ItemParseException();
|
14
|
31
|
|
15
|
|
- public Item parseStringIntoItem(String rawItem) throws ItemParseException{
|
16
|
|
- return null;
|
|
32
|
+ }
|
|
33
|
+ String Name = findName(rawItem);
|
|
34
|
+ Double Price = Double.parseDouble(findPrice(rawItem));
|
|
35
|
+ String Type = findType(rawItem);
|
|
36
|
+ String Expiration = findExpiration(rawItem);
|
|
37
|
+
|
|
38
|
+ return new Item(Name, Price, Type, Expiration);
|
17
|
39
|
}
|
18
|
40
|
|
19
|
41
|
public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){
|
20
|
|
- String stringPattern = "[;|^]";
|
|
42
|
+ String stringPattern = "[^|;]";
|
21
|
43
|
ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawItem);
|
22
|
44
|
return response;
|
23
|
45
|
}
|
24
|
|
-
|
25
|
|
- private ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString){
|
|
46
|
+ private ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString) {
|
26
|
47
|
return new ArrayList<String>(Arrays.asList(inputString.split(stringPattern)));
|
27
|
48
|
}
|
28
|
49
|
|
29
|
50
|
|
30
|
51
|
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+ public String findName(String rawItem){
|
|
56
|
+ String search = "(?<=([Nn][Aa][Mm][Ee][^A-Za-z])).*?(?=[^A-Za-z0])";
|
|
57
|
+ pattern = Pattern.compile(search);
|
|
58
|
+ matcher = pattern.matcher(rawItem);
|
|
59
|
+
|
|
60
|
+ if (matcher.find()){
|
|
61
|
+ if (!matcher.group().equals("")){
|
|
62
|
+ String name = matcher.group().replaceAll("\\d","o");
|
|
63
|
+ return name.toLowerCase();
|
|
64
|
+ }
|
|
65
|
+ }
|
|
66
|
+ return null;
|
|
67
|
+ }
|
|
68
|
+ public String findPrice(String rawItem){
|
|
69
|
+ pattern =Pattern.compile("(?<=([Pp][Rr][Ii][Cc][Ee][^A-Za-z])).*?(?=[^0-9.])");
|
|
70
|
+ matcher = pattern.matcher(rawItem);
|
|
71
|
+
|
|
72
|
+ if (matcher.find()){
|
|
73
|
+ if (!matcher.group().equals("")){
|
|
74
|
+ return matcher.group();
|
|
75
|
+ }
|
|
76
|
+ }
|
|
77
|
+ return null;
|
|
78
|
+ }
|
|
79
|
+ public String findType(String rawItem){
|
|
80
|
+ Pattern pattern =Pattern.compile("(?<=([Tt][Yy][Pp][Ee][^A-Za-z])).*?(?=[^A-Za-z0])");
|
|
81
|
+ Matcher regMatcher =pattern.matcher(rawItem);
|
|
82
|
+
|
|
83
|
+ if (regMatcher.find()){
|
|
84
|
+ return (regMatcher).group().toLowerCase();
|
|
85
|
+
|
|
86
|
+ } else return null;
|
|
87
|
+ }
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+ public String findExpiration(String rawItem){
|
|
92
|
+ Pattern pattern = Pattern.compile("(?<=([Tt][Yy][Pp][Ee][^A-Za-z])).*?(?=[^A-Za-z0])(.) + [^#]");
|
|
93
|
+ Matcher regMatcher2 = pattern.matcher(rawItem);
|
|
94
|
+
|
|
95
|
+ if (regMatcher2.find()){
|
|
96
|
+ return (regMatcher2).group();
|
|
97
|
+
|
|
98
|
+ } else return null;
|
|
99
|
+ }
|
|
100
|
+ public Map<String, ArrayList<Item>> getMap() throws Exception{
|
|
101
|
+ Main main = new Main();
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+ ArrayList<String> listOfItems = parseRawDataIntoStringArray(main.readRawDataToString());
|
|
105
|
+
|
|
106
|
+ for (String item : listOfItems){
|
|
107
|
+ try {
|
|
108
|
+ Item newItem = parseStringIntoItem(item);
|
|
109
|
+ if (!groceryListMap.containsKey(newItem.getName())){
|
|
110
|
+ ArrayList<Item> myItem = new ArrayList<Item>();
|
|
111
|
+ myItem.add(newItem);
|
|
112
|
+ groceryListMap.put(newItem.getName(),myItem);
|
|
113
|
+ }else {
|
|
114
|
+ groceryListMap.get(newItem.getName()).add(newItem);
|
|
115
|
+ }
|
|
116
|
+ }catch (ItemParseException e){
|
|
117
|
+ counter++;
|
|
118
|
+ }
|
|
119
|
+ }
|
|
120
|
+ return groceryListMap;
|
|
121
|
+ }
|
|
122
|
+ public String generateReport() throws Exception {
|
|
123
|
+ groceryListMap = getMap();
|
|
124
|
+ StringBuilder builder = new StringBuilder();
|
|
125
|
+
|
|
126
|
+ for (Map.Entry<String, ArrayList<Item>> groceryItems : groceryListMap.entrySet()) {
|
|
127
|
+ builder.append("\nname: ");
|
|
128
|
+ builder.append(String.format("%8s", captitalizeFirstLetter(groceryItems.getKey())));
|
|
129
|
+ builder.append("\t\t\t\tseen: " + getOccurencesOfItems(groceryItems.getValue()) + " times\n");
|
|
130
|
+ builder.append("===============" + "\t\t\t\t===============\n");
|
|
131
|
+ String priceReport = generatePriceReport(groceryItems);
|
|
132
|
+ builder.append(priceReport);
|
|
133
|
+ builder.append("---------------" + "\t\t\t\t---------------\n");
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+ }
|
|
137
|
+ builder.append("\nErrors\t\t\t\t\t\tseen: "+counter+" times\n");
|
|
138
|
+ return builder.toString();
|
|
139
|
+ }
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+ public String display() throws Exception{
|
|
144
|
+ groceryListMap = getMap();
|
|
145
|
+ StringBuilder displayBuild = new StringBuilder();
|
|
146
|
+
|
|
147
|
+ for (Map.Entry<String,ArrayList<Item>> item: groceryListMap.entrySet()){
|
|
148
|
+ String upperCase = item.getKey().substring(0,1).toUpperCase() + item.getKey().substring(1);
|
|
149
|
+
|
|
150
|
+ displayBuild.append("\n" + String.format("%-5s%10s%15s%2d%5s", "name:", upperCase, "seen: ", item.getValue().size(), " times"));
|
|
151
|
+ displayBuild.append("\n" + String.format("%15s%3s%5s", "===============", "\t\t\t", "===============") + "\n");
|
|
152
|
+
|
|
153
|
+ ArrayList<Double> uniquePriceList = getUniquePrices(item);
|
|
154
|
+ for (int i = 0; i < uniquePriceList.size(); i++) {
|
|
155
|
+ displayBuild.append(String.format("%-11s%.2f%15s%2d%5s", "Price:", uniquePriceList.get(i), "seen: ", getPriceOccurences(item.getValue(), uniquePriceList.get(i)), " times"));
|
|
156
|
+ displayBuild.append("\n" + String.format("%15s%3s%5s", "---------------", "\t\t\t", "---------------") + "\n");
|
|
157
|
+ }
|
|
158
|
+
|
|
159
|
+ }
|
|
160
|
+ displayBuild.append("\n" + String.format("%-20s%10s%2d%5s", "Errors", "seen: ", counter, " times"));
|
|
161
|
+
|
|
162
|
+ return displayBuild.toString();
|
|
163
|
+
|
|
164
|
+ }
|
|
165
|
+ public int getOccurencesOfItems(ArrayList list) {
|
|
166
|
+ return list.size();
|
|
167
|
+ }
|
|
168
|
+
|
|
169
|
+ public int getPriceOccurences(ArrayList<Item> list, Double price) {
|
|
170
|
+ int countPrice =0;
|
|
171
|
+ for (int i =0; i < list.size();i++){
|
|
172
|
+ if (list.get(i).getPrice().equals(price)){
|
|
173
|
+ countPrice++;
|
|
174
|
+ }
|
|
175
|
+ }
|
|
176
|
+ return countPrice;
|
|
177
|
+ }
|
|
178
|
+ public String generatePriceReport(Map.Entry<String,ArrayList<Item>> input) {
|
|
179
|
+ String reportPrice = "";
|
|
180
|
+ ArrayList<Double> nonDuplicatePrices = getUniquePrices(input);
|
|
181
|
+ for (int i = 0; i < nonDuplicatePrices.size(); i++) {
|
|
182
|
+ reportPrice += "Price";
|
|
183
|
+ reportPrice += (String.format("%10s", nonDuplicatePrices.get(i)));
|
|
184
|
+ reportPrice += ("\t\t\t\tseen: " + getPriceOccurences(input.getValue(), nonDuplicatePrices.get(i)) + " times\n");
|
|
185
|
+
|
|
186
|
+ }
|
|
187
|
+ return reportPrice;
|
|
188
|
+ }
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+ private ArrayList<Double> getUniquePrices(Map.Entry<String, ArrayList<Item>> item) {
|
|
192
|
+ ArrayList<Double> uniquePrice = new ArrayList<Double>();
|
|
193
|
+ for (int i = 0; i < item.getValue().size();i++){
|
|
194
|
+ if (!uniquePrice.contains(item.getValue().get(i).getPrice()));
|
|
195
|
+ uniquePrice.add(item.getValue().get(i).getPrice());
|
|
196
|
+
|
|
197
|
+ }
|
|
198
|
+ return uniquePrice;
|
|
199
|
+
|
|
200
|
+ }
|
|
201
|
+ public String captitalizeFirstLetter(String input) {
|
|
202
|
+ return input.substring(0, 1).toUpperCase() + input.substring(1).toLowerCase();
|
|
203
|
+ }
|
|
204
|
+
|
|
205
|
+
|
31
|
206
|
}
|