Skip to content

first commit #19

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
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
26 changes: 22 additions & 4 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 @@ -13,6 +15,10 @@ public HamletParser(){
this.hamletData = loadFile();
}

public String getHamletData(){
return hamletData;
}

private String loadFile(){
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("hamlet.txt").getFile());
Expand All @@ -23,17 +29,29 @@ private String loadFile(){
String line = scanner.nextLine();
result.append(line).append("\n");
}

scanner.close();
}catch(IOException e){
e.printStackTrace();
}

return result.toString();
}

public String getHamletData(){
return hamletData;
public String changeHamletToLeon(){
Pattern pattern1 = Pattern.compile("Hamlet");
Matcher matcher1 = pattern1.matcher(loadFile());
String leon = matcher1.replaceAll("Leon");
matcher1.reset(leon);
matcher1.usePattern(Pattern.compile("Horatio"));
return matcher1.replaceAll("Tariq");
}

//alternative method
public String changeName(String hamletName, String zcwName){
Pattern pattern = Pattern.compile(hamletName);
Matcher matcher = pattern.matcher(loadFile());
return matcher.replaceAll(zcwName);
}



}
23 changes: 18 additions & 5 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,29 @@ public void setUp() {

@Test
public void testChangeHamletToLeon() {
Assert.assertEquals(true, hamletText.contains("Hamlet"));
String replaced = hamletParser.changeHamletToLeon();
Assert.assertEquals(true, replaced.contains("Leon"));
Assert.assertEquals(false, replaced.contains("Hamlet"));
Assert.assertEquals(false, replaced.contains("Horatio"));
Assert.assertEquals(true, replaced.contains("Tariq"));
}

@Test
public void testChangeHoratioToTariq() {
Assert.assertTrue(hamletText.contains("Horatio"));
String replaced = hamletParser.changeHamletToLeon();
Assert.assertTrue(replaced.contains("Tariq"));
Assert.assertTrue(replaced.contains("Leon"));
Assert.assertFalse(replaced.contains("Hamlet"));
Assert.assertFalse(replaced.contains("Horatio"));
}

@Test
public void testFindHoratio() {
}

@Test
public void testFindHamlet() {
public void testChangeName(){
Assert.assertTrue(hamletText.contains("Horatio"));
hamletText = hamletParser.changeName("Horatio", "Tariq");
Assert.assertTrue(hamletText.contains("Tariq"));
Assert.assertFalse(hamletText.contains("Horatio"));
}
}
Loading