Browse Source

completed

Yesoda Sanka 5 years ago
parent
commit
52ca063f44

+ 40
- 4
src/main/java/rocks/zipcode/AlphaCharDocument.java View File

1
 package rocks.zipcode;
1
 package rocks.zipcode;
2
 
2
 
3
+
4
+import java.io.FileWriter;
3
 import java.io.IOException;
5
 import java.io.IOException;
6
+import java.nio.file.Files;
7
+import java.nio.file.Paths;
4
 
8
 
5
 /**
9
 /**
6
  * @author leon on 16/11/2018.
10
  * @author leon on 16/11/2018.
7
  */
11
  */
8
 public class AlphaCharDocument extends Document {
12
 public class AlphaCharDocument extends Document {
13
+
9
     public AlphaCharDocument(String fileName) throws IOException {
14
     public AlphaCharDocument(String fileName) throws IOException {
10
         super(fileName);
15
         super(fileName);
16
+        FileWriter fileWriter = new FileWriter(fileName);
11
     }
17
     }
12
 
18
 
13
     @Override
19
     @Override
14
-    public void write(String contentToBeWritten) {
15
-    }
20
+    public void write(String contentToBeWritten) throws IllegalArgumentException  {
21
+
22
+                if (isAlpha(contentToBeWritten)) {
23
+
24
+                    System.out.println("inside Alpha Write being - ");
25
+                    super.write(contentToBeWritten);
26
+                    System.out.println("after write completed - ");
27
+                }else {
28
+                    throw new IllegalArgumentException(" It' not Alpa" );
29
+                }
30
+
31
+
32
+            }
33
+
16
 
34
 
17
     private Boolean isAlpha(String s) {
35
     private Boolean isAlpha(String s) {
18
-        return null;
36
+        try {
37
+
38
+           String str = super.read();
39
+            //super.write(s);
40
+           System.out.println("inside isAlpha - " );
41
+           if(str.contains(s) ){
42
+               System.out.println("inside isAlpha -  inside if (string has alpha), will return true" );
43
+
44
+               return true;
45
+           }
46
+       }catch (Exception  e){
47
+           e.printStackTrace();
48
+       }
49
+      System.out.println("inside isAlpha -  No Alpha characters, will return false" );
50
+
51
+        return false;
52
+
53
+
54
+
19
     }
55
     }
20
-}
56
+}

+ 132
- 7
src/main/java/rocks/zipcode/Document.java View File

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.nio.file.Path;
6
+import java.nio.file.Paths;
7
+import java.util.ArrayList;
5
 import java.util.Arrays;
8
 import java.util.Arrays;
6
 import java.util.List;
9
 import java.util.List;
10
+import java.util.Scanner;
11
+import java.util.regex.Matcher;
12
+import java.util.regex.Pattern;
13
+import java.util.stream.Collectors;
14
+import java.util.stream.Stream;
7
 
15
 
