Skip to content
Open
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
118 changes: 118 additions & 0 deletions src/main/java/Item.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
public class Item {
private String name;
private final String price;
private final String type;
private final String expiration;
private boolean isError = false;

public Item(ItemBuilder builder) {
this.name = builder.getName();
this.price = builder.getPrice();
this.type = builder.getType();
this.expiration = builder.getExpiration();
this.isError = builder.getIsError();
}
public void setName(String newName){
this.name = newName;
}
public String getName() {
return name;
}

public String getPrice() {
return price;
}

public String getType() {
return type;
}

public String getExpiration() {
return expiration;
}
public boolean isError(){
return isError;
}
//equals method
//toString method


@Override
public String toString() {
if(!isError) {
return "Item{" +
"name='" + name + '\'' +
", price='" + price + '\'' +
", type='" + type + '\'' +
", expiration='" + expiration + '\'' +
"}\n";
}
return "Erroneous entry\n";
}

public static class ItemBuilder{
private String name;
private String price;
private String type;
private String expiration;
private boolean isError = false;
public ItemBuilder(){
}
public ItemBuilder setName(String name) {
checkError(name);
this.name = name;
return this;
}

public ItemBuilder setPrice(String price) {
checkError(price);
this.price = price;
return this;
}

public ItemBuilder setType(String type) {
checkError(type);
this.type = type;
return this;
}

public ItemBuilder setExpiration(String expiration) {
checkError(expiration);
this.expiration = expiration;
return this;
}
public ItemBuilder denoteError(){
this.isError = true;
return this;
}
public void checkError(String input){
if(input == null){
this.isError = true;
this.name = "error";
}
}

public String getName() {
return name;
}

public String getPrice() {
return price;
}

public String getType() {
return type;
}

public String getExpiration() {
return expiration;
}

public boolean getIsError(){
return isError;
}
public Item build(){
return new Item(this);
}
}
}
10 changes: 9 additions & 1 deletion src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import org.apache.commons.io.IOUtils;
import java.io.IOException;
import java.util.List;

public class Main {

Expand All @@ -11,7 +12,14 @@ 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("Raw Data:");
System.out.println(output + "\n");
System.out.println("Regexed & Encapsulated:");
List<Item> items = RegexThis.regexer(output);
System.out.println(items + "\n");
List<Item> beautifiedItems = RegexThis.beautifier(items);
System.out.println("Summarization: ");
System.out.println(Summarization.summarize(beautifiedItems));

}
}
51 changes: 51 additions & 0 deletions src/main/java/RegexThis.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexThis {
//3 to 5 lines of regex
public static List<Item> regexer(String raw){
Pattern p = Pattern.compile("(?:name\\W)?(?<NAME>[\\w]+)?(?:\\Wprice\\W)?(?<PRICE>[\\w\\.]+)?(?:\\Wtype\\W)?(?<TYPE>[\\w]+)?(?:\\Wexpiration\\W)?(?<EXPIRATION>[\\w\\/]+)?(?:##)"
,Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(raw);
//matches gotten
List<Item> items = new ArrayList<Item>();
//encapsulate entries
while(m.find()){
Item newbie = new Item.ItemBuilder()
.setName(m.group("NAME") != null ? (Pattern.compile("0")).matcher(m.group("NAME")).replaceAll("o") : null)
.setPrice(m.group("PRICE"))
.setType(m.group("TYPE"))
.setExpiration(m.group("EXPIRATION"))
.build();
items.add(newbie);
}
return items;
}
public static List<Item> beautifier(List<Item> raw){
for(Item item: raw){
String a = item.getName();
if(a != null) {
Matcher cookie = Pattern.compile("cookies", Pattern.CASE_INSENSITIVE).matcher(a);
Matcher milk = Pattern.compile("milk", Pattern.CASE_INSENSITIVE).matcher(a);
Matcher bread = Pattern.compile("bread", Pattern.CASE_INSENSITIVE).matcher(a);
Matcher apples = Pattern.compile("apples", Pattern.CASE_INSENSITIVE).matcher(a);
if (cookie.matches()) {
item.setName("COOKIES");
} else if (milk.matches()) {
item.setName("MILK");
} else if (bread.matches()) {
item.setName("BREAD");
} else if (apples.matches()) {
item.setName("APPLES");
}
}
}
return raw;
}
}
//TODO:
//nicely formatted summary(kind of)
//replace inconsistent casing with regular casing

62 changes: 62 additions & 0 deletions src/main/java/Summarization.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Summarization {
public static String summarize(List<Item> items){
StringBuilder sb = new StringBuilder();
Map<String,Map<String,Integer>> headcount = new HashMap<>();
int errorCount = 0;
for(Item item: items){
if(item.isError()){
errorCount++;
}
else if(!headcount.containsKey(item.getName())){
headcount.put(item.getName(),(new HashMap<>()));
if(item.getName() == null){
headcount.get(null).put("-1",1);
}
else if(item.getPrice() == null){
headcount.get("error").put("-1",1);
}
else{
headcount.get(item.getName()).put(item.getPrice(),1);
}
}
else{
if(!headcount.get(item.getName()).containsKey(item.getPrice())){
headcount.get(item.getName()).put(item.getPrice(),1);
}
else if(item.getName() == null){
Integer temp = headcount.get(null).get("-1");
headcount.get(null).put("-1",temp + 1);

}
else if(item.getPrice() == null){
Integer temp = headcount.get("error").get("-1");
headcount.get("error").replace("-1",temp + 1);
}
else{
Integer temp = headcount.get(item.getName()).get(item.getPrice());
headcount.get(item.getName()).replace(item.getPrice(),temp + 1);
}
}
}
for(String name: headcount.keySet()){
int total = 0;
for(String price: headcount.get(name).keySet()){
total+=headcount.get(name).get(price);
}
sb.append(String.format("name:%4s \t \t seen: %s times\n",name,total));
sb.append("============= \t \t =============\n");
for(String price: headcount.get(name).keySet()){
sb.append(String.format("Price:%4s \t \t seen: %s times\n",price,headcount.get(name).get(price)));
sb.append("-------------\t\t -------------\n");
}
sb.append("\n");
}
sb.append(String.format("Errors \t \t seen: %s times",errorCount));
return sb.toString();
}
}
Binary file removed target/classes/Main.class
Binary file not shown.