Przeglądaj źródła

methods written tests passed, working on formatting output

jacob andersen 6 lat temu
rodzic
commit
84d7208b40

+ 140
- 9
src/main/java/io/zipcoder/ItemParser.java Wyświetl plik

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

+ 13
- 5
src/main/java/io/zipcoder/Main.java Wyświetl plik

@@ -2,18 +2,26 @@ 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"));
11 17
         return result;
12 18
     }
13 19
 
14
-    public static void main(String[] args) throws Exception{
20
+    public static void main(String[] args) throws Exception
21
+    {
15 22
         String output = (new Main()).readRawDataToString();
16
-        System.out.println(output);
17
-        // TODO: parse the data in output into items, and display to console.
18
-    }
19
-}
23
+
24
+        int errs = itemParser.findNumberOfErrors(output);
25
+        ArrayList<Item> items = itemParser.findItems(output);
26
+            }
27
+        }

+ 26
- 11
src/test/java/io/zipcoder/ItemParserTest.java Wyświetl plik

@@ -10,24 +10,24 @@ import static org.junit.Assert.*;
10 10
 
11 11
 public class ItemParserTest {
12 12
 
13
-    private String rawSingleItem =    "naMe:Milk;price:3.23;type:Food;expiration:1/25/2016##";
13
+    private String rawSingleItem = "naMe:Milk;price:3.23;type:Food;expiration:1/25/2016##";
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
-                                      +"naME:BreaD;price:1.23;type:Food;expiration:1/02/2016##"
21
-                                      +"NAMe:BrEAD;price:1.23;type:Food;expiration:2/25/2016##";
20
+            + "naME:BreaD;price:1.23;type:Food;expiration:1/02/2016##"
21
+            + "NAMe:BrEAD;price:1.23;type:Food;expiration:2/25/2016##";
22 22
     private ItemParser itemParser;
23 23
 
24 24
     @Before
25
-    public void setUp(){
25
+    public void setUp() {
26 26
         itemParser = new ItemParser();
27 27
     }
28 28
 
29 29
     @Test
30
-    public void parseRawDataIntoStringArrayTest(){
30
+    public void parseRawDataIntoStringArrayTest() {
31 31
         Integer expectedArraySize = 3;
32 32
         ArrayList<String> items = itemParser.parseRawDataIntoStringArray(rawMultipleItems);
33 33
         Integer actualArraySize = items.size();
@@ -35,28 +35,43 @@ public class ItemParserTest {
35 35
     }
36 36
 
37 37
     @Test
38
-    public void parseStringIntoItemTest() throws ItemParseException{
39
-        Item expected = new Item("milk", 3.23, "food","1/25/2016");
38
+    public void parseStringIntoItemTest() throws ItemParseException {
39
+        Item expected = new Item("milk", 3.23, "food", "1/25/2016");
40 40
         Item actual = itemParser.parseStringIntoItem(rawSingleItem);
41 41
         assertEquals(expected.toString(), actual.toString());
42 42
     }
43 43
 
44 44
     @Test(expected = ItemParseException.class)
45
-    public void parseBrokenStringIntoItemTest() throws ItemParseException{
45
+    public void parseBrokenStringIntoItemTest() throws ItemParseException {
46 46
         itemParser.parseStringIntoItem(rawBrokenSingleItem);
47 47
     }
48 48
 
49 49
     @Test
50
-    public void findKeyValuePairsInRawItemDataTest(){
50
+    public void findKeyValuePairsInRawItemDataTest() {
51 51
         Integer expected = 4;
52 52
         Integer actual = itemParser.findKeyValuePairsInRawItemData(rawSingleItem).size();
53 53
         assertEquals(expected, actual);
54 54
     }
55 55
 
56 56
     @Test
57
-    public void findKeyValuePairsInRawItemDataTestIrregular(){
57
+    public void findKeyValuePairsInRawItemDataTestIrregular() {
58 58
         Integer expected = 4;
59 59
         Integer actual = itemParser.findKeyValuePairsInRawItemData(rawSingleItemIrregularSeperatorSample).size();
60 60
         assertEquals(expected, actual);
61 61
     }
62
+    @Test
63
+    public void toLowerCaseTest() {
64
+        String test = "MILK";
65
+        String expect = "milk";
66
+        String actual = itemParser.toLowerCase(test);
67
+        Assert.assertEquals(expect, actual);
68
+
69
+
70
+    }
71
+
72
+    @Test
73
+    public void isUpperTest() {
74
+        String test = "E";
75
+        Assert.assertTrue(itemParser.isUpper(test));
76
+    }
62 77
 }