Pārlūkot izejas kodu

Merge ecb72d30a0552e197e5b9b2a7076f51370b46e48 into 23c2c01bf07b924ac9b860fa2771bf6ceb246ad5

bell7692 6 gadus atpakaļ
vecāks
revīzija
e525b07213
Revīzijas autora e-pasts nav piesaistīts nevienam kontam

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

@@ -0,0 +1,23 @@
1
+package io.zipcoder;
2
+
3
+public class ErrorLogger {
4
+
5
+    private int errorCount = 0;
6
+
7
+    public void incrementError() {
8
+        errorCount++;
9
+    }
10
+
11
+    public String getFormattedError(){
12
+        StringBuilder sb = new StringBuilder();
13
+        sb.append("Errors\t\t\t\tseen: " + errorCount);
14
+        if (ItemParseException.errorCount > 1) {
15
+            sb.append(" times");
16
+        }
17
+        else {
18
+            sb.append(" time");
19
+        }
20
+        sb.append("\n");
21
+        return sb.toString();
22
+    }
23
+}

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

@@ -0,0 +1,21 @@
1
+package io.zipcoder;
2
+
3
+import java.util.HashMap;
4
+
5
+public class ItemOrganizer {
6
+
7
+    public Iterable<OrganizedItemData> organizeItems(Iterable<Item> items) {
8
+        HashMap<String, OrganizedItemData> organizedItemsMap = new HashMap<String, OrganizedItemData>();
9
+        for (Item item : items) {
10
+            if(organizedItemsMap.containsKey(item.getName())) {
11
+                OrganizedItemData organizedItemData = organizedItemsMap.get(item.getName());
12
+                organizedItemData.addPrice(item.getPrice());
13
+            } else {
14
+                OrganizedItemData organizedItemData = new OrganizedItemData(item.getName());
15
+                organizedItemData.addPrice(item.getPrice());
16
+                organizedItemsMap.put(item.getName(), organizedItemData);
17
+            }
18
+        }
19
+        return organizedItemsMap.values();
20
+    }
21
+}

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

@@ -1,4 +1,7 @@
1 1
 package io.zipcoder;
2 2
 
3 3
 public class ItemParseException extends Exception {
4
+
5
+    public static int errorCount = 0;
6
+
4 7
 }

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

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

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

@@ -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,27 @@ 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
+        System.out.println(output);;
19
+        ErrorLogger errorData = new ErrorLogger();
20
+
21
+        ItemParser itemParser = new ItemParser(errorData);
22
+
23
+        ArrayList<Item> itemList = itemParser.findRawItemToArrayList(output);
24
+
25
+
26
+        ItemOrganizer itemOrganizer = new ItemOrganizer();
27
+        Iterable<OrganizedItemData> organizedItems = itemOrganizer.organizeItems(itemList);
28
+
29
+        for(OrganizedItemData organizedItemData : organizedItems) {
30
+            System.out.println(organizedItemData.getFormattedData());
31
+        }
32
+        String errorOutput= errorData.getFormattedError();
33
+        System.out.println(errorOutput);
34
+
35
+        }
17 36
         // TODO: parse the data in output into items, and display to console.
18
-    }
37
+
38
+
39
+
40
+
19 41
 }

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

@@ -0,0 +1,78 @@
1
+package io.zipcoder;
2
+
3
+import java.util.Collection;
4
+import java.util.HashMap;
5
+import java.util.Iterator;
6
+import java.util.Map;
7
+
8
+public class OrganizedItemData {
9
+//take list of items and organize data in the way we need them of item name, price and count of prices
10
+    private String itemName;
11
+    private HashMap<Double, Integer> priceAndOccurrence = new HashMap<Double, Integer>();
12
+
13
+
14
+    public OrganizedItemData(String itemName) {
15
+        this.itemName = itemName;
16
+    }
17
+
18
+    public String getItemName() {
19
+        return itemName;
20
+    }
21
+
22
+    public void addPrice(double price) {
23
+        if(priceAndOccurrence.containsKey(price)) {
24
+            int count = priceAndOccurrence.get(price) + 1;
25
+            priceAndOccurrence.put(price, count);
26
+        } else {
27
+            priceAndOccurrence.put(price, 1);
28
+        }
29
+
30
+    }
31
+
32
+    private int getTotalCount() {
33
+        int count = 0;
34
+        for(Integer value : priceAndOccurrence.values()) {
35
+            count+=value;
36
+        }
37
+        return count;
38
+    }
39
+//
40
+//    public Iterable<Map.Entry<Double, Integer>> getPriceAndOccurrences() {
41
+//        return priceAndOccurrence.entrySet();
42
+//    }
43
+
44
+    public String getFormattedData() {
45
+        StringBuilder sb = new StringBuilder();
46
+        sb.append("name:\t" + itemName);
47
+        sb.append("\t\tseen: " + getTotalCount());
48
+        if (getTotalCount() > 1) {
49
+            sb.append(" times\n");
50
+        } else {
51
+            sb.append(" time\n");
52
+        }
53
+        sb.append("=============\t\t=============\n");
54
+        int count = priceAndOccurrence.entrySet().size();
55
+        for(Map.Entry<Double, Integer> entry: priceAndOccurrence.entrySet()) {
56
+            if (count > 0) {
57
+                sb.append("Price:\t" + entry.getKey());
58
+                sb.append("\t\tseen: " + entry.getValue());
59
+                if (entry.getValue() > 1) {
60
+                    sb.append(" times\n");
61
+                }
62
+                else {
63
+                    sb.append(" time\n");
64
+                }
65
+                if(count > 0) {
66
+                    sb.append("-------------\t\t-------------\n");
67
+                    count--;
68
+                }
69
+            }
70
+        }
71
+        sb.append("\n");
72
+        return sb.toString();
73
+    }
74
+
75
+
76
+
77
+}
78
+

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

@@ -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,13 @@ 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
+
62 74
 }