浏览代码

Merge 5833f1fac8254df93af8be5114e6ba73174ab0d9 into 23c2c01bf07b924ac9b860fa2771bf6ceb246ad5

Jordan Eldridge 6 年前
父节点
当前提交
2f7cf84ac1
没有帐户链接到提交者的电子邮件

+ 4
- 4
src/main/java/io/zipcoder/Item.java 查看文件

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

+ 1
- 0
src/main/java/io/zipcoder/ItemParseException.java 查看文件

@@ -1,4 +1,5 @@
1 1
 package io.zipcoder;
2 2
 
3 3
 public class ItemParseException extends Exception {
4
+
4 5
 }

+ 180
- 5
src/main/java/io/zipcoder/ItemParser.java 查看文件

@@ -2,30 +2,205 @@ package io.zipcoder;
2 2
 
3 3
 import java.util.ArrayList;
4 4
 import java.util.Arrays;
5
+import java.util.HashMap;
6
+import java.util.Map;
7
+import java.util.regex.Matcher;
8
+import java.util.regex.Pattern;
5 9
 
6 10
 public class ItemParser {
7 11
 
12
+    private Pattern pattern;
13
+
14
+    private Matcher matcher;
15
+
16
+    public int counter = 0;
17
+
18
+
19
+
20
+    private Map<String ,ArrayList<Item>> groceryListMap = new HashMap<String, ArrayList<Item>>();
21
+    // Splits string based on ##
8 22
 
9 23
     public ArrayList<String> parseRawDataIntoStringArray(String rawData){
10 24
         String stringPattern = "##";
11 25
         ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawData);
12 26
         return response;
13 27
     }
28
+    public Item parseStringIntoItem(String rawItem) throws ItemParseException {
29
+        if (findName(rawItem) == null || findPrice(rawItem) == null) {
30
+            throw new ItemParseException();
14 31
 
15
-    public Item parseStringIntoItem(String rawItem) throws ItemParseException{
16
-        return null;
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);
17 39
     }
18 40
 
19 41
     public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){
20
-        String stringPattern = "[;|^]";
42
+        String stringPattern = "[^|;]";
21 43
         ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawItem);
22 44
         return response;
23 45
     }
24
-
25
-    private ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString){
46
+    private ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString) {
26 47
         return new ArrayList<String>(Arrays.asList(inputString.split(stringPattern)));
27 48
     }
28 49
 
29 50
 
30 51
 
52
+
53
+
54
+
55
+    public String findName(String rawItem){
56
+        String search = "(?<=([Nn][Aa][Mm][Ee][^A-Za-z])).*?(?=[^A-Za-z0])";
57
+        pattern = Pattern.compile(search);
58
+        matcher = pattern.matcher(rawItem);
59
+
60
+        if (matcher.find()){
61
+            if (!matcher.group().equals("")){
62
+                String name = matcher.group().replaceAll("\\d","o");
63
+                return name.toLowerCase();
64
+            }
65
+        }
66
+        return null;
67
+    }
68
+    public String findPrice(String rawItem){
69
+        pattern =Pattern.compile("(?<=([Pp][Rr][Ii][Cc][Ee][^A-Za-z])).*?(?=[^0-9.])");
70
+        matcher = pattern.matcher(rawItem);
71
+
72
+        if (matcher.find()){
73
+            if (!matcher.group().equals("")){
74
+                return matcher.group();
75
+            }
76
+        }
77
+        return null;
78
+    }
79
+    public String findType(String rawItem){
80
+        Pattern pattern =Pattern.compile("(?<=([Tt][Yy][Pp][Ee][^A-Za-z])).*?(?=[^A-Za-z0])");
81
+        Matcher regMatcher =pattern.matcher(rawItem);
82
+
83
+        if (regMatcher.find()){
84
+            return (regMatcher).group().toLowerCase();
85
+
86
+            } else return null;
87
+        }
88
+
89
+
90
+
91
+    public String findExpiration(String rawItem){
92
+        Pattern pattern = Pattern.compile("(?<=([Tt][Yy][Pp][Ee][^A-Za-z])).*?(?=[^A-Za-z0])(.) + [^#]");
93
+        Matcher regMatcher2 = pattern.matcher(rawItem);
94
+
95
+        if (regMatcher2.find()){
96
+            return (regMatcher2).group();
97
+
98
+        } else return null;
99
+    }
100
+    public Map<String, ArrayList<Item>> getMap() throws Exception{
101
+        Main main = new Main();
102
+
103
+
104
+        ArrayList<String> listOfItems = parseRawDataIntoStringArray(main.readRawDataToString());
105
+
106
+        for (String item : listOfItems){
107
+            try {
108
+                Item newItem = parseStringIntoItem(item);
109
+                if (!groceryListMap.containsKey(newItem.getName())){
110
+                    ArrayList<Item> myItem = new ArrayList<Item>();
111
+                    myItem.add(newItem);
112
+                    groceryListMap.put(newItem.getName(),myItem);
113
+                }else {
114
+                    groceryListMap.get(newItem.getName()).add(newItem);
115
+                }
116
+            }catch (ItemParseException e){
117
+                counter++;
118
+            }
119
+        }
120
+        return groceryListMap;
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
+
142
+
143
+    public String display() throws Exception{
144
+        groceryListMap = getMap();
145
+        StringBuilder displayBuild = new StringBuilder();
146
+
147
+        for (Map.Entry<String,ArrayList<Item>> item: groceryListMap.entrySet()){
148
+            String upperCase = item.getKey().substring(0,1).toUpperCase() + item.getKey().substring(1);
149
+
150
+            displayBuild.append("\n" + String.format("%-5s%10s%15s%2d%5s", "name:", upperCase, "seen: ", item.getValue().size(), "  times"));
151
+            displayBuild.append("\n" + String.format("%15s%3s%5s", "===============", "\t\t\t", "===============") + "\n");
152
+
153
+            ArrayList<Double> uniquePriceList = getUniquePrices(item);
154
+            for (int i = 0; i < uniquePriceList.size(); i++) {
155
+                displayBuild.append(String.format("%-11s%.2f%15s%2d%5s", "Price:", uniquePriceList.get(i), "seen: ", getPriceOccurences(item.getValue(), uniquePriceList.get(i)), "  times"));
156
+                displayBuild.append("\n" + String.format("%15s%3s%5s", "---------------", "\t\t\t", "---------------") + "\n");
157
+            }
158
+
159
+        }
160
+        displayBuild.append("\n" + String.format("%-20s%10s%2d%5s", "Errors", "seen: ", counter, "  times"));
161
+
162
+        return displayBuild.toString();
163
+
164
+    }
165
+    public int getOccurencesOfItems(ArrayList list) {
166
+        return list.size();
167
+    }
168
+
169
+    public int getPriceOccurences(ArrayList<Item> list, Double price) {
170
+        int countPrice =0;
171
+        for (int i =0; i < list.size();i++){
172
+            if (list.get(i).getPrice().equals(price)){
173
+                countPrice++;
174
+            }
175
+        }
176
+        return countPrice;
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
+    }
189
+
190
+
191
+    private ArrayList<Double> getUniquePrices(Map.Entry<String, ArrayList<Item>> item) {
192
+        ArrayList<Double> uniquePrice = new ArrayList<Double>();
193
+        for (int i = 0; i < item.getValue().size();i++){
194
+            if (!uniquePrice.contains(item.getValue().get(i).getPrice()));
195
+            uniquePrice.add(item.getValue().get(i).getPrice());
196
+
197
+        }
198
+        return uniquePrice;
199
+
200
+    }
201
+    public String captitalizeFirstLetter(String input) {
202
+        return input.substring(0, 1).toUpperCase() + input.substring(1).toLowerCase();
203
+    }
204
+
205
+
31 206
 }

