Browse Source

most tests pass

mpierse 6 years ago
parent
commit
cbe82ce85f

+ 1
- 1
src/main/java/io/zipcoder/ItemParseException.java View File

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

+ 33
- 1
src/main/java/io/zipcoder/ItemParser.java View File

@@ -2,9 +2,15 @@ package io.zipcoder;
2 2
 
3 3
 import java.util.ArrayList;
4 4
 import java.util.Arrays;
5
+import java.util.logging.Level;
6
+import java.util.logging.Logger;
7
+import java.util.regex.Matcher;
8
+import java.util.regex.Pattern;
5 9
 
6 10
 public class ItemParser {
7 11
 
12
+    private static final Logger log = Logger.getLogger( ItemParser.class.getName() );
13
+
8 14
 
9 15
     public ArrayList<String> parseRawDataIntoStringArray(String rawData){
10 16
         String stringPattern = "##";
@@ -12,12 +18,38 @@ public class ItemParser {
12 18
         return response;
13 19
     }
14 20
 
21
+    public String correctCase(){
22
+        return "";
23
+    }
24
+
15 25
     public Item parseStringIntoItem(String rawItem) throws ItemParseException{
26
+            ArrayList<String> properties = findKeyValuePairsInRawItemData(rawItem);
27
+        Pattern patternAlpha = Pattern.compile("(?<=:).*");
28
+        Pattern patternDate = Pattern.compile("\\d/\\d\\d/\\d\\d\\d\\d");
29
+        Pattern patternDouble = Pattern.compile("\\d.\\d\\d");
30
+        Matcher mName = patternAlpha.matcher(properties.get(0));
31
+        Matcher mPrice = patternDouble.matcher(properties.get(1));
32
+        Matcher mDescription = patternAlpha.matcher(properties.get(2));
33
+        Matcher mExpiration = patternDate.matcher(properties.get(3));
34
+
35
+try {
36
+
37
+    if (mName.find() && mDescription.find() && mPrice.find() && mExpiration.find()) {
38
+        String name = mName.group();
39
+        Double price = Double.parseDouble(mPrice.group());
40
+        String type = mDescription.group();
41
+        String expiration = mExpiration.group();
42
+        return new Item(name, price, type, expiration);
43
+    }
44
+    } catch( ItemParseException e ){
45
+        log.log(Level.WARNING, "invalid");
46
+    }
47
+
16 48
         return null;
17 49
     }
18 50
 
19 51
     public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){
20
-        String stringPattern = "[;|^]";
52
+        String stringPattern = "[;|^!%@*]";
21 53
         ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawItem);
22 54
         return response;
23 55
     }

+ 27
- 4
src/main/java/io/zipcoder/Main.java View File

@@ -2,18 +2,41 @@ package io.zipcoder;
2 2
 
3 3
 import org.apache.commons.io.IOUtils;
4 4
 
5
+import java.io.IOException;
6
+import java.util.ArrayList;
7
+import java.util.logging.Level;
8
+import java.util.logging.Logger;
9
+
5 10
 
6 11
 public class Main {
7 12
 
8
-    public String readRawDataToString() throws Exception{
13
+    private static final Logger log = Logger.getLogger( ItemParser.class.getName() );
14
+
15
+    public String readRawDataToString() {
9 16
         ClassLoader classLoader = getClass().getClassLoader();
10
-        String result = IOUtils.toString(classLoader.getResourceAsStream("RawData.txt"));
17
+        String result = null;
18
+        try {
19
+            result = IOUtils.toString(classLoader.getResourceAsStream("RawData.txt"));
20
+        } catch (IOException e) {
21
+            e.printStackTrace();
22
+        }
11 23
         return result;
12 24
     }
13 25
 
14
-    public static void main(String[] args) throws Exception{
26
+    public static void main(String[] args) throws ItemParseException{
15 27
         String output = (new Main()).readRawDataToString();
16
-        System.out.println(output);
28
+        ItemParser parser = new ItemParser();
29
+        ArrayList<String> items = parser.parseRawDataIntoStringArray(output);
30
+        for (String item : items) {
31
+           try{
32
+            Item itemObj = parser.parseStringIntoItem(item);
33
+           System.out.println(itemObj.toString());}
34
+           catch (NullPointerException e){
35
+               log.log(Level.WARNING, "null pointer");
36
+               continue;
37
+           }
38
+        }
39
+      //  System.out.println(output);
17 40
         // TODO: parse the data in output into items, and display to console.
18 41
     }
19 42
 }