瀏覽代碼

Merge 5cf63fb0df0aba0aaad51a6b8006336e5aa35386 into 99c0c83b2f011a8e58c86fca53df6bfa67c86565

erc91087 6 年之前
父節點
當前提交
2cf895a19d
沒有帳戶連結到提交者的電子郵件
共有 4 個檔案被更改,包括 5473 行新增0 行删除
  1. 12
    0
      pom.xml
  2. 51
    0
      src/main/java/HamletParser.java
  3. 27
    0
      src/test/java/HamletParserTest.java
  4. 5383
    0
      target/classes/hamlet.txt

+ 12
- 0
pom.xml 查看文件

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>

+ 51
- 0
src/main/java/HamletParser.java 查看文件

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.*;
4
 
5
 
5
 /**
6
 /**
6
  * Created by thook on 10/7/15.
7
  * Created by thook on 10/7/15.
36
         return hamletData;
37
         return hamletData;
37
     }
38
     }
38
 
39
 
40
+    public String changeHamletToLeon(String string) {
41
+        Pattern lowerCase = Pattern.compile("Hamlet");
42
+        Matcher match1 = lowerCase.matcher(string);
43
+        String result1 = match1.replaceAll("Leon");
44
+
45
+        Pattern upperCase = Pattern.compile("HAMLET");
46
+        Matcher match2 = upperCase.matcher(result1);
47
+        String result2 = match2.replaceAll("LEON");
48
+
49
+        return result2;
50
+    }
51
+
52
+    public String changeHoratioToTariq(String string) {
53
+        Pattern lowerCase = Pattern.compile("Horatio");
54
+        Matcher match1 = lowerCase.matcher(string);
55
+        String result1 = match1.replaceAll("Tariq");
56
+
57
+        Pattern upperCase = Pattern.compile("HORATIO");
58
+        Matcher match2 = upperCase.matcher(result1);
59
+        String result2 = match2.replaceAll("TARIQ");
60
+
61
+        return result2;
62
+    }
63
+
64
+    public boolean findHamlet(String input){
65
+        if ( hamletData.contains("Hamlet") || hamletData.contains("HAMLET")) {
66
+        }
67
+        return true;
68
+    }
69
+
70
+    public boolean findHoratio(String input){
71
+        if ( hamletData.contains("Horatio") || hamletData.contains("HORATIO")) {
72
+        }
73
+        return true;
74
+    }
75
+
39
 }
76
 }
77
+/*
78
+REGEX
79
+
80
+Directions
81
+
82
+Make a project that will go through the hamlet file provided and
83
+using regex replace every instance of "Hamlet" with "Leon" and every instance of Horatio with "Tariq".
84
+
85
+Beginning with tests, you are to program all the steps it will take to complete that process.
86
+Some tests have been stubbed out for you but
87
+these will not cover all the methods you should have in your project.
88
+
89
+IMPORTANT: You are not to use String Utilities to simply replace words. You must use Pattern and Matcher.
90
+ */

+ 27
- 0
src/test/java/HamletParserTest.java 查看文件

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
 
15
 
16
 
16
     @Test
17
     @Test
17
     public void testChangeHamletToLeon() {
18
     public void testChangeHamletToLeon() {
19
+        // Given
20
+        HamletParser testHamletParser = new HamletParser();
21
+        String string = "Hamlet. HAMLET. Hamlet!";
22
+
23
+        // When
24
+        String expected = "Leon. LEON. Leon!";
25
+        String actual = testHamletParser.changeHamletToLeon(string);
26
+
27
+        // Then
28
+        Assert.assertEquals(expected, actual);
18
     }
29
     }
19
 
30
 
20
     @Test
31
     @Test
21
     public void testChangeHoratioToTariq() {
32
     public void testChangeHoratioToTariq() {
33
+        // Given
34
+        HamletParser testHamletParser = new HamletParser();
35
+        String string = "Horatio. HORATIO. Horatio!";
36
+
37
+        // When
38
+        String expected = "Tariq. TARIQ. Tariq!";
39
+        String actual = testHamletParser.changeHoratioToTariq(string);
40
+
41
+        // Then
42
+        Assert.assertEquals(expected, actual);
22
     }
43
     }
23
 
44
 
24
     @Test
45
     @Test
25
     public void testFindHoratio() {
46
     public void testFindHoratio() {
47
+        boolean expected = true;
48
+        boolean actual = hamletParser.findHoratio("Horatio found");
49
+        assertEquals(expected, actual);
26
     }
50
     }
27
 
51
 
28
     @Test
52
     @Test
29
     public void testFindHamlet() {
53
     public void testFindHamlet() {
54
+        boolean expected = true;
55
+        boolean actual = hamletParser.findHamlet("Hamlet found");
56
+        assertEquals(expected, actual);
30
     }
57
     }
31
 }
58
 }

+ 5383
- 0
target/classes/hamlet.txt
文件差異過大導致無法顯示
查看文件