8
 /**
16
 /**
9
  * @author leon on 16/11/2018.
17
  * @author leon on 16/11/2018.
24
 
32
 
25
     @Override
33
     @Override
26
     public void write(String contentToBeWritten) {
34
     public void write(String contentToBeWritten) {
35
+        try {
36
+            fileWriter.write(contentToBeWritten);
37
+            fileWriter.flush();
38
+            //fileWriter.close();
39
+        } catch (IOException e) {
40
+            e.printStackTrace();
41
+        } finally {
42
+
43
+        }
44
+
27
     }
45
     }
28
 
46
 
29
     @Override
47
     @Override
30
     public void write(Integer lineNumber, String valueToBeWritten) {
48
     public void write(Integer lineNumber, String valueToBeWritten) {
49
+//
50
+        try {
51
+            StringBuilder sb=new StringBuilder() ;
52
+            try {
53
+                BufferedReader  fileReader =new BufferedReader( new FileReader( file)) ;
54
+                String fileread ="";
55
+                int i=0;
56
+                while((fileread =fileReader.readLine()) !=null){
57
+                    if(lineNumber==i) {
58
+                        sb.append(valueToBeWritten);
59
+                    } else {
60
+                        sb.append(fileread);
61
+                    }
62
+                    sb.append("\n");
63
+                    i++;
64
+                }
65
+                overWrite(sb.toString());
66
+            }catch (Exception e) {
67
+                e.printStackTrace();
68
+            }
69
+
70
+
71
+
72
+
73
+        } catch (Exception e) {
74
+            e.printStackTrace();
75
+        }
31
     }
76
     }
32
 
77
 
33
     @Override
78
     @Override
34
     public String read(Integer lineNumber) {
79
     public String read(Integer lineNumber) {
35
-        return null;
80
+        try {
81
+            //return new String(Files.readAllBytes(Paths.get(file.getPath())));
82
+            String str = new String(Files.readAllLines(Paths.get(file.getPath())).get(lineNumber));
83
+            return str;
84
+        } catch (Exception e) {
85
+            e.printStackTrace();
86
+            return null;
87
+        }
88
+
36
     }
89
     }
37
 
90
 
38
     @Override
91
     @Override
39
     public String read() {
92
     public String read() {
40
-        return null;
93
+
94
+        StringBuilder sb=new StringBuilder() ;
95
+        try {
96
+          BufferedReader  fileReader =new BufferedReader( new FileReader( file)) ;
97
+           String fileread ="";
98
+            while((fileread =fileReader.readLine()) !=null){
99
+            sb.append(fileread);
100
+            sb.append("\n") ;
101
+
102
+        } }catch (Exception e) {
103
+            e.printStackTrace();
104
+        }
105
+        return sb.toString().trim() ;
106
+//        try {
107
+//            return new String(Files.readAllBytes(Paths.get(file.getPath())));
108
+//        } catch (Exception e) {
109
+//            e.printStackTrace();
110
+//            return null;
111
+//        }
112
+
41
     }
113
     }
42
 
114
 
115
+
43
     @Override
116
     @Override
44
-    public void replaceAll(String stringToReplace, String replacementString) {
45
-    }
117
+    public void replaceAll(String stringToReplace, String replacementString) {  //NOT WORKING
118
+        try{
119
+
120
+
121
+        String strFileInputs = read();
122
+            System.out.println("Inside replaceAll method -  After reading the input - " + strFileInputs );
123
+            Pattern ptn = Pattern.compile(stringToReplace );
124
+            Matcher mtch = ptn.matcher(strFileInputs  );
125
+            String replaced=mtch.replaceAll(replacementString );
46
 
126
 
127
+        overWrite(replaced ) ;
128
+
129
+            }catch (Exception e ){
130
+                e.printStackTrace() ;
131
+            }
132
+
133
+    }
47
     @Override
134
     @Override
48
     public void overWrite(String content) {
135
     public void overWrite(String content) {
136
+
137
+        try {
138
+
139
+            FileWriter fstream = new FileWriter(file, false);
140
+            fstream.write(content);
141
+            fstream.flush();
142
+        } catch (Exception e) {
143
+            e.printStackTrace();
144
+        }
145
+
49
     }
146
     }
50
 
147
 
51
     public List<String> toList() {
148
     public List<String> toList() {
52
-        return null;
149
+
150
+        List<String> list = null;
151
+        try (Stream<String> lines = Files.lines(Paths.get(file.getPath()))) {
152
+            list = lines.collect(Collectors.toList());
153
+        } catch (IOException e) {
154
+            e.printStackTrace();
155
+        }
156
+        return list;
53
     }
157
     }
54
 
158
 
55
     @Override
159
     @Override
56
     public File getFile() {
160
     public File getFile() {
57
-        return null;
161
+        try {
162
+            String str = "";
163
+            ClassLoader cl = getClass().getClassLoader();
164
+            File file = new File(cl.getResource(str).getFile());
165
+            return file;
166
+        } catch (Exception e) {
167
+            e.printStackTrace();
168
+            return null;
169
+        }
58
     }
170
     }
59
 
171
 
60
     @Override
172
     @Override
61
     public String toString() {
173
     public String toString() {
62
-        return null;
174
+        try {
175
+            StringBuilder sb=new StringBuilder() ;
176
+            String filepath=file.toString() ;
177
+            String str= new String(Files.readAllBytes(Paths.get(file.getPath())));
178
+            sb.append(filepath ) ;
179
+            sb.append( "{");
180
+            sb.append(str) ;
181
+            sb.append("}");
182
+
183
+            return sb.toString() ;
184
+        } catch (Exception e) {
185
+            e.printStackTrace();
186
+            return null;
187
+        }
63
     }
188
     }
64
 }
189
 }

+ 32
- 2
src/main/java/rocks/zipcode/NumericCharDocument.java View File

1
 package rocks.zipcode;
1
 package rocks.zipcode;
2
 
2
 
3
+import java.io.BufferedReader;
4
+import java.io.File;
3
 import java.io.IOException;
5
 import java.io.IOException;
6
+import java.nio.file.Files;
7
+import java.nio.file.Paths;
4
 
8
 
5
 /**
9
 /**
6
  * @author leon on 16/11/2018.
10
  * @author leon on 16/11/2018.
8
 public class NumericCharDocument extends Document {
12
 public class NumericCharDocument extends Document {
9
     public NumericCharDocument(String fileName) throws IOException {
13
     public NumericCharDocument(String fileName) throws IOException {
10
         super(fileName);
14
         super(fileName);
15
+
11
     }
16
     }
12
 
17
 
13
     @Override
18
     @Override
14
-    public void write(String contentToBeWritten) {
19
+    public void write(String contentToBeWritten) throws IllegalArgumentException {
20
+        if (isNumeric(contentToBeWritten) ) {
21
+           // System.out.println("inside Alpha Write being - ");
22
+            super.write(contentToBeWritten);
23
+            System.out.println("after write completed - ");
24
+        }else {
25
+            throw new IllegalArgumentException(" It' not Alpha" );
26
+        }
27
+
15
     }
28
     }
16
 
29
 
17
     private Boolean isNumeric(String s) {
30
     private Boolean isNumeric(String s) {
18
-        return null;
31
+        try{
32
+
33
+            String str = super.read();
34
+            //super.write(s);
35
+            System.out.println("inside isAlpha - " );
36
+            if(str.contains(s) ){
37
+                System.out.println("inside isAlpha -  inside if (string has alpha), will return true" );
38
+
39
+                return true;
40
+            }
41
+        }catch (Exception  e){
42
+            e.printStackTrace();
43
+        }
44
+        System.out.println("inside isAlpha -  No Alpha characters, will return false" );
45
+
46
+        return false;
47
+
48
+
19
     }
49
     }
20
 }
50
 }

+ 36
- 3
src/main/java/rocks/zipcode/SpecialCharDocument.java View File

1
 package rocks.zipcode;
1
 package rocks.zipcode;
2
 
2
 
3
+import java.io.File;
3
 import java.io.IOException;
4
 import java.io.IOException;
5
+import java.nio.file.Files;
6
+import java.nio.file.Path;
7
+import java.nio.file.Paths;
8
+import java.util.ArrayList;
9
+import java.util.List;
4
 
10
 
5
 /**
11
 /**
6
  * @author leon on 18/11/2018.
12
  * @author leon on 18/11/2018.
7
  */
