Browse Source

Merge a2d75e8cd984e32b2f51f7324f4feaa2aef31513 into 23c2c01bf07b924ac9b860fa2771bf6ceb246ad5

Rikker 6 years ago
parent
commit
99f3966f14
No account linked to committer's email

+ 10
- 0
src/main/java/io/zipcoder/CostumException.java View File

1
+package io.zipcoder;
2
+
3
+public class CostumException extends Exception  {
4
+
5
+    static int errorCount = 0;
6
+
7
+    public CostumException(){
8
+        errorCount++;
9
+    }
10
+}

+ 5
- 3
src/main/java/io/zipcoder/Item.java View File

1
 package io.zipcoder;
1
 package io.zipcoder;
2
 
2
 
3
+import java.util.regex.Pattern;
4
+
3
 public class Item {
5
 public class Item {
4
     private String name;
6
     private String name;
5
     private Double price;
7
     private Double price;
22
         this.expiration = expiration;
24
         this.expiration = expiration;
23
     }
25
     }
24
 
26
 
27
+    public Item(String name, String price) {
28
+    }
29
+
25
     public String getName() {
30
     public String getName() {
26
         return name;
31
         return name;
27
     }
32
     }
28
 
33
 
29
-
30
     public Double getPrice() {
34
     public Double getPrice() {
31
         return price;
35
         return price;
32
     }
36
     }
33
 
37
 
34
-
35
     public String getType() {
38
     public String getType() {
36
         return type;
39
         return type;
37
     }
40
     }
38
 
41
 
39
-
40
     public String getExpiration() {
42
     public String getExpiration() {
41
         return expiration;
43
         return expiration;
42
     }
44
     }

+ 29
- 0
src/main/java/io/zipcoder/ItemListGenerator.java View File

1
+package io.zipcoder;
2
+
3
+import java.util.ArrayList;
4
+
5
+//public class ItemListGenerator {
6
+//
7
+//    ItemParser itemParser;
8
+//
9
+//    public ItemListGenerator(ItemParser itemParser) {
10
+//        this.itemParser = itemParser;
11
+//    }
12
+//
13
+//    public Item generateItems(String input){
14
+//      ArrayList<String> inputSeparatedByNewLine = itemParser.parseRawDataIntoStringArray(input);
15
+//      String[] splitString = input.split(input);
16
+//
17
+//
18
+//            String name = "Milk";
19
+//            Double price = 1.23;
20
+//            String type = "Food";
21
+//            String expiration = "1/25/2016";
22
+//            Item item = new Item(splitString);
23
+//
24
+//        return item;
25
+//    }
26
+//
27
+//
28
+//}
29
+

+ 131
- 3
src/main/java/io/zipcoder/ItemParser.java View File

1
 package io.zipcoder;
1
 package io.zipcoder;
2
 
2
 
3
+
3
 import java.util.ArrayList;
4
 import java.util.ArrayList;
4
 import java.util.Arrays;
5
 import java.util.Arrays;
6
+import java.util.regex.Matcher;
7
+import java.util.regex.Pattern;
5
 
8
 
