Преглед на файлове

Merge a0205240fff870968ec27c640523c44f80dc2971 into 99c0c83b2f011a8e58c86fca53df6bfa67c86565

vvg3 преди 6 години
родител
ревизия
b2bea7b518
No account linked to committer's email
променени са 3 файла, в които са добавени 5435 реда и са изтрити 6 реда
  1. 42
    6
      src/main/java/HamletParser.java
  2. 10
    0
      src/test/java/HamletParserTest.java
  3. 5383
    0
      target/classes/hamlet.txt

+ 42
- 6
src/main/java/HamletParser.java Целия файл

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.
8
 public class HamletParser {
10
 public class HamletParser {
9
 
11
 
10
     private String hamletData;
12
     private String hamletData;
13
+    private Pattern pattern;
14
+    private Matcher matcher;
11
 
15
 
12
-    public HamletParser(){
16
+    public HamletParser() {
13
         this.hamletData = loadFile();
17
         this.hamletData = loadFile();
14
     }
18
     }
15
 
19
 
16
-    private String loadFile(){
20
+    public void changeHamletToLeon() {
21
+        String hamlet = "(Hamlet|HAMLET)";
22
+        this.hamletData = change(hamlet, "LEON");
23
+    }
24
+
25
+    public void changeHoratioToTariq() {
26
+        String horatio = "(Horatio|HORATIO)";
27
+        this.hamletData = change(horatio, "TARIQ");
28
+    }
29
+
30
+    public boolean findHoratio() {
31
+        String horatio = "(Horatio|HORATIO)";
32
+        return find(horatio);
33
+    }
34
+
35
+    public boolean findHamlet() {
36
+        String hamlet = "(Hamlet|HAMLET)";
37
+        return find(hamlet);
38
+    }
39
+
40
+    private boolean find(String aString) {
41
+        pattern = Pattern.compile(aString);
42
+        matcher = pattern.matcher(hamletData);
43
+        return matcher.find();
44
+    }
45
+
46
+    private String change(String original, String convertTo) {
47
+        pattern = Pattern.compile(original);
48
+        matcher = pattern.matcher(hamletData);
49
+        return matcher.replaceAll(convertTo);
50
+    }
51
+
52
+    private String loadFile() {
17
         ClassLoader classLoader = getClass().getClassLoader();
53
         ClassLoader classLoader = getClass().getClassLoader();
18
         File file = new File(classLoader.getResource("hamlet.txt").getFile());
54
         File file = new File(classLoader.getResource("hamlet.txt").getFile());
19
         StringBuilder result = new StringBuilder("");
55
         StringBuilder result = new StringBuilder("");
20
 
56
 
21
-        try(Scanner scanner = new Scanner(file)){
22
-            while(scanner.hasNextLine()){
57
+        try (Scanner scanner = new Scanner(file)) {
58
+            while (scanner.hasNextLine()) {
23
                 String line = scanner.nextLine();
59
                 String line = scanner.nextLine();
24
                 result.append(line).append("\n");
60
                 result.append(line).append("\n");
25
             }
61
             }
26
 
62
 
27
             scanner.close();
63
             scanner.close();
28
-        }catch(IOException e){
64
+        } catch (IOException e) {
29
             e.printStackTrace();
65
             e.printStackTrace();
30
         }
66
         }
31
 
67
 
32
         return result.toString();
68
         return result.toString();
33
     }
69
     }
34
 
70
 
35
-    public String getHamletData(){
71
+    public String getHamletData() {
36
         return hamletData;
72
         return hamletData;
37
     }
73
     }
38
 
74
 

+ 10
- 0
src/test/java/HamletParserTest.java Целия файл

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
+        hamletParser.changeHamletToLeon();
20
+        System.out.println(hamletParser.getHamletData());
21
+        Assert.assertFalse(hamletParser.findHamlet());
18
     }
22
     }
19
 
23
 
20
     @Test
24
     @Test
21
     public void testChangeHoratioToTariq() {
25
     public void testChangeHoratioToTariq() {
26
+        hamletParser.changeHoratioToTariq();
27
+        System.out.println(hamletParser.getHamletData());
28
+        Assert.assertFalse(hamletParser.findHoratio());
22
     }
29
     }
23
 
30
 
24
     @Test
31
     @Test
25
     public void testFindHoratio() {
32
     public void testFindHoratio() {
33
+        Assert.assertTrue(hamletParser.findHoratio());
34
+
26
     }
35
     }
27
 
36
 
28
     @Test
37
     @Test
29
     public void testFindHamlet() {
38
     public void testFindHamlet() {
39
+        Assert.assertTrue(hamletParser.findHoratio());
30
     }
40
     }
31
 }
41
 }

+ 5383
- 0
target/classes/hamlet.txt
Файловите разлики са ограничени, защото са твърде много
Целия файл