Przeglądaj źródła

backup progress

Lauren Green 7 lat temu
rodzic
commit
fd574aa3a4

+ 11
- 1
src/main/java/rocks/zipcode/AlphaCharDocument.java Wyświetl plik

@@ -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,17 @@ public class AlphaCharDocument extends Document {
12 14
 
13 15
     @Override
14 16
     public void write(String contentToBeWritten) {
17
+        if(isAlpha(contentToBeWritten)) {
18
+            super.write(contentToBeWritten);
19
+        } else {
20
+            throw new IllegalArgumentException();
21
+        }
15 22
     }
16 23
 
17 24
     private Boolean isAlpha(String s) {
18
-        return null;
25
+
26
+        Pattern pattern = Pattern.compile("^[a-zA-Z ]+$");
27
+        Matcher match = pattern.matcher(s);
28
+        return match.find();
19 29
     }
20 30
 }

+ 57
- 4
src/main/java/rocks/zipcode/Document.java Wyświetl plik

@@ -1,8 +1,6 @@
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;
7 5
 
8 6
 /**
@@ -24,6 +22,15 @@ public class Document implements DocumentInterface {
24 22
 
25 23
     @Override
26 24
     public void write(String contentToBeWritten) {
25
+
26
+        BufferedWriter bw = null;
27
+        try {
28
+            bw = new BufferedWriter(new FileWriter(file));
29
+            bw.write(contentToBeWritten);
30
+            bw.close();
31
+        } catch (IOException e) {
32
+            System.out.println("Error writing to file");
33
+        }
27 34
     }
28 35
 
29 36
     @Override
@@ -32,12 +39,58 @@ public class Document implements DocumentInterface {
32 39
 
33 40
     @Override
34 41
     public String read(Integer lineNumber) {
35
-        return null;
42
+
43
+        BufferedReader reader = null;
44
+        String line = null;
45
+
46
+        try {
47
+            reader = new BufferedReader(new FileReader(file));
48
+            for (int i = 0; i <= lineNumber; i++) {
49
+
50
+                line = reader.readLine();
51
+            }
52
+
53
+        } catch (FileNotFoundException e) {
54
+            System.out.println("File not found");
55
+        } catch (IOException ioExep) {
56
+            ioExep.printStackTrace();
57
+        } finally {
58
+            try {
59
+                reader.close();
60
+            } catch (IOException e) {
61
+                e.printStackTrace();
62
+            }
63
+        }
64
+        return line;
36 65
     }
37 66
 
38 67
     @Override
39 68
     public String read() {
40
-        return null;
69
+
70
+        BufferedReader reader = null;
71
+        String line = null;
72
+        StringBuilder stringBuilder = new StringBuilder();
73
+        String ls = System.getProperty("line.separator");
74
+
75
+        try {
76
+            reader = new BufferedReader(new FileReader(file));
77
+            while ((line = reader.readLine()) != null) {
78
+                stringBuilder.append(line);
79
+                stringBuilder.append(ls);
80
+            }
81
+
82
+        } catch (FileNotFoundException e) {
83
+            System.out.println("File not found");
84
+        } catch (IOException ioExep) {
85
+            ioExep.printStackTrace();
86
+        } finally {
87
+            try {
88
+                reader.close();
89
+            } catch (IOException e) {
90
+                e.printStackTrace();
91
+            }
92
+        }
93
+        return stringBuilder.toString().trim();
41 94
     }
42 95
 
43 96
     @Override