6
 public class ItemParser {
9
 public class ItemParser {
7
 
10
 
8
 
11
 
12
+    private String name;
13
+    private Double price;
14
+    private String type;
15
+    private String expiration;
16
+
9
     public ArrayList<String> parseRawDataIntoStringArray(String rawData){
17
     public ArrayList<String> parseRawDataIntoStringArray(String rawData){
10
         String stringPattern = "##";
18
         String stringPattern = "##";
11
-        ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawData);
19
+        ArrayList<String> response = splitStringWithRegexPattern(stringPattern , String.valueOf(rawData));
12
         return response;
20
         return response;
13
     }
21
     }
14
 
22
 
15
     public Item parseStringIntoItem(String rawItem) throws ItemParseException{
23
     public Item parseStringIntoItem(String rawItem) throws ItemParseException{
16
-        return null;
24
+        rawItem = rawItem.replaceAll("#", "");
25
+        ArrayList<String> keyValuePairsInRawItemData = findKeyValuePairsInRawItemData(rawItem);
26
+       // System.out.println(keyValuePairsInRawItemData);
27
+        for (String pair: keyValuePairsInRawItemData){
28
+            //System.out.println(pair);
29
+            if (pair.matches("[nameNAME]{4}"))
30
+                name = this.name(pair);
31
+            System.out.println(pair);
32
+            if(pair.matches("[pricePrice]{5}]"))
33
+                price = this.price(pair);
34
+            System.out.println(pair);
35
+            if(pair.matches("[type]{4}"))
36
+                type = this.type(pair);
37
+            System.out.println(pair);
38
+            if(pair.matches("[expriationEXPIRATION]{10}"))
39
+                expiration = this.expiration(pair);
40
+            System.out.println(pair);
41
+        }
42
+       return new Item(name, price, type, expiration);
43
+        //return name;
17
     }
44
     }
18
 
45
 
46
+
19
     public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){
47
     public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){
20
-        String stringPattern = "[;|^]";
48
+        String stringPattern = "[;|^|%|*|!|@]";
21
         ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawItem);
49
         ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawItem);
22
         return response;
50
         return response;
23
     }
51
     }
26
         return new ArrayList<String>(Arrays.asList(inputString.split(stringPattern)));
54
         return new ArrayList<String>(Arrays.asList(inputString.split(stringPattern)));
27
     }
55
     }
28
 
56
 
57
+     public String name(String value){
58
+        String name = null;
59
+        value = value.replaceAll("[~(nameNAME)~g]", "");
60
+            if (value.matches("[milkMILK]{4}")){
61
+                name= "Milk";
62
+            }
63
+            if (value.matches("[breadBREAD]{5}")){
64
+                name = "Bread";
65
+            }
66
+            if (value.matches("[cC]..[kK][iI][eE][sS]")){
67
+                name = "Cookies";
68
+            }
69
+            if (value.matches("[@applesAPPLES]{6}")){
70
+                name = "Apples";
71
+            }
72
+            return name;
73
+        }
74
+
75
+
76
+    public Double price(String value){
77
+        Double price = null;
78
+        Pattern pattern = Pattern.compile("\\d.\\d+(?=;)");
79
+        Matcher matcher = pattern.matcher(value);
80
+        if(matcher.find()){
81
+            price = Double.parseDouble(matcher.group(0));
82
+        }
83
+        return price;
84
+    }
85
+
86
+    public String type(String value){
87
+        String type = null;
88
+        value = value.replaceAll("[type]{4}", "");
89
+        if(value.matches("[fodFOD]{4}")) {
90
+            type = "Food";
91
+        }
92
+        System.out.println(type);
93
+        return type;
94
+    }
95
+
96
+    public String expiration(String value){
97
+        String expiration = null;
98
+        Pattern pattern = Pattern.compile("(\\d/\\d+/\\d+)");
99
+        Matcher matcher = pattern.matcher(value);
100
+        if (matcher.find()){
101
+            expiration = matcher.group();
102
+        }
103
+        return expiration;
104
+    }
105
+
29
 
106
 
