Преглед на файлове

Merge dba6ff4f4efa9f45f9684159f2ca3bed94bb7d3f into 99c0c83b2f011a8e58c86fca53df6bfa67c86565

Frankie Rodriguez преди 6 години
родител
ревизия
0981cd7770
No account linked to committer's email
променени са 4 файла, в които са добавени 5479 реда и са изтрити 8 реда
  1. 12
    0
      pom.xml
  2. 45
    6
      src/main/java/HamletParser.java
  3. 39
    2
      src/test/java/HamletParserTest.java
  4. 5383
    0
      target/classes/hamlet.txt

+ 12
- 0
pom.xml Целия файл

@@ -7,6 +7,18 @@
7 7
     <groupId>com.zipcodewilmington</groupId>
8 8
     <artifactId>regex</artifactId>
9 9
     <version>1.0-SNAPSHOT</version>
10
+    <build>
11
+        <plugins>
12
+            <plugin>
13
+                <groupId>org.apache.maven.plugins</groupId>
14
+                <artifactId>maven-compiler-plugin</artifactId>
15
+                <configuration>
16
+                    <source>1.7</source>
17
+                    <target>1.7</target>
18
+                </configuration>
19
+            </plugin>
20
+        </plugins>
21
+    </build>
10 22
 
11 23
     <dependencies>
12 24
         <dependency>

+ 45
- 6
src/main/java/HamletParser.java Целия файл

@@ -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.
@@ -9,31 +11,68 @@ public class HamletParser {
9 11
 
10 12
     private String hamletData;
11 13
 
12
-    public HamletParser(){
14
+
15
+    private Pattern horatioP = Pattern.compile("(\\bHoratio\\b|\\bHORATIO\\b)");
16
+
17
+
18
+    public HamletParser() {
13 19
         this.hamletData = loadFile();
14 20
     }
15 21
 
16
-    private String loadFile(){
22
+    private String loadFile() {
17 23
         ClassLoader classLoader = getClass().getClassLoader();
18 24
         File file = new File(classLoader.getResource("hamlet.txt").getFile());
19 25
         StringBuilder result = new StringBuilder("");
20 26
 
21
-        try(Scanner scanner = new Scanner(file)){
22
-            while(scanner.hasNextLine()){
27
+        try (Scanner scanner = new Scanner(file)) {
28
+            while (scanner.hasNextLine()) {
23 29
                 String line = scanner.nextLine();
24 30
                 result.append(line).append("\n");
25 31
             }
26 32
 
27 33
             scanner.close();
28
-        }catch(IOException e){
34
+        } catch (IOException e) {
29 35
             e.printStackTrace();
30 36
         }
31 37
 
32 38
         return result.toString();
33 39
     }
34 40
 
35
-    public String getHamletData(){
41
+    public String getHamletData() {
36 42
         return hamletData;
37 43
     }
38 44
 
45
+    public void changeHoratioToTariq() {
46
+        Pattern horatioL = Pattern.compile("Horatio");
47
+        Matcher tariqL = horatioL.matcher(hamletData);
48
+        hamletData = tariqL.replaceAll("Tariq");
49
+        Pattern horatioU = Pattern.compile("\\bHORATIO\\b");
50
+        Matcher tariqU = horatioU.matcher(hamletData);
51
+        hamletData = tariqU.replaceAll("TARIQ");
52
+    }
53
+
54
+    public void changeHamletToLeon() {
55
+        Pattern hamletL = Pattern.compile("Hamlet");
56
+        Matcher leonL = hamletL.matcher(hamletData);
57
+        hamletData = leonL.replaceAll("Leon");
58
+        Pattern hamletU = Pattern.compile("\\bHAMLET\\b");
59
+        Matcher leonU = hamletU.matcher(hamletData);
60
+        hamletData = leonU.replaceAll("LEON");
61
+    }
62
+
63
+    public void replaceBothNames(){
64
+        changeHamletToLeon();
65
+        changeHoratioToTariq();
66
+    }
67
+
68
+    public boolean contains(String wordToLookOutFor){
69
+        String[] hamletArray = getHamletData().split(" ");
70
+        for(String word : hamletArray){
71
+            if(word.equals(wordToLookOutFor)){
72
+                return true;
73
+            }
74
+        }
75
+        return false;
76
+    }
77
+
39 78
 }

+ 39
- 2
src/test/java/HamletParserTest.java Целия файл

@@ -1,3 +1,4 @@
1
+import org.junit.Assert;
1 2
 import org.junit.Before;
2 3
 import org.junit.Test;
3 4
 
@@ -15,17 +16,53 @@ public class HamletParserTest {
15 16
 
16 17
     @Test
17 18
     public void testChangeHamletToLeon() {
19
+        HamletParser hamletParser = new HamletParser();
20
+        String preChanges = hamletParser.getHamletData();
21
+        hamletParser.changeHamletToLeon();
22
+        String postChanges = hamletParser.getHamletData();
23
+        Assert.assertNotEquals(preChanges, postChanges);
18 24
     }
19 25
 
20 26
     @Test
21 27
     public void testChangeHoratioToTariq() {
28
+        HamletParser hamletParser = new HamletParser();
29
+        String preChanges = hamletParser.getHamletData();
30
+        hamletParser.changeHoratioToTariq();
31
+        String postChanges = hamletParser.getHamletData();
32
+        Assert.assertNotEquals(preChanges, postChanges);
22 33
     }
23 34
 
24 35
     @Test
25
-    public void testFindHoratio() {
36
+    public void testFindHoratioBeforeChange(){
37
+        HamletParser hamletParser = new HamletParser();
38
+        Assert.assertTrue(hamletParser.contains("Horatio") || hamletParser.contains("HORATIO"));
26 39
     }
27 40
 
28 41
     @Test
29
-    public void testFindHamlet() {
42
+    public void testFindHoratioAfterChange(){
43
+        HamletParser hamletParser = new HamletParser();
44
+        hamletParser.changeHoratioToTariq();
45
+        Assert.assertFalse(hamletParser.contains("Horatio") || hamletParser.contains("HORATIO"));
46
+    }
47
+
48
+    @Test
49
+    public void testFineHamletBeforeChange(){
50
+        HamletParser hamletParser = new HamletParser();
51
+        Assert.assertTrue(hamletParser.contains("Hamlet") || hamletParser.contains("HAMLET"));
52
+    }
53
+    @Test
54
+    public void testFineHamletAfterChange(){
55
+        HamletParser hamletParser = new HamletParser();
56
+        hamletParser.changeHamletToLeon();
57
+        Assert.assertFalse(hamletParser.contains("Hamlet") || hamletParser.contains("HAMLET"));
58
+    }
59
+
60
+    @Test
61
+    public void testChangeBothNames(){
62
+        HamletParser hamletParser = new HamletParser();
63
+        String preChanges = hamletParser.getHamletData();
64
+        hamletParser.replaceBothNames();
65
+        String postChanges = hamletParser.getHamletData();
66
+        Assert.assertNotEquals(preChanges, postChanges);
30 67
     }
31 68
 }

+ 5383
- 0
target/classes/hamlet.txt
Файловите разлики са ограничени, защото са твърде много
Целия файл