#4 trtong - There's so many hashmaps..

Open
trtong wants to merge 5 commits from trtong/JerkSON-Parser:master into master

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

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

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

+ 48
- 13
src/main/java/io/zipcoder/ItemParser.java View File

@@ -1,31 +1,66 @@
1 1
 package io.zipcoder;
2 2
 
3
-import java.util.ArrayList;
4
-import java.util.Arrays;
3
+import java.io.*;
4
+import java.util.*;
5
+import java.util.regex.Matcher;
6
+import java.util.regex.Pattern;
7
+
8
+import static io.zipcoder.LeetUtils.*;
5 9
 
6 10
 public class ItemParser {
7 11
 
12
+    public static boolean checkForValue(String itemString) {
13
+        Pattern pattern = Pattern.compile("(:;)|(:%)|(:\\*)|(:@)|(:!)");
14
+        Matcher m = pattern.matcher(itemString);
15
+
16
+        return m.find();
17
+    }
8 18
 
9
-    public ArrayList<String> parseRawDataIntoStringArray(String rawData){
19
+    public static ArrayList<String> parseRawDataIntoStringArray(String rawData){
10 20
         String stringPattern = "##";
11
-        ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawData);
12
-        return response;
21
+        return splitStringWithRegexPattern(stringPattern , rawData);
13 22
     }
14 23
 
15
-    public Item parseStringIntoItem(String rawItem) throws ItemParseException{
16
-        return null;
24
+    public static Item parseStringIntoItem(String rawItem) throws ItemParseException{
25
+
26
+        if (checkForValue(rawItem)) {
27
+            throw new ItemParseException();
28
+        } else {
29
+            String[] values = allValues(rawItem.toLowerCase()).split(",");
30
+            return new Item(fullManipulate(values[0]), Double.parseDouble(values[1]), fullManipulate(values[2]), values[3]);
31
+        }
17 32
     }
18 33
 
19
-    public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){
20
-        String stringPattern = "[;|^]";
21
-        ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawItem);
22
-        return response;
34
+    public static String allValues(String rawItem) {
35
+        StringBuilder sb = new StringBuilder();
36
+        
37
+        ArrayList<String> pairs = findKeyValuePairsInRawItemData(rawItem);
38
+        for (String s: pairs ) {
39
+            sb.append((sb.toString().equals("")) ? returnValue(s) : "," + returnValue(s));
40
+        }
41
+        return sb.toString();
23 42
     }
24 43
 
25
-    private ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString){
26
-        return new ArrayList<String>(Arrays.asList(inputString.split(stringPattern)));
44
+    public static String returnValue (String pair) {
45
+        return pair.substring(pair.lastIndexOf(":") + 1);
27 46
     }
28 47
 
48
+    public static ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){
49
+        String stringPattern = "[;^%*@!]";
50
+        return splitStringWithRegexPattern(stringPattern , rawItem);
51
+    }
29 52
 
53
+    private static ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString){
54
+        return new ArrayList<>(Arrays.asList(inputString.split(stringPattern)));
55
+    }
56
+
57
+    public static void printToFile(String fileName, String rawItemData) throws FileNotFoundException, ItemParseException {
30 58
 
59
+        OutPutObject opo = new OutPutObject(parseRawDataIntoStringArray(rawItemData));
60
+        PrintStream file = new PrintStream(new File(fileName));
61
+        PrintStream console = System.out;
62
+
63
+        System.setOut(file);
64
+        System.out.println(opo.outputString());
65
+    }
31 66
 }

+ 77
- 0
src/main/java/io/zipcoder/LeetUtils.java View File

