Bläddra i källkod

Merge 19cde2b0154cf2aee37c0b0d06ed7f7bd50e920f into 23c2c01bf07b924ac9b860fa2771bf6ceb246ad5

vvg3 6 år sedan
förälder
incheckning
6ab8c1b510
No account linked to committer's email

+ 174
- 8
src/main/java/io/zipcoder/ItemParser.java Visa fil

@@ -2,30 +2,196 @@ 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
+    private Matcher matcher;
14
+    private int exceptionCount = 0;
15
+    private HashMap<String, ArrayList<Item>> groceries;
8 16
 
9
-    public ArrayList<String> parseRawDataIntoStringArray(String rawData){
17
+    public ItemParser() {
18
+        groceries = new HashMap<String, ArrayList<Item>>();
19
+    }
20
+
21
+    public String parse(String aString) {
22
+        ArrayList<String> groceries = parseRawDataIntoStringArray(aString);
23
+        addGroceriesToList(groceries);
24
+        return printGroceries();
25
+    }
26
+
27
+    protected ArrayList<String> parseRawDataIntoStringArray(String rawData) {
10 28
         String stringPattern = "##";
11
-        ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawData);
29
+        ArrayList<String> response = splitStringWithRegexPattern(stringPattern, rawData);
12 30
         return response;
13 31
     }
14 32
 
15
-    public Item parseStringIntoItem(String rawItem) throws ItemParseException{
16
-        return null;
33
+    protected void addGroceriesToList(ArrayList<String> jerkList) {
34
+        for (int i = 0; i < jerkList.size(); i++) {
35
+            try {
36
+                Item temp = (parseStringIntoItem(jerkList.get(i)));
37
+                incrementItem(this.groceries, temp);
38
+            } catch (ItemParseException ipe) {
39
+                exceptionCount++;
40
+            }
41
+        }
42
+    }
43
+
44
+    protected Item parseStringIntoItem(String rawItem) throws ItemParseException {
45
+        String aName = findName(rawItem);
46
+        Double aPrice = findPrice(rawItem);
47
+        String aType = findType(rawItem);
48
+        String anExpiration = findExpiration(rawItem);
49
+
50
+        if (findName(rawItem) == null || findPrice(rawItem) == null) {
51
+            throw new ItemParseException();
52
+        }
53
+
54
+        return new Item(aName, aPrice, aType, anExpiration);
17 55
     }
18 56
 
19
-    public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){
57
+    protected ArrayList<String> findKeyValuePairsInRawItemData(String rawItem) {
20 58
         String stringPattern = "[;|^]";
21
-        ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawItem);
22
-        return response;
59
+        return splitStringWithRegexPattern(stringPattern, rawItem);
23 60
     }
24 61
 
25
-    private ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString){
62
+    public HashMap<String, ArrayList<Item>> getGroceries() {
63
+        return groceries;
64
+    }
65
+
66
+
67
+    // private methods
68
+
69
+    private ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString) {
26 70
         return new ArrayList<String>(Arrays.asList(inputString.split(stringPattern)));
27 71
     }
28 72
 
