瀏覽代碼

Merge 6418a07b31f8a52154e88e11d0ba0bad5cdbdc4f into 23c2c01bf07b924ac9b860fa2771bf6ceb246ad5

ktecle 6 年之前
父節點
當前提交
74d57ee13d
沒有帳戶連結到提交者的電子郵件

+ 12
- 0
pom.xml 查看文件

@@ -7,6 +7,18 @@
7 7
     <groupId>io.zipcoder</groupId>
8 8
     <artifactId>PainfullAfternoon</artifactId>
9 9
     <version>1.0-SNAPSHOT</version>
10
+    <build>
11
+        <plugins>
12
+            <plugin>
13
+                <groupId>org.apache.maven.plugins</groupId>
14
+                <artifactId>maven-compiler-plugin</artifactId>
15
+                <configuration>
16
+                    <source>1.7</source>
17
+                    <target>1.7</target>
18
+                </configuration>
19
+            </plugin>
20
+        </plugins>
21
+    </build>
10 22
 
11 23
     <dependencies>
12 24
         <dependency>

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

@@ -1,8 +1,9 @@
1 1
 package io.zipcoder;
2 2
 
3
+
3 4
 public class Item {
4
-    private String name;
5
-    private Double price;
5
+    private  String name;
6
+    private  Double price;
6 7
     private String type;
7 8
     private String expiration;
8 9
 
@@ -45,4 +46,5 @@ public class Item {
45 46
     public String toString(){
46 47
         return "name:" + name + " price:" + price + " type:" + type + " expiration:" + expiration;
47 48
     }
49
+
48 50
 }

+ 145
- 7
src/main/java/io/zipcoder/ItemParser.java 查看文件

@@ -2,30 +2,168 @@ 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
+    public static int counter = 0;
13
+    private HashMap<String, ArrayList<Item>> groceryList = new HashMap<String, ArrayList<Item>>();
8 14
 
9
-    public ArrayList<String> parseRawDataIntoStringArray(String rawData){
15
+
16
+    public ArrayList<String> parseRawDataIntoStringArray(String rawData) {
10 17
         String stringPattern = "##";
11
-        ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawData);
18
+        ArrayList<String> response = splitStringWithRegexPattern(stringPattern, rawData);
12 19
         return response;
13 20
     }
14 21
 
15
-    public Item parseStringIntoItem(String rawItem) throws ItemParseException{
16
-        return null;
22
+    public Item parseStringIntoItem(String rawItem) throws ItemParseException {
23
+
24
+        if (findName(rawItem) == null || findPrice(rawItem) == null) {
25
+            throw new ItemParseException();
26
+        }
27
+
28
+        String name = findName(rawItem);
29
+        Double price = Double.parseDouble(findPrice(rawItem));
30
+        String type = findType(rawItem);
31
+        String expirationDate = findExpirationDate(rawItem);
32
+
33
+        return new Item(name, price, type, expirationDate);
17 34
     }
18 35
 
19
-    public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){
36
+    public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem) {
20 37
         String stringPattern = "[;|^]";
21
-        ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawItem);
38
+        ArrayList<String> response = splitStringWithRegexPattern(stringPattern, rawItem);
22 39
         return response;
23 40
     }
24 41
 
25
-    private ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString){
42
+    private ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString) {
26 43
         return new ArrayList<String>(Arrays.asList(inputString.split(stringPattern)));
27 44
     }
28 45
 
