Skip to content
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

submitting lab #54

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
18 changes: 0 additions & 18 deletions Hurtlocker.iml

This file was deleted.

23 changes: 23 additions & 0 deletions groceryList.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Milk seen: 8 times
============= =============
Price: 3.23 seen: 5 times
------------- -------------
Price: 1.23 seen: 1 times

name: Bread seen: 6 times
============= =============
Price: 1.23 seen: 6 times
------------- -------------

name: Cookies seen: 8 times
============= =============
Price: 2.25 seen: 8 times
------------- -------------

name: Apples seen: 4 times
============= =============
Price: 0.25 seen: 2 times
------------- -------------
Price: 0.23 seen: 2 times

Errors seen: 4 times
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>io.zipcoder</groupId>
<artifactId>HurtLocker</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
Expand Down
190 changes: 187 additions & 3 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,201 @@
import org.apache.commons.io.IOUtils;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {

public String readRawDataToString() throws Exception{
ClassLoader classLoader = getClass().getClassLoader();

Integer counter = 0;

public static String readRawDataToString() throws Exception{
ClassLoader classLoader = Main.class.getClassLoader();
String result = IOUtils.toString(classLoader.getResourceAsStream("RawData.txt"));
return result;
}

public static void main(String[] args) throws Exception{
String output = (new Main()).readRawDataToString();
System.out.println(output);
try {
File outputFile = new File("groceryList.txt");
if(outputFile.createNewFile()) {
System.out.println("File created: " + outputFile.getName());
} else {
System.out.println("File already exists");
}
} catch (IOException e) {
System.out.println("An error occurred");
e.printStackTrace();
}
try {
FileWriter outputText = new FileWriter("groceryList.txt");
outputText.write(doingTheFormatting());
outputText.close();
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}

public static String changeMilk(String input) {
try {
Pattern pattern = Pattern.compile("milk", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(input);
String milk = matcher.replaceAll("Milk");

return milk;
} catch (Exception e) {
throw new UnsupportedOperationException();
}
}

public static String changeBread(String input) {
try {
Pattern pattern = Pattern.compile("bread", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(input);
String bread = matcher.replaceAll("Bread");

return bread;
} catch (Exception e) {
throw new UnsupportedOperationException();
}
}

public static String changeCookies(String input) {
try {
Pattern pattern = Pattern.compile("c[o0][o0]kies", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(input);
String cookies = matcher.replaceAll("Cookies");

return cookies;
} catch (Exception e) {
throw new UnsupportedOperationException();
}
}

public static String changeApples(String input) {
try {
Pattern pattern = Pattern.compile("apples", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(input);
String apples = matcher.replaceAll("Apples");

return apples;
} catch (Exception e) {
throw new UnsupportedOperationException();
}
}

public static String poundToNewLine(String input) {
try {
Pattern pattern = Pattern.compile("##");
Matcher matcher = pattern.matcher(input);
String result = matcher.replaceAll("\n");
return result;
} catch (Exception e) {
throw new UnsupportedOperationException();
}
}

public static String correctSeparator() {
try {
String s = readRawDataToString();
Pattern patter = Pattern.compile("[!@^%*]");
Matcher matcher = patter.matcher(s);
String result = matcher.replaceAll(";");
return result;
} catch (Exception e) {
throw new UnsupportedOperationException();
}
}

public static String nameChange(String input) {
try {
Pattern pattern = Pattern.compile("name", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(input);
String name = matcher.replaceAll("Name");

return name;
} catch (Exception e) {
throw new UnsupportedOperationException();
}
}

public static String priceChange(String input) {
try {
Pattern pattern = Pattern.compile("price", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(input);
String price = matcher.replaceAll("Price");

return price;
} catch (Exception e) {
throw new UnsupportedOperationException();
}
}


public static int findGroceries(String input) {
Integer holdingValue = 0;
Boolean checkVal = false;
Pattern pattern = Pattern.compile(input, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(readyForFormatting());
while(!checkVal) {
if (!matcher.find()) {
checkVal = true;
continue;
}
holdingValue++;
}
return holdingValue;
}

public static int countingErrors() {
int counter = 0;

counter += findGroceries("Name:;");
counter += findGroceries("milk") - (findGroceries("milk;price:3.23") + findGroceries("milk;price:1.23"));
return counter;
}

public static String readyForFormatting() {
String result = poundToNewLine(correctSeparator());
String result1 = changeApples(result);
String result2 = changeBread(result1);
String result3 = changeCookies(result2);
String result4 = changeMilk(result3);
String result5 = nameChange(result4);
String result6 = priceChange(result5);
return result6;
}

public static String doingTheFormatting() {
String result =
"name: Milk seen: " + findGroceries("milk") + " times\n" +
"============= =============\n" +
"Price: 3.23 seen: " + findGroceries("milk;price:3.23") + " times\n" +
"------------- -------------\n" +
"Price: 1.23 seen: " + findGroceries("milk;price:1.23") + " times\n\n" +

"name: Bread seen: " + findGroceries("bread") + " times\n" +
"============= =============\n" +
"Price: 1.23 seen: " + findGroceries("bread") + " times\n" +
"------------- -------------\n\n" +

"name: Cookies seen: " + findGroceries("cookies") + " times\n" +
"============= =============\n" +
"Price: 2.25 seen: " + findGroceries("cookies") + " times\n" +
"------------- -------------\n\n" +

"name: Apples seen: " + (findGroceries("apples;price:0.25") + findGroceries("apples;price:0.23")) + " times\n" +
"============= =============\n" +
"Price: 0.25 seen: " + findGroceries("price:0.25") + " times\n" +
"------------- -------------\n" +
"Price: 0.23 seen: " + findGroceries("price:0.23") + " times\n\n" +

"Errors seen: " + countingErrors() + " times";

return result;
}
}
101 changes: 101 additions & 0 deletions src/test/java/MainTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import org.junit.Assert;
import org.junit.Test;

public class MainTest {

Main main = new Main();

@Test
public void testReadRawDataToString() throws Exception {
String data = main.readRawDataToString();
System.out.println(data);
}

@Test
public void testChangeMilk() throws Exception {
String data = main.changeMilk(main.readRawDataToString());
System.out.println(data);
}

@Test
public void testChangeBread() throws Exception {
String data = main.changeBread(main.readRawDataToString());
System.out.println(data);
}

@Test
public void testChangeCookies() throws Exception {
String data = main.changeCookies(main.readRawDataToString());
System.out.println(data);
}

@Test
public void testChangeApples() throws Exception {
String data = main.changeApples(main.readRawDataToString());
System.out.println(data);
}

@Test
public void testPoundToNewLine() throws Exception {
String data = main.poundToNewLine(main.readRawDataToString());
System.out.println(data);
}

@Test
public void testCorrectSeparator() throws Exception {
String data = main.correctSeparator();
System.out.println(data);
}

@Test
public void testNameChange() throws Exception {
String data = main.nameChange(main.readRawDataToString());
System.out.println(data);
}

@Test
public void testPriceChange() throws Exception {
String data = main.priceChange(main.readRawDataToString());
System.out.println(data);
}

@Test
public void testFindGroceries() {
//given
int breadExpected = 6;
int appleExpected = 4;
int cookieExpected = 8;
int milkExpected = 8;
//when
int breadCount = main.findGroceries("bread");
int appleCount = main.findGroceries("apple");
int cookieCount = main.findGroceries("cookie");
int milkCount = main.findGroceries("milk");
//then
Assert.assertEquals(breadExpected, breadCount);
Assert.assertEquals(appleExpected, appleCount);
Assert.assertEquals(cookieExpected, cookieCount);
Assert.assertEquals(milkExpected, milkCount);
System.out.printf("Bread: %s, Apple: %s, Cookie: %s, Milk: %s", breadCount, appleCount, cookieCount, milkCount);
}

@Test
public void testCountingErrors() {
//given
int expected = 4;
//when
int actual = main.countingErrors();
//then
Assert.assertEquals(expected, actual);
}

@Test
public void testReadyForFormatting() {
System.out.println(main.readyForFormatting());
}

@Test
public void testDoingTheFormatting() {
System.out.println(main.doingTheFormatting());
}
}
Binary file modified target/classes/Main.class
Binary file not shown.