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