Selaa lähdekoodia

Completed Lab

Chaitali Patel 5 vuotta sitten
vanhempi
commit
d3e29f1155

+ 0
- 0
exception.txt Näytä tiedosto


+ 18
- 2
src/main/java/rocks/zipcode/AlphaCharDocument.java Näytä tiedosto

@@ -1,6 +1,9 @@
1 1
 package rocks.zipcode;
2 2
 
3
+import java.io.FileWriter;
3 4
 import java.io.IOException;
5
+import java.util.regex.Matcher;
6
+import java.util.regex.Pattern;
4 7
 
5 8
 /**
6 9
  * @author leon on 16/11/2018.
@@ -11,10 +14,23 @@ public class AlphaCharDocument extends Document {
11 14
     }
12 15
 
13 16
     @Override
14
-    public void write(String contentToBeWritten) {
17
+    public void write(String contentToBeWritten) throws IOException {
18
+
19
+        if (this.isAlpha(contentToBeWritten)){
20
+            super.write(contentToBeWritten);
21
+
22
+
23
+        }else{
24
+
25
+            throw  new  IllegalArgumentException();
26
+        }
27
+
15 28
     }
16 29
 
17 30
     private Boolean isAlpha(String s) {
18
-        return null;
31
+        Pattern pattern = Pattern.compile("[a-z]", Pattern.CASE_INSENSITIVE);
32
+        Matcher matcher = pattern.matcher(s);
33
+        boolean b = matcher.find();
34
+        return b;
19 35
     }
20 36
 }

+ 90
- 10
src/main/java/rocks/zipcode/Document.java Näytä tiedosto

@@ -1,20 +1,29 @@
1 1
 package rocks.zipcode;
2 2
 
3 3
 import java.io.*;
4
+import java.net.URI;
5
+import java.nio.charset.Charset;
6
+import java.nio.charset.StandardCharsets;
4 7
 import java.nio.file.Files;
5
-import java.util.Arrays;
8
+import java.nio.file.Path;
9
+import java.nio.file.Paths;
10
+import java.util.ArrayList;
6 11
 import java.util.List;
12
+import java.util.stream.Collectors;
13
+import java.util.stream.Stream;
7 14
 
8 15
 /**
9 16
  * @author leon on 16/11/2018.
10 17
  */
