Khalil Malik Saboor 6 лет назад
Родитель
Сommit
93989804f1
5 измененных файлов: 69 добавлений и 7 удалений
  1. 27
    3
      Crypto/src/ROT13.java
  2. 27
    1
      Crypto/src/ROT13Test.java
  3. 1
    1
      SimpleCrypt.iml
  4. 12
    0
      pom.xml
  5. 2
    2
      sonnet18.txt

+ 27
- 3
Crypto/src/ROT13.java Просмотреть файл

@@ -1,3 +1,7 @@
1
+import java.io.File;
2
+import java.io.FileNotFoundException;
3
+import java.util.Scanner;
4
+
1 5
 import static java.lang.Character.isLowerCase;
2 6
 import static java.lang.Character.isUpperCase;
3 7
 import static java.lang.Character.toLowerCase;
@@ -13,7 +17,7 @@ public class ROT13  {
13 17
     }
14 18
 
15 19
 
16
-    public String crypt(String text) throws UnsupportedOperationException {
20
+    public static String crypt(String text) throws UnsupportedOperationException {
17 21
         StringBuilder sb = new StringBuilder();
18 22
         char [] chars = text.toCharArray();
19 23
         for (int i = 0; i <chars.length ; i++) {
@@ -32,7 +36,7 @@ public class ROT13  {
32 36
         return sb.toString();
33 37
     }
34 38
 
35
-    public String encrypt(String text) {
39
+    public static String encrypt(String text) {
36 40
         return crypt(text);
37 41
     }
38 42
 
@@ -41,8 +45,28 @@ public class ROT13  {
41 45
     }
42 46
 
43 47
     public static String rotate(String s, Character c) {
48
+        //this method will rotate the given String
49
+        //by the character placement
50
+        String sub = s.substring(0,s.indexOf(c));
51
+        String sub1 = s.substring(s.indexOf(c));
52
+        //then concat the two substrings.
53
+        return sub1.concat(sub);
54
+    }
55
+    public static String textFile(String filename){
56
+        StringBuilder sb = new StringBuilder();
44 57
 
45
-        return "";
58
+        File file = new File(filename);
59
+        try(Scanner sc = new Scanner(file)) {
60
+        while(sc.hasNextLine()){
61
+        String line = sc.nextLine();
62
+        String xored = encrypt(line);
63
+        sb.append(xored+"\n");
64
+          }
65
+          sb.deleteCharAt(sb.length()-1);
66
+        } catch (FileNotFoundException e){
67
+            e.printStackTrace();
68
+        }
69
+        return sb.toString();
46 70
     }
47 71
 
48 72
 }

+ 27
- 1
Crypto/src/ROT13Test.java Просмотреть файл

@@ -1,5 +1,8 @@
1
+import org.junit.Assert;
1 2
 import org.junit.Test;
2 3
 
4
+import java.io.File;
5
+
3 6
 import static org.junit.Assert.*;
4 7
 
5 8
 public class ROT13Test {
@@ -91,5 +94,28 @@ public class ROT13Test {
91 94
         // Then
92 95
         assertTrue(actual.equals(Q1));
93 96
     }
94
-
97
+    @Test
98
+    public void textEncryptFile(){
99
+        //Given
100
+        File file = new File("sonnet18.txt");
101
+        //When
102
+        String expected = ROT13.encrypt("Shall I compare thee to a summer’s day?\n" +
103
+                "Thou art more lovely and more temperate:\n" +
104
+                "Rough winds do shake the darling buds of May,\n" +
105
+                "And summer’s lease hath all too short a date;\n" +
106
+                "Sometime too hot the eye of heaven shines,\n" +
107
+                "And often is his gold complexion dimm'd;\n" +
108
+                "And every fair from fair sometime declines,\n" +
109
+                "By chance or nature’s changing course untrimm'd;\n" +
110
+                "But thy eternal summer shall not fade,\n" +
111
+                "Nor lose possession of that fair thou ow’st;\n" +
112
+                "Nor shall death brag thou wander’st in his shade,\n" +
113
+                "When in eternal lines to time thou grow’st:\n" +
114
+                "So long as men can breathe or eyes can see,\n" +
115
+                "So long lives this, and this gives life to thee.");
116
+
117
+        String actual = ROT13.textFile("sonnet18.txt");
118
+        //Then
119
+        Assert.assertEquals(expected,actual);
120
+    }
95 121
 }

+ 1
- 1
SimpleCrypt.iml Просмотреть файл

@@ -1,6 +1,6 @@
1 1
 <?xml version="1.0" encoding="UTF-8"?>
2 2
 <module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
3
-  <component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_5">
3
+  <component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_7">
4 4
     <output url="file://$MODULE_DIR$/target/classes" />
5 5
     <output-test url="file://$MODULE_DIR$/target/test-classes" />
6 6
     <content url="file://$MODULE_DIR$">

+ 12
- 0
pom.xml Просмотреть файл

@@ -7,6 +7,18 @@
7 7
     <groupId>com.zipcodewilmington</groupId>
8 8
     <artifactId>SimpleCrypt</artifactId>
9 9
     <version>1.0-SNAPSHOT</version>
10
+    <build>
11
+        <plugins>
12
+            <plugin>
13
+                <groupId>org.apache.maven.plugins</groupId>
14
+                <artifactId>maven-compiler-plugin</artifactId>
15
+                <configuration>
16
+                    <source>7</source>
17
+                    <target>7</target>
18
+                </configuration>
19
+            </plugin>
20
+        </plugins>
21
+    </build>
10 22
     <dependencies>
11 23
         <dependency>
12 24
             <groupId>junit</groupId>

+ 2
- 2
sonnet18.txt Просмотреть файл

@@ -10,5 +10,5 @@ But thy eternal summer shall not fade,
10 10
 Nor lose possession of that fair thou ow’st;
11 11
 Nor shall death brag thou wander’st in his shade,
12 12
 When in eternal lines to time thou grow’st:
13
-   So long as men can breathe or eyes can see,
14
-   So long lives this, and this gives life to thee.
13
+So long as men can breathe or eyes can see,
14
+So long lives this, and this gives life to thee.