46
+    public String findName(String rawItem) {
47
+
48
+        Pattern namePattern = Pattern.compile("(?<=([Nn][Aa][Mm][Ee][^A-Za-z])).*?(?=[^A-Za-z0])");
49
+        Matcher regexMatcher1 = namePattern.matcher(rawItem);
50
+        if (regexMatcher1.find()) {
51
+            if (!regexMatcher1.group().equals("")) {
52
+                String name = regexMatcher1.group().replaceAll("\\d", "o");
53
+                return name.toLowerCase();
54
+            }
55
+
56
+        }
57
+        return null;
58
+    }
59
+
60
+    public String findPrice(String rawItem) {
61
+        Pattern pricePattern = Pattern.compile("(?<=([Pp][Rr][Ii][Cc][Ee][^A-Za-z])).*?(?=[^0-9.])");
62
+        Matcher regexMatcher2 = pricePattern.matcher(rawItem);
63
+        if (regexMatcher2.find()) {
64
+            if (!regexMatcher2.group().equals("")) {
65
+                return regexMatcher2.group();
66
+            }
67
+        }
68
+        return null;
69
+    }
70
+
71
+    public String findType(String rawItem) {
72
+        Pattern typePattern = Pattern.compile("(?<=([Tt][Yy][Pp][Ee][^A-Za-z])).*?(?=[^A-Za-z0])");
73
+        Matcher regexMatcher3 = typePattern.matcher(rawItem);
74
+        if (regexMatcher3.find()) {
75
+            return (regexMatcher3).group().toLowerCase();
76
+        } else return null;
77
+    }
78
+
79
+    public String findExpirationDate(String rawItem) {
80
+        Pattern expirationDatePattern = Pattern.compile("(?<=([Ee][Xx][Pp][Ii][Rr][Aa][Tt][Ii][Oo][Nn][^A-Za-z]))(.)+[^#]");
81
+        Matcher regexMatcher4 = expirationDatePattern.matcher(rawItem);
82
+        if (regexMatcher4.find()) {
83
+            return (regexMatcher4).group();
84
+        } else return null;
85
+
86
+    }
87
+
88
+    public HashMap<String, ArrayList<Item>> buildMap() throws Exception {
89
+        Main main = new Main();
29 90
 
91
+        ArrayList<String> listOfItems = parseRawDataIntoStringArray(main.readRawDataToString());
92
+        for (String anItem : listOfItems) {
93
+            try {
94
+                Item newItem = parseStringIntoItem(anItem);
95
+                if (!groceryList.containsKey(newItem.getName())) {
96
+                    ArrayList<Item> myItem = new ArrayList<Item>();
97
+                    myItem.add(newItem);
98
+                    groceryList.put(newItem.getName(), myItem);
99
+                } else {
100
+                    groceryList.get(newItem.getName()).add(newItem);
101
+                }
102
+            } catch (ItemParseException e) {
103
+                counter++;
104
+            }
105
+        }
106
+        return groceryList;
107
+    }
108
+
109
+    public String generateReport() throws Exception {
110
+        groceryList = buildMap();
111
+        StringBuilder builder = new StringBuilder();
112
+
113
+        for(Map.Entry<String,ArrayList<Item>>groceryItems:groceryList.entrySet()){
114
+            builder.append("\nname: ");
115
+            builder.append(String.format("%8s",captitalizeFirstLetter(groceryItems.getKey())));
116
+            builder.append("\t\t\t\tseen: "+getOccurencesOfItems(groceryItems.getValue())+" times\n");
117
+            builder.append("==============="+"\t\t\t\t===============\n");
118
+            String priceReport = generatePriceReport(groceryItems);
119
+            builder.append(priceReport);
120
+            builder.append("---------------"+"\t\t\t\t---------------\n");
121
+        }
122
+
123
+        builder.append("\nErrors\t\t\t\t\t\tseen: "+counter+" times\n");
124
+
125
+        return builder.toString();
126
+    }
127
+
128
+    public int getOccurencesOfItems(ArrayList list) {
129
+        return list.size();
130
+    }
131
+
132
+    public int getOccurencesOfPrices(ArrayList<Item> list, Double price) {
133
+        int priceCounter = 0;
134
+        for (int i = 0; i < list.size(); i++) {
135
+            if (list.get(i).getPrice().equals(price) ) {
136
+                priceCounter++;
137
+            }
138
+        }
139
+        return priceCounter;
140
+    }
141
+
142
+    public String generatePriceReport(Map.Entry<String, ArrayList<Item>> input) {
143
+        String priceReport = "";
144
+        ArrayList<Double> nonDupPrices = getUiniquePrices(input);
145
+        for(int i=0;i<nonDupPrices.size();i++){
146
+            priceReport+="Price";
147
+            priceReport+=(String.format("%10s",nonDupPrices.get(i)));
148
+            priceReport+=("\t\t\t\tseen: "+ getOccurencesOfPrices(input.getValue(),nonDupPrices.get(i))+
149
+                        " times\n");
150
+        }
151
+        return priceReport;
152
+
153
+    }
154
+
155
+    public ArrayList<Double> getUiniquePrices(Map.Entry<String, ArrayList<Item>> input) {
156
+        ArrayList<Double> uniquePrices = new ArrayList<>();
157
+        for (int i=0;i<input.getValue().size();i++) {
158
+            if (!uniquePrices.contains(input.getValue().get(i).getPrice())) {
159
+                uniquePrices.add(input.getValue().get(i).getPrice());
160
+            }
161
+        }
162
+        return uniquePrices;
163
+    }
164
+
165
+    public String captitalizeFirstLetter(String input) {
166
+        return input.substring(0, 1).toUpperCase() + input.substring(1).toLowerCase();
167
+    }
30 168
 
