|
@@ -1,7 +1,12 @@
|
1
|
1
|
package io.zipcoder;
|
2
|
2
|
|
|
3
|
+import java.lang.reflect.Array;
|
3
|
4
|
import java.util.ArrayList;
|
4
|
5
|
import java.util.Arrays;
|
|
6
|
+import java.util.HashMap;
|
|
7
|
+import java.util.Map;
|
|
8
|
+import java.util.regex.Matcher;
|
|
9
|
+import java.util.regex.Pattern;
|
5
|
10
|
|
6
|
11
|
public class ItemParser {
|
7
|
12
|
|
|
@@ -13,19 +18,154 @@ public class ItemParser {
|
13
|
18
|
}
|
14
|
19
|
|
15
|
20
|
public Item parseStringIntoItem(String rawItem) throws ItemParseException{
|
16
|
|
- return null;
|
|
21
|
+
|
|
22
|
+ ArrayList<String> values = getValues(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
|
+ return new Item(toLowerCase(name),price,toLowerCase(type),expiration);
|
|
29
|
+
|
17
|
30
|
}
|
18
|
31
|
|
19
|
32
|
public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){
|
20
|
|
- String stringPattern = "[;|^]";
|
|
33
|
+ String stringPattern = "[^a-zA-Z0-9\\:\\.\\/]";
|
21
|
34
|
ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawItem);
|
22
|
35
|
return response;
|
23
|
36
|
}
|
24
|
37
|
|
25
|
38
|
private ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString){
|
26
|
39
|
return new ArrayList<String>(Arrays.asList(inputString.split(stringPattern)));
|
|
40
|
+
|
|
41
|
+ }
|
|
42
|
+ public String getValue(String input) throws ItemParseException {
|
|
43
|
+ ArrayList<String> kv = splitStringWithRegexPattern(":", input);
|
|
44
|
+ if(kv.size() < 2){
|
|
45
|
+ throw new ItemParseException();
|
|
46
|
+ }else if(kv.size() == 2){
|
|
47
|
+ return kv.get(1);
|
|
48
|
+ }
|
|
49
|
+ return null;
|
|
50
|
+ }
|
|
51
|
+
|
|
52
|
+ public ArrayList<String> getValues(String rawItem) throws ItemParseException{
|
|
53
|
+ ArrayList<String> pairing = findKeyValuePairsInRawItemData(rawItem);
|
|
54
|
+ ArrayList<String> values = new ArrayList<String>();
|
|
55
|
+ for(String pair : pairing) {
|
|
56
|
+ String value = getValue(pair);
|
|
57
|
+ if (value.equals("")) {
|
|
58
|
+ throw new ItemParseException();
|
|
59
|
+
|
|
60
|
+ } else {
|
|
61
|
+ values.add((value));
|
|
62
|
+ }
|
|
63
|
+ }
|
|
64
|
+
|
|
65
|
+ return values;
|
|
66
|
+ }
|
|
67
|
+ public String toLowerCase(String test){
|
|
68
|
+ 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"};
|
|
69
|
+ 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"};
|
|
70
|
+
|
|
71
|
+ StringBuilder lowercase = new StringBuilder();
|
|
72
|
+
|
|
73
|
+ Pattern pattern = Pattern.compile(".");
|
|
74
|
+ Matcher matcher = pattern.matcher(test);
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+ while(matcher.find()){
|
|
78
|
+ String character = matcher.group();
|
|
79
|
+ if(isUpper(character)){
|
|
80
|
+ int i = indexOf(upper,character);
|
|
81
|
+ lowercase.append(lower[i]);
|
|
82
|
+ }else{
|
|
83
|
+ lowercase.append(character);
|
|
84
|
+ }
|
|
85
|
+ }
|
|
86
|
+ return lowercase.toString();
|
|
87
|
+ }
|
|
88
|
+
|
|
89
|
+ public boolean isUpper(String c){
|
|
90
|
+ Pattern pattern = Pattern.compile("[A-Z]");
|
|
91
|
+ Matcher matcher = pattern.matcher(c);
|
|
92
|
+ return matcher.find();
|
|
93
|
+ }
|
|
94
|
+
|
|
95
|
+ public int indexOf(String[] array, String c){
|
|
96
|
+ for (int i = 0; i < array.length ; i++) {
|
|
97
|
+ if(array[i].equals(c)) {
|
|
98
|
+ return i;
|
|
99
|
+ }
|
|
100
|
+ }
|
|
101
|
+ return -1;
|
|
102
|
+ }
|
|
103
|
+
|
|
104
|
+ public int findNumberOfErrors(String input){
|
|
105
|
+
|
|
106
|
+ ArrayList<String> badFormat = new ArrayList<String>();
|
|
107
|
+ ArrayList<String> rawData = parseRawDataIntoStringArray(input);
|
|
108
|
+
|
|
109
|
+ for(String data : rawData){
|
|
110
|
+ try{
|
|
111
|
+ Item item = parseStringIntoItem(data);
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+ }catch(ItemParseException e ){
|
|
115
|
+ badFormat.add(data);
|
|
116
|
+
|
|
117
|
+ }
|
|
118
|
+ }
|
|
119
|
+ return badFormat.size();
|
|
120
|
+ }
|
|
121
|
+
|
|
122
|
+ public ArrayList<Item> findItems(String input){
|
|
123
|
+ ArrayList<Item> items = new ArrayList<Item>();
|
|
124
|
+ ArrayList<String> rawData = parseRawDataIntoStringArray(input);
|
|
125
|
+
|
|
126
|
+ for(String data : rawData){
|
|
127
|
+ try{
|
|
128
|
+ Item item = parseStringIntoItem(data);
|
|
129
|
+ items.add(item);
|
|
130
|
+// System.out.format("%s %.2f \n", item.getName(), item.getPrice());
|
|
131
|
+ }catch(ItemParseException e ){
|
|
132
|
+ e.printStackTrace();
|
|
133
|
+
|
|
134
|
+ }
|
|
135
|
+ }
|
|
136
|
+ return items;
|
|
137
|
+ }
|
|
138
|
+
|
|
139
|
+ public Map<String, Integer> getUniqueItemNames(ArrayList<Item> items){
|
|
140
|
+
|
|
141
|
+ Map<String,Integer> uniqueNames = new HashMap<String,Integer>();
|
|
142
|
+
|
|
143
|
+ for(Item item : items) {
|
|
144
|
+ if (!uniqueNames.containsKey(item.getName())) {
|
|
145
|
+ uniqueNames.put(item.getName(), 1);
|
|
146
|
+ } else {
|
|
147
|
+ int amount = uniqueNames.get(item.getName());
|
|
148
|
+ amount += 1;
|
|
149
|
+ uniqueNames.put(item.getName(), amount);
|
|
150
|
+ }
|
|
151
|
+ }
|
|
152
|
+ return uniqueNames;
|
27
|
153
|
}
|
28
|
154
|
|
29
|
155
|
|
|
156
|
+// private String[] getKeyValue(String input){
|
|
157
|
+// Pattern pattern = Pattern.compile("([a-zA-Z0-9\\:\\.\\/]+)(?=[^\\/])");
|
|
158
|
+// Matcher matcher = pattern.matcher(input);
|
|
159
|
+//
|
|
160
|
+// }
|
|
161
|
+// public String toLowerCase(String test){
|
|
162
|
+// ArrayList<String> characters = new ArrayList<String>();
|
|
163
|
+//
|
|
164
|
+// Pattern pattern = Pattern.compile("\\w");
|
|
165
|
+// Matcher matcher = pattern.matcher(test);
|
|
166
|
+//
|
|
167
|
+//
|
|
168
|
+//
|
|
169
|
+// }
|
30
|
170
|
|
31
|
171
|
}
|