Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 66 additions & 7 deletions src/main/java/HamletParser.java
Original file line number Diff line number Diff line change
@@ -1,39 +1,98 @@
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* Created by thook on 10/7/15.
*/
public class HamletParser {

private String hamletData;
private Pattern thePattern;
private Matcher m;
private String s;

public HamletParser(){
public HamletParser() {
this.hamletData = loadFile();
}

private String loadFile(){
private String loadFile() {
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("hamlet.txt").getFile());
StringBuilder result = new StringBuilder("");

try(Scanner scanner = new Scanner(file)){
while(scanner.hasNextLine()){
try (Scanner scanner = new Scanner(file)) {
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
result.append(line).append("\n");
}

scanner.close();
}catch(IOException e){
} catch (IOException e) {
e.printStackTrace();
}

return result.toString();
}

public String getHamletData(){
public String getHamletData() {
return hamletData;
}

//Testing
public String changeHamletToLeon(String name) {
s = null;
if (findHamlet(name)) {
s = m.replaceAll("Leon");
}
return s;
}

public String changeHamletToLeon() {
s = null;
if (find("Hamlet")) {
s = m.replaceAll("Leon");
}
return s;
}


public String changeHoratioToTariq(String name) {
s = null;
if (findHoratio(name)) {
s = m.replaceAll("Tariq");
}
return s;
}


public boolean findHamlet(String name) {
thePattern = Pattern.compile("Hamlet");
m = thePattern.matcher((name));
return m.find();
}

public boolean findHoratio(String name) {
thePattern = Pattern.compile(("Horatio"));
m = thePattern.matcher((name));
return m.find();
}

public boolean find(String name) {
thePattern = Pattern.compile((name));
m = thePattern.matcher(getHamletData());
return m.find();
}


}









28 changes: 28 additions & 0 deletions src/test/java/HamletParserTest.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

Expand All @@ -15,17 +16,44 @@ public void setUp() {

@Test
public void testChangeHamletToLeon() {
String name = "Hamlet";
String expected = "Leon";
String actual = this.hamletParser.changeHamletToLeon(name);
Assert.assertEquals(expected, actual);
}

@Test
public void testChangeHamletToLeonFalse() {
String name = "Tim";
String expected = null;
String actual = this.hamletParser.changeHamletToLeon(name);
Assert.assertEquals(expected, actual);
}

@Test
public void testChangeHoratioToTariq() {
String name = "Horatio";
String expected = "Tariq";
String actual = this.hamletParser.changeHoratioToTariq(name);
Assert.assertEquals(expected, actual);

}

@Test
public void testFindHoratio() {
String hor = "Horatio";
boolean expected = true;
boolean actual = this.hamletParser.findHoratio(hor);
Assert.assertEquals(expected, actual);
}

@Test
public void testFindHamlet() {

boolean expected = true;
boolean actual = this.hamletParser.findHamlet("Hamlet");
Assert.assertEquals(expected, actual);
}


}
Loading