#2 laurenstewartgreen

Отворено
laurengreen жели да споји 3 комит(е) из laurengreen/JerkSON-Parser:master у master

+ 12
- 0
pom.xml Прегледај датотеку

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

+ 15
- 0
src/main/java/io/zipcoder/Item.java Прегледај датотеку

@@ -45,4 +45,19 @@ public class Item {
45 45
     public String toString(){
46 46
         return "name:" + name + " price:" + price + " type:" + type + " expiration:" + expiration;
47 47
     }
48
+
49
+    @Override
50
+    public boolean equals(Object o) {
51
+        Item otherItem = (Item) o;
52
+        if(!this.getName().equalsIgnoreCase(((Item) o).getName()))
53
+            return false;
54
+        if(!this.getType().equalsIgnoreCase(((Item) o).getType()))
55
+            return false;
56
+        if(!this.getPrice().equals(((Item) o).getPrice()))
57
+            return false;
58
+        if(!this.getExpiration().equalsIgnoreCase(((Item) o).getExpiration()))
59
+            return false;
60
+
61
+        return true;
62
+    }
48 63
 }

+ 16
- 0
src/main/java/io/zipcoder/ItemParseException.java Прегледај датотеку

@@ -1,4 +1,20 @@
1 1
 package io.zipcoder;
2 2
 
3 3
 public class ItemParseException extends Exception {
4
+
5
+    private static int count = 0;
6
+
7
+    public ItemParseException(String message)
8
+    {
9
+        super(message);
10
+
11
+        {
12
+            count++;
13
+        }
14
+    }
15
+
16
+    public static int getCount()
17
+    {
18
+        return count;
19
+    }
4 20
 }

+ 64
- 3
src/main/java/io/zipcoder/ItemParser.java Прегледај датотеку

@@ -2,6 +2,8 @@ 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;
5 7
 
6 8
 public class ItemParser {
7 9
 
@@ -12,12 +14,41 @@ public class ItemParser {
12 14
         return response;
13 15
     }
14 16
 
15
-    public Item parseStringIntoItem(String rawItem) throws ItemParseException{
16
-        return null;
17
+    public Item parseStringIntoItem(HashMap<String, String> itemMap) throws ItemParseException {
18
+
19
+        String name = null;
20
+        Double price = null;
21
+        String type = null;
22
+        String expiration = null;
23
+
24
+        if(checkForEmptyValues(itemMap)) {
25
+            throw new ItemParseException("Missing Values");
26
+        } else {
27
+            for (String key : itemMap.keySet()) {
28
+                if (key.matches("(?i:.*NAME.*)")) {
29
+                    if (!itemMap.get(key).equals(""))
30
+                        name = identifyName(itemMap.get(key));
31
+                }
32
+                if (key.matches("(?i:.*PRICE.*)")) {
33
+                    if (!itemMap.get(key).equals(""))
34
+                        price = Double.parseDouble(itemMap.get(key));
35
+                }
36
+                if (key.matches("(?i:.*TYPE.*)")) {
37
+                    if (!itemMap.get(key).equals(""))
38
+                        type = itemMap.get(key);
39
+                }
40
+                if (key.matches("(?i:.*EXPIRATION.*)")) {
41
+                    if (!itemMap.get(key).equals(""))
42
+                        expiration = itemMap.get(key);
43
+                }
44
+            }
45
+        }
46
+
47
+        return new Item(name, price, type, expiration);
17 48
     }
18 49
 
19 50
     public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){
20
-        String stringPattern = "[;|^]";
51
+        String stringPattern = "[:!;^%*@!]";
21 52
         ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawItem);
22 53
         return response;
23 54
     }
@@ -26,6 +57,36 @@ public class ItemParser {
26 57
         return new ArrayList<String>(Arrays.asList(inputString.split(stringPattern)));
27 58
     }
28 59
 
60
+    public HashMap<String, String> mapKeysAndValuesOfItem(String rawItem) {
61
+        ArrayList<String> item = findKeyValuePairsInRawItemData(rawItem);
62
+        HashMap<String, String> itemMap = new HashMap<String, String>();
63
+        for (int i = 0; i < item.size(); i+=2) {
64
+            itemMap.put(item.get(i), item.get(i+1));
65
+        }
66
+        return itemMap;
67
+    }
29 68
 
69
+    public boolean checkForEmptyValues(HashMap<String, String> itemMap) {
70
+        for(String s: itemMap.keySet()) {
71
+        if(itemMap.get(s).equals("")) {
72
+            return true;
73
+            }
74
+        }
75
+        return false;
76
+    }
77
+
78
+
79
+    public String identifyName(String input) {
80
+        if(input.matches("(?i:.*bread.*)"))
81
+            return "bread";
82
+        if(input.matches("(?i:.*milk.*)"))
83
+            return "milk";
84
+        if(input.matches("(?i:.*apples.*)"))
85
+            return "apples";
86
+        if(input.matches("[Cc][Oo0]{2}(?i:.*kies.*)"))
87
+            return "cookies";
88
+
89
+        return null;
90
+    }
30 91
 
