Browse Source

Merge 1227677299f5648b85ba9f5765c537e08938f8dc into 23c2c01bf07b924ac9b860fa2771bf6ceb246ad5

CWinarski 6 years ago
parent
commit
6ccddd8d78
No account linked to committer's email

+ 12
- 0
pom.xml View File

@@ -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.8</source>
17
+                    <target>1.8</target>
18
+                </configuration>
19
+            </plugin>
20
+        </plugins>
21
+    </build>
10 22
 
11 23
     <dependencies>
12 24
         <dependency>

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

@@ -1,4 +1,12 @@
1 1
 package io.zipcoder;
2 2
 
3 3
 public class ItemParseException extends Exception {
4
+    private static int count = 0;
5
+    public ItemParseException(){
6
+        count++;
7
+    }
8
+    public static int getCount(){
9
+        return count;
10
+    }
11
+
4 12
 }

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

@@ -2,30 +2,188 @@ 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
+    Map<String, ArrayList<Item>> realFoodList;
8 13
 
9
-    public ArrayList<String> parseRawDataIntoStringArray(String rawData){
14
+    public ItemParser() {
15
+        realFoodList = new HashMap<>();
16
+    }
17
+
18
+    public Map<String, ArrayList<Item>> getRealFoodList() {
19
+        return realFoodList;
20
+    }
21
+
22
+    //feed data from string split into arrayList
23
+    public ArrayList<String> parseRawDataIntoStringArray(String rawData) {
10 24
         String stringPattern = "##";
11
-        ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawData);
25
+        ArrayList<String> response = splitStringWithRegexPattern(stringPattern, rawData);
26
+        //naMe:Milk;price:3.23;type:Food;expiration:1/25/2016 these chunks stored at each index
12 27
         return response;
13 28
     }
14 29
 
15
-    public Item parseStringIntoItem(String rawItem) throws ItemParseException{
16
-        return null;
30
+    //general method for splitting arraylists with regex
31
+    private ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString) {
32
+        return new ArrayList<>(Arrays.asList(inputString.split(stringPattern)));
17 33
     }
18 34
 
19
-    public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){
20
-        String stringPattern = "[;|^]";
21
-        ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawItem);
35
+    //Splits arraylist that has chunks into the name:cake arraylist which gets used in testing
36
+    public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem) {
37
+        String stringPattern = "[;|^|!|%|*|@]";
38
+        ArrayList<String> response = splitStringWithRegexPattern(stringPattern, rawItem);
39
+        //ex {name:cake,price:2.50, type:food, expiration:1/4/2018}
22 40
         return response;
41
+
42
+    }
43
+
44
+    //takes the raw data arraylist(item chunks) parses them into items and then adds the items to the map
45
+    public void addItemToList(ArrayList<String> itemList){
46
+            for (int i = 0; i < itemList.size(); i++) {
47
+                try{
48
+                Item newItem = (parseStringIntoItem(itemList.get(i)));
49
+                addNewItemToMap(realFoodList, newItem);
50
+            }catch (ItemParseException e){
51
+                    continue;
52
+                }
53
+
54
+        }
55
+    }
56
+
57
+    public void addNewItemToMap(Map<String, ArrayList<Item>> map, Item newItem) {
58
+        if (!map.keySet().contains(newItem.getName())) {
59
+            map.put(newItem.getName(), new ArrayList<>());
60
+            map.get(newItem.getName()).add(newItem);
61
+        } else {
62
+            map.get(newItem.getName()).add(newItem);
63
+        }
64
+    }
65
+
66
+    //gets all the prices for the items
67
+    public ArrayList<Double> getPrices(Map.Entry<String, ArrayList<Item>> itemMap) {
68
+        ArrayList<Double> prices = new ArrayList<>();
69
+        for (int i = 0; i < itemMap.getValue().size(); i++) {
70
+            if (!prices.contains(itemMap.getValue().get(i).getPrice())) {
71
+                prices.add(itemMap.getValue().get(i).getPrice());
72
+            }
73
+        }
74
+        return prices;
75
+    }
76
+
77
+    public int countNumberOfDifferentPrices(ArrayList<Item> itemList, Double price) {
78
+        int counter = 0;
79
+        for (int i = 0; i < itemList.size(); i++) {
80
+            if (itemList.get(i).getPrice().equals(price)) {
81
+                counter++;
82
+            }
83
+        }
84
+        return counter;
85
+    }
86
+
87
+        //Create new item
88
+    public Item parseStringIntoItem(String rawItem) throws ItemParseException {
89
+        String name = findName(rawItem);
90
+        Double price = Double.valueOf(findPrice(rawItem));
91
+        String type = findType(rawItem);
92
+        String expiration = findExpiration(rawItem);
93
+
94
+        return new Item(name, price, type, expiration);
95
+    }
96
+
97
+    //Cookie has a 0 in it so we fix it before parsing into the item
98
+    public String fixCo0kie(String input) {
99
+        String regexCo0kie = "[0]";
100
+        Pattern pattern = Pattern.compile(regexCo0kie);
101
+        Matcher matcher = pattern.matcher(input);
102
+        return matcher.replaceAll("o");
103
+    }
104
+
105
+    //We find the item name by searching for the name:item pair then return the group that has the item
106
+    public String findName(String name) throws ItemParseException {
107
+        String regexName = "([Nn]..[Ee]:)(\\w+)";
108
+        Pattern pattern = Pattern.compile(regexName);
109
+        Matcher matcher = pattern.matcher(name);
110
+        if (matcher.find()) {
111
+            if(matcher.group(2).contains("0")) {
112
+                return fixCo0kie(matcher.group(2).toLowerCase());
113
+            }else {
114
+                return matcher.group(2).toLowerCase();
115
+            }
116
+        } else {
117
+            throw new ItemParseException();
118
+        }
119
+
120
+    }
121
+
122
+    public String findPrice(String price) throws ItemParseException {
123
+        String regexPrice = "([Pp]...[Ee]:)(\\d\\.\\d{2})";
124
+        Pattern pattern = Pattern.compile(regexPrice);
125
+        Matcher matcher = pattern.matcher(price);
126
+
127
+        if (matcher.find()) {
128
+            return matcher.group(2);
129
+        } else {
130
+            throw new ItemParseException();
131
+        }
23 132
     }
