#1 JerkSON-Parser

Open
Soujanya wants to merge 1 commits from Soujanya/JerkSON-Parser:master into master

+ 2
- 1
src/main/java/io/zipcoder/ItemParseException.java View File

@@ -1,4 +1,5 @@
1 1
 package io.zipcoder;
2 2
 
3
-public class ItemParseException extends Exception {
3
+public class ItemParseException extends Exception
4
+{
4 5
 }

+ 77
- 3
src/main/java/io/zipcoder/ItemParser.java View File

@@ -2,6 +2,8 @@ package io.zipcoder;
2 2
 
3 3
 import java.util.ArrayList;
4 4
 import java.util.Arrays;
5
+import java.util.regex.Matcher;
6
+import java.util.regex.Pattern;
5 7
 
6 8
 public class ItemParser {
7 9
 
@@ -12,12 +14,43 @@ public class ItemParser {
12 14
         return response;
13 15
     }
14 16
 
15
-    public Item parseStringIntoItem(String rawItem) throws ItemParseException{
16
-        return null;
17
+    public Item parseStringIntoItem(String rawItem) throws ItemParseException
18
+    {
19
+        ArrayList<String> values = getValues(toLowerCase(rawItem));
20
+
21
+        String name = values.get(0);
22
+        Double price = Double.valueOf(values.get(1));
23
+        String type = values.get(2);
24
+        String expiration = values.get(3);
25
+
26
+        return new Item(name,price,type,expiration);
27
+
28
+    }
29
+    private String getValue(String input)
30
+    {
31
+        return splitStringWithRegexPattern(":" , input).get(1);
32
+    }
33
+
34
+    private ArrayList<String> getValues(String rawItem) throws ItemParseException {
35
+        ArrayList<String> KVPairs = findKeyValuePairsInRawItemData(rawItem);
36
+        ArrayList<String> values = new ArrayList<String>();
37
+        for(String pair : KVPairs)
38
+        {
39
+            String value = getValue(pair);
40
+            if(value .equals(""))
41
+            {
42
+                throw  new ItemParseException();
43
+            }
44
+            else
45
+            {
46
+                values.add(value);
47
+            }
48
+        }
49
+        return values;
17 50
     }
18 51
 
19 52
     public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){
20
-        String stringPattern = "[;|^]";
53
+        String stringPattern = "[^a-zA-Z0-9\\:\\.\\/]";
21 54
         ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawItem);
22 55
         return response;
23 56
     }
@@ -26,6 +59,47 @@ public class ItemParser {
26 59
         return new ArrayList<String>(Arrays.asList(inputString.split(stringPattern)));
27 60
     }
28 61
 
62
+    public String toLowerCase(String test)
63
+    {
64
+        String[] lower = {"a","b","c","d","e","f","g","h","i","j","k","l","m","o","q","s","u","v","w","x","y","z"};
65
+        String[] upper = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
66
+
67
+        StringBuilder lowercase = new StringBuilder();
29 68
 
69
+            Pattern pattern =Pattern.compile(".");
70
+            Matcher matcher = pattern.matcher(test);
71
+
72
+
73
+            while(matcher.find())
74
+            {
75
+                String character = matcher.group();
76
+                if(isUpper(character))
77
+                {
78
+                    int i = indexOf(upper,character);
79
+                            lowercase.append(lower[i]);
80
+                }else {
81
+                    lowercase.append(character);
82
+                }
83
+            }
84
+            return lowercase.toString();
85
+    }
86
+
87
+    public boolean isUpper(String c)
88
+    {
89
+        Pattern pattern = Pattern.compile("[A-Z]");
90
+        Matcher matcher = pattern.matcher(c);
91
+        return matcher.find();
92
+    }
93
+    public int indexOf(String[] array,String c)
94
+    {
95
+        for(int i =0; i<array.length;i++)
96
+        {
97
+            if(array[i].equals(c))
98
+            {
99
+                return i;
100
+            }
101
+        }
102
+        return -1;
103
+    }
30 104
 
31 105
 }

+ 28
- 0
src/main/resources/RawDataEdit.txt View File

@@ -0,0 +1,28 @@
1
+naMe:Milk;price:3.23;type:Food;expiration:1/25/2016##
2
+naME:BreaD;price:1.23;type:Food;expiration:1/02/2016##
3
+NAMe:BrEAD;price:1.23;type:Food;expiration:2/25/2016##
4
+naMe:MiLK;price:3.23;type:Food^expiration:1/11/2016##
5
+naMe:Cookies;price:2.25;type:Food%expiration:1/25/2016##
6
+naMe:CoOkieS;price:2.25;type:Food*expiration:1/25/2016##
7
+naMe:COokIes;price:2.25;type:Food;expiration:3/22/2016##
8
+naMe:COOkieS;price:2.25;type:Food;expiration:1/25/2016##
9
+NAME:MilK;price:3.23;type:Food;expiration:1/17/2016##
10
+naMe:MilK;price:1.23;type:Food!expiration:4/25/2016##
11
+naMe:apPles;price:0.25;type:Food;expiration:1/23/2016##
12
+naMe:apPles;price:0.23;type:Food;expiration:5/02/2016##
13
+NAMe:BrEAD;price:1.23;type:Food;expiration:1/25/2016##
14
+naMe:;price:3.23;type:Food;expiration:1/04/2016##
15
+naMe:Milk;price:3.23;type:Food;expiration:1/25/2016##
16
+naME:BreaD;price:1.23;type:Food@expiration:1/02/2016##
17
+NAMe:BrEAD;price:1.23;type:Food@expiration:2/25/2016##
18
+naMe:MiLK;priCe:;type:Food;expiration:1/11/2016##
19
+naMe:Cookies;price:2.25;type:Food;expiration:1/25/2016##
20
+naMe:Co0kieS;pRice:2.25;type:Food;expiration:1/25/2016##
21
+naMe:COokIes;price:2.25;type:Food;expiration:3/22/2016##
22
+naMe:COOkieS;Price:2.25;type:Food;expiration:1/25/2016##
23
+NAME:MilK;price:3.23;type:Food;expiration:1/17/2016##
24
+naMe:MilK;priCe:;type:Food;expiration:4/25/2016##
25
+naMe:apPles;prIce:0.25;type:Food;expiration:1/23/2016##
26
+naMe:apPles;pRice:0.23;type:Food;expiration:5/02/2016##
27
+NAMe:BrEAD;price:1.23;type:Food;expiration:1/25/2016##
28
+naMe:;price:3.23;type:Food^expiration:1/04/2016##