+ 2
- 0
src/main/java/io/zipcoder/Main.java 查看文件

@@ -15,5 +15,7 @@ public class Main {
15 15
         String output = (new Main()).readRawDataToString();
16 16
         System.out.println(output);
17 17
         // TODO: parse the data in output into items, and display to console.
18
+        ItemParser itemParser = new ItemParser();
19
+        System.out.println(itemParser.display());
18 20
     }
19 21
 }

+ 54
- 1
src/test/java/io/zipcoder/ItemParserTest.java 查看文件

@@ -59,4 +59,57 @@ public class ItemParserTest {
59 59
         Integer actual = itemParser.findKeyValuePairsInRawItemData(rawSingleItemIrregularSeperatorSample).size();
60 60
         assertEquals(expected, actual);
61 61
     }
62
-}
62
+    @Test
63
+    public void findNameTest(){
64
+        String expected = "milk";
65
+        String actual = itemParser.findName(rawSingleItem);
66
+        Assert.assertEquals(expected,actual);
67
+
68
+    }
69
+    @Test
70
+    public void findPriceTest() {
71
+        String expected = "3.23";
72
+        String actual = itemParser.findPrice(rawSingleItem);
73
+        Assert.assertEquals(expected, actual);
74
+    }
75
+    @Test
76
+    public void findExpirationTest() {
77
+        String expected = "1/25/2016";
78
+        String actual = itemParser.findExpiration(rawSingleItem);
79
+        Assert.assertEquals(expected, actual);
80
+    }
81
+
82
+    @Test
83
+    public void findTypeTest() {
84
+        String expected = "food";
85
+        String actual = itemParser.findType(rawSingleItem);
86
+        Assert.assertEquals(expected, actual);
87
+    }
88
+
89
+    @Test
90
+    public void display() throws Exception{
91
+        String expected = "\n" + "name:     Bread         seen:  6  times\n" + "===============\t\t\t===============\n" +"Price:     1.23         seen:  6  times\n" + "---------------\t\t\t---------------\n" + "\n" + "name:      Milk         seen:  6  times\n" + "===============\t\t\t===============\n" + "Price:     3.23         seen:  5  times\n" + "---------------\t\t\t---------------\n" + "Price:     1.23         seen:  1  times\n" + "---------------\t\t\t---------------\n" + "\n" + "name:    Apples         seen:  4  times\n" + "===============\t\t\t===============\n" +
92
+                "Price:     0.25         seen:  2  times\n" + "---------------\t\t\t---------------\n" + "Price:     0.23         seen:  2  times\n" + "---------------\t\t\t---------------\n" + "\n" + "name:   Cookies         seen:  8  times\n" + "===============\t\t\t===============\n" + "Price:     2.25         seen:  8  times\n" + "---------------\t\t\t---------------\n" + "\n" + "Errors                  seen:  4  times";
93
+        String actual = itemParser.display();
94
+        Assert.assertEquals(expected,actual);
95
+
96
+
97
+    }
98
+    @Test
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
+
112
+        Assert.assertEquals(expected,actual);
113
+    }
114
+
115
+    }