Browse Source

completed jerkson lab

Jonathan Hinds 5 years ago
parent
commit
e6751553c6

+ 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>7</source>
17
+                    <target>7</target>
18
+                </configuration>
19
+            </plugin>
20
+        </plugins>
21
+    </build>
10 22
 
11 23
     <dependencies>
12 24
         <dependency>

+ 237
- 5
src/main/java/io/zipcoder/ItemParser.java View File

@@ -1,11 +1,13 @@
1 1
 package io.zipcoder;
2 2
 
3
-import java.util.ArrayList;
4
-import java.util.Arrays;
3
+import java.lang.reflect.Field;
4
+import java.lang.reflect.Method;
5
+import java.util.*;
6
+import java.util.regex.Matcher;
7
+import java.util.regex.Pattern;
5 8
 
6 9
 public class ItemParser {
7 10
 
8
-
9 11
     public ArrayList<String> parseRawDataIntoStringArray(String rawData){
10 12
         String stringPattern = "##";
11 13
         ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawData);
@@ -13,11 +15,18 @@ public class ItemParser {
13 15
     }
14 16
 
15 17
     public Item parseStringIntoItem(String rawItem) throws ItemParseException{
16
-        return null;
18
+        ArrayList<String> values = getValues(rawItem);
19
+
20
+        String name = uppercase(correctSpelling(values.get(0)));
21
+        Double price = Double.valueOf(values.get(1));
22
+        String type = uppercase(values.get(2));
23
+        String expiration = values.get(3);
24
+
25
+        return new Item(name, price, type, expiration);
17 26
     }
18 27
 
19 28
     public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){
20
-        String stringPattern = "[;|^]";
29
+        String stringPattern = "[^a-zA-Z0-9\\:\\.\\/]";
21 30
         ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawItem);
22 31
         return response;
23 32
     }
@@ -26,6 +35,229 @@ public class ItemParser {
26 35
         return new ArrayList<String>(Arrays.asList(inputString.split(stringPattern)));
27 36
     }
28 37
 
38
+    //keys - (.+)(?=:)
39
+    //values - (?<=:)(.+)
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 = toLowerCase(getValue(pair));
56
+            if(value.equals("")){
57
+                throw new ItemParseException();
58
+            } else{
59
+                values.add(value);
60
+            }
61
+        }
62
+        return values;
63
+    }
64
+
65
+    public String toLowerCase(String input) {
66
+        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"};
67
+        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"};
68
+
69
+        StringBuilder lowercase = new StringBuilder();
70
+
71
+        Pattern pattern = Pattern.compile(".");
72
+        Matcher matcher = pattern.matcher(input);
29 73
 
74
+        while(matcher.find()){
75
+            String character = matcher.group();
76
+            if(isUpper(character)){
77
+                int i = indexOf(upper, character);
78
+                lowercase.append(lower[i]);
79
+            }else {
80
+                lowercase.append(character);
81
+            }
82
+        }
83
+        return lowercase.toString();
84
+    }
85
+
86
+    public boolean isUpper(String c){
87
+        Pattern pattern = Pattern.compile("[A-Z]");
88
+        Matcher matcher = pattern.matcher(c);
89
+        return matcher.find();
90
+    }
30 91
 
