Browse Source

foundation and tests passing working on display

Joe Hendricks 6 years ago
parent
commit
c39b720e71

+ 1
- 1
src/main/java/io/zipcoder/Item.java View File

@@ -43,6 +43,6 @@ public class Item {
43 43
 
44 44
     @Override
45 45
     public String toString(){
46
-        return "name:" + name + " price:" + price + " type:" + type + " expiration:" + expiration;
46
+        return name + " " + price + " " + type + " " + expiration;
47 47
     }
48 48
 }

+ 1
- 0
src/main/java/io/zipcoder/ItemParseException.java View File

@@ -1,4 +1,5 @@
1 1
 package io.zipcoder;
2 2
 
3 3
 public class ItemParseException extends Exception {
4
+
4 5
 }

+ 106
- 8
src/main/java/io/zipcoder/ItemParser.java View File

@@ -2,30 +2,128 @@ package io.zipcoder;
2 2
 
3 3
 import java.util.ArrayList;
4 4
 import java.util.Arrays;
5
+import java.util.HashMap;
6
+import java.util.Map;
7
+import java.util.regex.Matcher;
8
+import java.util.regex.Pattern;
5 9
 
6 10
 public class ItemParser {
7 11
 
12
+    Pattern pattern;
13
+    Matcher matcher;
14
+    public int counter = 0;
8 15
 
9
-    public ArrayList<String> parseRawDataIntoStringArray(String rawData){
16
+    private Main main = new Main();
17
+    private Map<String, ArrayList<Item>> groceryMap = new HashMap<String, ArrayList<Item>>();
18
+
19
+
20
+    public ArrayList<String> parseRawDataIntoStringArray(String rawData) {
10 21
         String stringPattern = "##";
11
-        ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawData);
22
+        ArrayList<String> response = splitStringWithRegexPattern(stringPattern, rawData);
12 23
         return response;
13 24
     }
14 25
 
15
-    public Item parseStringIntoItem(String rawItem) throws ItemParseException{
16
-        return null;
26
+    public Item parseStringIntoItem(String rawItem) throws ItemParseException {
27
+
28
+        if (findName(rawItem) == null || findPrice(rawItem) == null) {
29
+            throw new ItemParseException();
30
+        }
31
+
32
+        String itemName = findName(rawItem);
33
+        Double itemPrice = Double.parseDouble(findPrice(rawItem));
34
+        String itemType = findType(rawItem);
35
+        String itemExpiration = findExpiration(rawItem);
36
+
37
+        return new Item(itemName, itemPrice, itemType, itemExpiration);
17 38
     }
18 39
 
19
-    public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){
40
+    public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem) {
20 41
         String stringPattern = "[;|^]";
21
-        ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawItem);
42
+        ArrayList<String> response = splitStringWithRegexPattern(stringPattern, rawItem);
22 43
         return response;
23 44
     }
24 45
 
25
-    private ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString){
46
+    private ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString) {
26 47
         return new ArrayList<String>(Arrays.asList(inputString.split(stringPattern)));
27 48
     }
28 49
 
