Bo Lee 6 лет назад
Родитель
Сommit
f83bdd8b1f

+ 159
- 12
src/main/java/io/zipcoder/ItemParser.java Просмотреть файл

@@ -1,31 +1,178 @@
1 1
 package io.zipcoder;
2 2
 
3
-import java.util.ArrayList;
4
-import java.util.Arrays;
3
+import com.sun.org.apache.xerces.internal.impl.xpath.regex.Match;
4
+import com.sun.xml.internal.bind.v2.runtime.reflect.Lister;
5
+
6
+import java.lang.reflect.Array;
7
+import java.util.*;
8
+import java.util.regex.Matcher;
9
+import java.util.regex.Pattern;
5 10
 
6 11
 public class ItemParser {
7 12
 
8 13
 
9
-    public ArrayList<String> parseRawDataIntoStringArray(String rawData){
14
+    private ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString) {
15
+        return new ArrayList<String>(Arrays.asList(inputString.split(stringPattern)));
16
+    }
17
+
18
+    public ArrayList<String> parseRawDataIntoStringArray(String rawData) {
10 19
         String stringPattern = "##";
11
-        ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawData);
20
+        ArrayList<String> response = splitStringWithRegexPattern(stringPattern, rawData);
12 21
         return response;
13 22
     }
14 23
 
15
-    public Item parseStringIntoItem(String rawItem) throws ItemParseException{
16
-        return null;
24
+    public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem) {
25
+        String stringPattern = "[@|^|*|%|!|;]";
26
+        ArrayList<String> response = splitStringWithRegexPattern(stringPattern, rawItem);
27
+        return response;
17 28
     }
18 29
 
19
-    public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){
20
-        String stringPattern = "[;|^]";
21
-        ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawItem);
22
-        return response;
30
+    public Item parseStringIntoItem(String rawItem) throws ItemParseException {
31
+
32
+        String name = checkName(rawItem);
33
+        Double price = Double.valueOf(checkPrice(rawItem));
34
+        String type = checkType(rawItem);
35
+        String expiration = checkExpiration(rawItem);
36
+
37
+        return new Item(name, price, type, expiration);
38
+
23 39
     }
24 40
 
25
-    private ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString){
26
-        return new ArrayList<String>(Arrays.asList(inputString.split(stringPattern)));
41
+    public ArrayList<Item> findRawItemToArrayList(String rawData) throws ItemParseException {
42
+        ArrayList<String> temp = parseRawDataIntoStringArray(rawData);
43
+        ArrayList<Item> itemArrayList = new ArrayList<Item>();
44
+        for (int i = 0; i < temp.size(); i++) {
45
+            itemArrayList.add(parseStringIntoItem(temp.get(i)));
46
+        }
47
+        return itemArrayList;
48
+    }
49
+
50
+
51
+    public HashMap<String, Integer> findBasedOnName(String rawData, String whatToSearch) throws ItemParseException {
52
+        HashMap<String, Integer> hashMap = new HashMap<String, Integer>();
53
+        ArrayList<Item> tempItems = findRawItemToArrayList(rawData);
54
+        for (int i = 0; i < tempItems.size(); i++) {
55
+            if (tempItems.get(i).getName().equalsIgnoreCase(whatToSearch)) {
56
+                incrementValue(hashMap, String.valueOf(tempItems.get(i).getPrice()));
57
+            }
58
+        }
59
+        return hashMap;
60
+    }
61
+
62
+    public void incrementValue(Map<String, Integer> map, String key) {
63
+        Integer count = map.get(key);
64
+        if (count == null) {
65
+            count = 0;
66
+        }
67
+        map.put(key, count + 1);
68
+    }
69
+
70
+    public Integer getTotalCount (String rawData, String whatToSearch) throws ItemParseException {
71
+        HashMap<String, Integer> hashMap = findBasedOnName(rawData,whatToSearch);
72
+        Integer totalCount = 0;
73
+        for (Map.Entry<String, Integer> entry: hashMap.entrySet()){
74
+            totalCount += entry.getValue();
75
+        }
76
+        return totalCount;
77
+
78
+    }
79
+
80
+    public String printOut (String rawData) throws ItemParseException {
81
+        ArrayList<Item> tempItems = findRawItemToArrayList(rawData);
82
+        HashMap<String, Integer> milk = findBasedOnName(rawData, "milk");
83
+        HashMap<String, Integer> bread = findBasedOnName(rawData, "bread");
84
+        HashMap<String, Integer> cookies = findBasedOnName(rawData, "cookies");
85
+        HashMap<String, Integer> apples = findBasedOnName(rawData, "apples");
86
+
87
+        StringBuilder sb = new StringBuilder();
88
+        sb.append("name:\tMilk\t\tseen: "+ )
89
+//
90
+//        name:    Milk 		 seen: 6 times
91
+//                ============= 	 	 =============
92
+//        Price: 	 3.23		 seen: 5 times
93
+//                -------------		 -------------
94
+//                Price:   1.23		 seen: 1 time
95
+//
96
+//        name:   Bread		 seen: 6 times
97
+//                =============		 =============
98
+//        Price:   1.23		 seen: 6 times
99
+//                -------------		 -------------
100
+//
101
+//                name: Cookies     	 seen: 8 times
102
+//                =============     	 =============
103
+//        Price:   2.25        seen: 8 times
104
+//                -------------        -------------
105
+//
106
+//                name:  Apples     	 seen: 4 times
107
+//                =============     	 =============
108
+//        Price:   0.25     	 seen: 2 times
109
+//                -------------     	 -------------
110
+//                Price:   0.23  	 	 seen: 2 times
111
+//
112
+//        Errors         	 	 seen: 4 times
113
+
114
+
115
+        return rawData;
116
+    }
117
+
118
+
119
+    public static void main(String[] args) throws ItemParseException {
120
+        String rawMultipleItems = "naMe:Milk;price:3.23;type:Food;expiration:1/25/2016##"
121
+                + "naME:BreaD;price:1.23;type:Food;expiration:1/02/2016##"
122
+                + "NAMe:BrEAD;price:1.23;type:Food;expiration:2/25/2016##"
123
+                + "NAMe:BrEAD;price:1.25;type:Food;expiration:2/25/2016##"
124
+                + "NAMe:chocolate;price:1.25;type:Food;expiration:2/25/2016##";
125
+        ;
126
+        ItemParser itemParser = new ItemParser();
127
+        System.out.println(itemParser.findBasedOnName(rawMultipleItems, "bread"));
128
+        System.out.println(itemParser.getTotalCount(rawMultipleItems, "bread"));
129
+
27 130
     }
28 131
 
29 132
 
30 133
 
134
+    public String fixCookie(String input) {
135
+        Pattern patternCookies = Pattern.compile("[Cc][Oo0][Oo0][Kk][Ii][Ee][Ss]");
136
+        Matcher matcherCookies = patternCookies.matcher(input);
137
+        return matcherCookies.replaceAll("cookies");
138
+    }
139
+
140
+    public String checkName(String input) throws ItemParseException {
141
+        String newInput = fixCookie(input);
142
+        Pattern patternName = Pattern.compile("([Nn]..[Ee]:)(\\w+)");
143
+        Matcher matcherName = patternName.matcher(newInput);
144
+
145
+        if (matcherName.find())
146
+            return matcherName.group(2).toLowerCase();
147
+        else throw new ItemParseException();
148
+    }
149
+
150
+    public String checkPrice(String input) throws ItemParseException {
151
+        Pattern patternPrice = Pattern.compile("([Pp]...[Ee]:)(\\d\\.\\d{2})");
152
+        Matcher matcherPrice = patternPrice.matcher(input);
153
+
154
+        if (matcherPrice.find())
155
+            return matcherPrice.group(2).toLowerCase();
156
+        else throw new ItemParseException();
157
+    }
158
+
159
+    public String checkType(String input) throws ItemParseException {
160
+        Pattern patternType = Pattern.compile("([Tt]..[Ee]:)(\\w+)");
161
+        Matcher matcherType = patternType.matcher(input);
162
+
163
+        if (matcherType.find())
164
+            return matcherType.group(2).toLowerCase();
165
+        else throw new ItemParseException();
166
+    }
167
+
168
+    public String checkExpiration(String input) throws ItemParseException {
169
+        Pattern patternExpiration = Pattern.compile("([Ee]........[Nn]:)(\\d\\/\\d{2}\\/\\d{4})");
170
+        Matcher matcherExpiration = patternExpiration.matcher(input);
171
+
172
+        if (matcherExpiration.find())
173
+            return matcherExpiration.group(2).toLowerCase();
174
+        else throw new ItemParseException();
175
+    }
31 176
 }
