Преглед на файлове

all test passing, refactoring

Trinh Tong преди 6 години
родител
ревизия
a070f1bad0

+ 19
- 1
src/main/java/rocks/zipcode/AlphaCharDocument.java Целия файл

@@ -1,6 +1,11 @@
1 1
 package rocks.zipcode;
2 2
 
3
+import java.io.BufferedWriter;
3 4
 import java.io.IOException;
5
+import java.nio.file.Files;
6
+import java.nio.file.Paths;
7
+import java.util.regex.Matcher;
8
+import java.util.regex.Pattern;
4 9
 
5 10
 /**
6 11
  * @author leon on 16/11/2018.
@@ -12,9 +17,22 @@ public class AlphaCharDocument extends Document {
12 17
 
13 18
     @Override
14 19
     public void write(String contentToBeWritten) {
20
+        if (isAlpha(contentToBeWritten)) {
21
+            try {
22
+                BufferedWriter out = Files.newBufferedWriter(Paths.get(getFile().getAbsolutePath()));
23
+                out.write(contentToBeWritten);
24
+                out.close();
25
+            } catch(IOException e) {
26
+                e.printStackTrace();
27
+            }
28
+        } else {
29
+            throw new IllegalArgumentException();
30
+        }
15 31
     }
16 32
 
17 33
     private Boolean isAlpha(String s) {
18
-        return null;
34
+        Pattern p = Pattern.compile("[^a-zA-Z ]");
35
+        Matcher m = p.matcher(s);
36
+        return !m.find();
19 37
     }
20 38
 }

+ 19
- 1
src/main/java/rocks/zipcode/NumericCharDocument.java Целия файл

@@ -1,6 +1,11 @@
1 1
 package rocks.zipcode;
2 2
 
3
+import java.io.BufferedWriter;
3 4
 import java.io.IOException;
5
+import java.nio.file.Files;
6
+import java.nio.file.Paths;
7
+import java.util.regex.Matcher;
8
+import java.util.regex.Pattern;
4 9
 
5 10
 /**
6 11
  * @author leon on 16/11/2018.
@@ -12,9 +17,22 @@ public class NumericCharDocument extends Document {
12 17
 
13 18
     @Override
14 19
     public void write(String contentToBeWritten) {
20
+        if (isNumeric(contentToBeWritten)) {
21
+            try {
22
+                BufferedWriter out = Files.newBufferedWriter(Paths.get(getFile().getAbsolutePath()));
23
+                out.write(contentToBeWritten);
24
+                out.close();
25
+            } catch(IOException e) {
26
+                e.printStackTrace();
27
+            }
28
+        } else {
29
+            throw new IllegalArgumentException();
30
+        }
15 31
     }
16 32
 
17 33
     private Boolean isNumeric(String s) {
18
-        return null;
34
+        Pattern p = Pattern.compile("[\\D]");
35
+        Matcher m = p.matcher(s);
36
+        return !m.find();
19 37
     }
20 38
 }

+ 22
- 2
src/main/java/rocks/zipcode/SpecialCharDocument.java Целия файл

@@ -1,6 +1,14 @@
1 1
 package rocks.zipcode;
2 2
 
3
-import java.io.IOException;
3
+import sun.nio.cs.UTF_32;
4
+
5
+import java.io.*;
6
+import java.nio.file.Files;
7
+import java.nio.file.Paths;
8
+import java.util.regex.Matcher;
9
+import java.util.regex.Pattern;
10
+
11
+import static java.nio.charset.StandardCharsets.UTF_16;
4 12
 
5 13
 /**
6 14
  * @author leon on 18/11/2018.
@@ -12,9 +20,21 @@ public class SpecialCharDocument extends Document {
12 20
 
13 21
     @Override
14 22
     public void write(String contentToBeWritten) {
23
+        if (isSpecialCharacters(contentToBeWritten)) {
24
+            try {
25
+                BufferedWriter out = Files.newBufferedWriter(Paths.get(getFile().getAbsolutePath()));
26
+                out.write(contentToBeWritten);
27
+            } catch (IOException io) {
28
+                System.out.println("An unexpected IO exception has occurred: " + io.getMessage());
29
+            }
30
+        } else {
31
+            throw new IllegalArgumentException();
32
+        }
15 33
     }
16 34
 
17 35
     private Boolean isSpecialCharacters(String s) {
18
-        return null;
36
+        Pattern p = Pattern.compile("[\\w&&[^_]]");
37
+        Matcher m = p.matcher(s);
38
+        return !m.find();
19 39
     }
20 40
 }