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
12 changes: 12 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@
<groupId>com.zipcodewilmington</groupId>
<artifactId>regex</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
Expand Down
37 changes: 30 additions & 7 deletions src/main/java/HamletParser.java
Original file line number Diff line number Diff line change
@@ -1,39 +1,62 @@

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;
String hamletData;

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

private String loadFile(){
String hamletPattern = "[Hh][Aa][Mm][Ll][Ee][Tt]";
Pattern hamletC = Pattern.compile(hamletPattern);


String horatioPattern = "[Hh][Oo][Rr][Aa][Tt][Ii][Oo]";
Pattern horatioC = Pattern.compile(horatioPattern);


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;
}

public String changeHamletToLeon(String input) {
Matcher hamletMatcher = hamletC.matcher(input);
return hamletMatcher.replaceAll("Leon");
}

public String changeHoratioToTariq(String input) {
Matcher horatioMatcher = horatioC.matcher(input);
return horatioMatcher.replaceAll("Tariq");
}


}
18 changes: 9 additions & 9 deletions src/test/java/HamletParserTest.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.*;

public class HamletParserTest {
private String hamletText;
Expand All @@ -15,17 +15,17 @@ public void setUp() {

@Test
public void testChangeHamletToLeon() {
String input = "Hamlet, HAMLET, HAMLET, hamlet, hamlet!";
String expected = "Leon, Leon, Leon, Leon, Leon!";
String actual = hamletParser.changeHamletToLeon(input);
Assert.assertEquals(expected, actual);
}

@Test
public void testChangeHoratioToTariq() {
}

@Test
public void testFindHoratio() {
}

@Test
public void testFindHamlet() {
String input = "HORATIO, HORATIO, Horatio, horatio!";
String expected = "Tariq, Tariq, Tariq, Tariq!";
String actual = hamletParser.changeHoratioToTariq(input);
Assert.assertEquals(expected, actual);
}
}
Loading