Xcuello 6 anni fa
parent
commit
c74f01fdcb

+ 16
- 1
src/main/java/rocks/zipcode/AlphaCharDocument.java Vedi File

@@ -1,20 +1,35 @@
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.
7 9
  */
8 10
 public class AlphaCharDocument extends Document {
11
+
9 12
     public AlphaCharDocument(String fileName) throws IOException {
13
+
10 14
         super(fileName);
11 15
     }
12 16
 
13 17
     @Override
14 18
     public void write(String contentToBeWritten) {
19
+
20
+        if (isAlpha(contentToBeWritten)) {
21
+            super.write(contentToBeWritten);
22
+
23
+        } else {
24
+            throw new IllegalArgumentException();
25
+        }
15 26
     }
16 27
 
17 28
     private Boolean isAlpha(String s) {
18
-        return null;
29
+
30
+        Pattern pattern = Pattern.compile("[a-zA-Z]");
31
+        Matcher matcher = pattern.matcher(s);
32
+
33
+        return matcher.find();
19 34
     }
20 35
 }

+ 96
- 4
src/main/java/rocks/zipcode/Document.java Vedi File

@@ -1,7 +1,11 @@
1 1
 package rocks.zipcode;
2 2
 
3 3
 import java.io.*;
4
+import java.nio.charset.StandardCharsets;
4 5
 import java.nio.file.Files;
6
+import java.nio.file.Path;
7
+import java.nio.file.Paths;
8
+import java.nio.file.StandardOpenOption;
5 9
 import java.util.Arrays;
6 10
 import java.util.List;
7 11
 
