From 49272e936cecd8d34314dd2d6fe6fedbcfbfd9df Mon Sep 17 00:00:00 2001 From: MikeZyeman Date: Fri, 12 Nov 2021 19:30:18 +0100 Subject: [PATCH] Edited README, added test methods the test class and renamed methods by its action, filled java docs, replaced redundant code scripts --- README.md | 51 +++++++++++++++--------- src/Text.java | 47 +++++++++------------- src/TextLogic.java | 17 ++++++-- src/TextOutput.java | 9 +++-- test/TextTest.java | 97 +++++++++++++++++++++++++-------------------- 5 files changed, 121 insertions(+), 100 deletions(-) diff --git a/README.md b/README.md index 6437956..98fb199 100644 --- a/README.md +++ b/README.md @@ -1,17 +1,28 @@ -#Texteditor -Texteditor is a program to edit a text by using the input and is then able to output it as desired via the console. -##Usermanual -###ADD a paragraph +# Texteditor + +Texteditor is a program to edit a text by using the input and is then able to output it as desired via the console. This +program has been programmed in Java. + +## Usermanual + +Because the program has a command line interface, the user has to work with commands in order to work with the program. + +The user has to use the following commands in order to interact with the program and to execute the actions of each +command: + +### ADD + ```ADD``` Adds a paragraph with custom text.

-Enter the command add to add a new paragraph at the end of text. After submitting the add command you will be prompted to enter the text to add as last paragraph. +Enter the command add to add a new paragraph at the end of text. After submitting the add command you will be prompted +to enter the text to add as last paragraph. ```ADD [n]``` Adds a paragraph with custom text at a specific position in text.

-Enter the command add with the paragraph number where it should be added. -After submitting the add [n] command you will be prompted to enter the text to add in the corresponding paragraph. +Enter the command add with the paragraph number where it should be added. After submitting the add [n] command you will +be prompted to enter the text to add in the corresponding paragraph. -###DUMMY Text +### DUMMY ```DUMMY``` Inserts a paragraph with dummy text at the end of the text.

Enter the command dummy to enter a pre-written paragraph into the text as your last paragraph. @@ -20,7 +31,7 @@ Enter the command dummy to enter a pre-written paragraph into the text as your l Inserts a paragraph with dummy text at a specific position.

Enter the command dummy with a paragraph number to insert a pre-written dummy text paragraph to a spcific position. -###REPLACE a Text +### REPLACE ```REPLACE``` Replaces desired words in the last paragraph with a desired replacement.

Enter the command replace, after submitting the command replace enter the characters that should be replaced. After submitting the characters you will be @@ -32,7 +43,7 @@ Enter the command replace with a desired paragraph where the characters should b after submitting the command replace enter the characters that should be replaced. After submitting the characters you will be need to input the characters wich will replace the previous characters. -###DELETE a Text +### DELETE ```DEL``` Deletes the last paragraph.

Enter the command del to delete the last paragraph. @@ -41,30 +52,32 @@ Enter the command del to delete the last paragraph. Deletes the selected paragraph.

Enter the command del with a paragraph number, the corresponding paragraph will then be deleted. -###INDEX the words +### INDEX ```INDEX``` Diplays the index of words used 3 times or more.

Enter the command index to display the words that are displayed more than three times in the text with their corresponding paragraphs. -###Print the Text +### PRINT + ```PRINT``` Prints the text with the previously set format option.

Enter the command print to print out the current text you have. The Text will be printed with the settings set before (see FORMAT the Text). If no settings have been set the standard to output is the FIX format and a line length of 20. -###FORMAT the Text +### FORMAT ```FORMAT RAW``` Sets the format to raw.

-Enter the command format raw to change the print format to raw. After you changed the format to raw you can use the command ```PRINT``` again +Enter the command format raw to change the print format to raw. After you changed the format to raw you can use the command ```PRINT``` again and each paragraph will be printed on one line with its corresponding paragraph number. ```FORMAT FIX [n]``` Sets the format fix with the maximum line length.