31 92
 }

+ 101
- 0
src/main/java/io/zipcoder/Items.java Прегледај датотеку

@@ -0,0 +1,101 @@
1
+package io.zipcoder;
2
+
3
+import java.util.ArrayList;
4
+import java.util.HashMap;
5
+
6
+public class Items extends ArrayList<Item>{
7
+
8
+    public Items getMilks() {
9
+        Items milkItems = new Items();
10
+        for(Item item : this) {
11
+            if(item.getName().equalsIgnoreCase("Milk")) {
12
+                milkItems.add(item);
13
+            }
14
+        }
15
+        return milkItems;
16
+    }
17
+
18
+    public Items getBread() {
19
+        Items breadItems = new Items();
20
+        for(Item item : this) {
21
+            if(item.getName().equalsIgnoreCase("Bread")) {
22
+                breadItems.add(item);
23
+            }
24
+        }
25
+        return breadItems;
26
+    }
27
+
28
+    public Items getCookies() {
29
+        Items cookiesItems = new Items();
30
+        for(Item item : this) {
31
+            if(item.getName().equalsIgnoreCase("Cookies")) {
32
+                cookiesItems.add(item);
33
+            }
34
+        }
35
+        return cookiesItems;
36
+    }
37
+
38
+    public Items getApples() {
39
+        Items applesItems = new Items();
40
+        for(Item item : this) {
41
+            if(item.getName().equalsIgnoreCase("Apples")) {
42
+                applesItems.add(item);
43
+            }
44
+        }
45
+        return applesItems;
46
+    }
47
+
48
+    public HashMap<Double, Integer> getPriceCount() {
49
+
50
+        HashMap<Double, Integer> priceMap = new HashMap<>();
51
+
52
+        for(Item item : this) {
53
+            if(priceMap.containsKey(item.getPrice())) {
54
+                int count = priceMap.get(item.getPrice());
55
+                count = count + 1;
56
+                priceMap.put(item.getPrice(), count);
57
+            } else {
58
+                priceMap.put(item.getPrice(), 1);
59
+            }
60
+        }
61
+
62
+        return priceMap;
63
+    }
64
+
65
+    public static String prettyOutput(Items milk, HashMap<Double, Integer> milkPrices, Items bread, HashMap<Double, Integer> breadPrices, Items cookies, HashMap<Double, Integer> cookiesPrices, Items apples, HashMap<Double, Integer> applesPrices) {
66
+
67
+        String result = String.format("%-8s %4s %12s %d %6s", "name:", "Milk", "seen:", milk.size(), "times\n") +
68
+                String.format("%12s %6s %13s", "=============", "", "=============\n");
69
+        for(Double priceM : milkPrices.keySet()) {
70
+                result += String.format("%-8s %.2f %12s %d %6s", "Price:", priceM, "seen:", milkPrices.get(priceM), "times\n") +
71
+                        String.format("%12s %6s %13s", "-------------", "", "-------------\n");
72
+            }
73
+
74
+            result += String.format("%-8s %4s %12s %d %6s", "\nname:", "Bread", "seen:", bread.size(), "times\n") +
75
+                String.format("%12s %6s %13s", "=============", "", "=============\n");
76
+        for(Double priceB : breadPrices.keySet()) {
77
+            result += String.format("%-8s %.2f %12s %d %6s", "Price:", priceB, "seen:", breadPrices.get(priceB), "times\n") +
78
+                    String.format("%12s %6s %13s", "-------------", "", "-------------\n");
79
+        }
80
+
81
+        result += String.format("%-5s %4s %12s %d %6s", "\nname:", "Cookies", "seen:", cookies.size(), "times\n") +
82
+                String.format("%12s %6s %13s", "=============", "", "=============\n");
83
+        for(Double priceC : cookiesPrices.keySet()) {
84
+            result += String.format("%-8s %.2f %12s %d %6s", "Price:", priceC, "seen:", cookiesPrices.get(priceC), "times\n") +
85
+                    String.format("%12s %6s %13s", "-------------", "", "-------------\n");
86
+        }
87
+
88
+        result += String.format("%-7s %4s %12s %d %6s", "\nname:", "Apples", "seen:", apples.size(), "times\n") +
89
+                String.format("%12s %6s %13s", "=============", "", "=============\n");
90
+        for(Double priceA : applesPrices.keySet()) {
91
+            result += String.format("%-8s %.2f %12s %d %6s", "Price:", priceA, "seen:", applesPrices.get(priceA), "times\n") +
92
+                    String.format("%12s %6s %13s", "-------------", "", "-------------\n");
93
+        }
94
+
95
+        result += String.format("%-12s %14s %d %5s", "\nErrors", "seen:", ItemParseException.getCount(), "times");
96
+
97
+        return result;
98
+
99
+    }
100
+
101
+}

