소스 검색

all tests pass

Tennessee Gibbs 6 년 전
부모
커밋
f5e234e97c

+ 12
- 0
pom.xml 파일 보기

@@ -7,6 +7,18 @@
7 7
     <groupId>io.zipcoder</groupId>
8 8
     <artifactId>PainfullAfternoon</artifactId>
9 9
     <version>1.0-SNAPSHOT</version>
10
+    <build>
11
+        <plugins>
12
+            <plugin>
13
+                <groupId>org.apache.maven.plugins</groupId>
14
+                <artifactId>maven-compiler-plugin</artifactId>
15
+                <configuration>
16
+                    <source>6</source>
17
+                    <target>6</target>
18
+                </configuration>
19
+            </plugin>
20
+        </plugins>
21
+    </build>
10 22
 
11 23
     <dependencies>
12 24
         <dependency>

+ 4
- 0
src/main/java/io/zipcoder/Item.java 파일 보기

@@ -22,6 +22,10 @@ public class Item {
22 22
         this.expiration = expiration;
23 23
     }
24 24
 
25
+    public Item() {
26
+
27
+    }
28
+
25 29
     public String getName() {
26 30
         return name;
27 31
     }

+ 10
- 0
src/main/java/io/zipcoder/ItemParseException.java 파일 보기

@@ -1,4 +1,14 @@
1 1
 package io.zipcoder;
2 2
 
3
+import java.util.ArrayList;
4
+import java.util.HashMap;
5
+import java.util.List;
6
+import java.util.Map;
7
+
3 8
 public class ItemParseException extends Exception {
9
+
10
+
4 11
 }
12
+
13
+
14
+

+ 31
- 3
src/main/java/io/zipcoder/ItemParser.java 파일 보기

@@ -2,20 +2,44 @@ package io.zipcoder;
2 2
 
3 3
 import java.util.ArrayList;
4 4
 import java.util.Arrays;
5
+import java.util.List;
5 6
 
6 7
 public class ItemParser {
7
-
8
+    private String omit = "^[a-zA-Z0-9]+:";
9
+    private String spacer = "[;#^@!%*]";
10
+    private Item item;
8 11
 
9 12
     public ArrayList<String> parseRawDataIntoStringArray(String rawData){
10 13
         String stringPattern = "##";
11 14
         ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawData);
15
+
12 16
         return response;
13 17
     }
14 18
 
15 19
     public Item parseStringIntoItem(String rawItem) throws ItemParseException{
16
-        return null;
20
+        Item item;
21
+        int error = 0;
22
+        String[] splitItem = rawItem.split(spacer);
23
+        try {
24
+            String name = splitItem[0].toLowerCase().replaceAll(omit, "").replaceAll("0", "o");
25
+            Double price = Double.parseDouble(splitItem[1].replaceAll(omit, ""));
26
+            String type = splitItem[2].toLowerCase().replaceAll(omit, "");
27
+            String expiration = splitItem[3].toLowerCase().replaceAll(omit, "");
28
+
29
+            if (name.isEmpty() || price == null || type.isEmpty()|| expiration.isEmpty())
30
+                throw new Exception();
31
+            error++;
32
+            item = new Item(name, price, type, expiration);
33
+        } catch (Exception e) {
34
+
35
+            throw new ItemParseException();
36
+        }
37
+        System.out.println(item);
38
+        System.out.println(error);
39
+        return item;
17 40
     }
18 41
 
42
+
19 43
     public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){
20 44
         String stringPattern = "[;|^]";
21 45
         ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawItem);
@@ -23,9 +47,13 @@ public class ItemParser {
23 47
     }
24 48
 
25 49
     private ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString){
50
+        //System.out.println(new ArrayList<String>(Arrays.asList(inputString.split(stringPattern))));
26 51
         return new ArrayList<String>(Arrays.asList(inputString.split(stringPattern)));
27
-    }
28 52
 
53
+    }
29 54
 
30 55
 
56
+    public List <String> parseRawDataIntoStringArray() {
57
+        return null;
58
+    }
31 59
 }

+ 6
- 1
src/main/java/io/zipcoder/Main.java 파일 보기

@@ -12,8 +12,13 @@ public class Main {
12 12
     }
13 13
 
14 14
     public static void main(String[] args) throws Exception{
15
+        Item item = new Item();
16
+
17
+        String out = item.toString();
18
+
15 19
         String output = (new Main()).readRawDataToString();
16
-        System.out.println(output);
20
+
21
+        System.out.println(out);
17 22
         // TODO: parse the data in output into items, and display to console.
18 23
     }
19 24
 }

+ 2
- 1
src/test/java/io/zipcoder/ItemParserTest.java 파일 보기

@@ -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##"
@@ -39,6 +39,7 @@ public class ItemParserTest {
39 39
         Item expected = new Item("milk", 3.23, "food","1/25/2016");
40 40
         Item actual = itemParser.parseStringIntoItem(rawSingleItem);
41 41
         assertEquals(expected.toString(), actual.toString());
42
+
42 43
     }
43 44
 
44 45
     @Test(expected = ItemParseException.class)