92
+    public int indexOf(String[] array, String c){
93
+        for(int i = 0; i < array.length; i++){
94
+            if(array[i].equals(c)){
95
+                return i;
96
+            }
97
+        }
98
+        return -1;
99
+    }
100
+
101
+
102
+
103
+
104
+
105
+
106
+
107
+
108
+
109
+
110
+
111
+
112
+
113
+
114
+    public int findNumberOfErrors(String input){
115
+        ArrayList<String> badFormat = new ArrayList<String>();
116
+        ArrayList<String> rawData = parseRawDataIntoStringArray(input);
117
+        for(String data : rawData){
118
+            try {
119
+                Item item = parseStringIntoItem(data);
120
+            }catch(ItemParseException e){
121
+                badFormat.add(data);
122
+            }
123
+        }
124
+        return badFormat.size();
125
+    }
126
+
127
+    public ArrayList<Item> findItems(String input){
128
+        ArrayList<Item> items = new ArrayList<Item>();
129
+        ArrayList<String> rawData = parseRawDataIntoStringArray(input);
130
+        for(String data : rawData){
131
+            try {
132
+                Item item = parseStringIntoItem(data);
133
+                items.add(item);
134
+            }catch(ItemParseException e){
135
+                e.printStackTrace();
136
+            }
137
+        }
138
+        return items;
139
+    }
140
+
141
+    public Map<Object, Integer> getUniqueItemNames(ArrayList<Item> items, String methodname){
142
+
143
+        try {
144
+            Map<Object, Integer> uniqueNames = new HashMap<>();
145
+            for (Item item : items) {
146
+                Class<?> e = item.getClass();
147
+                Method method = e.getMethod(methodname);
148
+                if (!uniqueNames.containsKey(method.invoke(item))) {
149
+                    uniqueNames.put(method.invoke(item), new Integer(1));
150
+                } else {
151
+                    int amount = uniqueNames.get(method.invoke(item));
152
+                    amount = amount + 1;
153
+                    uniqueNames.put(method.invoke(item), amount);
154
+                }
155
+            }
156
+            return uniqueNames;
157
+
158
+        }catch(Exception e){
159
+            e.printStackTrace();
160
+        }
161
+        return null;
162
+    }
163
+
164
+    public String correctSpelling(String test) {
165
+        String[] nums = {"0", "1", "3"};
166
+        String[] alpha = {"o", "l", "e"};
167
+
168
+        StringBuilder lowercase = new StringBuilder();
169
+
170
+        Pattern pattern = Pattern.compile(".");
171
+        Matcher matcher = pattern.matcher(test);
172
+
173
+        while(matcher.find()){
174
+            String character = matcher.group();
175
+            int index = indexOf(nums, character);
176
+            if(index != -1){
177
+                lowercase.append(alpha[index]);
178
+            } else{
179
+                lowercase.append(character);
180
+            }
181
+        }
182
+        return lowercase.toString();
183
+    }
184
+
185
+    public Map<String, Map<Double, Integer>> sortItems(ArrayList<Item> items){
186
+        Map<String, Map<Double, Integer>> uniqueItem = new HashMap<>();
187
+        Map<Object, Integer> names = getUniqueItemNames(items, "getName");
188
+
189
+        for(Object key : names.keySet()){
190
+            Map<Double, Integer> prices = new HashMap<>();
191
+            for(Item item : items){
192
+                if(item.getName().equals(key)){
193
+                    if(prices.containsKey(item.getPrice())){
194
+                        int amount = prices.get(item.getPrice());
195
+                        amount = amount + 1;
196
+                        prices.put(item.getPrice(), amount);
197
+                    } else {
198
+                        prices.put(item.getPrice(), 1);
199
+                    }
200
+                }
201
+                uniqueItem.put((String)key, prices);
202
+            }
203
+        }
204
+
205
+        return uniqueItem;
206
+    }
207
+
208
+    public String uppercase(String test) {
209
+        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"};
210
+        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"};
211
+
212
+        ArrayList<String> answer = getCharList(test);
213
+
214
+        if(answer.size() > 0){
215
+            if(isUpper(answer.get(0))){
216
+                return listToString(answer);
217
+            } else {
218
+                int index = indexOf(lower, answer.get(0));
219
+                answer.remove(0);
220
+                answer.add(0, upper[index]);
221
+            }
222
+        }
223
+        return listToString(answer);
224
+    }
225
+
226
+    public ArrayList<String> getCharList(String input){
227
+        ArrayList<String> answer = new ArrayList<>();
228
+
229
+        Pattern pattern = Pattern.compile(".");
230
+        Matcher matcher = pattern.matcher(input);
231
+
232
+        while(matcher.find()){
233
+            answer.add(matcher.group());
234
+        }
235
+        return answer;
236
+    }
237
+
238
+    public String listToString(List<String> list){
239
+        StringBuilder stringBuilder = new StringBuilder();
240
+        for(String string : list){
241
+            stringBuilder.append(string);
242
+        }
243
+        return stringBuilder.toString();
244
+    }
245
+
246
+    public void print(String input){
247
+        int errs = findNumberOfErrors(input);
248
+        ArrayList<Item> items = findItems(input);
249
+        Map<Object, Integer> names = getUniqueItemNames(items, "getName");
250
+        Map<String, Map<Double, Integer>> uniqueItem = sortItems(items);
251
+
252
+        for(String name : uniqueItem.keySet()){
253
+            System.out.printf("name: %7s %20s \n", name, "seen: " + names.get(name) + " times");
254
+            System.out.println("=============     \t =============");
255
+            for(Double price : uniqueItem.get(name).keySet()){
256
+                System.out.printf("Price: %6s %20s \n", price, "seen: " + uniqueItem.get(name).get(price) + " times");
257
+            }
258
+            System.out.println("=============     \t =============");
259
+
260
+        }
261
+        System.out.printf("\nErrors %26s", "seen " + errs + " times" );
262
+    }
31 263
 }

