Jordan Elderidge 6 år sedan
förälder
incheckning
cc6e6782a4
2 ändrade filer med 94 tillägg och 1 borttagningar
  1. 53
    1
      src/main/java/HamletParser.java
  2. 41
    0
      src/test/java/HamletParserTest.java

+ 53
- 1
src/main/java/HamletParser.java Visa fil

@@ -20,6 +20,8 @@ public class HamletParser {
20 20
         this.hamletData = sentence;
21 21
     }
22 22
 
23
+
24
+
23 25
     private String loadFile(){
24 26
         ClassLoader classLoader = getClass().getClassLoader();
25 27
         File file = new File(classLoader.getResource("hamlet.txt").getFile());
@@ -66,4 +68,54 @@ public class HamletParser {
66 68
         return str2;
67 69
     }
68 70
 
69
-}
71
+    public String changeHoratioToTariq(String sentence){
72
+
73
+        Pattern pattern = Pattern.compile("(h|H)oratio");
74
+
75
+        String str = sentence;
76
+
77
+        Matcher m = pattern.matcher(str);
78
+
79
+        String str2 = m.replaceAll("Tariq");
80
+
81
+        System.out.println(str2);
82
+
83
+        return str2;
84
+    }
85
+    public boolean findHoratio() {
86
+        boolean findHoratio = true;
87
+
88
+        String hString = "Horatio";
89
+        Pattern hPattern = Pattern.compile(hString);
90
+        Matcher hMatcher = hPattern.matcher(hamletData);
91
+
92
+        if (hMatcher.find()) {
93
+            findHoratio = true;
94
+        } else {
95
+            return false;
96
+        }
97
+        return findHoratio;
98
+
99
+    }
100
+    public boolean findHamlet() {
101
+        boolean findHamlet = true;
102
+
103
+        String hamString = "Hamlet";
104
+        Pattern hamPattern = Pattern.compile(hamString);
105
+        Matcher hamMatcher = hamPattern.matcher(hamletData);
106
+
107
+        if (hamMatcher.find()) {
108
+            findHamlet = true;
109
+        } else {
110
+            return false;
111
+        }
112
+        return findHamlet;
113
+    }
114
+
115
+
116
+
117
+
118
+
119
+
120
+    }
121
+

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

@@ -41,13 +41,54 @@ public class HamletParserTest {
41 41
 
42 42
     @Test
43 43
     public void testChangeHoratioToTariq() {
44
+        String sentence = "Horatio hates cowboy fans and will kill them all";
45
+        HamletParser hamletParser = new HamletParser(sentence);
46
+        String expected = "Tariq hates cowboy fans and will kill them all";
47
+        String actual = hamletParser.changeHoratioToTariq(sentence);
48
+        Assert.assertEquals(expected, actual);
49
+
44 50
     }
51
+    @Test
52
+    public void testChangeHoratioToTariq2() {
53
+        String sentence = "Horatio loves the eagles horatio was happy when the eagles won the superbowl";
54
+        HamletParser hamletParser = new HamletParser(sentence);
55
+        String expected = "Tariq loves the eagles Tariq was happy when the eagles won the superbowl";
56
+        String actual = hamletParser.changeHoratioToTariq(sentence);
57
+        Assert.assertEquals(expected, actual);
58
+    }
59
+
45 60
 
46 61
     @Test
47 62
     public void testFindHoratio() {
63
+        //Given
64
+        boolean expected = true;
65
+
66
+        //when
67
+        hamletParser = new HamletParser();
68
+        boolean actual = hamletParser.findHoratio();
69
+
70
+        //Then
71
+        Assert.assertEquals(expected,actual);
72
+
73
+
74
+
75
+
76
+
77
+
78
+
79
+
48 80
     }
49 81
 
50 82
     @Test
51 83
     public void testFindHamlet() {
84
+        //Given
85
+        boolean expected = true;
86
+
87
+        //when
88
+        hamletParser = new HamletParser();
89
+        boolean actual = hamletParser.findHoratio();
90
+
91
+        //Then
92
+        Assert.assertEquals(expected,actual);
52 93
     }
53 94
 }