|
@@ -2,6 +2,8 @@ package io.zipcoder;
|
2
|
2
|
|
3
|
3
|
import java.util.ArrayList;
|
4
|
4
|
import java.util.Arrays;
|
|
5
|
+import java.util.regex.Matcher;
|
|
6
|
+import java.util.regex.Pattern;
|
5
|
7
|
|
6
|
8
|
public class ItemParser {
|
7
|
9
|
|
|
@@ -12,12 +14,43 @@ public class ItemParser {
|
12
|
14
|
return response;
|
13
|
15
|
}
|
14
|
16
|
|
15
|
|
- public Item parseStringIntoItem(String rawItem) throws ItemParseException{
|
16
|
|
- return null;
|
|
17
|
+ public Item parseStringIntoItem(String rawItem) throws ItemParseException
|
|
18
|
+ {
|
|
19
|
+ ArrayList<String> values = getValues(toLowerCase(rawItem));
|
|
20
|
+
|
|
21
|
+ String name = values.get(0);
|
|
22
|
+ Double price = Double.valueOf(values.get(1));
|
|
23
|
+ String type = values.get(2);
|
|
24
|
+ String expiration = values.get(3);
|
|
25
|
+
|
|
26
|
+ return new Item(name,price,type,expiration);
|
|
27
|
+
|
|
28
|
+ }
|
|
29
|
+ private String getValue(String input)
|
|
30
|
+ {
|
|
31
|
+ return splitStringWithRegexPattern(":" , input).get(1);
|
|
32
|
+ }
|
|
33
|
+
|
|
34
|
+ private ArrayList<String> getValues(String rawItem) throws ItemParseException {
|
|
35
|
+ ArrayList<String> KVPairs = findKeyValuePairsInRawItemData(rawItem);
|
|
36
|
+ ArrayList<String> values = new ArrayList<String>();
|
|
37
|
+ for(String pair : KVPairs)
|
|
38
|
+ {
|
|
39
|
+ String value = getValue(pair);
|
|
40
|
+ if(value .equals(""))
|
|
41
|
+ {
|
|
42
|
+ throw new ItemParseException();
|
|
43
|
+ }
|
|
44
|
+ else
|
|
45
|
+ {
|
|
46
|
+ values.add(value);
|
|
47
|
+ }
|
|
48
|
+ }
|
|
49
|
+ return values;
|
17
|
50
|
}
|
18
|
51
|
|
19
|
52
|
public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){
|
20
|
|
- String stringPattern = "[;|^]";
|
|
53
|
+ String stringPattern = "[^a-zA-Z0-9\\:\\.\\/]";
|
21
|
54
|
ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawItem);
|
22
|
55
|
return response;
|
23
|
56
|
}
|
|
@@ -26,6 +59,47 @@ public class ItemParser {
|
26
|
59
|
return new ArrayList<String>(Arrays.asList(inputString.split(stringPattern)));
|
27
|
60
|
}
|
28
|
61
|
|
|
62
|
+ public String toLowerCase(String test)
|
|
63
|
+ {
|
|
64
|
+ String[] lower = {"a","b","c","d","e","f","g","h","i","j","k","l","m","o","q","s","u","v","w","x","y","z"};
|
|
65
|
+ String[] upper = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
|
|
66
|
+
|
|
67
|
+ StringBuilder lowercase = new StringBuilder();
|
29
|
68
|
|
|
69
|
+ Pattern pattern =Pattern.compile(".");
|
|
70
|
+ Matcher matcher = pattern.matcher(test);
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+ while(matcher.find())
|
|
74
|
+ {
|
|
75
|
+ String character = matcher.group();
|
|
76
|
+ if(isUpper(character))
|
|
77
|
+ {
|
|
78
|
+ int i = indexOf(upper,character);
|
|
79
|
+ lowercase.append(lower[i]);
|
|
80
|
+ }else {
|
|
81
|
+ lowercase.append(character);
|
|
82
|
+ }
|
|
83
|
+ }
|
|
84
|
+ return lowercase.toString();
|
|
85
|
+ }
|
|
86
|
+
|
|
87
|
+ public boolean isUpper(String c)
|
|
88
|
+ {
|
|
89
|
+ Pattern pattern = Pattern.compile("[A-Z]");
|
|
90
|
+ Matcher matcher = pattern.matcher(c);
|
|
91
|
+ return matcher.find();
|
|
92
|
+ }
|
|
93
|
+ public int indexOf(String[] array,String c)
|
|
94
|
+ {
|
|
95
|
+ for(int i =0; i<array.length;i++)
|
|
96
|
+ {
|
|
97
|
+ if(array[i].equals(c))
|
|
98
|
+ {
|
|
99
|
+ return i;
|
|
100
|
+ }
|
|
101
|
+ }
|
|
102
|
+ return -1;
|
|
103
|
+ }
|
30
|
104
|
|
31
|
105
|
}
|