Xcuello 6 år sedan
förälder
incheckning
b585b7c3eb
4 ändrade filer med 5545 tillägg och 0 borttagningar
  1. 12
    0
      pom.xml
  2. 80
    0
      src/main/java/HamletParser.java
  3. 70
    0
      src/test/java/HamletParserTest.java
  4. 5383
    0
      target/classes/hamlet.txt

+ 12
- 0
pom.xml Visa fil

7
     <groupId>com.zipcodewilmington</groupId>
7
     <groupId>com.zipcodewilmington</groupId>
8
     <artifactId>regex</artifactId>
8
     <artifactId>regex</artifactId>
9
     <version>1.0-SNAPSHOT</version>
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>8</source>
17
+                    <target>8</target>
18
+                </configuration>
19
+            </plugin>
20
+        </plugins>
21
+    </build>
10
 
22
 
11
     <dependencies>
23
     <dependencies>
12
         <dependency>
24
         <dependency>

+ 80
- 0
src/main/java/HamletParser.java Visa fil

1
 import java.io.File;
1
 import java.io.File;
2
 import java.io.IOException;
2
 import java.io.IOException;
3
 import java.util.Scanner;
3
 import java.util.Scanner;
4
+import java.util.regex.Matcher;
5
+import java.util.regex.Pattern;
4
 
6
 
