Tariq Hook 6 years ago
parent
commit
addf8ab10c

+ 14
- 1
src/main/java/io/zipcoder/ItemParser.java View File

@@ -1,6 +1,7 @@
1 1
 package io.zipcoder;
2 2
 
3 3
 import java.util.ArrayList;
4
+import java.util.Arrays;
4 5
 
5 6
 public class ItemParser {
6 7
 
@@ -11,13 +12,25 @@ public class ItemParser {
11 12
      */
12 13
 
13 14
     public ArrayList<String> parseRawDataIntoStringArray(String rawData){
14
-        return null;
15
+        String stringPattern = "##";
16
+        ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawData);
17
+        return response;
15 18
     }
16 19
 
17 20
     public Item parseStringIntoItem(String rawItem) throws ItemParseException{
18 21
         return null;
19 22
     }
20 23
 
24
+    public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){
25
+        String stringPattern = ";";
26
+        ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawItem);
27
+        return response;
28
+    }
29
+
30
+    private ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString){
31
+        return new ArrayList<String>(Arrays.asList(inputString.split(stringPattern)));
32
+    }
33
+
21 34
 
22 35
 
23 36
 }

+ 15
- 0
src/test/java/io/zipcoder/ItemParserTest.java View File

@@ -12,6 +12,8 @@ public class ItemParserTest {
12 12
 
13 13
     private String rawSingleItem =    "naMe:Milk;price:3.23;type:Food;expiration:1/25/2016##";
14 14
 
15
+    private String rawSingleItemIrregularSeperatorSample = "naMe:MiLK;price:3.23;type:Food^expiration:1/11/2016##";
16
+
15 17
     private String rawBrokenSingleItem =    "naMe:Milk;price:3.23;type:Food;expiration:1/25/2016##";
16 18
 
17 19
     private String rawMultipleItems = "naMe:Milk;price:3.23;type:Food;expiration:1/25/2016##"
@@ -44,4 +46,17 @@ public class ItemParserTest {
44 46
         itemParser.parseStringIntoItem(rawBrokenSingleItem);
45 47
     }
46 48
 
49
+    @Test
50
+    public void findKeyValuePairsInRawItemDataTest(){
51
+        Integer expected = 4;
52
+        Integer actual = itemParser.findKeyValuePairsInRawItemData(rawSingleItem).size();
53
+        assertEquals(expected, actual);
54
+    }
55
+
56
+    @Test
57
+    public void findKeyValuePairsInRawItemDataTestIrregular(){
58
+        Integer expected = 4;
59
+        Integer actual = itemParser.findKeyValuePairsInRawItemData(rawSingleItemIrregularSeperatorSample).size();
60
+        assertEquals(expected, actual);
61
+    }
47 62
 }