Browse Source

All tests passed

Lauren Green 7 years ago
parent
commit
4ed5653e00

+ 23
- 3
src/main/java/rocks/zipcode/Document.java View File

@@ -1,6 +1,9 @@
1 1
 package rocks.zipcode;
2 2
 
3 3
 import java.io.*;
4
+import java.nio.file.Files;
5
+import java.nio.file.Paths;
6
+import java.util.ArrayList;
4 7
 import java.util.List;
5 8
 
6 9
 /**
@@ -35,6 +38,12 @@ public class Document implements DocumentInterface {
35 38
 
36 39
     @Override
37 40
     public void write(Integer lineNumber, String valueToBeWritten) {
41
+
42
+            String line = read(lineNumber);
43
+            String doc = read();
44
+            doc = doc.replace(line, valueToBeWritten);
45
+            write(doc);
46
+
38 47
     }
39 48
 
40 49
     @Override
@@ -95,23 +104,34 @@ public class Document implements DocumentInterface {
95 104
 
96 105
     @Override
97 106
     public void replaceAll(String stringToReplace, String replacementString) {
107
+
108
+        String doc = read();
109
+        doc = doc.replaceAll(stringToReplace, replacementString);
110
+        write(doc);
111
+
98 112
     }
99 113
 
100 114
     @Override
101 115
     public void overWrite(String content) {
116
+        write(content);
102 117
     }
103 118
 
104 119
     public List<String> toList() {
105
-        return null;
120
+        String[] arr = read().split("\n");
121
+        List<String> result = new ArrayList<>();
122
+        for(String s: arr) {
123
+            result.add(s);
124
+        }
125
+        return result;
106 126
     }
107 127
 
108 128
     @Override
109 129
     public File getFile() {
110
-        return null;
130
+        return this.file;
111 131
     }
112 132
 
113 133
     @Override
114 134
     public String toString() {
115
-        return null;
135
+        return getFile() + "{" + read() + "}";
116 136
     }
117 137
 }

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

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

+ 11
- 1
src/main/java/rocks/zipcode/SpecialCharDocument.java View File

@@ -1,6 +1,8 @@
1 1
 package rocks.zipcode;
2 2
 
3 3
 import java.io.IOException;
4
+import java.util.regex.Matcher;
5
+import java.util.regex.Pattern;
4 6
 
5 7
 /**
6 8
  * @author leon on 18/11/2018.
@@ -12,9 +14,17 @@ public class SpecialCharDocument extends Document {
12 14
 
13 15
     @Override
14 16
     public void write(String contentToBeWritten) {
17
+        if(isSpecialCharacters(contentToBeWritten)) {
18
+            super.write(contentToBeWritten);
19
+        } else {
20
+            throw new IllegalArgumentException();
21
+        }
15 22
     }
16 23
 
17 24
     private Boolean isSpecialCharacters(String s) {
18
-        return null;
25
+
26
+        Pattern pattern = Pattern.compile("^[!@#$%^&*()_ ]+$");
27
+        Matcher match = pattern.matcher(s);
28
+        return match.find();
19 29
     }
20 30
 }