浏览代码

Lab Submission

Haysel Santiago 6 年前
父节点
当前提交
2e4d94fc9d
共有 4 个文件被更改,包括 166 次插入21 次删除
  1. 12
    0
      pom.xml
  2. 57
    12
      src/main/java/io/zipcoder/ItemParser.java
  3. 93
    5
      src/main/java/io/zipcoder/Main.java
  4. 4
    4
      src/test/java/io/zipcoder/ItemParserTest.java

+ 12
- 0
pom.xml 查看文件

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

+ 57
- 12
src/main/java/io/zipcoder/ItemParser.java 查看文件

@@ -3,29 +3,74 @@ package io.zipcoder;
3 3
 import java.util.ArrayList;
4 4
 import java.util.Arrays;
5 5
 
6
+
6 7
 public class ItemParser {
7 8
 
9
+    public int errorCount = 0;
8 10
 
9
-    public ArrayList<String> parseRawDataIntoStringArray(String rawData){
11
+    public ArrayList<String> parseRawDataIntoStringArray(String rawData) {
10 12
         String stringPattern = "##";
11
-        ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawData);
13
+        ArrayList<String> response = splitStringWithRegexPattern(stringPattern, rawData);
12 14
         return response;
13 15
     }
14 16
 
15
-    public Item parseStringIntoItem(String rawItem) throws ItemParseException{
16
-        return null;
17
-    }
17
+    public Item parseStringIntoItem(String rawItem) throws ItemParseException {
18 18
 
19
-    public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){
20
-        String stringPattern = "[;|^]";
21
-        ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawItem);
22
-        return response;
19
+        ArrayList<String> items = findKeyValuePairsInRawItemData(rawItem);
20
+
21
+
22
+        String name = items.get(0);
23
+        String price = items.get(1);
24
+        String type = items.get(2);
25
+        String exp = items.get(3);
26
+
27
+        String nameRegex = "[n][a][m][e]:";
28
+        name = name.replaceAll(nameRegex, "");
29
+        name = fixString(name);
30
+
31
+        String priceRegex = "[p][r][i][c][e]:";
32
+        price = price.replaceAll(priceRegex, "");
33
+        double priceAsDouble = fixPrice(price);
34
+
35
+
36
+        String typeRegex = "[t][y][p][e]:";
37
+        type = type.replaceAll(typeRegex, "");
38
+        type = fixString(type);
39
+
40
+        String expRegex = "[e][x][p][i][r][a][t][i][o][n]:";
41
+        exp = exp.replaceAll(expRegex, "");
42
+        exp = fixString(exp);
43
+
44
+        return new Item(name, priceAsDouble, type, exp);
23 45
     }
24 46
 
25
-    private ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString){
26
-        return new ArrayList<String>(Arrays.asList(inputString.split(stringPattern)));
47
+
48
+    public String fixString(String item) {
49
+        if (item.equals("")) {
50
+            errorCount++;
51
+            return "Error";
52
+        }
53
+        if (item.startsWith("c")) {
54
+            item = item.replace("0", "o");
55
+        }
56
+        return item.substring(0, 1).toUpperCase() + item.substring(1);
57
+
27 58
     }
28 59
 
60
+    public double fixPrice(String price) {
61
+        if (price.equals("")) {
62
+            return 0.0;
63
+        }
64
+        return Double.parseDouble(price);
65
+    }
29 66
 
67
+    public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem) {
68
+        String stringPattern = "[^a-z0-9/.:]";
69
+        ArrayList<String> response = splitStringWithRegexPattern(stringPattern, rawItem.toLowerCase());
70
+        return response;
71
+    }
30 72
 
31
-}
73
+    private ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString) {
74
+        return new ArrayList<String>(Arrays.asList(inputString.split(stringPattern)));
75
+    }
76
+}

+ 93
- 5
src/main/java/io/zipcoder/Main.java 查看文件

@@ -2,18 +2,106 @@ 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.Iterator;
8
+import java.util.Map;
9
+
5 10
 