+ 29
- 2
src/main/java/io/zipcoder/Main.java Прегледај датотеку

@@ -2,6 +2,9 @@ package io.zipcoder;
2 2
 
3 3
 import org.apache.commons.io.IOUtils;
4 4
 
5
+import java.util.ArrayList;
6
+import java.util.HashMap;
7
+
5 8
 
6 9
 public class Main {
7 10
 
@@ -11,9 +14,33 @@ public class Main {
11 14
         return result;
12 15
     }
13 16
 
14
-    public static void main(String[] args) throws Exception{
17
+    public static void main(String[] args) throws Exception {
15 18
         String output = (new Main()).readRawDataToString();
19
+        ItemParser itemParser = new ItemParser();
20
+        ArrayList<String> strItems = itemParser.parseRawDataIntoStringArray(output);
21
+        Item item = null;
22
+        Items items = new Items();
23
+        for(String itemStr : strItems) {
24
+            HashMap<String, String> itemMap = itemParser.mapKeysAndValuesOfItem(itemStr);
25
+
26
+            try {
27
+                item = itemParser.parseStringIntoItem(itemMap);
28
+                items.add(item);
29
+            } catch (ItemParseException e) {
30
+            }
31
+
32
+        }
33
+        Items milkItems = items.getMilks();
34
+        HashMap<Double, Integer> milkPrices = milkItems.getPriceCount();
35
+        Items breadItems = items.getBread();
36
+        HashMap<Double, Integer> breadPrices = breadItems.getPriceCount();
37
+        Items cookieItems = items.getCookies();
38
+        HashMap<Double, Integer> cookiePrices = cookieItems.getPriceCount();
39
+        Items appleItems = items.getApples();
40
+        HashMap<Double, Integer> applePrices = appleItems.getPriceCount();
41
+
42
+        output = Items.prettyOutput(milkItems, milkPrices, breadItems, breadPrices, cookieItems, cookiePrices, appleItems, applePrices);
43
+
16 44
         System.out.println(output);
17
-        // TODO: parse the data in output into items, and display to console.
18 45
     }
19 46
 }

+ 40
- 9
src/test/java/io/zipcoder/ItemParserTest.java Прегледај датотеку

@@ -14,7 +14,7 @@ public class ItemParserTest {
14 14
 
15 15
     private String rawSingleItemIrregularSeperatorSample = "naMe:MiLK;price:3.23;type:Food^expiration:1/11/2016##";
16 16
 
17
-    private String rawBrokenSingleItem =    "naMe:Milk;price:3.23;type:Food;expiration:1/25/2016##";
17
+    private String rawBrokenSingleItem =    "naMe:;price:3.23;type:Food;expiration:1/25/2016##";
18 18
 
19 19
     private String rawMultipleItems = "naMe:Milk;price:3.23;type:Food;expiration:1/25/2016##"
20 20
                                       +"naME:BreaD;price:1.23;type:Food;expiration:1/02/2016##"
@@ -37,26 +37,57 @@ public class ItemParserTest {
37 37
     @Test
38 38
     public void parseStringIntoItemTest() throws ItemParseException{
39 39
         Item expected = new Item("milk", 3.23, "food","1/25/2016");
40
-        Item actual = itemParser.parseStringIntoItem(rawSingleItem);
41
-        assertEquals(expected.toString(), actual.toString());
40
+        ArrayList<String> itemArray = itemParser.parseRawDataIntoStringArray(rawSingleItem);
41
+        Item actual = itemParser.parseStringIntoItem(itemParser.mapKeysAndValuesOfItem(itemArray.get(0)));
42
+        assertEquals(expected, actual);
42 43
     }
43 44
 
44 45
     @Test(expected = ItemParseException.class)
45 46
     public void parseBrokenStringIntoItemTest() throws ItemParseException{
46
-        itemParser.parseStringIntoItem(rawBrokenSingleItem);
47
-    }
47
+        ArrayList<String> itemArray = itemParser.parseRawDataIntoStringArray(rawBrokenSingleItem);
48
+        Item actual = itemParser.parseStringIntoItem(itemParser.mapKeysAndValuesOfItem(itemArray.get(0)));    }
48 49
 
49 50
     @Test
50 51
     public void findKeyValuePairsInRawItemDataTest(){
51
-        Integer expected = 4;
52
-        Integer actual = itemParser.findKeyValuePairsInRawItemData(rawSingleItem).size();
52
+        Integer expected = 8;
53
+        ArrayList<String> itemArray = itemParser.parseRawDataIntoStringArray(rawBrokenSingleItem);
54
+        Integer actual = itemParser.findKeyValuePairsInRawItemData(itemArray.get(0)).size();
53 55
         assertEquals(expected, actual);
54 56
     }
55 57
 
56 58
     @Test
57 59
     public void findKeyValuePairsInRawItemDataTestIrregular(){
58
-        Integer expected = 4;
59
-        Integer actual = itemParser.findKeyValuePairsInRawItemData(rawSingleItemIrregularSeperatorSample).size();
60
+        Integer expected = 8;
61
+        ArrayList<String> itemArray = itemParser.parseRawDataIntoStringArray(rawSingleItemIrregularSeperatorSample);
62
+        Integer actual = itemParser.findKeyValuePairsInRawItemData(itemArray.get(0)).size();
63
+        assertEquals(expected, actual);
64
+    }
65
+
66
+    @Test
67
+    public void identifyNameTestCookieWithZero(){
68
+        String expected = "cookies";
69
+        String actual = itemParser.identifyName("C0oKies");
70
+        assertEquals(expected, actual);
71
+    }
72
+
73
+    @Test
74
+    public void identifyNameTestNull(){
75
+        String expected = null;
76
+        String actual = itemParser.identifyName("bob");
77
+        assertEquals(expected, actual);
78
+    }
79
+
80
+    @Test
81
+    public void identifyNameTestApples(){
82
+        String expected = "apples";
83
+        String actual = itemParser.identifyName("aPpLEs");
84
+        assertEquals(expected, actual);
85
+    }
86
+
87
+    @Test
88
+    public void identifyNameTestBread(){
89
+        String expected = "bread";
90
+        String actual = itemParser.identifyName("BReAD");
60 91
         assertEquals(expected, actual);
61 92
     }
62 93
 }

+ 73
- 0
src/test/java/io/zipcoder/ItemsTest.java Прегледај датотеку

@@ -0,0 +1,73 @@
1
+package io.zipcoder;
2
+
3
+import org.junit.Assert;
4
+import org.junit.Before;
5
+import org.junit.Test;
6
+
7
+import java.util.HashMap;
8
+
9
+public class ItemsTest {
10
+
11
+    Items items = new Items();
12
+
13
+    @Before
14
+    public void setUp() throws Exception {
15
+        Item item1 = new Item("Milk", 3.40, "Food", "11/12/2020");
16
+        Item item2 = new Item("Milk", 1.45, "Food", "11/12/2020");
17
+        Item item3 = new Item("Milk", 3.40, "Food", "12/5/2020");
18
+        Item item4 = new Item("Cookies", 1.75, "Food", "11/12/2020");
19
+        Item item5 = new Item("Apples", 0.75, "Food", "11/12/2020");
20
+        Item item6 = new Item("Bread", 1.00, "Food", "11/12/2020");
21
+        Item item7 = new Item("Bread", 1.40, "Food", "11/12/2020");
22
+        Item item8 = new Item("Bread", 1.00, "Food", "11/12/2020");
23
+
24
+        items.add(item1);
25
+        items.add(item2);
26
+        items.add(item3);
27
+        items.add(item4);
28
+        items.add(item5);
29
+        items.add(item6);
30
+        items.add(item7);
31
+        items.add(item8);
32
+    }
33
+
34
+    @Test
35
+    public void testGetMilk() {
36
+        Items milkItems = items.getMilks();
37
+        int expected = 3;
38
+        int actual = milkItems.size();
39
+        Assert.assertEquals(expected, actual);
40
+    }
41
+
42
+    @Test
43
+    public void testGetBread() {
44
+        Items breadItems = items.getBread();
45
+        int expected = 3;
46
+        int actual = breadItems.size();
47
+        Assert.assertEquals(expected, actual);
48
+    }
49
+
50
+    @Test
51
+    public void testGetCookies() {
52
+        Items cookieItems = items.getCookies();
53
+        int expected = 1;
54
+        int actual = cookieItems.size();
55
+        Assert.assertEquals(expected, actual);
56
+    }
57
+
58
+    @Test
59
+    public void testGetApples() {
60
+        Items appleItems = items.getApples();
61
+        int expected = 1;
62
+        int actual = appleItems.size();
63
+        Assert.assertEquals(expected, actual);
64
+    }
65
+
66
+    @Test
67
+    public void testGetMilkPrices() {
68
+        Items milkItems = items.getMilks();
69
+        HashMap<Double, Integer> milkPrices = milkItems.getPriceCount();
70
+        Assert.assertEquals(2, (int) milkPrices.get(3.40));
71
+        Assert.assertEquals(1, (int) milkPrices.get(1.45));
72
+    }
73
+}