Jennifer Chao 6 years ago
parent
commit
9b6e319d39

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

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

2
 
2
 
3
 import java.io.*;
3
 import java.io.*;
4
 import java.nio.file.Files;
4
 import java.nio.file.Files;
5
+import java.util.ArrayList;
5
 import java.util.Arrays;
6
 import java.util.Arrays;
6
 import java.util.List;
7
 import java.util.List;
7
 
8
 
24
 
25
 
25
     @Override
26
     @Override
26
     public void write(String contentToBeWritten) {
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
     @Override
37
     @Override
30
     public void write(Integer lineNumber, String valueToBeWritten) {
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
     @Override
48
     @Override
34
     public String read(Integer lineNumber) {
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
     @Override
64
     @Override
39
     public String read() {
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
     @Override
81
     @Override
44
     public void replaceAll(String stringToReplace, String replacementString) {
82
     public void replaceAll(String stringToReplace, String replacementString) {
83
+        String newFile = read().replaceAll(stringToReplace, replacementString);
84
+        write(newFile);
45
     }
85
     }
46
 
86
 
47
     @Override
87
     @Override
48
     public void overWrite(String content) {
88
     public void overWrite(String content) {
89
+        write(content);
49
     }
90
     }
50
 
91
 
51
     public List<String> toList() {
92
     public List<String> toList() {
52
-        return null;
93
+        return Arrays.asList(read().split("\n"));
53
     }
94
     }
54
 
95
 
55
     @Override
96
     @Override
56
     public File getFile() {
97
     public File getFile() {
57
-        return null;
98
+        return file;
58
     }
99
     }
59
 
100
 
60
     @Override
101
     @Override
61
     public String toString() {
102
     public String toString() {
62
-        return null;
103
+        return file + "{" + read() + "}";
63
     }
104
     }
64
 }
105
 }

+ 7
- 1
src/main/java/rocks/zipcode/NumericCharDocument.java View File

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

1
 package rocks.zipcode;
1
 package rocks.zipcode;
2
 
2
 
3
 import java.io.IOException;
3
 import java.io.IOException;
4
+import java.util.regex.Pattern;
4
 
5
 
5
 /**
6
 /**
6
  * @author leon on 18/11/2018.
7
  * @author leon on 18/11/2018.
12
 
13
 
13
     @Override
14
     @Override
14
     public void write(String contentToBeWritten) {
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
     private Boolean isSpecialCharacters(String s) {
23
     private Boolean isSpecialCharacters(String s) {
18
-        return null;
24
+        return Pattern.matches("^[\\W_]+$", s);
19
     }
25
     }
20
 }
26
 }