5
 /**
7
 /**
6
  * Created by thook on 10/7/15.
8
  * Created by thook on 10/7/15.
10
     private String hamletData;
12
     private String hamletData;
11
 
13
 
12
     public HamletParser(){
14
     public HamletParser(){
15
+
13
         this.hamletData = loadFile();
16
         this.hamletData = loadFile();
14
     }
17
     }
15
 
18
 
16
     private String loadFile(){
19
     private String loadFile(){
20
+
17
         ClassLoader classLoader = getClass().getClassLoader();
21
         ClassLoader classLoader = getClass().getClassLoader();
22
+
18
         File file = new File(classLoader.getResource("hamlet.txt").getFile());
23
         File file = new File(classLoader.getResource("hamlet.txt").getFile());
24
+
19
         StringBuilder result = new StringBuilder("");
25
         StringBuilder result = new StringBuilder("");
20
 
26
 
21
         try(Scanner scanner = new Scanner(file)){
27
         try(Scanner scanner = new Scanner(file)){
28
+
22
             while(scanner.hasNextLine()){
29
             while(scanner.hasNextLine()){
30
+
23
                 String line = scanner.nextLine();
31
                 String line = scanner.nextLine();
32
+
24
                 result.append(line).append("\n");
33
                 result.append(line).append("\n");
25
             }
34
             }
26
 
35
 
27
             scanner.close();
36
             scanner.close();
37
+
28
         }catch(IOException e){
38
         }catch(IOException e){
39
+
29
             e.printStackTrace();
40
             e.printStackTrace();
30
         }
41
         }
31
 
42
 
33
     }
44
     }
34
 
45
 
35
     public String getHamletData(){
46
     public String getHamletData(){
47
+
36
         return hamletData;
48
         return hamletData;
37
     }
49
     }
38
 
50
 
51
+    public String changeHamletToLeon(String testString) {
52
+
53
+        Pattern pattern = Pattern.compile("\\w+(\\W)?");
54
+
55
+        Matcher matcher = pattern.matcher(testString);
56
+
57
+        String answer = "";
58
+
59
+        while(matcher.find()){
60
+
61
+            String word = matcher.group();
62
+
63
+            if(word.equals("Hamlet ")) {
64
+
65
+                answer += "Leon ";
66
+
67
+            } else {
68
+
69
+                answer += word;
70
+            }
71
+        }
72
+
73
+        return answer;
74
+    }
75
+
76
+    public String changeHoratioToTariq(String testString) {
77
+
78
+        Pattern pattern = Pattern.compile("\\w+(\\W)?");
79
+
80
+        Matcher matcher = pattern.matcher(testString);
81
+
82
+        String answer = "";
83
+
84
+        while(matcher.find()){
85
+
86
+            String word = matcher.group();
87
+
88
+            if(word.equals("Horatio ")) {
89
+
90
+                answer += "Tariq ";
91
+
92
+            } else {
93
+
94
+                answer += word;
95
+            }
96
+        }
97
+
98
+        return answer;
99
+    }
100
+
101
+    public boolean findHoratio(String name) {
102
+
103
+        Pattern pattern = Pattern.compile(name);
104
+
105
+        Matcher matcher = pattern.matcher(name);
106
+
107
+        return matcher.find();
108
+    }
109
+
110
+
111
+    public boolean findHamlet(String name) {
112
+
113
+        Pattern pattern = Pattern.compile(name);
114
+
115
+        Matcher matcher = pattern.matcher(name);
116
+
117
+        return matcher.find();
118
+    }
39
 }
119
 }

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

1
+import org.junit.Assert;
1
 import org.junit.Before;
2
 import org.junit.Before;
2
 import org.junit.Test;
3
 import org.junit.Test;
3
 
4
 
5
+import java.util.SplittableRandom;
6
+
4
 import static org.junit.Assert.*;
7
 import static org.junit.Assert.*;
5
 
8
 
6
 public class HamletParserTest {
9
 public class HamletParserTest {
10
+
7
     private String hamletText;
11
     private String hamletText;
8
     private HamletParser hamletParser;
12
     private HamletParser hamletParser;
9
 
13
 
10
     @Before
14
     @Before
11
     public void setUp() {
15
     public void setUp() {
16
+
12
         this.hamletParser = new HamletParser();
17
         this.hamletParser = new HamletParser();
18
+
13
         this.hamletText = hamletParser.getHamletData();
19
         this.hamletText = hamletParser.getHamletData();
14
     }
20
     }
15
 
21
 
16
     @Test
22
     @Test
17
     public void testChangeHamletToLeon() {
23
     public void testChangeHamletToLeon() {
24
+
25
+        //Given
26
+
27
+        String testString = "Hamlet is a wild guy";
28
+
29
+        String expected = "Leon is a wild guy";
30
+
31
+        //When
32
+
33
+        String actual = hamletParser.changeHamletToLeon(testString);
34
+
35
+        //Then
36
+
37
+        Assert.assertEquals(expected,actual);
38
+
18
     }
39
     }
19
 
40
 
20
     @Test
41
     @Test
21
     public void testChangeHoratioToTariq() {
42
     public void testChangeHoratioToTariq() {
43
+
44
+        //Given
45
+
46
+        String testString = "Horatio is a wild guy";
47
+
48
+        String expected = "Tariq is a wild guy";
49
+
50
+        //When
51
+
52
+        String actual = hamletParser.changeHoratioToTariq(testString);
53
+
54
+        //Then
55
+
56
+        Assert.assertEquals(expected,actual);
57
+
22
     }
58
     }
23
 
59
 
60
+
24
     @Test
61
     @Test
25
     public void testFindHoratio() {
62
     public void testFindHoratio() {
63
+
64
+        //Given
65
+
66
+        String name = " ";
67
+
68
+        String expected = "Horatio";
69
+
70
+        //When
71
+
72
+        boolean actual = hamletParser.findHoratio(name);
73
+
74
+        //Then
75
+
76
+        Assert.assertTrue(expected,actual);
77
+
78
+
26
     }
79
     }
27
 
80
 
28
     @Test
81
     @Test
29
     public void testFindHamlet() {
82
     public void testFindHamlet() {
83
+
84
+        //Given
85
+
86
+        String name = " ";
87
+
88
+        String expected = "Hamlet";
89
+
90
+        //When
91
+
92
+        boolean actual = hamletParser.findHamlet(name);
93
+
94
+        //Then
95
+
96
+        Assert.assertTrue(expected,actual);
97
+
98
+
99
+
30
     }
100
     }
31
 }
101
 }

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