thulasi 5 years ago
parent
commit
a706b79ec5

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

@@ -6,6 +6,7 @@ public class Item {
6 6
     private String type;
7 7
     private String expiration;
8 8
 
9
+
9 10
     /**
10 11
      * Item should not be created unless you have all of the elements, which is why you are forcing
11 12
      * it to be set in the constructor. In ItemParser, if you do not find all the elements of a Item,

+ 82
- 2
src/main/java/io/zipcoder/ItemParser.java View File

@@ -2,6 +2,8 @@ package io.zipcoder;
2 2
 
3 3
 import java.util.ArrayList;
4 4
 import java.util.Arrays;
5
+import java.util.regex.Matcher;
6
+import java.util.regex.Pattern;
5 7
 
6 8
 public class ItemParser {
7 9
 
@@ -13,11 +15,20 @@ public class ItemParser {
13 15
     }
14 16
 
15 17
     public Item parseStringIntoItem(String rawItem) throws ItemParseException{
16
-        return null;
18
+        ArrayList<String> values = getValues(toLowerCase(rawItem ) );
19
+        String name = values.get(0);
20
+        Double price = Double.valueOf(values.get(1));
21
+        String type = values.get(2);
22
+        String expiration = values.get(3);
23
+
24
+
25
+        // return new Item(toLowerCase(rawItem );
26
+
27
+        return new Item(name, price, type, expiration);
17 28
     }
18 29
 
19 30
     public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){
20
-        String stringPattern = "[;|^]";
31
+        String stringPattern = "[^a-zA-Z0-9\\:\\.\\/]";
21 32
         ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawItem);
22 33
         return response;
23 34
     }
@@ -26,6 +37,75 @@ public class ItemParser {
26 37
         return new ArrayList<String>(Arrays.asList(inputString.split(stringPattern)));
27 38
     }
28 39
 
40
+    public String getValue(String input) throws ItemParseException  {
41
+        ArrayList <String> kv=splitStringWithRegexPattern(":",input);
42
+        if(kv.size()<2 ){
43
+            throw new ItemParseException();
44
+        }else
45
+        {
46
+            if(kv.size()==2 ){
47
+                return kv.get(1);
48
+            }
49
+
50
+        }
51
+
52
+        // return splitStringWithRegexPattern(":", input).get(1);
53
+        return null;
54
+    }
55
+
56
+    public ArrayList<String> getValues(String rawItem) throws ItemParseException{
57
+        ArrayList<String> kvPairs = findKeyValuePairsInRawItemData(rawItem);
58
+        ArrayList<String> values = new ArrayList<String>();
59
+        for (String pair : kvPairs) {
60
+
61
+            String value = getValue(pair);
62
+            if (values.equals("")) {
63
+
64
+                throw new ItemParseException();
65
+
66
+            } else
67
+                values.add(value);
68
+
69
+
70
+        }
71
+        return values;
72
+    }
73
+
74
+
75
+  public boolean isUpper(String c){
76
+      Pattern pattern = Pattern.compile("[A-Z]");
77
+      Matcher matcher = pattern.matcher(c);
78
+      return matcher.find();
79
+  }
80
+
81
+  public String toLowerCase(String test){
82
+      String[] lower={"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
83
+      String[] upper = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
84
+      StringBuilder lowercase=new StringBuilder();
85
+
86
+      Pattern pattern =Pattern.compile(".");
87
+      Matcher matcher=pattern.matcher(test);
88
+
89
+      while(matcher.find() ){
90
+          String character=matcher.group();
91
+          if(isUpper(character ) ) {
92
+              int i = indexOf(upper, character);
93
+              lowercase.append(lower[i]);
94
+          }else{
95
+              lowercase.append(character );
96
+          }
97
+
98
+      }
99
+      return lowercase.toString();
100
+  }
29 101
 
102
+  public int indexOf(String[] array, String c){
103
+      for(int i=0;i<array.length;i++ ){
104
+          if(array[i].equals(c) ){
105
+              return i;
106
+          }
107
+      }
108
+      return -1;
109
+  }
30 110
 
31 111
 }

+ 28
- 1
src/main/resources/RawData.txt View File

@@ -1 +1,28 @@
1
-naMe:Milk;price:3.23;type:Food;expiration:1/25/2016##naME:BreaD;price:1.23;type:Food;expiration:1/02/2016##NAMe:BrEAD;price:1.23;type:Food;expiration:2/25/2016##naMe:MiLK;price:3.23;type:Food^expiration:1/11/2016##naMe:Cookies;price:2.25;type:Food%expiration:1/25/2016##naMe:CoOkieS;price:2.25;type:Food*expiration:1/25/2016##naMe:COokIes;price:2.25;type:Food;expiration:3/22/2016##naMe:COOkieS;price:2.25;type:Food;expiration:1/25/2016##NAME:MilK;price:3.23;type:Food;expiration:1/17/2016##naMe:MilK;price:1.23;type:Food!expiration:4/25/2016##naMe:apPles;price:0.25;type:Food;expiration:1/23/2016##naMe:apPles;price:0.23;type:Food;expiration:5/02/2016##NAMe:BrEAD;price:1.23;type:Food;expiration:1/25/2016##naMe:;price:3.23;type:Food;expiration:1/04/2016##naMe:Milk;price:3.23;type:Food;expiration:1/25/2016##naME:BreaD;price:1.23;type:Food@expiration:1/02/2016##NAMe:BrEAD;price:1.23;type:Food@expiration:2/25/2016##naMe:MiLK;priCe:;type:Food;expiration:1/11/2016##naMe:Cookies;price:2.25;type:Food;expiration:1/25/2016##naMe:Co0kieS;pRice:2.25;type:Food;expiration:1/25/2016##naMe:COokIes;price:2.25;type:Food;expiration:3/22/2016##naMe:COOkieS;Price:2.25;type:Food;expiration:1/25/2016##NAME:MilK;price:3.23;type:Food;expiration:1/17/2016##naMe:MilK;priCe:;type:Food;expiration:4/25/2016##naMe:apPles;prIce:0.25;type:Food;expiration:1/23/2016##naMe:apPles;pRice:0.23;type:Food;expiration:5/02/2016##NAMe:BrEAD;price:1.23;type:Food;expiration:1/25/2016##naMe:;price:3.23;type:Food^expiration:1/04/2016##
1
+naMe:Milk;price:3.23;type:Food;expiration:1/25/2016##
2
+naME:BreaD;price:1.23;type:Food;expiration:1/02/2016##
3
+NAMe:BrEAD;price:1.23;type:Food;expiration:2/25/2016##
4
+naMe:MiLK;price:3.23;type:Food^expiration:1/11/2016##
5
+naMe:Cookies;price:2.25;type:Food%expiration:1/25/2016##
6
+naMe:CoOkieS;price:2.25;type:Food*expiration:1/25/2016##
7
+naMe:COokIes;price:2.25;type:Food;expiration:3/22/2016##
8
+naMe:COOkieS;price:2.25;type:Food;expiration:1/25/2016##
9
+NAME:MilK;price:3.23;type:Food;expiration:1/17/2016##
10
+naMe:MilK;price:1.23;type:Food!expiration:4/25/2016##
11
+naMe:apPles;price:0.25;type:Food;expiration:1/23/2016##
12
+naMe:apPles;price:0.23;type:Food;expiration:5/02/2016##
13
+NAMe:BrEAD;price:1.23;type:Food;expiration:1/25/2016##
14
+naMe:;price:3.23;type:Food;expiration:1/04/2016##
15
+naMe:Milk;price:3.23;type:Food;expiration:1/25/2016##
16
+naME:BreaD;price:1.23;type:Food@expiration:1/02/2016##
17
+NAMe:BrEAD;price:1.23;type:Food@expiration:2/25/2016##
18
+naMe:MiLK;priCe:;type:Food;expiration:1/11/2016##
19
+naMe:Cookies;price:2.25;type:Food;expiration:1/25/2016##
20
+naMe:Co0kieS;pRice:2.25;type:Food;expiration:1/25/2016##
21
+naMe:COokIes;price:2.25;type:Food;expiration:3/22/2016##
22
+naMe:COOkieS;Price:2.25;type:Food;expiration:1/25/2016##
23
+NAME:MilK;price:3.23;type:Food;expiration:1/17/2016##
24
+naMe:MilK;priCe:;type:Food;expiration:4/25/2016##
25
+naMe:apPles;prIce:0.25;type:Food;expiration:1/23/2016##
26
+naMe:apPles;pRice:0.23;type:Food;expiration:5/02/2016##
27
+NAMe:BrEAD;price:1.23;type:Food;expiration:1/25/2016##
28
+naMe:;price:3.23;type:Food^expiration:1/04/2016##

+ 1
- 1
src/test/java/io/zipcoder/ItemParserTest.java View File

@@ -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:;price:3.23;type:Food;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##"