Nathan Hall 6 年 前
コミット
4fa414d388
共有3 個のファイルを変更した5428 個の追加0 個の削除を含む
  1. 32
    0
      src/main/java/HamletParser.java
  2. 13
    0
      src/test/java/HamletParserTest.java
  3. 5383
    0
      target/classes/hamlet.txt

+ 32
- 0
src/main/java/HamletParser.java ファイルの表示

@@ -1,6 +1,9 @@
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;
6
+
4 7
 
5 8
 /**
6 9
  * Created by thook on 10/7/15.
@@ -8,6 +11,7 @@ import java.util.Scanner;
8 11
 public class HamletParser {
9 12
 
10 13
     private String hamletData;
14
+    String patt = "\bHamlet\b";
11 15
 
12 16
     public HamletParser(){
13 17
         this.hamletData = loadFile();
@@ -36,4 +40,32 @@ public class HamletParser {
36 40
         return hamletData;
37 41
     }
38 42
 
43
+    public void changeHamletToLeon(){
44
+        Pattern pattern = Pattern.compile("Hamlet", Pattern.CASE_INSENSITIVE);
45
+        Matcher matcher = pattern.matcher(hamletData);
46
+        hamletData = matcher.replaceAll("Leon");
47
+
48
+    }
49
+
50
+    public void changeHoratioToTariq(){
51
+        Pattern pattern = Pattern.compile("Horatio", Pattern.CASE_INSENSITIVE);
52
+        Matcher matcher = pattern.matcher(hamletData);
53
+        hamletData = matcher.replaceAll("Tariq");
54
+
55
+    }
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
 }
70
+
71
+

+ 13
- 0
src/test/java/HamletParserTest.java ファイルの表示

@@ -1,3 +1,4 @@
1
+import org.junit.Assert;
1 2
 import org.junit.Before;
2 3
 import org.junit.Test;
3 4
 
@@ -15,17 +16,29 @@ public class HamletParserTest {
15 16
 
16 17
     @Test
17 18
     public void testChangeHamletToLeon() {
19
+        hamletParser.changeHamletToLeon();
20
+        Assert.assertFalse(hamletParser.findHamlet());
18 21
     }
19 22
 
20 23
     @Test
21 24
     public void testChangeHoratioToTariq() {
25
+        hamletParser.changeHoratioToTariq();
26
+        Assert.assertFalse(hamletParser.findHoratio());
22 27
     }
23 28
 
24 29
     @Test
25 30
     public void testFindHoratio() {
31
+        boolean expected = hamletParser.getHamletData().toLowerCase().contains("horatio");
32
+        boolean actual = hamletParser.findHoratio();
33
+
34
+        Assert.assertEquals(expected, actual);
26 35
     }
27 36
 
28 37
     @Test
29 38
     public void testFindHamlet() {
39
+        boolean expected = hamletParser.getHamletData().toLowerCase().contains("hamlet");
40
+        boolean actual = hamletParser.findHamlet();
41
+
42
+        Assert.assertEquals(expected, actual);
30 43
     }
31 44
 }

+ 5383
- 0
target/classes/hamlet.txt
ファイル差分が大きすぎるため省略します
ファイルの表示