Skip to content

done #28

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

done #28

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
77 changes: 77 additions & 0 deletions src/main/java/HamletParser.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,29 @@


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[] words;
private String hamletData;

public HamletParser(){

this.hamletData = loadFile();
}

public HamletParser(String input){
this.hamletData = input;
this.words = input.split("\\W");
}

private String loadFile(){
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("hamlet.txt").getFile());
Expand All @@ -32,8 +43,74 @@ private String loadFile(){
return result.toString();
}

//I know I need pattern and I know I need matching
// public static void changeHamletToLeon(String hamlet, String file2Check){
//
// Pattern checkHamletRegex = Pattern.compile(hamlet);
// Matcher regexMatcher = checkHamletRegex.matcher(file2Check);
//
// while(regexMatcher.find()){
// if(regexMatcher.group().length()!=0){
//
// }
// }
// }


public String getHamletData(){

return hamletData;
}


public void changeHamletToLeon(){
String hamletText = "Hamlet";
String leonText = "Leon";

Pattern checkRegex = Pattern.compile(hamletText);
Matcher m = checkRegex.matcher(hamletData);

hamletData = m.replaceAll(leonText);
}

public void changeHoratioToTariq(){
String horatioText = "Horatio";
String tariqText = "Tariq";

Pattern checkRegex = Pattern.compile(horatioText);
Matcher m = checkRegex.matcher(hamletData);

hamletData = m.replaceAll(tariqText);
}

public boolean findHamlet(){
boolean findHamlet = false;

String hamletString = "Hamlet";

Pattern checkHamletRegex = Pattern.compile(hamletString);
Matcher regexHamletMatcher = checkHamletRegex.matcher(hamletData);

if(regexHamletMatcher.find()){
findHamlet = true;
}

return findHamlet;
}

public boolean findHoratio(){
boolean findHoratio = false;

String horatioString = "Horatio";

Pattern checkHoratioRegex = Pattern.compile(horatioString);
Matcher regexHoratioMatcher = checkHoratioRegex.matcher(hamletData);

if(regexHoratioMatcher.find()){
findHoratio = true;
}

return findHoratio;
}

}
39 changes: 39 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,55 @@ public void setUp() {

@Test
public void testChangeHamletToLeon() {
String input = "LAERTES wounds Hamlet; then in scuffling, they change rapiers, and Hamlet wounds LAERTES";
String expected = "LAERTES wounds Leon; then in scuffling, they change rapiers, and Leon wounds LAERTES";

HamletParser hamletParser = new HamletParser(input);

//When
hamletParser.changeHamletToLeon();

//Then
String actual = hamletParser.getHamletData();
Assert.assertEquals(expected, actual);

}

@Test
public void testChangeHoratioToTariq() {
String input = "Horatio needs to change to Horatio";
String expected = "Tariq needs to change to Tariq";

HamletParser hamletParser = new HamletParser(input);

//When
hamletParser.changeHoratioToTariq();

//Then
String actual = hamletParser.getHamletData();
Assert.assertEquals(expected, actual);
}

@Test
public void testFindHoratio() {
//Given
boolean expected = true;
HamletParser hamletParser = new HamletParser();
//When
boolean actual = hamletParser.findHamlet();
//Then
Assert.assertEquals(expected, actual);
}

@Test
public void testFindHamlet() {

//Given
boolean expected = true;
HamletParser hamletParser = new HamletParser();
//When
boolean actual = hamletParser.findHoratio();
//Then
Assert.assertEquals(expected, actual);
}
}
Loading