瀏覽代碼

Merge 169665e2d3981241d829c788a190b89f22dbab8d into 23c2c01bf07b924ac9b860fa2771bf6ceb246ad5

April Rivera 6 年之前
父節點
當前提交
d88d54f356
沒有帳戶連結到提交者的電子郵件

+ 6
- 2
src/main/java/io/zipcoder/Item.java 查看文件

@@ -1,15 +1,18 @@
1 1
 package io.zipcoder;
2 2
 
3
+import java.util.ArrayList;
4
+
3 5
 public class Item {
4 6
     private String name;
5 7
     private Double price;
6 8
     private String type;
7 9
     private String expiration;
8 10
 
11
+
9 12
     /**
10 13
      * Item should not be created unless you have all of the elements, which is why you are forcing
11 14
      * 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.
15
+     * you should throw a Custom Exception.
13 16
      * @param name
14 17
      * @param price
15 18
      * @param type
@@ -23,7 +26,7 @@ public class Item {
23 26
     }
24 27
 
25 28
     public String getName() {
26
-        return name;
29
+        return null;
27 30
     }
28 31
 
29 32
 
@@ -45,4 +48,5 @@ public class Item {
45 48
     public String toString(){
46 49
         return "name:" + name + " price:" + price + " type:" + type + " expiration:" + expiration;
47 50
     }
51
+
48 52
 }

+ 15
- 0
src/main/java/io/zipcoder/ItemList.java 查看文件

@@ -0,0 +1,15 @@
1
+package io.zipcoder;
2
+
3
+import java.util.ArrayList;
4
+
5
+public class ItemList {
6
+
7
+    ArrayList<String> itemList = new ArrayList<String>();
8
+
9
+    public ArrayList<String> getItemList() {
10
+        return itemList;
11
+    }
12
+    public void addItem(String s){
13
+        itemList.add(s);
14
+    }
15
+}

+ 11
- 0
src/main/java/io/zipcoder/ItemParseException.java 查看文件

@@ -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
- 3
src/main/java/io/zipcoder/ItemParser.java 查看文件

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

+ 28
- 7
src/main/java/io/zipcoder/Main.java 查看文件

@@ -2,18 +2,39 @@ 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
+    static ItemList itemList = new ItemList();
11
+
12
+    public ArrayList<String> readRawDataToString() throws Exception {
9 13
         ClassLoader classLoader = getClass().getClassLoader();
10 14
         String result = IOUtils.toString(classLoader.getResourceAsStream("RawData.txt"));
11
-        return result;
15
+        ItemParser itemParser = new ItemParser();
16
+        ArrayList<String> myArray = itemParser.parseRawDataIntoStringArray(result);
17
+        System.out.println(myArray);
18
+        return myArray;
12 19
     }
13 20
 
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.
21
+    public static void main(String[] args) throws Exception {
22
+        ArrayList<String> output = (new Main()).readRawDataToString();
23
+        ItemParser itemParser = new ItemParser();
24
+        for (String s : output) {
25
+            try{
26
+            itemParser.parseStringIntoItem(s);
27
+
28
+
29
+            } catch (ItemParseException e) {
30
+                    continue;
31
+                }
32
+            }
33
+            System.out.println();
34
+            // TODO: parse the data in output into items, and display to console.
35
+        }
36
+
18 37
     }
19
-}
38
+
39
+
40
+

+ 3
- 4
src/test/java/io/zipcoder/ItemParserTest.java 查看文件

@@ -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