107
+//    public String milk(String milk){
108
+//        Pattern pattern = Pattern.compile("[milkMILK]{4}");
109
+//        String output = "";
110
+//        Matcher matcher = pattern.matcher(milk);
111
+//        while (matcher.find()){
112
+//            output = matcher.group(0);
113
+//        }
114
+//        return output;
115
+//    }
116
+//
117
+//    public String bread(String bread){
118
+//        Pattern pattern = Pattern.compile("[breadBREAD]{5}");
119
+//        String output = "";
120
+//        Matcher matcher = pattern.matcher(bread);
121
+//        while (matcher.find()){
122
+//            output = matcher.group(0);
123
+//        }
124
+//        return output;
125
+//    }
126
+//
127
+//    public String cookies(String cookies){
128
+//        Pattern pattern = Pattern.compile("[cC]..[kK][iI][eE][sS]");
129
+//        String output = "";
130
+//        Matcher matcher = pattern.matcher(cookies);
131
+//        while (matcher.find()){
132
+//            output = matcher.group(0);
133
+//        }
134
+//        return output;
135
+//    }
136
+//
137
+//    public String apples(String apples){
138
+//        Pattern pattern = Pattern.compile("[@applesAPPLES]{6}");
139
+//        String output = "";
140
+//        Matcher matcher = pattern.matcher(apples);
141
+//        while (matcher.find()){
142
+//            output = matcher.group(0);
143
+//        }
144
+//        return output;
145
+//    }
146
+
147
+//    public String food(String food){
148
+//        Pattern pattern = Pattern.compile("([0fodFOD]{4})+.");
149
+//        String output = "";
150
+//        Matcher matcher = pattern.matcher(food);
151
+//        while (matcher.find()){
152
+//            output = matcher.group(0);
153
+//        }
154
+//        return output;
155
+//    }
30
 
156
 
31
 }
157
 }
158
+
159
+

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

2
 
2
 
3
 import org.apache.commons.io.IOUtils;
3
 import org.apache.commons.io.IOUtils;
4
 
4
 
5
+import java.util.ArrayList;
6
+
5
 
7
 
6
 public class Main {
8
 public class Main {
7
 
9
 
13
 
15
 
14
     public static void main(String[] args) throws Exception{
16
     public static void main(String[] args) throws Exception{
15
         String output = (new Main()).readRawDataToString();
17
         String output = (new Main()).readRawDataToString();
16
-        System.out.println(output);
18
+        //System.out.println(output);
19
+
20
+        ItemParser itemParser = new ItemParser();
21
+        //ArrayList<String> outputSeparated = itemParser.parseRawDataIntoStringArray(output);
22
+//
23
+//        for (String itemString: outputSeparated){
24
+//            System.out.println(itemString);
25
+//        }
26
+
17
         // TODO: parse the data in output into items, and display to console.
27
         // TODO: parse the data in output into items, and display to console.
28
+        ArrayList<String> items = itemParser.parseRawDataIntoStringArray(output);
29
+//        ArrayList<String> items = itemParser.findKeyValuePairsInRawItemData(test);
30
+//        for (String item : items){
31
+//            System.out.println(item);
32
+//        }
33
+
34
+        ArrayList<ArrayList<String>> eachItem = new ArrayList<>();
35
+
36
+        for (String item : items){
37
+            ArrayList<String> itemArray = itemParser.findKeyValuePairsInRawItemData(item);
38
+            eachItem.add(itemArray);
39
+        }
40
+
41
+        for (ArrayList<String> itemDetail : eachItem){
42
+            System.out.println(itemDetail);
43
+        }
44
+
45
+
46
+        //System.out.println(test);
18
     }
47
     }
19
 }
48
 }

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

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##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/a2016##

+ 29
- 0
src/test/java/io/zipcoder/ItemListGeneratorTest.java View File

1
+package io.zipcoder;
2
+
3
+
4
+import org.junit.Assert;
5
+import org.junit.Test;
6
+
7
+import static org.junit.Assert.assertEquals;
8
+
9
+public class ItemListGeneratorTest {
10
+
11
+//    @Test
12
+//    public void generateItemsTest(){
13
+//        String input = "naMe:Milk;price:3.23;type:Food;expiration:1/25/2016##";
14
+//        String expectedName = "Milk";
15
+//        Double expectedPrice = 1.23;
16
+//        String expectedType = "Food";
17
+//        String expirationDate = "1/25/2016";
18
+//        ItemParser itemParser = new ItemParser();
19
+//        ItemListGenerator itemListGenerator = new ItemListGenerator(itemParser);
20
+//
21
+//        Item actual = itemListGenerator.generateItems(input);
22
+//
23
+//        assertEquals(expectedName, actual.getName());
24
+//        assertEquals(expectedPrice, actual.getPrice());
25
+//        assertEquals(expectedType, actual.getType());
26
+//        assertEquals(expirationDate, actual.getExpiration());
27
+//
28
+//    }
29
+}

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

