Nuridalia.Hernandez 6 gadus atpakaļ
vecāks
revīzija
4e795686d6

+ 1
- 0
src/main/java/io/zipcoder/ItemParseException.java Parādīt failu

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

+ 155
- 9
src/main/java/io/zipcoder/ItemParser.java Parādīt failu

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

+ 15
- 1
src/main/java/io/zipcoder/Main.java Parādīt failu

2
 
2
 
3
 import org.apache.commons.io.IOUtils;
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
 public class Main {
10
 public class Main {
11
+    private static ItemParser itemParser = new ItemParser();
7
 
12
 
8
     public String readRawDataToString() throws Exception{
13
     public String readRawDataToString() throws Exception{
14
+
9
         ClassLoader classLoader = getClass().getClassLoader();
15
         ClassLoader classLoader = getClass().getClassLoader();
10
         String result = IOUtils.toString(classLoader.getResourceAsStream("RawData.txt"));
16
         String result = IOUtils.toString(classLoader.getResourceAsStream("RawData.txt"));
11
         return result;
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
         String output = (new Main()).readRawDataToString();
22
         String output = (new Main()).readRawDataToString();
16
-        System.out.println(output);
23
+        int err= itemParser.findNuberOfErrors(output);
24
+        ArrayList <Item>  items= itemParser.findItems(output);
25
+
26
+
27
+
28
+
29
+
30
+
17
         // TODO: parse the data in output into items, and display to console.
31
         // TODO: parse the data in output into items, and display to console.
18
     }
32
     }
19
 }
33
 }

+ 18
- 1
src/test/java/io/zipcoder/ItemParserTest.java Parādīt failu

14
 
14
 
15
     private String rawSingleItemIrregularSeperatorSample = "naMe:MiLK;price:3.23;type:Food^expiration:1/11/2016##";
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
     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:1/02/2016##"
46
         itemParser.parseStringIntoItem(rawBrokenSingleItem);
47
         itemParser.parseStringIntoItem(rawBrokenSingleItem);
47
     }
48
     }
48
 
49
 
50
+
49
     @Test
51
     @Test
50
     public void findKeyValuePairsInRawItemDataTest(){
52
     public void findKeyValuePairsInRawItemDataTest(){
51
         Integer expected = 4;
53
         Integer expected = 4;
59
         Integer actual = itemParser.findKeyValuePairsInRawItemData(rawSingleItemIrregularSeperatorSample).size();
61
         Integer actual = itemParser.findKeyValuePairsInRawItemData(rawSingleItemIrregularSeperatorSample).size();
60
         assertEquals(expected, actual);
62
         assertEquals(expected, actual);
61
     }
63
     }
64
+    @Test
65
+    public void toLowercase(){
66
+        String test = "MILK";
67
+        String expected = "milk";
68
+
69
+        String actual = itemParser.toLowerCase(test);
70
+        Assert.assertEquals(expected,actual);
71
+    }
72
+
73
+    @Test
74
+    public void isUpperTest(){
75
+        String test = "Y";
76
+
77
+        Assert.assertTrue(itemParser.isUpper(test));
78
+    }
62
 }
79
 }