Browse Source

It passes

NedRedmond 7 years ago
parent
commit
26a4dd4773

+ 6
- 2
src/main/java/rocks/zipcode/AlphaCharDocument.java View File

@@ -1,20 +1,24 @@
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.
7 8
  */
8 9
 public class AlphaCharDocument extends Document {
9
-    public AlphaCharDocument(String fileName) throws IOException {
10
+    public AlphaCharDocument(String fileName) {
10 11
         super(fileName);
11 12
     }
12 13
 
13 14
     @Override
14 15
     public void write(String contentToBeWritten) {
16
+        if (isAlpha(contentToBeWritten)) {
17
+            super.write(contentToBeWritten);
18
+        } else throw new IllegalArgumentException("Only alpha, baby");
15 19
     }
16 20
 
17 21
     private Boolean isAlpha(String s) {
18
-        return null;
22
+        return Pattern.matches("[a-zA-Z\\s]*", s);
19 23
     }
20 24
 }

+ 51
- 12
src/main/java/rocks/zipcode/Document.java View File

@@ -1,64 +1,103 @@
1 1
 package rocks.zipcode;
2 2
 
3 3
 import java.io.*;
4
-import java.nio.file.Files;
5
-import java.util.Arrays;
6 4
 import java.util.List;
5
+import java.util.stream.Collectors;
6
+import java.util.stream.Stream;
7 7
 
8 8
 /**
9 9
  * @author leon on 16/11/2018.
10 10
  */
11 11
 public class Document implements DocumentInterface {
12 12
 
13
-    private final FileWriter fileWriter;
14 13
     private final File file;
15 14
 
16
-    public Document(String fileName) throws IOException {
15
+    public Document(String fileName) {
17 16
         this(new File(fileName));
18 17
     }
19 18
 
20
-    public Document(File file) throws IOException {
19
+    public Document(File file) {
21 20
         this.file = file;
22
-        this.fileWriter = new FileWriter(file);
23 21
     }
24 22
 
25 23
     @Override
26 24
     public void write(String contentToBeWritten) {
25
+        BufferedWriter fileWriter = null;
26
+        try {
27
+            fileWriter = new BufferedWriter(new FileWriter(file));
28
+            fileWriter.write(contentToBeWritten);
29
+        } catch (IOException e) {
30
+            e.printStackTrace();
31
+        } finally {
32
+            if (fileWriter != null) {
33
+                try {
34
+                    fileWriter.close();
35
+                } catch (IOException e) {
36
+                    e.printStackTrace();
37
+                }
38
+            }
39
+
40
+        }
27 41
     }
28 42
 
29 43
     @Override
30
-    public void write(Integer lineNumber, String valueToBeWritten) {
44
+    public void write(Integer lineNumber, String valueToBeWritten) throws IllegalStateException {
45
+        List<String> textList = toList();
46
+        textList.set(lineNumber,valueToBeWritten);
47
+        String newText = String.join("\n", textList);
48
+        write(newText);
31 49
     }
32 50
 
33 51
     @Override
34 52
     public String read(Integer lineNumber) {
35
-        return null;
53
+        return toList().get(lineNumber);
36 54
     }
37 55
 
38 56
     @Override
39 57
     public String read() {
40
-        return null;
58
+        Stream<String> textStream = getStringStream();
59
+        return textStream.map(s -> s.toString()).collect(Collectors.joining("\n"));
60
+    }
61
+
62
+    private Stream<String> getStringStream() {
63
+        BufferedReader buffy = null;
64
+        try {
65
+            buffy = new BufferedReader(new FileReader(file));
66
+        } catch (FileNotFoundException e) {
67
+            e.printStackTrace();
68
+        }
69
+        return buffy.lines();
41 70
     }
42 71
 
43 72
     @Override
44 73
     public void replaceAll(String stringToReplace, String replacementString) {
74
+        String newString = read().replaceAll(stringToReplace,replacementString);
75
+        write(newString);
45 76
     }
46 77
 
47 78
     @Override
48 79
     public void overWrite(String content) {
80
+        this.write(content);
49 81
     }
50 82
 
51 83
     public List<String> toList() {
52
-        return null;
84
+        Stream<String> textStream = getStringStream();
85
+        return textStream.collect(Collectors.toList());
53 86
     }
54 87
 
55 88
     @Override
56 89
     public File getFile() {
57
-        return null;
90
+        return file;
58 91
     }
59 92
 
60 93
     @Override
61 94
     public String toString() {
62
-        return null;
95
+        String fileName = file.getPath();
96
+        String contentToBeWritten = read();
97
+        return new StringBuilder(fileName)
98
+                .append("{")
99
+                .append(contentToBeWritten)
100
+                .append("}")
101
+                .toString();
63 102
     }
64 103
 }

+ 2
- 1
src/main/java/rocks/zipcode/DocumentInterface.java View File

@@ -1,6 +1,7 @@
1 1
 package rocks.zipcode;
2 2
 
3 3
 import java.io.File;
4
+import java.io.IOException;
4 5
 import java.util.List;
5 6
 
6 7
 /**
@@ -13,7 +14,7 @@ public interface DocumentInterface {
13 14
 
14 15
     String read(Integer lineNumber);
15 16
 
16
-    String read();
17
+    String read() throws IOException;
17 18
 
18 19
     void replaceAll(String stringToReplace, String replacementString);
19 20
 

+ 6
- 2
src/main/java/rocks/zipcode/NumericCharDocument.java View File

@@ -1,20 +1,24 @@
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.
7 8
  */
8 9
 public class NumericCharDocument extends Document {
9
-    public NumericCharDocument(String fileName) throws IOException {
10
+    public NumericCharDocument(String fileName) {
10 11
         super(fileName);
11 12
     }
12 13
 
13 14
     @Override
14 15
     public void write(String contentToBeWritten) {
16
+        if (isNumeric(contentToBeWritten)) {
17
+            super.write(contentToBeWritten);
18
+        } else throw new IllegalArgumentException("Only numeric, baby");
15 19
     }
16 20
 
17 21
     private Boolean isNumeric(String s) {
18
-        return null;
22
+        return Pattern.matches("[\\d\\s]*", s);
19 23
     }
20 24
 }

+ 6
- 2
src/main/java/rocks/zipcode/SpecialCharDocument.java View File

@@ -1,20 +1,24 @@
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.
7 8
  */
8 9
 public class SpecialCharDocument extends Document {
9
-    public SpecialCharDocument(String fileName) throws IOException {
10
+    public SpecialCharDocument(String fileName) {
10 11
         super(fileName);
11 12
     }
12 13
 
13 14
     @Override
14 15
     public void write(String contentToBeWritten) {
16
+        if (isSpecialCharacters(contentToBeWritten)) {
17
+            super.write(contentToBeWritten);
18
+        } else throw new IllegalArgumentException("Only special, baby");
15 19
     }
16 20
 
17 21
     private Boolean isSpecialCharacters(String s) {
18
-        return null;
22
+        return Pattern.matches("[\\W_]*", s);
19 23
     }
20 24
 }