Bladeren bron

Think im finished

Nicholas Maidanos 6 jaren geleden
bovenliggende
commit
12b5d96d20
4 gewijzigde bestanden met toevoegingen van 5464 en 0 verwijderingen
  1. 12
    0
      pom.xml
  2. 22
    0
      src/main/java/HamletParser.java
  3. 47
    0
      src/test/java/HamletParserTest.java
  4. 5383
    0
      target/classes/hamlet.txt

+ 12
- 0
pom.xml Bestand weergeven

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

+ 22
- 0
src/main/java/HamletParser.java Bestand weergeven

@@ -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,24 @@ public class HamletParser {
36 38
         return hamletData;
37 39
     }
38 40
 
41
+    public int wordCounter(String stringToCount) {
42
+        Pattern p = Pattern.compile(stringToCount);
43
+        Matcher m = p.matcher(this.hamletData);
44
+        int count = 0;
45
+        while(m.find()){
46
+            count++;
47
+        }
48
+        return count;
49
+
50
+    }
51
+
52
+    public void findAndReplace(String regex, String string) {
53
+
54
+        Pattern replace = Pattern.compile(regex);
55
+
56
+        Matcher regexMatcher = replace.matcher(this.hamletData);
57
+
58
+        this.hamletData = regexMatcher.replaceAll(string);
59
+
60
+    }
39 61
 }

+ 47
- 0
src/test/java/HamletParserTest.java Bestand weergeven

@@ -1,3 +1,4 @@
1
+import org.junit.Assert;
1 2
 import org.junit.Before;
2 3
 import org.junit.Test;
3 4
 
@@ -7,25 +8,71 @@ public class HamletParserTest {
7 8
     private String hamletText;
8 9
     private HamletParser hamletParser;
9 10
 
11
+    String hamletRegex;
12
+    String horatioRegex;
13
+
10 14
     @Before
11 15
     public void setUp() {
12 16
         this.hamletParser = new HamletParser();
13 17
         this.hamletText = hamletParser.getHamletData();
18
+
19
+        this.hamletRegex = "(HAMLET|Hamlet)";
20
+        this.horatioRegex = "(HORATIO|Horatio)";
14 21
     }
15 22
 
16 23
     @Test
17 24
     public void testChangeHamletToLeon() {
25
+        //When
26
+        int hamlet = hamletParser.wordCounter(hamletRegex);
27
+        hamletParser.findAndReplace(hamletRegex, "Leon");
28
+        int leon = hamletParser.wordCounter("Leon");
29
+
30
+        //Expect
31
+        int expect = hamlet;
32
+
33
+        //Actual
34
+        int actual = leon;
35
+
36
+        Assert.assertEquals(expect,actual);
37
+        Assert.assertTrue(hamletParser.wordCounter(hamletRegex) == 0);
18 38
     }
19 39
 
20 40
     @Test
21 41
     public void testChangeHoratioToTariq() {
42
+        //When
43
+        int horatio = hamletParser.wordCounter(horatioRegex);
44
+        hamletParser.findAndReplace(horatioRegex, "Tariq");
45
+        int tariq = hamletParser.wordCounter("Tariq");
46
+
47
+        //Expect
48
+        int expect = horatio;
49
+
50
+        //Actual
51
+        int actual = tariq;
52
+
53
+        Assert.assertEquals(expect, actual);
54
+        Assert.assertTrue(hamletParser.wordCounter(horatioRegex) == 0);
22 55
     }
23 56
 
24 57
     @Test
25 58
     public void testFindHoratio() {
59
+        //Expected
60
+        int expected = 158;
61
+
62
+        //Actual
63
+        int actual = hamletParser.wordCounter(horatioRegex);
64
+
65
+        Assert.assertEquals(expected, actual);
26 66
     }
27 67
 
28 68
     @Test
29 69
     public void testFindHamlet() {
70
+        //Expected
71
+        int expected = 472;
72
+
73
+        //Actual
74
+        int actual = hamletParser.wordCounter(hamletRegex);
75
+
76
+        Assert.assertEquals(expected, actual);
30 77
     }
31 78
 }

+ 5383
- 0
target/classes/hamlet.txt
Diff onderdrukt omdat het te groot bestand
Bestand weergeven