73
+    private String printGroceries() {
74
+        StringBuilder display = new StringBuilder();
75
+        for (Map.Entry<String, ArrayList<Item>> entry : groceries.entrySet()) {
76
+            displayItemInfo(display, entry);
77
+            displayPriceInfo(display, entry);
78
+        }
79
+        display.append("\n\n").append(exceptionCount).append("  errors\n");
80
+        return display.toString();
81
+    }
82
+
83
+    private void displayItemInfo(StringBuilder aBuilder, Map.Entry<String, ArrayList<Item>> anEntry) {
84
+        aBuilder.append("\nname:");
85
+        aBuilder.append(String.format("%9s", anEntry.getKey().substring(0, 1).toUpperCase() + anEntry.getKey().substring(1)));
86
+        aBuilder.append("          seen:  " + anEntry.getValue().size() + "  times\n");
87
+        aBuilder.append("==============" + "\t\t\t" + "===============\n");
88
+    }
89
+
90
+    private void displayPriceInfo(StringBuilder aBuilder, Map.Entry<String, ArrayList<Item>> anEntry) {
91
+        ArrayList<Double> uniquePriceArrayList = getUniquePrices(anEntry);
92
+        for (Double aPrice : uniquePriceArrayList) {
93
+            aBuilder.append("Price:").append(String.format("%8s", aPrice));
94
+            aBuilder.append("          seen:  " + priceOccurences(anEntry.getValue(), aPrice) + "  times\n");
95
+            aBuilder.append("--------------" + "\t\t\t" + "---------------\n");
96
+        }
97
+    }
98
+
99
+    private ArrayList<Double> getUniquePrices(Map.Entry<String, ArrayList<Item>> entry) {
100
+        ArrayList<Double> prices = new ArrayList<Double>();
29 101
 
102
+        for (int i = 0; i < entry.getValue().size(); i++) {
103
+            if (!prices.contains(entry.getValue().get(i).getPrice())) {
104
+                prices.add(entry.getValue().get(i).getPrice());
105
+            }
106
+        }
107
+        return prices;
108
+    }
109
+
110
+    private int priceOccurences(ArrayList<Item> aList, Double aPrice) {
111
+        int count = 0;
112
+        for (Item anAList : aList) {
113
+            if (anAList.getPrice().equals(aPrice)) {
114
+                count++;
115
+            }
116
+        }
117
+        return count;
118
+    }
119
+
120
+    private void incrementItem(Map<String, ArrayList<Item>> aMap, Item anItem) {
121
+        if (aMap.keySet().contains(anItem.getName())) {
122
+            aMap.get(anItem.getName()).add(anItem);
123
+        } else {
124
+            aMap.put(anItem.getName(), new ArrayList<Item>());
125
+            aMap.get(anItem.getName()).add(anItem);
126
+        }
127
+    }
128
+
129
+    private String findName(String aString) {
130
+        String search = "(?<=([n|N][a|A][m|M][e|E][^a-zA-z])).*?(?=[^a-zA-z0])";
131
+        pattern = Pattern.compile(search);
132
+        matcher = pattern.matcher(aString);
133
+
134
+        if (matcher.find()) {
135
+            if (matcher.group().length() > 0) {
136
+                return replaceZeros(matcher.group().toLowerCase());
137
+            } else {
138
+                return null;
139
+            }
140
+        } else {
141
+            return null;
142
+        }
143
+    }
144
+
145
+    private String replaceZeros(String aString) {
146
+        pattern = Pattern.compile("[0]");
147
+        matcher = pattern.matcher(aString);
148
+        return matcher.replaceAll("o");
149
+    }
150
+
151
+    private Double findPrice(String aString) {
152
+        pattern = Pattern.compile("(?<=([p|P][r|R][i|I][c|C][e|E][^a-zA-Z])).*?(?=[^0-9.])");
153
+        matcher = pattern.matcher(aString);
154
+
155
+        if (matcher.find()) {
156
+            if (matcher.group().length() > 0) {
157
+                return Double.parseDouble(matcher.group());
158
+            } else {
159
+                return null;
160
+            }
161
+        } else {
162
+            return null;
163
+        }
164
+    }
165
+
166
+    private String findType(String aString) {
167
+        pattern = Pattern.compile("(?<=([t|T][y|Y][p|P][e|E][^a-zA-z])).*?(?=[^a-zA-Z0])");
168
+        matcher = pattern.matcher(aString);
169
+
170
+        if (matcher.find()) {
171
+            if (matcher.group().length() > 0) {
172
+                return matcher.group().toLowerCase();
173
+            } else {
174
+                return null;
175
+            }
176
+        } else {
177
+            return null;
178
+        }
179
+    }
180
+
181
+    private String findExpiration(String aString) {
182
+        String search = "(?<=([e|E][x|X][p|P][i|I][r|R][a|A][t|T][i|I][o|O][n|N][^a-zA-z]))(.)+[^#]";
183
+        pattern = Pattern.compile(search);
184
+        matcher = pattern.matcher(aString);
185
+
186
+        if (matcher.find()) {
187
+            if (matcher.group().length() > 0) {
188
+                return matcher.group();
189
+            } else {
190
+                return null;
191
+            }
192
+        } else {
193
+            return null;
194
+        }
195
+    }
30 196
 
