#7 Complete - Demetrius

Aberto
demetrm quer mesclar 2 commits de demetrm/JerkSON-Parser:master em master

+ 12
- 0
pom.xml Ver arquivo

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

+ 54
- 4
src/main/java/io/zipcoder/ItemParser.java Ver arquivo

@@ -1,10 +1,16 @@
1 1
 package io.zipcoder;
2 2
 
3
-import java.util.ArrayList;
4
-import java.util.Arrays;
3
+import java.util.*;
4
+import java.util.regex.Matcher;
5
+import java.util.regex.Pattern;
6
+import java.util.stream.Stream;
7
+
8
+import static io.zipcoder.Main.count;
5 9
 
6 10
 public class ItemParser {
7 11
 
12
+    public ItemParser(){
13
+    }
8 14
 
9 15
     public ArrayList<String> parseRawDataIntoStringArray(String rawData){
10 16
         String stringPattern = "##";
@@ -13,7 +19,33 @@ public class ItemParser {
13 19
     }
14 20
 
15 21
     public Item parseStringIntoItem(String rawItem) throws ItemParseException{
16
-        return null;
22
+        List<String> list = findKeyValuePairsInRawItemData(rawItem);
23
+        Map<String, String> map = getItemMap(list);
24
+        try {
25
+            return new Item(
26
+                    rmvZeros(map.get("name")),
27
+                    Double.parseDouble(map.get("price")),
28
+                    map.get("type"),
29
+                    map.get("expiration"));
30
+
31
+        } catch (NullPointerException n) {
32
+            throw new ItemParseException();
33
+        }
34
+    }
35
+
36
+    private Map<String,String> getItemMap(List<String> list) {
37
+        Map<String, String> map = new HashMap<>();
38
+        for (String string : list){
39
+            Pattern keyPattern = Pattern.compile("([\\w]+)(?=\\:)"); //(.+)(?=[\:])
40
+            Matcher keyMatch = keyPattern.matcher(string);
41
+            Pattern valPattern = Pattern.compile("(?<=[\\:])([^##]+)");
42
+            Matcher valMatch = valPattern.matcher(string);
43
+            if (keyMatch.find() && valMatch.find()) {
44
+                map.put(lower(keyMatch.group()), lower(valMatch.group()));
45
+            }
46
+        }
47
+
48
+        return map;
17 49
     }
18 50
 
19 51
     public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){
@@ -23,9 +55,27 @@ public class ItemParser {
23 55
     }
24 56
 
25 57
     private ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString){
26
-        return new ArrayList<String>(Arrays.asList(inputString.split(stringPattern)));
58
+        return new ArrayList<>(Arrays.asList(inputString.split(stringPattern)));
27 59
     }
28 60
 
29 61
 
62
+    private String lower(String input) {
63
+        StringBuilder inputLineT = new StringBuilder(input);
64
+        for (int i = 0; i < inputLineT.length(); i++) {
65
+            if (inputLineT.charAt(i) >= 65 && inputLineT.charAt(i) < 91) {
66
+                inputLineT.setCharAt(i, (char) (inputLineT.charAt(i) + 32));
67
+            }
68
+        }
69
+        return inputLineT.toString();
70
+    }
30 71
 
72
+    private String rmvZeros(String input) {
73
+        StringBuilder inputLineT = new StringBuilder(input);
74
+        for (int i = 0; i < inputLineT.length(); i++) {
75
+            if (inputLineT.charAt(i) == 48) {
76
+                inputLineT.setCharAt(i, 'o');
77
+            }
78
+        }
79
+        return inputLineT.toString();
80
+    }
31 81
 }

+ 80
- 3
src/main/java/io/zipcoder/Main.java Ver arquivo

@@ -2,18 +2,95 @@ 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.List;
8
+import java.util.Map;
9
+import java.util.stream.Collectors;
10
+
5 11
 
