Explorar el Código

completed Alpha,Numeric,and SC classes

Michelle DiMarino hace 7 años
padre
commit
bfa796b004

+ 7
- 1
src/main/java/rocks/zipcode/AlphaCharDocument.java Ver fichero

@@ -12,9 +12,15 @@ public class AlphaCharDocument extends Document {
12 12
 
13 13
     @Override
14 14
     public void write(String contentToBeWritten) {
15
+        if (isAlpha(contentToBeWritten)){
16
+            super.write(contentToBeWritten);
17
+
18
+        }else{
19
+            throw new IllegalArgumentException();
20
+        }
15 21
     }
16 22
 
17 23
     private Boolean isAlpha(String s) {
18
-        return null;
24
+        return (s.matches("[a-zA-z ]+"));
19 25
     }
20 26
 }

+ 34
- 4
src/main/java/rocks/zipcode/Document.java Ver fichero

@@ -2,6 +2,7 @@ package rocks.zipcode;
2 2
 
3 3
 import java.io.*;
4 4
 import java.nio.file.Files;
5
+import java.nio.file.*;
5 6
 import java.util.Arrays;
6 7
 import java.util.List;
7 8
 
@@ -24,20 +25,49 @@ public class Document implements DocumentInterface {
24 25
 
25 26
     @Override
26 27
     public void write(String contentToBeWritten) {
28
+        try{
29
+            fileWriter.write(contentToBeWritten);
30
+            fileWriter.flush();
31
+            fileWriter.close();
32
+
33
+        } catch (IOException io) {
34
+            io.printStackTrace();
35
+        }
27 36
     }
28 37
 
29 38
     @Override
30 39
     public void write(Integer lineNumber, String valueToBeWritten) {
40
+
41
+
31 42
     }
32 43
 
33 44
     @Override
34
-    public String read(Integer lineNumber) {
45
+    public String read(Integer lineNumber){
35 46
         return null;
47
+
36 48
     }
37 49
 
38 50
     @Override
39
-    public String read() {
40
-        return null;
51
+    public String read() throws IOException{
52
+        BufferedReader readFile = new BufferedReader(new FileReader(file));
53
+        String fileAsString;
54
+        try {
55
+            StringBuilder sb = new StringBuilder();
56
+            String fileLine = readFile.readLine();
57
+
58
+            while (fileLine != null) {
59
+                sb.append(fileLine);
60
+                sb.append(System.lineSeparator());
61
+                fileLine = readFile.readLine();
62
+            }
63
+            fileAsString  = sb.toString().trim();
64
+
65
+        } finally {
66
+            readFile.close();
67
+        }
68
+
69
+        return fileAsString;
70
+
41 71
     }
42 72
 
43 73
     @Override
@@ -54,7 +84,7 @@ public class Document implements DocumentInterface {
54 84
 
55 85
     @Override
56 86
     public File getFile() {
57
-        return null;
87
+        return file;
58 88
     }
59 89
 
60 90
     @Override

+ 1
- 1
src/main/java/rocks/zipcode/DocumentInterface.java Ver fichero

@@ -13,7 +13,7 @@ public interface DocumentInterface {
13 13
 
14 14
     String read(Integer lineNumber);
15 15
 
16
-    String read();
16
+    String read() throws Exception;
17 17
 
18 18
     void replaceAll(String stringToReplace, String replacementString);
19 19
 

+ 7
- 1
src/main/java/rocks/zipcode/NumericCharDocument.java Ver fichero

@@ -12,9 +12,15 @@ public class NumericCharDocument extends Document {
12 12
 
13 13
     @Override
14 14
     public void write(String contentToBeWritten) {
15
+        if (isNumeric(contentToBeWritten)){
16
+            super.write(contentToBeWritten);
17
+
18
+        }else{
19
+            throw new IllegalArgumentException();
20
+        }
15 21
     }
16 22
 
17 23
     private Boolean isNumeric(String s) {
18
-        return null;
24
+        return (s.matches("[0-9 ]+"));
19 25
     }
20 26
 }

+ 7
- 1
src/main/java/rocks/zipcode/SpecialCharDocument.java Ver fichero

@@ -12,9 +12,15 @@ public class SpecialCharDocument extends Document {
12 12
 
13 13
     @Override
14 14
     public void write(String contentToBeWritten) {
15
+        if (isSpecialCharacters(contentToBeWritten)){
16
+            super.write(contentToBeWritten);
17
+
18
+        }else{
19
+            throw new IllegalArgumentException();
20
+        }
15 21
     }
16 22
 
17 23
     private Boolean isSpecialCharacters(String s) {
18
-        return null;
24
+        return (s.matches("[\\p{Punct}]+"));
19 25
     }
20 26
 }