Selaa lähdekoodia

completed all tests. working on output.

Tommy Rogers 6 vuotta sitten
vanhempi
commit
a80adf6c31

+ 12
- 0
pom.xml Näytä tiedosto

7
     <groupId>io.zipcoder</groupId>
7
     <groupId>io.zipcoder</groupId>
8
     <artifactId>PainfullAfternoon</artifactId>
8
     <artifactId>PainfullAfternoon</artifactId>
9
     <version>1.0-SNAPSHOT</version>
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
     <dependencies>
23
     <dependencies>
12
         <dependency>
24
         <dependency>

+ 8
- 0
src/main/java/io/zipcoder/ItemParseException.java Näytä tiedosto

1
 package io.zipcoder;
1
 package io.zipcoder;
2
 
2
 
3
 public class ItemParseException extends Exception {
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 Näytä tiedosto

1
 package io.zipcoder;
1
 package io.zipcoder;
2
 
2
 
3
-import java.util.ArrayList;
4
-import java.util.Arrays;
3
+import java.util.*;
5
 
4
 
6
 public class ItemParser {
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
     public ArrayList<String> parseRawDataIntoStringArray(String rawData){
21
     public ArrayList<String> parseRawDataIntoStringArray(String rawData){
10
         String stringPattern = "##";
22
         String stringPattern = "##";
11
         ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawData);
23
         ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawData);
24
+
12
         return response;
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
     public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){
95
     public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){
20
-        String stringPattern = "[;|^]";
96
+        String stringPattern = "[;|^|@|*|%|$|#|!]";
21
         ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawItem);
97
         ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawItem);
98
+
22
         return response;
99
         return response;
23
     }
100
     }
24
 
101
 

+ 19
- 1
src/main/java/io/zipcoder/Main.java Näytä tiedosto

2
 
2
 
3
 import org.apache.commons.io.IOUtils;
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
 public class Main {
10
 public class Main {
7
 
11
 
12
     }
16
     }
13
 
17
 
14
     public static void main(String[] args) throws Exception{
18
     public static void main(String[] args) throws Exception{
19
+        int count =0;
15
         String output = (new Main()).readRawDataToString();
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
         // TODO: parse the data in output into items, and display to console.
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 Näytä tiedosto

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