Bläddra i källkod

Completed the lab.

William Brown 6 år sedan
förälder
incheckning
3a3cb650fc
4 ändrade filer med 5426 tillägg och 1 borttagningar
  1. 13
    0
      pom.xml
  2. 15
    1
      src/main/java/HamletParser.java
  3. 15
    0
      src/test/java/HamletParserTest.java
  4. 5383
    0
      target/classes/hamlet.txt

+ 13
- 0
pom.xml Visa fil

@@ -16,4 +16,17 @@
16 16
         </dependency>
17 17
     </dependencies>
18 18
 
19
+    <build>
20
+        <plugins>
21
+            <plugin>
22
+                <groupId>org.apache.maven.plugins</groupId>
23
+                <artifactId>maven-compiler-plugin</artifactId>
24
+                <configuration>
25
+                    <source>1.8</source>
26
+                    <target>1.8</target>
27
+                </configuration>
28
+            </plugin>
29
+        </plugins>
30
+    </build>
31
+
19 32
 </project>

+ 15
- 1
src/main/java/HamletParser.java Visa fil

@@ -1,6 +1,8 @@
1 1
 import java.io.File;
2 2
 import java.io.IOException;
3 3
 import java.util.Scanner;
4
+import java.util.regex.Matcher;
5
+import java.util.regex.Pattern;
4 6
 
5 7
 /**
6 8
  * Created by thook on 10/7/15.
@@ -36,4 +38,16 @@ public class HamletParser {
36 38
         return hamletData;
37 39
     }
38 40
 
39
-}
41
+    public void changeToName(String nameToReplace, String name){
42
+        Pattern pattern = Pattern.compile(nameToReplace);
43
+        Matcher matcher = pattern.matcher(hamletData);
44
+        this.hamletData = matcher.replaceAll(name);
45
+    }
46
+
47
+    public boolean findName(String name){
48
+        Pattern pattern = Pattern.compile(name);
49
+        Matcher matcher = pattern.matcher(hamletData);
50
+        return matcher.find();
51
+    }
52
+
53
+}

+ 15
- 0
src/test/java/HamletParserTest.java Visa fil

@@ -1,3 +1,4 @@
1
+import org.junit.Assert;
1 2
 import org.junit.Before;
2 3
 import org.junit.Test;
3 4
 
@@ -15,17 +16,31 @@ public class HamletParserTest {
15 16
 
16 17
     @Test
17 18
     public void testChangeHamletToLeon() {
19
+        HamletParser hamletParser = new HamletParser();
20
+        hamletParser.changeToName("Hamlet","Leon");
21
+
22
+        Assert.assertTrue(hamletParser.findName("Leon"));
18 23
     }
19 24
 
20 25
     @Test
21 26
     public void testChangeHoratioToTariq() {
27
+        HamletParser hamletParser = new HamletParser();
28
+        hamletParser.changeToName("Horatio","Tariq");
29
+
30
+        Assert.assertTrue(hamletParser.findName("Tariq"));
22 31
     }
23 32
 
24 33
     @Test
25 34
     public void testFindHoratio() {
35
+        HamletParser hamletParser = new HamletParser();
36
+
37
+        Assert.assertTrue(hamletParser.findName("Horatio"));
26 38
     }
27 39
 
28 40
     @Test
29 41
     public void testFindHamlet() {
42
+        HamletParser hamletParser = new HamletParser();
43
+
44
+        Assert.assertTrue(hamletParser.findName("Hamlet"));
30 45
     }
31 46
 }

+ 5383
- 0
target/classes/hamlet.txt
Filskillnaden har hållits tillbaka eftersom den är för stor
Visa fil