April Rivera hace 6 años
padre
commit
2d4c08bf68

+ 2
- 2
src/main/java/io/zipcoder/Item.java Ver fichero

@@ -9,7 +9,7 @@ public class Item {
9 9
     /**
10 10
      * Item should not be created unless you have all of the elements, which is why you are forcing
11 11
      * it to be set in the constructor. In ItemParser, if you do not find all the elements of a Item,
12
-     * you should throw an Custom Exception.
12
+     * you should throw a Custom Exception.
13 13
      * @param name
14 14
      * @param price
15 15
      * @param type
@@ -23,7 +23,7 @@ public class Item {
23 23
     }
24 24
 
25 25
     public String getName() {
26
-        return name;
26
+        return null;
27 27
     }
28 28
 
29 29
 

+ 11
- 0
src/main/java/io/zipcoder/ItemParseException.java Ver fichero

@@ -1,4 +1,15 @@
1 1
 package io.zipcoder;
2 2
 
3
+
3 4
 public class ItemParseException extends Exception {
5
+
6
+    public static int numberOfExceptionsThrown;
7
+
8
+    public ItemParseException(){
9
+        numberOfExceptionsThrown++;
10
+    }
11
+    public static int getNumberOfTimesThrown(){
12
+        return numberOfExceptionsThrown;
13
+    }
14
+
4 15
 }

+ 36
- 2
src/main/java/io/zipcoder/ItemParser.java Ver fichero

@@ -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,9 +14,41 @@ public class ItemParser {
12 14
         return response;
13 15
     }
14 16
 
15
-    public Item parseStringIntoItem(String rawItem) throws ItemParseException{
16
-        return null;
17
+    public int countMatches(Matcher matcher){
18
+        int count = 0;
19
+        while (matcher.find()){
20
+            count++;
21
+        }
22
+        return count;
17 23
     }
24
+    public Item parseStringIntoItem(String rawItem) throws ItemParseException {
25
+        String name;
26
+        Double price;
27
+        String type;
28
+        String expiration;
29
+        Item item = null;
30
+
31
+        Matcher m = Pattern.compile(":([^,].*?)(,|##|\\^|!|%|\\*|@)").matcher(rawItem);
32
+
33
+        int numberOfMatches = countMatches(m);
34
+        final int REQUIRED_NUMBER_OF_FIELDS_FOR_ITEM = 4;
35
+        if (numberOfMatches <  REQUIRED_NUMBER_OF_FIELDS_FOR_ITEM){
36
+            throw  new ItemParseException();
37
+        }
38
+        m.reset();
39
+            while (m.find()){
40
+                name = m.group(1).toLowerCase();
41
+                m.find();
42
+                price = Double.parseDouble(m.group(1));
43
+                m.find();
44
+                type = m.group(1).toLowerCase();
45
+                m.find();
46
+                expiration = m.group(1);
47
+                item = new Item(name, price, type, expiration);
48
+            }
49
+        return item;
50
+        }
51
+
18 52
 
19 53
     public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){
20 54
         String stringPattern = "[;|^]";

+ 26
- 7
src/main/java/io/zipcoder/Main.java Ver fichero

@@ -2,18 +2,37 @@ package io.zipcoder;
2 2
 
3 3
 import org.apache.commons.io.IOUtils;
4 4
 
5
+import java.util.ArrayList;
6
+
5 7
 
6 8
 public class Main {
7 9
 
8
-    public String readRawDataToString() throws Exception{
10
+    public ArrayList<String> readRawDataToString() throws Exception {
9 11
         ClassLoader classLoader = getClass().getClassLoader();
10 12
         String result = IOUtils.toString(classLoader.getResourceAsStream("RawData.txt"));
11
-        return result;
13
+        ItemParser itemParser = new ItemParser();
14
+        ArrayList<String> myArray = itemParser.parseRawDataIntoStringArray(result);
15
+        System.out.println(myArray);
16
+        return myArray;
12 17
     }
13 18
 
14
-    public static void main(String[] args) throws Exception{
15
-        String output = (new Main()).readRawDataToString();
16
-        System.out.println(output);
17
-        // TODO: parse the data in output into items, and display to console.
19
+    public static void main(String[] args) throws Exception {
20
+        ArrayList<String> output = (new Main()).readRawDataToString();
21
+        ItemParser itemParser = new ItemParser();
22
+        ArrayList<String> lastArray = itemParser.findKeyValuePairsInRawItemData(output.toString());
23
+        for (String s : lastArray) {
24
+            try{
25
+            Item item = itemParser.parseStringIntoItem(s);
26
+
27
+            } catch (ItemParseException e) {
28
+                    continue;
29
+                }
30
+            }
31
+            System.out.println();
32
+            // TODO: parse the data in output into items, and display to console.
33
+        }
34
+
18 35
     }
19
-}
36
+
37
+
38
+

+ 3
- 4
src/test/java/io/zipcoder/ItemParserTest.java Ver fichero

@@ -1,6 +1,5 @@
1 1
 package io.zipcoder;
2 2
 
3
-import org.junit.Assert;
4 3
 import org.junit.Before;
5 4
 import org.junit.Test;
6 5
 
@@ -14,7 +13,7 @@ public class ItemParserTest {
14 13
 
15 14
     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
+    private String rawBrokenSingleItem =    "naMe:;price:3.23;type:Food;expiration:1/25/2016##";
18 17
 
19 18
     private String rawMultipleItems = "naMe:Milk;price:3.23;type:Food;expiration:1/25/2016##"
20 19
                                       +"naME:BreaD;price:1.23;type:Food;expiration:1/02/2016##"
@@ -35,14 +34,14 @@ public class ItemParserTest {
35 34
     }
36 35
 
37 36
     @Test
38
-    public void parseStringIntoItemTest() throws ItemParseException{
37
+    public void parseStringIntoItemTest() throws ItemParseException {
39 38
         Item expected = new Item("milk", 3.23, "food","1/25/2016");
40 39
         Item actual = itemParser.parseStringIntoItem(rawSingleItem);
41 40
         assertEquals(expected.toString(), actual.toString());
42 41
     }
43 42
 
44 43
     @Test(expected = ItemParseException.class)
45
-    public void parseBrokenStringIntoItemTest() throws ItemParseException{
44
+    public void parseBrokenStringIntoItemTest() throws ItemParseException {
46 45
         itemParser.parseStringIntoItem(rawBrokenSingleItem);
47 46
     }
48 47