Bläddra i källkod

all tests pass

Tennessee Gibbs 6 år sedan
förälder
incheckning
f5e234e97c

+ 12
- 0
pom.xml Visa fil

7
     <groupId>io.zipcoder</groupId>
7
     <groupId>io.zipcoder</groupId>
8
     <artifactId>PainfullAfternoon</artifactId>
8
     <artifactId>PainfullAfternoon</artifactId>
9
     <version>1.0-SNAPSHOT</version>
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
     <dependencies>
23
     <dependencies>
12
         <dependency>
24
         <dependency>

+ 4
- 0
src/main/java/io/zipcoder/Item.java Visa fil

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

+ 10
- 0
src/main/java/io/zipcoder/ItemParseException.java Visa fil

1
 package io.zipcoder;
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
 public class ItemParseException extends Exception {
8
 public class ItemParseException extends Exception {
9
+
10
+
4
 }
11
 }
12
+
13
+
14
+

+ 31
- 3
src/main/java/io/zipcoder/ItemParser.java Visa fil

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.List;
5
 
6
 
6
 public class ItemParser {
7
 public class ItemParser {
7
-
8
+    private String omit = "^[a-zA-Z0-9]+:";
9
+    private String spacer = "[;#^@!%*]";
10
+    private Item item;
8
 
11
 
9
     public ArrayList<String> parseRawDataIntoStringArray(String rawData){
12
     public ArrayList<String> parseRawDataIntoStringArray(String rawData){
10
         String stringPattern = "##";
13
         String stringPattern = "##";
11
         ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawData);
14
         ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawData);
15
+
12
         return response;
16
         return response;
13
     }
17
     }
14
 
18
 
15
     public Item parseStringIntoItem(String rawItem) throws ItemParseException{
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
     public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){
43
     public ArrayList<String> findKeyValuePairsInRawItemData(String rawItem){
20
         String stringPattern = "[;|^]";
44
         String stringPattern = "[;|^]";
21
         ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawItem);
45
         ArrayList<String> response = splitStringWithRegexPattern(stringPattern , rawItem);
23
     }
47
     }
24
 
48
 
25
     private ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString){
49
     private ArrayList<String> splitStringWithRegexPattern(String stringPattern, String inputString){
50
+        //System.out.println(new ArrayList<String>(Arrays.asList(inputString.split(stringPattern))));
26
         return new ArrayList<String>(Arrays.asList(inputString.split(stringPattern)));
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 Visa fil

12
     }
12
     }
13
 
13
 
14
     public static void main(String[] args) throws Exception{
14
     public static void main(String[] args) throws Exception{
15
+        Item item = new Item();
16
+
17
+        String out = item.toString();
18
+
15
         String output = (new Main()).readRawDataToString();
19
         String output = (new Main()).readRawDataToString();
16
-        System.out.println(output);
20
+
21
+        System.out.println(out);
17
         // TODO: parse the data in output into items, and display to console.
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 Visa fil

14
 
14
 
15
     private String rawSingleItemIrregularSeperatorSample = "naMe:MiLK;price:3.23;type:Food^expiration:1/11/2016##";
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
     private String rawMultipleItems = "naMe:Milk;price:3.23;type:Food;expiration:1/25/2016##"
19
     private String rawMultipleItems = "naMe:Milk;price:3.23;type:Food;expiration:1/25/2016##"
20
                                       +"naME:BreaD;price:1.23;type:Food;expiration:1/02/2016##"
20
                                       +"naME:BreaD;price:1.23;type:Food;expiration:1/02/2016##"
39
         Item expected = new Item("milk", 3.23, "food","1/25/2016");
39
         Item expected = new Item("milk", 3.23, "food","1/25/2016");
40
         Item actual = itemParser.parseStringIntoItem(rawSingleItem);
40
         Item actual = itemParser.parseStringIntoItem(rawSingleItem);
41
         assertEquals(expected.toString(), actual.toString());
41
         assertEquals(expected.toString(), actual.toString());
42
+
42
     }
43
     }
43
 
44
 
44
     @Test(expected = ItemParseException.class)
45
     @Test(expected = ItemParseException.class)