Simran Bhutani 6 jaren geleden
bovenliggende
commit
1f7d8dc91c
4 gewijzigde bestanden met toevoegingen van 5450 en 0 verwijderingen
  1. 12
    0
      pom.xml
  2. 39
    0
      src/main/java/HamletParser.java
  3. 16
    0
      src/test/java/HamletParserTest.java
  4. 5383
    0
      target/classes/hamlet.txt

+ 12
- 0
pom.xml Bestand weergeven

@@ -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>7</source>
17
+                    <target>7</target>
18
+                </configuration>
19
+            </plugin>
20
+        </plugins>
21
+    </build>
10 22
 
11 23
     <dependencies>
12 24
         <dependency>

+ 39
- 0
src/main/java/HamletParser.java Bestand weergeven

@@ -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,41 @@ public class HamletParser {
36 38
         return hamletData;
37 39
     }
38 40
 
41
+    public String changeHamletToLeon(){
42
+
43
+        Pattern pattern = Pattern.compile(".*Hamlet.*");
44
+        Matcher matcher = pattern.matcher(hamletData);
45
+        return matcher.replaceAll("Leon");
46
+
47
+    }
48
+
49
+
50
+    public String changeHoratioToTariq(){
51
+
52
+        Pattern pattern = Pattern.compile("Horatio");
53
+        Matcher matcher = pattern.matcher(hamletData);
54
+        return matcher.replaceAll("Tariq");
55
+    }
56
+
57
+    public boolean findHoratio(){
58
+//        String ham= hamletData;
59
+//        String regex = ".*Horatio.*";
60
+//        boolean matcher= Pattern.matches(regex,ham);
61
+//        System.out.println(matcher);
62
+//        return matcher;
63
+//     cannot use this method because matches expects char sequence but the given input is a text file.
64
+
65
+        Pattern pattern = Pattern.compile(".*Horatio.*");
66
+        Matcher matcher = pattern.matcher(hamletData);
67
+        return matcher.find();
68
+    }
69
+
70
+
71
+    public boolean findHamlet(){
72
+
73
+        Pattern pattern = Pattern.compile("Hamlet");
74
+        Matcher matcher = pattern.matcher(hamletData);
75
+        return matcher.find();
76
+    }
77
+
39 78
 }

+ 16
- 0
src/test/java/HamletParserTest.java Bestand weergeven

@@ -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,32 @@ public class HamletParserTest {
15 16
 
16 17
     @Test
17 18
     public void testChangeHamletToLeon() {
19
+        boolean expected = false;
20
+        String changedText = hamletParser.changeHamletToLeon();
21
+        boolean actual =changedText.contains("Hamlet");
22
+        Assert.assertEquals(expected,actual);
23
+
18 24
     }
19 25
 
20 26
     @Test
21 27
     public void testChangeHoratioToTariq() {
28
+        boolean expected = false;
29
+        String changedText = hamletParser.changeHoratioToTariq();
30
+        boolean actual =changedText.contains("Horatio");
31
+        Assert.assertEquals(expected,actual);
22 32
     }
23 33
 
24 34
     @Test
25 35
     public void testFindHoratio() {
36
+        boolean expected = true;
37
+        boolean actual= hamletParser.findHoratio();
38
+        Assert.assertEquals(expected,actual);
26 39
     }
27 40
 
28 41
     @Test
29 42
     public void testFindHamlet() {
43
+        boolean expected = true;
44
+        boolean actual = hamletParser.findHamlet();
45
+        Assert.assertEquals(expected,actual);
30 46
     }
31 47
 }

+ 5383
- 0
target/classes/hamlet.txt
Diff onderdrukt omdat het te groot bestand
Bestand weergeven