Lewis Dominguez 6 years ago
parent
commit
f381bb0c8c
3 changed files with 5450 additions and 0 deletions
  1. 37
    0
      src/main/java/HamletParser.java
  2. 30
    0
      src/test/java/HamletParserTest.java
  3. 5383
    0
      target/classes/hamlet.txt

+ 37
- 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.
@@ -36,4 +38,39 @@ public class HamletParser {
36 38
         return hamletData;
37 39
     }
38 40
 
41
+
42
+    public int findHamlet() {
43
+        Pattern pattern = Pattern.compile("(?i)Hamlet");
44
+        Matcher matcher = pattern.matcher(hamletData);
45
+        int count = 0;
46
+
47
+        while (matcher.find()) {
48
+            count++;
49
+        }
50
+        return count;
51
+    }
52
+
53
+    public int findHoratio() {
54
+        Pattern pattern = Pattern.compile("(?i)Horatio");
55
+        Matcher matcher = pattern.matcher(hamletData);
56
+        int count = 0;
57
+
58
+        while (matcher.find()){
59
+            count++;
60
+        }
61
+        return count;
62
+    }
63
+
64
+    public String changeHamletToLeon() {
65
+
66
+        Pattern pattern = Pattern.compile(hamletData);
67
+        Matcher matcher = pattern.matcher("Leon");
68
+        return matcher.replaceAll("Hamlet");
69
+    }
70
+
71
+    public String changeHoratioToTariq() {
72
+        Pattern pattern = Pattern.compile(hamletData);
73
+        Matcher matcher = pattern.matcher("Tariq");
74
+        return matcher.replaceAll("Horatio");
75
+    }
39 76
 }

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

@@ -15,17 +15,47 @@ public class HamletParserTest {
15 15
 
16 16
     @Test
17 17
     public void testChangeHamletToLeon() {
18
+        //Given
19
+        HamletParser hp = new HamletParser();
20
+        //When
21
+        String expected = "Leon";
22
+        String actual = hp.changeHamletToLeon();
23
+        //Then
24
+        assertEquals(expected, actual);
18 25
     }
19 26
 
20 27
     @Test
21 28
     public void testChangeHoratioToTariq() {
29
+        //Given
30
+        HamletParser hp = new HamletParser();
31
+        //When
32
+        String expected = "Tariq";
33
+        String actual = hp.changeHoratioToTariq();
34
+        //Then
35
+        assertEquals(expected, actual);
22 36
     }
23 37
 
24 38
     @Test
25 39
     public void testFindHoratio() {
40
+        //Given
41
+        HamletParser hp = new HamletParser();
42
+        //When
43
+        int expected = 158;
44
+        int actual = hp.findHoratio();
45
+        //Then
46
+        assertEquals(expected, actual, 0);
26 47
     }
27 48
 
28 49
     @Test
29 50
     public void testFindHamlet() {
51
+        //Given
52
+        HamletParser hp = new HamletParser();
53
+        //When
54
+        int expected = 472;
55
+        int actual = hp.findHamlet();
56
+        //Then
57
+        assertEquals(expected, actual, 0);
30 58
     }
59
+
60
+
31 61
 }

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