Browse Source

Happy St. Paddys Dayto us

Jessica Campbell 6 years ago
parent
commit
2ed526a979

+ 4
- 0
src/main/java/io/zipcoder/Item.java View File

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

+ 94
- 7
src/main/java/io/zipcoder/ItemParser.java View File

@@ -1,31 +1,118 @@
1 1
 package io.zipcoder;
2 2
 
3
+import com.sun.org.apache.xerces.internal.impl.xpath.regex.Match;
4
+import com.sun.org.apache.xpath.internal.operations.Bool;
5
+
6
+import java.lang.reflect.Array;
3 7
 import java.util.ArrayList;
4 8
 import java.util.Arrays;
9
+import java.util.HashMap;
10
+import java.util.regex.Matcher;
11
+import java.util.regex.Pattern;
5 12
 
6 13
 public class ItemParser {
7 14
 
15
+    public static int exceptionCount = 0;
16
+    private HashMap<String, ArrayList<Item>> groceryList = new HashMap<String, ArrayList<Item>>();
17
+
8 18
 
9
-    public ArrayList<String> parseRawDataIntoStringArray(String rawData){
19
+    public ArrayList<String> parseRawDataIntoStringArray(String rawData){ // entries split by ## with name,price,type and exp Example: [naMe:Milk;price:3.23;type:Food;expiration:1/25/2016, naME:BreaD;price:1.23;type:Food;expiration:1/02/2016 ... etc.]
10 20
         String stringPattern = "##";
11 21
         ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawData);
12 22
         return response;
13 23
     }
24
+    public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){ // entries split by crazy characters Example: [naMe:Milk, price:3.23, type:Food, expiration:1/25/2016] [] [] etc.
25
+        String stringPattern = "[;|^]";
26
+        ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawItem);
27
+        return response;
28
+    }
29
+//    public ArrayList<String> splitStringsAtSemiColon(String rawItem){ // entries split by : Example:
30
+//        String stringPattern = ":";
31
+//        ArrayList<String> response = splitStringWithRegexPattern(stringPattern, rawItem);
32
+//        return response;
33
+//    }
34
+
35
+    private ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString){ // helper method that splits strings at characters above into ArrayLists
36
+        return new ArrayList<String>(Arrays.asList(inputString.split(stringPattern)));
37
+    }
14 38
 
15 39
     public Item parseStringIntoItem(String rawItem) throws ItemParseException{
40
+        if (findName(rawItem) == null || findPrice(rawItem)== null) {
41
+            throw new ItemParseException();
42
+        }
43
+
44
+        String name = findName(rawItem);
45
+        Double price = Double.parseDouble(findPrice(rawItem));
46
+        String type = findType(rawItem);
47
+        String expDate = findExpiration(rawItem);
48
+
49
+        Item foundAnItem = new Item(name, price, type, expDate);
50
+        return foundAnItem;
51
+    }
52
+
53
+    public String findName(String rawItem) throws ItemParseException {
54
+        Pattern patternName = Pattern.compile("(?<=([Nn][Aa][Mm][Ee][^A-Za-z])).*?(?=[^A-Za-z0])"); // matches "nAmE: MiLk"
55
+        Matcher matcherName = patternName.matcher(rawItem);
56
+        if (matcherName.find()){
57
+            if(!matcherName.group().equals("")){ // if the name does not equal null --> some blank spaces were being counted as names
58
+                String fixedName = matcherName.group().replaceAll("\\d", "o"); // will fix C00kie mistake - once you hit a number replace it with an o
59
+                return fixedName.toLowerCase(); // changed to lowercase bc tests requires lowercase
60
+            }
61
+        }
16 62
         return null;
17 63
     }
18 64
 
19
-    public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){
20
-        String stringPattern = "[;|^]";
21
-        ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawItem);
22
-        return response;
65
+    //group() --> Matcher method
66
+    //Returns the input subsequent matched by the previous match.
67
+
68
+    public String findPrice(String rawItem) throws ItemParseException {
69
+        Pattern patternPrice = Pattern.compile("\\d(\\.)\\d\\d"); // matches "3.23"
70
+        Matcher matcherPrice = patternPrice.matcher(rawItem);
71
+        if(matcherPrice.find()){
72
+            if (!matcherPrice.group().equals("")){ // some blank prices were getting returned so make sure they're not blank
73
+                return matcherPrice.group();
74
+            }
75
+        }
76
+        return null;
23 77
     }
24 78
 