@@ -14,51 +18,139 @@ public class Document implements DocumentInterface {
14 18
     private final File file;
15 19
 
16 20
     public Document(String fileName) throws IOException {
21
+
17 22
         this(new File(fileName));
18 23
     }
19 24
 
20 25
     public Document(File file) throws IOException {
26
+
21 27
         this.file = file;
22 28
         this.fileWriter = new FileWriter(file);
23 29
     }
24 30
 
25 31
     @Override
26 32
     public void write(String contentToBeWritten) {
33
+
34
+        try {
35
+            fileWriter.write(contentToBeWritten);
36
+
37
+            fileWriter.flush();
38
+
39
+        } catch (IOException e) {
40
+
41
+            e.printStackTrace();
42
+
43
+            throw new IllegalArgumentException();
44
+
45
+        }catch (IllegalArgumentException iae) {
46
+
47
+            iae.printStackTrace();
48
+        }
27 49
     }
28 50
 
29 51
     @Override
30 52
     public void write(Integer lineNumber, String valueToBeWritten) {
53
+
54
+        try {
55
+            Path path = Paths.get("/Users/xzaviacuello/Labs/exceptions-readandwrite/target/file.txt");
56
+
57
+            List<String> lines = Files.readAllLines(path, StandardCharsets.UTF_8);
58
+
59
+            lines.set(lineNumber, valueToBeWritten);
60
+
61
+            Files.write(path, lines.subList(0, lines.size() - 1), StandardCharsets.UTF_8);
62
+
63
+            Files.write(path, lines.get(lines.size() - 1).getBytes("UTF-8"), StandardOpenOption.APPEND);
64
+
65
+        }catch (IllegalArgumentException e) {
66
+
67
+            e.printStackTrace();
68
+
69
+        }catch (IOException io){
70
+
71
+            io.printStackTrace();
72
+        }
31 73
     }
32 74
 
33 75
     @Override
34
-    public String read(Integer lineNumber) {
35
-        return null;
76
+    public String read(Integer lineNumber) throws IOException {
77
+
78
+        return Files.readAllLines(file.toPath()).get(lineNumber);
36 79
     }
37 80
 
38 81
     @Override
39
-    public String read() {
40
-        return null;
82
+    public String read() throws IOException {
83
+
84
+        return new String(Files.readAllBytes(Paths.get(file.getAbsolutePath())));
85
+
86
+        
41 87
     }
42 88
 
43 89
     @Override
44 90
     public void replaceAll(String stringToReplace, String replacementString) {
91
+
92
+        try{
93
+            Path path = Paths.get("/Users/xzaviacuello/Labs/exceptions-readandwrite/target/file.txt");
94
+
95
+            String content = new String(Files.readAllBytes(path),StandardCharsets.UTF_8);
96
+
97
+            content = content.replaceAll(stringToReplace, replacementString);
98
+
99
+            Files.write(path, content.getBytes(StandardCharsets.UTF_8));
100
+
101
+        }catch(IOException e) {
102
+
103
+            e.printStackTrace();
104
+        }
45 105
     }
46 106
 
47 107
     @Override
48 108
     public void overWrite(String content) {
109
+
110
+        try {
111
+            Path path = Paths.get("/Users/xzaviacuello/Labs/exceptions-readandwrite/target/file.txt");
112
+
113
+            List<String> lines = Files.readAllLines(path, StandardCharsets.UTF_8);
114
+
115
+            lines.clear();
116
+
117
+            lines.add(content);
118
+
119
+            Files.write(path, lines.subList(0, lines.size() - 1), StandardCharsets.UTF_8);
120
+
121
+            Files.write(path, lines.get(lines.size() - 1).getBytes("UTF-8"), StandardOpenOption.APPEND);
122
+
123
+        }catch (IOException io){
124
+
125
+            io.printStackTrace();
126
+        }
49 127
     }
50 128
 
51 129
     public List<String> toList() {
130
+
52 131
         return null;
53 132
     }
54 133
 
55 134
     @Override
56 135
     public File getFile() {
136
+
137
+        Path path = Paths.get("/Users/xzaviacuello/Labs/exceptions-readandwrite/target/file.txt");
138
+
57 139
         return null;
58 140
     }
59 141
 
60 142
     @Override
61 143
     public String toString() {
144
+
145
+        try{
146
+
147
+            return file.toString() + "{" + read() + "}";
148
+
149
+        }catch (IOException e) {
150
+
151
+            e.printStackTrace();
152
+        }
153
+
62 154
         return null;
63 155
     }
64 156
 }

+ 3
- 2
src/main/java/rocks/zipcode/DocumentInterface.java Vedi File

@@ -1,6 +1,7 @@
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
 /**
@@ -11,9 +12,9 @@ public interface DocumentInterface {
11 12
 
12 13
     void write(Integer lineNumber, String valueToBeWritten);
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
 

+ 18
- 1
src/main/java/rocks/zipcode/NumericCharDocument.java Vedi File

@@ -1,20 +1,37 @@
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.
7 9
  */
8 10
 public class NumericCharDocument extends Document {
11
+
9 12
     public NumericCharDocument(String fileName) throws IOException {
13
+
10 14
         super(fileName);
11 15
     }
12 16
 
13 17
     @Override
14 18
     public void write(String contentToBeWritten) {
19
+
20
+        if (isNumeric(contentToBeWritten)) {
21
+
22
+            super.write(contentToBeWritten);
23
+
24
+        } else {
25
+
26
+            throw new IllegalArgumentException();
27
+        }
15 28
     }
16 29
 
17 30
     private Boolean isNumeric(String s) {
18
-        return null;
31
+
32
+        Pattern pattern = Pattern.compile("[0-9]");
33
+        Matcher matcher = pattern.matcher(s);
34
+
35
+        return matcher.find();
19 36
     }
20 37
 }

+ 18
- 1
src/main/java/rocks/zipcode/SpecialCharDocument.java Vedi File

@@ -1,20 +1,37 @@
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.
7 9
  */
8 10
 public class SpecialCharDocument extends Document {
11
+
9 12
     public SpecialCharDocument(String fileName) throws IOException {
13
+
10 14
         super(fileName);
11 15
     }
12 16
 
13 17
     @Override
14 18
     public void write(String contentToBeWritten) {
19
+
20
+        if (isSpecialCharacters(contentToBeWritten)) {
21
+
22
+            super.write(contentToBeWritten);
23
+
24
+        } else {
25
+
26
+            throw new IllegalArgumentException();
27
+        }
15 28
     }
16 29
 
17 30
     private Boolean isSpecialCharacters(String s) {
18
-        return null;
31
+
32
+        Pattern pattern = Pattern.compile("[^\\w\\s]");
33
+        Matcher matcher = pattern.matcher(s);
34
+
35
+        return matcher.find();
19 36
     }
20 37
 }