Jennifer Chao 6 年前
父节点
当前提交
9b6e319d39

+ 7
- 1
src/main/java/rocks/zipcode/AlphaCharDocument.java 查看文件

@@ -1,6 +1,7 @@
1 1
 package rocks.zipcode;
2 2
 
3 3
 import java.io.IOException;
4
+import java.util.regex.Pattern;
4 5
 
5 6
 /**
6 7
  * @author leon on 16/11/2018.
@@ -12,9 +13,14 @@ public class AlphaCharDocument extends Document {
12 13
 
13 14
     @Override
14 15
     public void write(String contentToBeWritten) {
16
+        if (isAlpha(contentToBeWritten)) {
17
+            super.write(contentToBeWritten);
18
+        } else {
19
+            throw new IllegalArgumentException();
20
+        }
15 21
     }
16 22
 
17 23
     private Boolean isAlpha(String s) {
18
-        return null;
24
+        return Pattern.matches("^[a-zA-Z\\s]+$", s);
19 25
     }
20 26
 }

+ 46
- 5
src/main/java/rocks/zipcode/Document.java 查看文件

@@ -2,6 +2,7 @@ package rocks.zipcode;
2 2
 
3 3
 import java.io.*;
4 4
 import java.nio.file.Files;
5
+import java.util.ArrayList;
5 6
 import java.util.Arrays;
6 7
 import java.util.List;
7 8
 
@@ -24,41 +25,81 @@ public class Document implements DocumentInterface {
24 25
 
25 26
     @Override
26 27
     public void write(String contentToBeWritten) {
28
+        try {
29
+            BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file));
30
+            bufferedWriter.write(contentToBeWritten);
31
+            bufferedWriter.close();
32
+        } catch (IOException e) {
33
+            e.printStackTrace();
34
+        }
27 35
     }
28 36
 
29 37
     @Override
30 38
     public void write(Integer lineNumber, String valueToBeWritten) {
39
+        String[] array = read().split("\n");
40
+        array[lineNumber] = valueToBeWritten;
41
+        String newFile = "";
42
+        for (String element : array) {
43
+            newFile += element + "\n";
44
+        }
45
+        write(newFile.trim());
31 46
     }
32 47
 
33 48
     @Override
34 49
     public String read(Integer lineNumber) {
35
-        return null;
50
+        try {
51
+            BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
52
+            String line = "";
53
+            for (int i = 0; i <= lineNumber; i++) {
54
+                line = bufferedReader.readLine();
55
+            }
56
+            bufferedReader.close();
57
+            return line;
58
+        } catch (IOException e) {
59
+            e.printStackTrace();
60
+            return null;
61
+        }
36 62
     }
37 63
 
38 64
     @Override
39 65
     public String read() {
40
-        return null;
66
+        try {
67
+            BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
68
+            String stringFile = "";
69
+            String line;
70
+            while ((line = bufferedReader.readLine()) != null) {
71
+                stringFile += line + "\n";
72
+            }
73
+            bufferedReader.close();
74
+            return stringFile.trim();
75
+        } catch (IOException e) {
76
+            e.printStackTrace();
77
+            return null;
78
+        }
41 79
     }
42 80
 
43 81
     @Override
44 82
     public void replaceAll(String stringToReplace, String replacementString) {
83
+        String newFile = read().replaceAll(stringToReplace, replacementString);
84
+        write(newFile);
45 85
     }
46 86
 
47 87
     @Override
48 88
     public void overWrite(String content) {
89
+        write(content);
49 90
     }
50 91
 
51 92
     public List<String> toList() {
52
-        return null;
93
+        return Arrays.asList(read().split("\n"));
53 94
     }
54 95
 
55 96
     @Override
56 97
     public File getFile() {
57
-        return null;
98
+        return file;
58 99
     }
59 100
 
60 101
     @Override
61 102
     public String toString() {
62
-        return null;
103
+        return file + "{" + read() + "}";
63 104
     }
64 105
 }

+ 7
- 1
src/main/java/rocks/zipcode/NumericCharDocument.java 查看文件

@@ -1,6 +1,7 @@
1 1
 package rocks.zipcode;
2 2
 
3 3
 import java.io.IOException;
4
+import java.util.regex.Pattern;
4 5
 
5 6
 /**
6 7
  * @author leon on 16/11/2018.
@@ -12,9 +13,14 @@ public class NumericCharDocument extends Document {
12 13
 
13 14
     @Override
14 15
     public void write(String contentToBeWritten) {
16
+        if (isNumeric(contentToBeWritten)) {
17
+            super.write(contentToBeWritten);
18
+        } else {
19
+            throw new IllegalArgumentException();
20
+        }
15 21
     }
16 22
 
17 23
     private Boolean isNumeric(String s) {
18
-        return null;
24
+        return Pattern.matches("^[0-9]+$", s);
19 25
     }
20 26
 }

+ 7
- 1
src/main/java/rocks/zipcode/SpecialCharDocument.java 查看文件

@@ -1,6 +1,7 @@
1 1
 package rocks.zipcode;
2 2
 
3 3
 import java.io.IOException;
4
+import java.util.regex.Pattern;
4 5
 
5 6
 /**
6 7
  * @author leon on 18/11/2018.
@@ -12,9 +13,14 @@ public class SpecialCharDocument extends Document {
12 13
 
13 14
     @Override
14 15
     public void write(String contentToBeWritten) {
16
+        if (isSpecialCharacters(contentToBeWritten)) {
17
+            super.write(contentToBeWritten);
18
+        } else {
19
+            throw new IllegalArgumentException();
20
+        }
15 21
     }
16 22
 
17 23
     private Boolean isSpecialCharacters(String s) {
18
-        return null;
24
+        return Pattern.matches("^[\\W_]+$", s);
19 25
     }
20 26
 }