+ 8
- 2
src/main/java/io/zipcoder/Main.java View File

@@ -2,9 +2,15 @@ package io.zipcoder;
2 2
 
3 3
 import org.apache.commons.io.IOUtils;
4 4
 
5
+import java.util.ArrayList;
6
+import java.util.HashMap;
7
+import java.util.Map;
8
+
5 9
 
6 10
 public class Main {
7 11
 
12
+    private static ItemParser itemParser = new ItemParser();
13
+
8 14
     public String readRawDataToString() throws Exception{
9 15
         ClassLoader classLoader = getClass().getClassLoader();
10 16
         String result = IOUtils.toString(classLoader.getResourceAsStream("RawData.txt"));
@@ -13,7 +19,7 @@ public class Main {
13 19
 
14 20
     public static void main(String[] args) throws Exception{
15 21
         String output = (new Main()).readRawDataToString();
16
-        System.out.println(output);
17
-        // TODO: parse the data in output into items, and display to console.
22
+
23
+        itemParser.print(output);
18 24
     }
19 25
 }

+ 28
- 0
src/main/resources/RawDataEdit View File

@@ -0,0 +1,28 @@
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##

+ 38
- 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##"
@@ -30,6 +30,9 @@ public class ItemParserTest {
30 30
     public void parseRawDataIntoStringArrayTest(){
31 31
         Integer expectedArraySize = 3;
32 32
         ArrayList<String> items = itemParser.parseRawDataIntoStringArray(rawMultipleItems);
33
+        for(String string : items){
34
+            System.out.println(string);
35
+        }
33 36
         Integer actualArraySize = items.size();
34 37
         assertEquals(expectedArraySize, actualArraySize);
35 38
     }
@@ -50,6 +53,7 @@ public class ItemParserTest {
50 53
     public void findKeyValuePairsInRawItemDataTest(){
51 54
         Integer expected = 4;
52 55
         Integer actual = itemParser.findKeyValuePairsInRawItemData(rawSingleItem).size();
56
+        System.out.println(itemParser.findKeyValuePairsInRawItemData(rawSingleItem));
53 57
         assertEquals(expected, actual);
54 58
     }
55 59
 
@@ -59,4 +63,37 @@ public class ItemParserTest {
59 63
         Integer actual = itemParser.findKeyValuePairsInRawItemData(rawSingleItemIrregularSeperatorSample).size();
60 64
         assertEquals(expected, actual);
61 65
     }
66
+
67
+    @Test
68
+    public void toLowerCaseTest(){
69
+        String test = "MILK";
70
+        String expect = "milk";
71
+        String actual = itemParser.toLowerCase(test);
72
+        Assert.assertEquals(expect, actual);
73
+    }
74
+
75
+    @Test
76
+    public void isUpperTest(){
77
+        String test = "E";
78
+        Assert.assertTrue(itemParser.isUpper(test));
79
+    }
80
+
81
+    @Test
82
+    public void changeNumToAlphaTest(){
83
+        String test = "c00kies";
84
+        String expect = "cookies";
85
+
86
+        String actual = itemParser.correctSpelling(test);
87
+        Assert.assertEquals(expect, actual);
88
+
89
+    }
90
+
91
+    @Test
92
+    public void changeUpperTest(){
93
+        String test = "cookies";
94
+        String expect = "Cookies";
95
+
96
+        String actual = itemParser.uppercase(test);
97
+        Assert.assertEquals(expect, actual);
98
+    }
62 99
 }