Przeglądaj źródła

tested file replace all

Eric Foster 6 lat temu
rodzic
commit
4c03772727

+ 14
- 3
src/main/java/HamletParser.java Wyświetl plik

@@ -28,7 +28,6 @@ public class HamletParser {
28 28
                 String line = scanner.nextLine();
29 29
                 result.append(line).append("\n");
30 30
             }
31
-
32 31
             scanner.close();
33 32
         }catch(IOException e){
34 33
             e.printStackTrace();
@@ -37,12 +36,18 @@ public class HamletParser {
37 36
         return result.toString();
38 37
     }
39 38
 
39
+    public String loadFileAndReplace(String find, String replace){
40
+        Pattern pattern = createRegexPattern(find);
41
+        Matcher matcher = createMatcherFromInput(pattern, hamletData);
42
+        return findReplace(matcher, replace);
43
+    }
44
+
40 45
     public String getHamletData(){
41 46
         return hamletData;
42 47
     }
43 48
 
44
-    public Pattern createRegexPattern(String regex) {
45
-        return Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
49
+    public Pattern createRegexPattern(String find) {
50
+        return Pattern.compile(find, Pattern.CASE_INSENSITIVE);
46 51
     }
47 52
 
48 53
     public Matcher createMatcherFromInput(Pattern pattern, String input) {
@@ -52,4 +57,10 @@ public class HamletParser {
52 57
     public String findReplace(Matcher matcher, String replace) {
53 58
         return matcher.replaceAll(replace);
54 59
     }
60
+
61
+    public boolean contains(String find, String input){
62
+        Pattern pattern = createRegexPattern(find);
63
+        Matcher matcher = createMatcherFromInput(pattern, input);
64
+        return matcher.find();
65
+    }
55 66
 }

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

@@ -17,6 +17,13 @@ public class HamletParserTest {
17 17
     }
18 18
 
19 19
     @Test
20
+    public void testLoadFileAndReplace() {
21
+        String s = hamletParser.loadFileAndReplace("Hamlet","Dolio");
22
+        boolean actual = hamletParser.contains("Hamlet","s");
23
+        assertFalse(actual);
24
+    }
25
+
26
+    @Test
20 27
     public void testChangeHamletToDolio() {
21 28
         String input = "HamletHoratioHamletHoratio";
22 29
         String find = "Hamlet";