Trinh Tong пре 6 година
родитељ
комит
4dccdd87dc
3 измењених фајлова са 39 додато и 23 уклоњено
  1. 0
    0
      sonnet18.txt
  2. 17
    23
      src/main/java/AtbashCipher.java
  3. 22
    0
      src/test/java/AtbashCipherTest.java

src/main/resources/sonnet18.txt → sonnet18.txt Прегледај датотеку


+ 17
- 23
src/main/java/AtbashCipher.java Прегледај датотеку

1
 import java.io.*;
1
 import java.io.*;
2
-import java.nio.file.Path;
3
-import java.nio.file.Paths;
4
-import java.util.ArrayList;
2
+
5
 import java.util.HashMap;
3
 import java.util.HashMap;
6
-import java.util.stream.IntStream;
7
 import java.util.stream.Stream;
4
 import java.util.stream.Stream;
8
 
5
 
6
+
9
 public class AtbashCipher{
7
 public class AtbashCipher{
10
 
8
 
11
     private static final String abc = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz";
9
     private static final String abc = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz";
24
         for (int i = 0; i < abcArr.length; i++) {
22
         for (int i = 0; i < abcArr.length; i++) {
25
             key.put(abcArr[i], cbaArr[i]);
23
             key.put(abcArr[i], cbaArr[i]);
26
         }
24
         }
27
-
28
     }
25
     }
29
 
26
 
30
     public static String cipher(String s) {
27
     public static String cipher(String s) {
35
                 charArray[i] = key.get(charArray[i]);
32
                 charArray[i] = key.get(charArray[i]);
36
             }
33
             }
37
         }
34
         }
38
-
39
         return charArrayToString(charArray);
35
         return charArrayToString(charArray);
40
     }
36
     }
41
 
37
 
42
-    public static char swap(char c) {
43
-        return key.get(c);
44
-    }
45
-
46
-    public static String charArrayToString(char[] chars) {
38
+    public  static String charArrayToString(char[] chars) {
47
         StringBuilder sb = new StringBuilder();
39
         StringBuilder sb = new StringBuilder();
48
         for (char c: chars) {
40
         for (char c: chars) {
49
             sb.append(c);
41
             sb.append(c);
51
         return sb.toString();
43
         return sb.toString();
52
     }
44
     }
53
 
45
 
54
-    public void cipherFile(String filePath) throws FileNotFoundException {
46
+    public void cipherFile(String filePathIn, String filePathOut) throws FileNotFoundException {
47
+        String inFile = fileToString(filePathIn);
55
 
48
 
56
-        String ourFile = fileToString(filePath);
57
-
58
-        ArrayList<Character> charStream = ourFile.chars()
59
-                .mapToObj(c -> (char) c)
60
-                .forEach(AtbashCipher::swap)
61
-
62
-
63
-
64
-//        char[] ciphered = Stream.of(ourFile)
65
-//                .forEach(AtbashCipher::swap);
49
+        PrintStream file = new PrintStream(new File(filePathOut));
50
+        PrintStream console = System.out;
66
 
51
 
52
+        System.setOut(file);
67
 
53
 
54
+        String swappedChars = Stream.of(inFile)
55
+                .map(AtbashCipher::cipher)
56
+                .reduce("", String::concat);
68
 
57
 
58
+        System.out.println(swappedChars);
59
+        System.setOut(console);
69
     }
60
     }
70
 
61
 
62
+
71
     private String fileToString(String filePath) throws FileNotFoundException {
63
     private String fileToString(String filePath) throws FileNotFoundException {
72
 
64
 
73
-        FileReader fReader = new FileReader(filePath);
65
+        File file = new File(filePath);
66
+
67
+        FileReader fReader = new FileReader(file);
74
         BufferedReader bufferedReader = new BufferedReader(fReader);
68
         BufferedReader bufferedReader = new BufferedReader(fReader);
75
 
69
 
76
         StringBuilder sb = new StringBuilder();
70
         StringBuilder sb = new StringBuilder();

+ 22
- 0
src/test/java/AtbashCipherTest.java Прегледај датотеку

1
 import org.junit.Assert;
1
 import org.junit.Assert;
2
 import org.junit.Test;
2
 import org.junit.Test;
3
 
3
 
4
+import java.io.BufferedReader;
5
+import java.io.FileNotFoundException;
6
+import java.io.FileReader;
7
+
4
 import static org.junit.Assert.*;
8
 import static org.junit.Assert.*;
5
 
9
 
6
 public class AtbashCipherTest {
10
 public class AtbashCipherTest {
15
 
19
 
16
         Assert.assertEquals(exp, actual);
20
         Assert.assertEquals(exp, actual);
17
     }
21
     }
22
+
23
+    @Test
24
+    public void testFiles() throws FileNotFoundException {
25
+
26
+        AtbashCipher cipher = new AtbashCipher();
27
+
28
+        String inputFile = "sonnet18.txt";
29
+        String outputTest = "testFileOutput.txt";
30
+        String decryptTest = "decryptedTestFile.txt";
31
+
32
+        cipher.cipherFile(inputFile, outputTest);
33
+        cipher.cipherFile(outputTest, decryptTest);
34
+
35
+        FileReader fReader = new FileReader(inputFile);
36
+        BufferedReader bufferedReader = new BufferedReader(fReader);
37
+
38
+
39
+    }
18
 }
40
 }