ThuyKhong vor 6 Jahren
Ursprung
Commit
5028ae6aad
3 geänderte Dateien mit 5427 neuen und 2 gelöschten Zeilen
  1. 32
    2
      src/main/java/HamletParser.java
  2. 12
    0
      src/test/java/HamletParserTest.java
  3. 5383
    0
      target/classes/hamlet.txt

+ 32
- 2
src/main/java/HamletParser.java Datei anzeigen

1
 import java.io.File;
1
 import java.io.File;
2
 import java.io.IOException;
2
 import java.io.IOException;
3
+import java.nio.file.Files;
4
+import java.util.List;
3
 import java.util.Scanner;
5
 import java.util.Scanner;
6
+import java.util.regex.Matcher;
7
+import java.util.regex.Pattern;
8
+import java.util.stream.Collectors;
9
+import java.util.stream.Stream;
4
 
10
 
5
 /**
11
 /**
6
  * Created by thook on 10/7/15.
12
  * Created by thook on 10/7/15.
9
 
15
 
10
     private String hamletData;
16
     private String hamletData;
11
 
17
 
18
+
12
     public HamletParser(){
19
     public HamletParser(){
13
         this.hamletData = loadFile();
20
         this.hamletData = loadFile();
14
     }
21
     }
22
             while(scanner.hasNextLine()){
29
             while(scanner.hasNextLine()){
23
                 String line = scanner.nextLine();
30
                 String line = scanner.nextLine();
24
                 result.append(line).append("\n");
31
                 result.append(line).append("\n");
25
-            }
26
 
32
 
33
+            }
27
             scanner.close();
34
             scanner.close();
28
         }catch(IOException e){
35
         }catch(IOException e){
29
             e.printStackTrace();
36
             e.printStackTrace();
30
         }
37
         }
31
-
38
+        System.out.println(result.toString());
32
         return result.toString();
39
         return result.toString();
33
     }
40
     }
34
 
41
 
36
         return hamletData;
43
         return hamletData;
37
     }
44
     }
38
 
45
 
46
+    public void replaceHamlet(){
47
+        Pattern pattern = Pattern.compile("Hamlet", Pattern.CASE_INSENSITIVE);
48
+        Matcher matcher = pattern.matcher(hamletData);
49
+        hamletData = matcher.replaceAll("Leon");
50
+    }
51
+
52
+    public void replaceHoratio(){
53
+        Pattern pattern = Pattern.compile("Horatio", Pattern.CASE_INSENSITIVE);
54
+        Matcher matcher = pattern.matcher(hamletData);
55
+        hamletData = matcher.replaceAll("Tariq");
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
 }

+ 12
- 0
src/test/java/HamletParserTest.java Datei anzeigen

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
 
5
+import java.util.stream.Stream;
6
+
4
 import static org.junit.Assert.*;
7
 import static org.junit.Assert.*;
5
 
8
 
6
 public class HamletParserTest {
9
 public class HamletParserTest {
15
 
18
 
16
     @Test
19
     @Test
17
     public void testChangeHamletToLeon() {
20
     public void testChangeHamletToLeon() {
21
+        hamletParser.replaceHamlet();
22
+        Assert.assertFalse(hamletParser.findHamlet());
18
     }
23
     }
19
 
24
 
25
+
20
     @Test
26
     @Test
21
     public void testChangeHoratioToTariq() {
27
     public void testChangeHoratioToTariq() {
28
+        hamletParser.replaceHoratio();
29
+        Assert.assertFalse(hamletParser.findHoratio());
22
     }
30
     }
23
 
31
 
24
     @Test
32
     @Test
25
     public void testFindHoratio() {
33
     public void testFindHoratio() {
34
+
35
+        Assert.assertTrue(hamletParser.findHoratio());
26
     }
36
     }
27
 
37
 
28
     @Test
38
     @Test
29
     public void testFindHamlet() {
39
     public void testFindHamlet() {
40
+
41
+        Assert.assertTrue(hamletParser.findHamlet());
30
     }
42
     }
31
 }
43
 }

+ 5383
- 0
target/classes/hamlet.txt
Datei-Diff unterdrückt, da er zu groß ist
Datei anzeigen