Browse Source

Merge d4b84b6887b7f291ecda207a02204bf55cab32ab into 23c2c01bf07b924ac9b860fa2771bf6ceb246ad5

Keith Brinker 6 years ago
parent
commit
c106a794e1
No account linked to committer's email

+ 12
- 0
pom.xml View File

@@ -7,6 +7,18 @@
7 7
     <groupId>io.zipcoder</groupId>
8 8
     <artifactId>PainfullAfternoon</artifactId>
9 9
     <version>1.0-SNAPSHOT</version>
10
+    <build>
11
+        <plugins>
12
+            <plugin>
13
+                <groupId>org.apache.maven.plugins</groupId>
14
+                <artifactId>maven-compiler-plugin</artifactId>
15
+                <configuration>
16
+                    <source>1.7</source>
17
+                    <target>1.7</target>
18
+                </configuration>
19
+            </plugin>
20
+        </plugins>
21
+    </build>
10 22
 
11 23
     <dependencies>
12 24
         <dependency>

+ 177
- 10
src/main/java/io/zipcoder/ItemParser.java View File

@@ -1,31 +1,198 @@
1 1
 package io.zipcoder;
2 2
 
3
-import java.util.ArrayList;
4
-import java.util.Arrays;
3
+import java.util.*;
4
+import java.util.regex.Matcher;
5
+import java.util.regex.Pattern;
5 6
 
6 7
 public class ItemParser {
8
+    public HashMap<String, ArrayList<Item>> completeList = new HashMap<String, ArrayList<Item>>();
9
+    int exceptionCounter = 0;
7 10
 
8 11
 
9
-    public ArrayList<String> parseRawDataIntoStringArray(String rawData){
12
+    public ArrayList<String> parseRawDataIntoStringArray(String rawData) {
10 13
         String stringPattern = "##";
11
-        ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawData);
14
+        ArrayList<String> response = splitStringWithRegexPattern(stringPattern, rawData);
12 15
         return response;
13 16
     }
14 17
 
15
-    public Item parseStringIntoItem(String rawItem) throws ItemParseException{
16
-        return null;
18
+    public Item parseStringIntoItem(String rawItem) throws ItemParseException {
19
+        if (nameFinder(rawItem) == null || priceFinder(rawItem) == null) {
20
+            throw new ItemParseException();
21
+        }
22
+
23
+        String name = nameFinder(rawItem);
24
+        Double price = Double.parseDouble(priceFinder(rawItem));
25
+        String type = "food";
26
+        String expiration = expFinder(rawItem);
27
+
28
+        return new Item(name, price, type, expiration);
17 29
     }
18 30
 
19
-    public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){
31
+    public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem) {
20 32
         String stringPattern = "[;|^]";
21
-        ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawItem);
33
+        ArrayList<String> response = splitStringWithRegexPattern(stringPattern, rawItem);
22 34
         return response;
23 35
     }
24 36
 
25
-    private ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString){
37
+    private ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString) {
26 38
         return new ArrayList<String>(Arrays.asList(inputString.split(stringPattern)));
27 39
     }
28 40
 
