Procházet zdrojové kódy

working on exception counting

Katrice Williams-Dredden před 6 roky
rodič
revize
14d913cc35

+ 4
- 0
src/main/java/io/zipcoder/Item.java Zobrazit soubor

@@ -23,21 +23,25 @@ public class Item {
23 23
     }
24 24
 
25 25
     public String getName() {
26
+
26 27
         return name;
27 28
     }
28 29
 
29 30
 
30 31
     public Double getPrice() {
32
+
31 33
         return price;
32 34
     }
33 35
 
34 36
 
35 37
     public String getType() {
38
+
36 39
         return type;
37 40
     }
38 41
 
39 42
 
40 43
     public String getExpiration() {
44
+
41 45
         return expiration;
42 46
     }
43 47
 

+ 3
- 0
src/main/java/io/zipcoder/ItemParseException.java Zobrazit soubor

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

+ 80
- 1
src/main/java/io/zipcoder/ItemParser.java Zobrazit soubor

@@ -2,10 +2,14 @@ package io.zipcoder;
2 2
 
3 3
 import java.util.ArrayList;
4 4
 import java.util.Arrays;
5
+import java.util.Map;
5 6
 import java.util.TreeMap;
7
+import java.util.regex.Matcher;
8
+import java.util.regex.Pattern;
6 9
 
7 10
 public class ItemParser {
8 11
     //to organize my Key/Value pairs
12
+    private int exceptions = 0;
9 13
     private TreeMap<String, ArrayList<Item>> groceryMap;
10 14
 
11 15
     public ItemParser(){
@@ -13,7 +17,7 @@ public class ItemParser {
13 17
         groceryMap = new TreeMap<String, ArrayList<Item>>();
14 18
     }
15 19
 
16
-
20
+//splits a string of rawData by ## and puts it into an ArrayList called reponse
17 21
     public ArrayList<String> parseRawDataIntoStringArray(String rawData){
18 22
         String stringPattern = "##";
19 23
         ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawData);
@@ -21,6 +25,64 @@ public class ItemParser {
21 25
     }
22 26
 
23 27
     public Item parseStringIntoItem(String rawItem) throws ItemParseException{
28
+
29
+
30
+        if(findName(rawItem) == null || findPrice(rawItem) == null){
31
+            throw new ItemParseException();
32
+        }
33
+
34
+
35
+        String name = findName(rawItem);
36
+        Double price = Double.parseDouble(findPrice(rawItem));
37
+        String type = findType(rawItem);
38
+        String expirationDate = findExpirationDate(rawItem);
39
+
40
+        Item item = new Item(name, price, type, expirationDate);
41
+        return item;
42
+    }
43
+
44
+    private String findExpirationDate(String rawItem)throws ItemParseException {
45
+        Pattern checkExpDateRegex = Pattern.compile("\\d\\/\\d+\\/\\d+");
46
+        Matcher regexExpDateMatcher = checkExpDateRegex.matcher(rawItem);
47
+
48
+        if(regexExpDateMatcher.find()){
49
+            return regexExpDateMatcher.group();
50
+        }
51
+        return null;
52
+    }
53
+
54
+    private String findType(String rawItem)throws ItemParseException {
55
+        Pattern checkTypeRegex = Pattern.compile("(?<=([Tt][Yy][Pp][Ee][^A-Za-z])).*?(?=[^A-Za-z0])");
56
+        Matcher regexTypeMatcher = checkTypeRegex.matcher(rawItem);
57
+
58
+        if(regexTypeMatcher.find()){
59
+            String fixtype = regexTypeMatcher.group().replace("\\d", "f");
60
+            return fixtype.toLowerCase();
61
+        }
62
+
63
+        return null;
64
+    }
65
+
66
+    public String findName(String rawItem)throws ItemParseException{
67
+        Pattern checkNameRegex = Pattern.compile("(?<=([Nn][Aa][Mm][Ee][^A-Za-z])).*?(?=[^A-Za-z0])");
68
+        Matcher regexNameMatcher = checkNameRegex.matcher(rawItem);
69
+
70
+        if(regexNameMatcher.find()){
71
+            String fixName = regexNameMatcher.group().replace("\\d", "o");
72
+            return fixName.toLowerCase();
73
+        }
74
+        return null;
75
+    }
76
+
77
+    public String findPrice(String rawItem) throws NumberFormatException{
78
+        Pattern checkPriceRegex = Pattern.compile("\\d\\.\\d*");
79
+        Matcher regexPriceMatcher = checkPriceRegex.matcher(rawItem);
80
+
81
+        if(regexPriceMatcher.find()){
82
+            if(!regexPriceMatcher.group().equals("")){
83
+                return regexPriceMatcher.group();
84
+            }
85
+        }
24 86
         return null;
25 87
     }
26 88
 
@@ -35,6 +97,23 @@ public class ItemParser {
35 97
     }
36 98
 
37 99
 
100
+    public Map<String, ArrayList<Item>> printGroceries(){
101
+        return null;
102
+
103
+    }
104
+
105
+
106
+    //PseudoCode
107
+    //I need to print the grocery list
108
+    //I should be able to add to the grocery list
109
+    //I need to be able to return pricesoh
110
+    //I need to be able to count the exceptions
111
+    //need a replace method for the C00kies
112
+    //get key
113
+    //get value
114
+    //how to handle if the price field is empty
115
+    //patterns & matches
116
+
38 117
 
39 118
 
40 119
 

+ 1
- 0
src/test/java/io/zipcoder/ItemParserTest.java Zobrazit soubor

@@ -23,6 +23,7 @@ public class ItemParserTest {
23 23
 
24 24
     @Before
25 25
     public void setUp(){
26
+
26 27
         itemParser = new ItemParser();
27 28
     }
28 29