13
  */
8
 public class SpecialCharDocument extends Document {
14
 public class SpecialCharDocument extends Document {
9
     public SpecialCharDocument(String fileName) throws IOException {
15
     public SpecialCharDocument(String fileName) throws IOException {
10
-        super(fileName);
16
+        super(new File(fileName));
11
     }
17
     }
12
 
18
 
13
     @Override
19
     @Override
14
     public void write(String contentToBeWritten) {
20
     public void write(String contentToBeWritten) {
15
-    }
21
+
22
+            if (isSpecialCharacters(contentToBeWritten) ) {
23
+
24
+                super.write(contentToBeWritten);
25
+               // System.out.println("after write completed - ");
26
+            } else {
27
+                throw new IllegalArgumentException(" It' not Alpa");
28
+            }
29
+
30
+
31
+        }
16
 
32
 
17
     private Boolean isSpecialCharacters(String s) {
33
     private Boolean isSpecialCharacters(String s) {
18
-        return null;
34
+        try{
35
+
36
+            String str = super.read();
37
+            //super.write(s);
38
+            System.out.println("inside isAlpha - " );
39
+            if(str.contains(s) ){
40
+                System.out.println("inside isAlpha -  inside if (string has alpha), will return true" );
41
+
42
+                return true;
43
+            }
44
+        }catch (Exception  e){
45
+            e.printStackTrace();
46
+        }
47
+        System.out.println("inside isAlpha -  No Alpha characters, will return false" );
48
+
49
+        return false;
50
+
51
+
19
     }
52
     }
20
 }
53
 }