41
+    public String nameFinder(String rawNames) {
42
+        StringBuilder found = new StringBuilder();
43
+
44
+        String itemSearch = rawNames;
45
+        Pattern foodNames = Pattern.compile("\\w+(?=\\Wprice)", Pattern.CASE_INSENSITIVE);
46
+        Matcher m = foodNames.matcher(itemSearch);
47
+        int lastMatchPos = 0;
48
+        while (m.find()) {
49
+            if (!m.group().equals("")) {
50
+                found.append(m.group().replaceAll("\\d", "o"));
51
+                lastMatchPos = m.end();
52
+            }
53
+            return found.toString().toLowerCase();
54
+        }
55
+        return null;
56
+    }
57
+
58
+    public String priceFinder(String rawPrices) {
59
+        StringBuilder found = new StringBuilder();
60
+
61
+        String itemSearch = rawPrices;
62
+        Pattern foodNames = Pattern.compile("\\d+\\.\\d\\d", Pattern.CASE_INSENSITIVE);
63
+        Matcher m = foodNames.matcher(itemSearch);
64
+        int lastMatchPos = 0;
65
+        while (m.find()) {
66
+            if (!m.group().equals("")) {
67
+                found.append(m.group() + "");
68
+                lastMatchPos = m.end();
69
+            }
70
+            return found.toString();
71
+        }
72
+        return null;
73
+    }
74
+
75
+    public String expFinder(String rawExp) {
76
+        StringBuilder found = new StringBuilder();
77
+
78
+        String itemSearch = rawExp;
79
+        Pattern foodNames = Pattern.compile("\\d{1,2}\\/\\d\\d\\/\\d{4}", Pattern.CASE_INSENSITIVE);
80
+        Matcher m = foodNames.matcher(itemSearch);
81
+        int lastMatchPos = 0;
82
+        while (m.find()) {
83
+            found.append(m.group() + "");
84
+            lastMatchPos = m.end();
85
+        }
86
+        return found.toString();
87
+    }
88
+
89
+
90
+    public HashMap<String, ArrayList<Item>> getCompleteList() throws Exception {
91
+        Main mainObject = new Main();
92
+        ArrayList<String> rawinputdata = parseRawDataIntoStringArray(mainObject.readRawDataToString());
93
+        for (String item : rawinputdata) {
94
+            try {
95
+                Item myItem = parseStringIntoItem(item);
96
+                if (!completeList.containsKey(myItem.getName())) {
97
+                    ArrayList<Item> myItemList = new ArrayList<Item>();
98
+                    myItemList.add(myItem);
99
+                    completeList.put(myItem.getName(), myItemList);
100
+                } else {
101
+                    completeList.get(myItem.getName()).add(myItem);
102
+                }
103
+
104
+            } catch (ItemParseException except) {
105
+                exceptionCounter++;
106
+            }
107
+        }
108
+        return completeList;
109
+    }
110
+
111
+    public String printFinalList() throws Exception {
112
+        completeList = getCompleteList();
113
+        StringBuilder listBuilder = new StringBuilder();
114
+
115
+        for(Map.Entry<String,ArrayList<Item>>groceryItems:completeList.entrySet()){
116
+            listBuilder.append("\nname: ");
117
+            listBuilder.append(String.format("%8s",captitalizeFirstLetter(groceryItems.getKey())));
118
+            listBuilder.append("\t\t\t\tseen: "+getOccurencesOfItems(groceryItems.getValue())+" times\n");
119
+            listBuilder.append("==============="+"\t\t\t\t===============\n");
120
+            String priceReport = generatePriceReport(groceryItems);
121
+            listBuilder.append(priceReport);
122
+            listBuilder.append("---------------"+"\t\t\t\t---------------\n");
123
+        }
124
+
125
+        listBuilder.append("\nErrors\t\t\t\t\t\tseen: "+exceptionCounter+" times\n");
126
+
127
+        return listBuilder.toString();
128
+    }
129
+
130
+    public int getOccurencesOfItems(ArrayList list) {
131
+        return list.size();
132
+    }
133
+
134
+    public int getOccurencesOfPrices(ArrayList<Item> list, Double price) {
135
+        int priceCounter = 0;
136
+        for (int i = 0; i < list.size(); i++) {
137
+            if (list.get(i).getPrice().equals(price) ) {
138
+                priceCounter++;
139
+            }
140
+        }
141
+        return priceCounter;
142
+    }
143
+
144
+    public String generatePriceReport(Map.Entry<String, ArrayList<Item>> input) {
145
+        String priceReport = "";
146
+        ArrayList<Double> nonDupPrices = getUniquePrices(input);
147
+        for(int i=0;i<nonDupPrices.size();i++){
148
+            priceReport+="Price";
149
+            priceReport+=(String.format("%10s",nonDupPrices.get(i)));
150
+            priceReport+=("\t\t\t\tseen: "+ getOccurencesOfPrices(input.getValue(),nonDupPrices.get(i))+
151
+                    " times\n");
152
+        }
153
+        return priceReport;
154
+
155
+    }
156
+
157
+    public ArrayList<Double> getUniquePrices(Map.Entry<String, ArrayList<Item>> input) {
158
+        ArrayList<Double> uniquePrices = new ArrayList<>();
159
+        for (int i=0;i<input.getValue().size();i++) {
160
+            if (!uniquePrices.contains(input.getValue().get(i).getPrice())) {
161
+                uniquePrices.add(input.getValue().get(i).getPrice());
162
+            }
163
+        }
164
+        return uniquePrices;
165
+    }
166
+
167
+    public String captitalizeFirstLetter(String input) {
168
+        return input.substring(0, 1).toUpperCase() + input.substring(1).toLowerCase();
169
+    }
170
+
171
+    }
172
+
29 173
 