-Enter the command format fix and the max line length which will be used. After you changed the format to fix you can use the command ```PRINT``` again -and the text will be displayed with the max length in mind and a new line will be created if no space is left for a paragraph. -If a word is too long to fit the line length, it will be displayed as whole, ignoring the max length. +Enter the command format fix and the max line length which will be used. After you changed the format to fix you can use +the command ```PRINT``` again and the text will be displayed with the max length in mind and a new line will be created +if no space is left for a paragraph. If a word is too long to fit the line length, it will be displayed as whole, +ignoring the max length. +## Class diagramm -##Classdiagramm +![Classdiagramm of this program](docs/PM2_ClassDiagram.drawio.svg) -![Classdiagramm](docs/PM2_ClassDiagram.drawio.svg) diff --git a/src/Text.java b/src/Text.java index dea7aca..df37cb9 100644 --- a/src/Text.java +++ b/src/Text.java @@ -30,12 +30,12 @@ public class Text { * Method to add a paragraph at a specific position. * * @param paragraphNumber number of paragraph where the new text should be added. - * @param text the Text which should be added. + * @param newText the Text which should be added. * @return returns true if the given paragraph exists and is added successfully. */ - public boolean add(int paragraphNumber, String text) { + public boolean add(int paragraphNumber, String newText) { if (paragraphExists(paragraphNumber)) { - this.text.add((paragraphNumber - 1), text); + text.add((paragraphNumber - 1), newText); return true; } return false; @@ -44,11 +44,11 @@ public class Text { /** * Method to add a paragraph at the end of the existing text. * - * @param text the text which should be added. + * @param newText the text which should be added. * @return returns true if the paragraph is added successfully. */ - public boolean add(String text) { - this.text.add(text.replaceAll("[^A-Za-z0-9 .,:?!\"'-]", "")); + public boolean add(String newText) { + text.add(newText.replaceAll("[^A-Za-z0-9 .,:?!\"'-]", "")); return true; } @@ -104,11 +104,7 @@ public class Text { * @return returns true if the paragraph is changed successfully. */ public boolean replace(String oldChar, String newChar) { - if (paragraphExists(text.size())) { - text.set((text.size() - 1), text.get(text.size() - 1).replace(oldChar, newChar)); - return true; - } - return false; + return replace(text.size(), oldChar, newChar); } /** @@ -116,7 +112,7 @@ public class Text { * If the parameter is valid it deletes the specific pararaph otherwise it will return false. * * @param paragraphNumber the paragraph which should be deleted. - * @return False: If the int is not a valid paragraph. || True: If the int is a valid paragraph number. + * @return boolean = false: If the int is not a valid paragraph. || True: If the int is a valid paragraph number. */ public boolean del(int paragraphNumber) { if (paragraphExists(paragraphNumber)) { @@ -129,14 +125,10 @@ public class Text { /** * This method deletes the last paragraph in the text Array. * - * @return True: if paragraph has been deleted. + * @return boolean: if paragraph has been deleted. */ public boolean del() { - if (paragraphExists(text.size())) { - text.remove(text.size() - 1); - return true; - } - return false; + return del(text.size()); } /** @@ -144,18 +136,16 @@ public class Text { * Every word which is used in the text more than 3 times * will be added to the ArrayList with the paragraphs where the word has been used. * - * @return ArrayList + * @return ArrayList list of all words with the index of all lines, in which the word appears */ public ArrayList index() { HashMap> wordbook = new HashMap<>(); createWordlist(wordbook); - String input; ArrayList output = new ArrayList<>(); - ArrayList values; for (Map.Entry> entry : wordbook.entrySet()) { - input = ""; + String input = ""; String key = entry.getKey(); - values = entry.getValue(); + ArrayList values = entry.getValue(); if (values.get(0) >= 3) { input += key + " "; for (int i = 1; i < values.size(); i++) { @@ -173,20 +163,19 @@ public class Text { /** * This method counts all words in text and adds the count together with the paragraph where the word has been used to a Hashmap. + * + * @param wordbook */ private void createWordlist(HashMap> wordbook) { - int counter; ArrayList numbersList; - String firstLetter; - String restLetters; for (int i = 0; i < text.size(); i++) { String[] words = text.get(i).trim().toLowerCase().split("[. ,:?!\"'-]+"); for (String word : words) { //Words get formatted consistently if (word.length() > 0) { - counter = 1; - firstLetter = word.substring(0, 1); - restLetters = word.substring(1); + int counter = 1; + String firstLetter = word.substring(0, 1); + String restLetters = word.substring(1); word = firstLetter.toUpperCase() + restLetters; //Words are beeing counted if (wordbook.containsKey(word)) { diff --git a/src/TextLogic.java b/src/TextLogic.java index f3feb4e..ec117f2 100644 --- a/src/TextLogic.java +++ b/src/TextLogic.java @@ -13,6 +13,8 @@ public class TextLogic { private final TextOutput textOutput; /** + * @Constructor: TextLogic() + *

