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/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..0f083cb 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,5 +1,7 @@ import org.apache.commons.io.IOUtils; import java.io.IOException; +import java.util.regex.Matcher; +import java.util.regex.Pattern; public class Main { @@ -11,7 +13,187 @@ public String readRawDataToString() throws Exception{ public static void main(String[] args) throws Exception{ String output = (new Main()).readRawDataToString(); - System.out.println(output); + // String actualResult = changeCookies(changeApples(changeBread(changeMilk(output)))); + // System.out.println(actualResult); + + } + + public 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 String changeBread (String input) { + try { + Pattern pattern = Pattern.compile("bread", Pattern.CASE_INSENSITIVE); + Matcher matcher = pattern.matcher(input); + String milk = matcher.replaceAll("Bread"); + + return milk; + } catch (Exception e) { + throw new UnsupportedOperationException(); + } + } + + public String changeCookies (String input) { + try { + Pattern pattern = Pattern.compile("c[o0][o0]kies", Pattern.CASE_INSENSITIVE); + Matcher matcher = pattern.matcher(input); + String milk = matcher.replaceAll("Cookies"); + return milk; + } catch (Exception e) { + throw new UnsupportedOperationException(); + } + } + + public String changeApples (String input) { + try { + Pattern pattern = Pattern.compile("apples", Pattern.CASE_INSENSITIVE); + Matcher matcher = pattern.matcher(input); + String milk = matcher.replaceAll("Apples"); + return milk; + } catch (Exception e) { + throw new UnsupportedOperationException(); + } + } + + public 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 String correctSeparator () { + // "[!@^%*]" + try { + String jawn = readRawDataToString(); + Pattern patter = Pattern.compile("[!@^%*]"); + Matcher matcher = patter.matcher(jawn); + String result = matcher.replaceAll(";"); + return result; + } catch (Exception e) { + throw new UnsupportedOperationException(); + } + } + + public String nameChange(String input) { + try { + Pattern pattern = Pattern.compile("name", Pattern.CASE_INSENSITIVE); + Matcher matcher = pattern.matcher(input); + String milk = matcher.replaceAll("Name"); + return milk; + } catch (Exception e) { + throw new UnsupportedOperationException(); + } + } + + public String priceChange(String input) { + try { + Pattern pattern = Pattern.compile("price", Pattern.CASE_INSENSITIVE); + Matcher matcher = pattern.matcher(input); + String milk = matcher.replaceAll("Price"); + return milk; + } catch (Exception e) { + throw new UnsupportedOperationException(); + } + } + + public int findGroceries(String input) { + Integer counter = 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; + } + counter++; + } + return counter; + } + + + public 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") + " 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 int countingErrors() { + int counter = 0; + // beware + // receive 2 errors on 'milk' - milk shows more than the given prices (2 times) + counter += findGroceries("Name:;"); + counter += findGroceries("milk") - (findGroceries("milk;price:3.23") + findGroceries("milk;price:1.23")); + return counter; + } + + public 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 void parsingToObject(String input) { + Pattern pattern = Pattern.compile(input); + Matcher matcher = pattern.matcher(readyForFormatting()); } } diff --git a/src/main/test/MainTest.java b/src/main/test/MainTest.java new file mode 100644 index 0000000..910c44c --- /dev/null +++ b/src/main/test/MainTest.java @@ -0,0 +1,48 @@ +import org.junit.Test; + +public class MainTest { + + Main test = new Main(); + +// @Test +// public void milkChangeTest() { +// String result = test.changeMilk(); +// System.out.println(result); +// } + + @Test + public void poundNewLineTest() { + String result = test.poundToNewLine(test.correctSeparator()); + System.out.println(result); + } + + @Test // weird output + public void correctedSeparatorTest() { + String result = test.correctSeparator(); + System.out.println(result); + } + +// @Test +// public void applesChangeTest() { +// String result = test.changeApples(); +// System.out.println(result); +// } + + @Test + public void entireFormattingTest() { + String result = test.poundToNewLine(test.correctSeparator()); + String result1 = test.changeApples(result); + String result2 = test.changeBread(result1); + String result3 = test.changeCookies(result2); + String result4 = test.changeMilk(result3); + String result5 = test.nameChange(result4); + String result6 = test.priceChange(result5); + System.out.println(result6); + } + + @Test + public void printTest() { + String result = test.doingTheFormatting(); + System.out.println(result); + } +} diff --git a/target/classes/Main.class b/target/classes/Main.class index c9d3858..278f6f2 100644 Binary files a/target/classes/Main.class and b/target/classes/Main.class differ