174
+//    public int counts() throws Exception {
175
+//        String input = getCompleteList().toString();
176
+//        String[] inputs = input.split("([\\W\\s+])");
177
+//        Map<String, Integer> listCounter = new HashMap<String, Integer>();
178
+//        for (String searchWord : inputs) {
179
+//            if (listCounter.containsKey(searchWord)) {
180
+//                listCounter.put(searchWord, listCounter.get(searchWord) + 1);
181
+//            } else {
182
+//                listCounter.put(searchWord, 1);
183
+//            }
184
+//        }
185
+//        return listCounter.get();
186
+//    }
187
+//    public String printList() throws Exception{
188
+//        ItemParser itemParser = new ItemParser();
189
+//        StringBuilder finalList = new StringBuilder();
190
+//        HashMap<String, ArrayList<Item>> testMap = itemParser.getCompleteList();
191
+//        for (String key : testMap.keySet()) {
192
+//            for (int i = 0; i < testMap.get(key).size(); i++) {
193
+//                finalList.append(("name:      "+ key + " seen:"+testMap.get(key).size()+"\n" + testMap.get(key).get(i).getPrice()));
194
+//            }
195
+//        }
196
+//        return finalList.toString();
197
+//    }
30 198
 
31
-}

+ 32
- 3
src/main/java/io/zipcoder/Main.java View File

@@ -1,19 +1,48 @@
1 1
 package io.zipcoder;
2 2
 
3
+import com.sun.tools.doclets.formats.html.SourceToHTMLConverter;
3 4
 import org.apache.commons.io.IOUtils;
4 5
 
6
+import java.util.*;
7
+import java.util.regex.Matcher;
8
+import java.util.regex.Pattern;
9
+
5 10
 
