mpierse před 5 roky
rodič
revize
2437ba3c00

+ 22
- 11
src/main/java/io/zipcoder/ItemParser.java Zobrazit soubor

1
 package io.zipcoder;
1
 package io.zipcoder;
2
 
2
 
3
+import com.sun.xml.internal.fastinfoset.util.CharArray;
4
+
3
 import java.util.ArrayList;
5
 import java.util.ArrayList;
4
 import java.util.Arrays;
6
 import java.util.Arrays;
5
 import java.util.logging.Level;
7
 import java.util.logging.Level;
18
         return response;
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
     public Item parseStringIntoItem(String rawItem) throws ItemParseException{
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
         Pattern patternDate = Pattern.compile("\\d/\\d\\d/\\d\\d\\d\\d");
41
         Pattern patternDate = Pattern.compile("\\d/\\d\\d/\\d\\d\\d\\d");
29
         Pattern patternDouble = Pattern.compile("\\d.\\d\\d");
42
         Pattern patternDouble = Pattern.compile("\\d.\\d\\d");
30
         Matcher mName = patternAlpha.matcher(properties.get(0));
43
         Matcher mName = patternAlpha.matcher(properties.get(0));
31
         Matcher mPrice = patternDouble.matcher(properties.get(1));
44
         Matcher mPrice = patternDouble.matcher(properties.get(1));
32
         Matcher mDescription = patternAlpha.matcher(properties.get(2));
45
         Matcher mDescription = patternAlpha.matcher(properties.get(2));
33
         Matcher mExpiration = patternDate.matcher(properties.get(3));
46
         Matcher mExpiration = patternDate.matcher(properties.get(3));
47
+        int exceptionCount=0;
34
 
48
 
35
-try {
36
 
49
 
37
     if (mName.find() && mDescription.find() && mPrice.find() && mExpiration.find()) {
50
     if (mName.find() && mDescription.find() && mPrice.find() && mExpiration.find()) {
38
         String name = mName.group();
51
         String name = mName.group();
40
         String type = mDescription.group();
53
         String type = mDescription.group();
41
         String expiration = mExpiration.group();
54
         String expiration = mExpiration.group();
42
         return new Item(name, price, type, expiration);
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
     public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){
62
     public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){
52
         String stringPattern = "[;|^!%@*]";
63
         String stringPattern = "[;|^!%@*]";

+ 12
- 9
src/main/java/io/zipcoder/Main.java Zobrazit soubor

23
         return result;
23
         return result;
24
     }
24
     }
25
 
25
 
26
-    public static void main(String[] args) throws ItemParseException{
26
+    public static void main(String[] args) {
27
         String output = (new Main()).readRawDataToString();
27
         String output = (new Main()).readRawDataToString();
28
         ItemParser parser = new ItemParser();
28
         ItemParser parser = new ItemParser();
29
         ArrayList<String> items = parser.parseRawDataIntoStringArray(output);
29
         ArrayList<String> items = parser.parseRawDataIntoStringArray(output);
30
+        int exceptionCount = 1;
30
         for (String item : items) {
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
       //  System.out.println(output);
42
       //  System.out.println(output);
40
         // TODO: parse the data in output into items, and display to console.
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 Zobrazit soubor

14
 
14
 
15
     private String rawSingleItemIrregularSeperatorSample = "naMe:MiLK;price:3.23;type:Food^expiration:1/11/2016##";
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
     private String rawMultipleItems = "naMe:Milk;price:3.23;type:Food;expiration:1/25/2016##"
19
     private String rawMultipleItems = "naMe:Milk;price:3.23;type:Food;expiration:1/25/2016##"
20
                                       +"naME:BreaD;price:1.23;type:Food;expiration:1/02/2016##"
20
                                       +"naME:BreaD;price:1.23;type:Food;expiration:1/02/2016##"