Kaynağa Gözat

final submission

ahsonali 6 yıl önce
ebeveyn
işleme
4f72b11b2e

+ 198
- 8
src/main/java/io/zipcoder/ItemParser.java Dosyayı Görüntüle

@@ -2,30 +2,220 @@ package io.zipcoder;
2 2
 
3 3
 import java.util.ArrayList;
4 4
 import java.util.Arrays;
5
+import java.util.Date;
6
+import java.util.HashMap;
7
+import java.util.regex.Matcher;
8
+import java.util.regex.Pattern;
9
+import java.util.Map;
5 10
 
6
-public class ItemParser {
11
+public class ItemParser
12
+{
13
+    private Pattern pattern;
14
+    private Matcher matcher;
15
+    private Integer countExceptions = 0;
16
+    private Main main = new Main();
17
+    private Map<String, ArrayList<Item>> groceryMap = new HashMap<String, ArrayList<Item>>();
18
+    private String setTimes;
7 19
 
20
+    //
21
+    private ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString)
22
+    {
23
+        return new ArrayList<String>(Arrays.asList(inputString.split(stringPattern)));
24
+    }
8 25
 
26
+    //This method calls the above method
9 27
     public ArrayList<String> parseRawDataIntoStringArray(String rawData){
10 28
         String stringPattern = "##";
11 29
         ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawData);
12 30
         return response;
13 31
     }
14 32
 
15
-    public Item parseStringIntoItem(String rawItem) throws ItemParseException{
33
+    //not used really, but tested
34
+    public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem)
35
+    {
36
+        String stringPattern = "[@|^|*|%|!|;]";
37
+        ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawItem);
38
+        return response;
39
+    }
40
+
41
+
42
+    //Uses the helper methods created below named checkName, checkPrice, checkType, checkExpiration
43
+    public Item parseStringIntoItem(String rawItem) throws ItemParseException
44
+    {
45
+
46
+        if (checkName(rawItem) == null || checkPrice(rawItem) == null) {
47
+            throw new ItemParseException();
48
+        }
49
+
50
+
51
+        String name = checkName(rawItem);
52
+        Double price = Double.parseDouble(checkPrice(rawItem));
53
+        String type = checkType(rawItem);
54
+        String expiration = checkExpiration(rawItem);
55
+
56
+
57
+        return new Item(name, price, type, expiration);
58
+
59
+    }
60
+
61
+
62
+
63
+
64
+    public String checkName(String rawItem)
65
+    {
66
+
67
+        String search = "(?<=([Nn][Aa][Mm][Ee][^A-Za-z])).*?(?=[^A-Za-z0])";
68
+        pattern = Pattern.compile(search);
69
+        matcher = pattern.matcher(rawItem);
70
+
71
+        if (matcher.find())
72
+        {
73
+            if(!matcher.group().equals(""))
74
+            {
75
+                String name = matcher.group().replaceAll("\\d", "o");
76
+                return name.toLowerCase();
77
+            }
78
+        }
79
+
16 80
         return null;
81
+
82
+
83
+
84
+   }
85
+
86
+    public String checkPrice(String rawItem)
87
+    {
88
+
89
+        pattern = Pattern.compile("(?<=([Pp][Rr][Ii][Cc][Ee][^A-Za-z])).*?(?=[^0-9.])");
90
+        matcher = pattern.matcher(rawItem);
91
+
92
+        if (matcher.find()) {
93
+            if (!matcher.group().equals("")) {
94
+                return matcher.group();
95
+            }
96
+        }
97
+        return null;
98
+
17 99
     }
18 100
 
19
-    public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){
20
-        String stringPattern = "[;|^]";
21
-        ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawItem);
22
-        return response;
101
+    public String checkType(String rawItem) //throws itemParseException
102
+    {
103
+
104
+
105
+        pattern = Pattern.compile("(?<=([Tt][Yy][Pp][Ee][^A-Za-z])).*?(?=[^A-Za-z0])");
106
+        matcher = pattern.matcher(rawItem);
107
+
108
+        if (matcher.find()) {
109
+            return matcher.group().toLowerCase();
110
+        }
111
+        return null;
23 112
     }
