Skip to content

done #27

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 3 commits into
base: master
Choose a base branch
from
Open

done #27

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
72 changes: 66 additions & 6 deletions src/main/java/HamletParser.java
Original file line number Diff line number Diff line change
@@ -1,39 +1,99 @@
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 pattern;
// private Matcher matcher;

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 replaceHamletWithLeon(String input) {

Pattern patternLowerCase = Pattern.compile("Hamlet");
Matcher m1 = patternLowerCase.matcher(input);
String result1 = m1.replaceAll("Leon");

Pattern patternUpperCase = Pattern.compile("HAMLET");
Matcher m2 = patternUpperCase.matcher(result1);
String result2 = m2.replaceAll("LEON");

return result2;

}

public String replaceHoratioWithTariq(String input) {
Pattern patternLowerCase = Pattern.compile("Horatio");
Matcher m1 = patternLowerCase.matcher(input);
String result1 = m1.replaceAll("Tariq");

Pattern patternUpperCase = Pattern.compile("HORATIO");
Matcher m2 = patternUpperCase.matcher(result1);
String result2 = m2.replaceAll("TARIQ");

return result2;

}

public boolean findHamlet(String input)
{
if (input.contains("Hamlet") || input.contains("HAMLET"))
{
return true;
}

return false;
}

public boolean findHoratio(String input)
{
if (input.contains("Horatio") || input.contains("HORATIO"))
{
return true;
}

return false;

}

public String getHamletData()
{
return hamletData;
}





}
80 changes: 76 additions & 4 deletions src/test/java/HamletParserTest.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.omg.Messaging.SYNC_WITH_TRANSPORT;

import java.util.regex.Pattern;
import java.util.regex.Matcher;

import static org.junit.Assert.*;


public class HamletParserTest {
private String hamletText;
private HamletParser hamletParser;
Expand All @@ -14,18 +20,84 @@ public void setUp() {
}

@Test
public void testChangeHamletToLeon() {
public void testChangeHamletToLeon1()
{
HamletParser hamletParser = new HamletParser();

String input = "This is Hamlet.";

String expected = "This is Leon.";

String actual = hamletParser.replaceHamletWithLeon(input);

Assert.assertEquals(expected, actual);

}

@Test
public void testChangeHoratioToTariq() {
public void testChangeHamletToLeon2()
{
HamletParser hamletParser = new HamletParser();

String testString = "This is HAMLET.";

String expected = "This is LEON.";

String actual = hamletParser.replaceHamletWithLeon(testString);

Assert.assertEquals(expected, actual);

}



@Test
public void testFindHoratio() {
public void testChangeHoratioToTariq()
{
HamletParser hamletParser = new HamletParser();

String testString = "This is Horatio.";

String expected = "This is Tariq.";

String actual = hamletParser.replaceHoratioWithTariq(testString);

Assert.assertEquals(expected, actual);

}

@Test
public void testFindHoratio1()
{
boolean expected = true;
boolean actual = hamletParser.findHoratio("Horatio is here");
Assert.assertEquals(expected, actual);
}

@Test
public void testFindHamlet() {
public void testFindHoratio2()
{
boolean expected = true;
boolean actual = hamletParser.findHoratio("HORATIO is here");
Assert.assertEquals(expected, actual);
}


@Test
public void testFindHamlet1()
{
boolean expected = true;
boolean actual = hamletParser.findHamlet("Hamlet is here");
Assert.assertEquals(expected, actual);

}

@Test
public void testFindHamlet2()
{
boolean expected = true;
boolean actual = hamletParser.findHamlet("HAMLET is here");
Assert.assertEquals(expected, actual);

}
}
Loading