50
+    private String findName(String rawItem){
51
+        String search = "(?<=([Nn][Aa][Mm][Ee][^A-Za-z])).*?(?=[^A-Za-z0])";
52
+        pattern = Pattern.compile(search);
53
+        matcher = pattern.matcher(rawItem);
54
+
55
+        if (matcher.find()) {
56
+            if (!matcher.group().equals("")) {
57
+                String name = matcher.group().replaceAll("\\d", "o");
58
+                return name.toLowerCase();
59
+                //return name.substring(0, 1).toUpperCase() + name.substring(1).toLowerCase();
60
+            }
61
+        }
62
+        return null;
63
+    }
64
+
65
+    private String findPrice(String rawItem){
66
+        pattern = Pattern.compile("(?<=([Pp][Rr][Ii][Cc][Ee][^A-Za-z])).*?(?=[^0-9.])");
67
+        matcher = pattern.matcher(rawItem);
68
+
69
+        if (matcher.find()) {
70
+            if (!matcher.group().equals("")) {
71
+                return matcher.group();
72
+            }
73
+        }
74
+        return null;
75
+    }
76
+
77
+    public String findType(String rawItem) {
78
+        pattern = Pattern.compile("(?<=([Tt][Yy][Pp][Ee][^A-Za-z])).*?(?=[^A-Za-z0])");
79
+        matcher = pattern.matcher(rawItem);
80
+
81
+        if (matcher.find()) {
82
+            return matcher.group().toLowerCase();
83
+        }
84
+        return null;
85
+    }
86
+
87
+    public String findExpiration(String rawItem) {
88
+        pattern = Pattern.compile("(?<=([Ee][Xx][Pp][Ii][Rr][Aa][Tt][Ii][Oo][Nn][^A-Za-z]))(.)+[^#]");
89
+        matcher = pattern.matcher(rawItem);
90
+        if (matcher.find()) {
91
+            return matcher.group();
92
+        }
93
+        return null;
94
+    }
29 95
 
96
+    public Map<String, ArrayList<Item>> getMap() throws Exception {
97
+
98
+        ArrayList<String> listOfItems = parseRawDataIntoStringArray(main.readRawDataToString());
99
+        for (String anItem : listOfItems) {
100
+            try {
101
+                Item newItem = parseStringIntoItem(anItem);
102
+                if (!groceryMap.containsKey(newItem.getName())) {
103
+                    ArrayList<Item> myItem = new ArrayList<Item>();
104
+                    myItem.add(newItem);
105
+                    groceryMap.put(newItem.getName(), myItem);
106
+                } else {
107
+                    groceryMap.get(newItem.getName()).add(newItem);
108
+                }
109
+            } catch (ItemParseException e) {
110
+                counter++;
111
+            }
112
+        }
113
+        return groceryMap;
114
+    }
115
+
116
+    public String display(){
117
+        for(Map.Entry<String, ArrayList<Item>> mapKey : groceryMap.entrySet()) {
118
+            System.out.println(mapKey.getKey());
119
+            for (Item item : mapKey.getValue()) {
120
+                System.out.println(item.getPrice());
121
+            }
122
+        }
123
+        System.out.println("Errors              seen: " + counter + " times");
124
+        return null;
125
+        }
126
+
127
+    public
128
+    }
30 129
 
31
-}

+ 18
- 2
src/main/java/io/zipcoder/Main.java View File

@@ -1,7 +1,12 @@
1 1
 package io.zipcoder;
2 2
 
3
+import com.sun.javafx.geom.Edge;
3 4
 import org.apache.commons.io.IOUtils;
4 5
 
6
+import java.util.ArrayList;
7
+import java.util.HashMap;
8
+import java.util.Map;
9
+
5 10
 
6 11
 public class Main {
7 12
 
@@ -11,9 +16,20 @@ public class Main {
11 16
         return result;
12 17
     }
13 18
 
14
-    public static void main(String[] args) throws Exception{
19
+    public static void main(String[] args) throws Exception {
15 20
         String output = (new Main()).readRawDataToString();
16
-        System.out.println(output);
21
+        //System.out.println(output);
17 22
         // TODO: parse the data in output into items, and display to console.
23
+
24
+        ItemParser test = new ItemParser();
25
+        Main mainTest = new Main();
26
+
27
+        ArrayList<String> arrayTest = test.parseRawDataIntoStringArray(output);
28
+
29
+
30
+        System.out.println(test.parseRawDataIntoStringArray(mainTest.readRawDataToString()));
31
+
18 32
     }
33
+
19 34
 }
35
+

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

@@ -14,7 +14,7 @@ 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:;type:Food;expiration:1/25/2016##";
18 18
 
19 19
     private String rawMultipleItems = "naMe:Milk;price:3.23;type:Food;expiration:1/25/2016##"
20 20
                                       +"naME:BreaD;price:1.23;type:Food;expiration:1/02/2016##"