|
@@ -1,10 +1,16 @@
|
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;
|
|
6
|
+import java.util.stream.Stream;
|
|
7
|
+
|
|
8
|
+import static io.zipcoder.Main.count;
|
5
|
9
|
|
6
|
10
|
public class ItemParser {
|
7
|
11
|
|
|
12
|
+ public ItemParser(){
|
|
13
|
+ }
|
8
|
14
|
|
9
|
15
|
public ArrayList<String> parseRawDataIntoStringArray(String rawData){
|
10
|
16
|
String stringPattern = "##";
|
|
@@ -13,7 +19,27 @@ public class ItemParser {
|
13
|
19
|
}
|
14
|
20
|
|
15
|
21
|
public Item parseStringIntoItem(String rawItem) throws ItemParseException{
|
16
|
|
- return null;
|
|
22
|
+ List<String> list = findKeyValuePairsInRawItemData(rawItem);
|
|
23
|
+ Map<String, String> map = new HashMap<>();
|
|
24
|
+ for (String string : list){
|
|
25
|
+ Pattern keyPattern = Pattern.compile("([a-zA-Z0-9]+)(?=\\:)"); //(.+)(?=[\:])
|
|
26
|
+ Matcher keyMatch = keyPattern.matcher(string);
|
|
27
|
+ Pattern valPattern = Pattern.compile("(?<=[\\:])([^##]+)");
|
|
28
|
+ Matcher valMatch = valPattern.matcher(string);
|
|
29
|
+ if (keyMatch.find() && valMatch.find()) {
|
|
30
|
+ map.put(lower(keyMatch.group()), lower(valMatch.group()));
|
|
31
|
+ }
|
|
32
|
+ }
|
|
33
|
+ try {
|
|
34
|
+ return new Item(rmvZeros(map.get("name")), Double.parseDouble(map.get("price")), map.get("type"), map.get("expiration"));
|
|
35
|
+ } catch (NullPointerException n) {
|
|
36
|
+ throw new ItemParseException();
|
|
37
|
+ }
|
|
38
|
+// Pattern p = Pattern.compile("[^a-zA-Z0-9\\:\\.\\/]");
|
|
39
|
+// Matcher m = p.matcher(rawItem);
|
|
40
|
+// String replaced = m.replaceAll("-");
|
|
41
|
+// String otherPatter = "([a-zA-Z0-9\\/\\:\\.]+)(?=[^\\/])";
|
|
42
|
+
|
17
|
43
|
}
|
18
|
44
|
|
19
|
45
|
public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){
|
|
@@ -23,9 +49,27 @@ public class ItemParser {
|
23
|
49
|
}
|
24
|
50
|
|
25
|
51
|
private ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString){
|
26
|
|
- return new ArrayList<String>(Arrays.asList(inputString.split(stringPattern)));
|
|
52
|
+ return new ArrayList<>(Arrays.asList(inputString.split(stringPattern)));
|
27
|
53
|
}
|
28
|
54
|
|
29
|
55
|
|
|
56
|
+ private String lower(String input) {
|
|
57
|
+ StringBuilder inputLineT = new StringBuilder(input);
|
|
58
|
+ for (int i = 0; i < inputLineT.length(); i++) {
|
|
59
|
+ if (inputLineT.charAt(i) >= 65 && inputLineT.charAt(i) < 91 && i > 0) {
|
|
60
|
+ inputLineT.setCharAt(i, (char) (inputLineT.charAt(i) + 32));
|
|
61
|
+ }
|
|
62
|
+ }
|
|
63
|
+ return inputLineT.toString();
|
|
64
|
+ }
|
30
|
65
|
|
|
66
|
+ private String rmvZeros(String input) {
|
|
67
|
+ StringBuilder inputLineT = new StringBuilder(input);
|
|
68
|
+ for (int i = 0; i < inputLineT.length(); i++) {
|
|
69
|
+ if (inputLineT.charAt(i) == 48) {
|
|
70
|
+ inputLineT.setCharAt(i, 'o');
|
|
71
|
+ }
|
|
72
|
+ }
|
|
73
|
+ return inputLineT.toString();
|
|
74
|
+ }
|
31
|
75
|
}
|