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