Browse Source

this is what i have

Jordan Elderidge 6 years ago
parent
commit
5833f1fac8

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

@@ -21,6 +21,9 @@ public class Item {
21 21
         this.type = type;
22 22
         this.expiration = expiration;
23 23
     }
24
+    public String toString(){
25
+        return "name:" + name + "price:" + price + "type:" + type + "expiration:" + expiration;
26
+    }
24 27
 
25 28
     public String getName() {
26 29
         return name;
@@ -41,8 +44,5 @@ public class Item {
41 44
         return expiration;
42 45
     }
43 46
 
44
-    @Override
45
-    public String toString(){
46
-        return "name:" + name + " price:" + price + " type:" + type + " expiration:" + expiration;
47
-    }
47
+
48 48
 }

+ 73
- 27
src/main/java/io/zipcoder/ItemParser.java View File

@@ -10,19 +10,36 @@ import java.util.regex.Pattern;
10 10
 public class ItemParser {
11 11
 
12 12
     private Pattern pattern;
13
+
13 14
     private Matcher matcher;
14
-    private int counter = 0;
15
-    private Main main = new Main();
15
+
16
+    public int counter = 0;
17
+
18
+
19
+
16 20
     private Map<String ,ArrayList<Item>> groceryListMap = new HashMap<String, ArrayList<Item>>();
17 21
     // Splits string based on ##
22
+
18 23
     public ArrayList<String> parseRawDataIntoStringArray(String rawData){
19 24
         String stringPattern = "##";
20 25
         ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawData);
21 26
         return response;
22 27
     }
28
+    public Item parseStringIntoItem(String rawItem) throws ItemParseException {
29
+        if (findName(rawItem) == null || findPrice(rawItem) == null) {
30
+            throw new ItemParseException();
31
+
32
+        }
33
+        String Name = findName(rawItem);
34
+        Double Price = Double.parseDouble(findPrice(rawItem));
35
+        String Type = findType(rawItem);
36
+        String Expiration = findExpiration(rawItem);
37
+
38
+        return new Item(Name, Price, Type, Expiration);
39
+    }
23 40
 
24 41
     public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){
25
-        String stringPattern = "[^|*|@|!|$|%|&|;]";
42
+        String stringPattern = "[^|;]";
26 43
         ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawItem);
27 44
         return response;
28 45
     }
@@ -30,21 +47,11 @@ public class ItemParser {
30 47
         return new ArrayList<String>(Arrays.asList(inputString.split(stringPattern)));
31 48
     }
32 49
 
