ThuyKhong 6 年之前
父節點
當前提交
5028ae6aad
共有 3 個文件被更改,包括 5427 次插入2 次删除
  1. 32
    2
      src/main/java/HamletParser.java
  2. 12
    0
      src/test/java/HamletParserTest.java
  3. 5383
    0
      target/classes/hamlet.txt

+ 32
- 2
src/main/java/HamletParser.java 查看文件

@@ -1,6 +1,12 @@
1 1
 import java.io.File;
2 2
 import java.io.IOException;
3
+import java.nio.file.Files;
4
+import java.util.List;
3 5
 import java.util.Scanner;
6
+import java.util.regex.Matcher;
7
+import java.util.regex.Pattern;
8
+import java.util.stream.Collectors;
9
+import java.util.stream.Stream;
4 10
 
5 11
 /**
6 12
  * Created by thook on 10/7/15.
@@ -9,6 +15,7 @@ public class HamletParser {
9 15
 
10 16
     private String hamletData;
11 17
 
18
+
12 19
     public HamletParser(){
13 20
         this.hamletData = loadFile();
14 21
     }
@@ -22,13 +29,13 @@ public class HamletParser {
22 29
             while(scanner.hasNextLine()){
23 30
                 String line = scanner.nextLine();
24 31
                 result.append(line).append("\n");
25
-            }
26 32
 
33
+            }
27 34
             scanner.close();
28 35
         }catch(IOException e){
29 36
             e.printStackTrace();
30 37
         }
31
-
38
+        System.out.println(result.toString());
32 39
         return result.toString();
33 40
     }
34 41
 
@@ -36,4 +43,27 @@ public class HamletParser {
36 43
         return hamletData;
37 44
     }
38 45
 
46
+    public void replaceHamlet(){
47
+        Pattern pattern = Pattern.compile("Hamlet", Pattern.CASE_INSENSITIVE);
48
+        Matcher matcher = pattern.matcher(hamletData);
49
+        hamletData = matcher.replaceAll("Leon");
50
+    }
51
+
52
+    public void replaceHoratio(){
53
+        Pattern pattern = Pattern.compile("Horatio", Pattern.CASE_INSENSITIVE);
54
+        Matcher matcher = pattern.matcher(hamletData);
55
+        hamletData = matcher.replaceAll("Tariq");
56
+    }
57
+
58
+    public boolean findHamlet() {
59
+        Pattern pattern = Pattern.compile("Hamlet",Pattern.CASE_INSENSITIVE);
60
+        Matcher matcher = pattern.matcher(hamletData);
61
+        return matcher.find();
62
+    }
63
+
64
+    public boolean findHoratio() {
65
+        Pattern pattern = Pattern.compile("Horatio",Pattern.CASE_INSENSITIVE);
66
+        Matcher matcher = pattern.matcher(hamletData);
67
+        return matcher.find();
68
+    }
39 69
 }

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

@@ -1,6 +1,9 @@
1
+import org.junit.Assert;
1 2
 import org.junit.Before;
2 3
 import org.junit.Test;
3 4
 
5
+import java.util.stream.Stream;
6
+
4 7
 import static org.junit.Assert.*;
5 8
 
6 9
 public class HamletParserTest {
@@ -15,17 +18,26 @@ public class HamletParserTest {
15 18
 
16 19
     @Test
17 20
     public void testChangeHamletToLeon() {
21
+        hamletParser.replaceHamlet();
22
+        Assert.assertFalse(hamletParser.findHamlet());
18 23
     }
19 24
 
25
+
20 26
     @Test
21 27
     public void testChangeHoratioToTariq() {
28
+        hamletParser.replaceHoratio();
29
+        Assert.assertFalse(hamletParser.findHoratio());
22 30
     }
23 31
 
24 32
     @Test
25 33
     public void testFindHoratio() {
34
+
35
+        Assert.assertTrue(hamletParser.findHoratio());
26 36
     }
27 37
 
28 38
     @Test
29 39
     public void testFindHamlet() {
40
+
41
+        Assert.assertTrue(hamletParser.findHamlet());
30 42
     }
31 43
 }

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