Browse Source

Working Copy

Nick Satinover 6 years ago
parent
commit
04dd0976f3

+ 88
- 52
src/main/java/HamletParser.java View File

@@ -3,6 +3,9 @@ import java.io.IOException;
3 3
 import java.util.Scanner;
4 4
 import java.lang.*;
5 5
 import java.util.*;
6
+import java.util.regex.Pattern;
7
+import java.util.regex.Matcher;
8
+
6 9
 /**
7 10
  * Created by thook on 10/7/15.
8 11
  */
@@ -10,92 +13,125 @@ public class HamletParser {
10 13
 
11 14
     private String hamletData;
12 15
     private Formatter formatter;
16
+    private String hamletFile = "hamlet.txt";
13 17
 
14 18
     public HamletParser(){
15
-        this.hamletData = loadFile();
19
+        // this.hamletData = loadFile(fileName);
20
+    }
21
+
22
+    public String getHamletFile() {
23
+        return hamletFile;
16 24
     }
17 25
 
18
-    private String loadFile(){
26
+    protected String loadFile(String fileName){
19 27
         ClassLoader classLoader = getClass().getClassLoader();
20
-        File file = new File(classLoader.getResource("hamlet.txt").getFile());
28
+        File file = new File(classLoader.getResource(fileName).getFile());
21 29
         StringBuilder result = new StringBuilder("");
22
-        StringBuilder lineResult;
23
-        String[] wordArr;
24 30
 
25 31
         try(Scanner scanner = new Scanner(file)){
26 32
             while(scanner.hasNextLine()){
27
-                String line = scanner.nextLine();
28
-                wordArr = line.split(" ");
29
-                lineResult = new StringBuilder("");
30
-                for (String s: wordArr) {
31
-                    if (findHamlet(s)) {
32
-                        lineResult.append(changeHamletToLeon()).append(" ");
33
-                    } else if (findHoratio(s)) {
34
-                        lineResult.append(changeChangeHoratioToTariq()).append(" ");
35
-                    } else {
36
-                        lineResult.append(s).append(" ");
37
-                    }
38
-                }
39
-                result.append(lineResult.toString().trim());
33
+                String line = replaceHamletAndHoratio(scanner.nextLine());
34
+                result.append(line).append("\n");
40 35
             }
41
-
42 36
             scanner.close();
43 37
         }catch(IOException e){
44 38
             e.printStackTrace();
45 39
         }
46
-        openOutFile();
47
-        addText(result.toString());
48
-        closeOutFile();
49 40
         return result.toString();
50 41
     }
42
+//    protected String loadFile(){
43
+//        ClassLoader classLoader = getClass().getClassLoader();
44
+//        File file = new File(classLoader.getResource("hamlet.txt").getFile());
45
+//        StringBuilder result = new StringBuilder("");
46
+//        StringBuilder lineResult;
47
+//        String[] wordArr;
48
+//
49
+//        try(Scanner scanner = new Scanner(file)){
50
+//            while(scanner.hasNextLine()){
51
+//                String line = scanner.nextLine();
52
+//                wordArr = line.split(" ");
53
+//                lineResult = new StringBuilder("");
54
+//                for (String s: wordArr) {
55
+//                    if (findHamlet(s)) {
56
+//                        lineResult.append(changeHamletToLeon()).append(" ");
57
+//                    } else if (findHoratio(s)) {
58
+//                        lineResult.append(changeChangeHoratioToTariq()).append(" ");
59
+//                    } else {
60
+//                        lineResult.append(s).append(" ");
61
+//                    }
62
+//                }
63
+//                result.append(lineResult.toString().trim() + "\n");
64
+//            }
65
+//
66
+//            scanner.close();
67
+//        }catch(IOException e){
68
+//            e.printStackTrace();
69
+//        }
70
+//        openOutFile();
71
+//        addText(result.toString());
72
+//        closeOutFile();
73
+//        return result.toString();
74
+//    }
51 75
 
52 76
     public String getHamletData(){
53 77
         return hamletData;
54 78
     }
55 79
 
56
-    public boolean findHamlet(String s){
57
-        if (s.toLowerCase().matches("hamlet")){
58
-            return true;
59
-        } else {
60
-            return false;
61
-        }
62
-    }
63
-
64
-    public boolean findHoratio(String s){
65
-        if (s.toLowerCase().matches("horatio")){
66
-            return true;
67
-        } else {
68
-            return false;
69
-        }
70
-    }
71
-
72
-    public String changeHamletToLeon(){
73
-        return "Leon";
74
-    }
75
-
76
-    public String changeChangeHoratioToTariq(){
77
-        return "Tariq";
78
-    }
80
+//    public boolean findHamlet(String s){
81
+//        if (s.toLowerCase().matches("hamlet")){
82
+//            return true;
83
+//        } else {
84
+//            return false;
85
+//        }
86
+//    }
87
+//
88
+//    public boolean findHoratio(String s){
89
+//        if (s.toLowerCase().matches("horatio")){
90
+//            return true;
91
+//        } else {
92
+//            return false;
93
+//        }
94
+//    }
95
+//
96
+//    public String changeHamletToLeon(){
97
+//        return "Leon";
98
+//    }
99
+//
100
+//    public String changeChangeHoratioToTariq(){
101
+//        return "Tariq";
102
+//    }
79 103
 
80 104
     /**
81 105
      * Methods to print out to a new file
82 106
      */
83
-    public void openOutFile(){
107
+    public void openOutFile(String fileName){
84 108
         try {
85
-            formatter = new Formatter("output.txt");
109
+            formatter = new Formatter(fileName);
86 110
         }
87 111
         catch (Exception e){
88 112
             System.out.println("Error writing to file.");
89 113
         }
90 114
     }
91
-
92
-    public String addText(String s){
93
-        formatter.format("%s", s);
94
-        return loadFile();
115
+    public String writeToFile(String output){
116
+        formatter.format("%s", output);
117
+        return output;
95 118
     }
96
-
97 119
     public void closeOutFile(){
98 120
         formatter.close();
99 121
     }
100 122
 
123
+    public String replaceHamletAndHoratio(String input) {
124
+//        String noHamlet = input.replaceAll("(?i)hamlet", "Leon");
125
+//        String noHoratioOrHamlet = noHamlet.replaceAll("(?i)horatio", "Tarik");
126
+//        return noHoratioOrHamlet;
127
+        String returnString = input;
128
+        String[] replaceArr = {"Hamlet", "Horatio", "hamlet", "horatio", "HAMLET", "HORATIO"};
129
+        String[] changeToArr = {"Leon", "Tarik", "leon", "tarik", "LEON", "TARIK"};
130
+
131
+        for (int i = 0; i < replaceArr.length; i++){
132
+            returnString = returnString.replaceAll(replaceArr[i], changeToArr[i]);
133
+        }
134
+        return returnString;
135
+    }
136
+
101 137
 }

+ 10
- 0
src/main/java/MainApp.java View File

@@ -0,0 +1,10 @@
1
+public class MainApp {
2
+    public static void main(String[] args) {
3
+        String noHamletOrHoratio;
4
+        HamletParser hamletParser = new HamletParser();
5
+        noHamletOrHoratio = hamletParser.loadFile(hamletParser.getHamletFile());
6
+        hamletParser.openOutFile("output.txt");
7
+        hamletParser.writeToFile(noHamletOrHoratio);
8
+        hamletParser.closeOutFile();
9
+    }
10
+}

+ 2
- 0
src/main/resources/hamletTest.txt View File

@@ -0,0 +1,2 @@
1
+The Tragedy of Hamlet, Prince of Denmark
2
+HORATIO

+ 73
- 49
src/test/java/HamletParserTest.java View File

@@ -15,68 +15,92 @@ public class HamletParserTest {
15 15
     }
16 16
 
17 17
     @Test
18
-    public void testChangeHamletToLeon() {
18
+    public void loadFileTest() {
19 19
         //GIVEN
20 20
 
21 21
         //WHEN
22
-        String expected = "Leon";
22
+        String expected = "The Tragedy of Leon, Prince of Denmark\nTARIK\n";
23 23
         //THEN
24
-        String actual = hamletParser.changeHamletToLeon();
24
+        String actual = hamletParser.loadFile("hamletTest.txt");
25 25
         Assert.assertEquals(expected, actual);
26
-    }
27
-
28
-    @Test
29
-    public void testChangeHoratioToTariq() {
30
-        //GIVEN
31
-
32
-        //WHEN
33
-        String expected = "Tariq";
34
-        //THEN
35
-        String actual = hamletParser.changeChangeHoratioToTariq();
36
-        Assert.assertEquals(expected, actual);
37
-    }
38 26
 
39
-    @Test
40
-    public void testFindHoratio() {
41
-        //GIVEN
42
-        String input = "horatio";
43
-        //WHEN
44
-        boolean expected = true;
45
-        //THEN
46
-        boolean actual = hamletParser.findHoratio(input);
47
-        Assert.assertEquals(expected, actual);
27
+        // the file to output, then test output for 'has hamlet or horatio
48 28
     }
49 29
 
50
-    @Test
51
-    public void testFindHamlet() {
52
-        //GIVEN
53
-        String input = "HAMLET";
54
-        //WHEN
55
-        boolean expected = true;
56
-        //THEN
57
-        boolean actual = hamletParser.findHamlet(input);
58
-        Assert.assertEquals(expected, actual);
59
-    }
60
-
61
-    @Test
62
-    public void testFindHoratio2() {
63
-        //GIVEN
64
-        String input = "nope";
65
-        //WHEN
66
-        boolean expected = false;
67
-        //THEN
68
-        boolean actual = hamletParser.findHoratio(input);
69
-        Assert.assertEquals(expected, actual);
70
-    }
30
+//    @Test
31
+//    public void testChangeHamletToLeon() {
32
+//        //GIVEN
33
+//
34
+//        //WHEN
35
+//        String expected = "Leon";
36
+//        //THEN
37
+//        String actual = hamletParser.changeHamletToLeon();
38
+//        Assert.assertEquals(expected, actual);
39
+//    }
40
+//
41
+//    @Test
42
+//    public void testChangeHoratioToTariq() {
43
+//        //GIVEN
44
+//
45
+//        //WHEN
46
+//        String expected = "Tariq";
47
+//        //THEN
48
+//        String actual = hamletParser.changeChangeHoratioToTariq();
49
+//        Assert.assertEquals(expected, actual);
50
+//    }
51
+//
52
+//    @Test
53
+//    public void testFindHoratio() {
54
+//        //GIVEN
55
+//        String input = "horatio";
56
+//        //WHEN
57
+//        boolean expected = true;
58
+//        //THEN
59
+//        boolean actual = hamletParser.findHoratio(input);
60
+//        Assert.assertEquals(expected, actual);
61
+//    }
62
+//
63
+//    @Test
64
+//    public void testFindHamlet() {
65
+//        //GIVEN
66
+//        String input = "HAMLET";
67
+//        //WHEN
68
+//        boolean expected = true;
69
+//        //THEN
70
+//        boolean actual = hamletParser.findHamlet(input);
71
+//        Assert.assertEquals(expected, actual);
72
+//    }
73
+//
74
+//    @Test
75
+//    public void testFindHoratio2() {
76
+//        //GIVEN
77
+//        String input = "nope";
78
+//        //WHEN
79
+//        boolean expected = false;
80
+//        //THEN
81
+//        boolean actual = hamletParser.findHoratio(input);
82
+//        Assert.assertEquals(expected, actual);
83
+//    }
84
+//
85
+//    @Test
86
+//    public void testFindHamlet2() {
87
+//        //GIVEN
88
+//        String input = "hhamlet";
89
+//        //WHEN
90
+//        boolean expected = false;
91
+//        //THEN
92
+//        boolean actual = hamletParser.findHamlet(input);
93
+//        Assert.assertEquals(expected, actual);
94
+//    }
71 95
 
72 96
     @Test
73
-    public void testFindHamlet2() {
97
+    public void hamletReplaceTest() {
74 98
         //GIVEN
75
-        String input = "hhamlet";
99
+        String input = "Hamlet Horatio hamlet horatio HAMLET HORATIO";
76 100
         //WHEN
77
-        boolean expected = false;
101
+        String expected = "Leon Tarik leon tarik LEON TARIK";
78 102
         //THEN
79
-        boolean actual = hamletParser.findHamlet(input);
103
+        String actual = hamletParser.replaceHamletAndHoratio(input);
80 104
         Assert.assertEquals(expected, actual);
81 105
     }
82 106
 }