Edited README, added test methods the test class and renamed methods by its action, filled java docs, replaced redundant code scripts

This commit is contained in:
MikeZyeman 2021-11-12 19:30:18 +01:00
parent 8c6531dd82
commit 49272e936c
5 changed files with 121 additions and 100 deletions

View File

@ -1,17 +1,28 @@
#Texteditor # 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 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
###ADD a paragraph 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``` ```ADD```
Adds a paragraph with custom text.<br/> <br/> Adds a paragraph with custom text.<br/> <br/>
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]``` ```ADD [n]```
Adds a paragraph with custom text at a specific position in text.<br/> <br/> Adds a paragraph with custom text at a specific position in text.<br/> <br/>
Enter the command add with the paragraph number where it should be added. Enter the command add with the paragraph number where it should be added. After submitting the add [n] command you will
After submitting the add [n] command you will be prompted to enter the text to add in the corresponding paragraph. be prompted to enter the text to add in the corresponding paragraph.
###DUMMY Text ### DUMMY
```DUMMY``` ```DUMMY```
Inserts a paragraph with dummy text at the end of the text.<br/> <br/> Inserts a paragraph with dummy text at the end of the text.<br/> <br/>
Enter the command dummy to enter a pre-written paragraph into the text as your last paragraph. 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.<br/> <br/> Inserts a paragraph with dummy text at a specific position.<br/> <br/>
Enter the command dummy with a paragraph number to insert a pre-written dummy text paragraph to a spcific 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``` ```REPLACE```
Replaces desired words in the last paragraph with a desired replacement.<br/> <br/> Replaces desired words in the last paragraph with a desired replacement.<br/> <br/>
Enter the command replace, after submitting the command replace enter the characters that should be replaced. After submitting the characters you will be 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 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. need to input the characters wich will replace the previous characters.
###DELETE a Text ### DELETE
```DEL``` ```DEL```
Deletes the last paragraph.<br/> <br/> Deletes the last paragraph.<br/> <br/>
Enter the command del to delete the last paragraph. Enter the command del to delete the last paragraph.
@ -41,18 +52,19 @@ Enter the command del to delete the last paragraph.
Deletes the selected paragraph.<br/> <br/> Deletes the selected paragraph.<br/> <br/>
Enter the command del with a paragraph number, the corresponding paragraph will then be deleted. Enter the command del with a paragraph number, the corresponding paragraph will then be deleted.
###INDEX the words ### INDEX
```INDEX``` ```INDEX```
Diplays the index of words used 3 times or more.<br/> <br/> Diplays the index of words used 3 times or more.<br/> <br/>
Enter the command index to display the words that are displayed more than three times in the text with their corresponding paragraphs. 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``` ```PRINT```
Prints the text with the previously set format option.<br/> <br/> Prints the text with the previously set format option.<br/> <br/>
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). 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. 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``` ```FORMAT RAW```
Sets the format to raw.<br/> <br/> Sets the format to raw.<br/> <br/>
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
@ -60,11 +72,12 @@ and each paragraph will be printed on one line with its corresponding paragraph
```FORMAT FIX [n]``` ```FORMAT FIX [n]```
Sets the format fix with the maximum line length.<br/> <br/> Sets the format fix with the maximum line length.<br/> <br/>
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 Enter the command format fix and the max line length which will be used. After you changed the format to fix you can use
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. the command ```PRINT``` again and the text will be displayed with the max length in mind and a new line will be created
If a word is too long to fit the line length, it will be displayed as whole, ignoring the max length. 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)

View File