177
+
178
+

+ 8
- 1
src/main/java/io/zipcoder/Main.java Просмотреть файл

@@ -2,6 +2,8 @@ package io.zipcoder;
2 2
 
3 3
 import org.apache.commons.io.IOUtils;
4 4
 
5
+import java.util.ArrayList;
6
+
5 7
 
6 8
 public class Main {
7 9
 
@@ -13,7 +15,12 @@ public class Main {
13 15
 
14 16
     public static void main(String[] args) throws Exception{
15 17
         String output = (new Main()).readRawDataToString();
16
-        System.out.println(output);
18
+        ItemParser itemParser = new ItemParser();
19
+        ArrayList<Item> temp = itemParser.findRawItemToArrayList(output);
20
+        System.out.println(temp.toString());
17 21
         // TODO: parse the data in output into items, and display to console.
22
+
23
+
24
+
18 25
     }
19 26
 }

+ 32
- 1
src/test/java/io/zipcoder/ItemParserTest.java Просмотреть файл

@@ -14,7 +14,10 @@ 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:Food;expiration:1/25/2016##";
18
+
19
+    private String rawBrokenSingleItem =    "naMe:;price:3.23;type:Food;expiration:1/25/2016##";
20
+
18 21
 
19 22
     private String rawMultipleItems = "naMe:Milk;price:3.23;type:Food;expiration:1/25/2016##"
20 23
                                       +"naME:BreaD;price:1.23;type:Food;expiration:1/02/2016##"
@@ -59,4 +62,32 @@ public class ItemParserTest {
59 62
         Integer actual = itemParser.findKeyValuePairsInRawItemData(rawSingleItemIrregularSeperatorSample).size();
60 63
         assertEquals(expected, actual);
61 64
     }
65
+
66
+    @Test
67
+    public void findRawItem() throws ItemParseException {
68
+        String expected ="name:milk price:3.23 type:food expiration:1/25/2016";
69
+        String actual  = itemParser.findRawItemToArrayList(rawMultipleItems).get(0).toString();
70
+        System.out.println(itemParser.findRawItemToArrayList(rawMultipleItems).get(0));
71
+        assertEquals(expected, actual);
72
+    }
73
+
74
+    @Test
75
+    public void fixCookie() {
76
+    }
77
+
78
+    @Test
79
+    public void checkName() {
80
+    }
81
+
82
+    @Test
83
+    public void checkPrice() {
84
+    }
85
+
86
+    @Test
87
+    public void checkType() {
88
+    }
89
+
90
+    @Test
91
+    public void checkExpiration() {
92
+    }
62 93
 }