Browse Source

making progress

Keith Brinker 6 years ago
parent
commit
9335f36a04

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

@@ -2,6 +2,10 @@ package io.zipcoder;
2 2
 
3 3
 import java.util.ArrayList;
4 4
 import java.util.Arrays;
5
+import java.util.Map;
6
+import java.util.Scanner;
7
+import java.util.regex.Matcher;
8
+import java.util.regex.Pattern;
5 9
 
6 10
 public class ItemParser {
7 11
 
@@ -13,7 +17,14 @@ public class ItemParser {
13 17
     }
14 18
 
15 19
     public Item parseStringIntoItem(String rawItem) throws ItemParseException{
16
-        return null;
20
+
21
+        String name = nameFinder(rawItem);
22
+        Double price = Double.parseDouble(priceFinder(rawItem));
23
+        String type = "food";
24
+        String expiration = expFinder(rawItem);
25
+        return new Item(name, price, type, expiration);
26
+
27
+
17 28
     }
18 29
 
19 30
     public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){
@@ -26,6 +37,50 @@ public class ItemParser {
26 37
         return new ArrayList<String>(Arrays.asList(inputString.split(stringPattern)));
27 38
     }
28 39
 
40
+    public String nameFinder (String rawNames) throws Exception {
41
+        StringBuilder found = new StringBuilder();
42
+
43
+        String itemSearch = rawNames;
44
+        Pattern foodNames = Pattern.compile("\\w+(?=\\Wprice)", Pattern.CASE_INSENSITIVE);
45
+        Matcher m = foodNames.matcher(itemSearch);
46
+        int lastMatchPos = 0;
47
+           while (m.find())
48
+            {
49
+                found.append(m.group()+"\n");
50
+                lastMatchPos = m.end();
51
+            }
52
+        return found.toString();
53
+    }
54
+
55
+    public String priceFinder (String rawPrices) throws Exception {
56
+        StringBuilder found = new StringBuilder();
57
+
58
+        String itemSearch = rawPrices;
59
+        Pattern foodNames = Pattern.compile("\\d+\\.\\d\\d", Pattern.CASE_INSENSITIVE);
60
+        Matcher m = foodNames.matcher(itemSearch);
61
+        int lastMatchPos = 0;
62
+        while (m.find())
63
+        {
64
+            found.append(m.group()+"\n");
65
+            lastMatchPos = m.end();
66
+        }
67
+        return found.toString();
68
+    }
69
+
70
+    public String expFinder (String rawExp) throws Exception {
71
+        StringBuilder found = new StringBuilder();
72
+
73
+        String itemSearch = rawExp;
74
+        Pattern foodNames = Pattern.compile("\\d{1,2}\\/\\d\\d\\/\\d{4}", Pattern.CASE_INSENSITIVE);
75
+        Matcher m = foodNames.matcher(itemSearch);
76
+        int lastMatchPos = 0;
77
+        while (m.find())
78
+        {
79
+            found.append(m.group()+"\n");
80
+            lastMatchPos = m.end();
81
+        }
82
+        return found.toString();
83
+    }
29 84
 
30 85
 
31 86
 }

+ 10
- 0
src/main/java/io/zipcoder/Main.java View File

@@ -2,9 +2,16 @@ package io.zipcoder;
2 2
 
3 3
 import org.apache.commons.io.IOUtils;
4 4
 
5
+import java.util.Iterator;
6
+import java.util.LinkedHashMap;
7
+import java.util.Map;
8
+import java.util.regex.Matcher;
9
+import java.util.regex.Pattern;
10
+
5 11
 
6 12
 public class Main {
7 13
 
14
+
8 15
     public String readRawDataToString() throws Exception{
9 16
         ClassLoader classLoader = getClass().getClassLoader();
10 17
         String result = IOUtils.toString(classLoader.getResourceAsStream("RawData.txt"));
@@ -13,7 +20,10 @@ public class Main {
13 20
 
14 21
     public static void main(String[] args) throws Exception{
15 22
         String output = (new Main()).readRawDataToString();
23
+
16 24
         System.out.println(output);
17 25
         // TODO: parse the data in output into items, and display to console.
26
+
18 27
     }
28
+
19 29
 }

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

@@ -5,11 +5,15 @@ 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##";
@@ -59,4 +63,25 @@ 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
+    }
62 87
 }