|
@@ -1,23 +1,51 @@
|
1
|
1
|
package io.zipcoder;
|
2
|
2
|
|
|
3
|
+
|
3
|
4
|
import java.util.ArrayList;
|
4
|
5
|
import java.util.Arrays;
|
|
6
|
+import java.util.regex.Matcher;
|
|
7
|
+import java.util.regex.Pattern;
|
5
|
8
|
|
6
|
9
|
public class ItemParser {
|
7
|
10
|
|
8
|
11
|
|
|
12
|
+ private String name;
|
|
13
|
+ private Double price;
|
|
14
|
+ private String type;
|
|
15
|
+ private String expiration;
|
|
16
|
+
|
9
|
17
|
public ArrayList<String> parseRawDataIntoStringArray(String rawData){
|
10
|
18
|
String stringPattern = "##";
|
11
|
|
- ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawData);
|
|
19
|
+ ArrayList<String> response = splitStringWithRegexPattern(stringPattern , String.valueOf(rawData));
|
12
|
20
|
return response;
|
13
|
21
|
}
|
14
|
22
|
|
15
|
23
|
public Item parseStringIntoItem(String rawItem) throws ItemParseException{
|
16
|
|
- return null;
|
|
24
|
+ rawItem = rawItem.replaceAll("#", "");
|
|
25
|
+ ArrayList<String> keyValuePairsInRawItemData = findKeyValuePairsInRawItemData(rawItem);
|
|
26
|
+ // System.out.println(keyValuePairsInRawItemData);
|
|
27
|
+ for (String pair: keyValuePairsInRawItemData){
|
|
28
|
+ //System.out.println(pair);
|
|
29
|
+ if (pair.matches("[nameNAME]{4}"))
|
|
30
|
+ name = this.name(pair);
|
|
31
|
+ System.out.println(pair);
|
|
32
|
+ if(pair.matches("[pricePrice]{5}]"))
|
|
33
|
+ price = this.price(pair);
|
|
34
|
+ System.out.println(pair);
|
|
35
|
+ if(pair.matches("[type]{4}"))
|
|
36
|
+ type = this.type(pair);
|
|
37
|
+ System.out.println(pair);
|
|
38
|
+ if(pair.matches("[expriationEXPIRATION]{10}"))
|
|
39
|
+ expiration = this.expiration(pair);
|
|
40
|
+ System.out.println(pair);
|
|
41
|
+ }
|
|
42
|
+ return new Item(name, price, type, expiration);
|
|
43
|
+ //return name;
|
17
|
44
|
}
|
18
|
45
|
|
|
46
|
+
|
19
|
47
|
public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){
|
20
|
|
- String stringPattern = "[;|^]";
|
|
48
|
+ String stringPattern = "[;|^|%|*|!|@]";
|
21
|
49
|
ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawItem);
|
22
|
50
|
return response;
|
23
|
51
|
}
|
|
@@ -26,6 +54,106 @@ public class ItemParser {
|
26
|
54
|
return new ArrayList<String>(Arrays.asList(inputString.split(stringPattern)));
|
27
|
55
|
}
|
28
|
56
|
|
|
57
|
+ public String name(String value){
|
|
58
|
+ String name = null;
|
|
59
|
+ value = value.replaceAll("[~(nameNAME)~g]", "");
|
|
60
|
+ if (value.matches("[milkMILK]{4}")){
|
|
61
|
+ name= "Milk";
|
|
62
|
+ }
|
|
63
|
+ if (value.matches("[breadBREAD]{5}")){
|
|
64
|
+ name = "Bread";
|
|
65
|
+ }
|
|
66
|
+ if (value.matches("[cC]..[kK][iI][eE][sS]")){
|
|
67
|
+ name = "Cookies";
|
|
68
|
+ }
|
|
69
|
+ if (value.matches("[@applesAPPLES]{6}")){
|
|
70
|
+ name = "Apples";
|
|
71
|
+ }
|
|
72
|
+ return name;
|
|
73
|
+ }
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+ public Double price(String value){
|
|
77
|
+ Double price = null;
|
|
78
|
+ Pattern pattern = Pattern.compile("\\d.\\d+(?=;)");
|
|
79
|
+ Matcher matcher = pattern.matcher(value);
|
|
80
|
+ if(matcher.find()){
|
|
81
|
+ price = Double.parseDouble(matcher.group(0));
|
|
82
|
+ }
|
|
83
|
+ return price;
|
|
84
|
+ }
|
|
85
|
+
|
|
86
|
+ public String type(String value){
|
|
87
|
+ String type = null;
|
|
88
|
+ value = value.replaceAll("[type]{4}", "");
|
|
89
|
+ if(value.matches("[fodFOD]{4}")) {
|
|
90
|
+ type = "Food";
|
|
91
|
+ }
|
|
92
|
+ System.out.println(type);
|
|
93
|
+ return type;
|
|
94
|
+ }
|
|
95
|
+
|
|
96
|
+ public String expiration(String value){
|
|
97
|
+ String expiration = null;
|
|
98
|
+ Pattern pattern = Pattern.compile("(\\d/\\d+/\\d+)");
|
|
99
|
+ Matcher matcher = pattern.matcher(value);
|
|
100
|
+ if (matcher.find()){
|
|
101
|
+ expiration = matcher.group();
|
|
102
|
+ }
|
|
103
|
+ return expiration;
|
|
104
|
+ }
|
|
105
|
+
|
29
|
106
|
|
|
107
|
+// public String milk(String milk){
|
|
108
|
+// Pattern pattern = Pattern.compile("[milkMILK]{4}");
|
|
109
|
+// String output = "";
|
|
110
|
+// Matcher matcher = pattern.matcher(milk);
|
|
111
|
+// while (matcher.find()){
|
|
112
|
+// output = matcher.group(0);
|
|
113
|
+// }
|
|
114
|
+// return output;
|
|
115
|
+// }
|
|
116
|
+//
|
|
117
|
+// public String bread(String bread){
|
|
118
|
+// Pattern pattern = Pattern.compile("[breadBREAD]{5}");
|
|
119
|
+// String output = "";
|
|
120
|
+// Matcher matcher = pattern.matcher(bread);
|
|
121
|
+// while (matcher.find()){
|
|
122
|
+// output = matcher.group(0);
|
|
123
|
+// }
|
|
124
|
+// return output;
|
|
125
|
+// }
|
|
126
|
+//
|
|
127
|
+// public String cookies(String cookies){
|
|
128
|
+// Pattern pattern = Pattern.compile("[cC]..[kK][iI][eE][sS]");
|
|
129
|
+// String output = "";
|
|
130
|
+// Matcher matcher = pattern.matcher(cookies);
|
|
131
|
+// while (matcher.find()){
|
|
132
|
+// output = matcher.group(0);
|
|
133
|
+// }
|
|
134
|
+// return output;
|
|
135
|
+// }
|
|
136
|
+//
|
|
137
|
+// public String apples(String apples){
|
|
138
|
+// Pattern pattern = Pattern.compile("[@applesAPPLES]{6}");
|
|
139
|
+// String output = "";
|
|
140
|
+// Matcher matcher = pattern.matcher(apples);
|
|
141
|
+// while (matcher.find()){
|
|
142
|
+// output = matcher.group(0);
|
|
143
|
+// }
|
|
144
|
+// return output;
|
|
145
|
+// }
|
|
146
|
+
|
|
147
|
+// public String food(String food){
|
|
148
|
+// Pattern pattern = Pattern.compile("([0fodFOD]{4})+.");
|
|
149
|
+// String output = "";
|
|
150
|
+// Matcher matcher = pattern.matcher(food);
|
|
151
|
+// while (matcher.find()){
|
|
152
|
+// output = matcher.group(0);
|
|
153
|
+// }
|
|
154
|
+// return output;
|
|
155
|
+// }
|
30
|
156
|
|
31
|
157
|
}
|
|
158
|
+
|
|
159
|
+
|