Browse Source

Merge c6e92f2e2fa9fb1b86301f3037b2ba73ca2740da into 99c0c83b2f011a8e58c86fca53df6bfa67c86565

April Rivera 6 years ago
parent
commit
d16d85cfff
No account linked to committer's email

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

+ 39
- 7
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,61 @@ 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
-
32 33
         return result.toString();
33 34
     }
34 35
 
35
-    public String getHamletData(){
36
+    public String getHamletData() {
36 37
         return hamletData;
37 38
     }
38 39
 
40
+
41
+    public void changeIfHamlet() {
42
+        Pattern p = Pattern.compile("(Hamlet| HAMLET).*");
43
+        Matcher m = p.matcher(hamletData);
44
+        if (m.find()) {
45
+            hamletData = m.replaceAll("Leon");
46
+        } else if (m.find()){
47
+            hamletData = m.replaceAll("LEON");
48
+        }
49
+        }
50
+
51
+    public void changeIfHoratio(){
52
+        Pattern p = Pattern.compile("(Horatio | HORATIO).*");
53
+        Matcher m = p.matcher(hamletData);
54
+        if (m.find()){
55
+            hamletData = m.replaceAll("Tariq");
56
+        } else if (m.find()) {
57
+            hamletData = m.replaceAll("TARIQ");
58
+        }
59
+    }
60
+    public boolean findHamlet(){
61
+        Pattern p = Pattern.compile("(Hamlet| HAMLET).*");
62
+        Matcher m = p.matcher(hamletData);
63
+        return m.find();
64
+    }
65
+    public boolean findHoratio(){
66
+        Pattern p = Pattern.compile("(Horatio | HORATIO).*");
67
+        Matcher m = p.matcher(hamletData);
68
+        return m.find();
69
+    }
39 70
 }
71
+

+ 0
- 0
src/main/resources/newHamlet.txt View File


+ 32
- 2
src/test/java/HamletParserTest.java View File

@@ -1,11 +1,11 @@
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;
8
-    private HamletParser hamletParser;
8
+    public HamletParser hamletParser;
9 9
 
10 10
     @Before
11 11
     public void setUp() {
@@ -15,17 +15,47 @@ public class HamletParserTest {
15 15
 
16 16
     @Test
17 17
     public void testChangeHamletToLeon() {
18
+        //Given
19
+        HamletParser hamletParser = new HamletParser();
20
+        String before = hamletParser.getHamletData();
21
+        //When
22
+        hamletParser.changeIfHamlet();
23
+        String after = hamletParser.getHamletData();
24
+        //Then
25
+        Assert.assertNotEquals(before, after);
18 26
     }
19 27
 
20 28
     @Test
21 29
     public void testChangeHoratioToTariq() {
30
+        //Given
31
+        HamletParser hamletParser = new HamletParser();
32
+        String before = hamletParser.getHamletData();
33
+        //When
34
+        hamletParser.changeIfHoratio();
35
+        String after = hamletParser.getHamletData();
36
+        //Then
37
+        Assert.assertNotEquals(before, after);
22 38
     }
23 39
 
24 40
     @Test
25 41
     public void testFindHoratio() {
42
+        //Given
43
+        HamletParser hamletParser = new HamletParser();
44
+        String s = "Horatio";
45
+        //When
46
+        hamletParser.findHoratio();
47
+        //Then
48
+        Assert.assertTrue(s, hamletParser.findHoratio());
26 49
     }
27 50
 
28 51
     @Test
29 52
     public void testFindHamlet() {
53
+        //Given
54
+        HamletParser hamletParser = new HamletParser();
55
+        String s = "Hamlet";
56
+        //When
57
+        hamletParser.findHamlet();
58
+        //Then
59
+        Assert.assertTrue(s, hamletParser.findHamlet());
30 60
     }
31 61
 }

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


+ 0
- 0
target/classes/newHamlet.txt View File