Browse Source

all test passing, refactoring

Trinh Tong 6 years ago
parent
commit
a070f1bad0

+ 19
- 1
src/main/java/rocks/zipcode/AlphaCharDocument.java View File

1
 package rocks.zipcode;
1
 package rocks.zipcode;
2
 
2
 
3
+import java.io.BufferedWriter;
3
 import java.io.IOException;
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
  * @author leon on 16/11/2018.
11
  * @author leon on 16/11/2018.
12
 
17
 
13
     @Override
18
     @Override
14
     public void write(String contentToBeWritten) {
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
     private Boolean isAlpha(String s) {
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 View File

1
 package rocks.zipcode;
1
 package rocks.zipcode;
2
 
2
 
3
+import java.io.BufferedWriter;
3
 import java.io.IOException;
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
  * @author leon on 16/11/2018.
11
  * @author leon on 16/11/2018.
12
 
17
 
13
     @Override
18
     @Override
14
     public void write(String contentToBeWritten) {
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
     private Boolean isNumeric(String s) {
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 View File

1
 package rocks.zipcode;
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
  * @author leon on 18/11/2018.
14
  * @author leon on 18/11/2018.
12
 
20
 
13
     @Override
21
     @Override
14
     public void write(String contentToBeWritten) {
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
     private Boolean isSpecialCharacters(String s) {
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
 }