Procházet zdrojové kódy

Merge 4809d82a80c74d94f7333975f429733a900606b0 into 23c2c01bf07b924ac9b860fa2771bf6ceb246ad5

erc91087 před 6 roky
rodič
revize
ba73d56ba9
No account linked to committer's email

+ 12
- 0
pom.xml Zobrazit soubor

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

+ 1
- 0
src/main/java/io/zipcoder/ItemParseException.java Zobrazit soubor

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

+ 78
- 10
src/main/java/io/zipcoder/ItemParser.java Zobrazit soubor

@@ -1,31 +1,99 @@
1 1
 package io.zipcoder;
2 2
 
3
+import com.sun.org.apache.xerces.internal.impl.xpath.regex.Match;
4
+
3 5
 import java.util.ArrayList;
4 6
 import java.util.Arrays;
7
+import java.util.regex.Matcher;
8
+import java.util.regex.Pattern;
5 9
 
6 10
 public class ItemParser {
7 11
 
12
+    public int errorCount = 0;
13
+
14
+    // 1
15
+    private ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString) {
16
+        return new ArrayList<String>(Arrays.asList(inputString.split(stringPattern)));
17
+    }
8 18
 
9
-    public ArrayList<String> parseRawDataIntoStringArray(String rawData){
19
+    // 2
20
+    public ArrayList<String> parseRawDataIntoStringArray(String rawData) {
10 21
         String stringPattern = "##";
11
-        ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawData);
22
+        ArrayList<String> response = splitStringWithRegexPattern(stringPattern, rawData);
12 23
         return response;
13 24
     }
14 25
 
15
-    public Item parseStringIntoItem(String rawItem) throws ItemParseException{
16
-        return null;
26
+    // 3
27
+    public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem) {
28
+        String stringPattern = "[@|^|*|%|!|;]";
29
+        ArrayList<String> response = splitStringWithRegexPattern(stringPattern, rawItem);
30
+        return response;
17 31
     }
18 32
 
19
-    public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){
20
-        String stringPattern = "[;|^]";
21
-        ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawItem);
22
-        return response;
33
+    // 4
34
+    public Item parseStringIntoItem(String rawItem) throws ItemParseException { // need addtnl.. methods to check info
35
+
36
+        String name = checkName(rawItem);
37
+        Double price = Double.valueOf(checkPrice(rawItem));
38
+        String type = checkType(rawItem);
39
+        String expiration = checkExpiration(rawItem);
40
+
41
+
42
+        return new Item(name, price, type, expiration);
23 43
     }
24 44
 
25
-    private ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString){
26
-        return new ArrayList<String>(Arrays.asList(inputString.split(stringPattern)));
45
+    public String checkName(String input) throws ItemParseException {
46
+        Pattern patternName = Pattern.compile("([Nn]..[Ee]:)(\\w+)");
47
+        Matcher matcherName = patternName.matcher(input);
48
+
49
+        if (matcherName.find())         // find() will search substrings
50
+            return matcherName.group(2).toLowerCase();
51
+        else throw new ItemParseException();
27 52
     }
28 53
 
54
+    public String checkPrice(String input) throws ItemParseException {
55
+        Pattern patternPrice = Pattern.compile("([Pp]...[Ee]:)(\\d\\.\\d{2})");
56
+        Matcher matcherPrice = patternPrice.matcher(input);
29 57
 
58
+        if (matcherPrice.find())
59
+            return matcherPrice.group(2);
60
+        else throw new ItemParseException();
61
+    }
62
+
63
+    public String checkType(String input) throws ItemParseException {
64
+        Pattern patternType = Pattern.compile("([Tt]..[Ee]:)(\\w+)");
65
+        Matcher matcherType = patternType.matcher(input);
66
+
67
+        if (matcherType.find())
68
+            return matcherType.group(2).toLowerCase();
69
+        else throw new ItemParseException();
70
+    }
71
+
72
+    public String checkExpiration(String input) throws ItemParseException {
73
+        Pattern patternExpiration = Pattern.compile("([Ee]........[Nn]:)(\\d\\/\\d{2}\\/\\d{4})");
74
+        Matcher matcherExpiration = patternExpiration.matcher(input);
75
+
76
+        if (matcherExpiration.find())
77
+            return matcherExpiration.group(2);
78
+        else throw new ItemParseException();
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
+    }
30 98
 
31 99
 }

+ 102
- 2
src/main/java/io/zipcoder/Main.java Zobrazit soubor

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