Ver código fonte

working on output

Trinh Tong 5 anos atrás
pai
commit
7af6cee869

+ 12
- 0
pom.xml Ver arquivo

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

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

+ 104
- 9
src/main/java/io/zipcoder/ItemParser.java Ver arquivo

@@ -1,31 +1,126 @@
1 1
 package io.zipcoder;
2 2
 
3
-import java.util.ArrayList;
4
-import java.util.Arrays;
3
+
4
+import com.sun.tools.javac.jvm.Items;
5
+
6
+import java.io.*;
7
+import java.util.*;
8
+import java.util.regex.Matcher;
9
+import java.util.regex.Pattern;
10
+
11
+import static io.zipcoder.LeetUtils.capitalizeMe;
12
+import static io.zipcoder.LeetUtils.fullManipulate;
5 13
 
6 14
 public class ItemParser {
7 15
 
16
+    public boolean checkForValue(String itemString) {
17
+        Pattern pattern = Pattern.compile("(:;)|(:%)|(:\\*)|(:@)|(:!)");
18
+        Matcher m = pattern.matcher(itemString);
19
+
20
+        return m.find();
21
+    }
8 22
 
9 23
     public ArrayList<String> parseRawDataIntoStringArray(String rawData){
10 24
         String stringPattern = "##";
11
-        ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawData);
12
-        return response;
25
+        return splitStringWithRegexPattern(stringPattern , rawData);
13 26
     }
14 27
 
15 28
     public Item parseStringIntoItem(String rawItem) throws ItemParseException{
16
-        return null;
29
+
30
+        if (checkForValue(rawItem)) {
31
+            throw new ItemParseException();
32
+        } else {
33
+            String[] values = allValues(rawItem.toLowerCase()).split(",");
34
+            return new Item(fullManipulate(values[0]), Double.parseDouble(values[1]), fullManipulate(values[2]), values[3]);
35
+        }
36
+    }
37
+
38
+    public String allValues(String rawItem) {
39
+        StringBuilder sb = new StringBuilder();
40
+        
41
+        ArrayList<String> pairs = findKeyValuePairsInRawItemData(rawItem);
42
+        for (String s: pairs ) {
43
+            sb.append((sb.toString().equals("")) ? returnValue(s) : "," + returnValue(s));
44
+        }
45
+        return sb.toString();
46
+    }
47
+
48
+    public String returnValue (String pair) {
49
+        return pair.substring(pair.lastIndexOf(":") + 1);
17 50
     }
18 51
 
19 52
     public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){
20
-        String stringPattern = "[;|^]";
21
-        ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawItem);
22
-        return response;
53
+        String stringPattern = "[;^%*@!]";
54
+        return splitStringWithRegexPattern(stringPattern , rawItem);
23 55
     }
24 56
 
25 57
     private ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString){
26
-        return new ArrayList<String>(Arrays.asList(inputString.split(stringPattern)));
58
+        return new ArrayList<>(Arrays.asList(inputString.split(stringPattern)));
27 59
     }
28 60
 
61
+    public static void main(String[] args) throws FileNotFoundException, ItemParseException {
62
+        ItemParser ip = new ItemParser();
63
+        String rawMultipleItems = "naMe:C00kies;price:3.23;type:Food;expiration:1/25/2016##"
64
+                +"naME:;price:1.23;type:Food;expiration:1/02/2016##"
65
+                +"NAMe:BrEAD;price:1.23;type:Food;expiration:2/25/2016##"
66
+                +"NAMe:BrEAD;price:1.23;type:Food;expiration:2/25/2016##";
67
+
68
+        ip.printToFile("list.txt" ,rawMultipleItems);
69
+    }
70
+
71
+
29 72
 
73
+    public void printToFile(String fileName, String rawItemData) throws FileNotFoundException, ItemParseException {
74
+        // Output to file
75
+        ArrayList<String> allItemsRaw = parseRawDataIntoStringArray(rawItemData);
76
+        ArrayList<Item> allItemObjects = new ArrayList<>();
77
+        int errors = 0;
78
+
79
+        for (String s:allItemsRaw) {
80
+            if (checkForValue(s)) {
81
+                errors++;
82
+            } else {
83
+                allItemObjects.add(parseStringIntoItem(s));
84
+            }
85
+        }
86
+
87
+        HashMap<String, SameItems> itemCounters = sameItemCounter(allItemObjects);
88
+
89
+        PrintStream file = new PrintStream(new File(fileName));
90
+        PrintStream console = System.out;
91
+
92
+        System.setOut(file);
93
+
94
+        StringBuilder resultsOutput = new StringBuilder();
95
+
96
+        resultsOutput.append("Tariq's Messed up Grocery List...\n\n");
97
+
98
+        for (SameItems s : itemCounters.values()) {
99
+            resultsOutput.append(String.format("name: %8s seen: %d", capitalizeMe(s.getName()), s.getCount()));
100
+//            resultsOutput.append(String.format("%s %s", item.getName(), item.getPrice()));
101
+        }
102
+        System.out.println(resultsOutput.toString());
103
+    }
104
+
105
+    public HashMap<String, SameItems> sameItemCounter(ArrayList<Item> allItems) {
106
+
107
+        HashMap<String, SameItems> itemCounters = new HashMap<>();
108
+        for (Item i: allItems) {
109
+            SameItems temp;
110
+            if (!itemCounters.keySet().contains(i.getName())) {
111
+                temp = new SameItems(i.getName());
112
+                temp.getPrices().addNewPrice(i.getPrice());
113
+                itemCounters.put(i.getName(), temp);
114
+            } else {
115
+
116
+                temp = itemCounters.get(i.getName());
117
+                temp.getPrices().addNewPrice(i.getPrice());
118
+                temp.setCount(temp.getCount()+1);
119
+                itemCounters.put(i.getName(),temp);
120
+            }
121
+        }
122
+
123
+        return itemCounters;
124
+    }
30 125
 
31 126
 }

+ 53
- 0
src/main/java/io/zipcoder/LeetUtils.java Ver arquivo

@@ -0,0 +1,53 @@
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
+}

+ 21
- 0
src/main/java/io/zipcoder/Prices.java Ver arquivo

@@ -0,0 +1,21 @@
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
+}

+ 34
- 0
src/main/java/io/zipcoder/SameItems.java Ver arquivo

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

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