瀏覽代碼

testing atbash cipher

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,11 +1,9 @@
1 1
 import java.io.*;
2
-import java.nio.file.Path;
3
-import java.nio.file.Paths;
4
-import java.util.ArrayList;
2
+
5 3
 import java.util.HashMap;
6
-import java.util.stream.IntStream;
7 4
 import java.util.stream.Stream;
8 5
 
6
+
9 7
 public class AtbashCipher{
10 8
 
11 9
     private static final String abc = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz";
@@ -24,7 +22,6 @@ public class AtbashCipher{
24 22
         for (int i = 0; i < abcArr.length; i++) {
25 23
             key.put(abcArr[i], cbaArr[i]);
26 24
         }
27
-
28 25
     }
29 26
 
30 27
     public static String cipher(String s) {
@@ -35,15 +32,10 @@ public class AtbashCipher{
35 32
                 charArray[i] = key.get(charArray[i]);
36 33
             }
37 34
         }
38
-
39 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 39
         StringBuilder sb = new StringBuilder();
48 40
         for (char c: chars) {
49 41
             sb.append(c);
@@ -51,26 +43,28 @@ public class AtbashCipher{
51 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 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 68
         BufferedReader bufferedReader = new BufferedReader(fReader);
75 69
 
76 70
         StringBuilder sb = new StringBuilder();

+ 22
- 0
src/test/java/AtbashCipherTest.java 查看文件

@@ -1,6 +1,10 @@
1 1
 import org.junit.Assert;
2 2
 import org.junit.Test;
3 3
 
4
+import java.io.BufferedReader;
5
+import java.io.FileNotFoundException;
6
+import java.io.FileReader;
7
+
4 8
 import static org.junit.Assert.*;
5 9
 
6 10
 public class AtbashCipherTest {
@@ -15,4 +19,22 @@ public class AtbashCipherTest {
15 19
 
16 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
 }