|
@@ -2,30 +2,128 @@ 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
|
+ Pattern pattern;
|
|
13
|
+ Matcher matcher;
|
|
14
|
+ public int counter = 0;
|
8
|
15
|
|
9
|
|
- public ArrayList<String> parseRawDataIntoStringArray(String rawData){
|
|
16
|
+ private Main main = new Main();
|
|
17
|
+ private Map<String, ArrayList<Item>> groceryMap = new HashMap<String, ArrayList<Item>>();
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+ public ArrayList<String> parseRawDataIntoStringArray(String rawData) {
|
10
|
21
|
String stringPattern = "##";
|
11
|
|
- ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawData);
|
|
22
|
+ ArrayList<String> response = splitStringWithRegexPattern(stringPattern, rawData);
|
12
|
23
|
return response;
|
13
|
24
|
}
|
14
|
25
|
|
15
|
|
- public Item parseStringIntoItem(String rawItem) throws ItemParseException{
|
16
|
|
- return null;
|
|
26
|
+ public Item parseStringIntoItem(String rawItem) throws ItemParseException {
|
|
27
|
+
|
|
28
|
+ if (findName(rawItem) == null || findPrice(rawItem) == null) {
|
|
29
|
+ throw new ItemParseException();
|
|
30
|
+ }
|
|
31
|
+
|
|
32
|
+ String itemName = findName(rawItem);
|
|
33
|
+ Double itemPrice = Double.parseDouble(findPrice(rawItem));
|
|
34
|
+ String itemType = findType(rawItem);
|
|
35
|
+ String itemExpiration = findExpiration(rawItem);
|
|
36
|
+
|
|
37
|
+ return new Item(itemName, itemPrice, itemType, itemExpiration);
|
17
|
38
|
}
|
18
|
39
|
|
19
|
|
- public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){
|
|
40
|
+ public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem) {
|
20
|
41
|
String stringPattern = "[;|^]";
|
21
|
|
- ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawItem);
|
|
42
|
+ ArrayList<String> response = splitStringWithRegexPattern(stringPattern, rawItem);
|
22
|
43
|
return response;
|
23
|
44
|
}
|
24
|
45
|
|
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
|
|
|
50
|
+ private String findName(String rawItem){
|
|
51
|
+ String search = "(?<=([Nn][Aa][Mm][Ee][^A-Za-z])).*?(?=[^A-Za-z0])";
|
|
52
|
+ pattern = Pattern.compile(search);
|
|
53
|
+ matcher = pattern.matcher(rawItem);
|
|
54
|
+
|
|
55
|
+ if (matcher.find()) {
|
|
56
|
+ if (!matcher.group().equals("")) {
|
|
57
|
+ String name = matcher.group().replaceAll("\\d", "o");
|
|
58
|
+ return name.toLowerCase();
|
|
59
|
+ //return name.substring(0, 1).toUpperCase() + name.substring(1).toLowerCase();
|
|
60
|
+ }
|
|
61
|
+ }
|
|
62
|
+ return null;
|
|
63
|
+ }
|
|
64
|
+
|
|
65
|
+ private String findPrice(String rawItem){
|
|
66
|
+ pattern = Pattern.compile("(?<=([Pp][Rr][Ii][Cc][Ee][^A-Za-z])).*?(?=[^0-9.])");
|
|
67
|
+ matcher = pattern.matcher(rawItem);
|
|
68
|
+
|
|
69
|
+ if (matcher.find()) {
|
|
70
|
+ if (!matcher.group().equals("")) {
|
|
71
|
+ return matcher.group();
|
|
72
|
+ }
|
|
73
|
+ }
|
|
74
|
+ return null;
|
|
75
|
+ }
|
|
76
|
+
|
|
77
|
+ public String findType(String rawItem) {
|
|
78
|
+ pattern = Pattern.compile("(?<=([Tt][Yy][Pp][Ee][^A-Za-z])).*?(?=[^A-Za-z0])");
|
|
79
|
+ matcher = pattern.matcher(rawItem);
|
|
80
|
+
|
|
81
|
+ if (matcher.find()) {
|
|
82
|
+ return matcher.group().toLowerCase();
|
|
83
|
+ }
|
|
84
|
+ return null;
|
|
85
|
+ }
|
|
86
|
+
|
|
87
|
+ public String findExpiration(String rawItem) {
|
|
88
|
+ pattern = Pattern.compile("(?<=([Ee][Xx][Pp][Ii][Rr][Aa][Tt][Ii][Oo][Nn][^A-Za-z]))(.)+[^#]");
|
|
89
|
+ matcher = pattern.matcher(rawItem);
|
|
90
|
+ if (matcher.find()) {
|
|
91
|
+ return matcher.group();
|
|
92
|
+ }
|
|
93
|
+ return null;
|
|
94
|
+ }
|
29
|
95
|
|
|
96
|
+ public Map<String, ArrayList<Item>> getMap() throws Exception {
|
|
97
|
+
|
|
98
|
+ ArrayList<String> listOfItems = parseRawDataIntoStringArray(main.readRawDataToString());
|
|
99
|
+ for (String anItem : listOfItems) {
|
|
100
|
+ try {
|
|
101
|
+ Item newItem = parseStringIntoItem(anItem);
|
|
102
|
+ if (!groceryMap.containsKey(newItem.getName())) {
|
|
103
|
+ ArrayList<Item> myItem = new ArrayList<Item>();
|
|
104
|
+ myItem.add(newItem);
|
|
105
|
+ groceryMap.put(newItem.getName(), myItem);
|
|
106
|
+ } else {
|
|
107
|
+ groceryMap.get(newItem.getName()).add(newItem);
|
|
108
|
+ }
|
|
109
|
+ } catch (ItemParseException e) {
|
|
110
|
+ counter++;
|
|
111
|
+ }
|
|
112
|
+ }
|
|
113
|
+ return groceryMap;
|
|
114
|
+ }
|
|
115
|
+
|
|
116
|
+ public String display(){
|
|
117
|
+ for(Map.Entry<String, ArrayList<Item>> mapKey : groceryMap.entrySet()) {
|
|
118
|
+ System.out.println(mapKey.getKey());
|
|
119
|
+ for (Item item : mapKey.getValue()) {
|
|
120
|
+ System.out.println(item.getPrice());
|
|
121
|
+ }
|
|
122
|
+ }
|
|
123
|
+ System.out.println("Errors seen: " + counter + " times");
|
|
124
|
+ return null;
|
|
125
|
+ }
|
|
126
|
+
|
|
127
|
+ public
|
|
128
|
+ }
|
30
|
129
|
|
31
|
|
-}
|