25
-    private ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString){
26
-        return new ArrayList<String>(Arrays.asList(inputString.split(stringPattern)));
79
+    public String  findExpiration(String rawItem) throws ItemParseException{
80
+        Pattern patternExp = Pattern.compile("(?<=([Ee][Xx][Pp][Ii][Rr][Aa][Tt][Ii][Oo][Nn][^A-Za-z]))(.)+[^#]"); // matches "eXpirAtioN: 1/17/2016
81
+        Matcher matcherExp = patternExp.matcher(rawItem);
82
+        if (matcherExp.find()){
83
+            return matcherExp.group();
84
+        }
85
+        else return null;
27 86
     }
28 87
 
88
+    public String findType(String rawItem){
89
+        Pattern patternType = Pattern.compile("(?<=([Tt][Yy][Pp][Ee][^A-Za-z])).*?(?=[^A-Za-z0])"); // matches "type: Food"
90
+        Matcher matcherType = patternType.matcher(rawItem);
91
+        if (matcherType.find()){
92
+            return matcherType.group().toLowerCase();
93
+        }
94
+        else return null;
95
+    }
29 96
 
97
+    public HashMap<String, ArrayList<Item>> getGroceryList() throws Exception {
98
+        Main main = new Main();
30 99
 
100
+        ArrayList<String> listOfItems = parseRawDataIntoStringArray(main.readRawDataToString());
101
+
102
+        for(String anItem : listOfItems){
103
+            try {
104
+                Item newItem = parseStringIntoItem(anItem);
105
+                if (!groceryList.containsKey(newItem.getName())){ // if we do not have this key
106
+                    ArrayList<Item> myItemArrayList = new ArrayList<Item>(); // we will have to create a new arrayList to hold our items
107
+                    myItemArrayList.add(newItem); // then we can add our item to our arrayList
108
+                    groceryList.put(newItem.getName(), myItemArrayList); // we will add it to our map next
109
+                } else {
110
+                    groceryList.get(newItem.getName()).add(newItem);
111
+                }
112
+            } catch (ItemParseException e){
113
+                exceptionCount++;
114
+            }
115
+        }
116
+        return groceryList;
117
+    }
31 118
 }

+ 29
- 3
src/main/java/io/zipcoder/Main.java View File

@@ -2,6 +2,10 @@ package io.zipcoder;
2 2
 
3 3
 import org.apache.commons.io.IOUtils;
4 4
 
5
+import java.lang.reflect.Array;
6
+import java.util.ArrayList;
7
+import java.util.Map;
8
+
5 9
 
6 10
 public class Main {
7 11
 
@@ -13,7 +17,29 @@ public class Main {
13 17
 
14 18
     public static void main(String[] args) throws Exception{
15 19
         String output = (new Main()).readRawDataToString();
16
-        System.out.println(output);
17
-        // TODO: parse the data in output into items, and display to console.
20
+        ItemParser parser = new ItemParser();
21
+
22
+
23
+        for (Map.Entry<String, ArrayList<Item>> mapKey : parser.getGroceryList().entrySet()) {
24
+            System.out.println(mapKey.getKey());
25
+            for (Item item : mapKey.getValue()) {
26
+                System.out.println(item.getPrice());
27
+            }
28
+        }
29
+
30
+//        System.out.println(output);
31
+//        // TODO: parse the data in output into items, and display to console.
32
+//        ItemParser itemParser = new ItemParser();
33
+//        ArrayList<String> arrayListWithoutHashTags = itemParser.parseRawDataIntoStringArray(output);
34
+//        for (String s : arrayListWithoutHashTags)
35
+//        {
36
+//            //System.out.println(s);
37
+//            ItemParser itemParser2 = new ItemParser();
38
+//            //System.out.println(itemParser2.findKeyValuePairsInRawItemData(s));
39
+//            ArrayList<String> arrayWithoutVariousCharacters = itemParser2.splitStringsAtSemiColon(s);
40
+//                for (String s1 : arrayWithoutVariousCharacters){
41
+//                    System.out.println(s1);
42
+//                }
43
+        }
18 44
     }
19
-}
45
+

+ 2
- 1
src/test/java/io/zipcoder/ItemParserTest.java View File

@@ -14,7 +14,7 @@ public class ItemParserTest {
14 14
 
15 15
     private String rawSingleItemIrregularSeperatorSample = "naMe:MiLK;price:3.23;type:Food^expiration:1/11/2016##";
16 16
 
17
-    private String rawBrokenSingleItem =    "naMe:Milk;price:3.23;type:Food;expiration:1/25/2016##";
17
+    private String rawBrokenSingleItem =    "naMe:;price:3.23;type:Food;expiration:1/25/2016##";
18 18
 
19 19
     private String rawMultipleItems = "naMe:Milk;price:3.23;type:Food;expiration:1/25/2016##"
20 20
                                       +"naME:BreaD;price:1.23;type:Food;expiration:1/02/2016##"
@@ -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