Bläddra i källkod

Merge 925a5c1af48b74ae9ba4237237c766662860239e into 23c2c01bf07b924ac9b860fa2771bf6ceb246ad5

Frankie Rodriguez 6 år sedan
förälder
incheckning
62c97d249b
No account linked to committer's email

+ 138
- 7
src/main/java/io/zipcoder/ItemParser.java Visa fil

@@ -2,30 +2,161 @@ package io.zipcoder;
2 2
 
3 3
 import java.util.ArrayList;
4 4
 import java.util.Arrays;
5
+import java.util.LinkedHashMap;
6
+import java.util.Map;
7
+import java.util.regex.Matcher;
8
+import java.util.regex.Pattern;
5 9
 
6 10
 public class ItemParser {
11
+    private ArrayList<String> values;
12
+    private ArrayList<Item> itemList;
13
+    private int exceptionCount;
7 14
 
15
+    ItemParser(String rawList) {
16
+        this.values = parseRawDataIntoStringArray(rawList);
17
+        this.itemList = new ArrayList<Item>();
18
+        buildItemList();
19
+    }
8 20
 
9
-    public ArrayList<String> parseRawDataIntoStringArray(String rawData){
21
+    public ArrayList<String> parseRawDataIntoStringArray(String rawData) {
22
+        String rawDataFix = rawData.substring(0, rawData.length() - 2);
10 23
         String stringPattern = "##";
11
-        ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawData);
24
+        ArrayList<String> response = splitStringWithRegexPattern(stringPattern, rawDataFix);
12 25
         return response;
13 26
     }
14 27
 
15
-    public Item parseStringIntoItem(String rawItem) throws ItemParseException{
16
-        return null;
28
+    public Item parseStringIntoItem(String rawItem) throws ItemParseException {
29
+        LinkedHashMap<String, String> item = getKeyValueEntries(rawItem);
30
+        if (item.get("name") == null || item.get("price") == null) {
31
+            throw new ItemParseException();
32
+        }
33
+        Item newItem = new Item(fixAnyZeros(item.get("name")), Double.valueOf(item.get("price")), item.get("type"), item.get("expiration"));
34
+        return newItem;
35
+    }
36
+
37
+    private String fixAnyZeros(String s) {
38
+        Pattern pattern = Pattern.compile("[0]");
39
+        Matcher matcher = pattern.matcher(s);
40
+        return matcher.replaceAll("o");
41
+    }
42
+
43
+    public LinkedHashMap<String, String> getKeyValueEntries(String valuePair) {
44
+        ArrayList<String> keyValuePairs = findKeyValuePairsInRawItemData(valuePair);
45
+        LinkedHashMap<String, String> itemDescription = new LinkedHashMap<String, String>();
46
+        for (String keyValues : keyValuePairs) {
47
+            ArrayList<String> keyAndValue = splitKeysAndValue(keyValues);
48
+            if (keyAndValue.size() == 2) {
49
+                itemDescription.put(keyAndValue.get(0).toLowerCase(), keyAndValue.get(1).toLowerCase());
50
+            } else {
51
+                itemDescription.put(keyAndValue.get(0).toLowerCase(), null);
52
+            }
53
+        }
54
+        return itemDescription;
17 55
     }
18 56
 
19
-    public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){
57
+    public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem) {
20 58
         String stringPattern = "[;|^]";
21
-        ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawItem);
59
+        ArrayList<String> response = splitStringWithRegexPattern(stringPattern, rawItem);
22 60
         return response;
23 61
     }
24 62
 
25
-    private ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString){
63
+    public ArrayList<String> splitKeysAndValue(String rawItem) {
64
+        String keyValueSeparators = "[:|@|*|%]";
65
+        ArrayList<String> response = splitStringWithRegexPattern(keyValueSeparators, rawItem);
66
+        return response;
67
+    }
68
+
69
+    private ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString) {
26 70
         return new ArrayList<String>(Arrays.asList(inputString.split(stringPattern)));
27 71
     }
28 72
 
73
+    private void buildItemList() {
74
+        for (String itemDetails : values) {
75
+            try {
76
+                this.itemList.add(parseStringIntoItem(itemDetails));
77
+            } catch (ItemParseException e) {
78
+                incrementCounter();
79
+            }
80
+        }
81
+    }
82
+
83
+    private void incrementCounter() {
84
+        this.exceptionCount++;
85
+    }
86
+
87
+    public String getFormattedString() {
88
+        ArrayList<LinkedHashMap<String, Integer>> itemSeenInfo = new ArrayList<LinkedHashMap<String, Integer>>();
89
+        for (Item item : itemList) {
90
+            if (containsCheck(itemSeenInfo, item.getName()) == false) {
91
+                itemSeenInfo.add(getOccurrences(item.getName(), itemList));
92
+            }
93
+        }
94
+        StringBuilder output = new StringBuilder();
95
+
96
+        for (LinkedHashMap<String, Integer> item : itemSeenInfo) {
97
+            for (Map.Entry entry : item.entrySet()) {
98
+                if (checkIfName(entry.getKey())) {
99
+                    output.append(String.format("\nName: %-10s \t\t\t " +
100
+                            "Seen: %5d\n================ \t\t\t " +
101
+                            "===========\n", entry.getKey(), entry.getValue()));
102
+                } else {
103
+                    output.append(String.format("Price: %-5s \t\t\t\t " +
104
+                            "Seen: %5d\n---------------- \t\t\t " +
105
+                            "-----------\n", entry.getKey(), entry.getValue()));
106
+                }
107
+            }
108
+        }
109
+        output.append("\n\nErrors          \t\t\t Seen:     " + exceptionCount);
110
+        return output.toString();
111
+    }
112
+
113
+    private boolean checkIfName(Object key) {
114
+        Pattern numbers = Pattern.compile("\\d");
115
+        Matcher matcher = numbers.matcher(key.toString());
116
+        if (matcher.find() == true) {
117
+            return false;
118
+        }
119
+        return true;
120
+    }
29 121
 
