Bladeren bron

completed all tests. working on output.

Tommy Rogers 6 jaren geleden
bovenliggende
commit
a80adf6c31

+ 12
- 0
pom.xml Bestand weergeven

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

+ 8
- 0
src/main/java/io/zipcoder/ItemParseException.java Bestand weergeven

@@ -1,4 +1,12 @@
1 1
 package io.zipcoder;
2 2
 
3 3
 public class ItemParseException extends Exception {
4
+
5
+    public ItemParseException(){
6
+        super();
7
+    }
8
+
9
+
10
+
11
+
4 12
 }

+ 82
- 5
src/main/java/io/zipcoder/ItemParser.java Bestand weergeven

@@ -1,24 +1,101 @@
1 1
 package io.zipcoder;
2 2
 
3
-import java.util.ArrayList;
4
-import java.util.Arrays;
3
+import java.util.*;
5 4
 
6 5
 public class ItemParser {
7 6
 
7
+    //"naMe:Milk;price:3.23;type:Food;expiration:1/25/2016##"
8
+
9
+    public String lowerAllCasesCauseTariqCannotSpell(String rawData){
10
+
11
+        return null;
12
+    }
13
+
14
+
15
+    //    0 "naMe:Milk;price:3.23;type:Food;expiration:1/25/2016##"
16
+
17
+    //     1 +"naME:BreaD;price:1.23;type:Food;expiration:1/02/2016##"
18
+
19
+    //      2 +"NAMe:BrEAD;price:1.23;type:Food;expiration:2/25/2016##";
8 20
 
9 21
     public ArrayList<String> parseRawDataIntoStringArray(String rawData){
10 22
         String stringPattern = "##";
11 23
         ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawData);
24
+
12 25
         return response;
13 26
     }
14 27
 
15
-    public Item parseStringIntoItem(String rawItem) throws ItemParseException{
16
-        return null;
28
+    public Item parseStringIntoItem(String rawItems) throws ItemParseException {
29
+        String stringPattern = "[;|^|@|*|%|$|#|!|:]";
30
+        ArrayList<String> itemsPattern = splitStringWithRegexPattern(stringPattern , rawItems);
31
+        Item newItem = null;
32
+        Double d;
33
+        for(String s : itemsPattern)
34
+            if (!s.equals("")) {
35
+                d = Double.valueOf(itemsPattern.get(3));
36
+                newItem = new Item(toLowerCase(itemsPattern.get(1)), d, toLowerCase(itemsPattern.get(5)), toLowerCase(itemsPattern.get(7)));
37
+            }
38
+            else throw new ItemParseException();
39
+        return newItem;
17 40
     }
18 41
 
42
+    public String toLowerCase(String a) {
43
+        String s = "";
44
+        for (int i = 0; i < a.length(); i++) {
45
+            char aChar = a.charAt(i);
46
+            if (65 <= aChar && aChar <= 90) {
47
+                aChar = (char) ((aChar + 32));
48
+            }
49
+            s += aChar;
50
+        }
51
+        return s;
52
+    }
53
+
54
+    public Map<String, Map<Double, Integer>> collectAmounts(Item item) {
55
+        Map<String, Map<Double, Integer>> myMap = new HashMap<>();
56
+        int count;
57
+        String name = item.getName();
58
+        Double price = item.getPrice();
59
+        if (myMap.containsKey(name)) {
60
+            count = myMap.get(name).getOrDefault(price, 0);
61
+            count++;
62
+            myMap.get(name).put(price, count);
63
+        } else{ myMap.put(name, new HashMap<>());
64
+           myMap.get(name).put(price, 1);
65
+        }
66
+        return myMap;
67
+    }
68
+
69
+    public void printOutput(Map<String, Map<Double, Integer>> myMap){
70
+        Double doub = 0.0;
71
+        String key;
72
+        Integer count;
73
+        System.out.println(myMap.size());
74
+        //System.out.println(myMap.keySet());
75
+       // System.out.println(myMap.entrySet());
76
+//        for(String m : myMap.keySet()){
77
+//            key = m;
78
+//            Map map = myMap.get(key);
79
+//            Integer y = myMap.get(key).get(doub);
80
+//            System.out.println("Name: "  + key +  "    Seen: " + y);
81
+//            System.out.println("____________    ________________");
82
+//            System.out.println("____________    ________________");
83
+//            for(Object d : map.keySet())
84
+//            doub = (Double) d;
85
+//            count = (Integer) map.get(doub);
86
+//            System.out.println("Price: "+ doub +    "     Seen: " + count);
87
+//            System.out.println("____________    ________________");
88
+//        }
89
+    }
90
+
91
+
92
+
93
+
94
+
19 95
     public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){
20
-        String stringPattern = "[;|^]";
96
+        String stringPattern = "[;|^|@|*|%|$|#|!]";
21 97
         ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawItem);
98
+
22 99
         return response;
23 100
     }
24 101
 

+ 19
- 1
src/main/java/io/zipcoder/Main.java Bestand weergeven

@@ -2,6 +2,10 @@ 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
+import java.util.Map;
8
+
5 9
 
6 10
 public class Main {
7 11
 
@@ -12,8 +16,22 @@ public class Main {
12 16
     }
13 17
 
14 18
     public static void main(String[] args) throws Exception{
19
+        int count =0;
15 20
         String output = (new Main()).readRawDataToString();
16
-        System.out.println(output);
21
+        ItemParser itemParser = new ItemParser();
22
+        ArrayList<String> outputList = itemParser.parseRawDataIntoStringArray(output);
23
+        Map<String, Map<Double, Integer>> myMap = null;
24
+        for(String s: outputList) {
25
+            try {
26
+                Item item = itemParser.parseStringIntoItem(s);
27
+                myMap = itemParser.collectAmounts(item);
28
+            }
29
+            catch (ItemParseException e){
30
+                count++;
31
+            }
32
+
33
+        }
34
+        itemParser.printOutput(myMap);
17 35
         // TODO: parse the data in output into items, and display to console.
18 36
     }
19 37
 }

+ 3
- 2
src/test/java/io/zipcoder/ItemParserTest.java Bestand weergeven

@@ -4,6 +4,7 @@ import org.junit.Assert;
4 4
 import org.junit.Before;
5 5
 import org.junit.Test;
6 6
 
7
+import java.text.ParseException;
7 8
 import java.util.ArrayList;
8 9
 
9 10
 import static org.junit.Assert.*;
@@ -14,7 +15,7 @@ public class ItemParserTest {
14 15
 
15 16
     private String rawSingleItemIrregularSeperatorSample = "naMe:MiLK;price:3.23;type:Food^expiration:1/11/2016##";
16 17
 
17
-    private String rawBrokenSingleItem =    "naMe:Milk;price:3.23;type:Food;expiration:1/25/2016##";
18
+    private String rawBrokenSingleItem =    "naMe:;price:3.23;type:Food;expiration:1/25/2016##";
18 19
 
19 20
     private String rawMultipleItems = "naMe:Milk;price:3.23;type:Food;expiration:1/25/2016##"
20 21
                                       +"naME:BreaD;price:1.23;type:Food;expiration:1/02/2016##"
@@ -35,7 +36,7 @@ public class ItemParserTest {
35 36
     }
36 37
 
37 38
     @Test
38
-    public void parseStringIntoItemTest() throws ItemParseException{
39
+    public void parseStringIntoItemTest() throws ItemParseException, ParseException {
39 40
         Item expected = new Item("milk", 3.23, "food","1/25/2016");
40 41
         Item actual = itemParser.parseStringIntoItem(rawSingleItem);
41 42
         assertEquals(expected.toString(), actual.toString());