Skip to content

Alas, poor Yorick #23

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
50 changes: 43 additions & 7 deletions src/main/java/HamletParser.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
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.
Expand All @@ -9,31 +11,65 @@ public class HamletParser {

private String hamletData;

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 input) {
Pattern smallCase = Pattern.compile("Hamlet");
Matcher m1 = smallCase.matcher(input);
String result1 = m1.replaceAll("Leon");

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

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

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

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

public boolean findHoratio(String input) {
if (hamletData.contains("HORATIO") || hamletData.contains("Horatio")) {
}
return true;
}
}
28 changes: 27 additions & 1 deletion 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
HamletParser testHamletParser = new HamletParser();
String input = "Hamlet. HAMLET. Hamlet!";

//When
String expected = "Leon. LEON. Leon!";
String actual = testHamletParser.changeHamletToLeon(input);

//Then
Assert.assertEquals(expected,actual);
}

@Test
public void testChangeHoratioToTariq() {
//Given
HamletParser testHamletParser = new HamletParser();
String input = "Horatio. HORATIO. Horatio!";

//When
String expected = "Tariq. TARIQ. Tariq!";
String actual = testHamletParser.changeHoratioToTariq(input);

//Then
Assert.assertEquals(expected,actual);
}

@Test
public void testFindHoratio() {
boolean expected = true;
boolean actual = hamletParser.findHamlet("Horatio found");
assertEquals(expected, actual);
}

@Test
public void testFindHamlet() {
boolean expected = true;
boolean actual = hamletParser.findHamlet("Hamlet found");
assertEquals(expected, actual);
}
}
Loading