Demetrius Murray преди 5 години
родител
ревизия
ddfc24043a
променени са 2 файла, в които са добавени 41 реда и са изтрити 17 реда
  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 Целия файл

@@ -20,9 +20,23 @@ public class ItemParser {
20 20
 
21 21
     public Item parseStringIntoItem(String rawItem) throws ItemParseException{
22 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 37
         Map<String, String> map = new HashMap<>();
24 38
         for (String string : list){
25
-            Pattern keyPattern = Pattern.compile("([a-zA-Z0-9]+)(?=\\:)"); //(.+)(?=[\:])
39
+            Pattern keyPattern = Pattern.compile("([\\w]+)(?=\\:)"); //(.+)(?=[\:])
26 40
             Matcher keyMatch = keyPattern.matcher(string);
27 41
             Pattern valPattern = Pattern.compile("(?<=[\\:])([^##]+)");
28 42
             Matcher valMatch = valPattern.matcher(string);
@@ -30,16 +44,8 @@ public class ItemParser {
30 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 51
     public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){
@@ -56,7 +62,7 @@ public class ItemParser {
56 62
     private String lower(String input) {
57 63
         StringBuilder inputLineT = new StringBuilder(input);
58 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 66
                 inputLineT.setCharAt(i, (char) (inputLineT.charAt(i) + 32));
61 67
             }
62 68
         }

+ 24
- 6
src/main/java/io/zipcoder/Main.java Целия файл

@@ -10,7 +10,7 @@ import java.util.stream.Collectors;
10 10
 
11 11
 
12 12
 public class Main {
13
-    public static int count;
13
+    private static int count;
14 14
     Map<String, List<Double>> map = new HashMap<>();
15 15
 
16 16
     private String readRawDataToString() throws Exception{
@@ -39,19 +39,37 @@ public class Main {
39 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 46
     public String writeOutput(){
43 47
         StringBuilder sb = new StringBuilder();
48
+        String column1; String column2;
49
+
44 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 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 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 73
         return sb.toString();
56 74
     }
57 75