Kaynağa Gözat

reformatted item parser

Eric Cordell 6 yıl önce
ebeveyn
işleme
3e56f73966

+ 1
- 0
src/main/java/io/zipcoder/ItemParseException.java Dosyayı Görüntüle

@@ -1,4 +1,5 @@
1 1
 package io.zipcoder;
2 2
 
3 3
 public class ItemParseException extends Exception {
4
+
4 5
 }

+ 44
- 20
src/main/java/io/zipcoder/ItemParser.java Dosyayı Görüntüle

@@ -1,8 +1,11 @@
1 1
 package io.zipcoder;
2 2
 
3
+import com.sun.org.apache.xerces.internal.impl.xpath.regex.Match;
4
+
3 5
 import java.util.ArrayList;
4 6
 import java.util.Arrays;
5
-import java.util.HashMap;
7
+import java.util.regex.Matcher;
8
+import java.util.regex.Pattern;
6 9
 
7 10
 public class ItemParser {
8 11
 
@@ -27,30 +30,51 @@ public class ItemParser {
27 30
     }
28 31
 
29 32
     // 4
30
-    public Item parseStringIntoItem(String rawItem) throws ItemParseException {
31
-        int count = 0;
32
-        String strPrice = "\\d.\\d\\d";
33
-        Double price = Double.valueOf(strPrice);
34
-        String type = "(T|t)..(E|e)";
35
-        String expiration = "(E|e)........(N|n)";
33
+    public Item parseStringIntoItem(String rawItem) throws ItemParseException { // need addtnl.. methods to check info
34
+
35
+        String name = checkName(rawItem);
36
+        Double price = Double.valueOf(checkPrice(rawItem));
37
+        String type = checkType(rawItem);
38
+        String expiration = checkExpiration(rawItem);
39
+
36 40
 
37
-        ArrayList<String> temp = parseRawDataIntoStringArray(rawItem);
38
-      //  ArrayList<Item> myItem = new ArrayList<Item>();
41
+        return new Item(name, price, type, expiration);
42
+    }
39 43
 
40
-        for (int i = 0; i < temp.size(); i++) {
41
-            if (findKeyValuePairsInRawItemData(temp.get(i)).equals(null)) {
42
-                count++;
43
-                throw new ItemParseException();
44
-            }
45
-            else if (findKeyValuePairsInRawItemData(temp.get(i)).size() == 4) {
46
-                for (int j = 0; j < temp.size(); j++) {
44
+    public String checkName(String input) throws ItemParseException {
45
+        Pattern patternName = Pattern.compile("([Nn]..[Ee]:)(\\w+)");
46
+        Matcher matcherName = patternName.matcher(input);
47 47
 
48
-                }
49
-            }
50
-        }
51
-        return null;
48
+        if (matcherName.find())         // find() will search substrings
49
+            return matcherName.group(2).toLowerCase();
50
+        else throw new ItemParseException();
52 51
     }
53 52
 
53
+    public String checkPrice(String input) throws ItemParseException {
54
+        Pattern patternPrice = Pattern.compile("([Pp]...[Ee]:)(\\d\\.\\d{2})");
55
+        Matcher matcherPrice = patternPrice.matcher(input);
56
+
57
+        if (matcherPrice.find())
58
+            return matcherPrice.group(2);
59
+        else throw new ItemParseException();
60
+    }
54 61
 
62
+    public String checkType(String input) throws ItemParseException {
63
+        Pattern patternType = Pattern.compile("([Tt]..[Ee]:)(\\w+)");
64
+        Matcher matcherType = patternType.matcher(input);
65
+
66
+        if (matcherType.find())
67
+            return matcherType.group(2).toLowerCase();
68
+        else throw new ItemParseException();
69
+    }
70
+
71
+    public String checkExpiration(String input) throws ItemParseException {
72
+        Pattern patternExpiration = Pattern.compile("([Ee]........[Nn]:)(\\d\\/\\d{2}\\/\\d{4})");
73
+        Matcher matcherExpiration = patternExpiration.matcher(input);
74
+
75
+        if (matcherExpiration.find())
76
+            return matcherExpiration.group(2);
77
+        else throw new ItemParseException();
78
+    }
55 79
 
56 80
 }