Parcourir la source

test case passed

Aleena Rose-Mathew il y a 6 ans
Parent
révision
5d1ad93727
3 fichiers modifiés avec 53 ajouts et 13 suppressions
  1. 30
    10
      Crypto/src/ROT13.java
  2. 3
    2
      Crypto/src/ROT13Test.java
  3. 20
    1
      pom.xml

+ 30
- 10
Crypto/src/ROT13.java Voir le fichier

@@ -1,10 +1,10 @@
1
-import static java.lang.Character.isLowerCase;
2
-import static java.lang.Character.isUpperCase;
3
-import static java.lang.Character.toLowerCase;
4 1
 
5 2
 public class ROT13  {
6 3
 
4
+    Integer diff;
7 5
     ROT13(Character cs, Character cf) {
6
+        this.diff=Math.abs((int)cs-(int)cf);
7
+
8 8
     }
9 9
 
10 10
     ROT13() {
@@ -12,21 +12,41 @@ public class ROT13  {
12 12
 
13 13
 
14 14
     public String crypt(String text) throws UnsupportedOperationException {
15
-
16
-        return "";
15
+        String str="";
16
+
17
+        for (int i=0; i<text.length(); i++)
18
+        {
19
+            if(!Character.isLetter(text.charAt(i)))
20
+            {
21
+                str+=text.charAt(i);
22
+            }
23
+            else if (Character.isUpperCase(text.charAt(i)))
24
+            {
25
+                char ch = (char)(((int)text.charAt(i) + diff - 65) % 26 + 65);
26
+                str+=ch;
27
+            }
28
+            else if(Character.isLowerCase(text.charAt(i)))
29
+            {
30
+                char ch = (char)(((int)text.charAt(i) + diff - 97) % 26 + 97);
31
+                str+=ch;
32
+            }
33
+        }
34
+        return str;
17 35
     }
18 36
 
19 37
     public String encrypt(String text) {
20
-        return text;
38
+
39
+        return crypt(text);
21 40
     }
22 41
 
23 42
     public String decrypt(String text) {
24
-        return text;
43
+        return crypt(text);
25 44
     }
26 45
 
27
-    public static String rotate(String s, Character c) {
28
-
29
-        return "";
46
+    public static String rotate(String string, Character c)
47
+    {
48
+        Integer diff=(int)c-(int)'A';
49
+        return string.substring(diff,string.length())+string.substring(0,diff);
30 50
     }
31 51
 
32 52
 }

+ 3
- 2
Crypto/src/ROT13Test.java Voir le fichier

@@ -1,6 +1,6 @@
1
-import org.junit.Test;
2 1
 
3
-import static org.junit.Assert.*;
2
+import org.testng.annotations.Test;
3
+import static org.testng.Assert.assertTrue;
4 4
 
5 5
 public class ROT13Test {
6 6
 
@@ -63,6 +63,7 @@ public class ROT13Test {
63 63
         String actual = cipher.encrypt(Q1);
64 64
         System.out.println(Q1);
65 65
         System.out.println(A1);
66
+        System.out.println(actual);
66 67
         // Then
67 68
         assertTrue(actual.equals(A1));
68 69
 

+ 20
- 1
pom.xml Voir le fichier

@@ -7,6 +7,25 @@
7 7
     <groupId>com.zipcodewilmington</groupId>
8 8
     <artifactId>SimpleCrypt</artifactId>
9 9
     <version>1.0-SNAPSHOT</version>
10
-
10
+    <dependencies>
11
+        <dependency>
12
+            <groupId>junit</groupId>
13
+            <artifactId>junit</artifactId>
14
+            <version>4.12</version>
15
+            <scope>test</scope>
16
+        </dependency>
17
+        <dependency>
18
+            <groupId>org.junit.jupiter</groupId>
19
+            <artifactId>junit-jupiter-api</artifactId>
20
+            <version>RELEASE</version>
21
+            <scope>test</scope>
22
+        </dependency>
23
+        <dependency>
24
+            <groupId>org.testng</groupId>
25
+            <artifactId>testng</artifactId>
26
+            <version>RELEASE</version>
27
+            <scope>compile</scope>
28
+        </dependency>
29
+    </dependencies>
11 30
 
12 31
 </project>