Ver código fonte

Fixed the issues. Everything is working perfectly now

CWinarski 6 anos atrás
pai
commit
1227677299

+ 1
- 0
src/main/java/io/zipcoder/ItemParseException.java Ver arquivo

@@ -8,4 +8,5 @@ public class ItemParseException extends Exception {
8 8
     public static int getCount(){
9 9
         return count;
10 10
     }
11
+
11 12
 }

+ 32
- 22
src/main/java/io/zipcoder/ItemParser.java Ver arquivo

@@ -27,12 +27,12 @@ public class ItemParser {
27 27
         return response;
28 28
     }
29 29
 
30
-    //split string with regex
30
+    //general method for splitting arraylists with regex
31 31
     private ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString) {
32 32
         return new ArrayList<>(Arrays.asList(inputString.split(stringPattern)));
33 33
     }
34 34
 
35
-
35
+    //Splits arraylist that has chunks into the name:cake arraylist which gets used in testing
36 36
     public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem) {
37 37
         String stringPattern = "[;|^|!|%|*|@]";
38 38
         ArrayList<String> response = splitStringWithRegexPattern(stringPattern, rawItem);
@@ -41,6 +41,19 @@ public class ItemParser {
41 41
 
42 42
     }
43 43
 
44
+    //takes the raw data arraylist(item chunks) parses them into items and then adds the items to the map
45
+    public void addItemToList(ArrayList<String> itemList){
46
+            for (int i = 0; i < itemList.size(); i++) {
47
+                try{
48
+                Item newItem = (parseStringIntoItem(itemList.get(i)));
49
+                addNewItemToMap(realFoodList, newItem);
50
+            }catch (ItemParseException e){
51
+                    continue;
52
+                }
53
+
54
+        }
55
+    }
56
+
44 57
     public void addNewItemToMap(Map<String, ArrayList<Item>> map, Item newItem) {
45 58
         if (!map.keySet().contains(newItem.getName())) {
46 59
             map.put(newItem.getName(), new ArrayList<>());
@@ -50,17 +63,7 @@ public class ItemParser {
50 63
         }
51 64
     }
52 65
 
53
-    public void addItemToList(ArrayList<String> itemList) {
54
-        try {
55
-            for (int i = 0; i < itemList.size(); i++) {
56
-                Item newItem = (parseStringIntoItem(itemList.get(i)));
57
-                addNewItemToMap(realFoodList, newItem);
58
-            }
59
-        } catch (ItemParseException e) {
60
-            new ItemParseException();
61
-        }
62
-    }
63
-
66
+    //gets all the prices for the items
64 67
     public ArrayList<Double> getPrices(Map.Entry<String, ArrayList<Item>> itemMap) {
65 68
         ArrayList<Double> prices = new ArrayList<>();
66 69
         for (int i = 0; i < itemMap.getValue().size(); i++) {
@@ -81,33 +84,39 @@ public class ItemParser {
81 84
         return counter;
82 85
     }
83 86
 
84
-
87
+        //Create new item
85 88
     public Item parseStringIntoItem(String rawItem) throws ItemParseException {
86 89
         String name = findName(rawItem);
87 90
         Double price = Double.valueOf(findPrice(rawItem));
88 91
         String type = findType(rawItem);
89 92
         String expiration = findExpiration(rawItem);
93
+
90 94
         return new Item(name, price, type, expiration);
91 95
     }
92 96
 
93
-
94
-    public String fixCookie(String input) {
95
-        String regexCookie = "(C|c).....(S|s)";
96
-        Pattern pattern = Pattern.compile(regexCookie);
97
+    //Cookie has a 0 in it so we fix it before parsing into the item
98
+    public String fixCo0kie(String input) {
99
+        String regexCo0kie = "[0]";
100
+        Pattern pattern = Pattern.compile(regexCo0kie);
97 101
         Matcher matcher = pattern.matcher(input);
98
-        return matcher.replaceAll("cookies");
102
+        return matcher.replaceAll("o");
99 103
     }
100 104
 
105
+    //We find the item name by searching for the name:item pair then return the group that has the item
101 106
     public String findName(String name) throws ItemParseException {
102
-        String ifCookieFix = fixCookie(name);
103 107
         String regexName = "([Nn]..[Ee]:)(\\w+)";
104 108
         Pattern pattern = Pattern.compile(regexName);
105 109
         Matcher matcher = pattern.matcher(name);
106 110
         if (matcher.find()) {
107
-            return matcher.group(2).toLowerCase();
111
+            if(matcher.group(2).contains("0")) {
112
+                return fixCo0kie(matcher.group(2).toLowerCase());
113
+            }else {
114
+                return matcher.group(2).toLowerCase();
115
+            }
108 116
         } else {
109 117
             throw new ItemParseException();
110 118
         }
119
+
111 120
     }
112 121
 
113 122
     public String findPrice(String price) throws ItemParseException {
@@ -122,6 +131,7 @@ public class ItemParser {
122 131
         }
123 132
     }
124 133
 
134
+
125 135
     public String findType(String type) throws ItemParseException {
126 136
         String regexType = "([Tt]..[Ee]:)(\\w+)";
127 137
         Pattern pattern = Pattern.compile(regexType);
@@ -134,6 +144,7 @@ public class ItemParser {
134 144
         }
135 145
     }
136 146
 
147
+
137 148
     public String findExpiration(String expiration) throws ItemParseException {
138 149
         String regexExpiration = "([E|e]........[N|n]:)(\\d\\/\\d{2}\\/\\d{4})";
139 150
         Pattern pattern = Pattern.compile(regexExpiration);
@@ -145,7 +156,6 @@ public class ItemParser {
145 156
         }
146 157
     }
147 158
 
148
-
149 159
     public String printParsedJerkSON() {
150 160
         StringBuilder print = new StringBuilder();
151 161
         for (Map.Entry<String, ArrayList<Item>> entry : realFoodList.entrySet()) {

+ 97
- 1
src/test/java/io/zipcoder/ItemParserTest.java Ver arquivo

@@ -5,6 +5,7 @@ import org.junit.Before;
5 5
 import org.junit.Test;
6 6
 
7 7
 import java.util.ArrayList;
8
+import java.util.Map;
8 9
 
9 10
 import static org.junit.Assert.*;
10 11
 
@@ -63,7 +64,7 @@ public class ItemParserTest {
63 64
     }
64 65
 
65 66
     @Test
66
-    public void addNewItemToMapTest(){
67
+    public void addNewItemToMapTest() {
67 68
         //Given
68 69
         boolean expected = true;
69 70
         ArrayList<String> testList = itemParser.parseRawDataIntoStringArray(rawMultipleItems);
@@ -74,4 +75,99 @@ public class ItemParserTest {
74 75
         Assert.assertEquals(expected,actual);
75 76
     }
76 77
 
78
+    @Test
79
+    public void addItemToListTest(){
80
+        //Given
81
+        boolean expected = true;
82
+        ArrayList<String> testList;
83
+        testList = itemParser.parseRawDataIntoStringArray(rawMultipleItems);
84
+        itemParser.addItemToList(testList);
85
+        //When
86
+        boolean actual = itemParser.getRealFoodList().keySet().contains("milk");
87
+        //Then
88
+        Assert.assertEquals(expected,actual);
89
+    }
90
+
91
+    @Test
92
+    public void countNumberOfDifferentPricesTest(){
93
+        //Given
94
+        int expected = 2;
95
+        ArrayList<Item> testList = new ArrayList<>();
96
+        Item cake = new Item("cake", 2.50, "Food", "1/24/18");
97
+        Item cake2 = new Item("cake", 2.50, "Food", "1/24/18");
98
+        Item pie = new Item("cie", 2.00, "Food", "1/18/18");
99
+        testList.add(cake);
100
+        testList.add(cake2);
101
+        testList.add(pie);
102
+        //When
103
+        int actual = itemParser.countNumberOfDifferentPrices(testList, 2.50);
104
+        //Then
105
+        Assert.assertEquals(expected,actual);
106
+    }
107
+
108
+    @Test
109
+    public void fixCookieTest() throws ItemParseException {
110
+        //Given
111
+        String expected = "cookies";
112
+        String messedUpCookie = "c00kies";
113
+        //When
114
+       String actual = itemParser.fixCo0kie(messedUpCookie);
115
+        //Then
116
+        Assert.assertEquals(expected,actual);
117
+    }
118
+
119
+    @Test
120
+    public void findNameTest() throws ItemParseException {
121
+        //Given
122
+        String expected = "cake";
123
+        String messedUpName = "NaME:cAkE";
124
+        //When
125
+        String actual = itemParser.findName(messedUpName);
126
+        //Then
127
+        Assert.assertEquals(expected,actual);
128
+    }
129
+
130
+    @Test
131
+    public void findCo0kieName() throws ItemParseException {
132
+        //Given
133
+        String expected = "cookies";
134
+        String messedUpName = "NaME:co0kies";
135
+        //When
136
+        String actual = itemParser.findName(messedUpName);
137
+        //Then
138
+        Assert.assertEquals(expected,actual);
139
+    }
140
+
141
+    @Test
142
+    public void findPriceTest() throws ItemParseException {
143
+        //Given
144
+        String expected = "2.50";
145
+        String messedUpPrice = "PriCe:2.50";
146
+        //When
147
+        String actual = itemParser.findPrice(messedUpPrice);
148
+        //Then
149
+        Assert.assertEquals(expected,actual);
150
+    }
151
+
152
+    @Test
153
+    public void findTypeTest() throws ItemParseException {
154
+        //Given
155
+        String expected = "food";
156
+        String messedUpType = "TypE:FoOd";
157
+        //When
158
+        String actual = itemParser.findType(messedUpType);
159
+        //Then
160
+        Assert.assertEquals(expected,actual);
161
+    }
162
+
163
+    @Test
164
+    public void findExpirationTest() throws ItemParseException {
165
+        //Given
166
+        String expected = "1/24/2018";
167
+        String messedUpExpiration = "expirATIOn:1/24/2018";
168
+        //When
169
+        String actual = itemParser.findExpiration(messedUpExpiration);
170
+        //Then
171
+        Assert.assertEquals(expected,actual);
172
+    }
77 173
 }