Vincent Gasbarro 6 anni fa
parent
commit
19cde2b015

+ 71
- 69
src/main/java/io/zipcoder/ItemParser.java Vedi File

@@ -11,46 +11,92 @@ public class ItemParser {
11 11
 
12 12
     private Pattern pattern;
13 13
     private Matcher matcher;
14
-    int exceptionCount = 0;
14
+    private int exceptionCount = 0;
15 15
     private HashMap<String, ArrayList<Item>> groceries;
16 16
 
17 17
     public ItemParser() {
18 18
         groceries = new HashMap<String, ArrayList<Item>>();
19 19
     }
20 20
 
21
-    public void parse(String aString) {
21
+    public String parse(String aString) {
22 22
         ArrayList<String> groceries = parseRawDataIntoStringArray(aString);
23 23
         addGroceriesToList(groceries);
24
-        printGroceries();
25
-        System.out.println(printGroceries());
24
+        return printGroceries();
26 25
     }
27 26
 
28
-    public String printGroceries() {
29
-
30
-        StringBuilder display = new StringBuilder();
27
+    protected ArrayList<String> parseRawDataIntoStringArray(String rawData) {
28
+        String stringPattern = "##";
29
+        ArrayList<String> response = splitStringWithRegexPattern(stringPattern, rawData);
30
+        return response;
31
+    }
31 32
 
32
-        for (Map.Entry<String, ArrayList<Item>> entry : groceries.entrySet()) {
33
-            display.append("\nname:");
34
-            display.append(String.format("%9s", entry.getKey().substring(0, 1).toUpperCase() + entry.getKey().substring(1)));
35
-            display.append("          seen:  " + entry.getValue().size() + "  times\n");
36
-            display.append("==============" + "\t\t\t" + "===============\n");
37
-
38
-            ArrayList<Double> temp = getUniquePrices(entry);
39
-
40
-            for (int i = 0; i < temp.size(); i++) {
41
-                display.append("Price:");
42
-                display.append(String.format("%8s", temp.get(i)));
43
-                display.append("          seen:  " + countPriceOccurences(entry.getValue(), temp.get(i)) + "  times\n");
44
-                display.append("--------------" + "\t\t\t" + "---------------\n");
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++;
45 40
             }
46 41
         }
42
+    }
47 43
 
48
-        display.append("\n\n" + exceptionCount + "  errors\n");
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
+        }
49 53
 
54
+        return new Item(aName, aPrice, aType, anExpiration);
55
+    }
56
+
57
+    protected ArrayList<String> findKeyValuePairsInRawItemData(String rawItem) {
58
+        String stringPattern = "[;|^]";
59
+        return splitStringWithRegexPattern(stringPattern, rawItem);
60
+    }
61
+
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) {
70
+        return new ArrayList<String>(Arrays.asList(inputString.split(stringPattern)));
71
+    }
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");
50 80
         return display.toString();
51 81
     }
52 82
 
53
-    public ArrayList<Double> getUniquePrices(Map.Entry<String, ArrayList<Item>> entry) {
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) {
54 100
         ArrayList<Double> prices = new ArrayList<Double>();
55 101
 
56 102
         for (int i = 0; i < entry.getValue().size(); i++) {
@@ -61,28 +107,16 @@ public class ItemParser {
61 107
         return prices;
62 108
     }
63 109
 
64
-    public int countPriceOccurences(ArrayList<Item> aList, Double aPrice) {
110
+    private int priceOccurences(ArrayList<Item> aList, Double aPrice) {
65 111
         int count = 0;
66
-
67
-        for (int i = 0; i < aList.size(); i++) {
68
-            if (aList.get(i).getPrice().equals(aPrice)) {
112
+        for (Item anAList : aList) {
113
+            if (anAList.getPrice().equals(aPrice)) {
69 114
                 count++;
70 115
             }
71 116
         }
72 117
         return count;
73 118
     }
74 119
 
75
-    public void addGroceriesToList(ArrayList<String> jerkList) {
76
-        for (int i = 0; i < jerkList.size(); i++) {
77
-            try {
78
-                Item temp = (parseStringIntoItem(jerkList.get(i)));
79
-                incrementItem(this.groceries, temp);
80
-            } catch (ItemParseException ipe) {
81
-                exceptionCount++;
82
-            }
83
-        }
84
-    }
85
-
86 120
     private void incrementItem(Map<String, ArrayList<Item>> aMap, Item anItem) {
87 121
         if (aMap.keySet().contains(anItem.getName())) {
88 122
             aMap.get(anItem.getName()).add(anItem);
@@ -92,25 +126,6 @@ public class ItemParser {
92 126
         }
93 127
     }
94 128
 
95
-    public ArrayList<String> parseRawDataIntoStringArray(String rawData) {
96
-        String stringPattern = "##";
97
-        ArrayList<String> response = splitStringWithRegexPattern(stringPattern, rawData);
98
-        return response;
99
-    }
100
-
101
-    public Item parseStringIntoItem(String rawItem) throws ItemParseException {
102
-        String aName = findName(rawItem);
103
-        Double aPrice = findPrice(rawItem);
104
-        String aType = findType(rawItem);
105
-        String anExpiration = findExpiration(rawItem);
106
-
107
-        if (findName(rawItem) == null || findPrice(rawItem) == null) {
108
-            throw new ItemParseException();
109
-        }
110
-
111
-        return new Item(aName, aPrice, aType, anExpiration);
112
-    }
113
-
114 129
     private String findName(String aString) {
115 130
         String search = "(?<=([n|N][a|A][m|M][e|E][^a-zA-z])).*?(?=[^a-zA-z0])";
116 131
         pattern = Pattern.compile(search);
@@ -179,17 +194,4 @@ public class ItemParser {
179 194
         }
180 195
     }
181 196
 
182
-    public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem) {
183
-        String stringPattern = "[;|^]";
184
-        ArrayList<String> response = splitStringWithRegexPattern(stringPattern, rawItem);
185
-        return response;
186
-    }
187
-
188
-    private ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString) {
189
-        return new ArrayList<String>(Arrays.asList(inputString.split(stringPattern)));
190
-    }
191
-
192
-    public HashMap<String, ArrayList<Item>> getGroceries() {
193
-        return groceries;
194
-    }
195 197
 }

+ 1
- 4
src/main/java/io/zipcoder/Main.java Vedi File

@@ -15,10 +15,7 @@ public class Main {
15 15
 
16 16
     public static void main(String[] args) throws Exception{
17 17
         String output = (new Main()).readRawDataToString();
18
-        parser.parse(output);
19
-
20
-
21
-//        System.out.println(output);
22 18
         // TODO: parse the data in output into items, and display to console.
19
+        System.out.println(parser.parse(output));
23 20
     }
24 21
 }

+ 26
- 6
src/test/java/io/zipcoder/ItemParserTest.java Vedi File

@@ -100,12 +100,32 @@ public class ItemParserTest {
100 100
     }
101 101
 
102 102
     @Test
103
-    public void testDisplay() {
104
-        ArrayList<String> items = itemParser.parseRawDataIntoStringArray(rawMultipleItemsMore);
105
-        itemParser.addGroceriesToList(items);
106
-        System.out.println(itemParser.printGroceries());
107
-
108
-
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);
109 129
     }
110 130
 
111 131
 }