Procházet zdrojové kódy

all the tests pass

Whitney Martinez před 7 roky
rodič
revize
b983d41a13

+ 16
- 6
src/main/java/rocks/zipcode/AlphaCharDocument.java Zobrazit soubor

@@ -1,6 +1,7 @@
1 1
 package rocks.zipcode;
2 2
 
3 3
 import java.io.*;
4
+import java.util.regex.Matcher;
4 5
 import java.util.regex.Pattern;
5 6
 
6 7
 /**
@@ -16,9 +17,15 @@ public class AlphaCharDocument extends Document {
16 17
 
17 18
     @Override
18 19
     public void write(String contentToBeWritten) throws IOException {
19
-        BufferedWriter writer = new BufferedWriter(new FileWriter("target/file.txt"));
20
-        writer.write(contentToBeWritten);
21
-        writer.close();
20
+
21
+        if (this.isAlpha(contentToBeWritten)){
22
+            super.write(contentToBeWritten);
23
+
24
+
25
+        }else{
26
+
27
+            throw  new  IllegalArgumentException();
28
+        }
22 29
 
23 30
     }
24 31
 
@@ -30,9 +37,12 @@ public class AlphaCharDocument extends Document {
30 37
 
31 38
 //        return (s.matches("[a-zA-Z]+"));
32 39
 
33
-        if(! Pattern.matches(".*[a-zA-Z]+.*", s))
34
-            return true;
35
-        else return false;
40
+        Pattern pattern = Pattern.compile("[a-z]", Pattern.CASE_INSENSITIVE);
41
+        Matcher matcher = pattern.matcher(s);
42
+        boolean b = matcher.find();
43
+        return b;
44
+
45
+
36 46
 
37 47
 
38 48
     }

+ 26
- 4
src/main/java/rocks/zipcode/Document.java Zobrazit soubor

@@ -6,6 +6,7 @@ import java.nio.charset.StandardCharsets;
6 6
 import java.nio.file.Files;
7 7
 import java.nio.file.Path;
8 8
 import java.nio.file.Paths;
9
+import java.util.ArrayList;
9 10
 import java.util.Arrays;
10 11
 import java.util.List;
11 12
 import java.util.stream.Stream;
@@ -62,6 +63,7 @@ public class Document implements DocumentInterface {
62 63
 
63 64
     @Override
64 65
     public String read() {
66
+
65 67
         StringBuilder builder = new StringBuilder();
66 68
         try (BufferedReader br = new BufferedReader(new FileReader(file))) {
67 69
             String sCurrentLine;
@@ -104,17 +106,37 @@ public class Document implements DocumentInterface {
104 106
         }
105 107
     }
106 108
 
107
-    public List<String> toList() {
109
+    public List<String> toList() throws IOException {
110
+
108 111
         return null;
109 112
     }
110 113
 
114
+
111 115
     @Override
112 116
     public File getFile() {
113
-        return null;
117
+
118
+        return file;
114 119
     }
115 120
 
116 121
     @Override
117 122
     public String toString() {
118
-        return null;
119
-    }
123
+
124
+            String fileName = "target/file.txt";
125
+            String content = "";
126
+
127
+            try {
128
+                content = new String(Files.readAllBytes(Paths.get("target/file.txt")));
129
+
130
+            } catch (IOException e) {
131
+                e.printStackTrace();
132
+            }
133
+            StringBuilder builder = new StringBuilder(fileName)
134
+                    .append('{')
135
+                    .append(content)
136
+                    .append('}');
137
+
138
+
139
+            return builder.toString();
140
+        }
141
+
120 142
 }

+ 1
- 1
src/main/java/rocks/zipcode/DocumentInterface.java Zobrazit soubor

@@ -20,7 +20,7 @@ public interface DocumentInterface {
20 20
 
21 21
     void overWrite(String content);
22 22
 
23
-    List<String> toList();
23
+    List<String> toList() throws IOException;
24 24
 
25 25
     File getFile();
26 26
 

+ 15
- 4
src/main/java/rocks/zipcode/NumericCharDocument.java Zobrazit soubor

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

+ 16
- 4
src/main/java/rocks/zipcode/SpecialCharDocument.java Zobrazit soubor

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