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 c9d3858..aa2a9d8 100644 Binary files a/target/classes/Main.class and b/target/classes/Main.class differ