From 047ebd3f168eabf81db809114ba9272b08063fae Mon Sep 17 00:00:00 2001 From: jgiroso Date: Mon, 9 Aug 2021 17:53:39 -0400 Subject: [PATCH] everything in main rn. may try to split between multiple classes --- Hurtlocker.iml | 18 ---- groceryList.txt | 23 +++++ pom.xml | 12 +++ src/main/java/Main.java | 190 +++++++++++++++++++++++++++++++++++- src/test/java/MainTest.java | 101 +++++++++++++++++++ target/classes/Main.class | Bin 1162 -> 6325 bytes 6 files changed, 323 insertions(+), 21 deletions(-) delete mode 100644 Hurtlocker.iml create mode 100644 groceryList.txt create mode 100644 src/test/java/MainTest.java 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/groceryList.txt b/groceryList.txt new file mode 100644 index 0000000..93ba446 --- /dev/null +++ b/groceryList.txt @@ -0,0 +1,23 @@ +name: Milk seen: 8 times +============= ============= +Price: 3.23 seen: 5 times +------------- ------------- +Price: 1.23 seen: 1 times + +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/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/Main.java b/src/main/java/Main.java index 632942a..500a51e 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,17 +1,201 @@ import org.apache.commons.io.IOUtils; + +import java.io.File; +import java.io.FileWriter; import java.io.IOException; +import java.util.regex.Matcher; +import java.util.regex.Pattern; public class Main { - public String readRawDataToString() throws Exception{ - ClassLoader classLoader = getClass().getClassLoader(); + + Integer counter = 0; + + public static String readRawDataToString() throws Exception{ + ClassLoader classLoader = Main.class.getClassLoader(); String result = IOUtils.toString(classLoader.getResourceAsStream("RawData.txt")); return result; } public static void main(String[] args) throws Exception{ String output = (new Main()).readRawDataToString(); - System.out.println(output); + try { + File outputFile = new File("groceryList.txt"); + if(outputFile.createNewFile()) { + System.out.println("File created: " + outputFile.getName()); + } else { + System.out.println("File already exists"); + } + } catch (IOException e) { + System.out.println("An error occurred"); + e.printStackTrace(); + } + try { + FileWriter outputText = new FileWriter("groceryList.txt"); + outputText.write(doingTheFormatting()); + outputText.close(); + } catch (IOException e) { + System.out.println("An error occurred."); + e.printStackTrace(); + } + } + + public static String changeMilk(String input) { + try { + Pattern pattern = Pattern.compile("milk", Pattern.CASE_INSENSITIVE); + Matcher matcher = pattern.matcher(input); + String milk = matcher.replaceAll("Milk"); + + return milk; + } catch (Exception e) { + throw new UnsupportedOperationException(); + } + } + + public static String changeBread(String input) { + try { + Pattern pattern = Pattern.compile("bread", Pattern.CASE_INSENSITIVE); + Matcher matcher = pattern.matcher(input); + String bread = matcher.replaceAll("Bread"); + + return bread; + } catch (Exception e) { + throw new UnsupportedOperationException(); + } + } + + public static String changeCookies(String input) { + try { + Pattern pattern = Pattern.compile("c[o0][o0]kies", Pattern.CASE_INSENSITIVE); + Matcher matcher = pattern.matcher(input); + String cookies = matcher.replaceAll("Cookies"); + + return cookies; + } catch (Exception e) { + throw new UnsupportedOperationException(); + } + } + + public static String changeApples(String input) { + try { + Pattern pattern = Pattern.compile("apples", Pattern.CASE_INSENSITIVE); + Matcher matcher = pattern.matcher(input); + String apples = matcher.replaceAll("Apples"); + + return apples; + } catch (Exception e) { + throw new UnsupportedOperationException(); + } + } + + public static String poundToNewLine(String input) { + try { + Pattern pattern = Pattern.compile("##"); + Matcher matcher = pattern.matcher(input); + String result = matcher.replaceAll("\n"); + return result; + } catch (Exception e) { + throw new UnsupportedOperationException(); + } + } + + public static String correctSeparator() { + try { + String s = readRawDataToString(); + Pattern patter = Pattern.compile("[!@^%*]"); + Matcher matcher = patter.matcher(s); + String result = matcher.replaceAll(";"); + return result; + } catch (Exception e) { + throw new UnsupportedOperationException(); + } + } + public static String nameChange(String input) { + try { + Pattern pattern = Pattern.compile("name", Pattern.CASE_INSENSITIVE); + Matcher matcher = pattern.matcher(input); + String name = matcher.replaceAll("Name"); + + return name; + } catch (Exception e) { + throw new UnsupportedOperationException(); + } + } + + public static String priceChange(String input) { + try { + Pattern pattern = Pattern.compile("price", Pattern.CASE_INSENSITIVE); + Matcher matcher = pattern.matcher(input); + String price = matcher.replaceAll("Price"); + + return price; + } catch (Exception e) { + throw new UnsupportedOperationException(); + } + } + + + public static int findGroceries(String input) { + Integer holdingValue = 0; + Boolean checkVal = false; + Pattern pattern = Pattern.compile(input, Pattern.CASE_INSENSITIVE); + Matcher matcher = pattern.matcher(readyForFormatting()); + while(!checkVal) { + if (!matcher.find()) { + checkVal = true; + continue; + } + holdingValue++; + } + return holdingValue; + } + + public static int countingErrors() { + int counter = 0; + + counter += findGroceries("Name:;"); + counter += findGroceries("milk") - (findGroceries("milk;price:3.23") + findGroceries("milk;price:1.23")); + return counter; + } + + public static String readyForFormatting() { + String result = poundToNewLine(correctSeparator()); + String result1 = changeApples(result); + String result2 = changeBread(result1); + String result3 = changeCookies(result2); + String result4 = changeMilk(result3); + String result5 = nameChange(result4); + String result6 = priceChange(result5); + return result6; + } + + public static String doingTheFormatting() { + String result = + "name: Milk seen: " + findGroceries("milk") + " times\n" + + "============= =============\n" + + "Price: 3.23 seen: " + findGroceries("milk;price:3.23") + " times\n" + + "------------- -------------\n" + + "Price: 1.23 seen: " + findGroceries("milk;price:1.23") + " times\n\n" + + + "name: Bread seen: " + findGroceries("bread") + " times\n" + + "============= =============\n" + + "Price: 1.23 seen: " + findGroceries("bread") + " times\n" + + "------------- -------------\n\n" + + + "name: Cookies seen: " + findGroceries("cookies") + " times\n" + + "============= =============\n" + + "Price: 2.25 seen: " + findGroceries("cookies") + " times\n" + + "------------- -------------\n\n" + + + "name: Apples seen: " + (findGroceries("apples;price:0.25") + findGroceries("apples;price:0.23")) + " times\n" + + "============= =============\n" + + "Price: 0.25 seen: " + findGroceries("price:0.25") + " times\n" + + "------------- -------------\n" + + "Price: 0.23 seen: " + findGroceries("price:0.23") + " times\n\n" + + + "Errors seen: " + countingErrors() + " times"; + + return result; } } diff --git a/src/test/java/MainTest.java b/src/test/java/MainTest.java new file mode 100644 index 0000000..b34e9bd --- /dev/null +++ b/src/test/java/MainTest.java @@ -0,0 +1,101 @@ +import org.junit.Assert; +import org.junit.Test; + +public class MainTest { + + Main main = new Main(); + + @Test + public void testReadRawDataToString() throws Exception { + String data = main.readRawDataToString(); + System.out.println(data); + } + + @Test + public void testChangeMilk() throws Exception { + String data = main.changeMilk(main.readRawDataToString()); + System.out.println(data); + } + + @Test + public void testChangeBread() throws Exception { + String data = main.changeBread(main.readRawDataToString()); + System.out.println(data); + } + + @Test + public void testChangeCookies() throws Exception { + String data = main.changeCookies(main.readRawDataToString()); + System.out.println(data); + } + + @Test + public void testChangeApples() throws Exception { + String data = main.changeApples(main.readRawDataToString()); + System.out.println(data); + } + + @Test + public void testPoundToNewLine() throws Exception { + String data = main.poundToNewLine(main.readRawDataToString()); + System.out.println(data); + } + + @Test + public void testCorrectSeparator() throws Exception { + String data = main.correctSeparator(); + System.out.println(data); + } + + @Test + public void testNameChange() throws Exception { + String data = main.nameChange(main.readRawDataToString()); + System.out.println(data); + } + + @Test + public void testPriceChange() throws Exception { + String data = main.priceChange(main.readRawDataToString()); + System.out.println(data); + } + + @Test + public void testFindGroceries() { + //given + int breadExpected = 6; + int appleExpected = 4; + int cookieExpected = 8; + int milkExpected = 8; + //when + int breadCount = main.findGroceries("bread"); + int appleCount = main.findGroceries("apple"); + int cookieCount = main.findGroceries("cookie"); + int milkCount = main.findGroceries("milk"); + //then + Assert.assertEquals(breadExpected, breadCount); + Assert.assertEquals(appleExpected, appleCount); + Assert.assertEquals(cookieExpected, cookieCount); + Assert.assertEquals(milkExpected, milkCount); + System.out.printf("Bread: %s, Apple: %s, Cookie: %s, Milk: %s", breadCount, appleCount, cookieCount, milkCount); + } + + @Test + public void testCountingErrors() { + //given + int expected = 4; + //when + int actual = main.countingErrors(); + //then + Assert.assertEquals(expected, actual); + } + + @Test + public void testReadyForFormatting() { + System.out.println(main.readyForFormatting()); + } + + @Test + public void testDoingTheFormatting() { + System.out.println(main.doingTheFormatting()); + } +} diff --git a/target/classes/Main.class b/target/classes/Main.class index c9d3858bc278f548c9eaabc471ed4e0ba034dcf0..e68bcf6452b0bb68a1414e2eb72d4c6ad33a59b0 100644 GIT binary patch literal 6325 zcmb_g33yc175;B#k~f(=k}x3&30ok7AuF3!6Hpt8nK#Y?!KGEJ zwzlp|-Kx^MV69cr8d9-Z+ge+@@3o8Fcf0Slmj36xH(QeW@mVHc-n+}W=lu6<_uTi= zf1Z98zzVgQH&A|r8=e~py2H`LJ=+tT1m1X zgR&{s_%Q?J8Y*;DVx~e#hHHD+h{qL*hs=cC?6QnO;ZjwXEjw$f&4+4*qRqy>GmV7N zoES~`F&lF<%+)auH45bun5Q_FQN(65ZY5(ub9KBo5i^Yug~fGQa5)%?47ItELsm;i zB$`Y(q-}mIK&^%obS%Uog*jGisKtmH!C|u{XpM|mk$8)&*tjJT3Uf!kgypcy$e6$| zdxNfIKXvPo$6_7zXrMBQa?sstUz*{J9ObxcH8d&c_U0*QT_|k&&_Z1g#jKzi+us$6 zCmd}xVyU>xGOB8-&C^L5u|mg5I9Xv@kN^^9kGW46+D>#%Ax^=m8d`Oo^l@ z>Cr|sYDNYXnx=w3$($~0+OHAob!-r|YV?I3W5g7;XNf@@{OCfrh8`UoMKfh7J!V1Y zvZ!n%9P#07lGq51uvln+jX6sF#x?Y2t?k&jb~I>46CsN$wlLgQM{3Mi%!<`m!C*2L zGY9?HjIBD}DzRH)Gw)3p!99I3B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