import java.util.ArrayList; /** * New class TextOutput * This class puts out the finished text and formats the text if wanted. * Author: Leonardo Brandenberger * Date: 12.11.2021 */ public class TextOutput { private boolean formatRaw; private int columnWidth = 20; /** * Method that checks in which way the paragraphs should be displayed and then calls the corresponding * method to output the text as desired. * * @param text the ArrayList which is then formatted into the desired output. */ public void print(ArrayList text) { if (text.size() == 0) { errorMissingText(); } else if (formatRaw) { printFormated(text); } else { toFormat(text); } } /** * Method to print out the paragraphs in numerical order and with the whole paragraph as its length. * * @param text the ArrayList which is used for the output */ private void printFormated(ArrayList text) { for (int counter = 0; counter < text.size(); counter++) { System.out.println("<" + (counter + 1) + ">: <" + text.get(counter) + ">"); } } /** * Method to print out the paragraphs with the length taken from columnWidth and adding leftover words to the next * paragraph. * * @param text the ArrayList which is used for the output. */ private void toFormat(ArrayList text) { for (String paragraph : text) { int currentLength = 0; String[] words = paragraph.split(" "); int lastWordNumber = words.length; int currentWordNumber = 0; for (String word : words) { currentWordNumber++; if (word.length() >= columnWidth - currentLength && currentWordNumber != 1) { System.out.println(); currentLength = 0; } System.out.print(word); if (!(currentWordNumber == lastWordNumber)) { System.out.print(" "); currentLength = currentLength + word.length() + 1; } else { System.out.println(); } } System.out.println(); } } /** * Method which sets the Variable formatRaw to true. * * @return true to indicate that it was successful. */ public boolean formatRaw() { formatRaw = true; return true; } /** * Method to set the formatRaw to false and also to set the length of the desired paragraph length in order to limit * the paragraph length when printing the text. * * @param length the paragraph length when printing the text. */ public void formatFix(int length) { if (length > 0) { formatRaw = false; columnWidth = length; userInfoOutput("Command was successful"); } else { System.err.println("Minimum length has to be greater than 0"); } } /** * Method which outputs user information. * * @param info User information received as a String. */ public void userInfoOutput(String info) { System.out.println(info); } /** * Outputs the index that contains words with corresponding paragraphs. * * @param index ArrayList with words and in which part they have */ public void indexOutput(ArrayList index) { if (index.size() == 0) { userInfoOutput("index empty"); } else { for (String word : index) { System.out.println(word); } } } /** * Method to give out the Error "Invalid Paragraph". */ public void errorInvalidParagraph() { System.err.println("Invalid Paragraph"); } /** * Method to give out the Error "Invalid Command". */ public void errorInvalidCommand() { System.err.println("Invalid Command"); } /** * Method to give out the Error "Missing Text". */ public void errorMissingText() { System.err.println("Missing Text"); } }