24 133
 
25
-    private ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString){
26
-        return new ArrayList<String>(Arrays.asList(inputString.split(stringPattern)));
134
+
135
+    public String findType(String type) throws ItemParseException {
136
+        String regexType = "([Tt]..[Ee]:)(\\w+)";
137
+        Pattern pattern = Pattern.compile(regexType);
138
+        Matcher matcher = pattern.matcher(type);
139
+
140
+        if (matcher.find()) {
141
+            return matcher.group(2).toLowerCase();
142
+        } else {
143
+            throw new ItemParseException();
144
+        }
145
+    }
146
+
147
+
148
+    public String findExpiration(String expiration) throws ItemParseException {
149
+        String regexExpiration = "([E|e]........[N|n]:)(\\d\\/\\d{2}\\/\\d{4})";
150
+        Pattern pattern = Pattern.compile(regexExpiration);
151
+        Matcher matcher = pattern.matcher(expiration);
152
+        if (matcher.find()) {
153
+            return matcher.group(2).toLowerCase();
154
+        } else {
155
+            throw new ItemParseException();
156
+        }
27 157
     }
28 158
 
159
+    public String printParsedJerkSON() {
160
+        StringBuilder print = new StringBuilder();
161
+        for (Map.Entry<String, ArrayList<Item>> entry : realFoodList.entrySet()) {
162
+            print.append("Name: ");
163
+            print.append(String.format("%-10s",entry.getKey()));
164
+            print.append("\t\tseen: " + entry.getValue().size() + " times\n");
165
+            print.append("================" + "\t\t" + "==================\n");
29 166
 
30 167
 
168
+
169
+            ArrayList<Double> priceList = getPrices(entry);
170
+
171
+            for (int i = 0; i < priceList.size(); i++) {
172
+                print.append("Price: ");
173
+                print.append(String.format("%-10s",priceList.get(i)));
174
+                print.append("\t\tseen: " + countNumberOfDifferentPrices(entry.getValue(), priceList.get(i)) + " times\n");
175
+                print.append("----------------" + "\t\t" + "------------------\n");
176
+
177
+            }
178
+            print.append("\n");
179
+        }
180
+
181
+        print.append("\nErrors" + "\t\t\t\t\tseen: " + ItemParseException.getCount() + " times\n");
182
+        return print.toString();
183
+    }
184
+
31 185
 }
186
+
187
+
188
+
189
+

+ 6
- 2
src/main/java/io/zipcoder/Main.java View File

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

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

@@ -5,6 +5,7 @@ import org.junit.Before;
5 5
 import org.junit.Test;
6 6
 
7 7
 import java.util.ArrayList;
8
+import java.util.Map;
8 9
 
9 10
 import static org.junit.Assert.*;
10 11
 
