Demetrius Murray vor 6 Jahren
Ursprung
Commit
ddfc24043a
2 geänderte Dateien mit 41 neuen und 17 gelöschten Zeilen
  1. 17
    11
      src/main/java/io/zipcoder/ItemParser.java
  2. 24
    6
      src/main/java/io/zipcoder/Main.java

+ 17
- 11
src/main/java/io/zipcoder/ItemParser.java Datei anzeigen

20
 
20
 
21
     public Item parseStringIntoItem(String rawItem) throws ItemParseException{
21
     public Item parseStringIntoItem(String rawItem) throws ItemParseException{
22
         List<String> list = findKeyValuePairsInRawItemData(rawItem);
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) {
23
         Map<String, String> map = new HashMap<>();
37
         Map<String, String> map = new HashMap<>();
24
         for (String string : list){
38
         for (String string : list){
25
-            Pattern keyPattern = Pattern.compile("([a-zA-Z0-9]+)(?=\\:)"); //(.+)(?=[\:])
39
+            Pattern keyPattern = Pattern.compile("([\\w]+)(?=\\:)"); //(.+)(?=[\:])
26
             Matcher keyMatch = keyPattern.matcher(string);
40
             Matcher keyMatch = keyPattern.matcher(string);
27
             Pattern valPattern = Pattern.compile("(?<=[\\:])([^##]+)");
41
             Pattern valPattern = Pattern.compile("(?<=[\\:])([^##]+)");
28
             Matcher valMatch = valPattern.matcher(string);
42
             Matcher valMatch = valPattern.matcher(string);
30
                 map.put(lower(keyMatch.group()), lower(valMatch.group()));
44
                 map.put(lower(keyMatch.group()), lower(valMatch.group()));
31
             }
45
             }
32
         }
46
         }
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
 
47
 
48
+        return map;
43
     }
49
     }
44
 
50
 
45
     public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){
51
     public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){
56
     private String lower(String input) {
62
     private String lower(String input) {
57
         StringBuilder inputLineT = new StringBuilder(input);
63
         StringBuilder inputLineT = new StringBuilder(input);
58
         for (int i = 0; i < inputLineT.length(); i++) {
64
         for (int i = 0; i < inputLineT.length(); i++) {
59
-            if (inputLineT.charAt(i) >= 65 && inputLineT.charAt(i) < 91 && i > 0) {
65
+            if (inputLineT.charAt(i) >= 65 && inputLineT.charAt(i) < 91) {
60
                 inputLineT.setCharAt(i, (char) (inputLineT.charAt(i) + 32));
66
                 inputLineT.setCharAt(i, (char) (inputLineT.charAt(i) + 32));
61
             }
67
             }
62
         }
68
         }

+ 24
- 6
src/main/java/io/zipcoder/Main.java Datei anzeigen

10
 
10
 
11
 
11
 
12
 public class Main {
12
 public class Main {
13
-    public static int count;
13
+    private static int count;
14
     Map<String, List<Double>> map = new HashMap<>();
14
     Map<String, List<Double>> map = new HashMap<>();
15
 
15
 
16
     private String readRawDataToString() throws Exception{
16
     private String readRawDataToString() throws Exception{
39
         return list.size();
39
         return list.size();
40
     }
40
     }
41
 
41
 
42
+    private String getCountPhrase(int num){
43
+        return num == 1 ? num + " time": num + " times";
44
+    }
45
+
42
     public String writeOutput(){
46
     public String writeOutput(){
43
         StringBuilder sb = new StringBuilder();
47
         StringBuilder sb = new StringBuilder();
48
+        String column1; String column2;
49
+
44
         for (String s : map.keySet()){
50
         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()));
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));
47
             sb.append(String.format("%14s %8s %14s\n","==============", " ", "=============="));
58
             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)));
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));
50
                 sb.append(String.format("%14s %8s %14s\n","--------------", " ", "--------------"));
65
                 sb.append(String.format("%14s %8s %14s\n","--------------", " ", "--------------"));
51
             }
66
             }
67
+            sb.append("\n");
52
         }
68
         }
53
 
69
 
54
-        sb.append("\n").append(count);
70
+        column1 = "Errors";
71
+        column2 = String.format("%-7s%7s","Seen: ", getCountPhrase(count));
72
+        sb.append(String.format("%-14s %8s %14s",column1, " ",column2));
55
         return sb.toString();
73
         return sb.toString();
56
     }
74
     }
57
 
75