37
     @Test
37
     @Test
38
     public void parseStringIntoItemTest() throws ItemParseException{
38
     public void parseStringIntoItemTest() throws ItemParseException{
39
         Item expected = new Item("milk", 3.23, "food","1/25/2016");
39
         Item expected = new Item("milk", 3.23, "food","1/25/2016");
40
-        Item actual = itemParser.parseStringIntoItem(rawSingleItem);
40
+        Item actual = itemParser.parseStringIntoItem(rawMultipleItems);
41
         assertEquals(expected.toString(), actual.toString());
41
         assertEquals(expected.toString(), actual.toString());
42
     }
42
     }
43
 
43
 
44
     @Test(expected = ItemParseException.class)
44
     @Test(expected = ItemParseException.class)
45
     public void parseBrokenStringIntoItemTest() throws ItemParseException{
45
     public void parseBrokenStringIntoItemTest() throws ItemParseException{
46
-        itemParser.parseStringIntoItem(rawBrokenSingleItem);
46
+        itemParser.parseStringIntoItem(rawSingleItem);
47
     }
47
     }
48
 
48
 
49
     @Test
49
     @Test
59
         Integer actual = itemParser.findKeyValuePairsInRawItemData(rawSingleItemIrregularSeperatorSample).size();
59
         Integer actual = itemParser.findKeyValuePairsInRawItemData(rawSingleItemIrregularSeperatorSample).size();
60
         assertEquals(expected, actual);
60
         assertEquals(expected, actual);
61
     }
61
     }
62
+
63
+    @Test
64
+    public void MyStringToItemTest() throws ItemParseException{
65
+        String item = "naMe:Milk;price:3.23;type:Food;expiration:1/25/2016";
66
+        String expected = "";
67
+        Item actual = itemParser.parseStringIntoItem(item);
68
+        Assert.assertEquals(expected, actual);
69
+    }
70
+
71
+    @Test
72
+    public void nameTest() throws ItemParseException {
73
+        //String name = "naMe:apples;price:3.23;type:Food;expiration:1/25/2016";
74
+        ArrayList<String> keyValuePairsInRawItemData = itemParser.findKeyValuePairsInRawItemData(rawMultipleItems);
75
+        String item = keyValuePairsInRawItemData.get(0);
76
+        String expected = "";
77
+        String actual = itemParser.name(item);
78
+        System.out.println(item);
79
+
80
+        Assert.assertEquals(expected , actual);
81
+    }
82
+
83
+    @Test
84
+    public void priceTest(){
85
+        Double expected = 3.23;
86
+        Double actual = itemParser.price(rawSingleItem);
87
+        Assert.assertEquals(expected, actual);
88
+    }
89
+
90
+    @Test
91
+    public void typeTest(){
92
+        String name = "naMe:apples;price:3.23;type:Food;expiration:1/25/2016";
93
+
94
+        //ArrayList<String> keyValuePairsInRawItemData = itemParser.findKeyValuePairsInRawItemData(name);
95
+        //String item = keyValuePairsInRawItemData.get(2);
96
+        String expected = "Fod";
97
+        String actual = itemParser.type(name);
98
+        System.out.println(actual);
99
+        Assert.assertEquals(expected, actual);
100
+    }
101
+
102
+    @Test
103
+    public void expirationTest(){
104
+        String expected = "";
105
+        String actual = itemParser.expiration(rawMultipleItems);
106
+        Assert.assertEquals(expected, actual);
107
+    }
62
 }
108
 }