Преглед изворни кода

working on exception counting

Katrice Williams-Dredden пре 6 година
родитељ
комит
14d913cc35

+ 4
- 0
src/main/java/io/zipcoder/Item.java Прегледај датотеку

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

+ 3
- 0
src/main/java/io/zipcoder/ItemParseException.java Прегледај датотеку

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

+ 80
- 1
src/main/java/io/zipcoder/ItemParser.java Прегледај датотеку

2
 
2
 
3
 import java.util.ArrayList;
3
 import java.util.ArrayList;
4
 import java.util.Arrays;
4
 import java.util.Arrays;
5
+import java.util.Map;
5
 import java.util.TreeMap;
6
 import java.util.TreeMap;
7
+import java.util.regex.Matcher;
8
+import java.util.regex.Pattern;
6
 
9
 
7
 public class ItemParser {
10
 public class ItemParser {
8
     //to organize my Key/Value pairs
11
     //to organize my Key/Value pairs
12
+    private int exceptions = 0;
9
     private TreeMap<String, ArrayList<Item>> groceryMap;
13
     private TreeMap<String, ArrayList<Item>> groceryMap;
10
 
14
 
11
     public ItemParser(){
15
     public ItemParser(){
13
         groceryMap = new TreeMap<String, ArrayList<Item>>();
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
     public ArrayList<String> parseRawDataIntoStringArray(String rawData){
21
     public ArrayList<String> parseRawDataIntoStringArray(String rawData){
18
         String stringPattern = "##";
22
         String stringPattern = "##";
19
         ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawData);
23
         ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawData);
21
     }
25
     }
22
 
26
 
23
     public Item parseStringIntoItem(String rawItem) throws ItemParseException{
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
         return null;
86
         return null;
25
     }
87
     }
26
 
88
 
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 Прегледај датотеку

23
 
23
 
24
     @Before
24
     @Before
25
     public void setUp(){
25
     public void setUp(){
26
+
26
         itemParser = new ItemParser();
27
         itemParser = new ItemParser();
27
     }
28
     }
28
 
29