Browse Source

completing lab

De'Jon Johnson 6 years ago
parent
commit
1d24e0483a

+ 1
- 0
The quick brown fox View File

@@ -0,0 +1 @@
1
+The quick brown fox

+ 12
- 1
src/main/java/rocks/zipcode/AlphaCharDocument.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,18 @@ 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
+        Pattern pattern = Pattern.compile("^[a-zA-Z ]+$");
26
+        Matcher match = pattern.matcher(s);
27
+        return match.find();
28
+
29
+
19 30
     }
20 31
 }

+ 69
- 4
src/main/java/rocks/zipcode/Document.java View File

@@ -2,6 +2,10 @@ package rocks.zipcode;
2 2
 
3 3
 import java.io.*;
4 4
 import java.nio.file.Files;
5
+import java.nio.file.Path;
6
+import java.nio.file.Paths;
7
+import java.nio.file.StandardOpenOption;
8
+import java.util.ArrayList;
5 9
 import java.util.Arrays;
6 10
 import java.util.List;
7 11
 
@@ -24,41 +28,102 @@ public class Document implements DocumentInterface {
24 28
 
25 29
     @Override
26 30
     public void write(String contentToBeWritten) {
31
+
32
+        try(FileWriter fileWriter = new FileWriter("target/file.txt")) {
33
+            //inherited method from java.io.Writer
34
+            fileWriter.write(contentToBeWritten);
35
+            fileWriter.flush();
36
+        } catch (IOException e) {
37
+            e.printStackTrace();
38
+        }
27 39
     }
28 40
 
29 41
     @Override
30 42
     public void write(Integer lineNumber, String valueToBeWritten) {
43
+
44
+        try {
45
+            Path path = Paths.get(file.getPath());
46
+            List<String> list = Files.readAllLines(path);
47
+
48
+            String lastLine = list.get(list.size()-1);
49
+            List<String> allButLast = list.subList(0,list.size()-1);
50
+            allButLast.set(lineNumber, valueToBeWritten);
51
+            Files.write(path, allButLast);
52
+            Files.write(path, lastLine.getBytes(), StandardOpenOption.APPEND);
53
+
54
+        } catch (IOException e) {
55
+            e.printStackTrace();
56
+        }
31 57
     }
32 58
 
33 59
     @Override
34 60
     public String read(Integer lineNumber) {
35
-        return null;
61
+
62
+        try {
63
+            return new String(String.valueOf(Files.readAllLines(Paths.get(file.getPath())).get(lineNumber)));
64
+
65
+        } catch (IOException e) {
66
+            e.printStackTrace();
67
+        }
68
+        return null ;
36 69
     }
37 70
 
38 71
     @Override
39 72
     public String read() {
40
-        return null;
73
+
74
+        try {
75
+            return new String(Files.readAllBytes(Paths.get(file.getPath())));
76
+        }
77
+        catch(IOException e) {
78
+            e.printStackTrace();
79
+        }
80
+        return file.getPath();
41 81
     }
42 82
 
43 83
     @Override
44 84
     public void replaceAll(String stringToReplace, String replacementString) {
85
+
86
+        String doc = read();
87
+      doc = doc.replaceAll(stringToReplace, replacementString);
88
+      write(doc);
89
+
90
+
45 91
     }
46 92
 
93
+
47 94
     @Override
48 95
     public void overWrite(String content) {
96
+        write(content);
49 97
     }
50 98
 
51 99
     public List<String> toList() {
52
-        return null;
100
+        String[] arr = read().split("\n");
101
+        List<String> result = new ArrayList<>();
102
+        for(String s: arr) {
103
+           result.add(s);
104
+           }
105
+      return result;
53 106
     }
54 107
 
55 108
     @Override
56 109
     public File getFile() {
57
-        return null;
110
+
111
+        return this.file;
58 112
     }
59 113
 
60 114
     @Override
61 115
     public String toString() {
116
+        try {
117
+            Path path = Paths.get(file.getPath());
118
+            List<String> result = Files.readAllLines(path);
119
+            String list = result.toString().replace("[", "").replace("]", "");
120
+
121
+            return path +  "{" +list + "}";
122
+        }
123
+        catch(IOException e) {
124
+            e.printStackTrace();
125
+        }
62 126
         return null;
127
+
63 128
     }
64 129
 }

+ 12
- 2
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,17 @@ 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
-    private Boolean isNumeric(String s) {
18
-        return null;
24
+    private Boolean isNumeric(String s)
25
+    {
26
+        Pattern pattern = Pattern.compile("^[1-9 ]+$");
27
+       Matcher match = pattern.matcher(s);
28
+       return match.find();
19 29
     }
20 30
 }

+ 10
- 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,16 @@ 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
+        Pattern pattern = Pattern.compile("^[!@#$%^&*()_ ]+$");
26
+        Matcher match = pattern.matcher(s);
27
+        return match.find();
19 28
     }
20 29
 }