Browse Source

completed

Yesoda Sanka 6 years ago
parent
commit
d60afcb2e9

+ 12
- 0
pom.xml View File

@@ -7,6 +7,18 @@
7 7
     <groupId>io.zipcoder</groupId>
8 8
     <artifactId>PainfullAfternoon</artifactId>
9 9
     <version>1.0-SNAPSHOT</version>
10
+    <build>
11
+        <plugins>
12
+            <plugin>
13
+                <groupId>org.apache.maven.plugins</groupId>
14
+                <artifactId>maven-compiler-plugin</artifactId>
15
+                <configuration>
16
+                    <source>8</source>
17
+                    <target>8</target>
18
+                </configuration>
19
+            </plugin>
20
+        </plugins>
21
+    </build>
10 22
 
11 23
     <dependencies>
12 24
         <dependency>

+ 107
- 8
src/main/java/io/zipcoder/ItemParser.java View File

@@ -2,30 +2,129 @@ 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;
7
+import java.util.stream.Collectors;
8
+import java.util.stream.Stream;
5 9
 
6 10
 public class ItemParser {
7 11
 
8 12
 
9
-    public ArrayList<String> parseRawDataIntoStringArray(String rawData){
13
+    public ArrayList<String> parseRawDataIntoStringArray(String rawData) {
10 14
         String stringPattern = "##";
11
-        ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawData);
15
+        ArrayList<String> response = splitStringWithRegexPattern(stringPattern, rawData);
12 16
         return response;
13 17
     }
14 18
 
15
-    public Item parseStringIntoItem(String rawItem) throws ItemParseException{
16
-        return null;
19
+    public Item parseStringIntoItem(String rawItem) throws ItemParseException {
20
+
21
+
22
+        ArrayList<String> values = getValues(toLowerCase(rawItem ) );
23
+        String name = values.get(0);
24
+        Double price = Double.valueOf(values.get(1));
25
+        String type = values.get(2);
26
+        String expiration = values.get(3);
27
+
28
+
29
+        // return new Item(toLowerCase(rawItem );
30
+
31
+        return new Item(name, price, type, expiration);
17 32
     }
18 33
 
19
-    public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){
20
-        String stringPattern = "[;|^]";
21
-        ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawItem);
34
+    public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem) {
35
+        // String stringPattern = "[;|^]";
36
+        String stringPattern = "[^a-zA-Z0-9\\:\\.\\/]";
37
+        ArrayList<String> response = splitStringWithRegexPattern(stringPattern, rawItem);
22 38
         return response;
23 39
     }
24 40
 
25
-    private ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString){
41
+    private ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString) {
26 42
         return new ArrayList<String>(Arrays.asList(inputString.split(stringPattern)));
27 43
     }
28 44
 
29 45
 
30 46
 
47
+    public String getValue(String input) throws ItemParseException  {
48
+        ArrayList <String> kv=splitStringWithRegexPattern(":",input);
49
+        if(kv.size()<2 ){
50
+            throw new ItemParseException();
51
+        }else
52
+        {
53
+            if(kv.size()==2 ){
54
+                return kv.get(1);
55
+            }
56
+
57
+        }
58
+
59
+       // return splitStringWithRegexPattern(":", input).get(1);
60
+        return null;
61
+    }
62
+
63
+    public ArrayList<String> getValues(String rawItem) throws ItemParseException {
64
+        ArrayList<String> kvPairs = findKeyValuePairsInRawItemData(rawItem);
65
+        ArrayList<String> values = new ArrayList<String>();
66
+        for (String pair : kvPairs) {
67
+
68
+            String value = getValue(pair);
69
+            if (values.equals("")) {
70
+
71
+                throw new ItemParseException();
72
+
73
+            } else
74
+                values.add(value);
75
+
76
+
77
+        }
78
+        return values;
79
+
80
+    }
81
+
82
+    public String toLowerCase(String test){
83
+        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"};
84
+        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"};
85
+        StringBuilder lowercase=new StringBuilder();
86
+
87
+        Pattern pattern =Pattern.compile(".");
88
+        Matcher matcher=pattern.matcher(test);
89
+
90
+        while(matcher.find() ){
91
+            String character=matcher.group();
92
+            if(isUpper(character ) ) {
93
+                int i = indexOf(upper, character);
94
+                lowercase.append(lower[i]);
95
+            }else{
96
+                lowercase.append(character );
97
+            }
98
+
99
+        }
100
+        return lowercase.toString();
101
+    }
102
+    public boolean isUpper(String c){
103
+        Pattern pattern =Pattern.compile("[A-Z]") ;
104
+        Matcher matcher =pattern.matcher(c);
105
+        return matcher.find();
106
+    }
107
+    public int indexOf(String [] array,String c){
108
+        for(int i=0;i<array.length;i++ ){
109
+            if(array[i].equals(c) ){
110
+                return i;
111
+            }
112
+        }
113
+        return -1;
114
+    }
115
+
116
+//    public String lowercase(String test){
117
+//
118
+//
119
+//            StringBuffer stringbf = new StringBuffer();
120
+//            Matcher m = Pattern.compile(
121
+//                    "([a-z])([a-z]*)", Pattern.CASE_INSENSITIVE).matcher(test);
122
+//
123
+//            while (m.find()) {
124
+//                m.appendReplacement(
125
+//                        stringbf, m.group(1).toUpperCase() + m.group(2).toLowerCase());
126
+//            }
127
+//            return m.appendTail(stringbf).toString();
128
+//    }
129
+
31 130
 }

+ 20
- 0
src/main/java/io/zipcoder/Main.java View File

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

+ 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##

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

@@ -14,7 +14,8 @@ 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;3.23;type:Food;expiration:1/25/2016##";
18
+
18 19
 
19 20
     private String rawMultipleItems = "naMe:Milk;price:3.23;type:Food;expiration:1/25/2016##"
20 21
                                       +"naME:BreaD;price:1.23;type:Food;expiration:1/02/2016##"
@@ -59,4 +60,17 @@ public class ItemParserTest {
59 60
         Integer actual = itemParser.findKeyValuePairsInRawItemData(rawSingleItemIrregularSeperatorSample).size();
60 61
         assertEquals(expected, actual);
61 62
     }
63
+    @Test
64
+    public void toLowerCaseTest(){
65
+        String test="Milk";
66
+        String expected="milk";
67
+        String actual=itemParser.toLowerCase(test);
68
+       assertEquals(expected,actual );
69
+
70
+    }
71
+    @Test
72
+    public void isUpperTest(){
73
+        String test="E";
74
+        Assert.assertTrue(itemParser.isUpper(test) );
75
+    }
62 76
 }