Browse Source

Merge 0dd99dd6e4edfb0943ae36ee5944c6e0880e02b9 into 23c2c01bf07b924ac9b860fa2771bf6ceb246ad5

JoeHendricks415 6 years ago
parent
commit
2786b1dfa3
No account linked to committer's email

+ 1
- 1
src/main/java/io/zipcoder/Item.java View File

@@ -43,6 +43,6 @@ public class Item {
43 43
 
44 44
     @Override
45 45
     public String toString(){
46
-        return "name:" + name + " price:" + price + " type:" + type + " expiration:" + expiration;
46
+        return name + " " + price + " " + type + " " + expiration;
47 47
     }
48 48
 }

+ 1
- 0
src/main/java/io/zipcoder/ItemParseException.java View File

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

+ 151
- 9
src/main/java/io/zipcoder/ItemParser.java View File

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

+ 5
- 3
src/main/java/io/zipcoder/Main.java View File

@@ -2,7 +2,6 @@ package io.zipcoder;
2 2
 
3 3
 import org.apache.commons.io.IOUtils;
4 4
 
5
-
6 5
 public class Main {
7 6
 
8 7
     public String readRawDataToString() throws Exception{
@@ -11,9 +10,12 @@ public class Main {
11 10
         return result;
12 11
     }
13 12
 
14
-    public static void main(String[] args) throws Exception{
13
+    public static void main(String[] args) throws Exception {
15 14
         String output = (new Main()).readRawDataToString();
16
-        System.out.println(output);
15
+        //System.out.println(output);
17 16
         // TODO: parse the data in output into items, and display to console.
17
+        ItemParser item = new ItemParser();
18
+        System.out.println(item.display());
18 19
     }
19 20
 }
21
+

+ 90
- 1
src/test/java/io/zipcoder/ItemParserTest.java View File

@@ -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:Milk;price:;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,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   time\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
 }