Procházet zdrojové kódy

Tests pass but needs refactor

Nick Satinover před 7 roky
rodič
revize
7757eea0d5

+ 38
- 15
Crypto/src/Main/ROT13.java Zobrazit soubor

@@ -1,5 +1,4 @@
1 1
 
2
-import java.util.ArrayList;
3 2
 import java.util.Arrays;
4 3
 import java.util.List;
5 4
 
@@ -8,30 +7,38 @@ import static java.lang.Character.isUpperCase;
8 7
 import static java.lang.Character.toLowerCase;
9 8
 
10 9
 public class ROT13  {
11
-    private int rotateBy;
10
+    private int rotate;
12 11
     private int encryptShift = 26;
13
-    private int decryptShift = -26;
12
+
14 13
 
15 14
     ROT13(Character cs, Character cf) {
16
-        rotateBy = (int)(cs) - (int)(cf);
15
+        rotate = (int)(cs) - (int)(cf);
17 16
     }
18 17
 
19 18
     ROT13() {
20 19
     }
21 20
 
22
-    public String crypt(String text) throws UnsupportedOperationException {
21
+    public int getRotate() {
22
+        return rotate;
23
+    }
23 24
 
24
-        return "";
25
+    public String crypt(String text){
26
+        return encrypt(text);
25 27
     }
26 28
 
27
-    public String encrypt(String text) {
29
+
30
+    public String crypt(String text, int shift, int rotate, boolean encrypt) throws UnsupportedOperationException {
28 31
         List<String> textList = Arrays.asList(text.split(" "));
29 32
         StringBuilder builder = new StringBuilder();
30 33
 
31 34
         for (String s: textList) {
32 35
             char[] charArr = s.toCharArray();
33 36
             for (char c: charArr) {
34
-                builder.append(shiftChar(c, encryptShift));
37
+                if (encrypt)
38
+                    builder.append(shiftCharEncrypt(c, shift, rotate));
39
+                else {
40
+                    builder.append(shiftCharDecrypt(c, shift, rotate));
41
+                }
35 42
             }
36 43
             builder.append(" ");
37 44
         }
@@ -39,21 +46,37 @@ public class ROT13  {
39 46
         return builder.toString().trim();
40 47
     }
41 48
 
42
-    public String shiftChar(char c, int shift){
49
+    public String encrypt(String text) {
50
+        return crypt(text, encryptShift, getRotate(), true);
51
+    }
52
+
53
+    public String decrypt(String text) {
54
+
55
+        return crypt(text, encryptShift * -1, getRotate() * -1, false);
56
+    }
57
+
58
+    public String shiftCharEncrypt(char c, int shift, int rotate){
43 59
         if (c > 64){
44
-            if ( (c > 96 && (c+rotateBy) > 96) || (c < 97 && (c+rotateBy) > 64))
45
-                return  ( Character.toString( (char)(c+rotateBy) ) );
60
+            if ( (c > 96 && (c+ rotate) > 96) || (c <= 96 && (c+ rotate) > 64))
61
+                return  ( Character.toString( (char)(c+ rotate) ) );
46 62
             else {
47
-                return  ( Character.toString( (char)(c+rotateBy+shift) ) );
63
+                return  ( Character.toString( (char)(c+ rotate +shift) ) );
48 64
             }
49 65
         } else {
50 66
             return Character.toString(c);
51 67
         }
52 68
     }
53 69
 
54
-    public String decrypt(String text) {
55
-
56
-        return crypt(text);
70
+    public String shiftCharDecrypt(char c, int shift, int rotate){
71
+        if (c > 64){
72
+            if ( (c + rotate > 122) || (c <= 90  && c + rotate > 90 ) ) // ( (c < 123 && (c+ rotate) < 123) || (c <= 123 && (c+ rotate) > 64))
73
+                return  ( Character.toString( (char)(c+ rotate +shift) ) );
74
+            else {
75
+                return  ( Character.toString( (char)(c+ rotate) ) );
76
+            }
77
+        } else {
78
+            return Character.toString(c);
79
+        }
57 80
     }
58 81
 
59 82
     public static String rotate(String s, Character c) {

+ 33
- 14
Crypto/src/Main/ROT13A.java Zobrazit soubor

@@ -1,5 +1,4 @@
1 1
 
2
-import java.util.ArrayList;
3 2
 import java.util.Arrays;
4 3
 import java.util.List;
5 4
 
@@ -8,25 +7,33 @@ import static java.lang.Character.isUpperCase;
8 7
 import static java.lang.Character.toLowerCase;
9 8
 
10 9
 public class ROT13A  {
11
-    private int rotateBy;
10
+    private int rotate;
12 11
     private int encryptShift = 26;
13
-    private int decryptShift = -26;
12
+
14 13
 
15 14
     ROT13A(Character cs, Character cf) {
16
-        rotateBy = (int)(cs) - (int)(cf);
15
+        rotate = (int)(cs) - (int)(cf);
17 16
     }
18 17
 
19 18
     ROT13A() {
20 19
     }
21 20
 
22
-    public String crypt(String text, int shift) throws UnsupportedOperationException {
21
+    public int getRotate() {
22
+        return rotate;
23
+    }
24
+
25
+    public String crypt(String text, int shift, int rotate, boolean encrypt) throws UnsupportedOperationException {
23 26
         List<String> textList = Arrays.asList(text.split(" "));
24 27
         StringBuilder builder = new StringBuilder();
25 28
 
26 29
         for (String s: textList) {
27 30
             char[] charArr = s.toCharArray();
28 31
             for (char c: charArr) {
29
-                builder.append(shiftChar(c, shift));
32
+                if (encrypt)
33
+                    builder.append(shiftCharEncrypt(c, shift, rotate));
34
+                else {
35
+                    builder.append(shiftCharDecrypt(c, shift, rotate));
36
+                }
30 37
             }
31 38
             builder.append(" ");
32 39
         }
@@ -35,24 +42,36 @@ public class ROT13A  {
35 42
     }
36 43
 
37 44
     public String encrypt(String text) {
38
-        return crypt(text, encryptShift);
45
+        return crypt(text, encryptShift, getRotate(), true);
46
+    }
47
+
48
+    public String decrypt(String text) {
49
+
50
+        return crypt(text, encryptShift * -1, getRotate() * -1, false);
39 51
     }
40 52
 
41
-    public String shiftChar(char c, int shift){
53
+    public String shiftCharEncrypt(char c, int shift, int rotate){
42 54
         if (c > 64){
43
-            if ( (c > 96 && (c+rotateBy) > 96) || (c < 97 && (c+rotateBy) > 64))
44
-                return  ( Character.toString( (char)(c+rotateBy) ) );
55
+            if ( (c > 96 && (c+ rotate) > 96) || (c <= 96 && (c+ rotate) > 64))
56
+                return  ( Character.toString( (char)(c+ rotate) ) );
45 57
             else {
46
-                return  ( Character.toString( (char)(c+rotateBy+shift) ) );
58
+                return  ( Character.toString( (char)(c+ rotate +shift) ) );
47 59
             }
48 60
         } else {
49 61
             return Character.toString(c);
50 62
         }
51 63
     }
52 64
 
53
-    public String decrypt(String text) {
54
-
55
-        return crypt(text, decryptShift);
65
+    public String shiftCharDecrypt(char c, int shift, int rotate){
66
+        if (c > 64){
67
+            if ( (c + rotate > 122) || (c <= 90  && c + rotate > 90 ) ) // ( (c < 123 && (c+ rotate) < 123) || (c <= 123 && (c+ rotate) > 64))
68
+                return  ( Character.toString( (char)(c+ rotate +shift) ) );
69
+            else {
70
+                return  ( Character.toString( (char)(c+ rotate) ) );
71
+            }
72
+        } else {
73
+            return Character.toString(c);
74
+        }
56 75
     }
57 76
 
58 77
     public static String rotate(String s, Character c) {

+ 1
- 1
Crypto/src/Test/ROT13Test.java Zobrazit soubor

@@ -51,7 +51,7 @@ public class ROT13Test {
51 51
     @Test
52 52
     public void cryptTest1() {
53 53
         // Given
54
-        ROT13A cipher = new ROT13A('a', 'n');
54
+        ROT13 cipher = new ROT13('a', 'n');
55 55
 
56 56
         String Q1 = "Why did the chicken cross the road?";
57 57
         String A1 = "Jul qvq gur puvpxra pebff gur ebnq?";