Xcuello преди 6 години
родител
ревизия
b585b7c3eb
променени са 4 файла, в които са добавени 5545 реда и са изтрити 0 реда
  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 Целия файл

@@ -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>8</source>
17
+                    <target>8</target>
18
+                </configuration>
19
+            </plugin>
20
+        </plugins>
21
+    </build>
10 22
 
11 23
     <dependencies>
12 24
         <dependency>

+ 80
- 0
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.
@@ -10,22 +12,31 @@ public class HamletParser {
10 12
     private String hamletData;
11 13
 
12 14
     public HamletParser(){
15
+
13 16
         this.hamletData = loadFile();
14 17
     }
15 18
 
16 19
     private String loadFile(){
20
+
17 21
         ClassLoader classLoader = getClass().getClassLoader();
22
+
18 23
         File file = new File(classLoader.getResource("hamlet.txt").getFile());
24
+
19 25
         StringBuilder result = new StringBuilder("");
20 26
 
21 27
         try(Scanner scanner = new Scanner(file)){
28
+
22 29
             while(scanner.hasNextLine()){
30
+
23 31
                 String line = scanner.nextLine();
32
+
24 33
                 result.append(line).append("\n");
25 34
             }
26 35
 
27 36
             scanner.close();
37
+
28 38
         }catch(IOException e){
39
+
29 40
             e.printStackTrace();
30 41
         }
31 42
 
@@ -33,7 +44,76 @@ public class HamletParser {
33 44
     }
34 45
 
35 46
     public String getHamletData(){
47
+
36 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 Целия файл

@@ -1,31 +1,101 @@
1
+import org.junit.Assert;
1 2
 import org.junit.Before;
2 3
 import org.junit.Test;
3 4
 
5
+import java.util.SplittableRandom;
6
+
4 7
 import static org.junit.Assert.*;
5 8
 
6 9
 public class HamletParserTest {
10
+
7 11
     private String hamletText;
8 12
     private HamletParser hamletParser;
9 13
 
10 14
     @Before
11 15
     public void setUp() {
16
+
12 17
         this.hamletParser = new HamletParser();
18
+
13 19
         this.hamletText = hamletParser.getHamletData();
14 20
     }
15 21
 
16 22
     @Test
17 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 41
     @Test
21 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 61
     @Test
25 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 81
     @Test
29 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
Файловите разлики са ограничени, защото са твърде много
Целия файл