浏览代码

Merge f87afeabb2b6006c6f81dc65248284577e961dfd into 99c0c83b2f011a8e58c86fca53df6bfa67c86565

Mitch Taylor 6 年前
父节点
当前提交
f8cac895f7
没有帐户链接到提交者的电子邮件

+ 39
- 9
src/main/java/HamletParser.java 查看文件

@@ -1,39 +1,69 @@
1 1
 import java.io.File;
2
+import java.io.FileWriter;
2 3
 import java.io.IOException;
3 4
 import java.util.Scanner;
5
+import java.util.regex.Matcher;
6
+import java.util.regex.Pattern;
4 7
 
5 8
 /**
6 9
  * Created by thook on 10/7/15.
7 10
  */
8 11
 public class HamletParser {
9 12
 
10
-    private String hamletData;
13
+    private static String hamletData;
11 14
 
12
-    public HamletParser(){
13
-        this.hamletData = loadFile();
15
+    HamletParser(){
16
+        hamletData = loadFile();
14 17
     }
15 18
 
16
-    private String loadFile(){
19
+    public String loadFile(){
17 20
         ClassLoader classLoader = getClass().getClassLoader();
18 21
         File file = new File(classLoader.getResource("hamlet.txt").getFile());
19 22
         StringBuilder result = new StringBuilder("");
20
-
21 23
         try(Scanner scanner = new Scanner(file)){
22 24
             while(scanner.hasNextLine()){
23 25
                 String line = scanner.nextLine();
24 26
                 result.append(line).append("\n");
25 27
             }
26
-
27 28
             scanner.close();
28 29
         }catch(IOException e){
29 30
             e.printStackTrace();
30 31
         }
31
-
32 32
         return result.toString();
33 33
     }
34 34
 
35
-    public String getHamletData(){
36
-        return hamletData;
35
+    public static String changeHamletToLeon(String string) {
36
+        StringBuffer sb = new StringBuffer();
37
+        String hamletPattern = "([Hh][Aa][Mm][Ll][Ee][Tt])";
38
+        Pattern pattern = Pattern.compile(hamletPattern);
39
+        Matcher matcher = pattern.matcher(string);
40
+        while (matcher.find()) {
41
+            matcher.appendReplacement(sb, "Leon");
42
+        }
43
+        return matcher.appendTail(sb).toString();
44
+    }
45
+
46
+    public static String changeHoratioToTariq(String string) {
47
+        StringBuffer sb = new StringBuffer();
48
+        String horatioPattern = "([Hh][Oo][Rr][Aa][Tt][Ii][Oo])";
49
+        Pattern pattern = Pattern.compile(horatioPattern);
50
+        Matcher matcher = pattern.matcher(string);
51
+        while (matcher.find()) {
52
+            matcher.appendReplacement(sb, "Tariq");
53
+        }
54
+        return matcher.appendTail(sb).toString();
55
+    }
56
+
57
+    private void changeEverythingAndPutInFile() throws IOException {
58
+        String fixedFile = changeHamletToLeon(changeHoratioToTariq(hamletData));
59
+        FileWriter fileWriter = new FileWriter("src/main/resources/zipcodeHamlet.txt", true);
60
+        fileWriter.write(fixedFile);
61
+        fileWriter.close();
62
+    }
63
+
64
+    public static void main(String[] args) throws IOException {
65
+        HamletParser h = new HamletParser();
66
+        h.changeEverythingAndPutInFile();
37 67
     }
38 68
 
39 69
 }

+ 5383
- 0
src/main/resources/zipcodeHamlet.txt
文件差异内容过多而无法显示
查看文件


+ 14
- 7
src/test/java/HamletParserTest.java 查看文件

@@ -1,3 +1,4 @@
1
+import org.junit.Assert;
1 2
 import org.junit.Before;
2 3
 import org.junit.Test;
3 4
 
@@ -10,22 +11,28 @@ public class HamletParserTest {
10 11
     @Before
11 12
     public void setUp() {
12 13
         this.hamletParser = new HamletParser();
13
-        this.hamletText = hamletParser.getHamletData();
14 14
     }
15 15
 
16 16
     @Test
17 17
     public void testChangeHamletToLeon() {
18
+        String startingString = "Here are some words Hamlet and here are some words Horatio.\nHere are more.";
19
+        String expected = "Here are some words Leon and here are some words Horatio.\nHere are more.";
20
+        String actual = hamletParser.changeHamletToLeon(startingString);
21
+        Assert.assertEquals(expected, actual);
18 22
     }
19 23
 
20 24
     @Test
21 25
     public void testChangeHoratioToTariq() {
26
+        String startingString = "Here are some words Hamlet and here are some words Horatio.";
27
+        String expected = "Here are some words Hamlet and here are some words Tariq.";
28
+        String actual = hamletParser.changeHoratioToTariq(startingString);
29
+        Assert.assertEquals(expected, actual);
22 30
     }
23 31
 
24
-    @Test
25
-    public void testFindHoratio() {
26
-    }
32
+//    @Test
33
+//    public void testLoadFile() {
34
+//        String expected = ;
35
+//        String actual = hamletParser.loadFile();
36
+//    }
27 37
 
28
-    @Test
29
-    public void testFindHamlet() {
30
-    }
31 38
 }

+ 5383
- 0
target/classes/hamlet.txt
文件差异内容过多而无法显示
查看文件


+ 0
- 0
target/classes/zipcodeHamlet.txt 查看文件