Bo Lee 6 anni fa
parent
commit
ecb72d30a0

+ 23
- 0
src/main/java/io/zipcoder/ErrorLogger.java Vedi File

@@ -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 Vedi File

@@ -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 Vedi File

@@ -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
 }

+ 95
- 89
src/main/java/io/zipcoder/ItemParser.java Vedi File

@@ -10,6 +10,14 @@ import java.util.regex.Pattern;
10 10
 
11 11
 public class ItemParser {
12 12
 
13
+    private ErrorLogger errorLogger;
14
+
15
+    public ItemParser(ErrorLogger errorLogger) {
16
+        this.errorLogger = errorLogger;
17
+    }
18
+    public  ItemParser(){
19
+
20
+    }
13 21
 
14 22
     private ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString) {
15 23
         return new ArrayList<String>(Arrays.asList(inputString.split(stringPattern)));
@@ -42,95 +50,23 @@ public class ItemParser {
42 50
         ArrayList<String> temp = parseRawDataIntoStringArray(rawData);
43 51
         ArrayList<Item> itemArrayList = new ArrayList<Item>();
44 52
         for (int i = 0; i < temp.size(); i++) {
45
-            itemArrayList.add(parseStringIntoItem(temp.get(i)));
46
-        }
47
-        return itemArrayList;
48
-    }
53
+            Item item;
54
+            try {
55
+                item = parseStringIntoItem(temp.get(i));
56
+                itemArrayList.add(item);
49 57
 
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()));
58
+            } catch (ItemParseException e) {
59
+                errorLogger.incrementError();
57 60
             }
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 61
 
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 62
 
118 63
 
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 64
 
65
+        }
66
+        return itemArrayList;
130 67
     }
131 68
 
132 69
 
133
-
134 70
     public String fixCookie(String input) {
135 71
         Pattern patternCookies = Pattern.compile("[Cc][Oo0][Oo0][Kk][Ii][Ee][Ss]");
136 72
         Matcher matcherCookies = patternCookies.matcher(input);
@@ -144,35 +80,105 @@ public class ItemParser {
144 80
 
145 81
         if (matcherName.find())
146 82
             return matcherName.group(2).toLowerCase();
147
-        else throw new ItemParseException();
148
-    }
83
+        else {
84
+            throw new ItemParseException();
85
+        }    }
149 86
 
150 87
     public String checkPrice(String input) throws ItemParseException {
151 88
         Pattern patternPrice = Pattern.compile("([Pp]...[Ee]:)(\\d\\.\\d{2})");
152 89
         Matcher matcherPrice = patternPrice.matcher(input);
153
-
154
-        if (matcherPrice.find())
90
+        if (matcherPrice.find()){
155 91
             return matcherPrice.group(2).toLowerCase();
156
-        else throw new ItemParseException();
92
+        }
93
+        else {
94
+            throw new ItemParseException();
95
+        }
157 96
     }
158 97
 
159 98
     public String checkType(String input) throws ItemParseException {
160 99
         Pattern patternType = Pattern.compile("([Tt]..[Ee]:)(\\w+)");
161 100
         Matcher matcherType = patternType.matcher(input);
162 101
 
163
-        if (matcherType.find())
102
+        if (matcherType.find()) {
164 103
             return matcherType.group(2).toLowerCase();
165
-        else throw new ItemParseException();
104
+        }
105
+        else {
106
+            throw new ItemParseException();
107
+        }
166 108
     }
167 109
 
168 110
     public String checkExpiration(String input) throws ItemParseException {
169 111
         Pattern patternExpiration = Pattern.compile("([Ee]........[Nn]:)(\\d\\/\\d{2}\\/\\d{4})");
170 112
         Matcher matcherExpiration = patternExpiration.matcher(input);
171 113
 
172
-        if (matcherExpiration.find())
114
+        if (matcherExpiration.find()) {
173 115
             return matcherExpiration.group(2).toLowerCase();
174
-        else throw new ItemParseException();
116
+        }
117
+        else {
118
+            throw new ItemParseException();
119
+        }
175 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
+
181
+
176 182
 }
177 183
 
178 184
 

+ 19
- 4
src/main/java/io/zipcoder/Main.java Vedi File

@@ -15,12 +15,27 @@ public class Main {
15 15
 
16 16
     public static void main(String[] args) throws Exception{
17 17
         String output = (new Main()).readRawDataToString();
18
-        ItemParser itemParser = new ItemParser();
19
-        ArrayList<Item> temp = itemParser.findRawItemToArrayList(output);
20
-        System.out.println(temp.toString());
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
+        }
21 36
         // TODO: parse the data in output into items, and display to console.
22 37
 
23 38
 
24 39
 
25
-    }
40
+
26 41
 }

+ 78
- 0
src/main/java/io/zipcoder/OrganizedItemData.java Vedi File

@@ -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
+

+ 0
- 19
src/test/java/io/zipcoder/ItemParserTest.java Vedi File

@@ -71,23 +71,4 @@ public class ItemParserTest {
71 71
         assertEquals(expected, actual);
72 72
     }
73 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
-    }
93 74
 }