Eric Cordell пре 6 година
родитељ
комит
4809d82a80
3 измењених фајлова са 133 додато и 2 уклоњено
  1. 12
    0
      pom.xml
  2. 19
    0
      src/main/java/io/zipcoder/ItemParser.java
  3. 102
    2
      src/main/java/io/zipcoder/Main.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>

+ 19
- 0
src/main/java/io/zipcoder/ItemParser.java Прегледај датотеку

@@ -9,6 +9,7 @@ import java.util.regex.Pattern;
9 9
 
10 10
 public class ItemParser {
11 11
 
12
+    public int errorCount = 0;
12 13
 
13 14
     // 1
14 15
     private ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString) {
@@ -77,4 +78,22 @@ public class ItemParser {
77 78
         else throw new ItemParseException();
78 79
     }
79 80
 
81
+    public String fixString(String item) {
82
+        if (item.equals("")) {
83
+            errorCount++;
84
+            return "Error";
85
+        }
86
+        if (item.startsWith("c")) {
87
+            item = item.replace("0", "o");
88
+        }
89
+        return item.substring(0, 1).toUpperCase() + item.substring(1);
90
+    }
91
+
92
+    public double fixPrice(String price) {
93
+        if (price.equals("")) {
94
+            return 0.0;
95
+        }
96
+        return Double.parseDouble(price);
97
+    }
98
+
80 99
 }

+ 102
- 2
src/main/java/io/zipcoder/Main.java Прегледај датотеку

@@ -2,18 +2,118 @@ 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 21
         System.out.println(output);
17 22
         // TODO: parse the data in output into items, and display to console.
23
+
24
+        ItemParser itemParser = new ItemParser();
25
+
26
+        ArrayList<String> itemList = itemParser.parseRawDataIntoStringArray(output);
27
+
28
+        ArrayList<Item> items = new ArrayList<Item>();
29
+        for (String item : itemList) {
30
+            items.add(itemParser.parseStringIntoItem(item));
31
+        }
32
+
33
+        int errorCounter = 0;
34
+        Integer milkCounter = 0;
35
+
36
+        Iterator<Item> iterator = items.iterator();
37
+        while (iterator.hasNext()) {
38
+            Item item = iterator.next();
39
+            if ("N/A".equals(item.getName()) || item.getPrice() == 0.0) {
40
+                errorCounter++;
41
+                iterator.remove();
42
+            }
43
+        }
44
+
45
+
46
+        HashMap<String, Integer> names = getNameMap(items);
47
+
48
+        for (Map.Entry<String, Integer> name : names.entrySet()) {
49
+            HashMap<String, HashMap<Double, Integer>> namePriceMap = getNamePriceMap(items, name.getKey());
50
+
51
+            System.out.println("==============================");
52
+            System.out.println("Name: " + name.getKey() + " Appears: " + name.getValue() + " times");
53
+            System.out.println("------------------------------");
54
+
55
+            for (Map.Entry<String, HashMap<Double, Integer>> namePrice : namePriceMap.entrySet()) {
56
+                String key = namePrice.getKey();
57
+                for (Map.Entry<Double, Integer> priceMap : namePrice.getValue().entrySet()) {
58
+                    System.out.println("Price: " + priceMap.getKey() + " Appears: " + priceMap.getValue());
59
+                }
60
+
61
+
62
+            }
63
+        }
64
+        System.out.println("------------------------------");
65
+        System.out.println("Errors: " + errorCounter);
66
+
67
+
68
+    }
69
+
70
+
71
+    public static HashMap<String, HashMap<Double, Integer>> getNamePriceMap(ArrayList<Item> items, String itemName) {
72
+
73
+        HashMap<String, HashMap<Double, Integer>> namePriceMap = new HashMap<>();
74
+
75
+        ArrayList<Item> nameList = new ArrayList<>();
76
+
77
+        for (Item item : items) {
78
+            if (item.getName().equals(itemName)) {
79
+                nameList.add(item);
80
+            }
81
+        }
82
+        namePriceMap.put(itemName, getPriceMap(nameList));
83
+
84
+        return namePriceMap;
85
+    }
86
+
87
+    public static HashMap<Double, Integer> getPriceMap(ArrayList<Item> items) {
88
+
89
+        HashMap<Double, Integer> map = new HashMap<>();
90
+
91
+        for (Item item : items) {
92
+            if (map.containsKey(item.getPrice())) {
93
+                Integer newItemCount = map.get(item.getPrice());
94
+                newItemCount++;
95
+                map.put(item.getPrice(), newItemCount);
96
+            } else {
97
+                map.put(item.getPrice(), 1);
98
+            }
99
+        }
100
+        return map;
101
+    }
102
+
103
+    public static HashMap<String, Integer> getNameMap(ArrayList<Item> items) {
104
+
105
+        HashMap<String, Integer> map = new HashMap<>();
106
+
107
+        for (Item item : items) {
108
+            if (map.containsKey(item.getName())) {
109
+                Integer newItemCount = map.get(item.getName());
110
+                newItemCount++;
111
+                map.put(item.getName(), newItemCount);
112
+            } else {
113
+                map.put(item.getName(), 1);
114
+            }
115
+        }
116
+        return map;
18 117
     }
19 118
 }
119
+