diff --git a/pom.xml b/pom.xml index e66b725..efb68c4 100644 --- a/pom.xml +++ b/pom.xml @@ -7,6 +7,18 @@ io.zipcoder collections 1.0-SNAPSHOT + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.8 + 1.8 + + + + diff --git a/src/main/java/io/zipcoder/ParenChecker.java b/src/main/java/io/zipcoder/ParenChecker.java index caee675..51b5d76 100644 --- a/src/main/java/io/zipcoder/ParenChecker.java +++ b/src/main/java/io/zipcoder/ParenChecker.java @@ -1,4 +1,48 @@ package io.zipcoder; +import java.util.ArrayList; +import java.util.Stack; + public class ParenChecker { -} + public ParenChecker() { + } + + public boolean braceChecker(String str) { + Stack stack = new Stack<>(); + for (int i = 0; i < str.length(); i++) { + char openCharacter = str.charAt(i); + if (openCharacter == '[' || openCharacter == '(' || openCharacter == '{' || openCharacter == '<') { + stack.push(openCharacter); + } else if (openCharacter == ']') { + if (stack.isEmpty() || stack.pop() != '[') { + return false; + } + } else if (openCharacter == ')') { + if (stack.isEmpty() || stack.pop() != '(') { + return false; + } + } else if (openCharacter == '}') { + if (stack.isEmpty() || stack.pop() != '{') { + return false; + } + } else if (openCharacter == '>') { + if (stack.isEmpty() || stack.pop() != '<') { + return false; + } + } else if (openCharacter == '\"') { + if (stack.isEmpty() || stack.peek() != '\"') { + stack.push(openCharacter); + } else { + stack.pop(); + } + } else if (openCharacter == '\'') { + if (stack.isEmpty() || stack.peek() != '\'') { + stack.push(openCharacter); + } else { + stack.pop(); + } + } + } + return stack.isEmpty(); + } +} \ No newline at end of file diff --git a/src/main/java/io/zipcoder/WC.java b/src/main/java/io/zipcoder/WC.java index babb68c..4bf4619 100644 --- a/src/main/java/io/zipcoder/WC.java +++ b/src/main/java/io/zipcoder/WC.java @@ -2,22 +2,55 @@ import java.io.FileNotFoundException; import java.io.FileReader; -import java.util.Iterator; -import java.util.Scanner; +import java.util.*; +import java.util.TreeMap; + +/** +@TODO +*@1.)Create a parenthesis verifier method, that can (check if all parenthesis are ('paired') +* @2.)Create a method that checks all opening characters have a closing one +* - These Characters () {} [] <> "" '' +* This program should be able to: + * 1 - count all the words in a file + * 2 - Print out all the words + * 3 - Print the word counts in descending order + */ public class WC { private Iterator si; + private TreeMap book; + + public static void main(String[] args) { + WC wordCounts = new WC("/Users/kieranthomas/Dev/CR-MesoLabs-Collections-EncapsulativeCharacters/src/main/resources/aGoodBookFile.txt"); + wordCounts.descendAndPrintText(); + } - public WC(String fileName) { - try { - this.si = new Scanner(new FileReader(fileName)); + public WC(String fileName) { //nullary constructor + try { //As soon as the scanner receives my book(map) it will get + this.book = new TreeMap<>(); + this.si = new Scanner(new FileReader(fileName)).useDelimiter("[^a-zA-Z]+"); + this.metaCharRemover(); } catch (FileNotFoundException e) { - System.out.println(fileName + " Does Not Exist"); + System.out.println(fileName + "Does Not Exist"); System.exit(-1); } } - public WC(Iterator si) { - this.si = si; + public WC(Iterator stringIterator) { + this.si = stringIterator; + } + + public void metaCharRemover() { + while (si.hasNext()) { //while the iterator has the next line do this stuff below it + String word = si.next().toLowerCase().replaceAll("[\"^${}().+&~!@#%*]", ""); //change all characters within file to lower case and replace all metacharacters with nothing so that only the words are counted when its time to count + Integer wordCount = book.getOrDefault(word, 0); //set my default value for word count to 0 + book.put(word, (wordCount + 1)); //insert my word and word count into my map + } + } + + private void descendAndPrintText() { //stream is pulling out JUST my words in sequential order first before sorting them in descending order, reverse is so that the highest number will print first and get lower accordingly + String newBook = ""; + newBook += book.entrySet().stream().sorted(Map.Entry.comparingByValue().reversed()) + "\n"; + System.out.println(newBook); } -} +} \ No newline at end of file diff --git a/src/main/resources/someTextFile.txt b/src/main/resources/LesMiserables.txt similarity index 100% rename from src/main/resources/someTextFile.txt rename to src/main/resources/LesMiserables.txt diff --git a/src/main/resources/aGoodBookFile.txt b/src/main/resources/aGoodBookFile.txt new file mode 100644 index 0000000..afe5c10 --- /dev/null +++ b/src/main/resources/aGoodBookFile.txt @@ -0,0 +1 @@ +Mary had a little lamb. Mary is 24 years old. \ No newline at end of file diff --git a/src/test/java/io/zipcoder/ParenCheckerTest.java b/src/test/java/io/zipcoder/ParenCheckerTest.java index 76aa3b6..68e09e6 100644 --- a/src/test/java/io/zipcoder/ParenCheckerTest.java +++ b/src/test/java/io/zipcoder/ParenCheckerTest.java @@ -4,5 +4,161 @@ import org.junit.Test; public class ParenCheckerTest { + ParenChecker parenChecker = new ParenChecker(); + + @Test + public void isPaired1Test(){ + //Given + String strToCheck = "(UltraInstinct)"; + Boolean expected = true; + + //When + boolean actual = parenChecker.braceChecker(strToCheck); + + //Then + Assert.assertEquals(expected,actual); + } + + @Test + public void isNotPaired1Test(){ + //Given + String strToCheck = "(UltraInstinct"; + Boolean expected = false; + + //When + boolean actual = parenChecker.braceChecker(strToCheck); + + //Then + Assert.assertEquals(expected,actual); + } + + @Test + public void isPaired2Test(){ + //Given + String strToCheck = "{}UltraInstinct"; + Boolean expected = true; + + //When + boolean actual = parenChecker.braceChecker(strToCheck); + + //Then + Assert.assertEquals(expected,actual); + } + + @Test + public void isNotPaired2Test(){ + //Given + String strToCheck = "UltraInstinct}"; + Boolean expected = false; + + //When + boolean actual = parenChecker.braceChecker(strToCheck); + + //Then + Assert.assertEquals(expected,actual); + } + + @Test + public void isPaired3Test(){ + //Given + String strToCheck = "[Ultra]Instinct"; + Boolean expected = true; + + //When + boolean actual = parenChecker.braceChecker(strToCheck); + + //Then + Assert.assertEquals(expected,actual); + } + + @Test + public void isNotPaired3Test(){ + //Given + String strToCheck = "UltraInstin]ct"; + Boolean expected = false; + + //When + boolean actual = parenChecker.braceChecker(strToCheck); + + //Then + Assert.assertEquals(expected,actual); + } + + @Test + public void isPaired4Test(){ + //Given + String strToCheck = "Ultra<>Instinct"; + Boolean expected = true; + + //When + boolean actual = parenChecker.braceChecker(strToCheck); + + //Then + Assert.assertEquals(expected,actual); + } + + @Test + public void isNotPaired4Test(){ + //Given + String strToCheck = "UltraInstinc>>t"; + Boolean expected = false; + + //When + boolean actual = parenChecker.braceChecker(strToCheck); + + //Then + Assert.assertEquals(expected,actual); + } + @Test + public void isPaired5Test(){ + //Given + String strToCheck = "Ultra\"Instinc\"t"; + Boolean expected = true; + + //When + boolean actual = parenChecker.braceChecker(strToCheck); + + //Then + Assert.assertEquals(expected,actual); + } + + @Test + public void isNotPaired5Test(){ + //Given + String strToCheck = "Ultra\"Instinct"; + Boolean expected = false; + + //When + boolean actual = parenChecker.braceChecker(strToCheck); + + //Then + Assert.assertEquals(expected,actual); + } + + @Test + public void isPaired6Test(){ + //Given + String strToCheck = "Ult\'raIn\'stinct"; + Boolean expected = true; + + //When + boolean actual = parenChecker.braceChecker(strToCheck); + + //Then + Assert.assertEquals(expected,actual); + } + + @Test + public void isNotPaired6Test(){ + //Given + String strToCheck = "Ultra\'Inst\'inct\'"; + Boolean expected = false; + + //When + boolean actual = parenChecker.braceChecker(strToCheck); + + //Then + Assert.assertEquals(expected,actual); + } } \ No newline at end of file diff --git a/src/test/java/io/zipcoder/WCTest.java b/src/test/java/io/zipcoder/WCTest.java index 895e831..53665c8 100644 --- a/src/test/java/io/zipcoder/WCTest.java +++ b/src/test/java/io/zipcoder/WCTest.java @@ -1,11 +1,21 @@ -package io.zipcoder; - -import org.junit.Assert; -import org.junit.Test; - -import java.util.ArrayList; -import java.util.Arrays; - -public class WCTest { - -} \ No newline at end of file +//package io.zipcoder; +// +//import org.junit.Assert; +//import org.junit.Test; +// +//import java.util.ArrayList; +//import java.util.Arrays; +// +//public class WCTest { +// +// @Test +// public void test() { +// WC wc = new WC("/Users/kieranthomas/Dev/CR-MesoLabs-Collections-EncapsulativeCharacters/src/main/resources/aGoodBookFile.txt"); +// +// String exptected = ""; +// String actual = wc.descendAndPrintText(); +// +// Assert.assertEquals(exptected, actual); +// +// } +//} \ No newline at end of file