Sfoglia il codice sorgente

Merge 9262de7c5ee1ef3bf4cafabbd14e23d8779e2eec into 23c2c01bf07b924ac9b860fa2771bf6ceb246ad5

kfennimore 6 anni fa
parent
commit
f339cc937d
No account linked to committer's email

+ 12
- 0
pom.xml Vedi 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>

+ 5
- 0
src/main/java/io/zipcoder/Item.java Vedi File

@@ -1,5 +1,7 @@
1 1
 package io.zipcoder;
2 2
 
3
+import java.util.HashMap;
4
+
3 5
 public class Item {
4 6
     private String name;
5 7
     private Double price;
@@ -16,6 +18,7 @@ public class Item {
16 18
      * @param expiration
17 19
      */
18 20
     public Item(String name, Double price, String type, String expiration){
21
+
19 22
         this.name = name;
20 23
         this.price = price;
21 24
         this.type = type;
@@ -45,4 +48,6 @@ public class Item {
45 48
     public String toString(){
46 49
         return "name:" + name + " price:" + price + " type:" + type + " expiration:" + expiration;
47 50
     }
51
+
52
+
48 53
 }

+ 5
- 1
src/main/java/io/zipcoder/ItemParseException.java Vedi File

@@ -1,4 +1,8 @@
1 1
 package io.zipcoder;
2 2
 
3
+import static java.lang.Float.NaN;
4
+
3 5
 public class ItemParseException extends Exception {
4
-}
6
+
7
+
8
+    }

+ 185
- 6
src/main/java/io/zipcoder/ItemParser.java Vedi File

@@ -1,21 +1,193 @@
1 1
 package io.zipcoder;
2 2
 
3
+
4
+
5
+import jdk.nashorn.internal.objects.Global;
6
+
7
+import java.io.FileNotFoundException;
8
+import java.io.PrintWriter;
3 9
 import java.util.ArrayList;
4 10
 import java.util.Arrays;
11
+import java.util.HashMap;
12
+import java.util.Map;
13
+import java.util.logging.Logger;
14
+import java.util.regex.Matcher;
15
+import java.util.regex.Pattern;
16
+import java.util.stream.Collectors;
17
+
18
+
19
+public final class ItemParser {
20
+
21
+    private PrintWriter writer ;
22
+    private Map<String,ArrayList<Item>> groceryMap;
23
+
24
+    //counts exceptions
25
+    int exception = 0;
26
+
27
+    public ItemParser() {
28
+
29
+        groceryMap = new HashMap<>();
30
+        //add printWriter in constructor so that there is only ONE
31
+        try {
32
+            writer= new PrintWriter("/Users/karoushafennimore/Dev/PainfulAfternoon/src/main/resources/errors.txt");
33
+        } catch (FileNotFoundException e) {
34
+            e.printStackTrace();
35
+        }
36
+    }
37
+
38
+    public String printGroceryList(){
39
+
40
+        StringBuilder sb = new StringBuilder();
41
+        //for every new entry - put the Key and how many times you saw it
42
+        for (Map.Entry<String , ArrayList<Item> > item : groceryMap.entrySet()) {
43
+            sb.append(String.format("\nname: %9s", item.getKey().substring(0, 1).toUpperCase() + item.getKey().substring(1)));
44
+            sb.append("\t\t\tseen:  " + item.getValue().size() + "  times\n" + "===============" + "\t\t\t" + "===============\n");
5 45
 
6
-public class ItemParser {
46
+            ArrayList<Double> tempList = uniquePrices(item);
7 47
 
48
+            for(int i = 0; i < tempList.size(); i++) {
49
+                sb.append(String.format("Price: %8s", tempList.get(i)));
50
+                sb.append("\t\t\tseen:  " + priceCount(item.getValue(), tempList.get(i)) + "  times\n---------------" + "\t\t\t" + "---------------\n");
51
+            }
52
+        }
53
+        sb.append("\nErrors" + "\t\t\t\t\tseen:  " + exception + "  times\n");
54
+
55
+        return sb.toString();
56
+    }
57
+
58
+
59
+
60
+    public ArrayList<Double> uniquePrices(Map.Entry<String , ArrayList<Item> > item) {
61
+
62
+        //returning unique Prices from the array list
63
+        ArrayList<Double> uniquePrice = new ArrayList<>();
64
+        //loop through and check if the price has been repeated...if it hasn't then add it
65
+        for (int i = 0; i < item.getValue().size(); i++) {
66
+            if (!uniquePrice.contains(item.getValue().get(i).getPrice())) {
67
+                uniquePrice.add(item.getValue().get(i).getPrice());
68
+            }
69
+        }
70
+        //output ArrayList of Doubles
71
+        return uniquePrice;
72
+
73
+    }
74
+
75
+    public int priceCount(ArrayList<Item> item, Double price) {
76
+        int count = 0;
77
+        //loop through items and look for the price to see how many times it is there
78
+        for(int i = 0; i < item.size(); i++) {
79
+            if(item.get(i).getPrice().equals(price)) {
80
+                count++;
81
+            }
82
+        }
83
+        return count;
84
+    }
85
+
86
+    public Map<String, ArrayList<Item>> getMap() {
87
+        return this.groceryMap;
88
+    }
8 89
 
9 90
     public ArrayList<String> parseRawDataIntoStringArray(String rawData){
10
-        String stringPattern = "##";
11
-        ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawData);
12
-        return response;
91
+        return splitStringWithRegexPattern("##" , rawData);
92
+    }
93
+
94
+    private void incrementList(Map<String, ArrayList<Item>> myMap, Item myItem) {
95
+        //if the Key is already in the Map
96
+        if(myMap.keySet().contains(myItem.getName())) {
97
+            //giving the map the item name and then adding the value to THAT key
98
+            myMap.get(myItem.getName()).add(myItem);
99
+        }else {
100
+            //if the key is not in the map then add it
101
+            myMap.put(myItem.getName(), new ArrayList<Item>());
102
+            //then add the value to that added KEY!
103
+            myMap.get(myItem.getName()).add(myItem);
104
+        }
105
+
106
+    }
107
+
108
+    public void addItemToList(ArrayList<String> groceryList) {
109
+
110
+        for(int i = 0; i < groceryList.size(); i++) {
111
+            try {
112
+                Item tryItem = (parseStringIntoItem(groceryList.get(i)));
113
+                incrementList(groceryMap, tryItem);
114
+            } catch (ItemParseException e) {
115
+                exception++;
116
+                try {
117
+                    printErrorToFile(e);
118
+                } catch (FileNotFoundException e1) {
119
+                    e1.printStackTrace();
120
+                }
121
+            }
122
+        }
123
+    }
124
+
125
+    public Item parseStringIntoItem(String rawItem) throws ItemParseException {
126
+        String itemName = findName(rawItem);
127
+        Double itemPrice = itemPrice(rawItem);
128
+        String itemType = itemType(rawItem);
129
+        String itemExpiration = itemExpiration(rawItem);
130
+
131
+        //if item does not have a name or item Price - dont create it bc its irrelevant data.
132
+        if(findName(rawItem) == null || itemPrice(rawItem) == null) {
133
+            throw new ItemParseException();
134
+        }
135
+        return new Item (itemName, itemPrice, itemType, itemExpiration);
136
+    }
137
+
138
+
139
+    //getting all the values from string for
140
+    private String findName(String rawItem) {
141
+        Pattern namePattern = Pattern.compile("(?<=([Nn][Aa][Mm][Ee][^A-Za-z])).*?(?=[^A-Za-z0])");
142
+        Matcher regex = namePattern.matcher(rawItem);
143
+
144
+        if (regex.find()) {
145
+            if (regex.group().length() > 0) {
146
+                return replaceZeros(regex.group().toLowerCase());
147
+            } else {
148
+                return null;
149
+            }
150
+        } else {
151
+            return null;
152
+        }
153
+    }
154
+
155
+    public Double itemPrice(String rawItem) {
156
+        Pattern pricePattern = Pattern.compile("(?<=([Pp][Rr][Ii][Cc][Ee][^A-Za-z])).*?(?=[^0-9.])");
157
+        Matcher regex1 = pricePattern.matcher(rawItem);
158
+        if (regex1.find()) {
159
+            if (regex1.group().length() > 0) {
160
+                return Double.parseDouble(regex1.group().toLowerCase());
161
+            }
162
+        }
163
+        return null;
13 164
     }
14 165
 
15
-    public Item parseStringIntoItem(String rawItem) throws ItemParseException{
166
+    public String itemType(String rawItem) {
167
+        Pattern typePattern = Pattern.compile("(?<=([Tt][Yy][Pp][Ee][^A-Za-z])).*?(?=[^A-Za-z0])");
168
+        Matcher regex2 = typePattern.matcher(rawItem);
169
+        if (regex2.find()) {
170
+            return regex2.group().toLowerCase();
171
+        }
172
+        return null;
173
+    }
174
+    public String itemExpiration(String rawItem) {
175
+        Pattern expirationPattern = Pattern.compile("(?<=([Ee][Xx][Pp][Ii][Rr][Aa][Tt][Ii][Oo][Nn][^A-Za-z]))(.)*[^#]");
176
+        Matcher regex3 = expirationPattern.matcher(rawItem);
177
+        if (regex3.find()) {
178
+            return regex3.group();
179
+        }
16 180
         return null;
17 181
     }
18 182
 
183
+    //this is to catch the 0's in cookie and replace with a o instead
184
+    private String replaceZeros(String rawItem) {
185
+
186
+        Pattern pattern = Pattern.compile("[0]");
187
+        Matcher regexName2 = pattern.matcher(rawItem);
188
+        return regexName2.replaceAll("o");
189
+    }
190
+
19 191
     public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){
20 192
         String stringPattern = "[;|^]";
21 193
         ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawItem);
@@ -23,9 +195,16 @@ public class ItemParser {
23 195
     }
24 196
 
25 197
     private ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString){
26
-        return new ArrayList<String>(Arrays.asList(inputString.split(stringPattern)));
198
+        return new ArrayList<>(Arrays.asList(inputString.split(stringPattern)));
27 199
     }
28 200
 
201
+    public void printErrorToFile(ItemParseException e) throws FileNotFoundException {
202
+        //getting entire stack report on the errors that I am getting.
203
+        writer.write(Arrays.stream(e.getStackTrace()).map(Object::toString).collect(Collectors.joining("\n")));
29 204
 
205
+    }
30 206
 
207
+    public void flushExceptionsToFile() {
208
+        writer.close();
209
+    }
31 210
 }

+ 16
- 5
src/main/java/io/zipcoder/Main.java Vedi File

@@ -2,18 +2,29 @@ package io.zipcoder;
2 2
 
3 3
 import org.apache.commons.io.IOUtils;
4 4
 
5
+import java.util.ArrayList;
6
+import java.util.Map;
7
+
5 8
 
6 9
 public class Main {
7 10
 
8
-    public String readRawDataToString() throws Exception{
11
+    public static ItemParser itemParser = new ItemParser();
12
+
13
+    public String readRawDataToString() throws Exception {
9 14
         ClassLoader classLoader = getClass().getClassLoader();
10
-        String result = IOUtils.toString(classLoader.getResourceAsStream("RawData.txt"));
11
-        return result;
15
+
16
+        return IOUtils.toString(classLoader.getResourceAsStream("RawData.txt"));
12 17
     }
13 18
 
14
-    public static void main(String[] args) throws Exception{
19
+    public static void main(String[] args) throws Exception {
15 20
         String output = (new Main()).readRawDataToString();
16
-        System.out.println(output);
21
+        ArrayList<String> temp = itemParser.parseRawDataIntoStringArray(output);
22
+        itemParser.addItemToList(temp);
23
+        System.out.println(itemParser.printGroceryList());
24
+        //close the file when you're done writing to it!
25
+        itemParser.flushExceptionsToFile();
26
+
17 27
         // TODO: parse the data in output into items, and display to console.
18 28
     }
19 29
 }
30
+

+ 0
- 0
src/main/resources/errors.txt Vedi File


+ 46
- 1
src/test/java/io/zipcoder/ItemParserTest.java Vedi File

@@ -4,6 +4,7 @@ import org.junit.Assert;
4 4
 import org.junit.Before;
5 5
 import org.junit.Test;
6 6
 
7
+import java.io.FileNotFoundException;
7 8
 import java.util.ArrayList;
8 9
 
9 10
 import static org.junit.Assert.*;
@@ -14,7 +15,7 @@ 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:;price:3.23;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##"
@@ -23,6 +24,7 @@ public class ItemParserTest {
23 24
 
24 25
     @Before
25 26
     public void setUp(){
27
+
26 28
         itemParser = new ItemParser();
27 29
     }
28 30
 
@@ -59,4 +61,47 @@ public class ItemParserTest {
59 61
         Integer actual = itemParser.findKeyValuePairsInRawItemData(rawSingleItemIrregularSeperatorSample).size();
60 62
         assertEquals(expected, actual);
61 63
     }
64
+
65
+    @Test
66
+    public void addItemToListTest() {
67
+        //Given
68
+        ArrayList<String> testList;
69
+        //When
70
+        testList = itemParser.parseRawDataIntoStringArray(rawMultipleItems);
71
+        itemParser.addItemToList(testList);
72
+        //SIZE IS TWO BECAUSE BREAD IS REPEATED TWICE AND ITS A UNIQUE KEY!!!
73
+        int expected = 2;
74
+        int actual = itemParser.getMap().size();
75
+        //Then
76
+        Assert.assertEquals(expected, actual);
77
+    }
78
+
79
+    @Test
80
+    public void addItemToListTest2() {
81
+        //Given
82
+        ArrayList<String> testList;
83
+        //When
84
+        testList = itemParser.parseRawDataIntoStringArray(rawMultipleItems);
85
+        itemParser.addItemToList(testList);
86
+        String expected = "[bread, milk]";
87
+        String actual = itemParser.getMap().keySet().toString();
88
+        //Then
89
+        Assert.assertEquals(expected, actual);
90
+    }
91
+
92
+    @Test
93
+    public void addItemToListTest3() {
94
+        //Given
95
+        ArrayList<String> testList;
96
+        //When
97
+        testList = itemParser.parseRawDataIntoStringArray(rawMultipleItems);
98
+        itemParser.addItemToList(testList);
99
+        boolean expected = true;
100
+        boolean actual = itemParser.getMap().keySet().contains("bread");
101
+        System.out.println(itemParser.printGroceryList());
102
+        //Then
103
+        Assert.assertEquals(expected, actual);
104
+    }
105
+
106
+
62 107
 }