|
@@ -10,19 +10,36 @@ import java.util.regex.Pattern;
|
10
|
10
|
public class ItemParser {
|
11
|
11
|
|
12
|
12
|
private Pattern pattern;
|
|
13
|
+
|
13
|
14
|
private Matcher matcher;
|
14
|
|
- private int counter = 0;
|
15
|
|
- private Main main = new Main();
|
|
15
|
+
|
|
16
|
+ public int counter = 0;
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
16
|
20
|
private Map<String ,ArrayList<Item>> groceryListMap = new HashMap<String, ArrayList<Item>>();
|
17
|
21
|
// Splits string based on ##
|
|
22
|
+
|
18
|
23
|
public ArrayList<String> parseRawDataIntoStringArray(String rawData){
|
19
|
24
|
String stringPattern = "##";
|
20
|
25
|
ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawData);
|
21
|
26
|
return response;
|
22
|
27
|
}
|
|
28
|
+ public Item parseStringIntoItem(String rawItem) throws ItemParseException {
|
|
29
|
+ if (findName(rawItem) == null || findPrice(rawItem) == null) {
|
|
30
|
+ throw new ItemParseException();
|
|
31
|
+
|
|
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);
|
|
39
|
+ }
|
23
|
40
|
|
24
|
41
|
public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){
|
25
|
|
- String stringPattern = "[^|*|@|!|$|%|&|;]";
|
|
42
|
+ String stringPattern = "[^|;]";
|
26
|
43
|
ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawItem);
|
27
|
44
|
return response;
|
28
|
45
|
}
|
|
@@ -30,21 +47,11 @@ public class ItemParser {
|
30
|
47
|
return new ArrayList<String>(Arrays.asList(inputString.split(stringPattern)));
|
31
|
48
|
}
|
32
|
49
|
|
33
|
|
- public Item parseStringIntoItem(String rawItem) throws ItemParseException {
|
34
|
|
- if (findName(rawItem) == null || findPrice(rawItem) == null){
|
35
|
|
- throw new ItemParseException();
|
36
|
50
|
|
37
|
|
- }
|
38
|
|
- String itemName = findName(rawItem);
|
39
|
|
- Double ittemPrice = Double.parseDouble(findPrice(rawItem));
|
40
|
|
- String itemType = findType(rawItem);
|
41
|
|
- String itemExpiration = findExpiration(rawItem);
|
42
|
51
|
|
43
|
|
- return new Item(itemName,ittemPrice,itemType,itemExpiration);
|
44
|
52
|
|
45
|
53
|
|
46
|
54
|
|
47
|
|
- }
|
48
|
55
|
public String findName(String rawItem){
|
49
|
56
|
String search = "(?<=([Nn][Aa][Mm][Ee][^A-Za-z])).*?(?=[^A-Za-z0])";
|
50
|
57
|
pattern = Pattern.compile(search);
|
|
@@ -70,29 +77,30 @@ public class ItemParser {
|
70
|
77
|
return null;
|
71
|
78
|
}
|
72
|
79
|
public String findType(String rawItem){
|
73
|
|
- pattern =Pattern.compile("(?<=([Tt][Yy][Pp][Ee][^A-Za-z])).*?(?=[^A-Za-z0])");
|
74
|
|
- matcher.pattern().matcher(rawItem);
|
|
80
|
+ Pattern pattern =Pattern.compile("(?<=([Tt][Yy][Pp][Ee][^A-Za-z])).*?(?=[^A-Za-z0])");
|
|
81
|
+ Matcher regMatcher =pattern.matcher(rawItem);
|
75
|
82
|
|
76
|
|
- if (matcher.find()){
|
77
|
|
- return matcher.group().toLowerCase();
|
|
83
|
+ if (regMatcher.find()){
|
|
84
|
+ return (regMatcher).group().toLowerCase();
|
78
|
85
|
|
79
|
|
- }
|
80
|
|
- return null;
|
|
86
|
+ } else return null;
|
81
|
87
|
}
|
82
|
88
|
|
83
|
89
|
|
84
|
90
|
|
85
|
91
|
public String findExpiration(String rawItem){
|
86
|
|
- pattern = Pattern.compile("(?<=([Tt][Yy][Pp][Ee][^A-Za-z])).*?(?=[^A-Za-z0])(.) + [^#]");
|
87
|
|
- matcher = pattern.matcher(rawItem);
|
|
92
|
+ Pattern pattern = Pattern.compile("(?<=([Tt][Yy][Pp][Ee][^A-Za-z])).*?(?=[^A-Za-z0])(.) + [^#]");
|
|
93
|
+ Matcher regMatcher2 = pattern.matcher(rawItem);
|
88
|
94
|
|
89
|
|
- if (matcher.find()){
|
90
|
|
- return matcher.group();
|
91
|
|
- }
|
|
95
|
+ if (regMatcher2.find()){
|
|
96
|
+ return (regMatcher2).group();
|
92
|
97
|
|
93
|
|
- return null;
|
|
98
|
+ } else return null;
|
94
|
99
|
}
|
95
|
100
|
public Map<String, ArrayList<Item>> getMap() throws Exception{
|
|
101
|
+ Main main = new Main();
|
|
102
|
+
|
|
103
|
+
|
96
|
104
|
ArrayList<String> listOfItems = parseRawDataIntoStringArray(main.readRawDataToString());
|
97
|
105
|
|
98
|
106
|
for (String item : listOfItems){
|
|
@@ -111,6 +119,26 @@ public class ItemParser {
|
111
|
119
|
}
|
112
|
120
|
return groceryListMap;
|
113
|
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
|
+
|
114
|
142
|
|
115
|
143
|
public String display() throws Exception{
|
116
|
144
|
groceryListMap = getMap();
|
|
@@ -124,7 +152,7 @@ public class ItemParser {
|
124
|
152
|
|
125
|
153
|
ArrayList<Double> uniquePriceList = getUniquePrices(item);
|
126
|
154
|
for (int i = 0; i < uniquePriceList.size(); i++) {
|
127
|
|
- displayBuild.append(String.format("%-11s%.2f%15s%2d%5s", "Price:", uniquePriceList.get(i), "seen: ", seenPriceOccurences(item.getValue(), uniquePriceList.get(i)), " times"));
|
|
155
|
+ displayBuild.append(String.format("%-11s%.2f%15s%2d%5s", "Price:", uniquePriceList.get(i), "seen: ", getPriceOccurences(item.getValue(), uniquePriceList.get(i)), " times"));
|
128
|
156
|
displayBuild.append("\n" + String.format("%15s%3s%5s", "---------------", "\t\t\t", "---------------") + "\n");
|
129
|
157
|
}
|
130
|
158
|
|
|
@@ -134,7 +162,11 @@ public class ItemParser {
|
134
|
162
|
return displayBuild.toString();
|
135
|
163
|
|
136
|
164
|
}
|
137
|
|
- public int seenPriceOccurences(ArrayList<Item> list, Double price) {
|
|
165
|
+ public int getOccurencesOfItems(ArrayList list) {
|
|
166
|
+ return list.size();
|
|
167
|
+ }
|
|
168
|
+
|
|
169
|
+ public int getPriceOccurences(ArrayList<Item> list, Double price) {
|
138
|
170
|
int countPrice =0;
|
139
|
171
|
for (int i =0; i < list.size();i++){
|
140
|
172
|
if (list.get(i).getPrice().equals(price)){
|
|
@@ -143,6 +175,17 @@ public class ItemParser {
|
143
|
175
|
}
|
144
|
176
|
return countPrice;
|
145
|
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
|
+ }
|
146
|
189
|
|
147
|
190
|
|
148
|
191
|
private ArrayList<Double> getUniquePrices(Map.Entry<String, ArrayList<Item>> item) {
|
|
@@ -155,6 +198,9 @@ public class ItemParser {
|
155
|
198
|
return uniquePrice;
|
156
|
199
|
|
157
|
200
|
}
|
|
201
|
+ public String captitalizeFirstLetter(String input) {
|
|
202
|
+ return input.substring(0, 1).toUpperCase() + input.substring(1).toLowerCase();
|
|
203
|
+ }
|
158
|
204
|
|
159
|
205
|
|
160
|
206
|
}
|