@@ -14,11 +15,13 @@ public class ItemParserTest {
14 15
 
15 16
     private String rawSingleItemIrregularSeperatorSample = "naMe:MiLK;price:3.23;type:Food^expiration:1/11/2016##";
16 17
 
17
-    private String rawBrokenSingleItem =    "naMe:Milk;price:3.23;type:Food;expiration:1/25/2016##";
18
+    private String rawBrokenSingleItem =    "naMe:Milk;price:;;type:Food;expiration:1/25/2016##";
18 19
 
19 20
     private String rawMultipleItems = "naMe:Milk;price:3.23;type:Food;expiration:1/25/2016##"
20 21
                                       +"naME:BreaD;price:1.23;type:Food;expiration:1/02/2016##"
21 22
                                       +"NAMe:BrEAD;price:1.23;type:Food;expiration:2/25/2016##";
23
+
24
+
22 25
     private ItemParser itemParser;
23 26
 
24 27
     @Before
@@ -59,4 +62,112 @@ public class ItemParserTest {
59 62
         Integer actual = itemParser.findKeyValuePairsInRawItemData(rawSingleItemIrregularSeperatorSample).size();
60 63
         assertEquals(expected, actual);
61 64
     }
65
+
66
+    @Test
67
+    public void addNewItemToMapTest() {
68
+        //Given
69
+        boolean expected = true;
70
+        ArrayList<String> testList = itemParser.parseRawDataIntoStringArray(rawMultipleItems);
71
+        //When
72
+        itemParser.addItemToList(testList);
73
+        boolean actual = itemParser.getRealFoodList().keySet().contains("milk");
74
+        //Then
75
+        Assert.assertEquals(expected,actual);
76
+    }
77
+
78
+    @Test
79
+    public void addItemToListTest(){
80
+        //Given
81
+        boolean expected = true;
82
+        ArrayList<String> testList;
83
+        testList = itemParser.parseRawDataIntoStringArray(rawMultipleItems);
84
+        itemParser.addItemToList(testList);
85
+        //When
86
+        boolean actual = itemParser.getRealFoodList().keySet().contains("milk");
87
+        //Then
88
+        Assert.assertEquals(expected,actual);
89
+    }
90
+
91
+    @Test
92
+    public void countNumberOfDifferentPricesTest(){
93
+        //Given
94
+        int expected = 2;
95
+        ArrayList<Item> testList = new ArrayList<>();
96
+        Item cake = new Item("cake", 2.50, "Food", "1/24/18");
97
+        Item cake2 = new Item("cake", 2.50, "Food", "1/24/18");
98
+        Item pie = new Item("cie", 2.00, "Food", "1/18/18");
99
+        testList.add(cake);
100
+        testList.add(cake2);
101
+        testList.add(pie);
102
+        //When
103
+        int actual = itemParser.countNumberOfDifferentPrices(testList, 2.50);
104
+        //Then
105
+        Assert.assertEquals(expected,actual);
106
+    }
107
+
108
+    @Test
109
+    public void fixCookieTest() throws ItemParseException {
110
+        //Given
111
+        String expected = "cookies";
112
+        String messedUpCookie = "c00kies";
113
+        //When
114
+       String actual = itemParser.fixCo0kie(messedUpCookie);
115
+        //Then
116
+        Assert.assertEquals(expected,actual);
117
+    }
118
+
119
+    @Test
120
+    public void findNameTest() throws ItemParseException {
121
+        //Given
122
+        String expected = "cake";
123
+        String messedUpName = "NaME:cAkE";
124
+        //When
125
+        String actual = itemParser.findName(messedUpName);
126
+        //Then
127
+        Assert.assertEquals(expected,actual);
128
+    }
129
+
130
+    @Test
131
+    public void findCo0kieName() throws ItemParseException {
132
+        //Given
133
+        String expected = "cookies";
134
+        String messedUpName = "NaME:co0kies";
135
+        //When
136
+        String actual = itemParser.findName(messedUpName);
137
+        //Then
138
+        Assert.assertEquals(expected,actual);
139
+    }
140
+
141
+    @Test
142
+    public void findPriceTest() throws ItemParseException {
143
+        //Given
144
+        String expected = "2.50";
145
+        String messedUpPrice = "PriCe:2.50";
146
+        //When
147
+        String actual = itemParser.findPrice(messedUpPrice);
148
+        //Then
149
+        Assert.assertEquals(expected,actual);
150
+    }
151
+
152
+    @Test
153
+    public void findTypeTest() throws ItemParseException {
154
+        //Given
155
+        String expected = "food";
156
+        String messedUpType = "TypE:FoOd";
157
+        //When
158
+        String actual = itemParser.findType(messedUpType);
159
+        //Then
160
+        Assert.assertEquals(expected,actual);
161
+    }
162
+
163
+    @Test
164
+    public void findExpirationTest() throws ItemParseException {
165
+        //Given
166
+        String expected = "1/24/2018";
167
+        String messedUpExpiration = "expirATIOn:1/24/2018";
168
+        //When
169
+        String actual = itemParser.findExpiration(messedUpExpiration);
170
+        //Then
171
+        Assert.assertEquals(expected,actual);
172
+    }
62 173
 }