Connor Dunnigan 6 år sedan
förälder
incheckning
e8897527fd
3 ändrade filer med 5418 tillägg och 5 borttagningar
  1. 25
    5
      src/main/java/HamletParser.java
  2. 10
    0
      src/test/java/HamletParserTest.java
  3. 5383
    0
      target/classes/hamlet.txt

+ 25
- 5
src/main/java/HamletParser.java Visa fil

@@ -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.
@@ -16,19 +18,16 @@ public class HamletParser {
16 18
     private String loadFile(){
17 19
         ClassLoader classLoader = getClass().getClassLoader();
18 20
         File file = new File(classLoader.getResource("hamlet.txt").getFile());
19
-        StringBuilder result = new StringBuilder("");
21
+        StringBuilder result = new StringBuilder();
20 22
 
21 23
         try(Scanner scanner = new Scanner(file)){
22 24
             while(scanner.hasNextLine()){
23 25
                 String line = scanner.nextLine();
24 26
                 result.append(line).append("\n");
25 27
             }
26
-
27
-            scanner.close();
28
-        }catch(IOException e){
28
+        } catch(IOException e){
29 29
             e.printStackTrace();
30 30
         }
31
-
32 31
         return result.toString();
33 32
     }
34 33
 
@@ -36,4 +35,25 @@ public class HamletParser {
36 35
         return hamletData;
37 36
     }
38 37
 
38
+    public void changeHamletToLeon() {
39
+        Pattern hamPattern = Pattern.compile("Hamlet", Pattern.CASE_INSENSITIVE);
40
+        Matcher hamMatcher = hamPattern.matcher(hamletData);
41
+        hamletData = hamMatcher.replaceAll("Leon");
42
+    }
43
+    public void changeHoratioToTariq() {
44
+        Pattern horPattern = Pattern.compile("Horatio", Pattern.CASE_INSENSITIVE);
45
+        Matcher horMatcher = horPattern.matcher(hamletData);
46
+        hamletData = horMatcher.replaceAll("Tariq");
47
+    }
48
+
49
+    public boolean findHoratio() {
50
+        Pattern horPattern = Pattern.compile("Horatio", Pattern.CASE_INSENSITIVE);
51
+        Matcher horMatcher = horPattern.matcher(hamletData);
52
+        return horMatcher.find();
53
+    }
54
+    public boolean findHamlet() {
55
+        Pattern hamPattern = Pattern.compile("Hamlet", Pattern.CASE_INSENSITIVE);
56
+        Matcher hamMatcher = hamPattern.matcher(hamletData);
57
+        return hamMatcher.find();
58
+    }
39 59
 }

+ 10
- 0
src/test/java/HamletParserTest.java Visa fil

@@ -1,5 +1,6 @@
1 1
 import org.junit.Before;
2 2
 import org.junit.Test;
3
+import org.junit.Assert;
3 4
 
4 5
 import static org.junit.Assert.*;
5 6
 
@@ -15,17 +16,26 @@ public class HamletParserTest {
15 16
 
16 17
     @Test
17 18
     public void testChangeHamletToLeon() {
19
+        hamletParser.changeHamletToLeon();
20
+        Assert.assertFalse(hamletParser.findHamlet());
18 21
     }
19 22
 
20 23
     @Test
21 24
     public void testChangeHoratioToTariq() {
25
+        hamletParser.changeHoratioToTariq();
26
+        Assert.assertFalse(hamletParser.findHoratio());
27
+
22 28
     }
23 29
 
24 30
     @Test
25 31
     public void testFindHoratio() {
32
+        boolean exp = hamletParser.findHoratio();
33
+        Assert.assertTrue(exp);
26 34
     }
27 35
 
28 36
     @Test
29 37
     public void testFindHamlet() {
38
+        boolean exp = hamletParser.findHamlet();
39
+        Assert.assertTrue(exp);
30 40
     }
31 41
 }

+ 5383
- 0
target/classes/hamlet.txt
Filskillnaden har hållits tillbaka eftersom den är för stor
Visa fil