mpierse 7 лет назад
Родитель
Сommit
f32f021958

+ 26
- 2
src/main/java/rocks/zipcode/AlphaCharDocument.java Просмотреть файл

@@ -1,20 +1,44 @@
1 1
 package rocks.zipcode;
2 2
 
3
-import java.io.IOException;
3
+import java.io.*;
4 4
 
5 5
 /**
6 6
  * @author leon on 16/11/2018.
7 7
  */
8 8
 public class AlphaCharDocument extends Document {
9
+
10
+private FileWriter fw;
11
+private String fileName;
12
+private File file = new File(fileName);
13
+
9 14
     public AlphaCharDocument(String fileName) throws IOException {
10 15
         super(fileName);
16
+        this.fileName = fileName;
17
+        fw = new FileWriter(fileName);
11 18
     }
12 19
 
13 20
     @Override
14 21
     public void write(String contentToBeWritten) {
22
+        try {
23
+            isAlpha(contentToBeWritten);
24
+            fw.write(contentToBeWritten);
25
+        } catch (IOException ex) {
26
+            ex.printStackTrace();
27
+        }
15 28
     }
16 29
 
17
-    private Boolean isAlpha(String s) {
30
+    public String read() throws FileNotFoundException {
31
+        BufferedReader br = new BufferedReader(new FileReader(file));
32
+        try {
33
+            return br.readLine();
34
+        } catch (IOException e) {
35
+            e.printStackTrace();
36
+        }
18 37
         return null;
19 38
     }
39
+
40
+    private void isAlpha(String s) throws IOException {
41
+        if(!s.matches("[a-zA-Z\\s]*"))
42
+            throw new IllegalArgumentException();
43
+    }
20 44
 }

+ 1
- 1
src/main/java/rocks/zipcode/Document.java Просмотреть файл

@@ -36,7 +36,7 @@ public class Document implements DocumentInterface {
36 36
     }
37 37
 
38 38
     @Override
39
-    public String read() {
39
+    public String read() throws FileNotFoundException {
40 40
         return null;
41 41
     }
42 42
 

+ 2
- 1
src/main/java/rocks/zipcode/DocumentInterface.java Просмотреть файл

@@ -1,6 +1,7 @@
1 1
 package rocks.zipcode;
2 2
 
3 3
 import java.io.File;
4
+import java.io.FileNotFoundException;
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 FileNotFoundException;
17 18
 
18 19
     void replaceAll(String stringToReplace, String replacementString);
19 20
 

+ 1
- 1
src/test/java/rocks/zipcode/alphadocument/AlphaDocumentWriteTest.java Просмотреть файл

@@ -45,7 +45,7 @@ public class AlphaDocumentWriteTest {
45 45
         // when
46 46
         documentWriter.write(expected);
47 47
         String actual = documentWriter.read();
48
-
48
+        System.out.println(actual);
49 49
         // then
50 50
         Assert.assertEquals(expected, actual);
51 51
     }