瀏覽代碼

works but counting too many errors at the moment and not enough items

Demetrius Murray 5 年之前
父節點
當前提交
31e0e944ad

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

+ 48
- 4
src/main/java/io/zipcoder/ItemParser.java 查看文件

@@ -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,27 @@ 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 = new HashMap<>();
24
+        for (String string : list){
25
+            Pattern keyPattern = Pattern.compile("([a-zA-Z0-9]+)(?=\\:)"); //(.+)(?=[\:])
26
+            Matcher keyMatch = keyPattern.matcher(string);
27
+            Pattern valPattern = Pattern.compile("(?<=[\\:])([^##]+)");
28
+            Matcher valMatch = valPattern.matcher(string);
29
+            if (keyMatch.find() && valMatch.find()) {
30
+                map.put(lower(keyMatch.group()), lower(valMatch.group()));
31
+            }
32
+        }
33
+        try {
34
+            return new Item(rmvZeros(map.get("name")), Double.parseDouble(map.get("price")), map.get("type"), map.get("expiration"));
35
+        } catch (NullPointerException n) {
36
+            throw new ItemParseException();
37
+        }
38
+//        Pattern p = Pattern.compile("[^a-zA-Z0-9\\:\\.\\/]");
39
+//        Matcher m = p.matcher(rawItem);
40
+//        String replaced = m.replaceAll("-");
41
+//        String otherPatter = "([a-zA-Z0-9\\/\\:\\.]+)(?=[^\\/])";
42
+
17 43
     }
18 44
 
19 45
     public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){
@@ -23,9 +49,27 @@ public class ItemParser {
23 49
     }
24 50
 
25 51
     private ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString){
26
-        return new ArrayList<String>(Arrays.asList(inputString.split(stringPattern)));
52
+        return new ArrayList<>(Arrays.asList(inputString.split(stringPattern)));
27 53
     }
28 54
 
29 55
 
56
+    private String lower(String input) {
57
+        StringBuilder inputLineT = new StringBuilder(input);
58
+        for (int i = 0; i < inputLineT.length(); i++) {
59
+            if (inputLineT.charAt(i) >= 65 && inputLineT.charAt(i) < 91 && i > 0) {
60
+                inputLineT.setCharAt(i, (char) (inputLineT.charAt(i) + 32));
61
+            }
62
+        }
63
+        return inputLineT.toString();
64
+    }
30 65
 
66
+    private String rmvZeros(String input) {
67
+        StringBuilder inputLineT = new StringBuilder(input);
68
+        for (int i = 0; i < inputLineT.length(); i++) {
69
+            if (inputLineT.charAt(i) == 48) {
70
+                inputLineT.setCharAt(i, 'o');
71
+            }
72
+        }
73
+        return inputLineT.toString();
74
+    }
31 75
 }

+ 62
- 3
src/main/java/io/zipcoder/Main.java 查看文件

@@ -2,18 +2,77 @@ 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
+    public 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
+    public String writeOutput(){
43
+        StringBuilder sb = new StringBuilder();
44
+        for (String s : map.keySet()){
45
+            List<Double> l = map.get(s).stream().distinct().collect(Collectors.toList());
46
+            sb.append(String.format("%14s %8s %14s\n","name: " + s, " ", "seen: " + map.get(s).size()));
47
+            sb.append(String.format("%14s %8s %14s\n","==============", " ", "=============="));
48
+            for (Double d : l) {
49
+                sb.append(String.format("%14s %8s %14s\n","Price: " + d, " ", "seen: " + getCount(map.get(s),d)));
50
+                sb.append(String.format("%14s %8s %14s\n","--------------", " ", "--------------"));
51
+            }
52
+        }
53
+
54
+        sb.append("\n").append(count);
55
+        return sb.toString();
56
+    }
57
+
14 58
     public static void main(String[] args) throws Exception{
15
-        String output = (new Main()).readRawDataToString();
16
-        System.out.println(output);
59
+        Main main = new Main();
60
+        String output = main.readRawDataToString();
61
+
62
+        ItemParser ip = new ItemParser();
63
+        List<String> list = ip.parseRawDataIntoStringArray(output);
64
+
65
+        for (String str : list) {
66
+            try {
67
+                Item newItem = ip.parseStringIntoItem(str);
68
+                main.addToMap(newItem);
69
+            } catch (ItemParseException e1) {
70
+                count++;
71
+            }
72
+        }
73
+
74
+        System.out.println(main.writeOutput());
75
+
17 76
         // TODO: parse the data in output into items, and display to console.
18 77
     }
19 78
 }

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

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