Kate Moore 6 anos atrás
pai
commit
ab43901516

+ 1
- 0
The Ver arquivo

1
+quickest

+ 1
- 0
quick Ver arquivo

1
+quicker

+ 24
- 2
src/main/java/rocks/zipcode/AlphaCharDocument.java Ver arquivo

1
 package rocks.zipcode;
1
 package rocks.zipcode;
2
 
2
 
3
+import java.io.BufferedWriter;
4
+import java.io.FileWriter;
3
 import java.io.IOException;
5
 import java.io.IOException;
4
 
6
 
5
 /**
7
 /**
12
 
14
 
13
     @Override
15
     @Override
14
     public void write(String contentToBeWritten) {
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 Ver arquivo

2
 
2
 
3
 import java.io.*;
3
 import java.io.*;
4
 import java.nio.file.Files;
4
 import java.nio.file.Files;
5
+import java.util.ArrayList;
5
 import java.util.Arrays;
6
 import java.util.Arrays;
6
 import java.util.List;
7
 import java.util.List;
8
+import java.util.regex.Matcher;
9
+import java.util.regex.Pattern;
7
 
10
 
8
 /**
11
 /**
9
  * @author leon on 16/11/2018.
12
  * @author leon on 16/11/2018.
10
  */
13
  */
11
 public class Document implements DocumentInterface {
14
 public class Document implements DocumentInterface {
12
 
15
 
13
-    private final FileWriter fileWriter;
14
     private final File file;
16
     private final File file;
15
 
17
 
16
     public Document(String fileName) throws IOException {
18
     public Document(String fileName) throws IOException {
19
 
21
 
20
     public Document(File file) throws IOException {
22
     public Document(File file) throws IOException {
21
         this.file = file;
23
         this.file = file;
22
-        this.fileWriter = new FileWriter(file);
23
     }
24
     }
24
 
25
 
25
     @Override
26
     @Override
26
     public void write(String contentToBeWritten) {
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
     @Override
39
     @Override
30
     public void write(Integer lineNumber, String valueToBeWritten) {
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
     @Override
53
     @Override
34
     public String read(Integer lineNumber) {
54
     public String read(Integer lineNumber) {
35
-        return null;
55
+        return this.toList().get(lineNumber);
36
     }
56
     }
37
 
57
 
38
     @Override
58
     @Override
39
     public String read() {
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
     @Override
75
     @Override
44
     public void replaceAll(String stringToReplace, String replacementString) {
76
     public void replaceAll(String stringToReplace, String replacementString) {
77
+
45
     }
78
     }
46
 
79
 
47
     @Override
80
     @Override
48
     public void overWrite(String content) {
81
     public void overWrite(String content) {
82
+        this.write(content);
83
+
49
     }
84
     }
50
 
85
 
51
     public List<String> toList() {
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
     @Override
91
     @Override
56
     public File getFile() {
92
     public File getFile() {
57
-        return null;
93
+        return file;
58
     }
94
     }
59
 
95
 
60
     @Override
96
     @Override
61
     public String toString() {
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 Ver arquivo

1
 package rocks.zipcode;
1
 package rocks.zipcode;
2
 
2
 
3
 import java.io.File;
3
 import java.io.File;
4
+import java.io.IOException;
4
 import java.util.List;
5
 import java.util.List;
5
 
6
 
6
 /**
7
 /**
7
  * @author leon on 16/11/2018.
8
  * @author leon on 16/11/2018.
8
  */
9
  */
9
 public interface DocumentInterface {
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);
13
 
14
 

+ 20
- 1
src/main/java/rocks/zipcode/NumericCharDocument.java Ver arquivo

1
 package rocks.zipcode;
1
 package rocks.zipcode;
2
 
2
 
3
+import java.io.BufferedWriter;
4
+import java.io.FileWriter;
3
 import java.io.IOException;
5
 import java.io.IOException;
4
 
6
 
5
 /**
7
 /**
12
 
14
 
13
     @Override
15
     @Override
14
     public void write(String contentToBeWritten) {
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
     private Boolean isNumeric(String s) {
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 Ver arquivo

1
 package rocks.zipcode;
1
 package rocks.zipcode;
2
 
2
 
3
+import java.io.BufferedWriter;
4
+import java.io.FileWriter;
3
 import java.io.IOException;
5
 import java.io.IOException;
6
+import java.util.regex.Pattern;
4
 
7
 
5
 /**
8
 /**
6
  * @author leon on 18/11/2018.
9
  * @author leon on 18/11/2018.
12
 
15
 
13
     @Override
16
     @Override
14
     public void write(String contentToBeWritten) {
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
     private Boolean isSpecialCharacters(String s) {
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
         return null;
39
         return null;
19
     }
40
     }
20
 }
41
 }