#3 Completed JerkSON Parser

Open
mpierse wants to merge 4 commits from mpierse/JerkSON-Parser:master into master

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

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

+ 120
- 2
src/main/java/io/zipcoder/ItemParser.java View File

@@ -1,10 +1,30 @@
1 1
 package io.zipcoder;
2 2
 
3
+import com.sun.xml.internal.fastinfoset.util.CharArray;
4
+
3 5
 import java.util.ArrayList;
4 6
 import java.util.Arrays;
7
+import java.util.HashSet;
8
+import java.util.Set;
9
+import java.util.logging.Level;
10
+import java.util.logging.Logger;
11
+import java.util.regex.Matcher;
12
+import java.util.regex.Pattern;
5 13
 
6 14
 public class ItemParser {
7 15
 
16
+    private static final Logger log = Logger.getLogger( ItemParser.class.getName() );
17
+    private int exceptionCount = 0;
18
+    private Pattern patternAlpha = Pattern.compile("(?<=:)[^##\\s].*");
19
+    private Pattern patternDate = Pattern.compile("\\d/\\d\\d/\\d\\d\\d\\d");
20
+
21
+    public int getExceptionCount() {
22
+        return exceptionCount-1;
23
+    }
24
+
25
+    public void setExceptionCount(int exceptionCount) {
26
+        this.exceptionCount = exceptionCount;
27
+    }
8 28
 
9 29
     public ArrayList<String> parseRawDataIntoStringArray(String rawData){
10 30
         String stringPattern = "##";
@@ -12,12 +32,110 @@ public class ItemParser {
12 32
         return response;
13 33
     }
14 34
 
35
+    public String toLowerCase(String str){
36
+            StringBuilder inputLineT = new StringBuilder(str);
37
+            for(int i = 0 ; i < inputLineT.length() ; i++) {
38
+                if(inputLineT.charAt(i) >= 65 && inputLineT.charAt(i) <=91)
39
+                { inputLineT.setCharAt(i, (char)(inputLineT.charAt(i)+32)); }
40
+            }
41
+            return inputLineT.toString();
42
+        }
43
+
44
+    public String fixCookies(String str){
45
+        String result ="";
46
+        for (char c: str.toCharArray()) {
47
+            if(c == '0'){result += "o";}
48
+            else if (c != 0) result += c;
49
+        }
50
+        return result;
51
+    }
52
+
53
+
15 54
     public Item parseStringIntoItem(String rawItem) throws ItemParseException{
16
-        return null;
55
+        ArrayList<String> properties = findKeyValuePairsInRawItemData(toLowerCase(toLowerCase(rawItem)));
56
+        Matcher mName = patternAlpha.matcher(properties.get(0));
57
+        Matcher mPrice = patternAlpha.matcher(properties.get(1));
58
+        Matcher mDescription = patternAlpha.matcher(properties.get(2));
59
+        Matcher mExpiration = patternDate.matcher(properties.get(3));
60
+
61
+    if (mName.find() && mDescription.find() && mPrice.find() && mExpiration.find()) {
62
+        String name = fixCookies(mName.group());
63
+        Double price = Double.parseDouble(mPrice.group());
64
+        String type = mDescription.group();
65
+        String expiration = mExpiration.group();
66
+        return new Item(name, price, type, expiration);
67
+    } else {
68
+        throw new ItemParseException(); }
69
+    }
70
+
71
+    public ArrayList<Item> makeItemList(String rawData){
72
+        ArrayList<String> data = parseRawDataIntoStringArray(rawData);
73
+        ArrayList<Item> items = new ArrayList<Item>();
74
+        int exceptionCount = 1;
75
+        for (String parcel : data) {
76
+            try {
77
+                Item itemObj = parseStringIntoItem(parcel);
78
+               items.add(itemObj);
79
+            } catch (ItemParseException e){
80
+                exceptionCount++;
81
+                continue;
82
+            }
83
+        }
84
+        setExceptionCount(exceptionCount);
85
+        return items;
86
+    }
87
+
88
+    public Set<String> getUniqueNames(ArrayList<Item> list){
89
+        Set<String> set = new HashSet<String>();
90
+        for (Item item : list) {
91
+            set.add(item.getName());
92
+        }
93
+            return set;
94
+    }
95
+
96
+    public int getCountName(String name, ArrayList<Item> itemList){
97
+       int count = 0;
98
+        for (Item item : itemList) {
99
+            if (item.getName().equals(name)) count++;
100
+        }
101
+        return count;}
102
+
103
+    public Set<Double> getUniquePrices(String name, ArrayList<Item> itemList){
104
+        Set<Double> prices = new HashSet<Double>();
105
+        for (Item item: itemList) {
106
+            if(item.getName().equals(name)){
107
+                prices.add(item.getPrice());
108
+            }
109
+        }
110
+        return prices;
111
+    }
112
+
113
+    public int getCountPrice(Double price, String name, ArrayList<Item> itemList){
114
+        int count = 0;
115
+        for (Item item : itemList) {
116
+            if (item.getName().equals(name) && item.getPrice().equals(price)) count++;
117
+        }
118
+        return count;
119
+    }
120
+
121
+    public void printOutput(String rawData){
122
+       ArrayList<Item> itemList = makeItemList(rawData);
123
+        Set<String> nameSet = getUniqueNames(itemList);
124
+        for (String name : nameSet) {
125
+            Set<Double> priceList = getUniquePrices(name, itemList);
126
+            System.out.printf("%-13s %13s %n", "name: " + name, "seen: " + getCountName(name, itemList) + " times");
127
+            System.out.printf("%-13s %13s %n", "=============", "=============");
128
+            for (Double price : priceList) {
129
+                System.out.printf("%-13s %13s %n", "price: " + price,"seen: " + getCountPrice(price, name, itemList) + " times");
130
+                System.out.printf("%-13s %13s %n", "-------------", "-------------");
131
+            }
132
+            System.out.println();
133
+        }
134
+        System.out.printf("%-15s %15s %n","Errors", "seen: "+ getExceptionCount() + " times");
17 135
     }
18 136
 
19 137
     public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){
20
-        String stringPattern = "[;|^]";
138
+        String stringPattern = "[;|^!%@*]";
21 139
         ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawItem);
22 140
         return response;
23 141
     }

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

