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/outputFormat.txt b/outputFormat.txt new file mode 100644 index 0000000..c1dc6df --- /dev/null +++ b/outputFormat.txt @@ -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 \ No newline at end of file diff --git a/src/main/java/GroceryList.java b/src/main/java/GroceryList.java new file mode 100644 index 0000000..2da9b02 --- /dev/null +++ b/src/main/java/GroceryList.java @@ -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; + } +} diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 632942a..3aecbc7 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -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(); @@ -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(); + + } + + } diff --git a/src/main/java/Parser.java b/src/main/java/Parser.java new file mode 100644 index 0000000..a6594aa --- /dev/null +++ b/src/main/java/Parser.java @@ -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; + } + + +} diff --git a/src/main/test/testParsedData.java b/src/main/test/testParsedData.java new file mode 100644 index 0000000..5e14a84 --- /dev/null +++ b/src/main/test/testParsedData.java @@ -0,0 +1,123 @@ +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class testParsedData { + private Parser parser; + private Main main; + + @Before + public void setup(){ + this.parser = new Parser(); + this.main = new Main(); + + } + + @Test + public void testMilkQty(){ + //given + Integer expected = 8; + //when + Integer actual = main.countItem(parser.dataParser(), "Milk"); + + //then + Assert.assertEquals(expected,actual); + + } + + @Test + public void testMilkPricesCount(){ + + //given + Integer expectedPrice1 = 5; + Integer expectedPrice2 = 1; + //when + Integer actualPrice1 = main.countItemAtPrice(parser.dataParser(), "Milk,price:3[.]23"); + Integer actualPrice2 = main.countItemAtPrice(parser.dataParser(), "Milk,price:1[.]23"); + //then + Assert.assertEquals(expectedPrice1, actualPrice1); + Assert.assertEquals(expectedPrice2, actualPrice2); + } + @Test + public void testCookiesQty(){ + //given + Integer expected = 8; + //when + Integer actual = main.countItem(parser.dataParser(), "Cookies"); + + //then + Assert.assertEquals(expected,actual); + + } + @Test + public void testCookiesPricesCount(){ + + //given + Integer expectedPrice1 = 8; + //when + Integer actualPrice1 = main.countItemAtPrice(parser.dataParser(), "Cookies,price:2[.]25"); + //then + Assert.assertEquals(expectedPrice1, actualPrice1); + + } + @Test + public void testApplesQty(){ + //given + Integer expected = 4; + //when + Integer actual = main.countItem(parser.dataParser(), "Apples"); + + //then + Assert.assertEquals(expected,actual); + + } + @Test + public void testApplesPricesCount(){ + + //given + Integer expectedPrice1 = 2; + Integer expectedPrice2 = 2; + //when + Integer actualPrice1 = main.countItemAtPrice(parser.dataParser(), "Apples,price:0[.]25"); + Integer actualPrice2 = main.countItemAtPrice(parser.dataParser(), "Apples,price:0[.]23"); + //then + Assert.assertEquals(expectedPrice1, actualPrice1); + Assert.assertEquals(expectedPrice2, actualPrice2); + } + @Test + public void testBreadQty(){ + //given + Integer expected = 6; + //when + Integer actual = main.countItem(parser.dataParser(), "Bread"); + + //then + Assert.assertEquals(expected,actual); + + } + + @Test + public void testBreadPricesCount(){ + + //given + Integer expectedPrice1 = 6; + //when + Integer actualPrice1 = main.countItemAtPrice(parser.dataParser(), "Bread,price:1[.]23"); + + //then + Assert.assertEquals(expectedPrice1, actualPrice1); + + } + + @Test + public void testErrorCount(){ + //given + Integer expected = 4; + //when + Integer actual = main.errorCounter(); + //then + Assert.assertEquals(expected, actual); + } + + +} diff --git a/target/classes/Main.class b/target/classes/Main.class index c9d3858..a6d54a7 100644 Binary files a/target/classes/Main.class and b/target/classes/Main.class differ