#27 Completed lab

Отворено
Xcuello жели да споји 2 комит(е) из Xcuello/ZCW-Regex-Hamlet-Parser:master у master
4 измењених фајлова са 5545 додато и 2 уклоњено
  1. 12
    0
      pom.xml
  2. 80
    2
      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
- 2
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,29 @@ 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());
19
-        StringBuilder result = new StringBuilder("");
24
+
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
-            scanner.close();
28 36
         }catch(IOException e){
37
+
29 38
             e.printStackTrace();
30 39
         }
31 40
 
@@ -33,7 +42,76 @@ public class HamletParser {
33 42
     }
34 43
 
35 44
     public String getHamletData(){
45
+
36 46
         return hamletData;
37 47
     }
38 48
 
49
+    public String changeHamletToLeon(String testString) {
50
+
51
+        Pattern pattern = Pattern.compile("\\w+(\\W)?");
52
+
53
+        Matcher matcher = pattern.matcher(testString);
54
+
55
+        String answer = "";
56
+
57
+        while(matcher.find()){
58
+
59
+            String word = matcher.group();
60
+
61
+            if(word.equals("Hamlet ")) {
62
+
63
+                answer += "Leon ";
64
+
65
+            } else {
66
+
67
+                answer += word;
68
+            }
69
+        }
70
+
71
+        return answer;
72
+    }
73
+
74
+    public String changeHoratioToTariq(String testString) {
75
+
76
+        Pattern pattern = Pattern.compile("\\w+(\\W)?");
77
+
78
+        Matcher matcher = pattern.matcher(testString);
79
+
80
+        String answer = "";
81
+
82
+        while(matcher.find()){
83
+
84
+            String word = matcher.group();
85
+
86
+            if(word.equals("Horatio ")) {
87
+
88
+                answer += "Tariq ";
89
+
90
+            } else {
91
+
92
+                answer += word;
93
+            }
94
+        }
95
+
96
+        return answer;
97
+    }
98
+
99
+    public boolean findHoratio(String name) {
100
+
101
+        Pattern pattern = Pattern.compile(name);
102
+
103
+        Matcher matcher = pattern.matcher(name);
104
+
105
+        return matcher.find();
106
+    }
107
+
108
+
109
+    public boolean findHamlet(String name) {
110
+
111
+        Pattern pattern = Pattern.compile(name);
112
+
113
+        Matcher matcher = pattern.matcher(name);
114
+
115
+        return matcher.find();
116
+    }
39 117
 }

+ 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
Разлика између датотеке није приказан због своје велике величине
Прегледај датотеку