Keith Brinker 6 年 前
コミット
85df1d69db
共有3 個のファイルを変更した72 個の追加34 個の削除を含む
  1. 51
    27
      src/main/java/io/zipcoder/ItemParser.java
  2. 12
    6
      src/main/java/io/zipcoder/Main.java
  3. 9
    1
      src/test/java/io/zipcoder/ItemParserTest.java

+ 51
- 27
src/main/java/io/zipcoder/ItemParser.java ファイルの表示

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

+ 12
- 6
src/main/java/io/zipcoder/Main.java ファイルの表示

@@ -2,9 +2,7 @@ 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;
5
+import java.util.*;
8 6
 import java.util.regex.Matcher;
9 7
 import java.util.regex.Pattern;
10 8
 
@@ -12,18 +10,26 @@ import java.util.regex.Pattern;
12 10
 public class Main {
13 11
 
14 12
 
15
-    public String readRawDataToString() throws Exception{
13
+    public String readRawDataToString() throws Exception {
16 14
         ClassLoader classLoader = getClass().getClassLoader();
17 15
         String result = IOUtils.toString(classLoader.getResourceAsStream("RawData.txt"));
18 16
         return result;
19 17
     }
20 18
 
21
-    public static void main(String[] args) throws Exception{
19
+    public static void main(String[] args) throws Exception {
22 20
         String output = (new Main()).readRawDataToString();
23 21
 
24 22
         System.out.println(output);
25 23
         // TODO: parse the data in output into items, and display to console.
26 24
 
25
+
26
+        ItemParser itemParser = new ItemParser();
27
+        HashMap<String, ArrayList<Item>> testMap = itemParser.getCompleteList();
28
+        for (String key : testMap.keySet()) {
29
+            for (int i = 0; i < testMap.get(key).size(); i++) {
30
+                System.out.println(key + " " + testMap.get(key).get(i).getPrice());
31
+            }
32
+        }
33
+        }
27 34
     }
28 35
 
29
-}

+ 9
- 1
src/test/java/io/zipcoder/ItemParserTest.java ファイルの表示

@@ -18,7 +18,7 @@ public class ItemParserTest {
18 18
 
19 19
     private String rawSingleItemIrregularSeperatorSample = "naMe:MiLK;price:3.23;type:Food^expiration:1/11/2016##";
20 20
 
21
-    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##";
22 22
 
23 23
     private String rawMultipleItems = "naMe:Milk;price:3.23;type:Food;expiration:1/25/2016##"
24 24
                                       +"naME:BreaD;price:1.23;type:Food;expiration:1/02/2016##"
@@ -84,4 +84,12 @@ public class ItemParserTest {
84 84
         System.out.println(itemSearch);
85 85
 
86 86
     }
87
+
88
+    @Test
89
+    public void parseStringIntoItemTestMultiple() throws ItemParseException{
90
+        Item expected = new Item("milk", 3.23, "food","1/11/2016");
91
+        Item actual = itemParser.parseStringIntoItem(rawMultipleItems);
92
+        assertEquals(expected.toString(), actual.toString());
93
+    }
94
+
87 95
 }