Karousha Fennimore 6 år sedan
förälder
incheckning
b2033a1e6a

+ 12
- 0
pom.xml Visa fil

@@ -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>

+ 59
- 6
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.
@@ -8,32 +10,83 @@ import java.util.Scanner;
8 10
 public class HamletParser {
9 11
 
10 12
     private String hamletData;
13
+    private int count = 0;
11 14
 
12
-    public HamletParser(){
15
+
16
+    public HamletParser() {
13 17
         this.hamletData = loadFile();
14 18
     }
15 19
 
16
-    private String loadFile(){
20
+    private String loadFile() {
17 21
         ClassLoader classLoader = getClass().getClassLoader();
18 22
         File file = new File(classLoader.getResource("hamlet.txt").getFile());
19 23
         StringBuilder result = new StringBuilder("");
20 24
 
21
-        try(Scanner scanner = new Scanner(file)){
22
-            while(scanner.hasNextLine()){
25
+        try (Scanner scanner = new Scanner(file)) {
26
+            while (scanner.hasNextLine()) {
23 27
                 String line = scanner.nextLine();
24 28
                 result.append(line).append("\n");
25 29
             }
26 30
 
27 31
             scanner.close();
28
-        }catch(IOException e){
32
+        } catch (IOException e) {
29 33
             e.printStackTrace();
30 34
         }
31 35
 
32 36
         return result.toString();
33 37
     }
34 38
 
35
-    public String getHamletData(){
39
+    public String getHamletData() {
36 40
         return hamletData;
37 41
     }
38 42
 
43
+    public String changeHamletToLeon(String text) {
44
+
45
+        Pattern replacement = Pattern.compile("Hamlet");
46
+        Matcher match = replacement.matcher(text);
47
+        String hamletData1 = match.replaceAll("Leon");
48
+
49
+        Pattern replacement1 = Pattern.compile("HAMLET");
50
+        Matcher match1 = replacement1.matcher(hamletData1);
51
+        String hamletData2 = match1.replaceAll("LEON");
52
+
53
+        return hamletData2;
54
+    }
55
+
56
+    public String changeHoratioToTariq(String text) {
57
+
58
+        Pattern replacement = Pattern.compile("Horatio");
59
+        Matcher match = replacement.matcher(text);
60
+        String hamletData1 = match.replaceAll("Tariq");
61
+
62
+        Pattern replacement1 = Pattern.compile("HORATIO");
63
+        Matcher match1 = replacement1.matcher(hamletData1);
64
+        String hamletData2 = match1.replaceAll("TARIQ");
65
+
66
+        return hamletData2;
67
+    }
68
+
69
+
70
+    private int findMatch(String regexSearch, String text) {
71
+        Pattern checkName = Pattern.compile(regexSearch);
72
+        Matcher regexCheck = checkName.matcher(text);
73
+
74
+        while (regexCheck.find()) {
75
+            if (regexCheck.group().length() != 0) {
76
+                count++;
77
+            }
78
+        }
79
+        return count;
80
+
81
+    }
82
+    public int findHoratio() {
83
+
84
+        return findMatch("[H|h|][O|o][R|r][A|a][T|t][I|i][O|o]", hamletData);
85
+
86
+    }
87
+
88
+    public int findHamlet() {
89
+
90
+      return findMatch("[H|h|][A|a][M|m][L|l][E|e][T|t]", hamletData);
91
+    }
39 92
 }

+ 4
- 0
src/main/resources/hamletTest.txt Visa fil

@@ -0,0 +1,4 @@
1
+Hamlet hamlet froilan hamlet hamlet
2
+horatio horatio horatio
3
+tariq tariq tariq
4
+froilan froilan froilan

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

@@ -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,42 @@ public class HamletParserTest {
15 16
 
16 17
     @Test
17 18
     public void testChangeHamletToLeon() {
19
+        //Given
20
+        String test = "Hamlet Hamlet HAMLET";
21
+        //When
22
+        String expected = "Leon Leon LEON";
23
+        String actual = hamletParser.changeHamletToLeon(test);
24
+        //THen
25
+        Assert.assertEquals(expected, actual);
18 26
     }
19 27
 
20 28
     @Test
21 29
     public void testChangeHoratioToTariq() {
30
+        //Given
31
+        String test = "Hamlet Hamlet HAMLET";
32
+        //When
33
+        String expected = "Leon Leon LEON";
34
+        String actual = hamletParser.changeHamletToLeon(test);
35
+        //THen
36
+        Assert.assertEquals(expected, actual);
22 37
     }
23 38
 
24 39
     @Test
25 40
     public void testFindHoratio() {
41
+        //When
42
+        int actual = 158;
43
+        int expected = hamletParser.findHoratio();
44
+        //Then
45
+        Assert.assertEquals(expected, actual);
26 46
     }
27 47
 
28 48
     @Test
29 49
     public void testFindHamlet() {
50
+        //When
51
+        int actual = 472;
52
+        int expected = hamletParser.findHamlet();
53
+        //Then
54
+        Assert.assertEquals(expected, actual);
30 55
     }
56
+
31 57
 }

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


+ 4
- 0
target/classes/hamletTest.txt Visa fil

@@ -0,0 +1,4 @@
1
+Hamlet hamlet froilan hamlet hamlet
2
+horatio horatio horatio
3
+tariq tariq tariq
4
+froilan froilan froilan