Explorar el Código

Need to format output

Lauren Green hace 6 años
padre
commit
b93aa94fb3

+ 12
- 0
pom.xml Ver fichero

@@ -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>

+ 16
- 0
src/main/java/io/zipcoder/ItemParseException.java Ver fichero

@@ -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 Ver fichero

@@ -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
 }

+ 97
- 0
src/main/java/io/zipcoder/Items.java Ver fichero

@@ -0,0 +1,97 @@
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 = "name: " + "Milk " + "seen: " + milk.size() + " times\n" +
68
+                "====================\n";
69
+            for(Double priceM : milkPrices.keySet()) {
70
+                result += "Price: " + priceM + " seen: " + milkPrices.get(priceM) + " times\n" +
71
+                "---------------------\n";
72
+            }
73
+            result += "\nname: " + "Bread " + "seen: " + bread.size() + " times\n" +
74
+                    "====================\n";
75
+        for(Double priceB : breadPrices.keySet()) {
76
+            result += "Price: " + priceB + " seen: " + breadPrices.get(priceB) + " times\n" +
77
+                    "---------------------\n";
78
+        }
79
+        result += "\nname: " + "Cookies " + "seen: " + cookies.size() + " times\n" +
80
+                "====================\n";
81
+        for(Double priceC : cookiesPrices.keySet()) {
82
+            result += "Price: " + priceC + " seen: " + cookiesPrices.get(priceC) + " times\n" +
83
+                    "---------------------\n";
84
+        }
85
+        result += "\nname: " + "Apples " + "seen: " + apples.size() + " times\n" +
86
+                "====================\n";
87
+        for(Double priceA : applesPrices.keySet()) {
88
+            result += "Price: " + priceA + " seen: " + applesPrices.get(priceA) + " times\n" +
89
+                    "---------------------\n";
90
+        }
91
+        result += "\nErrors    seen: " + ItemParseException.getCount() + " times";
92
+
93
+        return result;
94
+
95
+    }
96
+
97
+}

+ 29
- 2
src/main/java/io/zipcoder/Main.java Ver fichero

@@ -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
 }

+ 12
- 9
src/test/java/io/zipcoder/ItemParserTest.java Ver fichero

@@ -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##"
@@ -36,27 +36,30 @@ public class ItemParserTest {
36 36
 
37 37
     @Test
38 38
     public void parseStringIntoItemTest() throws ItemParseException{
39
-        Item expected = new Item("milk", 3.23, "food","1/25/2016");
40
-        Item actual = itemParser.parseStringIntoItem(rawSingleItem);
39
+        Item expected = new Item("milk", 3.23, "Food","1/25/2016");
40
+        ArrayList<String> itemArray = itemParser.parseRawDataIntoStringArray(rawSingleItem);
41
+        Item actual = itemParser.parseStringIntoItem(itemParser.mapKeysAndValuesOfItem(itemArray.get(0)));
41 42
         assertEquals(expected.toString(), actual.toString());
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();
60 63
         assertEquals(expected, actual);
61 64
     }
62 65
 }