31 197
 }

+ 3
- 1
src/main/java/io/zipcoder/Main.java Visa fil

@@ -5,6 +5,8 @@ import org.apache.commons.io.IOUtils;
5 5
 
6 6
 public class Main {
7 7
 
8
+    static ItemParser parser = new ItemParser();
9
+
8 10
     public String readRawDataToString() throws Exception{
9 11
         ClassLoader classLoader = getClass().getClassLoader();
10 12
         String result = IOUtils.toString(classLoader.getResourceAsStream("RawData.txt"));
@@ -13,7 +15,7 @@ public class Main {
13 15
 
14 16
     public static void main(String[] args) throws Exception{
15 17
         String output = (new Main()).readRawDataToString();
16
-        System.out.println(output);
17 18
         // TODO: parse the data in output into items, and display to console.
19
+        System.out.println(parser.parse(output));
18 20
     }
19 21
 }

+ 81
- 12
src/test/java/io/zipcoder/ItemParserTest.java Visa fil

@@ -10,24 +10,36 @@ import static org.junit.Assert.*;
10 10
 
11 11
 public class ItemParserTest {
12 12
 
13
-    private String rawSingleItem =    "naMe:Milk;price:3.23;type:Food;expiration:1/25/2016##";
13
+    private String rawSingleItem = "naMe:Milk;price:3.23;type:Food;expiration:1/25/2016##";
14 14
 
15 15
     private String rawSingleItemIrregularSeperatorSample = "naMe:MiLK;price:3.23;type:Food^expiration:1/11/2016##";
16 16
 
17
-    private String rawBrokenSingleItem =    "naMe:Milk;price:3.23;type:Food;expiration:1/25/2016##";
17
+    private String rawBrokenSingleItem = "naMe:;price:3.23;type:Food;expiration:1/25/2016##";
18 18
 
19 19
     private String rawMultipleItems = "naMe:Milk;price:3.23;type:Food;expiration:1/25/2016##"
20
-                                      +"naME:BreaD;price:1.23;type:Food;expiration:1/02/2016##"
21
-                                      +"NAMe:BrEAD;price:1.23;type:Food;expiration:2/25/2016##";
20
+            + "naME:BreaD;price:1.23;type:Food;expiration:1/02/2016##"
21
+            + "NAMe:BrEAD;price:1.23;type:Food;expiration:2/25/2016##";
22
+
23
+    private String rawMultipleItemsMore = "naMe:;price:3.23;type:Food;expiration:1/25/2016##"
24
+            + "naME:MILK;price:2.75;type:Food;expiration:1/02/2016##"
25
+            + "NAMe:miLK;price:2.75;type:Food;expiration:2/25/2016##"
26
+            + "naME:c00kIEs;price:1.23;type:Food;expiration:1/02/2013##"
27
+            + "naME:BreaD;price:1.23;type:Food;expiration:1/02/2016##"
28
+            + "naME:milk;price:1.50;type:Food;expiration:3/02/2012##"
29
+            + "naME:BreaD;price:1.23;type:Food;expiration:1/02/2016##"
30
+            + "naME:COOKIES;price:1.75;type:Food;expiration:1/02/2014##"
31
+            + "naME:BreaD;price:1.50;type:Food;expiration:1/02/2016##";
32
+
22 33
     private ItemParser itemParser;
23 34
 
35
+
24 36
     @Before
25
-    public void setUp(){
37
+    public void setUp() {
26 38
         itemParser = new ItemParser();
27 39
     }
28 40
 
29 41
     @Test
30
-    public void parseRawDataIntoStringArrayTest(){
42
+    public void parseRawDataIntoStringArrayTest() {
31 43
         Integer expectedArraySize = 3;
32 44
         ArrayList<String> items = itemParser.parseRawDataIntoStringArray(rawMultipleItems);
33 45
         Integer actualArraySize = items.size();
@@ -35,28 +47,85 @@ public class ItemParserTest {
35 47
     }
36 48
 
37 49
     @Test
38
-    public void parseStringIntoItemTest() throws ItemParseException{
39
-        Item expected = new Item("milk", 3.23, "food","1/25/2016");
50
+    public void parseStringIntoItemTest() throws ItemParseException {
51
+        Item expected = new Item("milk", 3.23, "food", "1/25/2016");
40 52
         Item actual = itemParser.parseStringIntoItem(rawSingleItem);
41 53
         assertEquals(expected.toString(), actual.toString());
42 54
     }
43 55
 
44 56
     @Test(expected = ItemParseException.class)
45
-    public void parseBrokenStringIntoItemTest() throws ItemParseException{
57
+    public void parseBrokenStringIntoItemTest() throws ItemParseException {
46 58
         itemParser.parseStringIntoItem(rawBrokenSingleItem);
47 59
     }
48 60
 
49 61
     @Test
50
-    public void findKeyValuePairsInRawItemDataTest(){
62
+    public void findKeyValuePairsInRawItemDataTest() {
51 63
         Integer expected = 4;
52 64
         Integer actual = itemParser.findKeyValuePairsInRawItemData(rawSingleItem).size();
53 65
         assertEquals(expected, actual);
54 66
     }
55 67
 
56 68
     @Test
57
-    public void findKeyValuePairsInRawItemDataTestIrregular(){
69
+    public void findKeyValuePairsInRawItemDataTestIrregular() {
58 70
         Integer expected = 4;
59 71
         Integer actual = itemParser.findKeyValuePairsInRawItemData(rawSingleItemIrregularSeperatorSample).size();
60 72
         assertEquals(expected, actual);
61 73
     }
62
-}
74
+
75
+    @Test
76
+    public void addGroceriesToListTest1() {
77
+        ArrayList<String> items = itemParser.parseRawDataIntoStringArray(rawMultipleItems);
78
+        itemParser.addGroceriesToList(items);
79
+        int expected = 2;
80
+        int actual = itemParser.getGroceries().size();
81
+        Assert.assertEquals(expected, actual);
82
+    }
83
+
84
+    @Test
85
+    public void addGroceriesToListTest2() {
86
+        ArrayList<String> items = itemParser.parseRawDataIntoStringArray(rawMultipleItems);
87
+        itemParser.addGroceriesToList(items);
88
+        int expected = 2;
89
+        int actual = itemParser.getGroceries().get("bread").size();
90
+        Assert.assertEquals(expected, actual);
91
+    }
92
+
93
+    @Test
94
+    public void addGroceriesToListTest3() {
95
+        ArrayList<String> items = itemParser.parseRawDataIntoStringArray(rawMultipleItems);
96
+        itemParser.addGroceriesToList(items);
97
+        String expected = "[bread, milk]";
98
+        String actual = itemParser.getGroceries().keySet().toString();
99
+        Assert.assertEquals(expected, actual);
100
+    }
101
+
102
+    @Test
103
+    public void parseTest1() {
104
+        String expected = "\nname:    Bread          seen:  3  times\n" +
105
+                "==============\t\t\t===============\n" +
106
+                "Price:    1.23          seen:  2  times\n" +
107
+                "--------------\t\t\t---------------\n" +
108
+                "Price:     1.5          seen:  1  times\n" +
109
+                "--------------\t\t\t---------------\n" +
110
+                "\n" +
111
+                "name:     Milk          seen:  3  times\n" +
112
+                "==============\t\t\t===============\n" +
113
+                "Price:    2.75          seen:  2  times\n" +
114
+                "--------------\t\t\t---------------\n" +
115
+                "Price:     1.5          seen:  1  times\n" +
116
+                "--------------\t\t\t---------------\n" +
117
+                "\n" +
118
+                "name:  Cookies          seen:  2  times\n" +
119
+                "==============\t\t\t===============\n" +
120
+                "Price:    1.23          seen:  1  times\n" +
121
+                "--------------\t\t\t---------------\n" +
122
+                "Price:    1.75          seen:  1  times\n" +
123
+                "--------------\t\t\t---------------\n" +
124
+                "\n" +
125
+                "\n" +
126
+                "1  errors\n";
127
+        String actual = itemParser.parse(rawMultipleItemsMore);
128
+        Assert.assertEquals(expected, actual);
129
+    }
130
+
131
+}