@ -30,12 +30,12 @@ public class Text {
* Method to add a paragraph at a specific position. * Method to add a paragraph at a specific position.
* *
* @param paragraphNumber number of paragraph where the new text should be added. * @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. * @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)) { if (paragraphExists(paragraphNumber)) {
this.text.add((paragraphNumber - 1), text); text.add((paragraphNumber - 1), newText);
return true; return true;
} }
return false; return false;
@ -44,11 +44,11 @@ public class Text {
/** /**
* Method to add a paragraph at the end of the existing 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. * @return returns true if the paragraph is added successfully.
*/ */
public boolean add(String text) { public boolean add(String newText) {
this.text.add(text.replaceAll("[^A-Za-z0-9 .,:?!\"'-]", "")); text.add(newText.replaceAll("[^A-Za-z0-9 .,:?!\"'-]", ""));
return true; return true;
} }
@ -104,11 +104,7 @@ public class Text {
* @return returns true if the paragraph is changed successfully. * @return returns true if the paragraph is changed successfully.
*/ */
public boolean replace(String oldChar, String newChar) { public boolean replace(String oldChar, String newChar) {
if (paragraphExists(text.size())) { return replace(text.size(), oldChar, newChar);
text.set((text.size() - 1), text.get(text.size() - 1).replace(oldChar, newChar));
return true;
}
return false;
} }
/** /**
@ -116,7 +112,7 @@ public class Text {
* If the parameter is valid it deletes the specific pararaph otherwise it will return false. * If the parameter is valid it deletes the specific pararaph otherwise it will return false.
* *
* @param paragraphNumber the paragraph which should be deleted. * @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) { public boolean del(int paragraphNumber) {
if (paragraphExists(paragraphNumber)) { if (paragraphExists(paragraphNumber)) {
@ -129,14 +125,10 @@ public class Text {
/** /**
* This method deletes the last paragraph in the text Array. * 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() { public boolean del() {
if (paragraphExists(text.size())) { return del(text.size());
text.remove(text.size() - 1);
return true;
}
return false;
} }
/** /**
@ -144,18 +136,16 @@ public class Text {
* Every word which is used in the text more than 3 times * 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. * will be added to the ArrayList with the paragraphs where the word has been used.
* *
* @return ArrayList<String> * @return ArrayList<String> list of all words with the index of all lines, in which the word appears
*/ */
public ArrayList<String> index() { public ArrayList<String> index() {
HashMap<String, ArrayList<Integer>> wordbook = new HashMap<>(); HashMap<String, ArrayList<Integer>> wordbook = new HashMap<>();
createWordlist(wordbook); createWordlist(wordbook);
String input;
ArrayList<String> output = new ArrayList<>(); ArrayList<String> output = new ArrayList<>();
ArrayList<Integer> values;
for (Map.Entry<String, ArrayList<Integer>> entry : wordbook.entrySet()) { for (Map.Entry<String, ArrayList<Integer>> entry : wordbook.entrySet()) {
input = ""; String input = "";
String key = entry.getKey(); String key = entry.getKey();
values = entry.getValue(); ArrayList<Integer> values = entry.getValue();
if (values.get(0) >= 3) { if (values.get(0) >= 3) {
input += key + " "; input += key + " ";
for (int i = 1; i < values.size(); i++) { 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. * 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<String, ArrayList<Integer>> wordbook) { private void createWordlist(HashMap<String, ArrayList<Integer>> wordbook) {
int counter;
ArrayList<Integer> numbersList; ArrayList<Integer> numbersList;
String firstLetter;
String restLetters;
for (int i = 0; i < text.size(); i++) { for (int i = 0; i < text.size(); i++) {
String[] words = text.get(i).trim().toLowerCase().split("[. ,:?!\"'-]+"); String[] words = text.get(i).trim().toLowerCase().split("[. ,:?!\"'-]+");
for (String word : words) { for (String word : words) {
//Words get formatted consistently //Words get formatted consistently
if (word.length() > 0) { if (word.length() > 0) {
counter = 1; int counter = 1;
firstLetter = word.substring(0, 1); String firstLetter = word.substring(0, 1);
restLetters = word.substring(1); String restLetters = word.substring(1);
word = firstLetter.toUpperCase() + restLetters; word = firstLetter.toUpperCase() + restLetters;
//Words are beeing counted //Words are beeing counted
if (wordbook.containsKey(word)) { if (wordbook.containsKey(word)) {

View File

@ -13,6 +13,8 @@ public class TextLogic {
private final TextOutput textOutput; private final TextOutput textOutput;
/** /**
* @Constructor: TextLogic()
* <p>
* Initiates a new instance of the class TextLogic. * Initiates a new instance of the class TextLogic.
* Contains command instructions for the class Text. * Contains command instructions for the class Text.
* Defined the different cases and appointed to specific commands. * Defined the different cases and appointed to specific commands.
@ -106,7 +108,6 @@ public class TextLogic {
textOutput.errorInvalidCommand(); textOutput.errorInvalidCommand();
break; break;
} }
} while (!"EXIT".equals(command[0])); } while (!"EXIT".equals(command[0]));
} }
@ -120,9 +121,17 @@ public class TextLogic {
return str.matches("\\d+"); return str.matches("\\d+");
} }
private void checkIfSuccess(boolean method) { /**
if (method) { * Method for checking if a command has been executed successfully.
textOutput.userInfoOutput("Command was successfull"); * 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 { } else {
textOutput.errorInvalidParagraph(); textOutput.errorInvalidParagraph();
} }

View File

@ -1,7 +1,8 @@
import java.util.ArrayList; import java.util.ArrayList;
/** /**
* New class TextOutput * @Class TextOutput
* <p>
* This class puts out the finished text and formats the text if wanted. * This class puts out the finished text and formats the text if wanted.
* Author: Leonardo Brandenberger * Author: Leonardo Brandenberger
* Date: 12.11.2021 * Date: 12.11.2021
@ -20,7 +21,7 @@ public class TextOutput {
if (text.size() == 0) { if (text.size() == 0) {
errorMissingText(); errorMissingText();
} else if (formatRaw) { } else if (formatRaw) {
printFormated(text); printFormatted(text);
} else { } else {
toFormat(text); toFormat(text);
} }
@ -31,7 +32,7 @@ public class TextOutput {
* *
* @param text the ArrayList which is used for the output * @param text the ArrayList which is used for the output
*/ */
private void printFormated(ArrayList<String> text) { private void printFormatted(ArrayList<String> text) {
for (int counter = 0; counter < text.size(); counter++) { for (int counter = 0; counter < text.size(); counter++) {
System.out.println("<" + (counter + 1) + ">: <" + text.get(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. * 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<String> index) { public void indexOutput(ArrayList<String> index) {
if (index.size() == 0) { if (index.size() == 0) {

View File

@ -15,7 +15,7 @@ public class TextTest {
ArrayList<String> stringListe; ArrayList<String> stringListe;
/** /**
* This method is here as a preparation for the test. * This method prepares local parameters by resets them for each test method
*/ */
@BeforeEach @BeforeEach
void setup() { void setup() {
@ -23,6 +23,58 @@ public class TextTest {
txt = new Text(); txt = new Text();
} }
/**
* Test methode: indexNormalTest()
* <p>
* 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()
* <p>
* 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()
* <p>
* 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<String>(), txt.index());
}
/**
* Test method: indexWithAddedEmptyLine()
* <p>
* 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<String>(), txt.index());
}
/** /**
* Test Method for add and dummy methods * Test Method for add and dummy methods
*/ */
@ -75,47 +127,4 @@ public class TextTest {
Assertions.assertEquals("Test Test hallo Test", txt.getText().get(2)); 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();
}
} }