瀏覽代碼

completed lab

mpierse 6 年之前
父節點
當前提交
2437ba3c00

+ 22
- 11
src/main/java/io/zipcoder/ItemParser.java 查看文件

@@ -1,5 +1,7 @@
1 1
 package io.zipcoder;
2 2
 
3
+import com.sun.xml.internal.fastinfoset.util.CharArray;
4
+
3 5
 import java.util.ArrayList;
4 6
 import java.util.Arrays;
5 7
 import java.util.logging.Level;
@@ -18,21 +20,32 @@ public class ItemParser {
18 20
         return response;
19 21
     }
20 22
 
21
-    public String correctCase(){
22
-        return "";
23
-    }
23
+    public String toLowerCase(String str){
24
+
25
+            StringBuilder inputLineT = new StringBuilder(str);
26
+
27
+            for(int i = 0 ; i < inputLineT.length() ; i++)
28
+            {
29
+                if(inputLineT.charAt(i) >= 65 && inputLineT.charAt(i) <=91)
30
+                {
31
+                    inputLineT.setCharAt(i, (char)(inputLineT.charAt(i)+32));
32
+                }
33
+            }
34
+            return inputLineT.toString();
35
+        }
36
+
24 37
 
25 38
     public Item parseStringIntoItem(String rawItem) throws ItemParseException{
26
-            ArrayList<String> properties = findKeyValuePairsInRawItemData(rawItem);
27
-        Pattern patternAlpha = Pattern.compile("(?<=:).*");
39
+            ArrayList<String> properties = findKeyValuePairsInRawItemData(toLowerCase(toLowerCase(rawItem)));
40
+        Pattern patternAlpha = Pattern.compile("(?<=:)[^\\s].*");
28 41
         Pattern patternDate = Pattern.compile("\\d/\\d\\d/\\d\\d\\d\\d");
29 42
         Pattern patternDouble = Pattern.compile("\\d.\\d\\d");
30 43
         Matcher mName = patternAlpha.matcher(properties.get(0));
31 44
         Matcher mPrice = patternDouble.matcher(properties.get(1));
32 45
         Matcher mDescription = patternAlpha.matcher(properties.get(2));
33 46
         Matcher mExpiration = patternDate.matcher(properties.get(3));
47
+        int exceptionCount=0;
34 48
 
35
-try {
36 49
 
37 50
     if (mName.find() && mDescription.find() && mPrice.find() && mExpiration.find()) {
38 51
         String name = mName.group();
@@ -40,13 +53,11 @@ try {
40 53
         String type = mDescription.group();
41 54
         String expiration = mExpiration.group();
42 55
         return new Item(name, price, type, expiration);
43
-    }
44
-    } catch( ItemParseException e ){
45
-        log.log(Level.WARNING, "invalid");
56
+    } else {
57
+        throw new ItemParseException(); }
46 58
     }
47 59
 
48
-        return null;
49
-    }
60
+
50 61
 
51 62
     public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){
52 63
         String stringPattern = "[;|^!%@*]";

+ 12
- 9
src/main/java/io/zipcoder/Main.java 查看文件

@@ -23,20 +23,23 @@ public class Main {
23 23
         return result;
24 24
     }
25 25
 
26
-    public static void main(String[] args) throws ItemParseException{
26
+    public static void main(String[] args) {
27 27
         String output = (new Main()).readRawDataToString();
28 28
         ItemParser parser = new ItemParser();
29 29
         ArrayList<String> items = parser.parseRawDataIntoStringArray(output);
30
+        int exceptionCount = 1;
30 31
         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
-           }
32
+try {
33
+    Item itemObj = parser.parseStringIntoItem(item);
34
+    System.out.println(itemObj.toString());
35
+} catch (ItemParseException e){
36
+    log.log(Level.INFO, "Bad item, exception count: " + exceptionCount++);
37
+    continue;
38
+}
39
+            }
38 40
         }
41
+
39 42
       //  System.out.println(output);
40 43
         // TODO: parse the data in output into items, and display to console.
41 44
     }
42
-}
45
+

+ 1
- 1
src/test/java/io/zipcoder/ItemParserTest.java 查看文件

@@ -14,7 +14,7 @@ public class ItemParserTest {
14 14
 
15 15
     private String rawSingleItemIrregularSeperatorSample = "naMe:MiLK;price:3.23;type:Food^expiration:1/11/2016##";
16 16
 
17
-    private String rawBrokenSingleItem =    "naMe:Milk;price:3.23;type:Food;expiration:1/25/2016##";
17
+    private String rawBrokenSingleItem =    "naMe:Milk;price:3.23;type:;expiration:1/25/2016##";
18 18
 
19 19
     private String rawMultipleItems = "naMe:Milk;price:3.23;type:Food;expiration:1/25/2016##"
20 20
                                       +"naME:BreaD;price:1.23;type:Food;expiration:1/02/2016##"