@@ -0,0 +1,77 @@
1
+package io.zipcoder;
2
+
3
+import java.util.stream.Collectors;
4
+
5
+public class LeetUtils {
6
+
7
+    public static Character numToLetter(Character c) {
8
+        switch (c) {
9
+            case '0':
10
+                return 'o';
11
+            case '1':
12
+                return 'l';
13
+            case '3':
14
+                return 'e';
15
+            case '4':
16
+                return 'a';
17
+            case '5':
18
+                return 's';
19
+            case '7':
20
+                return 't';
21
+            default:
22
+                return c;
23
+
24
+        }
25
+    }
26
+
27
+    public static String capitalizeMe(String s) {
28
+        return Character.toUpperCase((s.charAt(0))) + s.substring(1);
29
+    }
30
+
31
+    public static String l33TToString(String cookies) {
32
+        StringBuilder letterString = new StringBuilder();
33
+
34
+        for (int i = 0; i < cookies.length(); i++) {
35
+            letterString.append(LeetUtils.numToLetter(cookies.charAt(i)));
36
+        }
37
+        return letterString.toString();
38
+    }
39
+
40
+    public static String sameCase(String s) {
41
+        StringBuilder result =  new StringBuilder(s);
42
+        for (int i = 0; i < s.length(); i++) {
43
+            char ch = s.charAt(i);
44
+            result.setCharAt(i, ch >= 'A' && ch <= 'Z' ? (char) (ch + 'a' - 'A') : ch);
45
+        }
46
+
47
+        return result.toString();
48
+    }
49
+
50
+    public static String fullManipulate(String s) {
51
+        return sameCase(l33TToString(s));
52
+    }
53
+
54
+    public static String stringFormatName(String name, int count) {
55
+        String time = (count > 1) ? "times" : "time";
56
+        return (String.format("name: %7s%6sseen: %d " + time, capitalizeMe(name), "", count));
57
+    }
58
+
59
+    public static String stringFormatPrice(Double price, int count, String label1, String label2) {
60
+        String time = (count > 1) ? "times" : "time";
61
+        return String.format("%7s%6.2f%5s%7s%d " + time + "\n",label1 + ": ", price,"",label2 + ": ", count);
62
+    }
63
+
64
+    public static String errorLine(int count) {
65
+        String time = (count > 1) ? "times" : "time";
66
+        return (String.format("%s%19s%2d " + time, "Errors", "seen: ", count));
67
+    }
68
+
69
+    public static String doubleLine() {
70
+        return (String.format("\n%8s%6s%8s\n", "=============", "", "============="));
71
+    }
72
+
73
+    public static String singleLine() {
74
+        return (String.format("%8s%6s%8s\n\n", "-------------", "", "-------------"));
75
+    }
76
+
77
+}

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

@@ -14,6 +14,6 @@ public class Main {
14 14
     public static void main(String[] args) throws Exception{
15 15
         String output = (new Main()).readRawDataToString();
16 16
         System.out.println(output);
17
-        // TODO: parse the data in output into items, and display to console.
17
+        ItemParser.printToFile("tariqsShoppingList.txt", output);
18 18
     }
19 19
 }

+ 89
- 0
src/main/java/io/zipcoder/OutPutObject.java View File

@@ -0,0 +1,89 @@
1
+package io.zipcoder;
2
+
3
+import java.util.ArrayList;
4
+import java.util.HashMap;
5
+
6
+import static io.zipcoder.ItemParser.checkForValue;
7
+import static io.zipcoder.ItemParser.parseStringIntoItem;
8
+import static io.zipcoder.LeetUtils.*;
9
+import static io.zipcoder.LeetUtils.errorLine;
10
+
11
+public class OutPutObject {
12
+    private int errorCount;
13
+    private HashMap<String, SameItems> allItemCounts;
14
+    private ArrayList<String> rawInputArray;
15
+    private ArrayList<Item> allItems;
16
+
17
+    public OutPutObject(ArrayList<String> rawInputArray) throws ItemParseException {
18
+        this.rawInputArray = rawInputArray;
19
+        this.allItems = new ArrayList<>();
20
+        errorCount = 0;
21
+
22
+        errorsAndCounts();
23
+        sameItemCounter(this.allItems);
24
+        allItemCounts = sameItemCounter(this.allItems);
25
+    }
26
+
27
+    public int getErrorCount() {
28
+        return errorCount;
29
+    }
30
+
31
+    public HashMap<String, SameItems> getAllItemCounts() {
32
+        return allItemCounts;
33
+    }
34
+
35
+    private void addErrorCount() {
36
+        this.errorCount = getErrorCount() + 1;
37
+    }
38
+
39
+    private void errorsAndCounts() throws ItemParseException {
40
+        for (String s:this.rawInputArray) {
41
+            if (checkForValue(s)) {
42
+                addErrorCount();
43
+            } else {
44
+                allItems.add(parseStringIntoItem(s));
45
+            }
46
+        }
47
+    }
48
+
49
+    private HashMap<String, SameItems> sameItemCounter(ArrayList<Item> allItems) {
50
+
51
+        HashMap<String, SameItems> itemCounters = new HashMap<>();
52
+        for (Item i: allItems) {
53
+            SameItems temp;
54
+            if (!itemCounters.keySet().contains(i.getName())) {
55
+                temp = new SameItems(i.getName());
56
+                temp.getPrices().addNewPrice(i.getPrice());
57
+                itemCounters.put(i.getName(), temp);
58
+            } else {
59
+                temp = itemCounters.get(i.getName());
60
+                if (!temp.getPrices().getPrices().keySet().contains(i.getPrice())) {
61
+                    temp.getPrices().addNewPrice(i.getPrice());
62
+                } else {
63
+                    temp.getPrices().IncrementPriceCounter(i.getPrice());
64
+                }
65
+                temp.setCount(temp.getCount() + 1);
66
+                itemCounters.put(i.getName(),temp);
67
+            }
68
+        }
69
+        return itemCounters;
70
+    }
71
+
72
+    public String outputString() {
73
+        StringBuilder resultsOutput = new StringBuilder();
74
+        resultsOutput.append("Tariq's Messed up Grocery List...\n\n");
75
+
76
+        for (SameItems s : this.getAllItemCounts().values()) {
77
+            resultsOutput.append(stringFormatName(s.getName(), s.getCount()));
78
+            resultsOutput.append(doubleLine());
79
+
80
+            for (Double d:s.getPrices().getPrices().keySet()) {
81
+                resultsOutput.append(stringFormatPrice(d, s.getPrices().getCount(d), "Price", "seen"));
82
+                resultsOutput.append(singleLine());
83
+            }
84
+        }
85
+
86
+        resultsOutput.append(errorLine(this.getErrorCount()));
87
+        return resultsOutput.toString();
88
+    }
89
+}