+ 7
- 0
src/test/java/rocks/zipcode/alphadocument/AlphaDocumentWriteTest.java View File

1
 package rocks.zipcode.alphadocument;
1
 package rocks.zipcode.alphadocument;
2
 
2
 
3
 import org.junit.Assert;
3
 import org.junit.Assert;
4
+import org.junit.Before;
4
 import org.junit.Test;
5
 import org.junit.Test;
5
 import rocks.zipcode.AlphaCharDocument;
6
 import rocks.zipcode.AlphaCharDocument;
6
 import rocks.zipcode.Document;
7
 import rocks.zipcode.Document;
12
  */
13
  */
13
 public class AlphaDocumentWriteTest {
14
 public class AlphaDocumentWriteTest {
14
 
15
 
16
+    @Before
17
+    public void setUp() throws Exception {
18
+
19
+    }
20
+
15
     @Test(expected = IllegalArgumentException.class)
21
     @Test(expected = IllegalArgumentException.class)
16
     public void writeNumericValuesToFile() throws IOException {
22
     public void writeNumericValuesToFile() throws IOException {
17
         // given
23
         // given
19
         String contentToBeWritten = "123";
25
         String contentToBeWritten = "123";
20
         Document documentWriter = new AlphaCharDocument(fileName);
26
         Document documentWriter = new AlphaCharDocument(fileName);
21
 
27
 
28
+
22
         // when
29
         // when
23
         documentWriter.write(contentToBeWritten);
30
         documentWriter.write(contentToBeWritten);
24
     }
31
     }

+ 4
- 3
src/test/java/rocks/zipcode/numericdocument/NumericDocumentWriteTest.java View File

12
  */
12
  */
13
 public class NumericDocumentWriteTest {
13
 public class NumericDocumentWriteTest {
14
 
14
 
15
-    @Test
15
+    @Test (expected = IllegalArgumentException.class)
16
     public void writeNumericValuesToFile() throws IOException {
16
     public void writeNumericValuesToFile() throws IOException {
17
         // given
17
         // given
18
         String fileName = "target/file.txt";
18
         String fileName = "target/file.txt";
21
 
21
 
22
         // when
22
         // when
23
         documentWriter.write(contentToBeWritten);
23
         documentWriter.write(contentToBeWritten);
24
+
24
     }
25
     }
25
 
26
 
26
     @Test(expected = IllegalArgumentException.class)
27
     @Test(expected = IllegalArgumentException.class)
27
-    public void writeSpecialCharacter() throws IOException {
28
+    public void writeSpecialCharacter() throws IOException,IllegalArgumentException {
28
         // given
29
         // given
29
         String fileName = "target/file.txt";
30
         String fileName = "target/file.txt";
30
         String contentToBeWritten = "()";
31
         String contentToBeWritten = "()";
35
     }
36
     }
36
 
37
 
37
     @Test(expected = IllegalArgumentException.class)
38
     @Test(expected = IllegalArgumentException.class)
38
-    public void writeAlphaValuesTest() throws IOException {
39
+    public void writeAlphaValuesTest() throws IOException, IllegalArgumentException {
39
         // given
40
         // given
40
         String fileName = "target/file.txt";
41
         String fileName = "target/file.txt";
41
         String expected = "The quick brown foxy";
42
         String expected = "The quick brown foxy";

+ 3
- 2
src/test/java/rocks/zipcode/specialdocument/SpecialCharDocumentTest.java View File

21
 
21
 
22
         // when
22
         // when
23
         documentWriter.write(contentToBeWritten);
23
         documentWriter.write(contentToBeWritten);
24
+
24
     }
25
     }
25
 
26
 
26
-    @Test
27
+    @Test(expected = IllegalArgumentException.class)
27
     public void writeSpecialCharacter1() throws IOException {
28
     public void writeSpecialCharacter1() throws IOException {
28
         // given
29
         // given
29
         String fileName = "target/file.txt";
30
         String fileName = "target/file.txt";
35
     }
36
     }
36
 
37
 
37
 
38
 
38
-    @Test
39
+    @Test(expected = IllegalArgumentException.class)
39
     public void writeSpecialCharacter2() throws IOException {
40
     public void writeSpecialCharacter2() throws IOException {
40
         // given
41
         // given
41
         String fileName = "target/file.txt";
42
         String fileName = "target/file.txt";