Garrett Arant 6 lat temu
rodzic
commit
c9da8f7b93

+ 12
- 0
pom.xml Wyświetl plik

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

+ 60
- 6
src/main/java/HamletParser.java Wyświetl plik

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.
9
 
11
 
10
     private String hamletData;
12
     private String hamletData;
11
 
13
 
12
-    public HamletParser(){
14
+    public HamletParser() {
13
         this.hamletData = loadFile();
15
         this.hamletData = loadFile();
14
     }
16
     }
15
 
17
 
16
-    private String loadFile(){
18
+    private String loadFile() {
17
         ClassLoader classLoader = getClass().getClassLoader();
19
         ClassLoader classLoader = getClass().getClassLoader();
18
         File file = new File(classLoader.getResource("hamlet.txt").getFile());
20
         File file = new File(classLoader.getResource("hamlet.txt").getFile());
19
         StringBuilder result = new StringBuilder("");
21
         StringBuilder result = new StringBuilder("");
20
 
22
 
21
-        try(Scanner scanner = new Scanner(file)){
22
-            while(scanner.hasNextLine()){
23
+        try (Scanner scanner = new Scanner(file)) {
24
+            while (scanner.hasNextLine()) {
23
                 String line = scanner.nextLine();
25
                 String line = scanner.nextLine();
24
                 result.append(line).append("\n");
26
                 result.append(line).append("\n");
25
             }
27
             }
26
 
28
 
27
             scanner.close();
29
             scanner.close();
28
-        }catch(IOException e){
30
+        } catch (IOException e) {
29
             e.printStackTrace();
31
             e.printStackTrace();
30
         }
32
         }
31
 
33
 
32
         return result.toString();
34
         return result.toString();
33
     }
35
     }
34
 
36
 
35
-    public String getHamletData(){
37
+    public String getHamletData() {
36
         return hamletData;
38
         return hamletData;
37
     }
39
     }
38
 
40
 
41
+    public String changeHamletToLeon(String hamletData) {
42
+        String input = hamletData;
43
+        Pattern p = Pattern.compile("Hamlet");
44
+        Matcher m = p.matcher(input);
45
+        String hamletToLeon = m.replaceAll("Leon");
46
+        Pattern upperP = Pattern.compile("HAMLET");
47
+        Matcher upperM = upperP.matcher(hamletToLeon);
48
+        String hamletToLeonUpper = upperM.replaceAll("LEON");
49
+
50
+        return hamletToLeonUpper.toString();
51
+    }
52
+
53
+    public String changeHoratioToTariq(String hamletData) {
54
+        String input = hamletData;
55
+        Pattern p = Pattern.compile("Horatio");
56
+        Matcher m = p.matcher(input);
57
+        String horatioToTariq = m.replaceAll("Tariq");
58
+        Pattern upperP = Pattern.compile("HORATIO");
59
+        Matcher upperM = upperP.matcher(horatioToTariq);
60
+        String horatioToTariqUpper = upperM.replaceAll("TARIQ");
61
+
62
+        return horatioToTariqUpper.toString();
63
+    }
64
+
65
+    public String changeAll(String hamletData) {
66
+        String changeAll = changeHoratioToTariq(changeHamletToLeon(hamletData));
67
+
68
+
69
+        return changeAll;
70
+    }
71
+
72
+
73
+    public Integer findHalmet(String hamletData) {
74
+        String input = hamletData;
75
+        Pattern p = Pattern.compile("Hamlet", Pattern.CASE_INSENSITIVE);
76
+        Matcher m = p.matcher(input);
77
+        int count = 0;
78
+        while (m.find())
79
+            count++;
80
+        return count;
81
+    }
82
+
83
+    public Integer findHoratio(String hamletData) {
84
+        String input = hamletData;
85
+        Pattern p = Pattern.compile("Horatio", Pattern.CASE_INSENSITIVE);
86
+        Matcher m = p.matcher(input);
87
+        int count = 0;
88
+        while (m.find())
89
+            count++;
90
+        return count;
91
+    }
92
+
39
 }
93
 }

+ 48
- 0
src/test/java/HamletParserTest.java Wyświetl plik

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
 
15
 
16
 
16
     @Test
17
     @Test
17
     public void testChangeHamletToLeon() {
18
     public void testChangeHamletToLeon() {
19
+        //Given
20
+        HamletParser testHamletParser = new HamletParser();
21
+        String testString = "Hamlet. HAMLET. Hamlet!";
22
+        //When
23
+        String expected = "Leon. LEON. Leon!";
24
+        String actual = testHamletParser.changeHamletToLeon(testString);
25
+        //Then
26
+        Assert.assertEquals(expected, actual);
27
+
18
     }
28
     }
19
 
29
 
20
     @Test
30
     @Test
21
     public void testChangeHoratioToTariq() {
31
     public void testChangeHoratioToTariq() {
32
+        //Given
33
+        HamletParser testHamletParser = new HamletParser();
34
+        String testString = "Horatio. HORATIO. Horatio!";
35
+        //When
36
+        String expected = "Tariq. TARIQ. Tariq!";
37
+        String actual = testHamletParser.changeAll(testString);
38
+        //Then
39
+        Assert.assertEquals(expected, actual);
40
+    }
41
+
42
+    @Test
43
+    public void testChangeAll() {
44
+        //Given
45
+        HamletParser testHamletParser = new HamletParser();
46
+        String testString = "Horatio. HORATIO. Horatio! Hamlet. HAMLET. Hamlet!";
47
+        //When
48
+        String expected = "Tariq. TARIQ. Tariq! Leon. LEON. Leon!";
49
+        String actual = testHamletParser.changeAll(testString);
50
+        //Then
51
+        Assert.assertEquals(expected, actual);
22
     }
52
     }
23
 
53
 
24
     @Test
54
     @Test
25
     public void testFindHoratio() {
55
     public void testFindHoratio() {
56
+        //Given
57
+        HamletParser testHamletParser = new HamletParser();
58
+        String testString = "Horatio. HORATIO. Horatio!";
59
+
60
+        //When
61
+        Integer expected = 158;
62
+        Integer actual = testHamletParser.findHoratio(hamletText);
63
+        //Then
64
+        Assert.assertEquals(expected, actual);
26
     }
65
     }
27
 
66
 
28
     @Test
67
     @Test
29
     public void testFindHamlet() {
68
     public void testFindHamlet() {
69
+        //Given
70
+        HamletParser testHamletParser = new HamletParser();
71
+
72
+        //When
73
+        Integer expected = 472;
74
+        Integer actual = testHamletParser.findHalmet(hamletText);
75
+
76
+        //Then
77
+        Assert.assertEquals(expected, actual);
30
     }
78
     }
31
 }
79
 }

+ 5383
- 0
target/classes/hamlet.txt
Plik diff jest za duży
Wyświetl plik