|
@@ -2,9 +2,12 @@ package io.zipcoder;
|
2
|
2
|
|
3
|
3
|
import java.util.ArrayList;
|
4
|
4
|
import java.util.Arrays;
|
|
5
|
+import java.util.regex.Pattern;
|
|
6
|
+import java.util.regex.Matcher;
|
5
|
7
|
|
6
|
|
-public class ItemParser {
|
|
8
|
+import static com.sun.tools.javac.util.StringUtils.toLowerCase;
|
7
|
9
|
|
|
10
|
+public class ItemParser{
|
8
|
11
|
|
9
|
12
|
public ArrayList<String> parseRawDataIntoStringArray(String rawData){
|
10
|
13
|
String stringPattern = "##";
|
|
@@ -13,11 +16,72 @@ public class ItemParser {
|
13
|
16
|
}
|
14
|
17
|
|
15
|
18
|
public Item parseStringIntoItem(String rawItem) throws ItemParseException{
|
16
|
|
- return null;
|
|
19
|
+
|
|
20
|
+ ArrayList<String> kvp = findKeyValuePairsInRawItemData(rawItem);
|
|
21
|
+
|
|
22
|
+ String name;
|
|
23
|
+ Double price;
|
|
24
|
+ String type;
|
|
25
|
+ String expiration;
|
|
26
|
+
|
|
27
|
+ //name block (with regex for milk, bread, cookies, apples)
|
|
28
|
+ Pattern namePattern = Pattern.compile("([Nn][Aa][Mm][Ee]):(\\w*\\d*)");
|
|
29
|
+ Matcher nameMatcher = namePattern.matcher(kvp.get(0));
|
|
30
|
+ if(nameMatcher.matches()){
|
|
31
|
+ name = nameMatcher.group(2);
|
|
32
|
+ }
|
|
33
|
+ else {
|
|
34
|
+ throw new ItemParseException("invalid name");
|
|
35
|
+ }
|
|
36
|
+
|
|
37
|
+ //price block (with regex for all prices)
|
|
38
|
+ Pattern pricePattern = Pattern.compile("([Pp][Rr][Ii][Cc][Ee]):(\\d.\\d\\d)");
|
|
39
|
+ Matcher priceMatcher = pricePattern.matcher(kvp.get(1));
|
|
40
|
+ if(priceMatcher.matches()){
|
|
41
|
+ price = Double.parseDouble(priceMatcher.group(2));
|
|
42
|
+ }
|
|
43
|
+ else {
|
|
44
|
+ throw new ItemParseException("invalid price");
|
|
45
|
+ }
|
|
46
|
+
|
|
47
|
+ //type block (with regex for all types)
|
|
48
|
+ Pattern typePattern = Pattern.compile("([Tt][Yy][Pp][Ee]):([A-Za-z0-9]*)");
|
|
49
|
+ Matcher typeMatcher = typePattern.matcher(kvp.get(2));
|
|
50
|
+ if(typeMatcher.matches()){
|
|
51
|
+ type = typeMatcher.group(2);
|
|
52
|
+ }
|
|
53
|
+ else {
|
|
54
|
+ throw new ItemParseException("invalid type");
|
|
55
|
+ }
|
|
56
|
+
|
|
57
|
+ //expiration block (with regex for all dates)
|
|
58
|
+ Pattern expPattern = Pattern.compile("([Ee][Xx][Pp][Ii][Rr][Aa][Tt][Ii][Oo][Nn]):(\\d/\\d{2}/\\d{4})##");
|
|
59
|
+ Matcher expMatcher = expPattern.matcher(kvp.get(3));
|
|
60
|
+ if(expMatcher.matches()){
|
|
61
|
+ expiration = expMatcher.group(2);
|
|
62
|
+ }
|
|
63
|
+ else {
|
|
64
|
+ throw new ItemParseException("invalid expiration");
|
|
65
|
+ }
|
|
66
|
+ return new Item(name, price, type, expiration);
|
|
67
|
+ }
|
|
68
|
+
|
|
69
|
+ //want to check and change spelling and casing here
|
|
70
|
+ public String checkSpelling(String itemName){
|
|
71
|
+ Matcher matcher = Pattern.compile("0", Pattern.CASE_INSENSITIVE).matcher(itemName);
|
|
72
|
+ matcher.find();
|
|
73
|
+ return matcher.replaceAll("o");
|
|
74
|
+ }
|
|
75
|
+
|
|
76
|
+ public String checkCase(String itemName){
|
|
77
|
+ if(itemName != null){
|
|
78
|
+ itemName = checkSpelling(toLowerCase(itemName));
|
|
79
|
+ }
|
|
80
|
+ return itemName;
|
17
|
81
|
}
|
18
|
82
|
|
19
|
83
|
public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){
|
20
|
|
- String stringPattern = "[;|^]";
|
|
84
|
+ String stringPattern = "[;|^|!|%|*|@]";
|
21
|
85
|
ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawItem);
|
22
|
86
|
return response;
|
23
|
87
|
}
|
|
@@ -26,6 +90,7 @@ public class ItemParser {
|
26
|
90
|
return new ArrayList<String>(Arrays.asList(inputString.split(stringPattern)));
|
27
|
91
|
}
|
28
|
92
|
|
29
|
|
-
|
30
|
|
-
|
|
93
|
+ public void errors(){
|
|
94
|
+ System.out.println(ItemParseException.errorsOutput());
|
|
95
|
+ }
|
31
|
96
|
}
|