Skip to content
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 outputFormat.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 time

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
51 changes: 51 additions & 0 deletions src/main/java/GroceryList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import java.text.SimpleDateFormat;

public class GroceryList {
private String name;
private String type;
private Double price;
private SimpleDateFormat expiration;

public GroceryList(){

}

public GroceryList(String name, String type, Double price, SimpleDateFormat expiration) {
this.name = name;
this.type = type;
this.price = price;
this.expiration = expiration;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public Double getPrice() {
return price;
}

public void setPrice(Double price) {
this.price = price;
}

public SimpleDateFormat getExpiration() {
return expiration;
}

public void setExpiration(SimpleDateFormat expiration) {
this.expiration = expiration;
}
}
105 changes: 104 additions & 1 deletion src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import org.apache.commons.io.IOUtils;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Formatter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
Parser parsedData = new Parser();

public String readRawDataToString() throws Exception{
ClassLoader classLoader = getClass().getClassLoader();
Expand All @@ -11,7 +18,103 @@ public String readRawDataToString() throws Exception{

public static void main(String[] args) throws Exception{
String output = (new Main()).readRawDataToString();
System.out.println(output);
System.out.println("JerkSON");
System.out.println(output+"\n");

Parser parser = new Parser();
System.out.println("JSON");
System.out.println(parser.dataParser());

Main main = new Main();

// System.out.println("Milk seen: "+main.countItem(parser.dataParser(),"Milk"));
// System.out.println("1.23 Price seen: "+main.countItemAtPrice(parser.dataParser(),"Milk,price:1[.]23"));
// System.out.println("3.23 Price seen : "+main.countItemAtPrice(parser.dataParser(), "Milk,price:3[.]23"));
// System.out.println("No Price seen: "+main.countItemNoPrice(parser.dataParser(),"Milk,price:,"));
// System.out.println(main.errorCounter());

System.out.println(main.formatedOutput());
main.outputToTXT();

}

public Integer countItem(String parsedData, String item){
Integer count = 0;
Pattern pattern = Pattern.compile(item);
Matcher matcher = pattern.matcher(parsedData);
while (matcher.find()){
count++;
}
return count;
}
public Integer countItemAtPrice(String parsedData, String price){
Integer count = 0;
Pattern pattern = Pattern.compile(price);
Matcher matcher = pattern.matcher(parsedData);
while (matcher.find()){
count++;
}
return count;
}

public Integer countKeyNoValue(String parsedData, String noValue){
Integer count = 0;
Pattern pattern = Pattern.compile(noValue);
Matcher matcher = pattern.matcher(parsedData);
while (matcher.find()){
count++;
}
return count;
}
public Integer errorCounter(){
Integer count = 0;
String[] keys = {"name","price","type","expiration"};
for (int i = 0; i < keys.length; i++) {
count += countKeyNoValue(parsedData.dataParser(),keys[i]+":,");
}
return count;
}



public String formatedOutput(){
String result = "";
result = "name: Milk \t\t seen: "+countItem(parsedData.dataParser(), "Milk")+" times\n" +
"============= \t \t =============\n" +
"Price: \t 3.23\t\t seen: "+countItemAtPrice(parsedData.dataParser(), "Milk,price:3[.]23")+" times\n" +
"-------------\t\t -------------\n" +
"Price: 1.23\t\t seen: "+countItemAtPrice(parsedData.dataParser(), "Milk,price:1[.]23")+" time\n" +
"\n" +
"name: Bread\t\t seen: "+countItem(parsedData.dataParser(), "Bread")+" times\n" +
"=============\t\t =============\n" +
"Price: 1.23\t\t seen: "+countItemAtPrice(parsedData.dataParser(), "Bread,price:1[.]23")+" times\n" +
"-------------\t\t -------------\n" +
"\n" +
"name: Cookies \t seen: "+countItem(parsedData.dataParser(), "Cookies")+" times\n" +
"============= \t =============\n" +
"Price: 2.25 seen: "+countItemAtPrice(parsedData.dataParser(), "Cookies,price:2[.]25")+" times\n" +
"------------- -------------\n" +
"\n" +
"name: Apples \t seen: "+countItem(parsedData.dataParser(), "Apples")+" times\n" +
"============= \t =============\n" +
"Price: 0.25 \t seen: "+countItemAtPrice(parsedData.dataParser(), "Apples,price:0[.]25")+" times\n" +
"------------- \t -------------\n" +
"Price: 0.23 \t \t seen: "+countItemAtPrice(parsedData.dataParser(), "Apples,price:0[.]23")+" times\n" +
"\n" +
"Errors \t \t seen: "+errorCounter()+" times ";


return result;
}

public void outputToTXT() throws FileNotFoundException {

FileOutputStream outputStream= new FileOutputStream("outputFormat.txt");
Formatter formatter = new Formatter(outputStream);
formatter.format(formatedOutput());
formatter.flush();

}


}
76 changes: 76 additions & 0 deletions src/main/java/Parser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Parser {
private String groceryListData;

public Parser() {
this.groceryListData = loadFile();
}

private String loadFile(){
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("RawData.txt").getFile());
StringBuilder result = new StringBuilder("");

try(Scanner scanner = new Scanner(file)){
while(scanner.hasNextLine()){
String line = scanner.nextLine();
result.append(line).append("\n");
}

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

return result.toString();
}

public String dataParser(){
String parsedString = "";
Pattern pattern = Pattern.compile("(?i)(name)");
Matcher matcher = pattern.matcher(loadFile());
parsedString = matcher.replaceAll("{name");


Pattern pattern1 = Pattern.compile("(?i)(##)");
Matcher matcher1 = pattern1.matcher(parsedString);
parsedString = matcher1.replaceAll("},\n");

Pattern cookies = Pattern.compile("(?i)(C[o0][o0]kies[;])");
Matcher matcherCookies = cookies.matcher(parsedString);
parsedString = matcherCookies.replaceAll("Cookies,");

Pattern milk = Pattern.compile("(?i)(milk[;])");
Matcher matcherMilk = milk.matcher(parsedString);
parsedString = matcherMilk.replaceAll("Milk,");

Pattern bread = Pattern.compile("(?i)(bread[;])");
Matcher matcherBread = bread.matcher(parsedString);
parsedString = matcherBread.replaceAll("Bread,");

Pattern apple = Pattern.compile("(?i)(apples)");
Matcher matcherApple = apple.matcher(parsedString);
parsedString = matcherApple.replaceAll("Apples");

Pattern pattern5 = Pattern.compile("(?i)[;,](price)");
Matcher matcher5 = pattern5.matcher(parsedString);
parsedString = matcher5.replaceAll(",price");

Pattern pattern6 = Pattern.compile("(?i)(;type)");
Matcher matcher6 = pattern6.matcher(parsedString);
parsedString = matcher6.replaceAll(",type");

Pattern pattern7 = Pattern.compile("(?i)(food)[!;@^*%]");
Matcher matcher7 = pattern7.matcher(parsedString);
parsedString = matcher7.replaceAll("Food,");

return parsedString;
}


}
Loading