|
@@ -1,31 +1,196 @@
|
1
|
1
|
package io.zipcoder;
|
2
|
2
|
|
3
|
|
-import java.util.ArrayList;
|
4
|
|
-import java.util.Arrays;
|
|
3
|
+import java.io.BufferedReader;
|
|
4
|
+import java.io.File;
|
|
5
|
+import java.io.FileReader;
|
|
6
|
+import java.io.IOException;
|
|
7
|
+import java.lang.reflect.Array;
|
|
8
|
+import java.util.*;
|
|
9
|
+import java.util.regex.Matcher;
|
|
10
|
+import java.util.regex.Pattern;
|
5
|
11
|
|
6
|
12
|
public class ItemParser {
|
7
|
13
|
|
|
14
|
+ private int numberOfExceptions;
|
8
|
15
|
|
9
|
|
- public ArrayList<String> parseRawDataIntoStringArray(String rawData){
|
|
16
|
+ public String readFromFile(String filePath) {
|
|
17
|
+ try {
|
|
18
|
+ File file = new File(filePath);
|
|
19
|
+ BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
|
|
20
|
+ String stringFile = "";
|
|
21
|
+ String line;
|
|
22
|
+ while ((line = bufferedReader.readLine()) != null) {
|
|
23
|
+ stringFile += line + "\n";
|
|
24
|
+ }
|
|
25
|
+ bufferedReader.close();
|
|
26
|
+ return stringFile.trim();
|
|
27
|
+ } catch (IOException e) {
|
|
28
|
+ e.printStackTrace();
|
|
29
|
+ return null;
|
|
30
|
+ }
|
|
31
|
+ }
|
|
32
|
+
|
|
33
|
+ public ArrayList<String> parseRawDataIntoStringArray(String rawData) {
|
10
|
34
|
String stringPattern = "##";
|
11
|
|
- ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawData);
|
|
35
|
+ ArrayList<String> response = splitStringWithRegexPattern(stringPattern, rawData);
|
12
|
36
|
return response;
|
13
|
37
|
}
|
14
|
38
|
|
15
|
|
- public Item parseStringIntoItem(String rawItem) throws ItemParseException{
|
16
|
|
- return null;
|
|
39
|
+ public Item parseStringIntoItem(String rawItem) throws ItemParseException {
|
|
40
|
+ List<String> keyValues = findKeyValuePairsInRawItemData(rawItem);
|
|
41
|
+
|
|
42
|
+ String name = findValue(keyValues.get(0));
|
|
43
|
+ double price = Double.valueOf(findValue(keyValues.get(1)));
|
|
44
|
+ String type = findValue(keyValues.get(2));
|
|
45
|
+ String expiration = findValue(keyValues.get(3));
|
|
46
|
+
|
|
47
|
+ return new Item(convertZeroTo0(name), price, type, expiration);
|
|
48
|
+ }
|
|
49
|
+
|
|
50
|
+ public List<Item> parseRawDataIntoItems(String rawData) {
|
|
51
|
+ List<Item> itemList = new ArrayList<>();
|
|
52
|
+
|
|
53
|
+ List<String> items = parseRawDataIntoStringArray(rawData);
|
|
54
|
+
|
|
55
|
+ for (String item : items) {
|
|
56
|
+ try {
|
|
57
|
+ itemList.add(parseStringIntoItem(item));
|
|
58
|
+ } catch (ItemParseException e) {
|
|
59
|
+ numberOfExceptions++;
|
|
60
|
+ }
|
|
61
|
+ }
|
|
62
|
+ return itemList;
|
|
63
|
+ }
|
|
64
|
+
|
|
65
|
+ public String findValue(String keyValue) throws ItemParseException {
|
|
66
|
+ List<String> keyAndValue = splitStringWithRegexPattern(":", keyValue);
|
|
67
|
+
|
|
68
|
+ if (keyAndValue.size() > 1) {
|
|
69
|
+ return convertToLowerCase(keyAndValue.get(1));
|
|
70
|
+ } else {
|
|
71
|
+ throw new ItemParseException();
|
|
72
|
+ }
|
|
73
|
+ }
|
|
74
|
+
|
|
75
|
+ public Map<String, Integer> itemCounts(List<Item> items) {
|
|
76
|
+ Map<String, Integer> itemMap = new HashMap<>();
|
|
77
|
+
|
|
78
|
+ for (Item item : items) {
|
|
79
|
+ if (!itemMap.containsKey(item.getName())) {
|
|
80
|
+ itemMap.put(item.getName(), 1);
|
|
81
|
+ } else {
|
|
82
|
+ itemMap.put(item.getName(), itemMap.get(item.getName()) + 1);
|
|
83
|
+ }
|
|
84
|
+ }
|
|
85
|
+
|
|
86
|
+ return itemMap;
|
|
87
|
+ }
|
|
88
|
+
|
|
89
|
+ public Map<Map<String, Double>, Integer> priceCounts(List<Item> items) {
|
|
90
|
+ Map<String, Double> namePrice = new HashMap<>();
|
|
91
|
+ Map<Map<String, Double>, Integer> itemMap = new HashMap<>();
|
|
92
|
+
|
|
93
|
+ for (Item item : items) {
|
|
94
|
+ if (!namePrice.containsKey(item.getName())) {
|
|
95
|
+ namePrice.put(item.getName(), item.getPrice());
|
|
96
|
+ }
|
|
97
|
+ }
|
|
98
|
+ // im tired
|
|
99
|
+ return itemMap;
|
|
100
|
+ }
|
|
101
|
+
|
|
102
|
+ public void printOutput(Map<String, Integer> itemMap) {
|
|
103
|
+ for (String name : itemMap.keySet()) {
|
|
104
|
+ System.out.println(String.format("name: %7s \t\t seen: %s times", capitalizeFirst(name), itemMap.get(name)));
|
|
105
|
+ System.out.println("============= \t\t =============");
|
|
106
|
+ }
|
|
107
|
+ System.out.println(String.format("\nErrors \t\t\t\t seen: %s times", getNumberOfExceptions()));
|
17
|
108
|
}
|
18
|
109
|
|
19
|
|
- public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){
|
20
|
|
- String stringPattern = "[;|^]";
|
21
|
|
- ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawItem);
|
|
110
|
+ public String convertToLowerCase(String input) {
|
|
111
|
+ String output = "";
|
|
112
|
+
|
|
113
|
+ List<String> upperAlpha = splitStringWithRegexPattern("", "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
|
|
114
|
+ List<String> lowerAlpha = splitStringWithRegexPattern("", "abcdefghijklmnopqrstuvwxyz");
|
|
115
|
+ ArrayList<String> stringCharacters = stringToArrayList(input);
|
|
116
|
+
|
|
117
|
+ for (int i = 0; i < stringCharacters.size(); i++) {
|
|
118
|
+ for (int j = 0; j < upperAlpha.size(); j++) {
|
|
119
|
+ if (stringCharacters.get(i).equals(upperAlpha.get(j))) {
|
|
120
|
+ stringCharacters.set(i, lowerAlpha.get(j));
|
|
121
|
+ }
|
|
122
|
+ }
|
|
123
|
+ output += stringCharacters.get(i);
|
|
124
|
+ }
|
|
125
|
+ return output;
|
|
126
|
+ }
|
|
127
|
+
|
|
128
|
+ public String convertZeroTo0(String input) {
|
|
129
|
+ String output = "";
|
|
130
|
+ ArrayList<String> stringCharacters = stringToArrayList(input);
|
|
131
|
+
|
|
132
|
+ for (int i = 0; i < stringCharacters.size(); i++) {
|
|
133
|
+ if (stringCharacters.get(i).equals("0")) {
|
|
134
|
+ stringCharacters.set(i, "o");
|
|
135
|
+ }
|
|
136
|
+ output += stringCharacters.get(i);
|
|
137
|
+ }
|
|
138
|
+ return output;
|
|
139
|
+ }
|
|
140
|
+
|
|
141
|
+ public String capitalizeFirst(String input) {
|
|
142
|
+ String output = "";
|
|
143
|
+ List<String> upperAlpha = splitStringWithRegexPattern("", "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
|
|
144
|
+ List<String> lowerAlpha = splitStringWithRegexPattern("", "abcdefghijklmnopqrstuvwxyz");
|
|
145
|
+ ArrayList<String> stringCharacters = stringToArrayList(input);
|
|
146
|
+
|
|
147
|
+ for (int i = 0; i < stringCharacters.size(); i++) {
|
|
148
|
+ for (int j = 0; j < upperAlpha.size(); j++) {
|
|
149
|
+ if (stringCharacters.get(0).equals(lowerAlpha.get(j))) {
|
|
150
|
+ stringCharacters.set(0, upperAlpha.get(j));
|
|
151
|
+ }
|
|
152
|
+ }
|
|
153
|
+ output += stringCharacters.get(i);
|
|
154
|
+ }
|
|
155
|
+ return output;
|
|
156
|
+ }
|
|
157
|
+
|
|
158
|
+ public ArrayList<String> stringToArrayList(String input) {
|
|
159
|
+ ArrayList<String> stringCharacters = new ArrayList<>();
|
|
160
|
+
|
|
161
|
+ Pattern pattern = Pattern.compile(".");
|
|
162
|
+ Matcher matcher = pattern.matcher(input);
|
|
163
|
+
|
|
164
|
+ while (matcher.find()) {
|
|
165
|
+ stringCharacters.add(matcher.group());
|
|
166
|
+ }
|
|
167
|
+
|
|
168
|
+ return stringCharacters;
|
|
169
|
+ }
|
|
170
|
+
|
|
171
|
+ public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem) {
|
|
172
|
+ String stringPattern = "[^a-zA-Z0-9\\:\\.\\/]";
|
|
173
|
+ ArrayList<String> response = splitStringWithRegexPattern(stringPattern, rawItem);
|
22
|
174
|
return response;
|
23
|
175
|
}
|
24
|
176
|
|
25
|
|
- private ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString){
|
26
|
|
- return new ArrayList<String>(Arrays.asList(inputString.split(stringPattern)));
|
|
177
|
+ private ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString) {
|
|
178
|
+ return new ArrayList<>(Arrays.asList(inputString.split(stringPattern)));
|
27
|
179
|
}
|
28
|
180
|
|
|
181
|
+ public int getNumberOfExceptions() {
|
|
182
|
+ return numberOfExceptions;
|
|
183
|
+ }
|
|
184
|
+
|
|
185
|
+ public static void main(String[] args) {
|
|
186
|
+ ItemParser itemParser = new ItemParser();
|
|
187
|
+
|
|
188
|
+ String rawData = itemParser.readFromFile("src/main/resources/RawData.txt");
|
29
|
189
|
|
|
190
|
+ List<Item> items = itemParser.parseRawDataIntoItems(rawData);
|
30
|
191
|
|
|
192
|
+ Map<String, Integer> itemMap = itemParser.itemCounts(items);
|
|
193
|
+
|
|
194
|
+ itemParser.printOutput(itemMap);
|
|
195
|
+ }
|
31
|
196
|
}
|