De'Jon Johnson 6 år sedan
förälder
incheckning
3bec1666d4

+ 2
- 0
src/main/java/io/zipcoder/ItemParseException.java Visa fil

@@ -1,4 +1,6 @@
1 1
 package io.zipcoder;
2 2
 
3 3
 public class ItemParseException extends Exception {
4
+
5
+
4 6
 }

+ 124
- 2
src/main/java/io/zipcoder/ItemParser.java Visa fil

@@ -1,7 +1,12 @@
1 1
 package io.zipcoder;
2 2
 
3
+import java.lang.reflect.Array;
3 4
 import java.util.ArrayList;
4 5
 import java.util.Arrays;
6
+import java.util.HashMap;
7
+import java.util.Map;
8
+import java.util.regex.Matcher;
9
+import java.util.regex.Pattern;
5 10
 
6 11
 public class ItemParser {
7 12
 
@@ -13,11 +18,18 @@ public class ItemParser {
13 18
     }
14 19
 
15 20
     public Item parseStringIntoItem(String rawItem) throws ItemParseException{
16
-        return null;
21
+
22
+        ArrayList<String> values = getValues(rawItem);
23
+
24
+        String name = values.get(0);
25
+        Double price = Double.valueOf(values.get(1));
26
+        String type = values.get(2);
27
+        String expiration = values.get(3);
28
+        return new Item(name, price, type, expiration);
17 29
     }
18 30
 
19 31
     public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){
20
-        String stringPattern = "[;|^]";
32
+        String stringPattern = "[^a-zA-Z0-9\\:\\.\\/]";
21 33
         ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawItem);
22 34
         return response;
23 35
     }
@@ -26,6 +38,116 @@ public class ItemParser {
26 38
         return new ArrayList<String>(Arrays.asList(inputString.split(stringPattern)));
27 39
     }
28 40
 
41
+    public String getValue(String input) throws ItemParseException{
42
+        ArrayList<String> kv = splitStringWithRegexPattern(":", input);
43
+        if(kv.size() < 2){
44
+            throw new ItemParseException();
45
+        } else if (kv.size()==2){
46
+            return kv.get(1);
47
+        }
48
+        return null;
49
+    }
50
+
51
+    public ArrayList<String> getValues(String rawItem) throws ItemParseException{
52
+        ArrayList <String> KVPairs = findKeyValuePairsInRawItemData(rawItem);
53
+        ArrayList<String> values = new ArrayList<String>();
54
+        for(String pair: KVPairs){
55
+            String value = getValue(toLowerCase(pair));
56
+            if(value.equals("")){
57
+                throw new ItemParseException();
58
+            } else{
59
+                values.add(value);
60
+            }
61
+        }
62
+        return values;
63
+    }
64
+
29 65
 
