|
@@ -1,86 +1,110 @@
|
1
|
1
|
package io.zipcoder;
|
2
|
2
|
|
3
|
|
-import java.util.ArrayList;
|
4
|
|
-import java.util.Arrays;
|
5
|
|
-import java.util.Map;
|
6
|
|
-import java.util.Scanner;
|
|
3
|
+import java.util.*;
|
7
|
4
|
import java.util.regex.Matcher;
|
8
|
5
|
import java.util.regex.Pattern;
|
9
|
6
|
|
10
|
7
|
public class ItemParser {
|
|
8
|
+ public HashMap<String, ArrayList<Item>> completeList = new HashMap<String, ArrayList<Item>>();
|
|
9
|
+ int exceptionCounter = 0;
|
11
|
10
|
|
12
|
11
|
|
13
|
|
- public ArrayList<String> parseRawDataIntoStringArray(String rawData){
|
|
12
|
+ public ArrayList<String> parseRawDataIntoStringArray(String rawData) {
|
14
|
13
|
String stringPattern = "##";
|
15
|
|
- ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawData);
|
|
14
|
+ ArrayList<String> response = splitStringWithRegexPattern(stringPattern, rawData);
|
16
|
15
|
return response;
|
17
|
16
|
}
|
18
|
17
|
|
19
|
|
- public Item parseStringIntoItem(String rawItem) throws ItemParseException{
|
|
18
|
+ public Item parseStringIntoItem(String rawItem) throws ItemParseException {
|
|
19
|
+ if (nameFinder(rawItem) == null || priceFinder(rawItem) == null) {
|
|
20
|
+ throw new ItemParseException();
|
|
21
|
+ }
|
20
|
22
|
|
21
|
23
|
String name = nameFinder(rawItem);
|
22
|
24
|
Double price = Double.parseDouble(priceFinder(rawItem));
|
23
|
25
|
String type = "food";
|
24
|
26
|
String expiration = expFinder(rawItem);
|
25
|
|
- return new Item(name, price, type, expiration);
|
26
|
|
-
|
27
|
27
|
|
|
28
|
+ return new Item(name, price, type, expiration);
|
28
|
29
|
}
|
29
|
30
|
|
30
|
|
- public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){
|
|
31
|
+ public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem) {
|
31
|
32
|
String stringPattern = "[;|^]";
|
32
|
|
- ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawItem);
|
|
33
|
+ ArrayList<String> response = splitStringWithRegexPattern(stringPattern, rawItem);
|
33
|
34
|
return response;
|
34
|
35
|
}
|
35
|
36
|
|
36
|
|
- private ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString){
|
|
37
|
+ private ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString) {
|
37
|
38
|
return new ArrayList<String>(Arrays.asList(inputString.split(stringPattern)));
|
38
|
39
|
}
|
39
|
40
|
|
40
|
|
- public String nameFinder (String rawNames) throws Exception {
|
|
41
|
+ public String nameFinder(String rawNames) {
|
41
|
42
|
StringBuilder found = new StringBuilder();
|
42
|
43
|
|
43
|
44
|
String itemSearch = rawNames;
|
44
|
45
|
Pattern foodNames = Pattern.compile("\\w+(?=\\Wprice)", Pattern.CASE_INSENSITIVE);
|
45
|
46
|
Matcher m = foodNames.matcher(itemSearch);
|
46
|
47
|
int lastMatchPos = 0;
|
47
|
|
- while (m.find())
|
48
|
|
- {
|
49
|
|
- found.append(m.group()+"\n");
|
|
48
|
+ while (m.find()) {
|
|
49
|
+ if (!m.group().equals("")) {
|
|
50
|
+ found.append(m.group().replaceAll("\\d", "o"));
|
50
|
51
|
lastMatchPos = m.end();
|
51
|
52
|
}
|
52
|
|
- return found.toString();
|
|
53
|
+ return found.toString().toLowerCase();
|
|
54
|
+ }
|
|
55
|
+ return null;
|
53
|
56
|
}
|
54
|
57
|
|
55
|
|
- public String priceFinder (String rawPrices) throws Exception {
|
|
58
|
+ public String priceFinder(String rawPrices) {
|
56
|
59
|
StringBuilder found = new StringBuilder();
|
57
|
60
|
|
58
|
61
|
String itemSearch = rawPrices;
|
59
|
62
|
Pattern foodNames = Pattern.compile("\\d+\\.\\d\\d", Pattern.CASE_INSENSITIVE);
|
60
|
63
|
Matcher m = foodNames.matcher(itemSearch);
|
61
|
64
|
int lastMatchPos = 0;
|
62
|
|
- while (m.find())
|
63
|
|
- {
|
64
|
|
- found.append(m.group()+"\n");
|
65
|
|
- lastMatchPos = m.end();
|
|
65
|
+ while (m.find()) {
|
|
66
|
+ if (!m.group().equals("")) {
|
|
67
|
+ found.append(m.group() + "");
|
|
68
|
+ lastMatchPos = m.end();
|
|
69
|
+ }
|
|
70
|
+ return found.toString();
|
66
|
71
|
}
|
67
|
|
- return found.toString();
|
|
72
|
+ return null;
|
68
|
73
|
}
|
69
|
74
|
|
70
|
|
- public String expFinder (String rawExp) throws Exception {
|
|
75
|
+ public String expFinder(String rawExp) {
|
71
|
76
|
StringBuilder found = new StringBuilder();
|
72
|
77
|
|
73
|
78
|
String itemSearch = rawExp;
|
74
|
79
|
Pattern foodNames = Pattern.compile("\\d{1,2}\\/\\d\\d\\/\\d{4}", Pattern.CASE_INSENSITIVE);
|
75
|
80
|
Matcher m = foodNames.matcher(itemSearch);
|
76
|
81
|
int lastMatchPos = 0;
|
77
|
|
- while (m.find())
|
78
|
|
- {
|
79
|
|
- found.append(m.group()+"\n");
|
|
82
|
+ while (m.find()) {
|
|
83
|
+ found.append(m.group() + "");
|
80
|
84
|
lastMatchPos = m.end();
|
81
|
85
|
}
|
82
|
86
|
return found.toString();
|
83
|
87
|
}
|
84
|
88
|
|
85
|
89
|
|
|
90
|
+ public HashMap<String, ArrayList<Item>> getCompleteList() throws Exception {
|
|
91
|
+ Main mainObject = new Main();
|
|
92
|
+ ArrayList<String> rawinputdata = parseRawDataIntoStringArray(mainObject.readRawDataToString());
|
|
93
|
+ for (String item : rawinputdata) {
|
|
94
|
+ try {
|
|
95
|
+ Item myItem = parseStringIntoItem(item);
|
|
96
|
+ if (!completeList.containsKey(myItem.getName())) {
|
|
97
|
+ ArrayList<Item> myItemList = new ArrayList<Item>();
|
|
98
|
+ myItemList.add(myItem);
|
|
99
|
+ completeList.put(myItem.getName(), myItemList);
|
|
100
|
+ } else {
|
|
101
|
+ completeList.get(myItem.getName()).add(myItem);
|
|
102
|
+ }
|
|
103
|
+
|
|
104
|
+ } catch (ItemParseException except) {
|
|
105
|
+ exceptionCounter++;
|
|
106
|
+ }
|
|
107
|
+ }
|
|
108
|
+ return completeList;
|
|
109
|
+ }
|
86
|
110
|
}
|