Skip to content

Completed Lab #12

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
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
language: java
sudo: false
script: mvn clean verify
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[![Build Status](https://travis-ci.org/gjarant/ZCW-Regex-Hamlet-Parser.svg?branch=master)](https://travis-ci.org/gjarant/ZCW-Regex-Hamlet-Parser)
# REGEX

## Directions
Expand All @@ -6,4 +7,4 @@ Make a project that will go through the hamlet file provided and using regex rep

Beginning with tests, you are to program all the steps it will take to complete that process. Some tests have been stubbed out for you but these will not cover all the methods you should have in your project.

*IMPORTANT*: You are not to use String Utilities to simply replace words. You must use Pattern and Matcher.
*IMPORTANT*: You are not to use String Utilities to simply replace words. You must use Pattern and Matcher.
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
66 changes: 60 additions & 6 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,83 @@ 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 hamletData) {
String input = hamletData;
Pattern p = Pattern.compile("Hamlet");
Matcher m = p.matcher(input);
String hamletToLeon = m.replaceAll("Leon");
Pattern upperP = Pattern.compile("HAMLET");
Matcher upperM = upperP.matcher(hamletToLeon);
String hamletToLeonUpper = upperM.replaceAll("LEON");

return hamletToLeonUpper.toString();
}

public String changeHoratioToTariq(String hamletData) {
String input = hamletData;
Pattern p = Pattern.compile("Horatio");
Matcher m = p.matcher(input);
String horatioToTariq = m.replaceAll("Tariq");
Pattern upperP = Pattern.compile("HORATIO");
Matcher upperM = upperP.matcher(horatioToTariq);
String horatioToTariqUpper = upperM.replaceAll("TARIQ");

return horatioToTariqUpper.toString();
}

public String changeAll(String hamletData) {
String changeAll = changeHoratioToTariq(changeHamletToLeon(hamletData));


return changeAll;
}


public Integer findHalmet(String hamletData) {
String input = hamletData;
Pattern p = Pattern.compile("Hamlet", Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(input);
int count = 0;
while (m.find())
count++;
return count;
}

public Integer findHoratio(String hamletData) {
String input = hamletData;
Pattern p = Pattern.compile("Horatio", Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(input);
int count = 0;
while (m.find())
count++;
return count;
}

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

@Test
public void testChangeHamletToLeon() {
//Given
HamletParser testHamletParser = new HamletParser();
String testString = "Hamlet. HAMLET. Hamlet!";
//When
String expected = "Leon. LEON. Leon!";
String actual = testHamletParser.changeHamletToLeon(testString);
//Then
Assert.assertEquals(expected, actual);

}

@Test
public void testChangeHoratioToTariq() {
//Given
HamletParser testHamletParser = new HamletParser();
String testString = "Horatio. HORATIO. Horatio!";
//When
String expected = "Tariq. TARIQ. Tariq!";
String actual = testHamletParser.changeAll(testString);
//Then
Assert.assertEquals(expected, actual);
}

@Test
public void testChangeAll() {
//Given
HamletParser testHamletParser = new HamletParser();
String testString = "Horatio. HORATIO. Horatio! Hamlet. HAMLET. Hamlet!";
//When
String expected = "Tariq. TARIQ. Tariq! Leon. LEON. Leon!";
String actual = testHamletParser.changeAll(testString);
//Then
Assert.assertEquals(expected, actual);
}

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

//When
Integer expected = 158;
Integer actual = testHamletParser.findHoratio(hamletText);
//Then
Assert.assertEquals(expected, actual);
}

@Test
public void testFindHamlet() {
//Given
HamletParser testHamletParser = new HamletParser();

//When
Integer expected = 472;
Integer actual = testHamletParser.findHalmet(hamletText);

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