|
@@ -1,10 +1,19 @@
|
1
|
1
|
package io.zipcoder;
|
2
|
2
|
|
3
|
|
-import java.util.ArrayList;
|
4
|
|
-import java.util.Arrays;
|
|
3
|
+import java.util.*;
|
|
4
|
+import java.util.regex.Matcher;
|
|
5
|
+import java.util.regex.Pattern;
|
5
|
6
|
|
6
|
7
|
public class ItemParser {
|
7
|
8
|
|
|
9
|
+ private Map<NamePricePair, Integer> pairCountMap;
|
|
10
|
+ private Map<String, Integer> nameCountMap;
|
|
11
|
+ private int exceptionCount = 0;
|
|
12
|
+
|
|
13
|
+ public ItemParser() {
|
|
14
|
+ pairCountMap = new LinkedHashMap<>();
|
|
15
|
+ nameCountMap = new LinkedHashMap<>();
|
|
16
|
+ }
|
8
|
17
|
|
9
|
18
|
public ArrayList<String> parseRawDataIntoStringArray(String rawData){
|
10
|
19
|
String stringPattern = "##";
|
|
@@ -13,19 +22,116 @@ public class ItemParser {
|
13
|
22
|
}
|
14
|
23
|
|
15
|
24
|
public Item parseStringIntoItem(String rawItem) throws ItemParseException{
|
16
|
|
- return null;
|
|
25
|
+ rawItem = splitStringWithRegexPattern("##", rawItem).get(0);
|
|
26
|
+ List<String> rawKeyValue = findKeyValuePairsInRawItemData(rawItem);
|
|
27
|
+ String name = replaceZero(toLower(findValue(rawKeyValue.get(0))));
|
|
28
|
+ Double price = Double.parseDouble(findValue(rawKeyValue.get(1)));
|
|
29
|
+ if (name.equals("0") || price == 0) {
|
|
30
|
+ exceptionCount++;
|
|
31
|
+ throw new ItemParseException();
|
|
32
|
+ } else {
|
|
33
|
+ addToPairCountMap(new NamePricePair(name, price));
|
|
34
|
+ addToNameCountMap(name);
|
|
35
|
+ }
|
|
36
|
+ String type = toLower(findValue(rawKeyValue.get(2)));
|
|
37
|
+ String expiration = toLower(findValue(rawKeyValue.get(3)));
|
|
38
|
+ return new Item(name, price, type, expiration);
|
|
39
|
+ }
|
|
40
|
+ public int addToPairCountMap(NamePricePair pair) {
|
|
41
|
+ int count = pairCountMap.containsKey(pair) ? pairCountMap.get(pair) : 0;
|
|
42
|
+ pairCountMap.put(pair, count + 1);
|
|
43
|
+ return count + 1;
|
|
44
|
+ }
|
|
45
|
+
|
|
46
|
+ public int addToNameCountMap(String name) {
|
|
47
|
+ int count = nameCountMap.containsKey(name) ? nameCountMap.get(name) : 0;
|
|
48
|
+ nameCountMap.put(name, count + 1);
|
|
49
|
+ return count + 1;
|
|
50
|
+ }
|
|
51
|
+ public String findValue(String pair) {
|
|
52
|
+ Pattern pttrn = Pattern.compile(":(.+)");
|
|
53
|
+ Matcher mtchr = pttrn.matcher(pair);
|
|
54
|
+ return mtchr.find() ? mtchr.group(1) : "0";
|
17
|
55
|
}
|
18
|
56
|
|
|
57
|
+ public String replaceZero(String name) {
|
|
58
|
+ return name.equals("co0kies") ? "cookies" : name;
|
|
59
|
+ }
|
19
|
60
|
public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){
|
20
|
|
- String stringPattern = "[;|^]";
|
|
61
|
+ String stringPattern = "[;|^!*^%@]";
|
21
|
62
|
ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawItem);
|
22
|
63
|
return response;
|
23
|
64
|
}
|
24
|
|
-
|
25
|
65
|
private ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString){
|
26
|
66
|
return new ArrayList<String>(Arrays.asList(inputString.split(stringPattern)));
|
27
|
67
|
}
|
|
68
|
+ private String countSpaces(int nameLength) {
|
|
69
|
+ String spaces ="";
|
|
70
|
+ for (int i = 0; i < 8 - nameLength; i++) {
|
|
71
|
+ spaces += " ";
|
|
72
|
+ }
|
|
73
|
+ return spaces;
|
|
74
|
+ }
|
|
75
|
+ private String toLower(String string)
|
|
76
|
+ {
|
|
77
|
+ String result="";
|
|
78
|
+ for(int i=0;i<string.length();i++)
|
|
79
|
+ {
|
|
80
|
+ int c=(int)string.charAt(i);
|
|
81
|
+ if(c>=65&&c<=90)
|
|
82
|
+ {
|
|
83
|
+ c=c+32;
|
|
84
|
+ }
|
|
85
|
+ result=result+(char)c;
|
|
86
|
+ }
|
|
87
|
+ return result;
|
|
88
|
+ }
|
|
89
|
+ private String toCamel(String string) {
|
|
90
|
+ String result="";
|
|
91
|
+ result=result+(char)((int)string.charAt(0)-32);
|
|
92
|
+ for(int i=1;i<string.length();i++) {
|
|
93
|
+ int c = (int)string.charAt(i);
|
|
94
|
+ result = result + (char)c;
|
|
95
|
+ }
|
|
96
|
+ return result;
|
|
97
|
+ }
|
28
|
98
|
|
29
|
99
|
|
30
|
100
|
|
|
101
|
+
|
|
102
|
+ public void printFormatted() {
|
|
103
|
+
|
|
104
|
+ Iterator<Map.Entry<String, Integer>> nameIterator = nameCountMap.entrySet().iterator();
|
|
105
|
+ while (nameIterator.hasNext()) {
|
|
106
|
+ int seenCounter = 0;
|
|
107
|
+ Map.Entry<String, Integer> nameEntry = nameIterator.next();
|
|
108
|
+ System.out.println("name:" + countSpaces(nameEntry.getKey().length()) + toCamel(nameEntry.getKey()) + " \t\t seen: " + nameEntry.getValue() + " times");
|
|
109
|
+ System.out.println("============= \t \t =============");
|
|
110
|
+ Iterator<Map.Entry<NamePricePair, Integer>> pairIterator = pairCountMap.entrySet().iterator();
|
|
111
|
+ while (pairIterator.hasNext()) {
|
|
112
|
+ Map.Entry<NamePricePair, Integer> pairEntry = pairIterator.next();
|
|
113
|
+ if (pairEntry.getKey().getName().equals(nameEntry.getKey())) {
|
|
114
|
+ System.out.println("Price: \t "+pairEntry.getKey().getPrice()+"\t\t seen: "+pairEntry.getValue()+" times");
|
|
115
|
+ if (nameEntry.getValue() - pairEntry.getValue() == 0) {
|
|
116
|
+ System.out.println("-------------\t\t -------------");
|
|
117
|
+ System.out.println();
|
|
118
|
+ } else if (nameEntry.getValue() - seenCounter != pairEntry.getValue()) {
|
|
119
|
+ seenCounter += pairEntry.getValue();
|
|
120
|
+ System.out.println("-------------\t\t -------------");
|
|
121
|
+ } else {
|
|
122
|
+ System.out.println();
|
|
123
|
+ }
|
|
124
|
+ }
|
|
125
|
+ }
|
|
126
|
+ }
|
|
127
|
+ }
|
|
128
|
+
|
|
129
|
+ public void printExceptions() {
|
|
130
|
+ System.out.println("Errors \t \t seen: "+ exceptionCount +" times");
|
|
131
|
+
|
|
132
|
+ }
|
31
|
133
|
}
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
|