11 18
 public class Document implements DocumentInterface {
12 19
 
20
+    //have to implement all abstract methods
21
+
13 22
     private final FileWriter fileWriter;
14 23
     private final File file;
15 24
 
16 25
     public Document(String fileName) throws IOException {
17
-        this(new File(fileName));
26
+        this(new File(fileName)); // have to open all the file name to read and write
18 27
     }
19 28
 
20 29
     public Document(File file) throws IOException {
@@ -23,42 +32,113 @@ public class Document implements DocumentInterface {
23 32
     }
24 33
 
25 34
     @Override
26
-    public void write(String contentToBeWritten) {
35
+    public void write(String contentToBeWritten) throws IOException {
36
+        try {
37
+            fileWriter.write(contentToBeWritten);
38
+            fileWriter.flush();
39
+        } catch (IOException e) {
40
+            e.printStackTrace();
41
+        }
27 42
     }
28 43
 
29 44
     @Override
30 45
     public void write(Integer lineNumber, String valueToBeWritten) {
31
-    }
46
+        List <String> lines = null;
47
+        try {
48
+            lines = Files.readAllLines(file.toPath());
49
+            lines.set(lineNumber,valueToBeWritten);
50
+            Files.write(file.toPath(), lines);
51
+        } catch (IOException e) {
52
+            e.printStackTrace();
53
+        }
32 54
 
55
+    }
33 56
     @Override
34
-    public String read(Integer lineNumber) {
35
-        return null;
57
+    public String read(Integer lineNumber) throws IOException {
58
+
59
+        try(Stream<String> lines = Files.lines(Paths.get("target/file.txt"))){
60
+            String line = lines.skip(lineNumber).findFirst().get();
61
+            return line;
62
+        }
63
+
36 64
     }
37 65
 
38 66
     @Override
39 67
     public String read() {
40
-        return null;
68
+
69
+        StringBuilder builder = new StringBuilder();
70
+        try (BufferedReader br = new BufferedReader(new FileReader(file))) {
71
+            String sCurrentLine;
72
+            while ((sCurrentLine = br.readLine()) != null) {
73
+                builder.append(sCurrentLine).append("\n");
74
+            }
75
+        } catch (IOException e) {
76
+            e.printStackTrace();
77
+        }
78
+        return builder.toString().trim();
41 79
     }
42 80
 
43 81
     @Override
44 82
     public void replaceAll(String stringToReplace, String replacementString) {
83
+        Path path = Paths.get("target/file.txt");
84
+        Charset charset = StandardCharsets.UTF_8;
85
+
86
+        String content = null;
87
+        try {
88
+            content = new String(Files.readAllBytes(path), charset);
89
+        } catch (IOException e) {
90
+            e.printStackTrace();
91
+        }
92
+        content = content.replaceAll(stringToReplace, replacementString);
93
+        try {
94
+            Files.write(path, content.getBytes(charset));
95
+        } catch (IOException e) {
96
+            e.printStackTrace();
97
+        }
45 98
     }
46 99
 
47 100
     @Override
48 101
     public void overWrite(String content) {
102
+
103
+        try {
104
+            Writer fileWriter = new FileWriter("target/file.txt", false);
105
+            write(content);
106
+        } catch (IOException e) {
107
+            e.printStackTrace();
108
+        }
49 109
     }
50 110
 
51
-    public List<String> toList() {
111
+    public List<String> toList() throws IOException {
112
+
52 113
         return null;
53 114
     }
54 115
 
116
+
55 117
     @Override
56 118
     public File getFile() {
57
-        return null;
119
+
120
+        return file;
58 121
     }
59 122
 
60 123
     @Override
61 124
     public String toString() {
62
-        return null;
125
+
126
+        String fileName = "target/file.txt";
127
+        String content = "";
128
+
129
+        try {
130
+            content = new String(Files.readAllBytes(Paths.get("target/file.txt")));
131
+
132
+        } catch (IOException e) {
133
+            e.printStackTrace();
134
+        }
135
+        StringBuilder builder = new StringBuilder(fileName)
136
+                .append('{')
137
+                .append(content)
138
+                .append('}');
139
+
140
+
141
+        return builder.toString();
63 142
     }
143
+
64 144
 }

+ 6
- 5
src/main/java/rocks/zipcode/DocumentInterface.java Näytä tiedosto

@@ -1,25 +1,26 @@
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
-    void write(Integer lineNumber, String valueToBeWritten);
13
+    void write(Integer lineNumber, String valueToBeWritten) throws IOException;
13 14
 
14
-    String read(Integer lineNumber);
15
+    String read(Integer lineNumber) throws IOException;
15 16
 
16
-    String read();
17
+    String read() throws IOException;
17 18
 
18 19
     void replaceAll(String stringToReplace, String replacementString);
19 20
 
20 21
     void overWrite(String content);
21 22
 
22
-    List<String> toList();
23
+    List<String> toList() throws IOException;
23 24
 
24 25
     File getFile();
25 26
 

+ 17
- 2
src/main/java/rocks/zipcode/NumericCharDocument.java Näytä tiedosto

@@ -1,6 +1,9 @@
1 1
 package rocks.zipcode;
2 2
 
3
+import java.io.FileWriter;
3 4
 import java.io.IOException;
5
+import java.util.regex.Matcher;
6
+import java.util.regex.Pattern;
4 7
 
5 8
 /**
6 9
  * @author leon on 16/11/2018.
@@ -11,10 +14,22 @@ public class NumericCharDocument extends Document {
11 14
     }
12 15
 
13 16
     @Override
14
-    public void write(String contentToBeWritten) {
17
+    public void write(String contentToBeWritten) throws IOException {
18
+        if (this.isNumeric(contentToBeWritten)){
19
+            super.write(contentToBeWritten);
20
+
21
+        }else{
22
+            throw  new  IllegalArgumentException();
23
+        }
24
+
25
+
15 26
     }
16 27
 
17 28
     private Boolean isNumeric(String s) {
18
-        return null;
29
+
30
+        Pattern pattern = Pattern.compile("[0-9]", Pattern.CASE_INSENSITIVE);
31
+        Matcher matcher = pattern.matcher(s);
32
+        boolean b = matcher.find();
33
+        return b;
19 34
     }
20 35
 }

+ 19
- 2
src/main/java/rocks/zipcode/SpecialCharDocument.java Näytä tiedosto

@@ -1,6 +1,9 @@
1 1
 package rocks.zipcode;
2 2
 
3
+import java.io.FileWriter;
3 4
 import java.io.IOException;
5
+import java.util.regex.Matcher;
6
+import java.util.regex.Pattern;
4 7
 
5 8
 /**
6 9
  * @author leon on 18/11/2018.
@@ -11,10 +14,24 @@ public class SpecialCharDocument extends Document {
11 14
     }
12 15
 
13 16
     @Override
14
-    public void write(String contentToBeWritten) {
17
+    public void write(String contentToBeWritten) throws IOException {
18
+        if (this.isSpecialCharacters(contentToBeWritten)){
19
+            super.write(contentToBeWritten);
20
+
21
+
22
+        }else{
23
+
24
+            throw  new  IllegalArgumentException();
25
+        }
15 26
     }
16 27
 
17 28
     private Boolean isSpecialCharacters(String s) {
18
-        return null;
29
+
30
+        Pattern pattern = Pattern.compile("[$&+,:;=?@#|'<>.^*()%!-]", Pattern.CASE_INSENSITIVE);
31
+        Matcher matcher = pattern.matcher(s);
32
+        boolean b = matcher.find();
33
+        return b;
34
+
35
+
19 36
     }
20 37
 }