Skip to content

completed #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
65 changes: 59 additions & 6 deletions src/main/java/HamletParser.java
Original file line number Diff line number Diff line change
@@ -1,39 +1,92 @@
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 int count = 0;

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

public String changeHamletToLeon(String text) {

Pattern replacement = Pattern.compile("Hamlet");
Matcher match = replacement.matcher(text);
String hamletData1 = match.replaceAll("Leon");

Pattern replacement1 = Pattern.compile("HAMLET");
Matcher match1 = replacement1.matcher(hamletData1);
String hamletData2 = match1.replaceAll("LEON");

return hamletData2;
}

public String changeHoratioToTariq(String text) {

Pattern replacement = Pattern.compile("Horatio");
Matcher match = replacement.matcher(text);
String hamletData1 = match.replaceAll("Tariq");

Pattern replacement1 = Pattern.compile("HORATIO");
Matcher match1 = replacement1.matcher(hamletData1);
String hamletData2 = match1.replaceAll("TARIQ");

return hamletData2;
}


private int findMatch(String regexSearch, String text) {
Pattern checkName = Pattern.compile(regexSearch);
Matcher regexCheck = checkName.matcher(text);

while (regexCheck.find()) {
if (regexCheck.group().length() != 0) {
count++;
}
}
return count;

}
public int findHoratio() {

return findMatch("[H|h|][O|o][R|r][A|a][T|t][I|i][O|o]", hamletData);

}

public int findHamlet() {

return findMatch("[H|h|][A|a][M|m][L|l][E|e][T|t]", hamletData);
}
}
4 changes: 4 additions & 0 deletions src/main/resources/hamletTest.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Hamlet hamlet froilan hamlet hamlet
horatio horatio horatio
tariq tariq tariq
froilan froilan froilan
26 changes: 26 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,42 @@ public void setUp() {

@Test
public void testChangeHamletToLeon() {
//Given
String test = "Hamlet Hamlet HAMLET";
//When
String expected = "Leon Leon LEON";
String actual = hamletParser.changeHamletToLeon(test);
//THen
Assert.assertEquals(expected, actual);
}

@Test
public void testChangeHoratioToTariq() {
//Given
String test = "Hamlet Hamlet HAMLET";
//When
String expected = "Leon Leon LEON";
String actual = hamletParser.changeHamletToLeon(test);
//THen
Assert.assertEquals(expected, actual);
}

@Test
public void testFindHoratio() {
//When
int actual = 158;
int expected = hamletParser.findHoratio();
//Then
Assert.assertEquals(expected, actual);
}

@Test
public void testFindHamlet() {
//When
int actual = 472;
int expected = hamletParser.findHamlet();
//Then
Assert.assertEquals(expected, actual);
}

}
Loading