+ 28
- 11
src/test/java/io/zipcoder/ItemParserTest.java View File

@@ -10,24 +10,24 @@ import static org.junit.Assert.*;
10 10
 
11 11
 public class ItemParserTest {
12 12
 
13
-    private String rawSingleItem =    "naMe:Milk;price:3.23;type:Food;expiration:1/25/2016##";
13
+    private String rawSingleItem = "naMe:Milk;price:3.23;type:Food;expiration:1/25/2016##";
14 14
 
15 15
     private String rawSingleItemIrregularSeperatorSample = "naMe:MiLK;price:3.23;type:Food^expiration:1/11/2016##";
16 16
 
17
-    private String rawBrokenSingleItem =    "naMe:Milk;price:3.23;type:Food;expiration:1/25/2016##";
17
+    private String rawBrokenSingleItem = "naMe:Milk;price:3.23;type:Food;expiration:1/25/2016##";
18 18
 
19 19
     private String rawMultipleItems = "naMe:Milk;price:3.23;type:Food;expiration:1/25/2016##"
20
-                                      +"naME:BreaD;price:1.23;type:Food;expiration:1/02/2016##"
21
-                                      +"NAMe:BrEAD;price:1.23;type:Food;expiration:2/25/2016##";
20
+            + "naME:BreaD;price:1.23;type:Food;expiration:1/02/2016##"
21
+            + "NAMe:BrEAD;price:1.23;type:Food;expiration:2/25/2016##";
22 22
     private ItemParser itemParser;
23 23
 
24 24
     @Before
25
-    public void setUp(){
25
+    public void setUp() {
26 26
         itemParser = new ItemParser();
27 27
     }
28 28
 
29 29
     @Test
30
-    public void parseRawDataIntoStringArrayTest(){
30
+    public void parseRawDataIntoStringArrayTest() {
31 31
         Integer expectedArraySize = 3;
32 32
         ArrayList<String> items = itemParser.parseRawDataIntoStringArray(rawMultipleItems);
33 33
         Integer actualArraySize = items.size();
@@ -35,28 +35,45 @@ public class ItemParserTest {
35 35
     }
36 36
 
37 37
     @Test
38
-    public void parseStringIntoItemTest() throws ItemParseException{
39
-        Item expected = new Item("milk", 3.23, "food","1/25/2016");
38
+    public void parseStringIntoItemTest() throws ItemParseException {
39
+        Item expected = new Item("milk", 3.23, "food", "1/25/2016");
40 40
         Item actual = itemParser.parseStringIntoItem(rawSingleItem);
41 41
         assertEquals(expected.toString(), actual.toString());
42 42
     }
43 43
 
44 44
     @Test(expected = ItemParseException.class)
45
-    public void parseBrokenStringIntoItemTest() throws ItemParseException{
45
+    public void parseBrokenStringIntoItemTest() throws ItemParseException {
46 46
         itemParser.parseStringIntoItem(rawBrokenSingleItem);
47 47
     }
48 48
 
49 49
     @Test
50
-    public void findKeyValuePairsInRawItemDataTest(){
50
+    public void findKeyValuePairsInRawItemDataTest() {
51 51
         Integer expected = 4;
52 52
         Integer actual = itemParser.findKeyValuePairsInRawItemData(rawSingleItem).size();
53 53
         assertEquals(expected, actual);
54 54
     }
55 55
 
56 56
     @Test
57
-    public void findKeyValuePairsInRawItemDataTestIrregular(){
57
+    public void findKeyValuePairsInRawItemDataTestIrregular() {
58 58
         Integer expected = 4;
59 59
         Integer actual = itemParser.findKeyValuePairsInRawItemData(rawSingleItemIrregularSeperatorSample).size();
60 60
         assertEquals(expected, actual);
61 61
     }
62
+
63
+    @Test
64
+
65
+    public void toLowerCaseTest() {
66
+        String test = "MILK";
67
+        String expect = "milk";
68
+        String actual = itemParser.toLowerCase(test);
69
+        Assert.assertEquals(expect, actual);
70
+    }
71
+
72
+    @Test
73
+    public void toUpperTest() {
74
+        String test = "E";
75
+        Assert.assertTrue(itemParser.isUpper(test));
76
+    }
77
+
78
+
62 79
 }