Browse Source

lab complete

mbowe4 6 years ago
parent
commit
fcc703238f
4 changed files with 5426 additions and 0 deletions
  1. 12
    0
      pom.xml
  2. 9
    0
      src/main/java/HamletParser.java
  3. 22
    0
      src/test/java/HamletParserTest.java
  4. 5383
    0
      target/classes/hamlet.txt

+ 12
- 0
pom.xml View File

@@ -7,6 +7,18 @@
7 7
     <groupId>com.zipcodewilmington</groupId>
8 8
     <artifactId>regex</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>1.7</source>
17
+                    <target>1.7</target>
18
+                </configuration>
19
+            </plugin>
20
+        </plugins>
21
+    </build>
10 22
 
11 23
     <dependencies>
12 24
         <dependency>

+ 9
- 0
src/main/java/HamletParser.java View File

@@ -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.
@@ -9,6 +11,7 @@ public class HamletParser {
9 11
 
10 12
     private String hamletData;
11 13
 
14
+
12 15
     public HamletParser(){
13 16
         this.hamletData = loadFile();
14 17
     }
@@ -36,4 +39,10 @@ public class HamletParser {
36 39
         return hamletData;
37 40
     }
38 41
 
42
+    public String changeName(String pattern, String replacement, String input) {
43
+        Pattern hamletPattern = Pattern.compile(pattern);
44
+        Matcher matcher = hamletPattern.matcher(input);
45
+
46
+        return matcher.replaceAll(replacement);
47
+    }
39 48
 }

+ 22
- 0
src/test/java/HamletParserTest.java View File

@@ -1,6 +1,10 @@
1
+import org.junit.Assert;
1 2
 import org.junit.Before;
2 3
 import org.junit.Test;
3 4
 
5
+import java.util.regex.Matcher;
6
+import java.util.regex.Pattern;
7
+
4 8
 import static org.junit.Assert.*;
5 9
 
6 10
 public class HamletParserTest {
@@ -15,17 +19,35 @@ public class HamletParserTest {
15 19
 
16 20
     @Test
17 21
     public void testChangeHamletToLeon() {
22
+        String expected = hamletParser.changeName("(h|H)amlet", "Leon", this.hamletText);
23
+        Assert.assertFalse(expected.contains("(h|H)amlet"));
18 24
     }
19 25
 
20 26
     @Test
21 27
     public void testChangeHoratioToTariq() {
28
+        String expected = hamletParser.changeName("(h|H)oratio", "Tariq", this.hamletText);
29
+        Assert.assertFalse(expected.contains("(h|H)oratio"));
22 30
     }
23 31
 
24 32
     @Test
25 33
     public void testFindHoratio() {
34
+        String testHoratioStr = "My name is Horatio";
35
+        String horatioPattern = ("(h|H)oratio");
36
+
37
+        String expected = "My name is Tariq";
38
+        String actual = hamletParser.changeName(horatioPattern, "Tariq", testHoratioStr);
39
+
40
+        Assert.assertEquals(expected, actual);
26 41
     }
27 42
 
28 43
     @Test
29 44
     public void testFindHamlet() {
45
+        String testHoratioStr = "My name is Hamlet, hamlet";
46
+        String hamletPattern = ("(h|H)amlet");
47
+
48
+        String expected = "My name is Leon, Leon";
49
+        String actual = hamletParser.changeName(hamletPattern, "Leon", testHoratioStr);
50
+
51
+        Assert.assertEquals(expected, actual);
30 52
     }
31 53
 }

+ 5383
- 0
target/classes/hamlet.txt
File diff suppressed because it is too large
View File