|
@@ -2,30 +2,169 @@ package io.zipcoder;
|
2
|
2
|
|
3
|
3
|
import java.util.ArrayList;
|
4
|
4
|
import java.util.Arrays;
|
|
5
|
+import java.util.LinkedHashMap;
|
|
6
|
+import java.util.Map;
|
|
7
|
+import java.util.regex.Matcher;
|
|
8
|
+import java.util.regex.Pattern;
|
5
|
9
|
|
6
|
10
|
public class ItemParser {
|
|
11
|
+ private ArrayList<String> values;
|
|
12
|
+ private ArrayList<Item> itemList;
|
|
13
|
+ private int exceptionCount;
|
7
|
14
|
|
|
15
|
+ ItemParser(String rawList) {
|
|
16
|
+ this.values = parseRawDataIntoStringArray(rawList);
|
|
17
|
+ this.itemList = new ArrayList<Item>();
|
|
18
|
+ buildItemList();
|
|
19
|
+ }
|
8
|
20
|
|
9
|
|
- public ArrayList<String> parseRawDataIntoStringArray(String rawData){
|
|
21
|
+ public ArrayList<String> parseRawDataIntoStringArray(String rawData) {
|
10
|
22
|
String stringPattern = "##";
|
11
|
|
- ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawData);
|
|
23
|
+ ArrayList<String> response = splitStringWithRegexPattern(stringPattern, rawData);
|
12
|
24
|
return response;
|
13
|
25
|
}
|
14
|
26
|
|
15
|
|
- public Item parseStringIntoItem(String rawItem) throws ItemParseException{
|
16
|
|
- return null;
|
|
27
|
+ public Item parseStringIntoItem(String rawItem) throws ItemParseException {
|
|
28
|
+ LinkedHashMap<String, String> item = getKeyValueEntries(rawItem);
|
|
29
|
+ if (item.get(0) == null || item.get(1) == null) {
|
|
30
|
+ throw new ItemParseException();
|
|
31
|
+ }
|
|
32
|
+ Item newItem = new Item(fixAnyZeros(item.get(0)), Double.valueOf(item.get(1)), item.get(2), item.get(3));
|
|
33
|
+ return newItem;
|
|
34
|
+ }
|
|
35
|
+
|
|
36
|
+ public String fixAnyZeros(String s) {
|
|
37
|
+ Pattern pattern = Pattern.compile("[0]");
|
|
38
|
+ Matcher matcher = pattern.matcher(s);
|
|
39
|
+ return matcher.replaceAll("o");
|
|
40
|
+ }
|
|
41
|
+
|
|
42
|
+ public LinkedHashMap<String, String> getKeyValueEntries(String valuePair) {
|
|
43
|
+ ArrayList<String> keyValuePairs = findKeyValuePairsInRawItemData(valuePair);
|
|
44
|
+ LinkedHashMap<String, String> itemDescription = new LinkedHashMap<String, String>();
|
|
45
|
+ for (String keyValues : keyValuePairs) {
|
|
46
|
+ ArrayList<String> keyAndValue = splitKeysAndValue(keyValues);
|
|
47
|
+ itemDescription.put(convertToLowerCase(keyAndValue.get(0)), convertToLowerCase(keyAndValue.get(1)));
|
|
48
|
+ }
|
|
49
|
+ return itemDescription;
|
|
50
|
+ }
|
|
51
|
+
|
|
52
|
+ private String convertToLowerCase(String s) {
|
|
53
|
+ StringBuilder loweredString = new StringBuilder();
|
|
54
|
+ for (int i = 0; i < s.length(); i++) {
|
|
55
|
+ if (s.charAt(i) >= 65 && s.charAt(i) <= 90) {
|
|
56
|
+ loweredString.append(s.charAt(i) + 32);
|
|
57
|
+ } else {
|
|
58
|
+ loweredString.append(s.charAt(i));
|
|
59
|
+ }
|
|
60
|
+ }
|
|
61
|
+ return loweredString.toString();
|
17
|
62
|
}
|
18
|
63
|
|
19
|
|
- public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){
|
|
64
|
+ public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem) {
|
20
|
65
|
String stringPattern = "[;|^]";
|
21
|
|
- ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawItem);
|
|
66
|
+ ArrayList<String> response = splitStringWithRegexPattern(stringPattern, rawItem);
|
|
67
|
+ return response;
|
|
68
|
+ }
|
|
69
|
+
|
|
70
|
+ public ArrayList<String> splitKeysAndValue(String rawItem) {
|
|
71
|
+ String keyValueSeparators = "[:|@|*|%]";
|
|
72
|
+ ArrayList<String> response = splitStringWithRegexPattern(keyValueSeparators, rawItem);
|
22
|
73
|
return response;
|
23
|
74
|
}
|
24
|
75
|
|
25
|
|
- private ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString){
|
|
76
|
+ private ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString) {
|
26
|
77
|
return new ArrayList<String>(Arrays.asList(inputString.split(stringPattern)));
|
27
|
78
|
}
|
28
|
79
|
|
|
80
|
+ public void buildItemList() {
|
|
81
|
+ for (String itemDetails : values) {
|
|
82
|
+ try {
|
|
83
|
+ this.itemList.add(parseStringIntoItem(itemDetails));
|
|
84
|
+ } catch (ItemParseException e) {
|
|
85
|
+ incrementCounter();
|
|
86
|
+ }
|
|
87
|
+ }
|
|
88
|
+ }
|
29
|
89
|
|
|
90
|
+ private void incrementCounter() {
|
|
91
|
+ this.exceptionCount++;
|
|
92
|
+ }
|
|
93
|
+
|
|
94
|
+ public String getFormattedString() {
|
|
95
|
+ ArrayList<LinkedHashMap<String, Integer>> itemSeenInfo = new ArrayList<LinkedHashMap<String, Integer>>();
|
|
96
|
+ for (Item item : itemList) {
|
|
97
|
+ if (containsCheck(itemSeenInfo, item.getName()) == false) {
|
|
98
|
+ itemSeenInfo.add(getOccurrences(item.getName(), itemList));
|
|
99
|
+ }
|
|
100
|
+ }
|
|
101
|
+ StringBuilder output = new StringBuilder();
|
|
102
|
+
|
|
103
|
+ for (LinkedHashMap<String, Integer> item : itemSeenInfo) {
|
|
104
|
+ for (Map.Entry entry : item.entrySet()) {
|
|
105
|
+ if (checkIfName(entry.getKey())) {
|
|
106
|
+ output.append(String.format("Name: %-10s \t\t\t " +
|
|
107
|
+ "Seen: %5d\n================ \t\t\t " +
|
|
108
|
+ "===========\n", entry.getKey(), entry.getValue()));
|
|
109
|
+ } else {
|
|
110
|
+ output.append(String.format("Price: %-5s \t\t\t " +
|
|
111
|
+ "Seen: %5d\n---------------- \t\t\t " +
|
|
112
|
+ "-----------\n", entry.getKey(), entry.getValue()));
|
|
113
|
+ }
|
|
114
|
+ }
|
|
115
|
+ }
|
|
116
|
+ output.append("Errors \t\t\t Seen: " + exceptionCount);
|
|
117
|
+ return output.toString();
|
|
118
|
+ }
|
|
119
|
+
|
|
120
|
+ private boolean checkIfName(Object key) {
|
|
121
|
+ Pattern numbers = Pattern.compile("\\d");
|
|
122
|
+ Matcher matcher = numbers.matcher(key.toString());
|
|
123
|
+ if (matcher.find() == true) {
|
|
124
|
+ return false;
|
|
125
|
+ }
|
|
126
|
+ return true;
|
|
127
|
+ }
|
|
128
|
+
|
|
129
|
+ private boolean containsCheck(ArrayList<LinkedHashMap<String, Integer>> itemSeenInfo, String targetItem) {
|
|
130
|
+ for (LinkedHashMap<String, Integer> item : itemSeenInfo) {
|
|
131
|
+ for (Map.Entry entry : item.entrySet()) {
|
|
132
|
+ if (entry.getKey().equals(targetItem)) {
|
|
133
|
+ return true;
|
|
134
|
+ }
|
|
135
|
+ }
|
|
136
|
+ }
|
|
137
|
+ return false;
|
|
138
|
+ }
|
|
139
|
+
|
|
140
|
+ public LinkedHashMap<String, Integer> getOccurrences(String name, ArrayList<Item> items) {
|
|
141
|
+ LinkedHashMap<String, Integer> itemOcc = new LinkedHashMap<String, Integer>();
|
|
142
|
+ ArrayList<Double> prices = new ArrayList<Double>();
|
|
143
|
+ int nameCount = 0;
|
|
144
|
+ for (Item item : items) {
|
|
145
|
+ if (item.getName().equals(name)) {
|
|
146
|
+ nameCount++;
|
|
147
|
+ prices.add(item.getPrice());
|
|
148
|
+ }
|
|
149
|
+ }
|
|
150
|
+ itemOcc.put(name, nameCount);
|
|
151
|
+ for (Double price : prices) {
|
|
152
|
+ if (!itemOcc.containsKey(price.toString())) {
|
|
153
|
+ int priceOcc = getPriceOccurrences(price, prices);
|
|
154
|
+ itemOcc.put(price.toString(), priceOcc);
|
|
155
|
+ }
|
|
156
|
+ }
|
|
157
|
+ return itemOcc;
|
|
158
|
+ }
|
|
159
|
+
|
|
160
|
+ public int getPriceOccurrences(Double targetPrice, ArrayList<Double> prices) {
|
|
161
|
+ int priceCount = 0;
|
|
162
|
+ for (Double price : prices) {
|
|
163
|
+ if (price == targetPrice) {
|
|
164
|
+ priceCount++;
|
|
165
|
+ }
|
|
166
|
+ }
|
|
167
|
+ return priceCount;
|
|
168
|
+ }
|
30
|
169
|
|
31
|
170
|
}
|