Ver código fonte

FINISHED stringFormated and testCases passing

Joe Hendricks 6 anos atrás
pai
commit
78ce826a08

+ 57
- 20
src/main/java/io/zipcoder/ItemParser.java Ver arquivo

@@ -1,22 +1,17 @@
1 1
 package io.zipcoder;
2 2
 
3
-import java.util.ArrayList;
4
-import java.util.Arrays;
5
-import java.util.HashMap;
6
-import java.util.Map;
3
+import java.util.*;
7 4
 import java.util.regex.Matcher;
8 5
 import java.util.regex.Pattern;
9 6
 
10 7
 public class ItemParser {
11 8
 
12
-    Pattern pattern;
13
-    Matcher matcher;
14
-    public int counter = 0;
15
-
9
+    private Pattern pattern;
10
+    private Matcher matcher;
11
+    private int counter = 0;
16 12
     private Main main = new Main();
17 13
     private Map<String, ArrayList<Item>> groceryMap = new HashMap<String, ArrayList<Item>>();
18 14
 
19
-
20 15
     public ArrayList<String> parseRawDataIntoStringArray(String rawData) {
21 16
         String stringPattern = "##";
22 17
         ArrayList<String> response = splitStringWithRegexPattern(stringPattern, rawData);
@@ -47,7 +42,7 @@ public class ItemParser {
47 42
         return new ArrayList<String>(Arrays.asList(inputString.split(stringPattern)));
48 43
     }
49 44
 
50
-    private String findName(String rawItem){
45
+    public String findName(String rawItem) {
51 46
         String search = "(?<=([Nn][Aa][Mm][Ee][^A-Za-z])).*?(?=[^A-Za-z0])";
52 47
         pattern = Pattern.compile(search);
53 48
         matcher = pattern.matcher(rawItem);
@@ -56,13 +51,12 @@ public class ItemParser {
56 51
             if (!matcher.group().equals("")) {
57 52
                 String name = matcher.group().replaceAll("\\d", "o");
58 53
                 return name.toLowerCase();
59
-                //return name.substring(0, 1).toUpperCase() + name.substring(1).toLowerCase();
60 54
             }
61 55
         }
62 56
         return null;
63 57
     }
64 58
 
65
-    private String findPrice(String rawItem){
59
+    public String findPrice(String rawItem) {
66 60
         pattern = Pattern.compile("(?<=([Pp][Rr][Ii][Cc][Ee][^A-Za-z])).*?(?=[^0-9.])");
67 61
         matcher = pattern.matcher(rawItem);
68 62
 
@@ -96,7 +90,9 @@ public class ItemParser {
96 90
     public Map<String, ArrayList<Item>> getMap() throws Exception {
97 91
 
98 92
         ArrayList<String> listOfItems = parseRawDataIntoStringArray(main.readRawDataToString());
93
+
99 94
         for (String anItem : listOfItems) {
95
+
100 96
             try {
101 97
                 Item newItem = parseStringIntoItem(anItem);
102 98
                 if (!groceryMap.containsKey(newItem.getName())) {
@@ -109,21 +105,62 @@ public class ItemParser {
109 105
             } catch (ItemParseException e) {
110 106
                 counter++;
111 107
             }
108
+
112 109
         }
113 110
         return groceryMap;
114 111
     }
115 112
 
116
-    public String display(){
117
-        for(Map.Entry<String, ArrayList<Item>> mapKey : groceryMap.entrySet()) {
118
-            System.out.println(mapKey.getKey());
119
-            for (Item item : mapKey.getValue()) {
120
-                System.out.println(item.getPrice());
113
+    public String display() throws Exception {
114
+        groceryMap = getMap();
115
+        StringBuilder displayBuild = new StringBuilder();
116
+
117
+        for (Map.Entry<String, ArrayList<Item>> item : groceryMap.entrySet()) {
118
+            String upperCaseString = item.getKey().substring(0, 1).toUpperCase() + item.getKey().substring(1);
119
+
120
+            displayBuild.append("\n" +
121
+                    String.format("%-5s%10s%15s%2d%5s", "name:", upperCaseString
122
+                            , "seen: ", item.getValue().size(), "  times"));
123
+            displayBuild.append("\n" +
124
+                    String.format("%15s%3s%5s", "===============", "\t\t\t", "===============") + "\n");
125
+
126
+            ArrayList<Double> uniquePriceList = getUniquePrices(item);
127
+
128
+            for (int i = 0; i < uniquePriceList.size(); i++) {
129
+                displayBuild.append(String.format("%-11s%.2f%15s%2d%5s", "Price:", uniquePriceList.get(i)
130
+                        , "seen: ", seenPriceOccurences(item.getValue(), uniquePriceList.get(i))
131
+                        , "  times"));
132
+                displayBuild.append("\n" +
133
+                        String.format("%15s%3s%5s", "---------------", "\t\t\t", "---------------") + "\n");
121 134
             }
122 135
         }
123
-        System.out.println("Errors              seen: " + counter + " times");
124
-        return null;
136
+        displayBuild.append("\n" +
137
+                String.format("%-20s%10s%2d%5s", "Errors", "seen: ", counter, "  times"));
138
+
139
+        return displayBuild.toString();
140
+    }
141
+
142
+    public int seenPriceOccurences(ArrayList<Item> list, Double price) {
143
+        int countPrice = 0;
144
+
145
+        for (int i = 0; i < list.size(); i++) {
146
+            if (list.get(i).getPrice().equals(price)) {
147
+                countPrice++;
148
+            }
125 149
         }
150
+        return countPrice;
151
+    }
126 152
 
127
-    public
153
+    public ArrayList<Double> getUniquePrices(Map.Entry<String, ArrayList<Item>> item) {
154
+        ArrayList<Double> uniquePrices = new ArrayList<Double>();
155
+
156
+        for (int i = 0; i < item.getValue().size(); i++) {
157
+            if (!uniquePrices.contains(item.getValue().get(i).getPrice())) {
158
+                uniquePrices.add(item.getValue().get(i).getPrice());
159
+            }
160
+        }
161
+        return uniquePrices;
128 162
     }
163
+}
164
+
165
+
129 166
 

+ 2
- 16
src/main/java/io/zipcoder/Main.java Ver arquivo

@@ -1,13 +1,7 @@
1 1
 package io.zipcoder;
2 2
 
3
-import com.sun.javafx.geom.Edge;
4 3
 import org.apache.commons.io.IOUtils;
5 4
 
6
-import java.util.ArrayList;
7
-import java.util.HashMap;
8
-import java.util.Map;
9
-
10
-
11 5
 public class Main {
12 6
 
13 7
     public String readRawDataToString() throws Exception{
@@ -20,16 +14,8 @@ public class Main {
20 14
         String output = (new Main()).readRawDataToString();
21 15
         //System.out.println(output);
22 16
         // TODO: parse the data in output into items, and display to console.
23
-
24
-        ItemParser test = new ItemParser();
25
-        Main mainTest = new Main();
26
-
27
-        ArrayList<String> arrayTest = test.parseRawDataIntoStringArray(output);
28
-
29
-
30
-        System.out.println(test.parseRawDataIntoStringArray(mainTest.readRawDataToString()));
31
-
17
+        ItemParser item = new ItemParser();
18
+        System.out.println(item.display());
32 19
     }
33
-
34 20
 }
35 21
 

+ 89
- 0
src/test/java/io/zipcoder/ItemParserTest.java Ver arquivo

@@ -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
 
@@ -59,4 +61,91 @@ public class ItemParserTest {
59 61
         Integer actual = itemParser.findKeyValuePairsInRawItemData(rawSingleItemIrregularSeperatorSample).size();
60 62
         assertEquals(expected, actual);
61 63
     }
64
+
65
+    @Test
66
+    public void findNameTest(){
67
+
68
+        String expected = "milk";
69
+        String actual = itemParser.findName(rawSingleItem);
70
+
71
+        Assert.assertEquals(expected, actual);
72
+    }
73
+
74
+    @Test
75
+    public void findPrice(){
76
+
77
+        String expected = "3.23";
78
+        String actual = itemParser.findPrice(rawSingleItem);
79
+
80
+        Assert.assertEquals(expected, actual);
81
+    }
82
+
83
+    @Test
84
+    public void findExpirationTest(){
85
+
86
+        String expected = "1/25/2016";
87
+        String actual = itemParser.findExpiration(rawSingleItem);
88
+
89
+        Assert.assertEquals(expected, actual);
90
+    }
91
+
92
+    @Test
93
+    public void findTypeTest(){
94
+
95
+        String expected = "food";
96
+        String actual = itemParser.findType(rawSingleItem);
97
+
98
+        Assert.assertEquals(expected, actual);
99
+    }
100
+
101
+    @Test
102
+    public void display() throws Exception{
103
+
104
+        String expected = "\n" +
105
+                "name:     Bread         seen:  6  times\n" +
106
+                "===============\t\t\t===============\n" +
107
+                "Price:     1.23         seen:  6  times\n" +
108
+                "---------------\t\t\t---------------\n" +
109
+                "\n" +
110
+                "name:      Milk         seen:  6  times\n" +
111
+                "===============\t\t\t===============\n" +
112
+                "Price:     3.23         seen:  5  times\n" +
113
+                "---------------\t\t\t---------------\n" +
114
+                "Price:     1.23         seen:  1  times\n" +
115
+                "---------------\t\t\t---------------\n" +
116
+                "\n" +
117
+                "name:    Apples         seen:  4  times\n" +
118
+                "===============\t\t\t===============\n" +
119
+                "Price:     0.25         seen:  2  times\n" +
120
+                "---------------\t\t\t---------------\n" +
121
+                "Price:     0.23         seen:  2  times\n" +
122
+                "---------------\t\t\t---------------\n" +
123
+                "\n" +
124
+                "name:   Cookies         seen:  8  times\n" +
125
+                "===============\t\t\t===============\n" +
126
+                "Price:     2.25         seen:  8  times\n" +
127
+                "---------------\t\t\t---------------\n" +
128
+                "\n" +
129
+                "Errors                  seen:  4  times";
130
+        String actual = itemParser.display();
131
+
132
+        Assert.assertEquals(expected, actual);
133
+    }
134
+
135
+    @Test
136
+    public void seenPriceOccurencesTest(){
137
+        ArrayList<Item> listTest = new ArrayList<Item>();
138
+        Item itemTest = new Item("Bread", 3.12, "Food", "1/25/2016");
139
+        Item itemTest2 = new Item("Bread", 3.12, "Food", "1/25/2016");
140
+        Item itemTest3 = new Item("Bread", 2.00, "Food", "4/16/2016");
141
+
142
+        listTest.add(itemTest);
143
+        listTest.add(itemTest2);
144
+        listTest.add(itemTest3);
145
+
146
+        int expected = 2;
147
+        int actual = itemParser.seenPriceOccurences(listTest, 3.12);
148
+
149
+        Assert.assertEquals(expected, actual);
150
+    }
62 151
 }