Преглед изворни кода

atbash-cipher: make class use instance method

Fixes issue #366.
Frida Johanne Tveit пре 9 година
родитељ
комит
065fb5907f

+ 5
- 5
exercises/atbash-cipher/src/example/java/Atbash.java Прегледај датотеку

7
     private static final String PLAIN = "abcdefghijklmnopqrstuvwxyz";
7
     private static final String PLAIN = "abcdefghijklmnopqrstuvwxyz";
8
     private static final String CIPHER = "zyxwvutsrqponmlkjihgfedcba";
8
     private static final String CIPHER = "zyxwvutsrqponmlkjihgfedcba";
9
 
9
 
10
-    public static String encode(String input) {
10
+    public String encode(String input) {
11
         String encoded = stripInvalidCharacters(input).toLowerCase();
11
         String encoded = stripInvalidCharacters(input).toLowerCase();
12
         String cyphered = "";
12
         String cyphered = "";
13
 
13
 
18
         return splitIntoFiveLetterWords(cyphered);
18
         return splitIntoFiveLetterWords(cyphered);
19
     }
19
     }
20
 
20
 
21
-    public static String decode(String input) {
21
+    public String decode(String input) {
22
         String encoded = stripInvalidCharacters(input).toLowerCase();
22
         String encoded = stripInvalidCharacters(input).toLowerCase();
23
         String deciphered = "";
23
         String deciphered = "";
24
 
24
 
29
         return deciphered;
29
         return deciphered;
30
     }
30
     }
31
 
31
 
32
-    private static String stripInvalidCharacters(String input) {
32
+    private String stripInvalidCharacters(String input) {
33
         String filteredValue = "";
33
         String filteredValue = "";
34
 
34
 
35
         for (char c : input.toCharArray()) {
35
         for (char c : input.toCharArray()) {
41
         return filteredValue;
41
         return filteredValue;
42
     }
42
     }
43
 
43
 
44
-    private static char applyCipher(char input) {
44
+    private char applyCipher(char input) {
45
         int idx = PLAIN.indexOf(input);
45
         int idx = PLAIN.indexOf(input);
46
 
46
 
47
         return idx >= 0 ? CIPHER.toCharArray()[idx] : input;
47
         return idx >= 0 ? CIPHER.toCharArray()[idx] : input;
48
     }
48
     }
49
 
49
 
50
-    private static String splitIntoFiveLetterWords(String value) {
50
+    private String splitIntoFiveLetterWords(String value) {
51
         List<String> words = new ArrayList<>();
51
         List<String> words = new ArrayList<>();
52
 
52
 
53
         for (int i = 0; i < value.length(); i += GROUP_SIZE) {
53
         for (int i = 0; i < value.length(); i += GROUP_SIZE) {

+ 18
- 18
exercises/atbash-cipher/src/test/java/AtbashTest.java Прегледај датотеку

1
-import org.junit.Test;
2
 import org.junit.Ignore;
1
 import org.junit.Ignore;
2
+import org.junit.Test;
3
 import org.junit.experimental.runners.Enclosed;
3
 import org.junit.experimental.runners.Enclosed;
4
 import org.junit.runner.RunWith;
4
 import org.junit.runner.RunWith;
5
 import org.junit.runners.Parameterized;
5
 import org.junit.runners.Parameterized;
20
 
20
 
21
         @Parameters(name = "{index}: expected plaintext \"{0}\" to encode to ciphertext \"{1}\".")
21
         @Parameters(name = "{index}: expected plaintext \"{0}\" to encode to ciphertext \"{1}\".")
22
         public static Collection<Object[]> data() {
22
         public static Collection<Object[]> data() {
23
-            return Arrays.asList(new Object[][] {
24
-                    { "no", "ml" },
25
-                    { "yes", "bvh" },
26
-                    { "OMG", "lnt" },
27
-                    { "mindblowingly", "nrmwy oldrm tob" },
28
-                    { "Testing, 1 2 3, testing.", "gvhgr mt123 gvhgr mt" },
29
-                    { "Truth is fiction.", "gifgs rhurx grlm" },
30
-                    { "The quick brown fox jumps over the lazy dog.", "gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt" }
23
+            return Arrays.asList(new Object[][]{
24
+                    {"no", "ml"},
25
+                    {"yes", "bvh"},
26
+                    {"OMG", "lnt"},
27
+                    {"mindblowingly", "nrmwy oldrm tob"},
28
+                    {"Testing, 1 2 3, testing.", "gvhgr mt123 gvhgr mt"},
29
+                    {"Truth is fiction.", "gifgs rhurx grlm"},
30
+                    {"The quick brown fox jumps over the lazy dog.", "gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt"}
31
             });
31
             });
32
         }
32
         }
33
 
33
 
37
         }
37
         }
38
 
38
 
39
 
39
 
40
-    @Test
40
+        @Test
41
         public void test() {
41
         public void test() {
42
-            assertEquals(ciphertext, Atbash.encode(plaintext));
42
+            assertEquals(ciphertext, new Atbash().encode(plaintext));
43
         }
43
         }
44
     }
44
     }
45
 
45
 
50
 
50
 
51
         @Parameters(name = "{index}: expected ciphertext \"{0}\" to decode to plaintext \"{1}\".")
51
         @Parameters(name = "{index}: expected ciphertext \"{0}\" to decode to plaintext \"{1}\".")
52
         public static Collection<Object[]> data() {
52
         public static Collection<Object[]> data() {
53
-            return Arrays.asList(new Object[][] {
54
-                    { "vcvix rhn", "exercism" },
55
-                    { "zmlyh gzxov rhlug vmzhg vkkrm thglm v", "anobstacleisoftenasteppingstone" },
56
-                    { "gvhgr mt123 gvhgr mt", "testing123testing" },
57
-                    { "gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt", "thequickbrownfoxjumpsoverthelazydog" }
53
+            return Arrays.asList(new Object[][]{
54
+                    {"vcvix rhn", "exercism"},
55
+                    {"zmlyh gzxov rhlug vmzhg vkkrm thglm v", "anobstacleisoftenasteppingstone"},
56
+                    {"gvhgr mt123 gvhgr mt", "testing123testing"},
57
+                    {"gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt", "thequickbrownfoxjumpsoverthelazydog"}
58
             });
58
             });
59
         }
59
         }
60
 
60
 
64
         }
64
         }
65
 
65
 
66
         @Ignore
66
         @Ignore
67
-    @Test
67
+        @Test
68
         public void test() {
68
         public void test() {
69
-            assertEquals(plaintext, Atbash.decode(ciphertext));
69
+            assertEquals(plaintext, new Atbash().decode(ciphertext));
70
         }
70
         }
71
     }
71
     }
72
 }
72
 }