+ 26
- 0
src/main/java/io/zipcoder/Prices.java View File

@@ -0,0 +1,26 @@
1
+package io.zipcoder;
2
+
3
+import java.util.HashMap;
4
+
5
+public class Prices {
6
+    private HashMap<Double, Integer > prices = new HashMap<>();
7
+
8
+    public void IncrementPriceCounter(Double price) {
9
+        prices.put(price, prices.get(price) + 1);
10
+    }
11
+
12
+    public void addNewPrice(Double price) {
13
+        prices.put(price, 1);
14
+    }
15
+
16
+    public Integer getCount(Double price) {
17
+        return prices.get(price);
18
+    }
19
+
20
+
21
+    public HashMap<Double, Integer> getPrices() {
22
+        return prices;
23
+    }
24
+
25
+
26
+}

+ 34
- 0
src/main/java/io/zipcoder/SameItems.java View File

@@ -0,0 +1,34 @@
1
+package io.zipcoder;
2
+
3
+public class SameItems {
4
+    private String name;
5
+    private int count;
6
+    private Prices prices;
7
+
8
+    public SameItems(String name) {
9
+        this.name = name;
10
+        count = 1;
11
+        prices = new Prices();
12
+    }
13
+
14
+
15
+    public String getName() {
16
+        return name;
17
+    }
18
+
19
+    public int getCount() {
20
+        return count;
21
+    }
22
+
23
+    public void setCount(int count) {
24
+        this.count = count;
25
+    }
26
+
27
+    public Prices getPrices() {
28
+        return prices;
29
+    }
30
+
31
+    public void setPrices(Prices prices) {
32
+        this.prices = prices;
33
+    }
34
+}

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

@@ -14,7 +14,9 @@ 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
+    // This string isn't broken!
18
+//    private String rawBrokenSingleItem =    "naMe:Milk;price:3.23;type:Food;expiration:1/25/2016##";
19
+    private String rawBrokenSingleItem =    "naMe:;price:3.23;type:Food;expiration:1/25/2016##";
18 20
 
19 21
     private String rawMultipleItems = "naMe:Milk;price:3.23;type:Food;expiration:1/25/2016##"
20 22
                                       +"naME:BreaD;price:1.23;type:Food;expiration:1/02/2016##"
@@ -31,13 +33,14 @@ public class ItemParserTest {
31 33
         Integer expectedArraySize = 3;
32 34
         ArrayList<String> items = itemParser.parseRawDataIntoStringArray(rawMultipleItems);
33 35
         Integer actualArraySize = items.size();
36
+//        items.forEach(System.out::println);
34 37
         assertEquals(expectedArraySize, actualArraySize);
35 38
     }
36 39
 
37 40
     @Test
38 41
     public void parseStringIntoItemTest() throws ItemParseException{
39 42
         Item expected = new Item("milk", 3.23, "food","1/25/2016");
40
-        Item actual = itemParser.parseStringIntoItem(rawSingleItem);
43
+        Item actual = itemParser.parseStringIntoItem(itemParser.parseRawDataIntoStringArray(rawSingleItem).get(0));
41 44
         assertEquals(expected.toString(), actual.toString());
42 45
     }
43 46