122
+    private boolean containsCheck(ArrayList<LinkedHashMap<String, Integer>> itemSeenInfo, String targetItem) {
123
+        for (LinkedHashMap<String, Integer> item : itemSeenInfo) {
124
+            for (Map.Entry entry : item.entrySet()) {
125
+                if (entry.getKey().equals(targetItem)) {
126
+                    return true;
127
+                }
128
+            }
129
+        }
130
+        return false;
131
+    }
30 132
 
133
+    public LinkedHashMap<String, Integer> getOccurrences(String name, ArrayList<Item> items) {
134
+        LinkedHashMap<String, Integer> itemOcc = new LinkedHashMap<String, Integer>();
135
+        ArrayList<Double> prices = new ArrayList<Double>();
136
+        int nameCount = 0;
137
+        for (Item item : items) {
138
+            if (item.getName().equals(name)) {
139
+                nameCount++;
140
+                prices.add(item.getPrice());
141
+            }
142
+        }
143
+        itemOcc.put(name, nameCount);
144
+        for (Double price : prices) {
145
+            if (!itemOcc.containsKey(price.toString())) {
146
+                int priceOcc = getPriceOccurrences(price, prices);
147
+                itemOcc.put(price.toString(), priceOcc);
148
+            }
149
+        }
150
+        return itemOcc;
151
+    }
152
+
153
+    private int getPriceOccurrences(Double targetPrice, ArrayList<Double> prices) {
154
+        int priceCount = 0;
155
+        for (Double price : prices) {
156
+            if (price == targetPrice) {
157
+                priceCount++;
158
+            }
159
+        }
160
+        return priceCount;
161
+    }
31 162
 }

+ 3
- 1
src/main/java/io/zipcoder/Main.java Visa fil

@@ -13,7 +13,9 @@ public class Main {
13 13
 
14 14
     public static void main(String[] args) throws Exception{
15 15
         String output = (new Main()).readRawDataToString();
16
-        System.out.println(output);
17 16
         // TODO: parse the data in output into items, and display to console.
17
+        ItemParser itemParser = new ItemParser(output);
18
+        String finalString = itemParser.getFormattedString();
19
+        System.out.println(finalString);
18 20
     }
19 21
 }

+ 8
- 4
src/test/java/io/zipcoder/ItemParserTest.java Visa fil

@@ -1,20 +1,22 @@
1 1
 package io.zipcoder;
2 2
 
3
+import org.apache.commons.io.IOUtils;
3 4
 import org.junit.Assert;
4 5
 import org.junit.Before;
5 6
 import org.junit.Test;
6 7
 
8
+import java.io.IOException;
7 9
 import java.util.ArrayList;
8 10
 
9 11
 import static org.junit.Assert.*;
10 12
 
11 13
 public class ItemParserTest {
12 14
 
13
-    private String rawSingleItem =    "naMe:Milk;price:3.23;type:Food;expiration:1/25/2016##";
15
+    private String rawSingleItem =    "naMe:Milk;price:3.23;type:Food;expiration:1/25/2016";
14 16
 
15 17
     private String rawSingleItemIrregularSeperatorSample = "naMe:MiLK;price:3.23;type:Food^expiration:1/11/2016##";
16 18
 
17
-    private String rawBrokenSingleItem =    "naMe:Milk;price:3.23;type:Food;expiration:1/25/2016##";
19
+    private String rawBrokenSingleItem =    "naMe:;price:3.23;type:Food;expiration:1/25/2016##";
18 20
 
19 21
     private String rawMultipleItems = "naMe:Milk;price:3.23;type:Food;expiration:1/25/2016##"
20 22
                                       +"naME:BreaD;price:1.23;type:Food;expiration:1/02/2016##"
@@ -22,8 +24,10 @@ public class ItemParserTest {
22 24
     private ItemParser itemParser;
23 25
 
24 26
     @Before
25
-    public void setUp(){
26
-        itemParser = new ItemParser();
27
+    public void setUp() throws Exception {
28
+        ClassLoader classLoader = getClass().getClassLoader();
29
+        String result = IOUtils.toString(classLoader.getResourceAsStream("RawData.txt"));
30
+        itemParser = new ItemParser(result);
27 31
     }
28 32
 
29 33
     @Test