April Rivera 6 years ago
parent
commit
c6e92f2e2f

+ 12
- 0
pom.xml View File

7
     <groupId>com.zipcodewilmington</groupId>
7
     <groupId>com.zipcodewilmington</groupId>
8
     <artifactId>regex</artifactId>
8
     <artifactId>regex</artifactId>
9
     <version>1.0-SNAPSHOT</version>
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
     <dependencies>
23
     <dependencies>
12
         <dependency>
24
         <dependency>

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

1
 import java.io.File;
1
 import java.io.File;
2
 import java.io.IOException;
2
 import java.io.IOException;
3
 import java.util.Scanner;
3
 import java.util.Scanner;
4
+import java.util.regex.Matcher;
5
+import java.util.regex.Pattern;
4
 
6
 
5
 /**
7
 /**
6
  * Created by thook on 10/7/15.
8
  * Created by thook on 10/7/15.
9
 
11
 
10
     private String hamletData;
12
     private String hamletData;
11
 
13
 
12
-    public HamletParser(){
14
+    public HamletParser() {
13
         this.hamletData = loadFile();
15
         this.hamletData = loadFile();
14
     }
16
     }
15
 
17
 
16
-    private String loadFile(){
18
+    private String loadFile() {
17
         ClassLoader classLoader = getClass().getClassLoader();
19
         ClassLoader classLoader = getClass().getClassLoader();
18
         File file = new File(classLoader.getResource("hamlet.txt").getFile());
20
         File file = new File(classLoader.getResource("hamlet.txt").getFile());
19
         StringBuilder result = new StringBuilder("");
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
                 String line = scanner.nextLine();
25
                 String line = scanner.nextLine();
24
                 result.append(line).append("\n");
26
                 result.append(line).append("\n");
25
             }
27
             }
26
 
28
 
27
             scanner.close();
29
             scanner.close();
28
-        }catch(IOException e){
30
+        } catch (IOException e) {
29
             e.printStackTrace();
31
             e.printStackTrace();
30
         }
32
         }
31
-
32
         return result.toString();
33
         return result.toString();
33
     }
34
     }
34
 
35
 
35
-    public String getHamletData(){
36
+    public String getHamletData() {
36
         return hamletData;
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
+import org.junit.Assert;
1
 import org.junit.Before;
2
 import org.junit.Before;
2
 import org.junit.Test;
3
 import org.junit.Test;
3
 
4
 
4
-import static org.junit.Assert.*;
5
 
5
 
6
 public class HamletParserTest {
6
 public class HamletParserTest {
7
     private String hamletText;
7
     private String hamletText;
8
-    private HamletParser hamletParser;
8
+    public HamletParser hamletParser;
9
 
9
 
10
     @Before
10
     @Before
11
     public void setUp() {
11
     public void setUp() {
15
 
15
 
16
     @Test
16
     @Test
17
     public void testChangeHamletToLeon() {
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
     @Test
28
     @Test
21
     public void testChangeHoratioToTariq() {
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
     @Test
40
     @Test
25
     public void testFindHoratio() {
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
     @Test
51
     @Test
29
     public void testFindHamlet() {
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