Ver código fonte

finished commit

Joshua Chung 6 anos atrás
pai
commit
8da1cd5172
4 arquivos alterados com 5422 adições e 1 exclusões
  1. 12
    0
      pom.xml
  2. 11
    1
      src/main/java/HamletParser.java
  3. 16
    0
      src/test/java/HamletParserTest.java
  4. 5383
    0
      target/classes/hamlet.txt

+ 12
- 0
pom.xml Ver arquivo

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>

+ 11
- 1
src/main/java/HamletParser.java Ver arquivo

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.Pattern;
4
 
5
 
5
 /**
6
 /**
6
  * Created by thook on 10/7/15.
7
  * Created by thook on 10/7/15.
16
     private String loadFile(){
17
     private String loadFile(){
17
         ClassLoader classLoader = getClass().getClassLoader();
18
         ClassLoader classLoader = getClass().getClassLoader();
18
         File file = new File(classLoader.getResource("hamlet.txt").getFile());
19
         File file = new File(classLoader.getResource("hamlet.txt").getFile());
19
-        StringBuilder result = new StringBuilder("");
20
+        StringBuilder result = new StringBuilder();
20
 
21
 
21
         try(Scanner scanner = new Scanner(file)){
22
         try(Scanner scanner = new Scanner(file)){
22
             while(scanner.hasNextLine()){
23
             while(scanner.hasNextLine()){
36
         return hamletData;
37
         return hamletData;
37
     }
38
     }
38
 
39
 
40
+    public String changeHamletToLeon(String hamlet) {
41
+        hamlet = getHamletData();
42
+        return hamlet.replaceAll( "(?i:.*HAMLET*)", "Leon");
43
+    }
44
+
45
+    public String changeHoratioToTariq(String hamlet) {
46
+        hamlet = getHamletData();
47
+        return hamlet.replaceAll("(?i:.*HORATIO*)", "Tariq");
48
+    }
39
 }
49
 }

+ 16
- 0
src/test/java/HamletParserTest.java Ver arquivo

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 {
15
 
19
 
16
     @Test
20
     @Test
17
     public void testChangeHamletToLeon() {
21
     public void testChangeHamletToLeon() {
22
+        String expected = hamletParser.changeHamletToLeon(hamletText);
23
+        Assert.assertTrue(!expected.equals(hamletText));
18
     }
24
     }
19
 
25
 
20
     @Test
26
     @Test
21
     public void testChangeHoratioToTariq() {
27
     public void testChangeHoratioToTariq() {
28
+        String expected = hamletParser.changeHoratioToTariq(hamletText);
29
+        Assert.assertTrue(!expected.equals(hamletText));
22
     }
30
     }
23
 
31
 
24
     @Test
32
     @Test
25
     public void testFindHoratio() {
33
     public void testFindHoratio() {
34
+        Pattern p = Pattern.compile("(i:.*HORATIO*)");
35
+        Matcher m = p.matcher(hamletParser.changeHoratioToTariq(hamletText));
36
+        boolean b = m.matches();
37
+        Assert.assertFalse(b);
26
     }
38
     }
27
 
39
 
28
     @Test
40
     @Test
29
     public void testFindHamlet() {
41
     public void testFindHamlet() {
42
+        Pattern p = Pattern.compile("(i:.*HAMLET*)");
43
+        Matcher m = p.matcher(hamletParser.changeHamletToLeon(hamletText));
44
+        boolean b = m.matches();
45
+        Assert.assertFalse(b);
30
     }
46
     }
31
 }
47
 }

+ 5383
- 0
target/classes/hamlet.txt
Diferenças do arquivo suprimidas por serem muito extensas
Ver arquivo