Lauren Green před 6 roky
rodič
revize
80027c9025

+ 0
- 32
Crypto/src/ROT13.java Zobrazit soubor

@@ -1,32 +0,0 @@
1
-import static java.lang.Character.isLowerCase;
2
-import static java.lang.Character.isUpperCase;
3
-import static java.lang.Character.toLowerCase;
4
-
5
-public class ROT13  {
6
-
7
-    ROT13(Character cs, Character cf) {
8
-    }
9
-
10
-    ROT13() {
11
-    }
12
-
13
-
14
-    public String crypt(String text) throws UnsupportedOperationException {
15
-
16
-        return "";
17
-    }
18
-
19
-    public String encrypt(String text) {
20
-        return text;
21
-    }
22
-
23
-    public String decrypt(String text) {
24
-        return text;
25
-    }
26
-
27
-    public static String rotate(String s, Character c) {
28
-
29
-        return "";
30
-    }
31
-
32
-}

+ 91
- 0
Crypto/src/main/java/ROT13.java Zobrazit soubor

@@ -0,0 +1,91 @@
1
+import java.util.Arrays;
2
+import java.util.List;
3
+import java.util.Map;
4
+import java.util.stream.Stream;
5
+
6
+import static java.lang.Character.isLowerCase;
7
+import static java.lang.Character.isUpperCase;
8
+import static java.lang.Character.toLowerCase;
9
+
10
+public class ROT13  {
11
+
12
+    Character original;
13
+    Character adjusted;
14
+    int shift = 13;
15
+    String alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
16
+    String shifted = "NOPQRSTUVWXYZABCDEFGHIJKLM";
17
+
18
+    ROT13(Character cs, Character cf) {
19
+        this.original = cs;
20
+        this.adjusted = cf;
21
+        this.shift = cf - cs;
22
+        this.shifted = rotate(alpha, cf);
23
+
24
+    }
25
+
26
+    ROT13() {
27
+
28
+    }
29
+
30
+    public String getShifted() {
31
+        return shifted;
32
+    }
33
+
34
+
35
+    public String crypt(String text) throws UnsupportedOperationException {
36
+
37
+        return text;
38
+    }
39
+
40
+    public String encrypt(String text) {
41
+        String result = "";
42
+        text = text.toUpperCase();
43
+        for (Character c: text.toCharArray()) {
44
+            if(Character.isLetter(c)) {
45
+                for (int i = 0; i < alpha.length(); i++) {
46
+                    if(c.equals(alpha.charAt(i))) {
47
+                        result += getShifted().charAt(i);
48
+                    }
49
+                }
50
+            } else {
51
+                result += c;
52
+            }
53
+
54
+        }
55
+
56
+        return result.charAt(0) + result.substring(1).toLowerCase();
57
+
58
+    }
59
+
60
+    public String decrypt(String text) {
61
+        String result = "";
62
+        text = text.toUpperCase();
63
+        for (Character c: text.toCharArray()) {
64
+            if(Character.isLetter(c)) {
65
+                for (int i = 0; i < alpha.length(); i++) {
66
+                    if(c.equals(alpha.charAt(i))) {
67
+                        result += getShifted().charAt(i);
68
+                    }
69
+                }
70
+            } else {
71
+                result += c;
72
+            }
73
+
74
+        }
75
+
76
+        return result.charAt(0) + result.substring(1).toLowerCase();
77
+    }
78
+
79
+    public static String rotate(String s, Character c) {
80
+
81
+        c = Character.toUpperCase(c);
82
+        int index = 0;
83
+        for (int i = 0; i < s.length(); i++) {
84
+            if(s.charAt(i) == c){
85
+                index = i;
86
+            }
87
+        }
88
+        return s.substring(index) + s.substring(0, index);
89
+    }
90
+
91
+}

Crypto/src/ROT13Test.java → Crypto/src/test/java/ROT13Test.java Zobrazit soubor

@@ -1,10 +1,10 @@
1
+import org.junit.Assert;
1 2
 import org.junit.Test;
2 3
 
3 4
 import static org.junit.Assert.*;
4 5
 
5 6
 public class ROT13Test {
6 7
 
7
-
8 8
     @Test
9 9
     public void rotateStringTest0() {
10 10
         // Given
@@ -52,6 +52,7 @@ public class ROT13Test {
52 52
     public void cryptTest1() {
53 53
         // Given
54 54
         ROT13 cipher = new ROT13('a', 'n');
55
+        System.out.println(cipher.getShifted());
55 56
 
56 57
         String Q1 = "Why did the chicken cross the road?";
57 58
         String A1 = "Jul qvq gur puvpxra pebff gur ebnq?";
@@ -64,14 +65,14 @@ public class ROT13Test {
64 65
         System.out.println(Q1);
65 66
         System.out.println(A1);
66 67
         // Then
67
-        assertTrue(actual.equals(A1));
68
+        Assert.assertEquals(A1, actual);
68 69
 
69 70
         // When
70 71
         String actual2 = cipher.decrypt(Q2);
71 72
         System.out.println(Q2);
72 73
         System.out.println(A2);
73 74
         // Then
74
-        assertTrue(actual2.equals(A2));
75
+        Assert.assertEquals(A2, actual2);
75 76
     }
76 77
     @Test
77 78
     public void cryptTest2() {

+ 16
- 0
SimpleCrypt.iml Zobrazit soubor

@@ -0,0 +1,16 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
3
+  <component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_5">
4
+    <output url="file://$MODULE_DIR$/target/classes" />
5
+    <output-test url="file://$MODULE_DIR$/target/test-classes" />
6
+    <content url="file://$MODULE_DIR$">
7
+      <sourceFolder url="file://$MODULE_DIR$/Crypto/src/main/java" isTestSource="false" />
8
+      <sourceFolder url="file://$MODULE_DIR$/Crypto/src/test/java" isTestSource="true" />
9
+      <excludeFolder url="file://$MODULE_DIR$/target" />
10
+    </content>
11
+    <orderEntry type="inheritedJdk" />
12
+    <orderEntry type="sourceFolder" forTests="false" />
13
+    <orderEntry type="library" name="Maven: junit:junit:4.12" level="project" />
14
+    <orderEntry type="library" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
15
+  </component>
16
+</module>

+ 8
- 0
pom.xml Zobrazit soubor

@@ -8,5 +8,13 @@
8 8
     <artifactId>SimpleCrypt</artifactId>
9 9
     <version>1.0-SNAPSHOT</version>
10 10
 
11
+    <dependencies>
11 12
 
13
+
14
+        <dependency>
15
+            <groupId>junit</groupId>
16
+            <artifactId>junit</artifactId>
17
+            <version>RELEASE</version>
18
+        </dependency>
19
+    </dependencies>
12 20
 </project>