Alfredo Castillo 6 gadus atpakaļ
vecāks
revīzija
13230c72bb

+ 50
- 0
src/main/java/HamletParser.java Parādīt failu

1
 import java.io.File;
1
 import java.io.File;
2
 import java.io.IOException;
2
 import java.io.IOException;
3
 import java.util.Scanner;
3
 import java.util.Scanner;
4
+import java.util.regex.Matcher;
5
+import java.util.regex.Pattern;
4
 
6
 
5
 /**
7
 /**
6
  * Created by thook on 10/7/15.
8
  * Created by thook on 10/7/15.
13
         this.hamletData = loadFile();
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
     private String loadFile(){
66
     private String loadFile(){
17
         ClassLoader classLoader = getClass().getClassLoader();
67
         ClassLoader classLoader = getClass().getClassLoader();
18
         File file = new File(classLoader.getResource("hamlet.txt").getFile());
68
         File file = new File(classLoader.getResource("hamlet.txt").getFile());

+ 29
- 0
src/test/java/HamletParserTest.java Parādīt failu

1
+import org.junit.Assert;
1
 import org.junit.Before;
2
 import org.junit.Before;
2
 import org.junit.Test;
3
 import org.junit.Test;
3
 
4
 
15
 
16
 
16
     @Test
17
     @Test
17
     public void testChangeHamletToLeon() {
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
     @Test
29
     @Test
21
     public void testChangeHoratioToTariq() {
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
     @Test
38
     @Test
25
     public void testFindHoratio() {
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
     @Test
50
     @Test
29
     public void testFindHamlet() {
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
Failā izmaiņas netiks attēlotas, jo tās ir par lielu
Parādīt failu