Browse Source

Completed Lab

Garrett Arant 6 years ago
parent
commit
c9da8f7b93
4 changed files with 5503 additions and 6 deletions
  1. 12
    0
      pom.xml
  2. 60
    6
      src/main/java/HamletParser.java
  3. 48
    0
      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>

+ 60
- 6
src/main/java/HamletParser.java View File

@@ -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.
@@ -9,31 +11,83 @@ public class HamletParser {
9 11
 
10 12
     private String hamletData;
11 13
 
12
-    public HamletParser(){
14
+    public HamletParser() {
13 15
         this.hamletData = loadFile();
14 16
     }
15 17
 
16
-    private String loadFile(){
18
+    private String loadFile() {
17 19
         ClassLoader classLoader = getClass().getClassLoader();
18 20
         File file = new File(classLoader.getResource("hamlet.txt").getFile());
19 21
         StringBuilder result = new StringBuilder("");
20 22
 
21
-        try(Scanner scanner = new Scanner(file)){
22
-            while(scanner.hasNextLine()){
23
+        try (Scanner scanner = new Scanner(file)) {
24
+            while (scanner.hasNextLine()) {
23 25
                 String line = scanner.nextLine();
24 26
                 result.append(line).append("\n");
25 27
             }
26 28
 
27 29
             scanner.close();
28
-        }catch(IOException e){
30
+        } catch (IOException e) {
29 31
             e.printStackTrace();
30 32
         }
31 33
 
32 34
         return result.toString();
33 35
     }
34 36
 
35
-    public String getHamletData(){
37
+    public String getHamletData() {
36 38
         return hamletData;
37 39
     }
38 40
 
41
+    public String changeHamletToLeon(String hamletData) {
42
+        String input = hamletData;
43
+        Pattern p = Pattern.compile("Hamlet");
44
+        Matcher m = p.matcher(input);
45
+        String hamletToLeon = m.replaceAll("Leon");
46
+        Pattern upperP = Pattern.compile("HAMLET");
47
+        Matcher upperM = upperP.matcher(hamletToLeon);
48
+        String hamletToLeonUpper = upperM.replaceAll("LEON");
49
+
50
+        return hamletToLeonUpper.toString();
51
+    }
52
+
53
+    public String changeHoratioToTariq(String hamletData) {
54
+        String input = hamletData;
55
+        Pattern p = Pattern.compile("Horatio");
56
+        Matcher m = p.matcher(input);
57
+        String horatioToTariq = m.replaceAll("Tariq");
58
+        Pattern upperP = Pattern.compile("HORATIO");
59
+        Matcher upperM = upperP.matcher(horatioToTariq);
60
+        String horatioToTariqUpper = upperM.replaceAll("TARIQ");
61
+
62
+        return horatioToTariqUpper.toString();
63
+    }
64
+
65
+    public String changeAll(String hamletData) {
66
+        String changeAll = changeHoratioToTariq(changeHamletToLeon(hamletData));
67
+
68
+
69
+        return changeAll;
70
+    }
71
+
72
+
73
+    public Integer findHalmet(String hamletData) {
74
+        String input = hamletData;
75
+        Pattern p = Pattern.compile("Hamlet", Pattern.CASE_INSENSITIVE);
76
+        Matcher m = p.matcher(input);
77
+        int count = 0;
78
+        while (m.find())
79
+            count++;
80
+        return count;
81
+    }
82
+
83
+    public Integer findHoratio(String hamletData) {
84
+        String input = hamletData;
85
+        Pattern p = Pattern.compile("Horatio", Pattern.CASE_INSENSITIVE);
86
+        Matcher m = p.matcher(input);
87
+        int count = 0;
88
+        while (m.find())
89
+            count++;
90
+        return count;
91
+    }
92
+
39 93
 }

+ 48
- 0
src/test/java/HamletParserTest.java View File

@@ -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,64 @@ public class HamletParserTest {
15 16
 
16 17
     @Test
17 18
     public void testChangeHamletToLeon() {
19
+        //Given
20
+        HamletParser testHamletParser = new HamletParser();
21
+        String testString = "Hamlet. HAMLET. Hamlet!";
22
+        //When
23
+        String expected = "Leon. LEON. Leon!";
24
+        String actual = testHamletParser.changeHamletToLeon(testString);
25
+        //Then
26
+        Assert.assertEquals(expected, actual);
27
+
18 28
     }
19 29
 
20 30
     @Test
21 31
     public void testChangeHoratioToTariq() {
32
+        //Given
33
+        HamletParser testHamletParser = new HamletParser();
34
+        String testString = "Horatio. HORATIO. Horatio!";
35
+        //When
36
+        String expected = "Tariq. TARIQ. Tariq!";
37
+        String actual = testHamletParser.changeAll(testString);
38
+        //Then
39
+        Assert.assertEquals(expected, actual);
40
+    }
41
+
42
+    @Test
43
+    public void testChangeAll() {
44
+        //Given
45
+        HamletParser testHamletParser = new HamletParser();
46
+        String testString = "Horatio. HORATIO. Horatio! Hamlet. HAMLET. Hamlet!";
47
+        //When
48
+        String expected = "Tariq. TARIQ. Tariq! Leon. LEON. Leon!";
49
+        String actual = testHamletParser.changeAll(testString);
50
+        //Then
51
+        Assert.assertEquals(expected, actual);
22 52
     }
23 53
 
24 54
     @Test
25 55
     public void testFindHoratio() {
56
+        //Given
57
+        HamletParser testHamletParser = new HamletParser();
58
+        String testString = "Horatio. HORATIO. Horatio!";
59
+
60
+        //When
61
+        Integer expected = 158;
62
+        Integer actual = testHamletParser.findHoratio(hamletText);
63
+        //Then
64
+        Assert.assertEquals(expected, actual);
26 65
     }
27 66
 
28 67
     @Test
29 68
     public void testFindHamlet() {
69
+        //Given
70
+        HamletParser testHamletParser = new HamletParser();
71
+
72
+        //When
73
+        Integer expected = 472;
74
+        Integer actual = testHamletParser.findHalmet(hamletText);
75
+
76
+        //Then
77
+        Assert.assertEquals(expected, actual);
30 78
     }
31 79
 }

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