Pārlūkot izejas kodu

Tests pass but needs refactor

Nick Satinover 7 gadus atpakaļ
vecāks
revīzija
7757eea0d5

+ 38
- 15
Crypto/src/Main/ROT13.java Parādīt failu

1
 
1
 
2
-import java.util.ArrayList;
3
 import java.util.Arrays;
2
 import java.util.Arrays;
4
 import java.util.List;
3
 import java.util.List;
5
 
4
 
8
 import static java.lang.Character.toLowerCase;
7
 import static java.lang.Character.toLowerCase;
9
 
8
 
10
 public class ROT13  {
9
 public class ROT13  {
11
-    private int rotateBy;
10
+    private int rotate;
12
     private int encryptShift = 26;
11
     private int encryptShift = 26;
13
-    private int decryptShift = -26;
12
+
14
 
13
 
15
     ROT13(Character cs, Character cf) {
14
     ROT13(Character cs, Character cf) {
16
-        rotateBy = (int)(cs) - (int)(cf);
15
+        rotate = (int)(cs) - (int)(cf);
17
     }
16
     }
18
 
17
 
19
     ROT13() {
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
         List<String> textList = Arrays.asList(text.split(" "));
31
         List<String> textList = Arrays.asList(text.split(" "));
29
         StringBuilder builder = new StringBuilder();
32
         StringBuilder builder = new StringBuilder();
30
 
33
 
31
         for (String s: textList) {
34
         for (String s: textList) {
32
             char[] charArr = s.toCharArray();
35
             char[] charArr = s.toCharArray();
33
             for (char c: charArr) {
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
             builder.append(" ");
43
             builder.append(" ");
37
         }
44
         }
39
         return builder.toString().trim();
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
         if (c > 64){
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
             else {
62
             else {
47
-                return  ( Character.toString( (char)(c+rotateBy+shift) ) );
63
+                return  ( Character.toString( (char)(c+ rotate +shift) ) );
48
             }
64
             }
49
         } else {
65
         } else {
50
             return Character.toString(c);
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
     public static String rotate(String s, Character c) {
82
     public static String rotate(String s, Character c) {

+ 33
- 14
Crypto/src/Main/ROT13A.java Parādīt failu

1
 
1
 
2
-import java.util.ArrayList;
3
 import java.util.Arrays;
2
 import java.util.Arrays;
4
 import java.util.List;
3
 import java.util.List;
5
 
4
 
8
 import static java.lang.Character.toLowerCase;
7
 import static java.lang.Character.toLowerCase;
9
 
8
 
10
 public class ROT13A  {
9
 public class ROT13A  {
11
-    private int rotateBy;
10
+    private int rotate;
12
     private int encryptShift = 26;
11
     private int encryptShift = 26;
13
-    private int decryptShift = -26;
12
+
14
 
13
 
15
     ROT13A(Character cs, Character cf) {
14
     ROT13A(Character cs, Character cf) {
16
-        rotateBy = (int)(cs) - (int)(cf);
15
+        rotate = (int)(cs) - (int)(cf);
17
     }
16
     }
18
 
17
 
19
     ROT13A() {
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
         List<String> textList = Arrays.asList(text.split(" "));
26
         List<String> textList = Arrays.asList(text.split(" "));
24
         StringBuilder builder = new StringBuilder();
27
         StringBuilder builder = new StringBuilder();
25
 
28
 
26
         for (String s: textList) {
29
         for (String s: textList) {
27
             char[] charArr = s.toCharArray();
30
             char[] charArr = s.toCharArray();
28
             for (char c: charArr) {
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
             builder.append(" ");
38
             builder.append(" ");
32
         }
39
         }
35
     }
42
     }
36
 
43
 
37
     public String encrypt(String text) {
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
         if (c > 64){
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
             else {
57
             else {
46
-                return  ( Character.toString( (char)(c+rotateBy+shift) ) );
58
+                return  ( Character.toString( (char)(c+ rotate +shift) ) );
47
             }
59
             }
48
         } else {
60
         } else {
49
             return Character.toString(c);
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
     public static String rotate(String s, Character c) {
77
     public static String rotate(String s, Character c) {

+ 1
- 1
Crypto/src/Test/ROT13Test.java Parādīt failu

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