Alfredo Castillo 6 lat temu
rodzic
commit
13230c72bb

+ 50
- 0
src/main/java/HamletParser.java Wyświetl plik

@@ -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.
@@ -13,6 +15,54 @@ public class HamletParser {
13 15
         this.hamletData = loadFile();
14 16
     }
15 17
 
18
+    public static String findHamlet(String book) {
19
+        Pattern p = Pattern.compile("Hamlet");
20
+        Matcher m = p.matcher(book);
21
+
22
+        String str = null;
23
+        if (m.find()){
24
+            str = m.group();
25
+
26
+        }else{
27
+            return null;
28
+        }
29
+        return str;
30
+
31
+    }
32
+
33
+    public static String findHoratio(String book) {
34
+
35
+        Pattern p = Pattern.compile("[HoratioHORATIO]");
36
+        Matcher m = p.matcher(book);
37
+
38
+        String str;
39
+
40
+        if(m.find()){
41
+            str = m.group();
42
+        } else {
43
+            return null;
44
+        }
45
+        return str;
46
+    }
47
+
48
+    public static String changeToLeon(String book) {
49
+        Pattern p = Pattern.compile(book);
50
+        Matcher m = p.matcher(book.toLowerCase());
51
+
52
+        book = m.replaceAll("leon");
53
+
54
+        return book;
55
+    }
56
+
57
+    public static String changeToTariq(String book) {
58
+        Pattern p = Pattern.compile(book);
59
+        Matcher m = p.matcher(book.toLowerCase());
60
+
61
+        book = m.replaceAll("tariq");
62
+
63
+        return book;
64
+    }
65
+
16 66
     private String loadFile(){
17 67
         ClassLoader classLoader = getClass().getClassLoader();
18 68
         File file = new File(classLoader.getResource("hamlet.txt").getFile());

+ 29
- 0
src/test/java/HamletParserTest.java Wyświetl plik

@@ -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,45 @@ public class HamletParserTest {
15 16
 
16 17
     @Test
17 18
     public void testChangeHamletToLeon() {
19
+
20
+        String book = hamletText;
21
+
22
+        String actual = HamletParser.changeToLeon(book);
23
+        Assert.assertTrue(actual.contains("leon"));
24
+        //research .replace
25
+
26
+
18 27
     }
19 28
 
20 29
     @Test
21 30
     public void testChangeHoratioToTariq() {
31
+        String book = hamletText;
32
+
33
+        String actual = HamletParser.changeToTariq(book);
34
+        Assert.assertTrue(actual.contains("Tariq"));
35
+        //research .replace
22 36
     }
23 37
 
24 38
     @Test
25 39
     public void testFindHoratio() {
40
+        String book = hamletText;
41
+
42
+        String expected = "Horatio";
43
+
44
+        String actual = HamletParser.findHoratio(book);
45
+
46
+        Assert.assertEquals(expected, actual);
47
+        //repeat find hamlet
26 48
     }
27 49
 
28 50
     @Test
29 51
     public void testFindHamlet() {
52
+        String book = hamletText;
53
+
54
+        String expected = "Hamlet";
55
+
56
+        String actual = HamletParser.findHamlet(book);
57
+
58
+        Assert.assertEquals(expected, actual);
30 59
     }
31 60
 }

+ 5383
- 0
target/classes/hamlet.txt
Plik diff jest za duży
Wyświetl plik