|
@@ -2,30 +2,168 @@ 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
|
+ public static int counter = 0;
|
|
13
|
+ private HashMap<String, ArrayList<Item>> groceryList = new HashMap<String, ArrayList<Item>>();
|
8
|
14
|
|
9
|
|
- public ArrayList<String> parseRawDataIntoStringArray(String rawData){
|
|
15
|
+
|
|
16
|
+ public ArrayList<String> parseRawDataIntoStringArray(String rawData) {
|
10
|
17
|
String stringPattern = "##";
|
11
|
|
- ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawData);
|
|
18
|
+ ArrayList<String> response = splitStringWithRegexPattern(stringPattern, rawData);
|
12
|
19
|
return response;
|
13
|
20
|
}
|
14
|
21
|
|
15
|
|
- public Item parseStringIntoItem(String rawItem) throws ItemParseException{
|
16
|
|
- return null;
|
|
22
|
+ public Item parseStringIntoItem(String rawItem) throws ItemParseException {
|
|
23
|
+
|
|
24
|
+ if (findName(rawItem) == null || findPrice(rawItem) == null) {
|
|
25
|
+ throw new ItemParseException();
|
|
26
|
+ }
|
|
27
|
+
|
|
28
|
+ String name = findName(rawItem);
|
|
29
|
+ Double price = Double.parseDouble(findPrice(rawItem));
|
|
30
|
+ String type = findType(rawItem);
|
|
31
|
+ String expirationDate = findExpirationDate(rawItem);
|
|
32
|
+
|
|
33
|
+ return new Item(name, price, type, expirationDate);
|
17
|
34
|
}
|
18
|
35
|
|
19
|
|
- public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){
|
|
36
|
+ public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem) {
|
20
|
37
|
String stringPattern = "[;|^]";
|
21
|
|
- ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawItem);
|
|
38
|
+ ArrayList<String> response = splitStringWithRegexPattern(stringPattern, rawItem);
|
22
|
39
|
return response;
|
23
|
40
|
}
|
24
|
41
|
|
25
|
|
- private ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString){
|
|
42
|
+ private ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString) {
|
26
|
43
|
return new ArrayList<String>(Arrays.asList(inputString.split(stringPattern)));
|
27
|
44
|
}
|
28
|
45
|
|
|
46
|
+ public String findName(String rawItem) {
|
|
47
|
+
|
|
48
|
+ Pattern namePattern = Pattern.compile("(?<=([Nn][Aa][Mm][Ee][^A-Za-z])).*?(?=[^A-Za-z0])");
|
|
49
|
+ Matcher regexMatcher1 = namePattern.matcher(rawItem);
|
|
50
|
+ if (regexMatcher1.find()) {
|
|
51
|
+ if (!regexMatcher1.group().equals("")) {
|
|
52
|
+ String name = regexMatcher1.group().replaceAll("\\d", "o");
|
|
53
|
+ return name.toLowerCase();
|
|
54
|
+ }
|
|
55
|
+
|
|
56
|
+ }
|
|
57
|
+ return null;
|
|
58
|
+ }
|
|
59
|
+
|
|
60
|
+ public String findPrice(String rawItem) {
|
|
61
|
+ Pattern pricePattern = Pattern.compile("(?<=([Pp][Rr][Ii][Cc][Ee][^A-Za-z])).*?(?=[^0-9.])");
|
|
62
|
+ Matcher regexMatcher2 = pricePattern.matcher(rawItem);
|
|
63
|
+ if (regexMatcher2.find()) {
|
|
64
|
+ if (!regexMatcher2.group().equals("")) {
|
|
65
|
+ return regexMatcher2.group();
|
|
66
|
+ }
|
|
67
|
+ }
|
|
68
|
+ return null;
|
|
69
|
+ }
|
|
70
|
+
|
|
71
|
+ public String findType(String rawItem) {
|
|
72
|
+ Pattern typePattern = Pattern.compile("(?<=([Tt][Yy][Pp][Ee][^A-Za-z])).*?(?=[^A-Za-z0])");
|
|
73
|
+ Matcher regexMatcher3 = typePattern.matcher(rawItem);
|
|
74
|
+ if (regexMatcher3.find()) {
|
|
75
|
+ return (regexMatcher3).group().toLowerCase();
|
|
76
|
+ } else return null;
|
|
77
|
+ }
|
|
78
|
+
|
|
79
|
+ public String findExpirationDate(String rawItem) {
|
|
80
|
+ Pattern expirationDatePattern = Pattern.compile("(?<=([Ee][Xx][Pp][Ii][Rr][Aa][Tt][Ii][Oo][Nn][^A-Za-z]))(.)+[^#]");
|
|
81
|
+ Matcher regexMatcher4 = expirationDatePattern.matcher(rawItem);
|
|
82
|
+ if (regexMatcher4.find()) {
|
|
83
|
+ return (regexMatcher4).group();
|
|
84
|
+ } else return null;
|
|
85
|
+
|
|
86
|
+ }
|
|
87
|
+
|
|
88
|
+ public HashMap<String, ArrayList<Item>> buildMap() throws Exception {
|
|
89
|
+ Main main = new Main();
|
29
|
90
|
|
|
91
|
+ ArrayList<String> listOfItems = parseRawDataIntoStringArray(main.readRawDataToString());
|
|
92
|
+ for (String anItem : listOfItems) {
|
|
93
|
+ try {
|
|
94
|
+ Item newItem = parseStringIntoItem(anItem);
|
|
95
|
+ if (!groceryList.containsKey(newItem.getName())) {
|
|
96
|
+ ArrayList<Item> myItem = new ArrayList<Item>();
|
|
97
|
+ myItem.add(newItem);
|
|
98
|
+ groceryList.put(newItem.getName(), myItem);
|
|
99
|
+ } else {
|
|
100
|
+ groceryList.get(newItem.getName()).add(newItem);
|
|
101
|
+ }
|
|
102
|
+ } catch (ItemParseException e) {
|
|
103
|
+ counter++;
|
|
104
|
+ }
|
|
105
|
+ }
|
|
106
|
+ return groceryList;
|
|
107
|
+ }
|
|
108
|
+
|
|
109
|
+ public String generateReport() throws Exception {
|
|
110
|
+ groceryList = buildMap();
|
|
111
|
+ StringBuilder builder = new StringBuilder();
|
|
112
|
+
|
|
113
|
+ for(Map.Entry<String,ArrayList<Item>>groceryItems:groceryList.entrySet()){
|
|
114
|
+ builder.append("\nname: ");
|
|
115
|
+ builder.append(String.format("%8s",captitalizeFirstLetter(groceryItems.getKey())));
|
|
116
|
+ builder.append("\t\t\t\tseen: "+getOccurencesOfItems(groceryItems.getValue())+" times\n");
|
|
117
|
+ builder.append("==============="+"\t\t\t\t===============\n");
|
|
118
|
+ String priceReport = generatePriceReport(groceryItems);
|
|
119
|
+ builder.append(priceReport);
|
|
120
|
+ builder.append("---------------"+"\t\t\t\t---------------\n");
|
|
121
|
+ }
|
|
122
|
+
|
|
123
|
+ builder.append("\nErrors\t\t\t\t\t\tseen: "+counter+" times\n");
|
|
124
|
+
|
|
125
|
+ return builder.toString();
|
|
126
|
+ }
|
|
127
|
+
|
|
128
|
+ public int getOccurencesOfItems(ArrayList list) {
|
|
129
|
+ return list.size();
|
|
130
|
+ }
|
|
131
|
+
|
|
132
|
+ public int getOccurencesOfPrices(ArrayList<Item> list, Double price) {
|
|
133
|
+ int priceCounter = 0;
|
|
134
|
+ for (int i = 0; i < list.size(); i++) {
|
|
135
|
+ if (list.get(i).getPrice().equals(price) ) {
|
|
136
|
+ priceCounter++;
|
|
137
|
+ }
|
|
138
|
+ }
|
|
139
|
+ return priceCounter;
|
|
140
|
+ }
|
|
141
|
+
|
|
142
|
+ public String generatePriceReport(Map.Entry<String, ArrayList<Item>> input) {
|
|
143
|
+ String priceReport = "";
|
|
144
|
+ ArrayList<Double> nonDupPrices = getUiniquePrices(input);
|
|
145
|
+ for(int i=0;i<nonDupPrices.size();i++){
|
|
146
|
+ priceReport+="Price";
|
|
147
|
+ priceReport+=(String.format("%10s",nonDupPrices.get(i)));
|
|
148
|
+ priceReport+=("\t\t\t\tseen: "+ getOccurencesOfPrices(input.getValue(),nonDupPrices.get(i))+
|
|
149
|
+ " times\n");
|
|
150
|
+ }
|
|
151
|
+ return priceReport;
|
|
152
|
+
|
|
153
|
+ }
|
|
154
|
+
|
|
155
|
+ public ArrayList<Double> getUiniquePrices(Map.Entry<String, ArrayList<Item>> input) {
|
|
156
|
+ ArrayList<Double> uniquePrices = new ArrayList<>();
|
|
157
|
+ for (int i=0;i<input.getValue().size();i++) {
|
|
158
|
+ if (!uniquePrices.contains(input.getValue().get(i).getPrice())) {
|
|
159
|
+ uniquePrices.add(input.getValue().get(i).getPrice());
|
|
160
|
+ }
|
|
161
|
+ }
|
|
162
|
+ return uniquePrices;
|
|
163
|
+ }
|
|
164
|
+
|
|
165
|
+ public String captitalizeFirstLetter(String input) {
|
|
166
|
+ return input.substring(0, 1).toUpperCase() + input.substring(1).toLowerCase();
|
|
167
|
+ }
|
30
|
168
|
|
31
|
169
|
}
|