From 88225e30809d5f5fbd3e9796948dfb6569699ed7 Mon Sep 17 00:00:00 2001 From: laura Date: Mon, 9 Aug 2021 19:57:00 -0400 Subject: [PATCH] first --- Hurtlocker.iml | 18 --- pom.xml | 12 ++ src/main/java/Grocery.java | 41 ++++++ src/main/java/Main.java | 256 ++++++++++++++++++++++++++++++++++- src/test/java/MaintTest.java | 145 ++++++++++++++++++++ target/classes/Main.class | Bin 1162 -> 6699 bytes 6 files changed, 449 insertions(+), 23 deletions(-) delete mode 100644 Hurtlocker.iml create mode 100644 src/main/java/Grocery.java create mode 100644 src/test/java/MaintTest.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/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/Grocery.java b/src/main/java/Grocery.java new file mode 100644 index 0000000..8515ff2 --- /dev/null +++ b/src/main/java/Grocery.java @@ -0,0 +1,41 @@ +import java.text.SimpleDateFormat; + +public class Grocery { + + String name; + Double price; + SimpleDateFormat date; + + public Grocery() { + } + + public Grocery(String name, Double price, SimpleDateFormat date) { + this.name = name; + this.price = price; + this.date = date; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Double getPrice() { + return price; + } + + public void setPrice(Double price) { + this.price = price; + } + + public SimpleDateFormat getDate() { + return date; + } + + public void setDate(SimpleDateFormat date) { + this.date = date; + } +} diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 632942a..2bbfc48 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,12 +1,35 @@ -import org.apache.commons.io.IOUtils; + + +import java.io.File; import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + + public class Main { - public String readRawDataToString() throws Exception{ - ClassLoader classLoader = getClass().getClassLoader(); - String result = IOUtils.toString(classLoader.getResourceAsStream("RawData.txt")); - return result; + public static String readRawDataToString() { + ClassLoader classLoader = Main.class.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 static void main(String[] args) throws Exception{ @@ -14,4 +37,227 @@ public static void main(String[] args) throws Exception{ System.out.println(output); } + + public String findAlphabetCharacters () { + String result = ""; + String jerkText = readRawDataToString(); + Pattern pattern = Pattern.compile("[A-Z]", Pattern.CASE_INSENSITIVE); + Matcher matcher = pattern.matcher(jerkText); + String str = ""; + while (matcher.find()) { + str = matcher.group(); + result+= str; + + } + return result; + } + + public String getList() { + // String result = ""; + String jerkTest = readRawDataToString(); + + Pattern pattern = Pattern.compile("##"); + Matcher matcher = pattern.matcher(jerkTest); + + return matcher.replaceAll("\n"); + + + } + +// +// public int countMilk(){ +// List indices = new ArrayList<>(); +// +// String jerkText = readRawDataToString(); +// //any instance of the name milk +// //find a price of 3.23 +// Pattern pattern = Pattern.compile("milk", Pattern.CASE_INSENSITIVE); +// Matcher matcher = pattern.matcher(jerkText); +// for (int i = 0; matcher.find(); i++) { +// indices.add(matcher.start()); +// } +// return indices.size(); +// } +// +// +// public String correctSeparator(){ +// String fix = getList(); +// Pattern pattern = Pattern.compile("[!@^%*]"); +// Matcher matcher = pattern.matcher(fix); +// +// return matcher.replaceAll(";"); +// +// } +// +// public String separateBySemiColon(){ +// String list = correctSeparator(); +// Pattern pattern = Pattern.compile(";"); +// Matcher matcher = pattern.matcher(list); +// +// return matcher.replaceAll("\n"); +// +// +// } +// + + 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 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; + } + + 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; + } } diff --git a/src/test/java/MaintTest.java b/src/test/java/MaintTest.java new file mode 100644 index 0000000..a0c008c --- /dev/null +++ b/src/test/java/MaintTest.java @@ -0,0 +1,145 @@ +import org.junit.Assert; +import org.junit.Test; + +public class MaintTest { + + Main main = new Main(); + + @Test + public void findAlPhaTest() { + //GIVEN + + //WHEN + + //THEN + String look = main.findAlphabetCharacters(); + + System.out.println(look); + } + + @Test + public void formList() { + String look = main.getList(); + System.out.println(look); + } + + + @Test + public void correctSeparatorTest(){ + String look = main.correctSeparator(); + + System.out.println(look); + } + + @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()); + } + + +} +// +// @Test +// public void findMilkTest(){ +// int actual = test.countMilk(); +// int expected = 8; +// +// Assert.assertEquals(expected, actual); +// +// } + +// @Test +// public void correctAndSeparateTest(){ +// String look = test.separateBySemiColon(); +// +// System.out.println(look); +// } \ No newline at end of file diff --git a/target/classes/Main.class b/target/classes/Main.class index c9d3858bc278f548c9eaabc471ed4e0ba034dcf0..aa2a9d87a3e6c7a13241356021658be3f74fa375 100644 GIT binary patch literal 6699 zcmbtY33yyp75?umc`q+7Z8~j|rfbsDEoqvrAPE%O(n9GZElt|mRR*-5(e*ai=u< zrMXMUck8%E2KQ>X&%j9Bui*g$mH44d{>Xp_H%s&5Qv3uzHSjY$sN*3G4;vVdN2Gbw zz!`WZU3F4Xa~j%Rc{tK(7~ z&*`{K$MXiR!V3nj#)~5PCF#8^wBO3`chbBf&8yPOAV6kQuiMr7;~ed(@9iadg? zp-8MnVPw(@b*>KWI3|=1b;R4#$w+Lg!l>ypg4;sdLru|8Y-^K4Jp6CK7L2zH;f#u$4$h;sT;991W#X!FZ@s_E+bzUy_>; z`CB4#uwhv?%Qz;zBx$93qiKbjysZxE#l4XzxpGl?dpZ>EZVe@DO=!wPemzuzLe=z* zMf+N8)rOK=sjQKMOjBsQH=XFEP$PR3uC3Y%0x-X?F>uAdt;p)@iuFRxUoV-IG#*e;dHx|pbFD*YC*{c zM{_(?EYxEyu}?;@GN2bFl96zh=(k5SyBOryWISvoZ7`LCsbIN+*%gm=()rhhqP=A1 zIrqYD>(qu9&F2?(S2cfn}7Qay(8SU}jWZ05G zQ7Dz5pC!R-;!`-0niGPiG*juSlsd5En&IgzFLUpOS?Qf=QyHpEQ>LlP(aGIv7z6dd zYrqXQ1t^TLC-W}>*)`l$6>5a30%W2_s!;@Kn5t5+jLdQC=t{Leswp^I6lBLi9=2>4C``*CUK308CKB;v+Ui`Hu;{V8*z#8T-3TcUZ;a1AQU2V> z$i_%XCML9vbw{5xrJleQ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