diff --git a/Hurtlocker.iml b/Hurtlocker.iml deleted file mode 100644 index 22967e8..0000000 --- a/Hurtlocker.iml +++ /dev/null @@ -1,18 +0,0 @@ - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/pom.xml b/pom.xml index 39639cd..3c31de2 100644 --- a/pom.xml +++ b/pom.xml @@ -7,6 +7,18 @@ io.zipcoder HurtLocker 1.0-SNAPSHOT + + + + org.apache.maven.plugins + maven-compiler-plugin + + 8 + 8 + + + + diff --git a/src/main/java/JerkSONParser.java b/src/main/java/JerkSONParser.java new file mode 100644 index 0000000..4145e48 --- /dev/null +++ b/src/main/java/JerkSONParser.java @@ -0,0 +1,164 @@ +import java.io.*; +import java.util.*; + +public class JerkSONParser { + private String data; + private int exceptionCount; + private List pairedValues; + private List linesToPrint; + private Set 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); + } + } +} diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 632942a..66f9ca5 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -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(); } } diff --git a/src/main/java/ParserUtils.java b/src/main/java/ParserUtils.java new file mode 100644 index 0000000..387a433 --- /dev/null +++ b/src/main/java/ParserUtils.java @@ -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); + } + +} diff --git a/src/main/java/Writer.java b/src/main/java/Writer.java new file mode 100644 index 0000000..c12c97d --- /dev/null +++ b/src/main/java/Writer.java @@ -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 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(); + } +} diff --git a/src/main/resources/output.txt b/src/main/resources/output.txt new file mode 100644 index 0000000..0ea6d19 --- /dev/null +++ b/src/main/resources/output.txt @@ -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 + diff --git a/src/test/java/TestJerkSONParser.java b/src/test/java/TestJerkSONParser.java new file mode 100644 index 0000000..91a919b --- /dev/null +++ b/src/test/java/TestJerkSONParser.java @@ -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); + } + + +} + + + + + + diff --git a/target/classes/Main.class b/target/classes/Main.class index c9d3858..2ed51f2 100644 Binary files a/target/classes/Main.class and b/target/classes/Main.class differ