6 12
 public class Main {
13
+    private static int count;
14
+    Map<String, List<Double>> map = new HashMap<>();
7 15
 
8
-    public String readRawDataToString() throws Exception{
16
+    private String readRawDataToString() throws Exception{
9 17
         ClassLoader classLoader = getClass().getClassLoader();
10 18
         String result = IOUtils.toString(classLoader.getResourceAsStream("RawData.txt"));
11 19
         return result;
12 20
     }
13 21
 
22
+    private void addToMap(Item item){
23
+        String key = item.getName();
24
+        List<Double> list = new ArrayList<>();
25
+
26
+        if (!map.containsKey(key)) {
27
+            list.add(item.getPrice());
28
+            map.put(key, list);
29
+        }
30
+        else {
31
+            list = map.get(key);
32
+            list.add(item.getPrice());
33
+            map.put(key, list);
34
+        }
35
+    }
36
+
37
+    private int getCount(List<Double> list, double d){
38
+        list.removeIf(e -> e != d);
39
+        return list.size();
40
+    }
41
+
42
+    private String getCountPhrase(int num){
43
+        return num == 1 ? num + " time": num + " times";
44
+    }
45
+
46
+    public String writeOutput(){
47
+        StringBuilder sb = new StringBuilder();
48
+        String column1; String column2;
49
+
50
+        for (String s : map.keySet()){
51
+            List<Double> prices = map.get(s);
52
+            List<Double> uniquePrices = prices.stream().distinct().collect(Collectors.toList());
53
+
54
+            column1 = String.format("%-7s%7s", "Name: ", s);
55
+            column2 = String.format("%-7s%7s","Seen: ", getCountPhrase(prices.size()));
56
+
57
+            sb.append(String.format("%14s %8s %14s\n",column1, " ",column2));
58
+            sb.append(String.format("%14s %8s %14s\n","==============", " ", "=============="));
59
+
60
+            for (Double d : uniquePrices) {
61
+                column1 = String.format("%-7s%7s", "Price: ", d);
62
+                column2 = String.format("%-7s%7s","Seen: ", getCountPhrase(getCount(prices,d)));
63
+
64
+                sb.append(String.format("%14s %8s %14s\n",column1, " ",column2));
65
+                sb.append(String.format("%14s %8s %14s\n","--------------", " ", "--------------"));
66
+            }
67
+            sb.append("\n");
68
+        }
69
+
70
+        column1 = "Errors";
71
+        column2 = String.format("%-7s%7s","Seen: ", getCountPhrase(count));
72
+        sb.append(String.format("%-14s %8s %14s",column1, " ",column2));
73
+        return sb.toString();
74
+    }
75
+
14 76
     public static void main(String[] args) throws Exception{
15
-        String output = (new Main()).readRawDataToString();
16
-        System.out.println(output);
77
+        Main main = new Main();
78
+        String output = main.readRawDataToString();
79
+
80
+        ItemParser ip = new ItemParser();
81
+        List<String> list = ip.parseRawDataIntoStringArray(output);
82
+
83
+        for (String str : list) {
84
+            try {
85
+                Item newItem = ip.parseStringIntoItem(str);
86
+                main.addToMap(newItem);
87
+            } catch (ItemParseException e1) {
88
+                count++;
89
+            }
90
+        }
91
+
92
+        System.out.println(main.writeOutput());
93
+
17 94
         // TODO: parse the data in output into items, and display to console.
18 95
     }
19 96
 }

+ 1
- 0
src/test/java/io/zipcoder/ItemParserTest.java Ver arquivo

@@ -50,6 +50,7 @@ public class ItemParserTest {
50 50
     public void findKeyValuePairsInRawItemDataTest(){
51 51
         Integer expected = 4;
52 52
         Integer actual = itemParser.findKeyValuePairsInRawItemData(rawSingleItem).size();
53
+        //System.out.println(itemParser.findKeyValuePairsInRawItemData(rawSingleItem));
53 54
         assertEquals(expected, actual);
54 55
     }
55 56