thulasi před 6 roky
rodič
revize
5a70bdd08f

+ 31
- 2
src/main/java/HamletParser.java Zobrazit soubor

@@ -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.
@@ -23,8 +25,6 @@ public class HamletParser {
23 25
                 String line = scanner.nextLine();
24 26
                 result.append(line).append("\n");
25 27
             }
26
-
27
-            scanner.close();
28 28
         }catch(IOException e){
29 29
             e.printStackTrace();
30 30
         }
@@ -36,4 +36,33 @@ public class HamletParser {
36 36
         return hamletData;
37 37
     }
38 38
 
39
+    private Matcher matchingwords(String regex){
40
+        Pattern pattern = Pattern.compile(regex);
41
+        Matcher matcher = pattern.matcher(this.hamletData.toLowerCase());
42
+        return  matcher;
43
+    }
44
+
45
+    public void changeHamletToLeon() {
46
+        if(findHamlet()) {
47
+            Matcher matcher = matchingwords("Hamlet");
48
+            matcher.replaceAll("Leon");
49
+        }
50
+    }
51
+
52
+    public void changeHoratioToTariq() {
53
+        if(findHamlet()) {
54
+            Matcher matcher = matchingwords("Horatio");
55
+            matcher.replaceAll("Tariq");
56
+        }
57
+    }
58
+
59
+    public boolean findHoratio() {
60
+        Matcher matcher = matchingwords("horatio");
61
+        return matcher.find();
62
+    }
63
+
64
+    public boolean findHamlet() {
65
+        Matcher matcher = matchingwords("hamlet");
66
+        return matcher.find();
67
+    }
39 68
 }

+ 11
- 0
src/test/java/HamletParserTest.java Zobrazit soubor

@@ -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,27 @@ public class HamletParserTest {
15 16
 
16 17
     @Test
17 18
     public void testChangeHamletToLeon() {
19
+        hamletParser.changeHamletToLeon();
20
+        boolean actual = hamletParser.findHamlet();
21
+        Assert.assertFalse(actual);
18 22
     }
19 23
 
20 24
     @Test
21 25
     public void testChangeHoratioToTariq() {
26
+        hamletParser.changeHoratioToTariq();
27
+        boolean actual = hamletParser.findHoratio();
28
+        Assert.assertFalse(actual);
22 29
     }
23 30
 
24 31
     @Test
25 32
     public void testFindHoratio() {
33
+        boolean actual = hamletParser.findHoratio();
34
+        Assert.assertTrue(actual);
26 35
     }
27 36
 
28 37
     @Test
29 38
     public void testFindHamlet() {
39
+        boolean actual = hamletParser.findHamlet();
40
+        Assert.assertTrue(actual);
30 41
     }
31 42
 }

+ 5383
- 0
target/classes/hamlet.txt
Diff nebyl zobrazen, protože je příliš veliký
Zobrazit soubor