April Rivera 6 年之前
父節點
當前提交
169665e2d3

+ 4
- 0
src/main/java/io/zipcoder/Item.java 查看文件

@@ -1,11 +1,14 @@
1 1
 package io.zipcoder;
2 2
 
3
+import java.util.ArrayList;
4
+
3 5
 public class Item {
4 6
     private String name;
5 7
     private Double price;
6 8
     private String type;
7 9
     private String expiration;
8 10
 
11
+
9 12
     /**
10 13
      * Item should not be created unless you have all of the elements, which is why you are forcing
11 14
      * it to be set in the constructor. In ItemParser, if you do not find all the elements of a Item,
@@ -45,4 +48,5 @@ public class Item {
45 48
     public String toString(){
46 49
         return "name:" + name + " price:" + price + " type:" + type + " expiration:" + expiration;
47 50
     }
51
+
48 52
 }

+ 15
- 0
src/main/java/io/zipcoder/ItemList.java 查看文件

@@ -0,0 +1,15 @@
1
+package io.zipcoder;
2
+
3
+import java.util.ArrayList;
4
+
5
+public class ItemList {
6
+
7
+    ArrayList<String> itemList = new ArrayList<String>();
8
+
9
+    public ArrayList<String> getItemList() {
10
+        return itemList;
11
+    }
12
+    public void addItem(String s){
13
+        itemList.add(s);
14
+    }
15
+}

+ 1
- 2
src/main/java/io/zipcoder/ItemParser.java 查看文件

@@ -7,7 +7,6 @@ import java.util.regex.Pattern;
7 7
 
8 8
 public class ItemParser {
9 9
 
10
-
11 10
     public ArrayList<String> parseRawDataIntoStringArray(String rawData){
12 11
         String stringPattern = "##";
13 12
         ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawData);
@@ -28,7 +27,7 @@ public class ItemParser {
28 27
         String expiration;
29 28
         Item item = null;
30 29
 
31
-        Matcher m = Pattern.compile(":([^,].*?)(;|##|\\^|!|%|\\*|@)").matcher(rawItem);
30
+        Matcher m = Pattern.compile(":([^,].+?)(\\W|;|\\^|!|%|\\*|@)").matcher(rawItem);
32 31
 
33 32
         int numberOfMatches = countMatches(m);
34 33
         final int REQUIRED_NUMBER_OF_FIELDS_FOR_ITEM = 4;

+ 5
- 3
src/main/java/io/zipcoder/Main.java 查看文件

@@ -7,6 +7,8 @@ import java.util.ArrayList;
7 7
 
8 8
 public class Main {
9 9
 
10
+    static ItemList itemList = new ItemList();
11
+
10 12
     public ArrayList<String> readRawDataToString() throws Exception {
11 13
         ClassLoader classLoader = getClass().getClassLoader();
12 14
         String result = IOUtils.toString(classLoader.getResourceAsStream("RawData.txt"));
@@ -19,10 +21,10 @@ public class Main {
19 21
     public static void main(String[] args) throws Exception {
20 22
         ArrayList<String> output = (new Main()).readRawDataToString();
21 23
         ItemParser itemParser = new ItemParser();
22
-        ArrayList<String> lastArray = itemParser.findKeyValuePairsInRawItemData(output.toString());
23
-        for (String s : lastArray) {
24
+        for (String s : output) {
24 25
             try{
25
-            Item item = itemParser.parseStringIntoItem(s);
26
+            itemParser.parseStringIntoItem(s);
27
+
26 28
 
27 29
             } catch (ItemParseException e) {
28 30
                     continue;