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

done #44

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

done #44

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.

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
164 changes: 164 additions & 0 deletions src/main/java/JerkSONParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
import java.io.*;
import java.util.*;

public class JerkSONParser {
private String data;
private int exceptionCount;
private List<String> pairedValues;
private List<String> linesToPrint;
private Set<String> uniqueValues;
private Writer writer;


public JerkSONParser(String data){
this.data = data;
pairedValues = new ArrayList<>();
linesToPrint = new ArrayList<>();
uniqueValues = new HashSet<>();
writer = new Writer();
}

public void processFile(){
getAndSetLines();
countItems();
createPrintLine(exceptionCount, "errors\t\t");
try {
writer.writeToFile(linesToPrint);
} catch (IOException e){
e.printStackTrace();
}
}


public void getAndSetLines(){
Scanner sc = new Scanner(data);
sc.useDelimiter("##");

while(sc.hasNext()){
String line = sc.next();
getErrors(line);
storeValues(line);
}
}

public void storeValues(String toParse){
String name;
String price;

name = ParserUtils.matchedString(toParse, "(?i)name:(.*?);");
price = ParserUtils.matchedString(toParse, "(?i)price:(.*?);");

if(price.equals("") || name.equals("")){
}
else{
name = name.toLowerCase();
pairedValues.add(name + "-" + price + "-");
uniqueValues.add(name);
}
}



public void getPriceCounter(String item){
String price = "";
String previousPrice = "";
int priceCounter = 0;

for(String s : pairedValues){
if(s.matches("(.*)(?i)" + item + "(.*)")) {
if (priceCounter == 0) {
previousPrice = ParserUtils.matchedString(s, "-(.*?)-");
}
price = ParserUtils.matchedString(s, "-(.*?)-");


if (price.equals(previousPrice)) {
priceCounter++;
} else {
createPrintLine(priceCounter, "price:" + ParserUtils.printPadding(previousPrice, 9));
previousPrice = price;
priceCounter = 1;
}
}
else if(priceCounter != 0){
createPrintLine(priceCounter, "price:" + ParserUtils.printPadding(price, 9));
price = "";
priceCounter = 0;
}
}
if(priceCounter != 0) {
createPrintLine(priceCounter, "price:\t" + price);
}
printDividers("new");

}

public void countItems(){
Collections.sort(pairedValues);

int counter = 0;
for(String element : uniqueValues){
for(String s : pairedValues){
if(s.matches("(.*)(?i)" + element + "(.*)") && s.matches("(.*)(?i)[0-9](.*)")){
counter++;
}
}
createPrintLine(counter, "name:" + ParserUtils.printPadding(element,10).toLowerCase());
counter = 0;
getPriceCounter(element.toLowerCase());
}
}

public void getErrors(String lineIn){
String pattern = "[;!%@^*]";
Scanner sc = new Scanner(lineIn);
sc.useDelimiter(pattern);

while (sc.hasNext()){
String line = sc.next();
if(!ParserUtils.findPattern(lineIn, ":")){
exceptionCount++;
}
else{
findMissingVals(line);
}
}
}

public void findMissingVals(String input){
int count = 0;
Scanner sc = new Scanner(input);
sc.useDelimiter(":");

while (sc.hasNext()){
String line = sc.next();
count++;
}
if(count != 2){
exceptionCount++;
}
}



public void createPrintLine(Integer counter, String item){
linesToPrint.add(item + "\t\tseen: " + counter + " times\n");
printDividers(item);
}

public void printDividers(String item){
if(ParserUtils.findPattern(item, "name")){
String line = "=================\t\t=================";
linesToPrint.add(line);
}
else if(ParserUtils.findPattern(item, "errors")){
}
else if(ParserUtils.findPattern(item, "new")){
linesToPrint.add("\n");
}
else{
String line = "-------------------\t\t-------------------";
linesToPrint.add(line);
}
}
}
2 changes: 2 additions & 0 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@ public static void main(String[] args) throws Exception{
String output = (new Main()).readRawDataToString();
System.out.println(output);

JerkSONParser jp = new JerkSONParser(output);
jp.processFile();
}
}
22 changes: 22 additions & 0 deletions src/main/java/ParserUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ParserUtils {
public static String matchedString(String textToSearch, String pattern){
Pattern pattern1 = Pattern.compile(pattern);
Matcher matcher = pattern1.matcher(textToSearch);
if(matcher.find()){
return matcher.group(1);
}
return "";
}

public static Boolean findPattern(String textToSearch, String pattern){
Pattern pattern1 = Pattern.compile(pattern);
return pattern1.matcher(textToSearch).find();
}
public static String printPadding(String s, int n){
return String.format("%" + n + "s", s);
}

}
24 changes: 24 additions & 0 deletions src/main/java/Writer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

public class Writer {
private String fileName;
private String lineToWrite = "";
FileWriter writer;

public Writer(){
this.fileName = "src/main/resources/output.txt";
}

public void writeToFile(List<String> printAll) throws IOException {
for(String s : printAll){
lineToWrite = lineToWrite.concat(s);
}
writer = new FileWriter(this.fileName);
PrintWriter printLine = new PrintWriter(writer);
printLine.printf("%s" + "%n", lineToWrite);
printLine.close();
}
}
19 changes: 19 additions & 0 deletions src/main/resources/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: bread seen: 6 times
================= =================price: 1.23 seen: 6 times
------------------- -------------------
name: milk seen: 6 times
================= =================price: 1.23 seen: 1 times
------------------- -------------------price: 3.23 seen: 5 times
------------------- -------------------
name: co0kies seen: 1 times
================= =================price: 2.25 seen: 1 times
------------------- -------------------
name: apples seen: 4 times
================= =================price: 0.23 seen: 2 times
------------------- -------------------price: 0.25 seen: 2 times
------------------- -------------------
name: cookies seen: 7 times
================= =================price: 2.25 seen: 7 times
------------------- -------------------
errors seen: 4 times

54 changes: 54 additions & 0 deletions src/test/java/TestJerkSONParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import com.sun.javafx.tools.packager.PackagerException;
import jdk.nashorn.internal.runtime.ParserException;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import java.io.*;
import java.util.StringTokenizer;

public class TestJerkSONParser {
JerkSONParser jp;
String testData;


@Before
public void init() throws FileNotFoundException {
testData = "naMe:Milk;price:3.23;type:Food;expiration:1/25/2016##naME:BreaD;price:1.23;type:Food;expiration:1/02/2016##NAMe:BrEAD;price:1.23;type:Food;expiration:2/25/2016##";
jp = new JerkSONParser(testData);
}

@Test
public void findPattern1(){
String pattern = "##";
Boolean actual = ParserUtils.findPattern(testData,pattern);
Assert.assertTrue(actual);
}

@Test
public void findPattern2(){
String pattern = "qg";
Boolean actual = ParserUtils.findPattern(testData,pattern);
Assert.assertFalse(actual);
}


@Test
public void matchedString(){
String pattern = ":(.*?);";
String test = "type:Food;";

String expected = "Food";
String actual = ParserUtils.matchedString(test, pattern);

Assert.assertEquals(expected,actual);
}


}






Binary file modified target/classes/Main.class
Binary file not shown.