mpierse преди 6 години
родител
ревизия
cbe82ce85f
променени са 3 файла, в които са добавени 61 реда и са изтрити 6 реда
  1. 1
    1
      src/main/java/io/zipcoder/ItemParseException.java
  2. 33
    1
      src/main/java/io/zipcoder/ItemParser.java
  3. 27
    4
      src/main/java/io/zipcoder/Main.java

+ 1
- 1
src/main/java/io/zipcoder/ItemParseException.java Целия файл

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

+ 33
- 1
src/main/java/io/zipcoder/ItemParser.java Целия файл

2
 
2
 
3
 import java.util.ArrayList;
3
 import java.util.ArrayList;
4
 import java.util.Arrays;
4
 import java.util.Arrays;
5
+import java.util.logging.Level;
6
+import java.util.logging.Logger;
7
+import java.util.regex.Matcher;
8
+import java.util.regex.Pattern;
5
 
9
 
6
 public class ItemParser {
10
 public class ItemParser {
7
 
11
 
12
+    private static final Logger log = Logger.getLogger( ItemParser.class.getName() );
13
+
8
 
14
 
9
     public ArrayList<String> parseRawDataIntoStringArray(String rawData){
15
     public ArrayList<String> parseRawDataIntoStringArray(String rawData){
10
         String stringPattern = "##";
16
         String stringPattern = "##";
12
         return response;
18
         return response;
13
     }
19
     }
14
 
20
 
21
+    public String correctCase(){
22
+        return "";
23
+    }
24
+
15
     public Item parseStringIntoItem(String rawItem) throws ItemParseException{
25
     public Item parseStringIntoItem(String rawItem) throws ItemParseException{
26
+            ArrayList<String> properties = findKeyValuePairsInRawItemData(rawItem);
27
+        Pattern patternAlpha = Pattern.compile("(?<=:).*");
28
+        Pattern patternDate = Pattern.compile("\\d/\\d\\d/\\d\\d\\d\\d");
29
+        Pattern patternDouble = Pattern.compile("\\d.\\d\\d");
30
+        Matcher mName = patternAlpha.matcher(properties.get(0));
31
+        Matcher mPrice = patternDouble.matcher(properties.get(1));
32
+        Matcher mDescription = patternAlpha.matcher(properties.get(2));
33
+        Matcher mExpiration = patternDate.matcher(properties.get(3));
34
+
35
+try {
36
+
37
+    if (mName.find() && mDescription.find() && mPrice.find() && mExpiration.find()) {
38
+        String name = mName.group();
39
+        Double price = Double.parseDouble(mPrice.group());
40
+        String type = mDescription.group();
41
+        String expiration = mExpiration.group();
42
+        return new Item(name, price, type, expiration);
43
+    }
44
+    } catch( ItemParseException e ){
45
+        log.log(Level.WARNING, "invalid");
46
+    }
47
+
16
         return null;
48
         return null;
17
     }
49
     }
18
 
50
 
19
     public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){
51
     public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){
20
-        String stringPattern = "[;|^]";
52
+        String stringPattern = "[;|^!%@*]";
21
         ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawItem);
53
         ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawItem);
22
         return response;
54
         return response;
23
     }
55
     }

+ 27
- 4
src/main/java/io/zipcoder/Main.java Целия файл

2
 
2
 
3
 import org.apache.commons.io.IOUtils;
3
 import org.apache.commons.io.IOUtils;
4
 
4
 
5
+import java.io.IOException;
6
+import java.util.ArrayList;
7
+import java.util.logging.Level;
8
+import java.util.logging.Logger;
9
+
5
 
10
 
6
 public class Main {
11
 public class Main {
7
 
12
 
8
-    public String readRawDataToString() throws Exception{
13
+    private static final Logger log = Logger.getLogger( ItemParser.class.getName() );
14
+
15
+    public String readRawDataToString() {
9
         ClassLoader classLoader = getClass().getClassLoader();
16
         ClassLoader classLoader = getClass().getClassLoader();
10
-        String result = IOUtils.toString(classLoader.getResourceAsStream("RawData.txt"));
17
+        String result = null;
18
+        try {
19
+            result = IOUtils.toString(classLoader.getResourceAsStream("RawData.txt"));
20
+        } catch (IOException e) {
21
+            e.printStackTrace();
22
+        }
11
         return result;
23
         return result;
12
     }
24
     }
13
 
25
 
14
-    public static void main(String[] args) throws Exception{
26
+    public static void main(String[] args) throws ItemParseException{
15
         String output = (new Main()).readRawDataToString();
27
         String output = (new Main()).readRawDataToString();
16
-        System.out.println(output);
28
+        ItemParser parser = new ItemParser();
29
+        ArrayList<String> items = parser.parseRawDataIntoStringArray(output);
30
+        for (String item : items) {
31
+           try{
32
+            Item itemObj = parser.parseStringIntoItem(item);
33
+           System.out.println(itemObj.toString());}
34
+           catch (NullPointerException e){
35
+               log.log(Level.WARNING, "null pointer");
36
+               continue;
37
+           }
38
+        }
39
+      //  System.out.println(output);
17
         // TODO: parse the data in output into items, and display to console.
40
         // TODO: parse the data in output into items, and display to console.
18
     }
41
     }
19
 }
42
 }