Browse Source

Finished the Lab

CWinarski 6 years ago
parent
commit
b1216a7aca
4 changed files with 5434 additions and 16 deletions
  1. 12
    0
      pom.xml
  2. 30
    7
      src/main/java/HamletParser.java
  3. 9
    9
      src/test/java/HamletParserTest.java
  4. 5383
    0
      target/classes/hamlet.txt

+ 12
- 0
pom.xml View File

@@ -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>

+ 30
- 7
src/main/java/HamletParser.java View File

@@ -1,39 +1,62 @@
1
+
1 2
 import java.io.File;
2 3
 import java.io.IOException;
3 4
 import java.util.Scanner;
5
+import java.util.regex.Matcher;
6
+import java.util.regex.Pattern;
4 7
 
5 8
 /**
6 9
  * Created by thook on 10/7/15.
7 10
  */
8 11
 public class HamletParser {
9 12
 
10
-    private String hamletData;
13
+    String hamletData;
11 14
 
12
-    public HamletParser(){
15
+    public HamletParser() {
13 16
         this.hamletData = loadFile();
14 17
     }
15 18
 
16
-    private String loadFile(){
19
+    String hamletPattern = "[Hh][Aa][Mm][Ll][Ee][Tt]";
20
+    Pattern hamletC = Pattern.compile(hamletPattern);
21
+
22
+
23
+    String horatioPattern = "[Hh][Oo][Rr][Aa][Tt][Ii][Oo]";
24
+    Pattern horatioC = Pattern.compile(horatioPattern);
25
+
26
+
27
+    private String loadFile() {
17 28
         ClassLoader classLoader = getClass().getClassLoader();
18 29
         File file = new File(classLoader.getResource("hamlet.txt").getFile());
19 30
         StringBuilder result = new StringBuilder("");
20 31
 
21
-        try(Scanner scanner = new Scanner(file)){
22
-            while(scanner.hasNextLine()){
32
+        try (Scanner scanner = new Scanner(file)) {
33
+            while (scanner.hasNextLine()) {
23 34
                 String line = scanner.nextLine();
24 35
                 result.append(line).append("\n");
25 36
             }
26 37
 
27 38
             scanner.close();
28
-        }catch(IOException e){
39
+        } catch (IOException e) {
29 40
             e.printStackTrace();
30 41
         }
31 42
 
32 43
         return result.toString();
33 44
     }
34 45
 
35
-    public String getHamletData(){
46
+
47
+    public String getHamletData() {
36 48
         return hamletData;
37 49
     }
38 50
 
51
+    public String changeHamletToLeon(String input) {
52
+        Matcher hamletMatcher = hamletC.matcher(input);
53
+        return hamletMatcher.replaceAll("Leon");
54
+    }
55
+
56
+    public String changeHoratioToTariq(String input) {
57
+        Matcher horatioMatcher = horatioC.matcher(input);
58
+        return horatioMatcher.replaceAll("Tariq");
59
+    }
60
+
61
+
39 62
 }

+ 9
- 9
src/test/java/HamletParserTest.java View File

@@ -1,7 +1,7 @@
1
+import org.junit.Assert;
1 2
 import org.junit.Before;
2 3
 import org.junit.Test;
3 4
 
4
-import static org.junit.Assert.*;
5 5
 
6 6
 public class HamletParserTest {
7 7
     private String hamletText;
@@ -15,17 +15,17 @@ public class HamletParserTest {
15 15
 
16 16
     @Test
17 17
     public void testChangeHamletToLeon() {
18
+        String input  = "Hamlet, HAMLET, HAMLET, hamlet, hamlet!";
19
+        String expected = "Leon, Leon, Leon, Leon, Leon!";
20
+        String actual = hamletParser.changeHamletToLeon(input);
21
+        Assert.assertEquals(expected, actual);
18 22
     }
19 23
 
20 24
     @Test
21 25
     public void testChangeHoratioToTariq() {
22
-    }
23
-
24
-    @Test
25
-    public void testFindHoratio() {
26
-    }
27
-
28
-    @Test
29
-    public void testFindHamlet() {
26
+        String input = "HORATIO, HORATIO, Horatio, horatio!";
27
+        String expected = "Tariq, Tariq, Tariq, Tariq!";
28
+        String actual = hamletParser.changeHoratioToTariq(input);
29
+        Assert.assertEquals(expected, actual);
30 30
     }
31 31
 }

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