|
@@ -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
|
|
|
@@ -13,11 +15,20 @@ public class ItemParser {
|
13
|
15
|
}
|
14
|
16
|
|
15
|
17
|
public Item parseStringIntoItem(String rawItem) throws ItemParseException{
|
16
|
|
- return null;
|
|
18
|
+ ArrayList<String> values = getValues(toLowerCase(rawItem ) );
|
|
19
|
+ String name = values.get(0);
|
|
20
|
+ Double price = Double.valueOf(values.get(1));
|
|
21
|
+ String type = values.get(2);
|
|
22
|
+ String expiration = values.get(3);
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+ // return new Item(toLowerCase(rawItem );
|
|
26
|
+
|
|
27
|
+ return new Item(name, price, type, expiration);
|
17
|
28
|
}
|
18
|
29
|
|
19
|
30
|
public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){
|
20
|
|
- String stringPattern = "[;|^]";
|
|
31
|
+ String stringPattern = "[^a-zA-Z0-9\\:\\.\\/]";
|
21
|
32
|
ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawItem);
|
22
|
33
|
return response;
|
23
|
34
|
}
|
|
@@ -26,6 +37,75 @@ public class ItemParser {
|
26
|
37
|
return new ArrayList<String>(Arrays.asList(inputString.split(stringPattern)));
|
27
|
38
|
}
|
28
|
39
|
|
|
40
|
+ public String getValue(String input) throws ItemParseException {
|
|
41
|
+ ArrayList <String> kv=splitStringWithRegexPattern(":",input);
|
|
42
|
+ if(kv.size()<2 ){
|
|
43
|
+ throw new ItemParseException();
|
|
44
|
+ }else
|
|
45
|
+ {
|
|
46
|
+ if(kv.size()==2 ){
|
|
47
|
+ return kv.get(1);
|
|
48
|
+ }
|
|
49
|
+
|
|
50
|
+ }
|
|
51
|
+
|
|
52
|
+ // return splitStringWithRegexPattern(":", input).get(1);
|
|
53
|
+ return null;
|
|
54
|
+ }
|
|
55
|
+
|
|
56
|
+ public ArrayList<String> getValues(String rawItem) throws ItemParseException{
|
|
57
|
+ ArrayList<String> kvPairs = findKeyValuePairsInRawItemData(rawItem);
|
|
58
|
+ ArrayList<String> values = new ArrayList<String>();
|
|
59
|
+ for (String pair : kvPairs) {
|
|
60
|
+
|
|
61
|
+ String value = getValue(pair);
|
|
62
|
+ if (values.equals("")) {
|
|
63
|
+
|
|
64
|
+ throw new ItemParseException();
|
|
65
|
+
|
|
66
|
+ } else
|
|
67
|
+ values.add(value);
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+ }
|
|
71
|
+ return values;
|
|
72
|
+ }
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+ public boolean isUpper(String c){
|
|
76
|
+ Pattern pattern = Pattern.compile("[A-Z]");
|
|
77
|
+ Matcher matcher = pattern.matcher(c);
|
|
78
|
+ return matcher.find();
|
|
79
|
+ }
|
|
80
|
+
|
|
81
|
+ public String toLowerCase(String test){
|
|
82
|
+ String[] lower={"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"};
|
|
83
|
+ 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"};
|
|
84
|
+ StringBuilder lowercase=new StringBuilder();
|
|
85
|
+
|
|
86
|
+ Pattern pattern =Pattern.compile(".");
|
|
87
|
+ Matcher matcher=pattern.matcher(test);
|
|
88
|
+
|
|
89
|
+ while(matcher.find() ){
|
|
90
|
+ String character=matcher.group();
|
|
91
|
+ if(isUpper(character ) ) {
|
|
92
|
+ int i = indexOf(upper, character);
|
|
93
|
+ lowercase.append(lower[i]);
|
|
94
|
+ }else{
|
|
95
|
+ lowercase.append(character );
|
|
96
|
+ }
|
|
97
|
+
|
|
98
|
+ }
|
|
99
|
+ return lowercase.toString();
|
|
100
|
+ }
|
29
|
101
|
|
|
102
|
+ public int indexOf(String[] array, String c){
|
|
103
|
+ for(int i=0;i<array.length;i++ ){
|
|
104
|
+ if(array[i].equals(c) ){
|
|
105
|
+ return i;
|
|
106
|
+ }
|
|
107
|
+ }
|
|
108
|
+ return -1;
|
|
109
|
+ }
|
30
|
110
|
|
31
|
111
|
}
|