Kate Moore 6 lat temu
rodzic
commit
ab43901516

+ 1
- 0
The Wyświetl plik

@@ -0,0 +1 @@
1
+quickest

+ 1
- 0
quick Wyświetl plik

@@ -0,0 +1 @@
1
+quicker

+ 24
- 2
src/main/java/rocks/zipcode/AlphaCharDocument.java Wyświetl plik

@@ -1,5 +1,7 @@
1 1
 package rocks.zipcode;
2 2
 
3
+import java.io.BufferedWriter;
4
+import java.io.FileWriter;
3 5
 import java.io.IOException;
4 6
 
5 7
 /**
@@ -12,9 +14,29 @@ public class AlphaCharDocument extends Document {
12 14
 
13 15
     @Override
14 16
     public void write(String contentToBeWritten) {
17
+
18
+        if (contentToBeWritten.matches("[a-zA-Z ]+")) {
19
+            try{
20
+                BufferedWriter bw = new BufferedWriter(new FileWriter(this.getFile()));
21
+                bw.write(contentToBeWritten);
22
+                bw.close();
23
+            } catch (IOException e) {
24
+                e.printStackTrace();
25
+            }
26
+        } else {
27
+            throw new IllegalArgumentException();
28
+        }
15 29
     }
16 30
 
17
-    private Boolean isAlpha(String s) {
18
-        return null;
31
+
32
+        private Boolean isAlpha(String s) {
33
+
34
+        char[] chars = s.toCharArray();
35
+        for(char c : chars) {
36
+            if(!Character.isLetter(c)) {
37
+                return false;
38
+            }
39
+        }
40
+        return true;
19 41
     }
20 42
 }

+ 45
- 7
src/main/java/rocks/zipcode/Document.java Wyświetl plik

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

+ 2
- 1
src/main/java/rocks/zipcode/DocumentInterface.java Wyświetl plik

@@ -1,13 +1,14 @@
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
 /**
7 8
  * @author leon on 16/11/2018.
8 9
  */
9 10
 public interface DocumentInterface {
10
-    void write(String contentToBeWritten);
11
+    void write(String contentToBeWritten) throws IOException;
11 12
 
12 13
     void write(Integer lineNumber, String valueToBeWritten);
13 14
 

+ 20
- 1
src/main/java/rocks/zipcode/NumericCharDocument.java Wyświetl plik

@@ -1,5 +1,7 @@
1 1
 package rocks.zipcode;
2 2
 
3
+import java.io.BufferedWriter;
4
+import java.io.FileWriter;
3 5
 import java.io.IOException;
4 6
 
5 7
 /**
@@ -12,9 +14,26 @@ public class NumericCharDocument extends Document {
12 14
 
13 15
     @Override
14 16
     public void write(String contentToBeWritten) {
17
+        if (contentToBeWritten.matches("[0-9]+")) {
18
+            try{
19
+                BufferedWriter bw = new BufferedWriter(new FileWriter(this.getFile()));
20
+                bw.write(contentToBeWritten);
21
+                bw.close();
22
+            } catch (IOException e) {
23
+                e.printStackTrace();
24
+            }
25
+        } else {
26
+            throw new IllegalArgumentException();
27
+        }
15 28
     }
16 29
 
17 30
     private Boolean isNumeric(String s) {
18
-        return null;
31
+        char[] chars = s.toCharArray();
32
+        for(char c : chars) {
33
+            if(!Character.isDigit(c)) {
34
+                return false;
35
+            }
36
+        }
37
+        return true;
19 38
     }
20 39
 }

+ 21
- 0
src/main/java/rocks/zipcode/SpecialCharDocument.java Wyświetl plik

@@ -1,6 +1,9 @@
1 1
 package rocks.zipcode;
2 2
 
3
+import java.io.BufferedWriter;
4
+import java.io.FileWriter;
3 5
 import java.io.IOException;
6
+import java.util.regex.Pattern;
4 7
 
5 8
 /**
6 9
  * @author leon on 18/11/2018.
@@ -12,9 +15,27 @@ public class SpecialCharDocument extends Document {
12 15
 
13 16
     @Override
14 17
     public void write(String contentToBeWritten) {
18
+        if (contentToBeWritten.matches("[^A-Za-z0-9]+")) {
19
+            try{
20
+                BufferedWriter bw = new BufferedWriter(new FileWriter(this.getFile()));
21
+                bw.write(contentToBeWritten);
22
+                bw.close();
23
+            } catch (IOException e) {
24
+                e.printStackTrace();
25
+            }
26
+        } else {
27
+            throw new IllegalArgumentException();
28
+        }
15 29
     }
16 30
 
17 31
     private Boolean isSpecialCharacters(String s) {
32
+
33
+        char[] chars = s.toCharArray();
34
+        for(char c : chars) {
35
+            if(!Character.isLetterOrDigit(c)) {
36
+                return false;
37
+            }
38
+        }
18 39
         return null;
19 40
     }
20 41
 }