Browse Source

Completed Lab

Garrett Arant 6 years ago
parent
commit
6c7794afcd

+ 37
- 20
src/main/java/io/zipcoder/ItemParser.java View File

@@ -7,6 +7,7 @@ import java.util.regex.Pattern;
7 7
 public class ItemParser {
8 8
 
9 9
     private Integer countExceptionsThrown = 0;
10
+    private Integer count = 0;
10 11
 
11 12
     private ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString) {
12 13
         return new ArrayList<String>(Arrays.asList(inputString.split(stringPattern)));
@@ -25,20 +26,29 @@ public class ItemParser {
25 26
     }
26 27
 
27 28
     public Item parseStringIntoItem(String rawItem) throws ItemParseException {
28
-        String name = checkName(rawItem);
29
-        Double price = Double.valueOf(checkPrice(rawItem));
30
-        String type = checkType(rawItem);
31
-        String expiration = checkExpiration(rawItem);
29
+            if (checkName(rawItem) == null || checkPrice(rawItem) == null || checkType(rawItem) == null
30
+                    || checkExpiration(rawItem) == null) {
31
+                throw new ItemParseException();
32
+            }
33
+
34
+            String name = checkName(rawItem);
35
+            Double price = Double.valueOf(checkPrice(rawItem));
36
+            String type = checkType(rawItem);
37
+            String expiration = checkExpiration(rawItem);
32 38
 
33
-        return new Item(name, price, type, expiration);
39
+            return new Item(name, price, type, expiration);
34 40
     }
35 41
 
36
-    public ArrayList<Item> createItemArrayList(String rawData) throws ItemParseException{
42
+    public ArrayList<Item> createItemArrayList(String rawData) {
37 43
         ArrayList<String> temp = parseRawDataIntoStringArray(rawData);
38 44
         ArrayList<Item> itemArrayList = new ArrayList<Item>();
39 45
 
40 46
         for (int i = 0; i <temp.size() ; i++) {
41
-            itemArrayList.add(parseStringIntoItem(temp.get(i)));
47
+            try {
48
+                itemArrayList.add(parseStringIntoItem(temp.get(i)));
49
+            } catch (ItemParseException e) {
50
+                count++;
51
+            }
42 52
         }
43 53
         return itemArrayList;
44 54
     }
@@ -66,20 +76,20 @@ public class ItemParser {
66 76
         return priceTotals;
67 77
     }
68 78
 
69
-    public Map<Double, Integer> itemTypeMapWithCounts(String rawData, String filterType) throws ItemParseException {
79
+    public Map<Double, Integer> itemTypeMapWithCounts(String rawData, String filterType)  {
70 80
         ArrayList<Item> itemArrayList = createItemArrayList(rawData);
71 81
         ArrayList<Item> filterItemArrayList = filterItemArrayList(itemArrayList, filterType);
72 82
         Map<Double, Integer> priceTotals = individualItemCount(filterItemArrayList);
73 83
         return priceTotals;
74 84
     }
75 85
 
76
-    public Integer totalTimesItemSeen(String rawData, String filterType) throws ItemParseException {
86
+    public Integer totalTimesItemSeen(String rawData, String filterType)  {
77 87
         ArrayList<Item> itemArrayList = createItemArrayList(rawData);
78 88
         ArrayList<Item> filterItemArrayList = filterItemArrayList(itemArrayList, filterType);
79 89
         return filterItemArrayList.size();
80 90
     }
81 91
 
82
-    public ArrayList<String> filterTypeArrayList(String rawData) throws ItemParseException {
92
+    public ArrayList<String> filterTypeArrayList(String rawData)  {
83 93
         ArrayList<Item> itemArrayList = createItemArrayList(rawData);
84 94
         ArrayList<String> filterType = new ArrayList<String>();
85 95
 
@@ -112,7 +122,7 @@ public class ItemParser {
112 122
         return sb.toString();
113 123
     }
114 124
 
115
-    public String formatPriceField(String rawData, String filterType) throws ItemParseException {
125
+    public String formatPriceField(String rawData, String filterType)  {
116 126
         Map<Double, Integer> priceTotals = itemTypeMapWithCounts(rawData, filterType);
117 127
         StringBuilder sb = new StringBuilder();
118 128
         Set mapSet = (Set) priceTotals.entrySet();
@@ -123,20 +133,27 @@ public class ItemParser {
123 133
             Object keyValue = mapEntry.getKey();
124 134
             //getValue method returns corresponding key's value
125 135
             Object value = mapEntry.getValue();
126
-            sb.append("Price:   " + keyValue + "\t\t seen: " + value + " times" + "\n");
136
+            String time;
137
+            if(value.equals(1)){
138
+                 time = " time";
139
+            }
140
+            else{
141
+                 time = " times";
142
+            }
143
+            sb.append("Price:   " + keyValue + "\t\t seen: " + value + time + "\n");
127 144
             sb.append("-------------\t\t -------------\n");
128 145
         }
129 146
         return sb.toString();
130 147
     }
131 148
 
132
-    public String checkName(String input) throws ItemParseException {
149
+    public String checkName(String input) {
133 150
         String newInput = fixCookie(input);
134 151
         Pattern patternName = Pattern.compile("([Nn]..[Ee]:)(\\w+)");
135 152
         Matcher matcherName= patternName.matcher(newInput);
136 153
 
137 154
         if (matcherName.find())
138 155
             return matcherName.group(2).toLowerCase();
139
-        else throw new ItemParseException();
156
+        else return null;
140 157
     }
141 158
 
142 159
     public String fixCookie(String input){
@@ -145,31 +162,31 @@ public class ItemParser {
145 162
         return matcherCookie.replaceAll("cookies");
146 163
     }
147 164
 
148
-    public String checkPrice(String input) throws ItemParseException{
165
+    public String checkPrice(String input) {
149 166
         Pattern patternPrice = Pattern.compile("([Pp]...[Ee]:)(\\d\\.\\d{2})");
150 167
         Matcher matcherPrice= patternPrice.matcher(input);
151 168
 
152 169
         if (matcherPrice.find())
153 170
             return matcherPrice.group(2);
154
-        else throw new ItemParseException();
171
+        else return null;
155 172
     }
156 173
 
157
-    public String checkType(String input) throws ItemParseException{
174
+    public String checkType(String input) {
158 175
         Pattern patternType = Pattern.compile("([Tt]..[Ee]:)(\\w+)");
159 176
         Matcher matcherType = patternType.matcher(input);
160 177
 
161 178
         if (matcherType.find())
162 179
             return matcherType.group(2).toLowerCase();
163
-        else throw new ItemParseException();
180
+        else return null;
164 181
     }
165 182
 
166
-    public String checkExpiration(String input) throws ItemParseException{
183
+    public String checkExpiration(String input) {
167 184
         Pattern patternExpiration = Pattern.compile("([Ee]........[Nn]:)(\\d\\/\\d{2}\\/\\d{4})");
168 185
         Matcher matcherExpiration = patternExpiration.matcher(input);
169 186
 
170 187
         if (matcherExpiration.find())
171 188
             return matcherExpiration.group(2);
172
-        else throw new ItemParseException();
189
+        else return null;
173 190
     }
174 191
 
175 192
     public Integer getExceptionsThrown(String rawData)  {

+ 1
- 0
src/main/java/io/zipcoder/Main.java View File

@@ -14,6 +14,7 @@ public class Main {
14 14
     public static void main(String[] args) throws Exception{
15 15
         String output = (new Main()).readRawDataToString();
16 16
         ItemParser item = new ItemParser();
17
+        ItemParseException itemParseException = new ItemParseException();
17 18
         System.out.println(item.formatText(output));
18 19
         // TODO: parse the data in output into items, and display to console.
19 20
     }

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

@@ -75,14 +75,14 @@ public class ItemParserTest {
75 75
     }
76 76
 
77 77
     @Test
78
-    public void createItemArrayListTest() throws ItemParseException {
78
+    public void createItemArrayListTest() {
79 79
         String expected ="name:milk price:3.23 type:food expiration:1/25/2016";
80 80
         String actual  = itemParser.createItemArrayList(rawMultipleItems).get(0).toString();
81 81
         assertEquals(expected, actual);
82 82
     }
83 83
 
84 84
     @Test
85
-    public void filterItemArrayListTest() throws ItemParseException {
85
+    public void filterItemArrayListTest() {
86 86
         ArrayList<Item> filterItemArrayListTester = itemParser.createItemArrayList(rawMultipleItems);
87 87
         String expected ="[name:bread price:1.23 type:food expiration:1/02/2016, name:bread price:1.23 type:food expiration:2/25/2016]";
88 88
         String actual  = itemParser.filterItemArrayList(filterItemArrayListTester,"bread").toString();
@@ -91,7 +91,7 @@ public class ItemParserTest {
91 91
     }
92 92
 
93 93
     @Test
94
-    public void individualItemCountTest1() throws ItemParseException {
94
+    public void individualItemCountTest1() {
95 95
         ArrayList<Item> individualItemCountTester = itemParser.filterItemArrayList(itemParser.createItemArrayList(rawMultipleItems),"bread");
96 96
         String expected ="{1.23=2}";
97 97
         String actual  = itemParser.individualItemCount(individualItemCountTester).toString();
@@ -100,7 +100,7 @@ public class ItemParserTest {
100 100
     }
101 101
 
102 102
     @Test
103
-    public void individualItemCountTest2() throws ItemParseException {
103
+    public void individualItemCountTest2()  {
104 104
         ArrayList<Item> individualItemCountTester = itemParser.filterItemArrayList(itemParser.createItemArrayList(rawMultipleItems2),"bread");
105 105
         String expected ="{3.23=2, 2.23=1, 1.23=2}";
106 106
         String actual  = itemParser.individualItemCount(individualItemCountTester).toString();
@@ -132,14 +132,14 @@ public class ItemParserTest {
132 132
     public void formatTextTest() throws ItemParseException {
133 133
         String expected = "name:    Milk\t \t seen: 1 times\n" +
134 134
                 "============= \t \t =============\n" +
135
-                "Price:   3.23\t\t seen: 1 times\n" +
135
+                "Price:   3.23\t\t seen: 1 time\n" +
136 136
                 "-------------\t\t -------------\n" +
137 137
                 "\n" +
138 138
                 "name:   Bread\t \t seen: 5 times\n" +
139 139
                 "============= \t \t =============\n" +
140 140
                 "Price:   3.23\t\t seen: 2 times\n" +
141 141
                 "-------------\t\t -------------\n" +
142
-                "Price:   2.23\t\t seen: 1 times\n" +
142
+                "Price:   2.23\t\t seen: 1 time\n" +
143 143
                 "-------------\t\t -------------\n" +
144 144
                 "Price:   1.23\t\t seen: 2 times\n" +
145 145
                 "-------------\t\t -------------\n" +
@@ -153,7 +153,7 @@ public class ItemParserTest {
153 153
     public void formatPriceFieldTest() throws ItemParseException {
154 154
         String expected = "Price:   3.23\t\t seen: 2 times\n" +
155 155
                 "-------------\t\t -------------\n" +
156
-                "Price:   2.23\t\t seen: 1 times\n" +
156
+                "Price:   2.23\t\t seen: 1 time\n" +
157 157
                 "-------------\t\t -------------\n" +
158 158
                 "Price:   1.23\t\t seen: 2 times\n" +
159 159
                 "-------------\t\t -------------\n";
@@ -162,7 +162,7 @@ public class ItemParserTest {
162 162
     }
163 163
 
164 164
     @Test
165
-    public void checkNameTest1() throws ItemParseException{
165
+    public void checkNameTest1() {
166 166
         String rawItemTest = "naMe:c00kies;price:3.23;type:Food;expiration:1/25/2016##";
167 167
         String expected = "cookies";
168 168
         String  actual = itemParser.checkName(rawItemTest);
@@ -170,7 +170,7 @@ public class ItemParserTest {
170 170
     }
171 171
 
172 172
     @Test
173
-    public void checkNameTest2() throws ItemParseException{
173
+    public void checkNameTest2() {
174 174
         String rawItemTest = "naMe:mIlk;price:3.23;type:Food;expiration:1/25/2016##";
175 175
         String expected = "milk";
176 176
         String  actual = itemParser.checkName(rawItemTest);
@@ -202,7 +202,7 @@ public class ItemParserTest {
202 202
     }
203 203
 
204 204
     @Test
205
-    public void getExceptionsThrownTest() throws ItemParseException{
205
+    public void getExceptionsThrownTest() {
206 206
         Integer expected = 3;
207 207
         Integer  actual = itemParser.getExceptionsThrown(rawMultipleItemsBroken);
208 208
         assertEquals(expected, actual);