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