|
@@ -2,19 +2,38 @@ package io.zipcoder;
|
2
|
2
|
|
3
|
3
|
import java.util.ArrayList;
|
4
|
4
|
import java.util.Arrays;
|
|
5
|
+import java.util.HashMap;
|
5
|
6
|
|
6
|
7
|
public class ItemParser {
|
7
|
8
|
|
8
|
|
-
|
9
|
9
|
public ArrayList<String> parseRawDataIntoStringArray(String rawData){
|
10
|
10
|
String stringPattern = "##";
|
11
|
11
|
ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawData);
|
12
|
12
|
return response;
|
13
|
13
|
}
|
14
|
14
|
|
15
|
|
- public Item parseStringIntoItem(String rawItem) throws ItemParseException{
|
16
|
|
- return null;
|
|
15
|
+public Item parseStringIntoItem(String rawItem) throws ItemParseException{
|
|
16
|
+ Item item;
|
|
17
|
+ int error = 0;
|
|
18
|
+ String[] splitItem = rawItem.split("[;#^@!%*]");
|
|
19
|
+ try {
|
|
20
|
+ String name = splitItem[0].toLowerCase().replaceAll("^[a-zA-Z0-9]+:", "").replaceAll("0", "o");
|
|
21
|
+ Double price = Double.parseDouble(splitItem[1].replaceAll("^[a-zA-Z0-9]+:", ""));
|
|
22
|
+ String type = splitItem[2].toLowerCase().replaceAll("^[a-zA-Z0-9]+:", "");
|
|
23
|
+ String expiration = splitItem[3].toLowerCase().replaceAll("^[a-zA-Z0-9]+:", "");
|
|
24
|
+
|
|
25
|
+ if (name.isEmpty() || price == null || type.isEmpty()|| expiration.isEmpty())
|
|
26
|
+ throw new Exception();
|
|
27
|
+ error++;
|
|
28
|
+ item = new Item(name, price, type, expiration);
|
|
29
|
+ } catch (Exception e) {
|
|
30
|
+
|
|
31
|
+ throw new ItemParseException();
|
17
|
32
|
}
|
|
33
|
+ System.out.println(item);
|
|
34
|
+ System.out.println(error);
|
|
35
|
+ return item;
|
|
36
|
+}
|
18
|
37
|
|
19
|
38
|
public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){
|
20
|
39
|
String stringPattern = "[;|^]";
|
|
@@ -25,7 +44,15 @@ public class ItemParser {
|
25
|
44
|
private ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString){
|
26
|
45
|
return new ArrayList<String>(Arrays.asList(inputString.split(stringPattern)));
|
27
|
46
|
}
|
28
|
|
-
|
29
|
|
-
|
30
|
|
-
|
|
47
|
+ private String lowerCase(String str){
|
|
48
|
+ String lower = "";
|
|
49
|
+ for (int i = 0; i <str.length() ; i++) {
|
|
50
|
+ int c = (int)str.charAt(i);
|
|
51
|
+ if(c >= 'A' && c <= 'Z'){
|
|
52
|
+ c = c + 32;
|
|
53
|
+ }
|
|
54
|
+ lower += (char) c;
|
|
55
|
+ }
|
|
56
|
+ return lower;
|
|
57
|
+ }
|
31
|
58
|
}
|