|
@@ -2,10 +2,14 @@ package io.zipcoder;
|
2
|
2
|
|
3
|
3
|
import java.util.ArrayList;
|
4
|
4
|
import java.util.Arrays;
|
|
5
|
+import java.util.Map;
|
5
|
6
|
import java.util.TreeMap;
|
|
7
|
+import java.util.regex.Matcher;
|
|
8
|
+import java.util.regex.Pattern;
|
6
|
9
|
|
7
|
10
|
public class ItemParser {
|
8
|
11
|
//to organize my Key/Value pairs
|
|
12
|
+ private int exceptions = 0;
|
9
|
13
|
private TreeMap<String, ArrayList<Item>> groceryMap;
|
10
|
14
|
|
11
|
15
|
public ItemParser(){
|
|
@@ -13,7 +17,7 @@ public class ItemParser {
|
13
|
17
|
groceryMap = new TreeMap<String, ArrayList<Item>>();
|
14
|
18
|
}
|
15
|
19
|
|
16
|
|
-
|
|
20
|
+//splits a string of rawData by ## and puts it into an ArrayList called reponse
|
17
|
21
|
public ArrayList<String> parseRawDataIntoStringArray(String rawData){
|
18
|
22
|
String stringPattern = "##";
|
19
|
23
|
ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawData);
|
|
@@ -21,6 +25,64 @@ public class ItemParser {
|
21
|
25
|
}
|
22
|
26
|
|
23
|
27
|
public Item parseStringIntoItem(String rawItem) throws ItemParseException{
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+ if(findName(rawItem) == null || findPrice(rawItem) == null){
|
|
31
|
+ throw new ItemParseException();
|
|
32
|
+ }
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+ String name = findName(rawItem);
|
|
36
|
+ Double price = Double.parseDouble(findPrice(rawItem));
|
|
37
|
+ String type = findType(rawItem);
|
|
38
|
+ String expirationDate = findExpirationDate(rawItem);
|
|
39
|
+
|
|
40
|
+ Item item = new Item(name, price, type, expirationDate);
|
|
41
|
+ return item;
|
|
42
|
+ }
|
|
43
|
+
|
|
44
|
+ private String findExpirationDate(String rawItem)throws ItemParseException {
|
|
45
|
+ Pattern checkExpDateRegex = Pattern.compile("\\d\\/\\d+\\/\\d+");
|
|
46
|
+ Matcher regexExpDateMatcher = checkExpDateRegex.matcher(rawItem);
|
|
47
|
+
|
|
48
|
+ if(regexExpDateMatcher.find()){
|
|
49
|
+ return regexExpDateMatcher.group();
|
|
50
|
+ }
|
|
51
|
+ return null;
|
|
52
|
+ }
|
|
53
|
+
|
|
54
|
+ private String findType(String rawItem)throws ItemParseException {
|
|
55
|
+ Pattern checkTypeRegex = Pattern.compile("(?<=([Tt][Yy][Pp][Ee][^A-Za-z])).*?(?=[^A-Za-z0])");
|
|
56
|
+ Matcher regexTypeMatcher = checkTypeRegex.matcher(rawItem);
|
|
57
|
+
|
|
58
|
+ if(regexTypeMatcher.find()){
|
|
59
|
+ String fixtype = regexTypeMatcher.group().replace("\\d", "f");
|
|
60
|
+ return fixtype.toLowerCase();
|
|
61
|
+ }
|
|
62
|
+
|
|
63
|
+ return null;
|
|
64
|
+ }
|
|
65
|
+
|
|
66
|
+ public String findName(String rawItem)throws ItemParseException{
|
|
67
|
+ Pattern checkNameRegex = Pattern.compile("(?<=([Nn][Aa][Mm][Ee][^A-Za-z])).*?(?=[^A-Za-z0])");
|
|
68
|
+ Matcher regexNameMatcher = checkNameRegex.matcher(rawItem);
|
|
69
|
+
|
|
70
|
+ if(regexNameMatcher.find()){
|
|
71
|
+ String fixName = regexNameMatcher.group().replace("\\d", "o");
|
|
72
|
+ return fixName.toLowerCase();
|
|
73
|
+ }
|
|
74
|
+ return null;
|
|
75
|
+ }
|
|
76
|
+
|
|
77
|
+ public String findPrice(String rawItem) throws NumberFormatException{
|
|
78
|
+ Pattern checkPriceRegex = Pattern.compile("\\d\\.\\d*");
|
|
79
|
+ Matcher regexPriceMatcher = checkPriceRegex.matcher(rawItem);
|
|
80
|
+
|
|
81
|
+ if(regexPriceMatcher.find()){
|
|
82
|
+ if(!regexPriceMatcher.group().equals("")){
|
|
83
|
+ return regexPriceMatcher.group();
|
|
84
|
+ }
|
|
85
|
+ }
|
24
|
86
|
return null;
|
25
|
87
|
}
|
26
|
88
|
|
|
@@ -35,6 +97,23 @@ public class ItemParser {
|
35
|
97
|
}
|
36
|
98
|
|
37
|
99
|
|
|
100
|
+ public Map<String, ArrayList<Item>> printGroceries(){
|
|
101
|
+ return null;
|
|
102
|
+
|
|
103
|
+ }
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+ //PseudoCode
|
|
107
|
+ //I need to print the grocery list
|
|
108
|
+ //I should be able to add to the grocery list
|
|
109
|
+ //I need to be able to return pricesoh
|
|
110
|
+ //I need to be able to count the exceptions
|
|
111
|
+ //need a replace method for the C00kies
|
|
112
|
+ //get key
|
|
113
|
+ //get value
|
|
114
|
+ //how to handle if the price field is empty
|
|
115
|
+ //patterns & matches
|
|
116
|
+
|
38
|
117
|
|
39
|
118
|
|
40
|
119
|
|