24 113
 
25
-    private ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString){
26
-        return new ArrayList<String>(Arrays.asList(inputString.split(stringPattern)));
114
+    public String checkExpiration(String rawItem)
115
+    {
116
+        pattern = Pattern.compile("(?<=([Ee][Xx][Pp][Ii][Rr][Aa][Tt][Ii][Oo][Nn][^A-Za-z]))(.)+[^#]");
117
+        matcher = pattern.matcher(rawItem);
118
+        if (matcher.find()) {
119
+            return matcher.group();
120
+        }
121
+        return null;
122
+    }
123
+
124
+    public Map<String, ArrayList<Item>> getMap()  throws Exception
125
+    {
126
+        ArrayList<String> allItems = parseRawDataIntoStringArray(main.readRawDataToString());
127
+
128
+        for (String eachItem : allItems)
129
+        {
130
+            try {
131
+                Item newItem = parseStringIntoItem(eachItem);
132
+                if (!groceryMap.containsKey(newItem.getName()))
133
+                {
134
+                    ArrayList<Item> thisItem = new ArrayList<Item>();
135
+                    thisItem.add(newItem);
136
+                    groceryMap.put(newItem.getName(), thisItem);
137
+                }
138
+                else
139
+                    {
140
+                        groceryMap.get(newItem.getName()).add(newItem);
141
+                    }
142
+            }
143
+
144
+            catch (ItemParseException e)
145
+            {
146
+                countExceptions++;
147
+            }
148
+        }
149
+
150
+        return groceryMap;
151
+    }
152
+
153
+    public String displayOutput() throws Exception{
154
+        groceryMap = getMap();
155
+
156
+        StringBuilder sb = new StringBuilder();
157
+
158
+        for  (Map.Entry<String, ArrayList<Item>> item: groceryMap.entrySet())
159
+        {
160
+            String upperCase = item.getKey().substring(0, 1).toUpperCase()+ item.getKey().substring(1);
161
+
162
+            sb.append("\n" +
163
+                    String.format("%-5s%10s%15s%2d%5s", "name:", upperCase
164
+                            , "seen: ", item.getValue().size(), "  times"));
165
+
166
+            sb.append("\n" + String.format("%15s%3s%5s", "===============", "\t\t\t", "===============") + "\n");
167
+
168
+            ArrayList<Double> uniquePriceList = getUniquePrices(item);
169
+
170
+            for (int i = 0; i <uniquePriceList.size(); i++)
171
+            {
172
+                if(priceOccurencesSeen(item.getValue(), uniquePriceList.get(i)) == 1){
173
+                    setTimes = "  time";
174
+                } else setTimes = "  times";
175
+
176
+                sb.append(String.format("%-11s%.2f%15s%2d%5s", "Price:", uniquePriceList.get(i)
177
+                        , "seen: ", priceOccurencesSeen(item.getValue(), uniquePriceList.get(i))
178
+                        , setTimes));
179
+                sb.append("\n" +
180
+                        String.format("%15s%3s%5s", "---------------", "\t\t\t", "---------------") + "\n");
181
+            }
182
+
183
+        }
184
+
185
+        sb.append("\n"+ String.format("%-20s%10s%2d%5s", "Errors", "seen: ", countExceptions, "  times"));
186
+
187
+        return sb.toString();
27 188
     }
28 189
 
190
+    public int priceOccurencesSeen(ArrayList<Item>list, Double price)
191
+    {
192
+        int priceCounter = 0;
193
+
194
+        for (int i = 0; i < list.size(); i++)
195
+        {
196
+            if(list.get(i).getPrice().equals(price))
197
+            {
198
+                priceCounter++;
199
+            }
200
+        }
201
+
202
+        return priceCounter;
203
+    }
204
+
205
+    public ArrayList<Double> getUniquePrices(Map.Entry<String,ArrayList<Item>> item)
206
+    {
207
+        ArrayList<Double> uniquePrices = new ArrayList<Double>();
208
+
209
+        for (int i = 0; i < item.getValue().size(); i++)
210
+        {
211
+            if(!uniquePrices.contains(item.getValue().get(i).getPrice()))
212
+            {
213
+                uniquePrices.add(item.getValue().get(i).getPrice());
214
+            }
215
+        }
216
+
217
+        return uniquePrices;
218
+    }
29 219
 
30 220
 
31 221
 }

