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/Item.java b/src/main/java/Item.java new file mode 100644 index 0000000..5370c47 --- /dev/null +++ b/src/main/java/Item.java @@ -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); + } + } +} diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 632942a..4255c20 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,5 +1,6 @@ import org.apache.commons.io.IOUtils; import java.io.IOException; +import java.util.List; public class Main { @@ -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 items = RegexThis.regexer(output); + System.out.println(items + "\n"); + List beautifiedItems = RegexThis.beautifier(items); + System.out.println("Summarization: "); + System.out.println(Summarization.summarize(beautifiedItems)); } } diff --git a/src/main/java/RegexThis.java b/src/main/java/RegexThis.java new file mode 100644 index 0000000..7a848e6 --- /dev/null +++ b/src/main/java/RegexThis.java @@ -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 regexer(String raw){ + Pattern p = Pattern.compile("(?:name\\W)?(?[\\w]+)?(?:\\Wprice\\W)?(?[\\w\\.]+)?(?:\\Wtype\\W)?(?[\\w]+)?(?:\\Wexpiration\\W)?(?[\\w\\/]+)?(?:##)" + ,Pattern.CASE_INSENSITIVE); + Matcher m = p.matcher(raw); + //matches gotten + List items = new ArrayList(); + //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 beautifier(List 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 + diff --git a/src/main/java/Summarization.java b/src/main/java/Summarization.java new file mode 100644 index 0000000..5235b52 --- /dev/null +++ b/src/main/java/Summarization.java @@ -0,0 +1,62 @@ + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class Summarization { + public static String summarize(List items){ + StringBuilder sb = new StringBuilder(); + Map> 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(); + } +} diff --git a/target/classes/Main.class b/target/classes/Main.class deleted file mode 100644 index c9d3858..0000000 Binary files a/target/classes/Main.class and /dev/null differ