Katrice Williams-Dredden 6 years ago
parent
commit
a48da22f1c

+ 76
- 38
src/main/java/io/zipcoder/ItemParser.java View File

@@ -1,22 +1,27 @@
1 1
 package io.zipcoder;
2 2
 
3
-import java.util.ArrayList;
4
-import java.util.Arrays;
5
-import java.util.Map;
6
-import java.util.TreeMap;
3
+import java.util.*;
7 4
 import java.util.regex.Matcher;
8 5
 import java.util.regex.Pattern;
9 6
 
10 7
 public class ItemParser {
8
+
9
+
11 10
     //to organize my Key/Value pairs
12
-    private int exceptions = 0;
13
-    private TreeMap<String, ArrayList<Item>> groceryMap;
11
+    public static int exceptions = 0;
12
+    private HashMap<String, ArrayList<Item>> groceryMap;
14 13
 
15 14
     public ItemParser(){
16 15
         //initializing the TreeMap
17
-        groceryMap = new TreeMap<String, ArrayList<Item>>();
16
+        groceryMap = new HashMap<String, ArrayList<Item>>();
18 17
     }
19 18
 
19
+
20
+
21
+
22
+
23
+
24
+
20 25
 //splits a string of rawData by ## and puts it into an ArrayList called reponse
21 26
     public ArrayList<String> parseRawDataIntoStringArray(String rawData){
22 27
         String stringPattern = "##";
@@ -41,7 +46,7 @@ public class ItemParser {
41 46
         return item;
42 47
     }
43 48
 
44
-    public String findExpirationDate(String rawItem)throws ItemParseException {
49
+    public String findExpirationDate(String rawItem) {
45 50
         Pattern checkExpDateRegex = Pattern.compile("\\d\\/\\d+\\/\\d+");
46 51
         Matcher regexExpDateMatcher = checkExpDateRegex.matcher(rawItem);
47 52
 
@@ -51,7 +56,7 @@ public class ItemParser {
51 56
         return null;
52 57
     }
53 58
 
54
-    public String findType(String rawItem)throws ItemParseException {
59
+    public String findType(String rawItem){
55 60
         Pattern checkTypeRegex = Pattern.compile("(?<=([Tt][Yy][Pp][Ee][^A-Za-z])).*?(?=[^A-Za-z0])");
56 61
         Matcher regexTypeMatcher = checkTypeRegex.matcher(rawItem);
57 62
 
@@ -63,7 +68,7 @@ public class ItemParser {
63 68
         return null;
64 69
     }
65 70
 
66
-    public String findName(String rawItem)throws ItemParseException{
71
+    public String findName(String rawItem){
67 72
         Pattern checkNameRegex = Pattern.compile("(?<=([Nn][Aa][Mm][Ee][^A-Za-z])).*?(?=[^A-Za-z0])");
68 73
         Matcher regexNameMatcher = checkNameRegex.matcher(rawItem);
69 74
 
@@ -74,7 +79,7 @@ public class ItemParser {
74 79
         return null;
75 80
     }
76 81
 
77
-    public String findPrice(String rawItem) throws NumberFormatException{
82
+    public String findPrice(String rawItem){
78 83
         Pattern checkPriceRegex = Pattern.compile("\\d\\.\\d*");
79 84
         Matcher regexPriceMatcher = checkPriceRegex.matcher(rawItem);
80 85
 
@@ -96,7 +101,11 @@ public class ItemParser {
96 101
         return new ArrayList<String>(Arrays.asList(inputString.split(stringPattern)));
97 102
     }
98 103
 
99
-    public TreeMap<String, ArrayList<Item>> getGroceryMap()throws Exception {
104
+
105
+
106
+
107
+    public HashMap<String, ArrayList<Item>> getGroceryMap() throws Exception {
108
+
100 109
         Main main = new Main();
101 110
 
102 111
         ArrayList<String> items = parseRawDataIntoStringArray(main.readRawDataToString());
@@ -115,53 +124,82 @@ public class ItemParser {
115 124
             } catch (ItemParseException e){
116 125
                 exceptions++;
117 126
             }
118
-            }
127
+        }
119 128
         return groceryMap;
120 129
     }
121 130
 
122 131
 
123 132
     public String printGroceries() throws Exception{
124
-
125 133
         groceryMap = getGroceryMap();
126 134
 
127 135
         StringBuilder sb = new StringBuilder();
128 136
 
129
-        //how do I print grocery list
130
-
131 137
         for(Map.Entry<String, ArrayList<Item>> namesAndItems : groceryMap.entrySet()){
132
-            String makeUpperCase = namesAndItems.getKey().substring(0,1).toUpperCase() + namesAndItems.getKey().substring(1);
138
+            sb.append("\nname: ");
139
+            sb.append(String.format("%8s",captitalizeFirstLetter(namesAndItems.getKey())));
140
+            sb.append("\t\t\t\tseen: "+getOccurencesOfItems(namesAndItems.getValue())+" times\n");
141
+            sb.append("==============="+"\t\t\t\t===============\n");
142
+            String priceList = generatePriceList(namesAndItems);
143
+            sb.append(priceList);
144
+            sb.append("---------------"+"\t\t\t\t---------------\n");
145
+            }
133 146
 
134
-            sb.append("\n" + "name: " + makeUpperCase + "\t\t\t\t" + "seen: " + namesAndItems.getValue().size() + " times");
135
-            sb.append("\n" + "------------------------------------------");
147
+            sb.append("\nErrors\t\t\t\t\t\tseen: "+exceptions+" times\n");
136 148
 
137
-            ArrayList<Double> getDiffPrices = getDifferentPrices(namesAndItems);
138
-            for (int i = 0; i < getDiffPrices.size(); i++) {
139
-                if (getPriceOccurrences(namesAndItems.getValue(), getDiffPrices.get(i)) == 1) {
140
-                    String time = " time";
141
-                } else {
142
-                    String time = " times";
143
-                    sb.append("\n" + "Price: " + getDiffPrices.get(i) + "\t\t\t\t" + " seen: " + getPriceOccurrences(namesAndItems.getValue(), getDiffPrices.get(i)) + " "+time);
144
-                    sb.append("\n" + "==========================================");
145
-                    }
146
-                }
149
+            return sb.toString();
147 150
 
148
-            }
149
-        sb.append("\n\n" + "Errors: " + exceptions + " times\n\n");
150
-        sb.append("\n" + "------------------------------------------");
151
-        return sb.toString();
152 151
 
152
+    }
153
+
154
+
155
+    public String generatePriceList(Map.Entry<String, ArrayList<Item>> input) {
156
+        String priceList = "";
157
+        ArrayList<Double> nonDupPrices = getDifferentPrices(input);
158
+        for(int i=0;i<nonDupPrices.size();i++){
159
+            priceList+="Price";
160
+            priceList+=(String.format("%10s",nonDupPrices.get(i)));
161
+            priceList+=("\t\t\t\tseen: "+ getPriceOccurrences(input.getValue(),nonDupPrices.get(i))+
162
+                    " times\n");
163
+        }
164
+        return priceList;
165
+
166
+    }
153 167
 
168
+    public String captitalizeFirstLetter(String input) {
169
+        return input.substring(0, 1).toUpperCase() + input.substring(1).toLowerCase();
154 170
     }
155 171
 
156
-    private ArrayList<Double> getDifferentPrices(Map.Entry<String, ArrayList<Item>> namesAndItems) {
157 172
 
158
-        return null;
173
+    public int getOccurencesOfItems(ArrayList list) {
174
+        return list.size();
175
+    }
176
+
177
+
178
+
179
+
180
+
181
+    private ArrayList<Double> getDifferentPrices(Map.Entry<String, ArrayList<Item>> items) {
182
+
183
+        ArrayList<Double> differentPrices = new ArrayList<Double>();
184
+
185
+        for(int i =0; i<items.getValue().size(); i++){
186
+            if(!differentPrices.contains(items.getValue().get(i).getPrice())){
187
+                differentPrices.add(items.getValue().get(i).getPrice());
188
+            }
189
+        }
190
+
191
+        return differentPrices;
159 192
     }
160 193
 
161
-    private boolean getPriceOccurrences(ArrayList<Item> value, Double aDouble) {
162
-        //retrieve the prices
194
+    private int getPriceOccurrences(ArrayList<Item> value, Double aDouble) {
195
+        int price = 0;
163 196
 
164
-        return Boolean.parseBoolean(null);
197
+        for(int i = 0; i<value.size(); i++){
198
+            if(value.get(i).getPrice().equals(aDouble)){
199
+                price++;
200
+            }
201
+        }
202
+        return price;
165 203
     }
166 204
 
167 205
 

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

@@ -2,6 +2,8 @@ package io.zipcoder;
2 2
 
3 3
 import org.apache.commons.io.IOUtils;
4 4
 
5
+import java.util.ArrayList;
6
+
5 7
 
6 8
 public class Main {
7 9
 
@@ -15,5 +17,12 @@ public class Main {
15 17
         String output = (new Main()).readRawDataToString();
16 18
         System.out.println(output);
17 19
         // TODO: parse the data in output into items, and display to console.
20
+
21
+        ItemParser parser = new ItemParser();
22
+        Main main = new Main();
23
+
24
+        ArrayList<String> item = parser.parseRawDataIntoStringArray(output);
25
+
26
+        System.out.println(parser.printGroceries());
18 27
     }
19 28
 }

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

@@ -30,37 +30,21 @@ public class ItemParserTest {
30 30
     @Test
31 31
     public void testFindExpirationDate(){
32 32
         String expected = "1/11/2016";
33
-        String actual = null;
34
-        try {
35
-            actual = itemParser.findExpirationDate(rawSingleItemIrregularSeperatorSample);
36
-        } catch (ItemParseException e) {
37
-            e.printStackTrace();
38
-        }
33
+        String actual = itemParser.findExpirationDate(rawSingleItemIrregularSeperatorSample);//
39 34
         Assert.assertEquals(expected, actual);
40 35
     }
41 36
 
42 37
     @Test
43 38
     public void testFindType(){
44 39
         String expected = "food";
45
-        String actual = null;
46
-        try {
47
-            actual = itemParser.findType(rawBrokenSingleItem);
48
-        } catch (ItemParseException e) {
49
-            e.printStackTrace();
50
-        }
40
+        String actual = itemParser.findType(rawBrokenSingleItem);
51 41
         Assert.assertEquals(expected, actual);
52 42
     }
53 43
 
54 44
     @Test
55 45
     public void testFindName(){
56 46
         String expected = "milk";
57
-        String actual = null;
58
-        try {
59
-            actual = itemParser.findName(rawSingleItem);
60
-        } catch (ItemParseException e) {
61
-            e.printStackTrace();
62
-        }
63
-
47
+        String actual = itemParser.findName(rawSingleItem);
64 48
         Assert.assertEquals(expected, actual);
65 49
     }
66 50
 
@@ -86,10 +70,11 @@ public class ItemParserTest {
86 70
         assertEquals(expected.toString(), actual.toString());
87 71
     }
88 72
 
89
-//    @Test(expected = ItemParseException.class)
90
-//    public void parseBrokenStringIntoItemTest() throws ItemParseException{
91
-//        itemParser.parseStringIntoItem(rawBrokenSingleItem);
92
-//    }
73
+    @Test(expected = ItemParseException.class)
74
+    public void parseBrokenStringIntoItemTest() throws ItemParseException{
75
+        itemParser.parseStringIntoItem(rawBrokenSingleItem);
76
+    }
77
+
93 78
 
94 79
     @Test
95 80
     public void findKeyValuePairsInRawItemDataTest(){