@@ -2,18 +2,33 @@ package io.zipcoder;
2 2
 
3 3
 import org.apache.commons.io.IOUtils;
4 4
 
5
+import java.io.IOException;
6
+import java.util.ArrayList;
7
+import java.util.logging.Level;
8
+import java.util.logging.Logger;
9
+
5 10
 
6 11
 public class Main {
7 12
 
8
-    public String readRawDataToString() throws Exception{
13
+    private static final Logger log = Logger.getLogger( ItemParser.class.getName() );
14
+
15
+    public String readRawDataToString() {
9 16
         ClassLoader classLoader = getClass().getClassLoader();
10
-        String result = IOUtils.toString(classLoader.getResourceAsStream("RawData.txt"));
17
+        String result = null;
18
+        try {
19
+            result = IOUtils.toString(classLoader.getResourceAsStream("RawData.txt"));
20
+        } catch (IOException e) {
21
+            e.printStackTrace();
22
+        }
11 23
         return result;
12 24
     }
13 25
 
14
-    public static void main(String[] args) throws Exception{
26
+    public static void main(String[] args) {
15 27
         String output = (new Main()).readRawDataToString();
16
-        System.out.println(output);
28
+        ItemParser parser = new ItemParser();
29
+        parser.printOutput(output);
30
+        }
31
+
17 32
         // TODO: parse the data in output into items, and display to console.
18 33
     }
19
-}
34
+

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

@@ -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:Milk;price:3.23;type:;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##"