* Initiates a new instance of the class TextLogic. * Contains command instructions for the class Text. * Defined the different cases and appointed to specific commands. @@ -106,7 +108,6 @@ public class TextLogic { textOutput.errorInvalidCommand(); break; } - } while (!"EXIT".equals(command[0])); } @@ -120,9 +121,17 @@ public class TextLogic { return str.matches("\\d+"); } - private void checkIfSuccess(boolean method) { - if (method) { - textOutput.userInfoOutput("Command was successfull"); + /** + * Method for checking if a command has been executed successfully. + * If it is success it would output a message, which tells the user that is was successfully executed. + * If it failed during execution it would output a message, + * which tells the user that the execution of command has failed + * + * @param result is the result of the executed command + */ + private void checkIfSuccess(boolean result) { + if (result) { + textOutput.userInfoOutput("Command was successful!"); } else { textOutput.errorInvalidParagraph(); } diff --git a/src/TextOutput.java b/src/TextOutput.java index 9b21b07..95fc4c8 100644 --- a/src/TextOutput.java +++ b/src/TextOutput.java @@ -1,7 +1,8 @@ import java.util.ArrayList; /** - * New class TextOutput + * @Class TextOutput + *

* This class puts out the finished text and formats the text if wanted. * Author: Leonardo Brandenberger * Date: 12.11.2021 @@ -20,7 +21,7 @@ public class TextOutput { if (text.size() == 0) { errorMissingText(); } else if (formatRaw) { - printFormated(text); + printFormatted(text); } else { toFormat(text); } @@ -31,7 +32,7 @@ public class TextOutput { * * @param text the ArrayList which is used for the output */ - private void printFormated(ArrayList text) { + private void printFormatted(ArrayList text) { for (int counter = 0; counter < text.size(); counter++) { System.out.println("<" + (counter + 1) + ">: <" + text.get(counter) + ">"); } @@ -105,7 +106,7 @@ public class TextOutput { /** * Outputs the index that contains words with corresponding paragraphs. * - * @param index ArrayList with words and in which part they have + * @param index ArrayList with words and in which line one word appears */ public void indexOutput(ArrayList index) { if (index.size() == 0) { diff --git a/test/TextTest.java b/test/TextTest.java index 61d1372..c510014 100644 --- a/test/TextTest.java +++ b/test/TextTest.java @@ -15,7 +15,7 @@ public class TextTest { ArrayList stringListe; /** - * This method is here as a preparation for the test. + * This method prepares local parameters by resets them for each test method */ @BeforeEach void setup() { @@ -23,6 +23,58 @@ public class TextTest { txt = new Text(); } + /** + * Test methode: indexNormalTest() + *

+ * This test method checks, if the index method lists the words with all lines, in which they appear + * It should return "Word 1, 2" and "Test 1, 2, 3" + */ + @Test + void indexNormalTest() { + txt.add("Word word Test"); + txt.add("Word word etc. !!test zweite... Zeile"); + txt.add("Lorem ipsum lorem ipsum TEST"); + stringListe = txt.index(); + Assertions.assertEquals("Word 1, 2", stringListe.get(0)); + Assertions.assertEquals("Test 1, 2, 3", stringListe.get(1)); + } + + /** + * Test method: indexMultipleWordsTest() + *

+ * This method tests if the method get the first word, which appears multiple times + * Index() should return + */ + @Test + void indexMultipleWordsTest() { + txt.add("Word Word Word Test"); + Assertions.assertEquals("Word 1", txt.index().get(0)); + } + + /** + * Test method: indexEmptyTest() + *

+ * Tests the method index(), when there are no text saved or available + * It should return an empty Array with the type String + */ + @Test + void indexEmptyTest() { + Assertions.assertEquals(new ArrayList(), txt.index()); + } + + /** + * Test method: indexWithAddedEmptyLine() + *

+ * Test Method to check if the method index() is working with empty Strings + * It should return an empty ArrayList with the type String + */ + @Test + void indexWithAddedEmptyLine() { + txt.add(""); + Assertions.assertEquals(new ArrayList(), txt.index()); + } + + /** * Test Method for add and dummy methods */ @@ -75,47 +127,4 @@ public class TextTest { Assertions.assertEquals("Test Test hallo Test", txt.getText().get(2)); } - /** - * Test methode for Index method - * For TestCase Positiv Testcase One, Three and Four Negativ Testcase Two - */ - @Test - void indexTestOne() { - txt.add("Word word Test"); - txt.add("Word word etc. !!test zweite... Zeile"); - txt.add("Lorem ipsum lorem ipsum TEST"); - stringListe = txt.index(); - Assertions.assertEquals("Word 1, 2", stringListe.get(0)); - Assertions.assertEquals("Test 1, 2, 3", stringListe.get(1)); - } - - /** - * Test methode for Index method - * For TestCase Positiv Testcase Two - */ - @Test - void indexTestTwo() { - txt.add("\"Word word Word Test"); - stringListe = txt.index(); - Assertions.assertEquals("Word 1", stringListe.get(0)); - } - - /** - * Test methode for Index method - * For TestCase Negativ Testcase One - */ - @Test - void indexTestThree() { - stringListe = txt.index(); - Assertions.assertEquals(0, stringListe.size()); - } - - /** - * Test Method to check if index is working with empty Strings - */ - @Test - void indexTestFour() { - txt.add(""); - txt.index(); - } }