|
@@ -2,9 +2,15 @@ package io.zipcoder;
|
2
|
2
|
|
3
|
3
|
import java.util.ArrayList;
|
4
|
4
|
import java.util.Arrays;
|
|
5
|
+import java.util.logging.Level;
|
|
6
|
+import java.util.logging.Logger;
|
|
7
|
+import java.util.regex.Matcher;
|
|
8
|
+import java.util.regex.Pattern;
|
5
|
9
|
|
6
|
10
|
public class ItemParser {
|
7
|
11
|
|
|
12
|
+ private static final Logger log = Logger.getLogger( ItemParser.class.getName() );
|
|
13
|
+
|
8
|
14
|
|
9
|
15
|
public ArrayList<String> parseRawDataIntoStringArray(String rawData){
|
10
|
16
|
String stringPattern = "##";
|
|
@@ -12,12 +18,38 @@ public class ItemParser {
|
12
|
18
|
return response;
|
13
|
19
|
}
|
14
|
20
|
|
|
21
|
+ public String correctCase(){
|
|
22
|
+ return "";
|
|
23
|
+ }
|
|
24
|
+
|
15
|
25
|
public Item parseStringIntoItem(String rawItem) throws ItemParseException{
|
|
26
|
+ ArrayList<String> properties = findKeyValuePairsInRawItemData(rawItem);
|
|
27
|
+ Pattern patternAlpha = Pattern.compile("(?<=:).*");
|
|
28
|
+ Pattern patternDate = Pattern.compile("\\d/\\d\\d/\\d\\d\\d\\d");
|
|
29
|
+ Pattern patternDouble = Pattern.compile("\\d.\\d\\d");
|
|
30
|
+ Matcher mName = patternAlpha.matcher(properties.get(0));
|
|
31
|
+ Matcher mPrice = patternDouble.matcher(properties.get(1));
|
|
32
|
+ Matcher mDescription = patternAlpha.matcher(properties.get(2));
|
|
33
|
+ Matcher mExpiration = patternDate.matcher(properties.get(3));
|
|
34
|
+
|
|
35
|
+try {
|
|
36
|
+
|
|
37
|
+ if (mName.find() && mDescription.find() && mPrice.find() && mExpiration.find()) {
|
|
38
|
+ String name = mName.group();
|
|
39
|
+ Double price = Double.parseDouble(mPrice.group());
|
|
40
|
+ String type = mDescription.group();
|
|
41
|
+ String expiration = mExpiration.group();
|
|
42
|
+ return new Item(name, price, type, expiration);
|
|
43
|
+ }
|
|
44
|
+ } catch( ItemParseException e ){
|
|
45
|
+ log.log(Level.WARNING, "invalid");
|
|
46
|
+ }
|
|
47
|
+
|
16
|
48
|
return null;
|
17
|
49
|
}
|
18
|
50
|
|
19
|
51
|
public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){
|
20
|
|
- String stringPattern = "[;|^]";
|
|
52
|
+ String stringPattern = "[;|^!%@*]";
|
21
|
53
|
ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawItem);
|
22
|
54
|
return response;
|
23
|
55
|
}
|