Browse Source

lab finished

yauhenip 6 years ago
parent
commit
97922ef5a8
3 changed files with 5458 additions and 1 deletions
  1. 51
    1
      src/main/java/HamletParser.java
  2. 24
    0
      src/test/java/HamletParserTest.java
  3. 5383
    0
      target/classes/hamlet.txt

+ 51
- 1
src/main/java/HamletParser.java View File

@@ -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,6 +10,8 @@ import java.util.Scanner;
8 10
 public class HamletParser {
9 11
 
10 12
     private String hamletData;
13
+    private Pattern hamlet = Pattern.compile("hamlet", Pattern.CASE_INSENSITIVE);
14
+    private Pattern horatio = Pattern.compile("horatio", Pattern.CASE_INSENSITIVE);
11 15
 
12 16
     public HamletParser(){
13 17
         this.hamletData = loadFile();
@@ -36,4 +40,50 @@ public class HamletParser {
36 40
         return hamletData;
37 41
     }
38 42
 
39
-}
43
+    public String changeHamletToLeonLower(String text) {
44
+        Pattern pttrn = Pattern.compile("Hamlet");
45
+        Matcher mtchr = pttrn.matcher(text);
46
+        return mtchr.replaceAll("Leon");
47
+    }
48
+
49
+    public String changeHamletToLeonUpper(String text) {
50
+        Pattern pttrn = Pattern.compile("HAMLET");
51
+        Matcher mtchr = pttrn.matcher(text);
52
+        return mtchr.replaceAll("LEON");
53
+    }
54
+
55
+    public String changeHoratioToTariqLower(String text) {
56
+        Pattern pttrn = Pattern.compile("Horatio");
57
+        Matcher mtchr = pttrn.matcher(text);
58
+        return mtchr.replaceAll("Tariq");
59
+    }
60
+
61
+    public String changeHoratioToTariqUpper(String text) {
62
+        Pattern pttrn = Pattern.compile("HORATIO");
63
+        Matcher mtchr = pttrn.matcher(text);
64
+        return mtchr.replaceAll("TARIQ");
65
+    }
66
+
67
+    public boolean findHamlet(String text) {
68
+        Matcher mtchr = hamlet.matcher(text);
69
+        return mtchr.find();
70
+    }
71
+
72
+    public boolean findHoratio(String text) {
73
+        Matcher mtchr = horatio.matcher(text);
74
+        return mtchr.find();
75
+    }
76
+
77
+    private void replaceAndPrint() {
78
+        hamletData = changeHoratioToTariqUpper(hamletData);
79
+        hamletData = changeHoratioToTariqLower(hamletData);
80
+        hamletData = changeHamletToLeonUpper(hamletData);
81
+        hamletData = changeHamletToLeonLower(hamletData);
82
+        System.out.println(hamletData);
83
+    }
84
+
85
+    public static void main(String[] args) {
86
+        HamletParser hp = new HamletParser();
87
+        hp.replaceAndPrint();
88
+    }
89
+}

+ 24
- 0
src/test/java/HamletParserTest.java View File

@@ -15,17 +15,41 @@ public class HamletParserTest {
15 15
 
16 16
     @Test
17 17
     public void testChangeHamletToLeon() {
18
+        hamletText = hamletParser.changeHamletToLeonLower(hamletText);
19
+        System.out.println(hamletText);
20
+        assertTrue(hamletText.contains("Leon"));
21
+    }
22
+
23
+    @Test
24
+    public void testChangeHamletToLeonCapital() {
25
+        hamletText = hamletParser.changeHamletToLeonUpper(hamletText);
26
+        assertTrue(hamletText.contains("LEON"));
18 27
     }
19 28
 
20 29
     @Test
21 30
     public void testChangeHoratioToTariq() {
31
+        hamletText = hamletParser.changeHoratioToTariqLower(hamletText);
32
+        assertTrue(hamletText.contains("Tariq"));
33
+    }
34
+
35
+    @Test
36
+    public void testChangeHoratioToTariqCapital() {
37
+        hamletText = hamletParser.changeHoratioToTariqUpper(hamletText);
38
+        assertTrue(hamletText.contains("TARIQ"));
22 39
     }
23 40
 
24 41
     @Test
25 42
     public void testFindHoratio() {
43
+        assertTrue(hamletParser.findHoratio(hamletText));
26 44
     }
27 45
 
28 46
     @Test
29 47
     public void testFindHamlet() {
48
+        assertTrue(hamletParser.findHamlet(hamletText));
49
+    }
50
+
51
+    @Test
52
+    public void testFindHamletFalse() {
53
+        assertFalse(hamletParser.findHamlet(hamletParser.changeHamletToLeonUpper(hamletParser.changeHamletToLeonLower(hamletText))));
30 54
     }
31 55
 }

+ 5383
- 0
target/classes/hamlet.txt
File diff suppressed because it is too large
View File