Bladeren bron

Merge ffec988c948b90cbf422634f321b30115b4f5b0f into 99c0c83b2f011a8e58c86fca53df6bfa67c86565

ahsonali 6 jaren geleden
bovenliggende
commit
4fb9c8f103
Geen account gekoppeld aan de committers e-mail
4 gewijzigde bestanden met toevoegingen van 5537 en 10 verwijderingen
  1. 12
    0
      pom.xml
  2. 66
    6
      src/main/java/HamletParser.java
  3. 76
    4
      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>1.7</source>
17
+                    <target>1.7</target>
18
+                </configuration>
19
+            </plugin>
20
+        </plugins>
21
+    </build>
10 22
 
11 23
     <dependencies>
12 24
         <dependency>

+ 66
- 6
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.
@@ -8,32 +10,90 @@ import java.util.Scanner;
8 10
 public class HamletParser {
9 11
 
10 12
     private String hamletData;
13
+//    private Pattern pattern;
14
+//    private Matcher matcher;
11 15
 
12
-    public HamletParser(){
16
+    public HamletParser() {
13 17
         this.hamletData = loadFile();
18
+
14 19
     }
15 20
 
16
-    private String loadFile(){
21
+
22
+    private String loadFile() {
17 23
         ClassLoader classLoader = getClass().getClassLoader();
18 24
         File file = new File(classLoader.getResource("hamlet.txt").getFile());
19 25
         StringBuilder result = new StringBuilder("");
20 26
 
21
-        try(Scanner scanner = new Scanner(file)){
22
-            while(scanner.hasNextLine()){
27
+        try (Scanner scanner = new Scanner(file)) {
28
+            while (scanner.hasNextLine()) {
23 29
                 String line = scanner.nextLine();
24 30
                 result.append(line).append("\n");
25 31
             }
26 32
 
27 33
             scanner.close();
28
-        }catch(IOException e){
34
+        } catch (IOException e) {
29 35
             e.printStackTrace();
30 36
         }
31 37
 
32 38
         return result.toString();
33 39
     }
34 40
 
35
-    public String getHamletData(){
41
+
42
+    public String replaceHamletWithLeon(String input) {
43
+
44
+        Pattern patternLowerCase = Pattern.compile("Hamlet");
45
+        Matcher m1 = patternLowerCase.matcher(input);
46
+        String result1 = m1.replaceAll("Leon");
47
+
48
+        Pattern patternUpperCase = Pattern.compile("HAMLET");
49
+        Matcher m2 = patternUpperCase.matcher(result1);
50
+        String result2 = m2.replaceAll("LEON");
51
+
52
+        return result2;
53
+
54
+    }
55
+
56
+    public String replaceHoratioWithTariq(String input) {
57
+        Pattern patternLowerCase = Pattern.compile("Horatio");
58
+        Matcher m1 = patternLowerCase.matcher(input);
59
+        String result1 = m1.replaceAll("Tariq");
60
+
61
+        Pattern patternUpperCase = Pattern.compile("HORATIO");
62
+        Matcher m2 = patternUpperCase.matcher(result1);
63
+        String result2 = m2.replaceAll("TARIQ");
64
+
65
+        return result2;
66
+
67
+    }
68
+
69
+    public boolean findHamlet(String input)
70
+    {
71
+        if (input.contains("Hamlet") || input.contains("HAMLET"))
72
+        {
73
+            return true;
74
+        }
75
+
76
+        return false;
77
+    }
78
+
79
+    public boolean findHoratio(String input)
80
+    {
81
+        if (input.contains("Horatio") || input.contains("HORATIO"))
82
+        {
83
+            return true;
84
+        }
85
+
86
+        return false;
87
+
88
+    }
89
+
90
+    public String getHamletData()
91
+    {
36 92
         return hamletData;
37 93
     }
38 94
 
95
+
96
+
97
+
98
+
39 99
 }

+ 76
- 4
src/test/java/HamletParserTest.java Bestand weergeven

@@ -1,8 +1,14 @@
1
+import org.junit.Assert;
1 2
 import org.junit.Before;
2 3
 import org.junit.Test;
4
+import org.omg.Messaging.SYNC_WITH_TRANSPORT;
5
+
6
+import java.util.regex.Pattern;
7
+import java.util.regex.Matcher;
3 8
 
4 9
 import static org.junit.Assert.*;
5 10
 
11
+
6 12
 public class HamletParserTest {
7 13
     private String hamletText;
8 14
     private HamletParser hamletParser;
@@ -14,18 +20,84 @@ public class HamletParserTest {
14 20
     }
15 21
 
16 22
     @Test
17
-    public void testChangeHamletToLeon() {
23
+    public void testChangeHamletToLeon1()
24
+    {
25
+        HamletParser hamletParser = new HamletParser();
26
+
27
+        String input = "This is Hamlet.";
28
+
29
+        String expected = "This is Leon.";
30
+
31
+        String actual = hamletParser.replaceHamletWithLeon(input);
32
+
33
+        Assert.assertEquals(expected, actual);
34
+
18 35
     }
19 36
 
20 37
     @Test
21
-    public void testChangeHoratioToTariq() {
38
+    public void testChangeHamletToLeon2()
39
+    {
40
+        HamletParser hamletParser = new HamletParser();
41
+
42
+        String testString = "This is HAMLET.";
43
+
44
+        String expected = "This is LEON.";
45
+
46
+        String actual = hamletParser.replaceHamletWithLeon(testString);
47
+
48
+        Assert.assertEquals(expected, actual);
49
+
22 50
     }
23 51
 
52
+
53
+
24 54
     @Test
25
-    public void testFindHoratio() {
55
+    public void testChangeHoratioToTariq()
56
+    {
57
+        HamletParser hamletParser = new HamletParser();
58
+
59
+        String testString = "This is Horatio.";
60
+
61
+        String expected = "This is Tariq.";
62
+
63
+        String actual = hamletParser.replaceHoratioWithTariq(testString);
64
+
65
+        Assert.assertEquals(expected, actual);
66
+
67
+    }
68
+
69
+    @Test
70
+    public void testFindHoratio1()
71
+    {
72
+        boolean expected = true;
73
+        boolean actual = hamletParser.findHoratio("Horatio is here");
74
+        Assert.assertEquals(expected, actual);
26 75
     }
27 76
 
28 77
     @Test
29
-    public void testFindHamlet() {
78
+    public void testFindHoratio2()
79
+    {
80
+        boolean expected = true;
81
+        boolean actual = hamletParser.findHoratio("HORATIO is here");
82
+        Assert.assertEquals(expected, actual);
83
+    }
84
+
85
+
86
+    @Test
87
+    public void testFindHamlet1()
88
+    {
89
+        boolean expected = true;
90
+        boolean actual = hamletParser.findHamlet("Hamlet is here");
91
+        Assert.assertEquals(expected, actual);
92
+
93
+    }
94
+
95
+    @Test
96
+    public void testFindHamlet2()
97
+    {
98
+        boolean expected = true;
99
+        boolean actual = hamletParser.findHamlet("HAMLET is here");
100
+        Assert.assertEquals(expected, actual);
101
+
30 102
     }
31 103
 }

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