|
@@ -2,30 +2,112 @@ 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 static java.util.regex.Pattern.CASE_INSENSITIVE;
|
|
8
|
+import static java.util.regex.Pattern.LITERAL;
|
5
|
9
|
|
6
|
10
|
public class ItemParser {
|
7
|
11
|
|
|
12
|
+ private static int exceptionCounter;
|
|
13
|
+
|
|
14
|
+ public ItemParser(){
|
|
15
|
+ exceptionCounter = 0;
|
|
16
|
+ }
|
|
17
|
+
|
|
18
|
+ public int getExceptionCounter(){
|
|
19
|
+ return exceptionCounter;
|
|
20
|
+ }
|
8
|
21
|
|
9
|
22
|
public ArrayList<String> parseRawDataIntoStringArray(String rawData){
|
|
23
|
+ String normalizedRawData = normalizeRawData(rawData);
|
10
|
24
|
String stringPattern = "##";
|
11
|
|
- ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawData);
|
|
25
|
+ ArrayList<String> response = splitStringWithRegexPattern(stringPattern , normalizedRawData);
|
12
|
26
|
return response;
|
13
|
27
|
}
|
14
|
28
|
|
15
|
|
- public Item parseStringIntoItem(String rawItem) throws ItemParseException{
|
16
|
|
- return null;
|
|
29
|
+ public ArrayList<Item> stringArrayToItemArray(ArrayList<String> rawItems)throws ItemParseException{
|
|
30
|
+ ArrayList<Item> itemArray = new ArrayList<>();
|
|
31
|
+ for(String item: rawItems){
|
|
32
|
+ Item it = parseStringIntoItem(item);
|
|
33
|
+ if(!it.getName().equals("")) {
|
|
34
|
+ itemArray.add(it);
|
|
35
|
+ }
|
|
36
|
+ }
|
|
37
|
+ return itemArray;
|
17
|
38
|
}
|
18
|
39
|
|
19
|
|
- public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){
|
20
|
|
- String stringPattern = "[;|^]";
|
21
|
|
- ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawItem);
|
22
|
|
- return response;
|
|
40
|
+ public Item parseStringIntoItem(String rawItem) throws ItemParseException{
|
|
41
|
+ String name = "";
|
|
42
|
+ String priceString = "";
|
|
43
|
+ Double price = 0.00;
|
|
44
|
+ String type = "";
|
|
45
|
+ String expiration = "";
|
|
46
|
+ Pattern pattern = Pattern.compile("name:(\\w*);price:(\\d*.\\d*\\d*);type:(\\w*);expiration:(\\w*/\\w*/\\w*)");
|
|
47
|
+ Matcher matcher = pattern.matcher(rawItem);
|
|
48
|
+ if(matcher.find()) {
|
|
49
|
+ try {
|
|
50
|
+ name = matcher.group(1);
|
|
51
|
+ priceString = matcher.group(2);
|
|
52
|
+ price = Double.parseDouble(priceString);
|
|
53
|
+ type = matcher.group(3);
|
|
54
|
+ expiration = matcher.group(4);
|
|
55
|
+ if (name.length() == 0 || priceString.length() == 0 || type.length() == 0 || expiration.length() == 0) {
|
|
56
|
+ exceptionCounter++;
|
|
57
|
+ throw new ItemParseException();
|
|
58
|
+ }
|
|
59
|
+ } catch (ItemParseException e) {
|
|
60
|
+ exceptionCounter++;
|
|
61
|
+ }
|
|
62
|
+ }
|
|
63
|
+ return new Item(name, price, type, expiration);
|
23
|
64
|
}
|
24
|
65
|
|
25
|
66
|
private ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString){
|
26
|
67
|
return new ArrayList<String>(Arrays.asList(inputString.split(stringPattern)));
|
27
|
68
|
}
|
28
|
69
|
|
|
70
|
+ public String normalizeRawData(String rawData){
|
|
71
|
+ String normal = convertWeirdCharactersToSemiColon(rawData);
|
|
72
|
+ String normal2 = toLowerCaseString(normal);
|
|
73
|
+ return remove0FromCookie(normal2);
|
|
74
|
+ }
|
|
75
|
+
|
|
76
|
+ public String convertWeirdCharactersToSemiColon(String rawData) {
|
|
77
|
+ ArrayList<String> wierdCharacters = new ArrayList<>(Arrays.asList("^", "!", "*", "%"));
|
|
78
|
+ Pattern pattern = Pattern.compile("@");
|
|
79
|
+ Matcher matcher = pattern.matcher(rawData);
|
|
80
|
+ String unWeird = matcher.replaceAll(";");
|
|
81
|
+ for (String character : wierdCharacters) {
|
|
82
|
+ matcher.reset(unWeird);
|
|
83
|
+ matcher.usePattern(Pattern.compile(character, Pattern.LITERAL));
|
|
84
|
+ unWeird = matcher.replaceAll(";");
|
|
85
|
+ }
|
|
86
|
+ return unWeird;
|
|
87
|
+ }
|
|
88
|
+
|
|
89
|
+ public String toLowerCaseString(String rawData){
|
|
90
|
+ ArrayList<String> upperCase = new ArrayList<>(Arrays.asList("A","B","C","D","E","F","G","H","I","J",
|
|
91
|
+ "K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"));
|
|
92
|
+ ArrayList<String> lowerCase = new ArrayList<>(Arrays.asList("a","b","c","d","e","f","g","h","i","j",
|
|
93
|
+ "k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"));
|
|
94
|
+ Pattern pattern = Pattern.compile("A");
|
|
95
|
+ Matcher matcher = pattern.matcher(rawData);
|
|
96
|
+ String lower = matcher.replaceAll("a");
|
|
97
|
+ for(int i = 1; i<upperCase.size(); i++){
|
|
98
|
+ matcher.reset(lower);
|
|
99
|
+ matcher.usePattern(Pattern.compile(upperCase.get(i)));
|
|
100
|
+ lower = matcher.replaceAll(lowerCase.get(i));
|
|
101
|
+ }
|
|
102
|
+ return lower;
|
|
103
|
+ }
|
|
104
|
+
|
|
105
|
+ public String remove0FromCookie(String rawData){
|
|
106
|
+ Pattern pattern = Pattern.compile("c..kie");
|
|
107
|
+ Matcher matcher = pattern.matcher(rawData);
|
|
108
|
+ return matcher.replaceAll("cookie");
|
|
109
|
+
|
|
110
|
+ }
|
29
|
111
|
|
30
|
112
|
|
31
|
113
|
}
|