Added JavaDocs, changed some formatting

This commit is contained in:
Leonardo Brandenberger 2021-11-09 20:54:54 +01:00
parent e96f492386
commit e2261dc2f4
2 changed files with 24 additions and 18 deletions

View File

@ -1,12 +1,15 @@
import java.util.ArrayList; import java.util.ArrayList;
/**
*
*/
public class TextOutput { public class TextOutput {
private boolean formatRaw; private boolean formatRaw;
private int columnWidth = 10; private int columnWidth = 10;
/** /**
* Method which checks in which way the paragraphs should be displayed. And then * Method which 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. * @param text the ArrayList which is then formatted into the desired output.
*/ */
@ -30,12 +33,6 @@ public class TextOutput {
} }
/**
* 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 toFormatold(ArrayList<String> text) { private void toFormatold(ArrayList<String> text) {
for (String paragraph : text) { for (String paragraph : text) {
String[] words = paragraph.split(" "); String[] words = paragraph.split(" ");
@ -73,17 +70,23 @@ public class TextOutput {
System.out.println(); //added System.out.println(); //added
} }
} }
/**
* 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<String> text) { private void toFormat(ArrayList<String> text) {
int currentLength = 0; int currentLength = 0;
for (String paragraph : text) { for (String paragraph : text) {
String[] words = paragraph.split(" "); String[] words = paragraph.split(" ");
int lastwordNumber = words.length; int lastWordNumber = words.length;
int currentWordNumber = 0; int currentWordNumber = 0;
for(String word : words) { for(String word : words) {
currentWordNumber++; currentWordNumber++;
if (word.length()<= columnWidth - currentLength) { if (word.length()<= columnWidth - currentLength) {
System.out.print(word); System.out.print(word);
if (!(lastwordNumber == currentWordNumber)) { if (!(lastWordNumber == currentWordNumber)) {
System.out.print(" "); System.out.print(" ");
currentLength = currentLength + word.length() + 1; currentLength = currentLength + word.length() + 1;
} }
@ -94,7 +97,7 @@ public class TextOutput {
else { else {
System.out.println(); System.out.println();
System.out.print(word); System.out.print(word);
if(!(currentWordNumber == lastwordNumber)) { if(!(currentWordNumber == lastWordNumber)) {
System.out.print(" "); System.out.print(" ");
currentLength = word.length(); currentLength = word.length();
} }

View File

@ -1,6 +1,7 @@
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.util.ArrayList; import java.util.ArrayList;
class TextOutputTest { class TextOutputTest {
@ -10,15 +11,17 @@ class TextOutputTest {
@BeforeEach @BeforeEach
public void setup() { public void setup() {
textOutput = new TextOutput(); textOutput = new TextOutput();
textOutput.formatFix(9); textOutput.formatFix(20);
//textOutput.formatRaw(); //textOutput.formatRaw();
text = new ArrayList<>(); text = new ArrayList<>();
text.add("123456789"); text.add("Virtute praecedunt, quod fere cotidianis proeliis cum Germanis contendunt, septentr ionesimmensoslongusw ordos.");
text.add("1234"); text.add("Virtutedasjdhashdkjhakjdhakdshjkashd praecedunt, quod fere cotidianis proeliis cum");
text.add("12 12 12 12 12 12"); //text.add("ordos.");
//text.add("1234");
text.add("12417575147517845 445264565");
text.add(" "); text.add(" ");
text.add("1eeeeeee8597389751"); //text.add("1eeeeeee8597389751");
text.add("TextTextTextTextTextTextTextTextTextTextTextTextTextText TextTextTextTextTextTextTextTextTextTextTextText TextTextText"); //text.add("TextTextTextTextTextTextTextTextTextTextTextTextTextText TextTextTextTextTextTextTextTextTextTextTextText TextTextText");
} }
@Test @Test