|
@@ -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,11 +18,18 @@ 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
|
+
|
|
24
|
+ String name = values.get(0);
|
|
25
|
+ Double price = Double.valueOf(values.get(1));
|
|
26
|
+ String type = values.get(2);
|
|
27
|
+ String expiration = values.get(3);
|
|
28
|
+ return new Item(name, price, type, expiration);
|
17
|
29
|
}
|
18
|
30
|
|
19
|
31
|
public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){
|
20
|
|
- String stringPattern = "[;|^]";
|
|
32
|
+ String stringPattern = "[^a-zA-Z0-9\\:\\.\\/]";
|
21
|
33
|
ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawItem);
|
22
|
34
|
return response;
|
23
|
35
|
}
|
|
@@ -26,6 +38,116 @@ public class ItemParser {
|
26
|
38
|
return new ArrayList<String>(Arrays.asList(inputString.split(stringPattern)));
|
27
|
39
|
}
|
28
|
40
|
|
|
41
|
+ public String getValue(String input) throws ItemParseException{
|
|
42
|
+ ArrayList<String> kv = splitStringWithRegexPattern(":", input);
|
|
43
|
+ if(kv.size() < 2){
|
|
44
|
+ throw new ItemParseException();
|
|
45
|
+ } else if (kv.size()==2){
|
|
46
|
+ return kv.get(1);
|
|
47
|
+ }
|
|
48
|
+ return null;
|
|
49
|
+ }
|
|
50
|
+
|
|
51
|
+ public ArrayList<String> getValues(String rawItem) throws ItemParseException{
|
|
52
|
+ ArrayList <String> KVPairs = findKeyValuePairsInRawItemData(rawItem);
|
|
53
|
+ ArrayList<String> values = new ArrayList<String>();
|
|
54
|
+ for(String pair: KVPairs){
|
|
55
|
+ String value = getValue(toLowerCase(pair));
|
|
56
|
+ if(value.equals("")){
|
|
57
|
+ throw new ItemParseException();
|
|
58
|
+ } else{
|
|
59
|
+ values.add(value);
|
|
60
|
+ }
|
|
61
|
+ }
|
|
62
|
+ return values;
|
|
63
|
+ }
|
|
64
|
+
|
29
|
65
|
|
|
66
|
+ public String toLowerCase(String test){
|
|
67
|
+ //ArrayList<String> characters = new ArrayList<String>();
|
|
68
|
+
|
|
69
|
+ 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"};
|
|
70
|
+ 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"};
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+ Pattern pattern = Pattern.compile(".");
|
|
74
|
+ Matcher matcher = pattern.matcher(test);
|
|
75
|
+ StringBuilder lowercase = new StringBuilder();
|
|
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
|
+
|
|
87
|
+ return lowercase.toString();
|
|
88
|
+ }
|
|
89
|
+
|
|
90
|
+ public boolean isUpper(String c){
|
|
91
|
+ Pattern pattern = Pattern.compile("[A-Z]");
|
|
92
|
+ Matcher matcher = pattern.matcher(c);
|
|
93
|
+ return matcher.find();
|
|
94
|
+ }
|
|
95
|
+
|
|
96
|
+ public int indexOf(String[] array, String c){
|
|
97
|
+ for(int i = 0; i <array.length; i++){
|
|
98
|
+ if (array[i].equals(c)){
|
|
99
|
+ return i;
|
|
100
|
+ }
|
|
101
|
+ }
|
|
102
|
+ return -1;
|
|
103
|
+ }
|
|
104
|
+
|
|
105
|
+ public int findNumberOfErrors(String input) {
|
|
106
|
+ ArrayList<String> badFormat = new ArrayList<String>();
|
|
107
|
+ ArrayList<String> rawData = parseRawDataIntoStringArray(input);
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+ for (String data : rawData) {
|
|
111
|
+ try {
|
|
112
|
+ Item item = parseStringIntoItem(data);
|
|
113
|
+ }catch (ItemParseException e) {
|
|
114
|
+ badFormat.add(data);
|
|
115
|
+ }
|
|
116
|
+ }
|
|
117
|
+
|
|
118
|
+ return badFormat.size();
|
|
119
|
+
|
|
120
|
+ }
|
|
121
|
+
|
|
122
|
+ public ArrayList<Item> findItems(String input){
|
|
123
|
+ ArrayList<Item> items = new ArrayList<Item>();
|
|
124
|
+ ArrayList<String> rawData = parseRawDataIntoStringArray(input);
|
|
125
|
+ for(String data : rawData) {
|
|
126
|
+ try {
|
|
127
|
+ Item item = parseStringIntoItem(data);
|
|
128
|
+ items.add(item);
|
|
129
|
+ }catch (ItemParseException e) {
|
|
130
|
+ e.printStackTrace();
|
|
131
|
+ }
|
|
132
|
+ }
|
|
133
|
+ return items;
|
|
134
|
+
|
|
135
|
+ }
|
|
136
|
+
|
|
137
|
+ public Map<String, Integer> getUniqueNames(ArrayList<Item> items) {
|
|
138
|
+ Map<String, Integer> uniqueNames = new HashMap<String, Integer>();
|
|
139
|
+
|
|
140
|
+ for (Item item : items) {
|
|
141
|
+ if (!uniqueNames.containsKey(item.getName())) {
|
|
142
|
+ uniqueNames.put(item.getName(), 1);
|
|
143
|
+ } else {
|
|
144
|
+ int amount = uniqueNames.get(item.getName());
|
|
145
|
+ amount++;
|
|
146
|
+ uniqueNames.put(item.getName(), amount);
|
|
147
|
+ }
|
|
148
|
+ }
|
|
149
|
+
|
|
150
|
+ return uniqueNames;
|
|
151
|
+ }
|
30
|
152
|
|
31
|
153
|
}
|