Skip to content

Done? #25

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
46 changes: 39 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,61 @@ 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 void changeIfHamlet() {
Pattern p = Pattern.compile("(Hamlet| HAMLET).*");
Matcher m = p.matcher(hamletData);
if (m.find()) {
hamletData = m.replaceAll("Leon");
} else if (m.find()){
hamletData = m.replaceAll("LEON");
}
}

public void changeIfHoratio(){
Pattern p = Pattern.compile("(Horatio | HORATIO).*");
Matcher m = p.matcher(hamletData);
if (m.find()){
hamletData = m.replaceAll("Tariq");
} else if (m.find()) {
hamletData = m.replaceAll("TARIQ");
}
}
public boolean findHamlet(){
Pattern p = Pattern.compile("(Hamlet| HAMLET).*");
Matcher m = p.matcher(hamletData);
return m.find();
}
public boolean findHoratio(){
Pattern p = Pattern.compile("(Horatio | HORATIO).*");
Matcher m = p.matcher(hamletData);
return m.find();
}
}

Empty file.
34 changes: 32 additions & 2 deletions src/test/java/HamletParserTest.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.*;

public class HamletParserTest {
private String hamletText;
private HamletParser hamletParser;
public HamletParser hamletParser;

@Before
public void setUp() {
Expand All @@ -15,17 +15,47 @@ public void setUp() {

@Test
public void testChangeHamletToLeon() {
//Given
HamletParser hamletParser = new HamletParser();
String before = hamletParser.getHamletData();
//When
hamletParser.changeIfHamlet();
String after = hamletParser.getHamletData();
//Then
Assert.assertNotEquals(before, after);
}

@Test
public void testChangeHoratioToTariq() {
//Given
HamletParser hamletParser = new HamletParser();
String before = hamletParser.getHamletData();
//When
hamletParser.changeIfHoratio();
String after = hamletParser.getHamletData();
//Then
Assert.assertNotEquals(before, after);
}

@Test
public void testFindHoratio() {
//Given
HamletParser hamletParser = new HamletParser();
String s = "Horatio";
//When
hamletParser.findHoratio();
//Then
Assert.assertTrue(s, hamletParser.findHoratio());
}

@Test
public void testFindHamlet() {
//Given
HamletParser hamletParser = new HamletParser();
String s = "Hamlet";
//When
hamletParser.findHamlet();
//Then
Assert.assertTrue(s, hamletParser.findHamlet());
}
}
Loading