ソースを参照

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

Demetrius Murray 5 年 前
コミット
31e0e944ad
共有4 個のファイルを変更した123 個の追加7 個の削除を含む
  1. 12
    0
      pom.xml
  2. 48
    4
      src/main/java/io/zipcoder/ItemParser.java
  3. 62
    3
      src/main/java/io/zipcoder/Main.java
  4. 1
    0
      src/test/java/io/zipcoder/ItemParserTest.java

+ 12
- 0
pom.xml ファイルの表示

7
     <groupId>io.zipcoder</groupId>
7
     <groupId>io.zipcoder</groupId>
8
     <artifactId>PainfullAfternoon</artifactId>
8
     <artifactId>PainfullAfternoon</artifactId>
9
     <version>1.0-SNAPSHOT</version>
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
     <dependencies>
23
     <dependencies>
12
         <dependency>
24
         <dependency>

+ 48
- 4
src/main/java/io/zipcoder/ItemParser.java ファイルの表示

1
 package io.zipcoder;
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
 public class ItemParser {
10
 public class ItemParser {
7
 
11
 
12
+    public ItemParser(){
13
+    }
8
 
14
 
9
     public ArrayList<String> parseRawDataIntoStringArray(String rawData){
15
     public ArrayList<String> parseRawDataIntoStringArray(String rawData){
10
         String stringPattern = "##";
16
         String stringPattern = "##";
13
     }
19
     }
14
 
20
 
15
     public Item parseStringIntoItem(String rawItem) throws ItemParseException{
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
     public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){
45
     public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){
23
     }
49
     }
24
 
50
 
25
     private ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString){
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
 
2
 
3
 import org.apache.commons.io.IOUtils;
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
 public class Main {
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
         ClassLoader classLoader = getClass().getClassLoader();
17
         ClassLoader classLoader = getClass().getClassLoader();
10
         String result = IOUtils.toString(classLoader.getResourceAsStream("RawData.txt"));
18
         String result = IOUtils.toString(classLoader.getResourceAsStream("RawData.txt"));
11
         return result;
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
     public static void main(String[] args) throws Exception{
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
         // TODO: parse the data in output into items, and display to console.
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
     public void findKeyValuePairsInRawItemDataTest(){
50
     public void findKeyValuePairsInRawItemDataTest(){
51
         Integer expected = 4;
51
         Integer expected = 4;
52
         Integer actual = itemParser.findKeyValuePairsInRawItemData(rawSingleItem).size();
52
         Integer actual = itemParser.findKeyValuePairsInRawItemData(rawSingleItem).size();
53
+        //System.out.println(itemParser.findKeyValuePairsInRawItemData(rawSingleItem));
53
         assertEquals(expected, actual);
54
         assertEquals(expected, actual);
54
     }
55
     }
55
 
56