Kate Moore 6 years ago
parent
commit
dde2c857f0
3 changed files with 5434 additions and 2 deletions
  1. 31
    0
      src/main/java/HamletParser.java
  2. 20
    2
      src/test/java/HamletParserTest.java
  3. 5383
    0
      target/classes/hamlet.txt

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

1
 import java.io.File;
1
 import java.io.File;
2
 import java.io.IOException;
2
 import java.io.IOException;
3
 import java.util.Scanner;
3
 import java.util.Scanner;
4
+import java.util.regex.Matcher;
5
+import java.util.regex.Pattern;
4
 
6
 
5
 /**
7
 /**
6
  * Created by thook on 10/7/15.
8
  * Created by thook on 10/7/15.
8
 public class HamletParser {
10
 public class HamletParser {
9
 
11
 
10
     private String hamletData;
12
     private String hamletData;
13
+    private Pattern p;
14
+    private Matcher m;
11
 
15
 
12
     public HamletParser(){
16
     public HamletParser(){
13
         this.hamletData = loadFile();
17
         this.hamletData = loadFile();
36
         return hamletData;
40
         return hamletData;
37
     }
41
     }
38
 
42
 
43
+
44
+    public void changeHamletToLeon(){
45
+        p = Pattern.compile("hamlet", Pattern.CASE_INSENSITIVE);
46
+        m = p.matcher(hamletData);
47
+        hamletData = m.replaceAll("Leon");
48
+    }
49
+
50
+    public void changeHoratioToTariq(){
51
+        p = Pattern.compile("horatio", Pattern.CASE_INSENSITIVE);
52
+        m = p.matcher(hamletData);
53
+        hamletData = m.replaceAll("Tariq");
54
+    }
55
+
56
+    public boolean findHamlet(){
57
+        p = Pattern.compile("hamlet", Pattern.CASE_INSENSITIVE);
58
+        m = p.matcher(hamletData);
59
+        return m.find();
60
+    }
61
+
62
+    public boolean findHoratio(){
63
+        p = Pattern.compile("horatio", Pattern.CASE_INSENSITIVE);
64
+        m = p.matcher(hamletData);
65
+        return m.find();
66
+
67
+    }
68
+
69
+
39
 }
70
 }

+ 20
- 2
src/test/java/HamletParserTest.java View File

1
+import org.junit.Assert;
1
 import org.junit.Before;
2
 import org.junit.Before;
2
 import org.junit.Test;
3
 import org.junit.Test;
3
 
4
 
5
+import java.util.regex.Matcher;
6
+import java.util.regex.Pattern;
7
+
4
 import static org.junit.Assert.*;
8
 import static org.junit.Assert.*;
5
 
9
 
6
 public class HamletParserTest {
10
 public class HamletParserTest {
7
-    private String hamletText;
11
+
8
     private HamletParser hamletParser;
12
     private HamletParser hamletParser;
9
 
13
 
10
     @Before
14
     @Before
11
     public void setUp() {
15
     public void setUp() {
12
         this.hamletParser = new HamletParser();
16
         this.hamletParser = new HamletParser();
13
-        this.hamletText = hamletParser.getHamletData();
14
     }
17
     }
15
 
18
 
16
     @Test
19
     @Test
17
     public void testChangeHamletToLeon() {
20
     public void testChangeHamletToLeon() {
21
+       hamletParser.changeHamletToLeon();
22
+       boolean expected = false;
23
+       boolean actual = hamletParser.findHamlet();
24
+       Assert.assertEquals(expected, actual);
25
+
18
     }
26
     }
19
 
27
 
20
     @Test
28
     @Test
21
     public void testChangeHoratioToTariq() {
29
     public void testChangeHoratioToTariq() {
30
+        hamletParser.changeHoratioToTariq();
31
+        boolean expected = false;
32
+        boolean actual = hamletParser.findHoratio();
33
+        Assert.assertEquals(expected, actual);
22
     }
34
     }
23
 
35
 
24
     @Test
36
     @Test
25
     public void testFindHoratio() {
37
     public void testFindHoratio() {
38
+        boolean expected = hamletParser.getHamletData().toLowerCase().contains("horatio");
39
+        boolean actual = hamletParser.findHoratio();
40
+        Assert.assertEquals(expected, actual);
26
     }
41
     }
27
 
42
 
28
     @Test
43
     @Test
29
     public void testFindHamlet() {
44
     public void testFindHamlet() {
45
+        boolean expected = hamletParser.getHamletData().toLowerCase().contains("hamlet");
46
+        boolean actual = hamletParser.findHamlet();
47
+        Assert.assertEquals(expected, actual);
30
     }
48
     }
31
 }
49
 }

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