6 11
 public class Main {
7 12
 
8
-    public String readRawDataToString() throws Exception{
13
+    public String readRawDataToString() throws Exception {
9 14
         ClassLoader classLoader = getClass().getClassLoader();
10 15
         String result = IOUtils.toString(classLoader.getResourceAsStream("RawData.txt"));
11 16
         return result;
12 17
     }
13 18
 
14
-    public static void main(String[] args) throws Exception{
19
+    public static void main(String[] args) throws Exception {
15 20
         String output = (new Main()).readRawDataToString();
16
-        System.out.println(output);
17
-        // TODO: parse the data in output into items, and display to console.
21
+
22
+        ItemParser parser = new ItemParser();
23
+
24
+        ArrayList<String> dataStringArray = parser.parseRawDataIntoStringArray(output);
25
+
26
+        ArrayList<Item> items = new ArrayList<>();
27
+        for (String item : dataStringArray) {
28
+            items.add(parser.parseStringIntoItem(item));
29
+        }
30
+
31
+        int errorCounter = 0;
32
+        Integer milkCounter = 0;
33
+
34
+
35
+        Iterator<Item> iterator = items.iterator();
36
+        while (iterator.hasNext()) {
37
+            Item item = iterator.next();
38
+            if ("N/A".equals(item.getName()) || item.getPrice() == 0.0) {
39
+                errorCounter++;
40
+                iterator.remove();
41
+            }
42
+        }
43
+
44
+        HashMap<String, Integer> names = getNameMap(items);
45
+
46
+        for (Map.Entry<String, Integer> name : names.entrySet()) {
47
+            HashMap<String, HashMap<Double, Integer>> namePriceMap = getNamePriceMap(items, name.getKey());
48
+            System.out.println("==============================");
49
+            System.out.println("Name: " + name.getKey() + " Appears: " + name.getValue() + " times");
50
+            System.out.println("------------------------------");
51
+            for(Map.Entry<String, HashMap<Double, Integer>> namePrice : namePriceMap.entrySet()){
52
+                String key = namePrice.getKey();
53
+                for(Map.Entry<Double,Integer> priceMap : namePrice.getValue().entrySet()){
54
+                    System.out.println("Price: " + priceMap.getKey() + " Appears: " + priceMap.getValue());
55
+                }
56
+
57
+
58
+            }
59
+        }
60
+        System.out.println("------------------------------");
61
+        System.out.println("Errors: " + errorCounter);
62
+
63
+
64
+
65
+    }
66
+
67
+
68
+    public static HashMap<String, HashMap<Double, Integer>> getNamePriceMap(ArrayList<Item> items, String itemName) {
69
+        HashMap<String, HashMap<Double, Integer>> namePriceMap = new HashMap<>();
70
+        ArrayList <Item> nameList = new ArrayList<>();
71
+        for (Item item : items) {
72
+            if(item.getName().equals(itemName)){
73
+                nameList.add(item);
74
+            }
75
+        }
76
+        namePriceMap.put(itemName, getPriceMap(nameList));
77
+        return namePriceMap;
78
+    }
79
+
80
+    public static HashMap<Double, Integer> getPriceMap(ArrayList<Item> items) {
81
+        HashMap<Double, Integer> map = new HashMap<>();
82
+        for (Item item : items) {
83
+            if (map.containsKey(item.getPrice())) {
84
+                Integer newItemCount = map.get(item.getPrice());
85
+                newItemCount++;
86
+                map.put(item.getPrice(), newItemCount);
87
+            } else {
88
+                map.put(item.getPrice(), 1);
89
+            }
90
+        }
91
+        return map;
92
+    }
93
+
94
+    public static HashMap<String, Integer> getNameMap(ArrayList<Item> items) {
95
+        HashMap<String, Integer> map = new HashMap<>();
96
+        for (Item item : items) {
97
+            if (map.containsKey(item.getName())) {
98
+                Integer newItemCount = map.get(item.getName());
99
+                newItemCount++;
100
+                map.put(item.getName(), newItemCount);
101
+            } else {
102
+                map.put(item.getName(), 1);
103
+            }
104
+        }
105
+        return map;
18 106
     }
19
-}
107
+}

+ 4
- 4
src/test/java/io/zipcoder/ItemParserTest.java 查看文件

@@ -17,8 +17,8 @@ public class ItemParserTest {
17 17
     private String rawBrokenSingleItem =    "naMe:Milk;price:3.23;type:Food;expiration:1/25/2016##";
18 18
 
19 19
     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:2/25/2016##";
20
+            +"naME:BreaD;price:1.23;type:Food;expiration:1/02/2016##"
21
+            +"NAMe:BrEAD;price:1.23;type:Food;expiration:2/25/2016##";
22 22
     private ItemParser itemParser;
23 23
 
24 24
     @Before
@@ -36,7 +36,7 @@ public class ItemParserTest {
36 36
 
37 37
     @Test
38 38
     public void parseStringIntoItemTest() throws ItemParseException{
39
-        Item expected = new Item("milk", 3.23, "food","1/25/2016");
39
+        Item expected = new Item("Milk", 3.23, "Food","1/25/2016");
40 40
         Item actual = itemParser.parseStringIntoItem(rawSingleItem);
41 41
         assertEquals(expected.toString(), actual.toString());
42 42
     }
@@ -59,4 +59,4 @@ public class ItemParserTest {
59 59
         Integer actual = itemParser.findKeyValuePairsInRawItemData(rawSingleItemIrregularSeperatorSample).size();
60 60
         assertEquals(expected, actual);
61 61
     }
62
-}
62
+}