Explorar el Código

Hamlet complete

Tennessee Gibbs hace 6 años
padre
commit
cf36317dd9
Se han modificado 4 ficheros con 5490 adiciones y 1 borrados
  1. 12
    0
      pom.xml
  2. 46
    0
      src/main/java/HamletParser.java
  3. 49
    1
      src/test/java/HamletParserTest.java
  4. 5383
    0
      target/classes/hamlet.txt

+ 12
- 0
pom.xml Ver fichero

7
     <groupId>com.zipcodewilmington</groupId>
7
     <groupId>com.zipcodewilmington</groupId>
8
     <artifactId>regex</artifactId>
8
     <artifactId>regex</artifactId>
9
     <version>1.0-SNAPSHOT</version>
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
     <dependencies>
23
     <dependencies>
12
         <dependency>
24
         <dependency>

+ 46
- 0
src/main/java/HamletParser.java Ver fichero

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.
36
         return hamletData;
38
         return hamletData;
37
     }
39
     }
38
 
40
 
41
+    public int findHamlet() {
42
+        Pattern pattern = Pattern.compile("(?i)Hamlet");
43
+        Matcher matcher = pattern.matcher(hamletData);
44
+        int count = 0;
45
+
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
+
59
+        while (matcher.find()){
60
+            count++;}
61
+
62
+        return count;
63
+    }
64
+
65
+    public String changeHamletToLeon() {
66
+        String replaceWith;
67
+        String inputString;
68
+        {
69
+            Pattern pattern = Pattern.compile(hamletData);
70
+            Matcher matcher = pattern.matcher("Leon");
71
+            return matcher.replaceAll("Hamlet");
72
+        }
73
+
74
+    }
75
+
76
+    public String changeHoratioToTariq() {
77
+        String replaceWith;
78
+        String inputString;
79
+        {
80
+            Pattern pattern = Pattern.compile(hamletData);
81
+            Matcher matcher = pattern.matcher("Tariq");
82
+            return matcher.replaceAll("Horatio");
83
+        }
84
+    }
39
 }
85
 }

+ 49
- 1
src/test/java/HamletParserTest.java Ver fichero

1
 import org.junit.Before;
1
 import org.junit.Before;
2
 import org.junit.Test;
2
 import org.junit.Test;
3
 
3
 
4
+import java.util.regex.Matcher;
5
+import java.util.regex.Pattern;
6
+
4
 import static org.junit.Assert.*;
7
 import static org.junit.Assert.*;
5
 
8
 
6
 public class HamletParserTest {
9
 public class HamletParserTest {
15
 
18
 
16
     @Test
19
     @Test
17
     public void testChangeHamletToLeon() {
20
     public void testChangeHamletToLeon() {
21
+        //Given
22
+        HamletParser hamletParser = new HamletParser();
23
+        String hamletText;
24
+        //When
25
+        String expected = "Leon";
26
+        String actual = hamletParser.changeHamletToLeon();
27
+
28
+        //Then
29
+        assertEquals( expected, actual);
30
+
18
     }
31
     }
19
 
32
 
33
+
34
+
35
+
36
+
37
+
20
     @Test
38
     @Test
21
     public void testChangeHoratioToTariq() {
39
     public void testChangeHoratioToTariq() {
40
+        //Given
41
+        HamletParser hamletParser = new HamletParser();
42
+        String hamletText;
43
+        //When
44
+        String expected = "Tariq";
45
+        String actual = hamletParser.changeHoratioToTariq();
46
+
47
+        //Then
48
+        assertEquals( expected, actual);
49
+
22
     }
50
     }
23
 
51
 
24
     @Test
52
     @Test
25
     public void testFindHoratio() {
53
     public void testFindHoratio() {
26
-    }
54
+            //Given
55
+            HamletParser hamletParser = new HamletParser();
56
+            String hamletText;
57
+            //When
58
+            int expected = 158;
59
+            int actual = hamletParser.findHoratio();
60
+
61
+            //Then
62
+            assertEquals( expected, actual, 0 );
63
+        }
64
+
65
+
27
 
66
 
28
     @Test
67
     @Test
29
     public void testFindHamlet() {
68
     public void testFindHamlet() {
69
+        //Given
70
+        HamletParser hamletParser = new HamletParser();
71
+        String hamletText;
72
+        //When
73
+        int expected = 472;
74
+        int actual = hamletParser.findHamlet();
75
+
76
+        //Then
77
+        assertEquals( expected, actual, 0 );
30
     }
78
     }
31
 }
79
 }

+ 5383
- 0
target/classes/hamlet.txt
La diferencia del archivo ha sido suprimido porque es demasiado grande
Ver fichero