6 11
 public class Main {
7 12
 
8
-    public String readRawDataToString() throws Exception{
13
+
14
+    public String readRawDataToString() throws Exception {
9 15
         ClassLoader classLoader = getClass().getClassLoader();
10 16
         String result = IOUtils.toString(classLoader.getResourceAsStream("RawData.txt"));
11 17
         return result;
12 18
     }
13 19
 
14
-    public static void main(String[] args) throws Exception{
20
+    public static void main(String[] args) throws Exception {
15 21
         String output = (new Main()).readRawDataToString();
22
+
16 23
         System.out.println(output);
17 24
         // TODO: parse the data in output into items, and display to console.
25
+        ItemParser itemParser = new ItemParser();
26
+        Main main = new Main();
27
+
28
+        ArrayList<String> itemList = itemParser.parseRawDataIntoStringArray(output);
29
+
30
+        System.out.println(itemParser.printFinalList());
31
+        }
32
+
33
+
18 34
     }
19
-}
35
+
36
+//    String s = String.format("%-13s%s", "name:", "bread");
37
+//        System.out.println(s);
38
+//
39
+//                ItemParser itemParser = new ItemParser();
40
+//                HashMap<String, ArrayList<Item>> testMap = itemParser.getCompleteList();
41
+//        for (String key : testMap.keySet()) {
42
+//        for (int i = 0; i < testMap.get(key).size(); i++) {
43
+//        System.out.println("name:    "+key + "                 " + "seen:    " + testMap.get(key).size() + "\n" +
44
+//        "=================              =================\n"
45
+//        +"price:   "+ testMap.get(key).get(i).getPrice() +"                 "+ "seen:    "+ testMap.get(key).size()+"\n"+
46
+//        "----------------              ---------------\n");
47
+//        }
48
+//        }

+ 29
- 1
src/test/java/io/zipcoder/ItemParserTest.java View File

@@ -5,16 +5,20 @@ import org.junit.Before;
5 5
 import org.junit.Test;
6 6
 
7 7
 import java.util.ArrayList;
8
+import java.util.regex.Matcher;
9
+import java.util.regex.Pattern;
8 10
 
9 11
 import static org.junit.Assert.*;
10 12
 
11 13
 public class ItemParserTest {
12 14
 
15
+    private String therealRawData = "naMe:Milk;price:3.23;type:Food;expiration:1/25/2016##naME:BreaD;price:1.23;type:Food;expiration:1/02/2016##NAMe:BrEAD;price:1.23;type:Food;expiration:2/25/2016##naMe:MiLK;price:3.23;type:Food^expiration:1/11/2016##naMe:Cookies;price:2.25;type:Food%expiration:1/25/2016##naMe:CoOkieS;price:2.25;type:Food*expiration:1/25/2016##naMe:COokIes;price:2.25;type:Food;expiration:3/22/2016##naMe:COOkieS;price:2.25;type:Food;expiration:1/25/2016##NAME:MilK;price:3.23;type:Food;expiration:1/17/2016##naMe:MilK;price:1.23;type:Food!expiration:4/25/2016##naMe:apPles;price:0.25;type:Food;expiration:1/23/2016##naMe:apPles;price:0.23;type:Food;expiration:5/02/2016##NAMe:BrEAD;price:1.23;type:Food;expiration:1/25/2016##naMe:;price:3.23;type:Food;expiration:1/04/2016##naMe:Milk;price:3.23;type:Food;expiration:1/25/2016##naME:BreaD;price:1.23;type:Food@expiration:1/02/2016##NAMe:BrEAD;price:1.23;type:Food@expiration:2/25/2016##naMe:MiLK;priCe:;type:Food;expiration:1/11/2016##naMe:Cookies;price:2.25;type:Food;expiration:1/25/2016##naMe:Co0kieS;pRice:2.25;type:Food;expiration:1/25/2016##naMe:COokIes;price:2.25;type:Food;expiration:3/22/2016##naMe:COOkieS;Price:2.25;type:Food;expiration:1/25/2016##NAME:MilK;price:3.23;type:Food;expiration:1/17/2016##naMe:MilK;priCe:;type:Food;expiration:4/25/2016##naMe:apPles;prIce:0.25;type:Food;expiration:1/23/2016##naMe:apPles;pRice:0.23;type:Food;expiration:5/02/2016##NAMe:BrEAD;price:1.23;type:Food;expiration:1/25/2016##naMe:;price:3.23;type:Food^expiration:1/04/2016##";
16
+
13 17
     private String rawSingleItem =    "naMe:Milk;price:3.23;type:Food;expiration:1/25/2016##";
14 18
 
15 19
     private String rawSingleItemIrregularSeperatorSample = "naMe:MiLK;price:3.23;type:Food^expiration:1/11/2016##";
16 20
 
17
-    private String rawBrokenSingleItem =    "naMe:Milk;price:3.23;type:Food;expiration:1/25/2016##";
21
+    private String rawBrokenSingleItem =    "naMe:;price:3.23;type:Food;expiration:1/25/2016##";
18 22
 
19 23
     private String rawMultipleItems = "naMe:Milk;price:3.23;type:Food;expiration:1/25/2016##"
20 24
                                       +"naME:BreaD;price:1.23;type:Food;expiration:1/02/2016##"
@@ -59,4 +63,28 @@ public class ItemParserTest {
59 63
         Integer actual = itemParser.findKeyValuePairsInRawItemData(rawSingleItemIrregularSeperatorSample).size();
60 64
         assertEquals(expected, actual);
61 65
     }
66
+
67
+    @Test
68
+    public void testFindFoodNames() throws Exception {
69
+        String itemSearch = itemParser.nameFinder(therealRawData);
70
+        System.out.println(itemSearch);
71
+
72
+    }
73
+
74
+    @Test
75
+    public void testFindPrices() throws Exception {
76
+        String itemSearch = itemParser.priceFinder(therealRawData);
77
+        System.out.println(itemSearch);
78
+
79
+    }
80
+
81
+    @Test
82
+    public void testFindExpiration() throws Exception {
83
+        String itemSearch = itemParser.expFinder(therealRawData);
84
+        System.out.println(itemSearch);
85
+
86
+    }
87
+
88
+
89
+
62 90
 }