Ver código fonte

Merge aa855707e51f490f9cad93f2eb429239fbdaf6df into 99c0c83b2f011a8e58c86fca53df6bfa67c86565

Luis J. Romero 6 anos atrás
pai
commit
7d5efca32a
Nenhuma conta conectada ao e-mail do autor de commit

+ 12
- 0
pom.xml Ver arquivo

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

+ 65
- 2
src/main/java/HamletParser.java Ver arquivo

@@ -1,6 +1,9 @@
1 1
 import java.io.File;
2 2
 import java.io.IOException;
3
+import java.util.Arrays;
3 4
 import java.util.Scanner;
5
+import java.util.regex.Pattern;
6
+import java.util.regex.Matcher;
4 7
 
5 8
 /**
6 9
  * Created by thook on 10/7/15.
@@ -8,11 +11,18 @@ import java.util.Scanner;
8 11
 public class HamletParser {
9 12
 
10 13
     private String hamletData;
14
+    private String[] words;
11 15
 
12 16
     public HamletParser(){
13 17
         this.hamletData = loadFile();
14 18
     }
15 19
 
20
+    // My constructor
21
+    public HamletParser(String input) {
22
+        this.hamletData = input;
23
+        this.words = input.split("\\W");
24
+    }
25
+
16 26
     private String loadFile(){
17 27
         ClassLoader classLoader = getClass().getClassLoader();
18 28
         File file = new File(classLoader.getResource("hamlet.txt").getFile());
@@ -23,9 +33,8 @@ public class HamletParser {
23 33
                 String line = scanner.nextLine();
24 34
                 result.append(line).append("\n");
25 35
             }
26
-
27 36
             scanner.close();
28
-        }catch(IOException e){
37
+        } catch(IOException e) {
29 38
             e.printStackTrace();
30 39
         }
31 40
 
@@ -36,4 +45,58 @@ public class HamletParser {
36 45
         return hamletData;
37 46
     }
38 47
 
48
+    public void changeHamletToLeon() {
49
+        String HamletString = "Hamlet";
50
+        String LeonString = "Leon";
51
+
52
+        Pattern HamletPattern = Pattern.compile(HamletString);
53
+        Matcher HamletMatcher = HamletPattern.matcher(hamletData);
54
+        hamletData = HamletMatcher.replaceAll(LeonString);
55
+    }
56
+
57
+    public void changeHoratioToTariq() {
58
+        String HoratioString = "Horatio";
59
+        String TariqString = "Tariq";
60
+
61
+        Pattern HoratioPattern = Pattern.compile(HoratioString);
62
+        Matcher HoratioMatcher = HoratioPattern.matcher(hamletData);
63
+        hamletData = HoratioMatcher.replaceAll(TariqString);
64
+    }
65
+
66
+    public boolean findHoratio() {
67
+        boolean findHoratio = false;
68
+
69
+        String HoratioString = "Horatio";
70
+        Pattern HoratioPattern = Pattern.compile(HoratioString);
71
+        Matcher HoratioMatcher = HoratioPattern.matcher(hamletData);
72
+
73
+        if (HoratioMatcher.find()) {
74
+            findHoratio = true;
75
+        }
76
+
77
+        return findHoratio;
78
+    }
79
+
80
+    public boolean findHamlet() {
81
+        boolean findHamlet = false;
82
+
83
+        String HamletString = "Horatio";
84
+        Pattern HamletPattern = Pattern.compile(HamletString);
85
+        Matcher HamletMatcher = HamletPattern.matcher(hamletData);
86
+
87
+        if (HamletMatcher.find()) {
88
+            findHamlet = true;
89
+        }
90
+
91
+        return findHamlet;
92
+    }
93
+
94
+    public static void main(String[] args) {
95
+        String inputString = "Change hamleta;ldsfjkhamletHamlet and Hamlet and also Hamlet to Leon";
96
+        HamletParser hamletParser = new HamletParser(inputString);
97
+        System.out.println(hamletParser.getHamletData());
98
+        hamletParser.changeHamletToLeon();
99
+        System.out.println(hamletParser.getHamletData());
100
+    }
101
+
39 102
 }

+ 5383
- 0
src/main/resources/hamletShort.txt
Diferenças do arquivo suprimidas por serem muito extensas
Ver arquivo


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

@@ -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,49 @@ public class HamletParserTest {
15 16
 
16 17
     @Test
17 18
     public void testChangeHamletToLeon() {
19
+        // Given
20
+        String inputString = "Change Hamlet to Leon";
21
+        String expectedString = "Change Leon to Leon";
22
+        hamletParser = new HamletParser(inputString);
23
+        // When
24
+        hamletParser.changeHamletToLeon();
25
+        String actualString = hamletParser.getHamletData();
26
+        // Then
27
+        Assert.assertEquals(expectedString, actualString);
18 28
     }
19 29
 
20 30
     @Test
21 31
     public void testChangeHoratioToTariq() {
32
+        // Given
33
+        String inputString = "Change Horatio to Tariq";
34
+        String expectedString = "Change Tariq to Tariq";
35
+        hamletParser = new HamletParser(inputString);
36
+        // When
37
+        hamletParser.changeHoratioToTariq();
38
+        String actualString = hamletParser.getHamletData();
39
+        // Then
40
+        Assert.assertEquals(expectedString, actualString);
22 41
     }
23 42
 
24 43
     @Test
25 44
     public void testFindHoratio() {
45
+        // Given
46
+        boolean expectedFindHoratio = true;
47
+        // When
48
+        hamletParser = new HamletParser();
49
+        boolean actualFindHoratio = hamletParser.findHoratio();
50
+        // Then
51
+        Assert.assertEquals(expectedFindHoratio, actualFindHoratio);
26 52
     }
27 53
 
28 54
     @Test
29 55
     public void testFindHamlet() {
56
+        // Given
57
+        boolean expectedFindHamlet = true;
58
+        // When
59
+        hamletParser = new HamletParser();
60
+        boolean actualFindHamlet = hamletParser.findHamlet();
61
+        // Then
62
+        Assert.assertEquals(expectedFindHamlet, actualFindHamlet);
30 63
     }
31 64
 }

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


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