31 169
 }

+ 16
- 3
src/main/java/io/zipcoder/Main.java 查看文件

@@ -2,18 +2,31 @@ package io.zipcoder;
2 2
 
3 3
 import org.apache.commons.io.IOUtils;
4 4
 
5
+import java.util.ArrayList;
6
+import java.util.Map;
7
+
5 8
 
6 9
 public class Main {
7 10
 
8
-    public String readRawDataToString() throws Exception{
11
+    public String readRawDataToString() throws Exception {
9 12
         ClassLoader classLoader = getClass().getClassLoader();
10 13
         String result = IOUtils.toString(classLoader.getResourceAsStream("RawData.txt"));
11 14
         return result;
12 15
     }
13 16
 
14
-    public static void main(String[] args) throws Exception{
17
+    public static void main(String[] args) throws Exception {
15 18
         String output = (new Main()).readRawDataToString();
16 19
         System.out.println(output);
17 20
         // TODO: parse the data in output into items, and display to console.
18
-    }
21
+
22
+        ItemParser parser = new ItemParser();
23
+        Main main = new Main();
24
+
25
+        ArrayList<String> itemList = parser.parseRawDataIntoStringArray(output);
26
+
27
+        System.out.println(parser.generateReport());
28
+   }
29
+
30
+
31
+
19 32
 }

+ 3
- 1
src/main/resources/RawData.txt 查看文件

@@ -1 +1,3 @@
1
-naMe:Milk;price:3.23;type:Food;expiration:1/25/2016##naME:BreaD;price:1.23;type:Food;expiration:1/02/2016##NAMe:BrEAD;price:1.23;type:Food;expiration:2/25/2016##naMe:MiLK;price:3.23;type:Food^expiration:1/11/2016##naMe:Cookies;price:2.25;type:Food%expiration:1/25/2016##naMe:CoOkieS;price:2.25;type:Food*expiration:1/25/2016##naMe:COokIes;price:2.25;type:Food;expiration:3/22/2016##naMe:COOkieS;price:2.25;type:Food;expiration:1/25/2016##NAME:MilK;price:3.23;type:Food;expiration:1/17/2016##naMe:MilK;price:1.23;type:Food!expiration:4/25/2016##naMe:apPles;price:0.25;type:Food;expiration:1/23/2016##naMe:apPles;price:0.23;type:Food;expiration:5/02/2016##NAMe:BrEAD;price:1.23;type:Food;expiration:1/25/2016##naMe:;price:3.23;type:Food;expiration:1/04/2016##naMe:Milk;price:3.23;type:Food;expiration:1/25/2016##naME:BreaD;price:1.23;type:Food@expiration:1/02/2016##NAMe:BrEAD;price:1.23;type:Food@expiration:2/25/2016##naMe:MiLK;priCe:;type:Food;expiration:1/11/2016##naMe:Cookies;price:2.25;type:Food;expiration:1/25/2016##naMe:Co0kieS;pRice:2.25;type:Food;expiration:1/25/2016##naMe:COokIes;price:2.25;type:Food;expiration:3/22/2016##naMe:COOkieS;Price:2.25;type:Food;expiration:1/25/2016##NAME:MilK;price:3.23;type:Food;expiration:1/17/2016##naMe:MilK;priCe:;type:Food;expiration:4/25/2016##naMe:apPles;prIce:0.25;type:Food;expiration:1/23/2016##naMe:apPles;pRice:0.23;type:Food;expiration:5/02/2016##NAMe:BrEAD;price:1.23;type:Food;expiration:1/25/2016##naMe:;price:3.23;type:Food^expiration:1/04/2016##
1
+naMe:Milk;price:3.23;type:Food;expiration:1/25/2016##naME:BreaD;price:1.23;type:Food;expiration:1/02/2016##
2
+NAMe:BrEAD;price:1.23;type:Food;expiration:2/25/2016##naMe:MiLK;price:3.23;type:Food^expiration:1/11/2016##
3
+naMe:Cookies;price:2.25;type:Food%expiration:1/25/2016##naMe:CoOkieS;price:2.25;type:Food*expiration:1/25/2016##naMe:COokIes;price:2.25;type:Food;expiration:3/22/2016##naMe:COOkieS;price:2.25;type:Food;expiration:1/25/2016##NAME:MilK;price:3.23;type:Food;expiration:1/17/2016##naMe:MilK;price:1.23;type:Food!expiration:4/25/2016##naMe:apPles;price:0.25;type:Food;expiration:1/23/2016##naMe:apPles;price:0.23;type:Food;expiration:5/02/2016##NAMe:BrEAD;price:1.23;type:Food;expiration:1/25/2016##naMe:;price:3.23;type:Food;expiration:1/04/2016##naMe:Milk;price:3.23;type:Food;expiration:1/25/2016##naME:BreaD;price:1.23;type:Food@expiration:1/02/2016##NAMe:BrEAD;price:1.23;type:Food@expiration:2/25/2016##naMe:MiLK;priCe:;type:Food;expiration:1/11/2016##naMe:Cookies;price:2.25;type:Food;expiration:1/25/2016##naMe:Co0kieS;pRice:2.25;type:Food;expiration:1/25/2016##naMe:COokIes;price:2.25;type:Food;expiration:3/22/2016##naMe:COOkieS;Price:2.25;type:Food;expiration:1/25/2016##NAME:MilK;price:3.23;type:Food;expiration:1/17/2016##naMe:MilK;priCe:;type:Food;expiration:4/25/2016##naMe:apPles;prIce:0.25;type:Food;expiration:1/23/2016##naMe:apPles;pRice:0.23;type:Food;expiration:5/02/2016##NAMe:BrEAD;price:1.23;type:Food;expiration:1/25/2016##naMe:;price:3.23;type:Food^expiration:1/04/2016##

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

@@ -5,6 +5,8 @@ import org.junit.Before;
5 5
 import org.junit.Test;
6 6
 
7 7
 import java.util.ArrayList;
8
+import java.util.HashMap;
9
+import java.util.Map;
8 10
 
9 11
 import static org.junit.Assert.*;
10 12
 
@@ -14,7 +16,7 @@ public class ItemParserTest {
14 16
 
15 17
     private String rawSingleItemIrregularSeperatorSample = "naMe:MiLK;price:3.23;type:Food^expiration:1/11/2016##";
16 18
 
17
-    private String rawBrokenSingleItem =    "naMe:Milk;price:3.23;type:Food;expiration:1/25/2016##";
19
+    private String rawBrokenSingleItem =    "naMe:;price:3.23;type:Food;expiration:1/25/2016##";
18 20
 
19 21
     private String rawMultipleItems = "naMe:Milk;price:3.23;type:Food;expiration:1/25/2016##"
20 22
                                       +"naME:BreaD;price:1.23;type:Food;expiration:1/02/2016##"
@@ -59,4 +61,76 @@ public class ItemParserTest {
59 61
         Integer actual = itemParser.findKeyValuePairsInRawItemData(rawSingleItemIrregularSeperatorSample).size();
60 62
         assertEquals(expected, actual);
61 63
     }
64
+    @Test
65
+    public void findNameTest(){
66
+        String expected = "milk";
67
+        String actual = itemParser.findName(rawSingleItem);
68
+        Assert.assertEquals(expected,actual);
69
+    }
70
+    @Test
71
+    public void findPriceTest(){
72
+        String expected = "3.23";
73
+        String actual = itemParser.findPrice(rawSingleItem);
74
+        Assert.assertEquals(expected,actual);
75
+    }
76
+    @Test
77
+    public void findTypeTest(){
78
+        String expected = "food";
79
+        String actual = itemParser.findType(rawBrokenSingleItem);
80
+        Assert.assertEquals(expected,actual);
81
+    }
82
+    @Test
83
+    public void findExpirationDate(){
84
+        String expected = "1/11/2016";
85
+        String actual= itemParser.findExpirationDate(rawSingleItemIrregularSeperatorSample);
86
+        Assert.assertEquals(expected,actual);
87
+    }
88
+    @Test
89
+    public void buildMapTest1 ()throws Exception{
90
+        Map<String,ArrayList<Item>>testMap= new HashMap<String, ArrayList<Item>>();
91
+        testMap = itemParser.buildMap();
92
+        boolean actual = testMap.containsKey("milk");
93
+        Assert.assertTrue(actual);
94
+    }
95
+    @Test
96
+    public void buildMapTest2() throws Exception{
97
+        Map<String,ArrayList<Item>>testMap= new HashMap<String, ArrayList<Item>>();
98
+        testMap = itemParser.buildMap();
99
+        int expected = 4;
100
+        int actual = testMap.get("apples").size();
101
+        Assert.assertEquals(expected,actual);
102
+
103
+    }
104
+    @Test
105
+    public void getOccurencesOfItemsTest() throws ItemParseException {
106
+        ArrayList<Item>test = new ArrayList<Item>();
107
+
108
+        Item item1 = itemParser.parseStringIntoItem(rawSingleItem);
109
+        String rawItem2 =("naME:BreaD;price:1.23;type:Food;expiration:1/02/2016##");
110
+        Item item2 = itemParser.parseStringIntoItem(rawItem2);
111
+        test.add(item1);
112
+        test.add(item2);
113
+        int expected = 2;
114
+        int actual = itemParser.getOccurencesOfItems(test);
115
+        Assert.assertEquals(expected,actual);
116
+
117
+    }
118
+    @Test
119
+    public void getOccurencesOfPricesTest() throws ItemParseException {
120
+
121
+        ArrayList<Item>test = new ArrayList<Item>();
122
+
123
+        Item item1 = itemParser.parseStringIntoItem(rawSingleItem);
124
+        String rawItem2 =("naME:BreaD;price:1.23;type:Food;expiration:1/02/2016##");
125
+        String rawItem3 = "naMe:Bread;price:1.23;type:Food^expiration:1/11/2016##";
126
+        Item item2 = itemParser.parseStringIntoItem(rawItem2);
127
+        Item item3 = itemParser.parseStringIntoItem(rawItem3);
128
+        test.add(item1);
129
+        test.add(item2);
130
+        test.add(item3);
131
+        int expected =2;
132
+        int actual = itemParser.getOccurencesOfPrices(test,1.23);
133
+        Assert.assertEquals(expected,actual);
134
+    }
135
+
62 136
 }