Pārlūkot izejas kodu

Merge 38b58d92d1164a97cfb44d2e05d0bec9f07743b0 into 23c2c01bf07b924ac9b860fa2771bf6ceb246ad5

asusnick 6 gadus atpakaļ
vecāks
revīzija
6ddf180cc1
Revīzijas autora e-pasts nav piesaistīts nevienam kontam

+ 19
- 11
src/main/java/io/zipcoder/Item.java Parādīt failu

@@ -1,11 +1,10 @@
1 1
 package io.zipcoder;
2 2
 
3
-public class Item {
3
+public class Item{
4 4
     private String name;
5 5
     private Double price;
6 6
     private String type;
7 7
     private String expiration;
8
-
9 8
     /**
10 9
      * Item should not be created unless you have all of the elements, which is why you are forcing
11 10
      * it to be set in the constructor. In ItemParser, if you do not find all the elements of a Item,
@@ -22,22 +21,18 @@ public class Item {
22 21
         this.expiration = expiration;
23 22
     }
24 23
 
25
-    public String getName() {
26
-        return name;
24
+    public Item(String name, String price, String type, String expiration) {
27 25
     }
28 26
 
29
-
30
-    public Double getPrice() {
31
-        return price;
27
+    public String getName(){
28
+        return name;
32 29
     }
33 30
 
34
-
35
-    public String getType() {
31
+    public String getType(){
36 32
         return type;
37 33
     }
38 34
 
39
-
40
-    public String getExpiration() {
35
+    public String getExpiration(){
41 36
         return expiration;
42 37
     }
43 38
 
@@ -45,4 +40,17 @@ public class Item {
45 40
     public String toString(){
46 41
         return "name:" + name + " price:" + price + " type:" + type + " expiration:" + expiration;
47 42
     }
43
+
44
+//    public String formattedOutput(){
45
+//        StringBuilder stringBuilder = new StringBuilder();
46
+//        stringBuilder.append("name:\t" + name)
47
+//                .append("\t\tseen: "+  +" times\n")
48
+//                .append("=============\t\t=============\n");
49
+//        for(     ){
50
+//            stringBuilder.append("Price:\t"+
51
+//                    .append("\t\tseen: "+  +" times\n")
52
+//                    .append("-------------\t\t-------------\n");
53
+//        }
54
+//        return stringBuilder.toString();
55
+//    }
48 56
 }

+ 12
- 1
src/main/java/io/zipcoder/ItemParseException.java Parādīt failu

@@ -1,4 +1,15 @@
1 1
 package io.zipcoder;
2 2
 
3
-public class ItemParseException extends Exception {
3
+public class ItemParseException extends Exception{
4
+
5
+    public static int numberOfErrors = 0;
6
+
7
+    public ItemParseException(String errors){
8
+        super(errors);
9
+        numberOfErrors++;
10
+    }
11
+
12
+    public static String errorsOutput(){
13
+        return "Errors\t\t\t\tseen: " + numberOfErrors + " times";
14
+    }
4 15
 }

+ 70
- 5
src/main/java/io/zipcoder/ItemParser.java Parādīt failu

@@ -2,9 +2,12 @@ package io.zipcoder;
2 2
 
3 3
 import java.util.ArrayList;
4 4
 import java.util.Arrays;
5
+import java.util.regex.Pattern;
6
+import java.util.regex.Matcher;
5 7
 
6
-public class ItemParser {
8
+import static com.sun.tools.javac.util.StringUtils.toLowerCase;
7 9
 
10
+public class ItemParser{
8 11
 
9 12
     public ArrayList<String> parseRawDataIntoStringArray(String rawData){
10 13
         String stringPattern = "##";
@@ -13,11 +16,72 @@ public class ItemParser {
13 16
     }
14 17
 
15 18
     public Item parseStringIntoItem(String rawItem) throws ItemParseException{
16
-        return null;
19
+
20
+        ArrayList<String> kvp = findKeyValuePairsInRawItemData(rawItem);
21
+
22
+        String name;
23
+        Double price;
24
+        String type;
25
+        String expiration;
26
+
27
+        //name block (with regex for milk, bread, cookies, apples)
28
+        Pattern namePattern = Pattern.compile("([Nn][Aa][Mm][Ee]):(\\w*\\d*)");
29
+        Matcher nameMatcher = namePattern.matcher(kvp.get(0));
30
+        if(nameMatcher.matches()){
31
+            name = nameMatcher.group(2);
32
+        }
33
+        else {
34
+            throw new ItemParseException("invalid name");
35
+        }
36
+
37
+        //price block (with regex for all prices)
38
+        Pattern pricePattern = Pattern.compile("([Pp][Rr][Ii][Cc][Ee]):(\\d.\\d\\d)");
39
+        Matcher priceMatcher = pricePattern.matcher(kvp.get(1));
40
+        if(priceMatcher.matches()){
41
+            price = Double.parseDouble(priceMatcher.group(2));
42
+        }
43
+        else {
44
+            throw new ItemParseException("invalid price");
45
+        }
46
+
47
+        //type block (with regex for all types)
48
+        Pattern typePattern = Pattern.compile("([Tt][Yy][Pp][Ee]):([A-Za-z0-9]*)");
49
+        Matcher typeMatcher = typePattern.matcher(kvp.get(2));
50
+        if(typeMatcher.matches()){
51
+            type = typeMatcher.group(2);
52
+        }
53
+        else {
54
+            throw new ItemParseException("invalid type");
55
+        }
56
+
57
+        //expiration block (with regex for all dates)
58
+        Pattern expPattern = Pattern.compile("([Ee][Xx][Pp][Ii][Rr][Aa][Tt][Ii][Oo][Nn]):(\\d/\\d{2}/\\d{4})##");
59
+        Matcher expMatcher = expPattern.matcher(kvp.get(3));
60
+        if(expMatcher.matches()){
61
+            expiration = expMatcher.group(2);
62
+        }
63
+        else {
64
+            throw new ItemParseException("invalid expiration");
65
+        }
66
+        return new Item(name, price, type, expiration);
67
+    }
68
+
69
+    //want to check and change spelling and casing here
70
+    public String checkSpelling(String itemName){
71
+        Matcher matcher = Pattern.compile("0", Pattern.CASE_INSENSITIVE).matcher(itemName);
72
+        matcher.find();
73
+        return matcher.replaceAll("o");
74
+    }
75
+
76
+    public String checkCase(String itemName){
77
+        if(itemName != null){
78
+            itemName = checkSpelling(toLowerCase(itemName));
79
+        }
80
+        return itemName;
17 81
     }
18 82
 
19 83
     public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){
20
-        String stringPattern = "[;|^]";
84
+        String stringPattern = "[;|^|!|%|*|@]";
21 85
         ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawItem);
22 86
         return response;
23 87
     }
@@ -26,6 +90,7 @@ public class ItemParser {
26 90
         return new ArrayList<String>(Arrays.asList(inputString.split(stringPattern)));
27 91
     }
28 92
 
29
-
30
-
93
+    public void errors(){
94
+        System.out.println(ItemParseException.errorsOutput());
95
+    }
31 96
 }

+ 3
- 3
src/main/java/io/zipcoder/Main.java Parādīt failu

@@ -2,7 +2,6 @@ package io.zipcoder;
2 2
 
3 3
 import org.apache.commons.io.IOUtils;
4 4
 
5
-
6 5
 public class Main {
7 6
 
8 7
     public String readRawDataToString() throws Exception{
@@ -13,7 +12,8 @@ public class Main {
13 12
 
14 13
     public static void main(String[] args) throws Exception{
15 14
         String output = (new Main()).readRawDataToString();
16
-        System.out.println(output);
17
-        // TODO: parse the data in output into items, and display to console.
15
+        ItemParser itemParser = new ItemParser();
16
+        //insert formatted output here?
17
+        itemParser.errors();
18 18
     }
19 19
 }

+ 4
- 5
src/test/java/io/zipcoder/ItemParserTest.java Parādīt failu

@@ -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
 
@@ -12,9 +11,9 @@ public class ItemParserTest {
12 11
 
13 12
     private String rawSingleItem =    "naMe:Milk;price:3.23;type:Food;expiration:1/25/2016##";
14 13
 
15
-    private String rawSingleItemIrregularSeperatorSample = "naMe:MiLK;price:3.23;type:Food^expiration:1/11/2016##";
14
+    private String rawSingleItemIrregularSeparatorSample = "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:Milk;price:;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##"
@@ -36,7 +35,7 @@ public class ItemParserTest {
36 35
 
37 36
     @Test
38 37
     public void parseStringIntoItemTest() throws ItemParseException{
39
-        Item expected = new Item("milk", 3.23, "food","1/25/2016");
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
     }
@@ -56,7 +55,7 @@ public class ItemParserTest {
56 55
     @Test
57 56
     public void findKeyValuePairsInRawItemDataTestIrregular(){
58 57
         Integer expected = 4;
59
-        Integer actual = itemParser.findKeyValuePairsInRawItemData(rawSingleItemIrregularSeperatorSample).size();
58
+        Integer actual = itemParser.findKeyValuePairsInRawItemData(rawSingleItemIrregularSeparatorSample).size();
60 59
         assertEquals(expected, actual);
61 60
     }
62 61
 }