コミット
711ae6280d

+ 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>7</source>
17
+                    <target>7</target>
18
+                </configuration>
19
+            </plugin>
20
+        </plugins>
21
+    </build>
10 22
 
11 23
     <dependencies>
12 24
         <dependency>

+ 7
- 3
src/main/java/io/zipcoder/Item.java ファイルの表示

@@ -15,10 +15,10 @@ public class Item {
15 15
      * @param type
16 16
      * @param expiration
17 17
      */
18
-    public Item(String name, Double price, String type, String expiration){
19
-        this.name = name;
18
+    public Item(String name, Double price, String type, String expiration)throws ItemParseException{
19
+        this.name = spellcheck(name.replace("0","o"));
20 20
         this.price = price;
21
-        this.type = type;
21
+        this.type = spellcheck(type);
22 22
         this.expiration = expiration;
23 23
     }
24 24
 
@@ -45,4 +45,8 @@ public class Item {
45 45
     public String toString(){
46 46
         return "name:" + name + " price:" + price + " type:" + type + " expiration:" + expiration;
47 47
     }
48
+    public String spellcheck(String item){
49
+
50
+        return null;
51
+    }
48 52
 }

+ 21
- 0
src/main/java/io/zipcoder/ItemParseException.java ファイルの表示

@@ -1,4 +1,25 @@
1 1
 package io.zipcoder;
2 2
 
3 3
 public class ItemParseException extends Exception {
4
+    private static int counter = 0;
5
+    private static final Object countLock = new Object();
6
+    public ItemParseException(String message) {
7
+       super(message);
8
+        // Only one thread can send a message
9
+        // at a time.
10
+       synchronized (countLock){
11
+           counter++;
12
+       }
13
+   }
14
+
15
+    public ItemParseException() {
16
+
17
+    }
18
+
19
+    public static int getCount(){
20
+        return counter;
21
+   }
22
+   public String getMessage(){
23
+        return super.getMessage() + "Errors             seen: " + getCount() + " times!!!";
24
+   }
4 25
 }

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

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

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

@@ -15,5 +15,10 @@ public class Main {
15 15
         String output = (new Main()).readRawDataToString();
16 16
         System.out.println(output);
17 17
         // TODO: parse the data in output into items, and display to console.
18
+        int error = 0;
19
+                            //name:  Milk   seen: 6 times
20
+        System.out.println("============= \t \t =============");
21
+
22
+        System.out.println("-------------\t\t -------------");
18 23
     }
19 24
 }

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

@@ -10,11 +10,11 @@ import static org.junit.Assert.*;
10 10
 
11 11
 public class ItemParserTest {
12 12
 
13
-    private String rawSingleItem =    "naMe:Milk;price:3.23;type:Food;expiration:1/25/2016##";
13
+    private String rawSingleItem = "naMe:Milk;price:3.23;type:Food;expiration:1/25/2016##";
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 =    "naM:Milk;price:3.23;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##"