33
-    public Item parseStringIntoItem(String rawItem) throws ItemParseException {
34
-        if (findName(rawItem) == null || findPrice(rawItem) == null){
35
-            throw new ItemParseException();
36 50
 
37
-        }
38
-        String itemName = findName(rawItem);
39
-        Double ittemPrice = Double.parseDouble(findPrice(rawItem));
40
-        String itemType = findType(rawItem);
41
-        String itemExpiration = findExpiration(rawItem);
42 51
 
43
-        return new Item(itemName,ittemPrice,itemType,itemExpiration);
44 52
 
45 53
 
46 54
 
47
-    }
48 55
     public String findName(String rawItem){
49 56
         String search = "(?<=([Nn][Aa][Mm][Ee][^A-Za-z])).*?(?=[^A-Za-z0])";
50 57
         pattern = Pattern.compile(search);
@@ -70,29 +77,30 @@ public class ItemParser {
70 77
         return null;
71 78
     }
72 79
     public String findType(String rawItem){
73
-        pattern =Pattern.compile("(?<=([Tt][Yy][Pp][Ee][^A-Za-z])).*?(?=[^A-Za-z0])");
74
-        matcher.pattern().matcher(rawItem);
80
+        Pattern pattern =Pattern.compile("(?<=([Tt][Yy][Pp][Ee][^A-Za-z])).*?(?=[^A-Za-z0])");
81
+        Matcher regMatcher =pattern.matcher(rawItem);
75 82
 
76
-        if (matcher.find()){
77
-            return matcher.group().toLowerCase();
83
+        if (regMatcher.find()){
84
+            return (regMatcher).group().toLowerCase();
78 85
 
79
-            }
80
-        return null;
86
+            } else return null;
81 87
         }
82 88
 
83 89
 
84 90
 
85 91
     public String findExpiration(String rawItem){
86
-        pattern = Pattern.compile("(?<=([Tt][Yy][Pp][Ee][^A-Za-z])).*?(?=[^A-Za-z0])(.) + [^#]");
87
-        matcher = pattern.matcher(rawItem);
92
+        Pattern pattern = Pattern.compile("(?<=([Tt][Yy][Pp][Ee][^A-Za-z])).*?(?=[^A-Za-z0])(.) + [^#]");
93
+        Matcher regMatcher2 = pattern.matcher(rawItem);
88 94
 
89
-        if (matcher.find()){
90
-            return matcher.group();
91
-        }
95
+        if (regMatcher2.find()){
96
+            return (regMatcher2).group();
92 97
 
93
-        return null;
98
+        } else return null;
94 99
     }
95 100
     public Map<String, ArrayList<Item>> getMap() throws Exception{
101
+        Main main = new Main();
102
+
103
+
96 104
         ArrayList<String> listOfItems = parseRawDataIntoStringArray(main.readRawDataToString());
97 105
 
98 106
         for (String item : listOfItems){
@@ -111,6 +119,26 @@ public class ItemParser {
111 119
         }
112 120
         return groceryListMap;
113 121
     }
122
+    public String generateReport() throws Exception {
123
+        groceryListMap = getMap();
124
+        StringBuilder builder = new StringBuilder();
125
+
126
+        for (Map.Entry<String, ArrayList<Item>> groceryItems : groceryListMap.entrySet()) {
127
+            builder.append("\nname: ");
128
+            builder.append(String.format("%8s", captitalizeFirstLetter(groceryItems.getKey())));
129
+            builder.append("\t\t\t\tseen: " + getOccurencesOfItems(groceryItems.getValue()) + " times\n");
130
+            builder.append("===============" + "\t\t\t\t===============\n");
131
+            String priceReport = generatePriceReport(groceryItems);
132
+            builder.append(priceReport);
133
+            builder.append("---------------" + "\t\t\t\t---------------\n");
134
+
135
+
136
+        }
137
+        builder.append("\nErrors\t\t\t\t\t\tseen: "+counter+" times\n");
138
+        return builder.toString();
139
+    }
140
+
141
+
114 142
 
115 143
     public String display() throws Exception{
116 144
         groceryListMap = getMap();
@@ -124,7 +152,7 @@ public class ItemParser {
124 152
 
125 153
             ArrayList<Double> uniquePriceList = getUniquePrices(item);
126 154
             for (int i = 0; i < uniquePriceList.size(); i++) {
127
-                displayBuild.append(String.format("%-11s%.2f%15s%2d%5s", "Price:", uniquePriceList.get(i), "seen: ", seenPriceOccurences(item.getValue(), uniquePriceList.get(i)), "  times"));
155
+                displayBuild.append(String.format("%-11s%.2f%15s%2d%5s", "Price:", uniquePriceList.get(i), "seen: ", getPriceOccurences(item.getValue(), uniquePriceList.get(i)), "  times"));
128 156
                 displayBuild.append("\n" + String.format("%15s%3s%5s", "---------------", "\t\t\t", "---------------") + "\n");
129 157
             }
130 158
 
@@ -134,7 +162,11 @@ public class ItemParser {
134 162
         return displayBuild.toString();
135 163
 
136 164
     }
137
-    public int seenPriceOccurences(ArrayList<Item> list, Double price) {
165
+    public int getOccurencesOfItems(ArrayList list) {
166
+        return list.size();
167
+    }
168
+
169
+    public int getPriceOccurences(ArrayList<Item> list, Double price) {
138 170
         int countPrice =0;
139 171
         for (int i =0; i < list.size();i++){
140 172
             if (list.get(i).getPrice().equals(price)){
@@ -143,6 +175,17 @@ public class ItemParser {
143 175
         }
144 176
         return countPrice;
145 177
     }
178
+    public String generatePriceReport(Map.Entry<String,ArrayList<Item>> input) {
179
+        String reportPrice = "";
180
+        ArrayList<Double> nonDuplicatePrices = getUniquePrices(input);
181
+        for (int i = 0; i < nonDuplicatePrices.size(); i++) {
182
+            reportPrice += "Price";
183
+            reportPrice += (String.format("%10s", nonDuplicatePrices.get(i)));
184
+            reportPrice += ("\t\t\t\tseen: " + getPriceOccurences(input.getValue(), nonDuplicatePrices.get(i)) + " times\n");
185
+
186
+        }
187
+        return reportPrice;
188
+    }
146 189
 
147 190
 
148 191
     private ArrayList<Double> getUniquePrices(Map.Entry<String, ArrayList<Item>> item) {
@@ -155,6 +198,9 @@ public class ItemParser {
155 198
         return uniquePrice;
156 199
 
157 200
     }
201
+    public String captitalizeFirstLetter(String input) {
202
+        return input.substring(0, 1).toUpperCase() + input.substring(1).toLowerCase();
203
+    }
158 204
 
159 205
 
160 206
 }

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

@@ -96,18 +96,18 @@ public class ItemParserTest {
96 96
 
97 97
     }
98 98
     @Test
99
-    public void seenPriceOccurencesTest(){
100
-        ArrayList<Item> listTest = new ArrayList<Item>();
101
-        Item itemTest = new Item("Bread", 3.12, "Food", "1/25/2016");
102
-        Item itemTest2 = new Item("Bread", 3.12, "Food", "1/25/2016");
103
-        Item itemTest3 = new Item("Bread", 2.00, "Food", "4/16/2016");
104
-
105
-        listTest.add(itemTest);
106
-        listTest.add(itemTest2);
107
-        listTest.add(itemTest3);
108
-
109
-        int expected = 2;
110
-        int actual = itemParser.seenPriceOccurences(listTest,3.12);
99
+    public void getPriceOccurencesTest() throws ItemParseException {
100
+        ArrayList<Item> test = new ArrayList<Item>();
101
+        Item item1 = itemParser.parseStringIntoItem(rawSingleItem);
102
+        String rawItem2 = ("naME:BreaD;price:1.23;type:Food;expiration:1/02/2016##");
103
+        String rawItem3 = "naMe:Bread;price:1.23;type:Food^expiration:1/11/2016##";
104
+        Item item2 = itemParser.parseStringIntoItem(rawItem2);
105
+        Item item3 = itemParser.parseStringIntoItem(rawItem3);
106
+        test.add(item1);
107
+        test.add(item2);
108
+        test.add(item3);
109
+        int expected =2;
110
+        int actual = itemParser.getPriceOccurences(test,1.23);
111 111
 
112 112
         Assert.assertEquals(expected,actual);
113 113
     }