66
+    public String toLowerCase(String test){
67
+        //ArrayList<String> characters = new ArrayList<String>();
68
+
69
+        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"};
70
+        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"};
71
+
72
+
73
+        Pattern pattern = Pattern.compile(".");
74
+        Matcher matcher = pattern.matcher(test);
75
+        StringBuilder lowercase = new StringBuilder();
76
+
77
+        while(matcher.find()){
78
+            String character = matcher.group();
79
+            if(isUpper(character)){
80
+                int i = indexOf(upper, character);
81
+                lowercase.append(lower[i]);
82
+            } else {
83
+                lowercase.append(character);
84
+            }
85
+        }
86
+
87
+        return lowercase.toString();
88
+    }
89
+
90
+    public boolean isUpper(String c){
91
+        Pattern pattern = Pattern.compile("[A-Z]");
92
+        Matcher matcher = pattern.matcher(c);
93
+        return matcher.find();
94
+    }
95
+
96
+    public int indexOf(String[] array, String c){
97
+        for(int i = 0; i <array.length; i++){
98
+            if (array[i].equals(c)){
99
+                return i;
100
+            }
101
+        }
102
+        return -1;
103
+    }
104
+
105
+    public int findNumberOfErrors(String input) {
106
+        ArrayList<String> badFormat = new ArrayList<String>();
107
+        ArrayList<String> rawData = parseRawDataIntoStringArray(input);
108
+
109
+
110
+        for (String data : rawData) {
111
+            try {
112
+                Item item = parseStringIntoItem(data);
113
+            }catch (ItemParseException e) {
114
+                badFormat.add(data);
115
+            }
116
+        }
117
+
118
+        return badFormat.size();
119
+
120
+    }
121
+
122
+    public ArrayList<Item> findItems(String input){
123
+        ArrayList<Item> items = new ArrayList<Item>();
124
+        ArrayList<String> rawData = parseRawDataIntoStringArray(input);
125
+        for(String data : rawData) {
126
+            try {
127
+                Item item = parseStringIntoItem(data);
128
+                items.add(item);
129
+            }catch (ItemParseException e) {
130
+                e.printStackTrace();
131
+            }
132
+        }
133
+        return items;
134
+
135
+    }
136
+
137
+    public Map<String, Integer> getUniqueNames(ArrayList<Item> items) {
138
+        Map<String, Integer> uniqueNames = new HashMap<String, Integer>();
139
+
140
+        for (Item item : items) {
141
+            if (!uniqueNames.containsKey(item.getName())) {
142
+                uniqueNames.put(item.getName(), 1);
143
+            } else {
144
+                int amount = uniqueNames.get(item.getName());
145
+                amount++;
146
+                uniqueNames.put(item.getName(), amount);
147
+            }
148
+        }
149
+
150
+        return uniqueNames;
151
+    }
30 152
 
31 153
 }

+ 25
- 1
src/main/java/io/zipcoder/Main.java Visa fil

@@ -2,9 +2,16 @@ package io.zipcoder;
2 2
 
3 3
 import org.apache.commons.io.IOUtils;
4 4
 
5
+import java.lang.reflect.Array;
6
+import java.util.ArrayList;
7
+import java.util.HashMap;
8
+import java.util.Map;
9
+
5 10
 
6 11
 public class Main {
7 12
 
13
+    private static ItemParser itemParser = new ItemParser();
14
+
8 15
     public String readRawDataToString() throws Exception{
9 16
         ClassLoader classLoader = getClass().getClassLoader();
10 17
         String result = IOUtils.toString(classLoader.getResourceAsStream("RawData.txt"));
@@ -12,8 +19,25 @@ public class Main {
12 19
     }
13 20
 
14 21
     public static void main(String[] args) throws Exception{
22
+
15 23
         String output = (new Main()).readRawDataToString();
16
-        System.out.println(output);
24
+
25
+        int errors = itemParser.findNumberOfErrors(output);
26
+        ArrayList<Item> items = itemParser.findItems(output);
27
+
28
+        Map<String, Integer> uniqueNames = new HashMap<String, Integer>();
29
+
30
+        for(Item item :items){
31
+            if(!uniqueNames.containsKey(item.getName())){
32
+                uniqueNames.put(item.getName(), 1);
33
+            } else{
34
+                int amount = uniqueNames.get(item.getName());
35
+                amount = amount+1;
36
+                uniqueNames.put(item.getName(), amount);
37
+            }
38
+        }
39
+
40
+
17 41
         // TODO: parse the data in output into items, and display to console.
18 42
     }
19 43
 }

+ 28
- 1
src/main/resources/RawData.txt Visa fil

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

+ 16
- 1
src/test/java/io/zipcoder/ItemParserTest.java Visa fil

@@ -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##"
@@ -59,4 +59,19 @@ public class ItemParserTest {
59 59
         Integer actual = itemParser.findKeyValuePairsInRawItemData(rawSingleItemIrregularSeperatorSample).size();
60 60
         assertEquals(expected, actual);
61 61
     }
62
+
63
+    @Test
64
+    public void toLowerCase(){
65
+        String test = "MILK";
66
+        String expected = "milk";
67
+        String actual = itemParser.toLowerCase(test);
68
+        Assert.assertEquals(expected, actual);
69
+    }
70
+
71
+    @Test
72
+    public void isUpperTest(){
73
+        String test = "E";
74
+        Assert.assertTrue(itemParser.isUpper(test));
75
+    }
76
+
62 77
 }