+ 3
- 1
src/main/java/io/zipcoder/Main.java Dosyayı Görüntüle

@@ -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();
18
+        System.out.println(itemParser.displayOutput());
19
+
18 20
     }
19 21
 }

+ 56
- 0
src/test/java/io/zipcoder/ItemParserTest.java Dosyayı Görüntüle

@@ -3,6 +3,7 @@ package io.zipcoder;
3 3
 import org.junit.Assert;
4 4
 import org.junit.Before;
5 5
 import org.junit.Test;
6
+import org.omg.CORBA.IRObject;
6 7
 
7 8
 import java.util.ArrayList;
8 9
 
@@ -59,4 +60,59 @@ public class ItemParserTest {
59 60
         Integer actual = itemParser.findKeyValuePairsInRawItemData(rawSingleItemIrregularSeperatorSample).size();
60 61
         assertEquals(expected, actual);
61 62
     }
63
+
64
+    @Test
65
+    public void displayOutpitTest() throws Exception{
66
+
67
+        String expected = "\n" +
68
+                "name:     Bread         seen:  6  times\n" +
69
+                "===============\t\t\t===============\n" +
70
+                "Price:     1.23         seen:  6  times\n" +
71
+                "---------------\t\t\t---------------\n" +
72
+                "\n" +
73
+                "name:      Milk         seen:  6  times\n" +
74
+                "===============\t\t\t===============\n" +
75
+                "Price:     3.23         seen:  5  times\n" +
76
+                "---------------\t\t\t---------------\n" +
77
+                "Price:     1.23         seen:  1  time\n" +
78
+                "---------------\t\t\t---------------\n" +
79
+                "\n" +
80
+                "name:    Apples         seen:  4  times\n" +
81
+                "===============\t\t\t===============\n" +
82
+                "Price:     0.25         seen:  2  times\n" +
83
+                "---------------\t\t\t---------------\n" +
84
+                "Price:     0.23         seen:  2  times\n" +
85
+                "---------------\t\t\t---------------\n" +
86
+                "\n" +
87
+                "name:   Cookies         seen:  8  times\n" +
88
+                "===============\t\t\t===============\n" +
89
+                "Price:     2.25         seen:  8  times\n" +
90
+                "---------------\t\t\t---------------\n" +
91
+                "\n" +
92
+                "Errors                  seen:  4  times";
93
+        String actual = itemParser.displayOutput();
94
+
95
+        Assert.assertEquals(expected, actual);
96
+    }
97
+
98
+    @Test
99
+    public void priceOccurencesSeenTest()
100
+    {
101
+        ArrayList<Item> test = new ArrayList<Item>();
102
+
103
+        Item item1 = new Item("Milk", 2.89, "Drink", "3/18/2018");
104
+        Item item2 = new Item("Bread", 2.89,"Food","3/21/2018");
105
+        Item item3 = new Item("Bread", 2.79, "Food", "3/24/2018");
106
+
107
+        test.add(item1);
108
+        test.add(item2);
109
+
110
+        int expected= 2;
111
+        int actual = itemParser.priceOccurencesSeen(test, 2.89);
112
+
113
+        Assert.assertEquals(expected, actual);
114
+
115
+    }
116
+
117
+
62 118
 }