NedRedmond il y a 5 ans
Parent
révision
24807fa49d

+ 19
- 0
pom.xml Voir le fichier

@@ -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>
@@ -19,5 +31,12 @@
19 31
             <artifactId>org.apache.commons.io</artifactId>
20 32
             <version>2.4</version>
21 33
         </dependency>
34
+        <!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
35
+        <dependency>
36
+            <groupId>com.google.guava</groupId>
37
+            <artifactId>guava</artifactId>
38
+            <version>19.0</version>
39
+        </dependency>
40
+
22 41
     </dependencies>
23 42
 </project>

+ 19
- 0
src/main/java/io/zipcoder/Item.java Voir le fichier

@@ -1,11 +1,29 @@
1 1
 package io.zipcoder;
2 2
 
3
+import java.lang.reflect.Field;
4
+import java.util.ArrayList;
5
+import java.util.List;
6
+
3 7
 public class Item {
4 8
     private String name;
5 9
     private Double price;
6 10
     private String type;
7 11
     private String expiration;
8 12
 
13
+    Field[] fields = Item.class.getFields();
14
+
15
+    public Item() {
16
+
17
+    }
18
+
19
+    public ArrayList<String> getFieldNames() {
20
+        ArrayList<String> fieldNames = new ArrayList<>();
21
+        for (Field field : fields) {
22
+            fieldNames.add(field.getName());
23
+        }
24
+        return fieldNames;
25
+    }
26
+
9 27
     /**
10 28
      * Item should not be created unless you have all of the elements, which is why you are forcing
11 29
      * it to be set in the constructor. In ItemParser, if you do not find all the elements of a Item,
@@ -45,4 +63,5 @@ public class Item {
45 63
     public String toString(){
46 64
         return "name:" + name + " price:" + price + " type:" + type + " expiration:" + expiration;
47 65
     }
66
+
48 67
 }

+ 1
- 2
src/main/java/io/zipcoder/ItemParseException.java Voir le fichier

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

+ 95
- 8
src/main/java/io/zipcoder/ItemParser.java Voir le fichier

@@ -1,31 +1,118 @@
1 1
 package io.zipcoder;
2 2
 
3
-import java.util.ArrayList;
4
-import java.util.Arrays;
3
+import java.util.*;
4
+import java.util.regex.Matcher;
5
+import java.util.regex.Pattern;
6
+
7
+import com.google.common.base.CharMatcher;
8
+import com.google.common.base.Splitter;
9
+
10
+import static java.util.stream.Collectors.groupingBy;
11
+
5 12
 
6 13
 public class ItemParser {
7 14
 
15
+    int errorCounter = 0;
16
+
17
+    public int getErrorCounter() {
18
+        return errorCounter;
19
+    }
20
+
21
+    public Map<String, Map<Double, List<Item>>> getItemsMap(ArrayList<Item> items) {
22
+        return items
23
+                .stream()
24
+                .collect(
25
+                        groupingBy(
26
+                                Item::getName,
27
+                                groupingBy(
28
+                                        Item::getPrice
29
+                                )
30
+                        )
31
+                );
32
+    }
8 33
 
9 34
     public ArrayList<String> parseRawDataIntoStringArray(String rawData){
35
+        String fixedString = fixString(rawData);
10 36
         String stringPattern = "##";
11
-        ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawData);
37
+        ArrayList<String> response = splitStringWithRegexPattern(stringPattern , fixedString);
12 38
         return response;
13 39
     }
14 40
 
15
-    public Item parseStringIntoItem(String rawItem) throws ItemParseException{
16
-        return null;
41
+    public Item parseStringIntoItem(String rawItem) {
42
+//        try {
43
+//            validateItem(rawItem);
44
+//        } catch (ItemParseException e) {
45
+//            //NO OP
46
+//        }
47
+        Map<String, String> itemMap = null;
48
+        try {
49
+            itemMap = itemStringToHashMap(rawItem);
50
+        } catch (ItemParseException e) {
51
+            errorCounter++;
52
+            return null;
53
+        }
54
+        return createItem(itemMap);
55
+    }
56
+
57
+    private Item createItem(Map<String, String> itemMap) {
58
+        return new Item(itemMap.get("name"), Double.parseDouble(itemMap.get("price")), itemMap.get("type"), itemMap.get("expiration"));
59
+    }
60
+
61
+//    public boolean validateItem(String rawItem) throws ItemParseException{
62
+//        boolean isValid = false;
63
+//        if (rawItem.contains("[:\\W]")) {
64
+//            throw new ItemParseException();
65
+//        } else isValid = true;
66
+//        return isValid;
67
+//    }
68
+
69
+    public String toLowerCase(String rawString) {
70
+        Matcher m = Pattern.compile("\\w").matcher(rawString);
71
+        StringBuilder sb = new StringBuilder();
72
+
73
+        int last = 0;
74
+        while (m.find()) {
75
+            sb.append(rawString, last, m.start());
76
+            sb.append(m.group(0).toLowerCase());
77
+            last = m.end();
78
+        }
79
+        sb.append(rawString.substring(last));
80
+
81
+        return sb.toString();
82
+    }
83
+
84
+    public String fixCo0kies(String rawString) {
85
+        Matcher m = Pattern.compile("co0kies").matcher(rawString);
86
+        String fixedString = m.replaceAll("cookies");
87
+        return fixedString;
88
+    }
89
+
90
+    public String fixString(String rawString) {
91
+        String lowerCased = toLowerCase(rawString);
92
+        String fixedString = fixCo0kies(lowerCased);
93
+        return fixedString;
17 94
     }
18 95
 
19 96
     public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){
20
-        String stringPattern = "[;|^]";
97
+        String stringPattern = "[;|\\^]";
21 98
         ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawItem);
99
+        //TODO
22 100
         return response;
23 101
     }
24 102
 
25 103
     private ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString){
26
-        return new ArrayList<String>(Arrays.asList(inputString.split(stringPattern)));
104
+        return new ArrayList<>(Arrays.asList(inputString.split(stringPattern)));
27 105
     }
28 106
 
29
-
107
+    private Map<String, String> itemStringToHashMap(String rawItem) throws ItemParseException {
108
+        CharMatcher matcher = CharMatcher.anyOf(";^%*!@");
109
+        Map<String,String> itemMap = Splitter.on(matcher).withKeyValueSeparator(':').split(rawItem);
110
+        for (Map.Entry<String,String> entry : itemMap.entrySet()) {
111
+            if ( entry.getValue().equals("")) {
112
+                throw new ItemParseException();
113
+            }
114
+        }
115
+        return itemMap;
116
+    }
30 117
 
31 118
 }

+ 53
- 0
src/main/java/io/zipcoder/ItemPrinter.java Voir le fichier

@@ -0,0 +1,53 @@
1
+package io.zipcoder;
2
+
3
+import java.util.ArrayList;
4
+import java.util.List;
5
+import java.util.Map;
6
+
7
+public class ItemPrinter {
8
+
9
+    ItemParser parser = new ItemParser();
10
+    ArrayList<Item> itemsArray = new ArrayList<>();
11
+
12
+    public String prettyPrint(String rawData) {
13
+        ArrayList<String> itemStrings = parser.parseRawDataIntoStringArray(rawData);
14
+        for (String s : itemStrings) {
15
+            Item item = parser.parseStringIntoItem(s);
16
+            if (item != null) {
17
+                itemsArray.add(item);
18
+            }
19
+        }
20
+
21
+        Map<String, Map<Double, List<Item>>> itemsMap = parser.getItemsMap(itemsArray);
22
+
23
+        StringBuilder output = new StringBuilder();
24
+        itemsMap.forEach(
25
+                (str,value) -> {
26
+                    int itemCount = getItemCount(value);
27
+                    output.append(
28
+                            String.format("name: %7s     seen: %d times", str, itemCount)
29
+                            + ("\n") + ("=============     =============") + ("\n")
30
+                    );
31
+                    for (Map.Entry<Double, List<Item>> entry : value.entrySet()) {
32
+                        output.append(
33
+                                String.format("price: $%5.2f     seen: %d times", entry.getKey(), entry.getValue().size())
34
+                                        + ("\n") + ("-------------     -------------") + ("\n")
35
+                        );
36
+                    }
37
+                    output.append("\n");
38
+                }
39
+                );
40
+        return output.toString();
41
+    }
42
+
43
+    private int getItemCount(Map<Double, List<Item>> value) {
44
+        int itemCount = 0;
45
+        for (Map.Entry<Double, List<Item>> entry : value.entrySet()) {
46
+            Double price = entry.getKey();
47
+            List<Item> itemArray = entry.getValue();
48
+            itemCount += itemArray.size();
49
+        }
50
+        return itemCount;
51
+    }
52
+
53
+}

+ 3
- 2
src/main/java/io/zipcoder/Main.java Voir le fichier

@@ -13,7 +13,8 @@ public class Main {
13 13
 
14 14
     public static void main(String[] args) throws Exception{
15 15
         String output = (new Main()).readRawDataToString();
16
-        System.out.println(output);
17
-        // TODO: parse the data in output into items, and display to console.
16
+        ItemPrinter printer = new ItemPrinter();
17
+        String improvedOutput = printer.prettyPrint(output);
18
+        System.out.println(improvedOutput);
18 19
     }
19 20
 }

+ 0
- 0
src/main/resources/prettyItems.txt Voir le fichier


+ 1
- 1
src/test/java/io/zipcoder/ItemParserTest.java Voir le fichier

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