added improved Unformated class prototype
This commit is contained in:
parent
6e921233df
commit
4c304e06e3
|
@ -1,17 +1,29 @@
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Iterator;
|
||||||
|
|
||||||
|
|
||||||
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
|
||||||
|
*
|
||||||
|
* @param text the ArrayList which is then formated into the desired output.
|
||||||
|
*/
|
||||||
public void print(ArrayList<String> text) {
|
public void print(ArrayList<String> text) {
|
||||||
if (formatRaw) {
|
if (formatRaw) {
|
||||||
printFormated(text);
|
printFormated(text);
|
||||||
} else {
|
} else {
|
||||||
printUnformated(text);
|
printUnformatedV2(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<String> text) {
|
private void printFormated(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) + ">");
|
||||||
|
@ -19,6 +31,12 @@ 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 printUnformated(ArrayList<String> text) {
|
private void printUnformated(ArrayList<String> text) {
|
||||||
for (String paragraph : text) {
|
for (String paragraph : text) {
|
||||||
String[] words = paragraph.split(" ");
|
String[] words = paragraph.split(" ");
|
||||||
|
@ -29,7 +47,6 @@ public class TextOutput {
|
||||||
int letterLenght = letters.length;
|
int letterLenght = letters.length;
|
||||||
int lettersPrinted = 0;
|
int lettersPrinted = 0;
|
||||||
do {
|
do {
|
||||||
//System.out.println();
|
|
||||||
currentLength = 0;
|
currentLength = 0;
|
||||||
for (int i = 0; i < columnWidth; i++) {
|
for (int i = 0; i < columnWidth; i++) {
|
||||||
if (letterLenght > 0) {
|
if (letterLenght > 0) {
|
||||||
|
@ -57,20 +74,82 @@ public class TextOutput {
|
||||||
System.out.println(); //added
|
System.out.println(); //added
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
private void printUnformatedV2(ArrayList<String> text) {
|
||||||
|
for(String paragraph : text) {
|
||||||
|
if (paragraph.length() <= columnWidth) {
|
||||||
|
System.out.println(paragraph);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
String[] word = paragraph.split(" ");
|
||||||
|
int currentLineLength = 0;
|
||||||
|
for(String eachword : word) {
|
||||||
|
if (eachword.length() > columnWidth) {
|
||||||
|
String remainingText = eachword;
|
||||||
|
do {
|
||||||
|
if (remainingText.length() < columnWidth) {
|
||||||
|
System.out.println(remainingText);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
System.out.println(remainingText.substring(0, columnWidth));
|
||||||
|
remainingText = remainingText.substring((columnWidth));
|
||||||
|
|
||||||
|
} while (!(remainingText.length() == 0));
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if (currentLineLength == 0) {
|
||||||
|
System.out.println();
|
||||||
|
}
|
||||||
|
if (columnWidth > eachword.length()) {
|
||||||
|
System.out.print(eachword);
|
||||||
|
if (!(currentLineLength == columnWidth)) {
|
||||||
|
System.out.print(" ");
|
||||||
|
}
|
||||||
|
currentLineLength = currentLineLength + eachword.length();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
System.out.println(eachword);
|
||||||
|
currentLineLength = eachword.length();
|
||||||
|
if (!(currentLineLength == columnWidth)) {
|
||||||
|
System.out.print(" ");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Method which sets the Variable formatRaw to true.
|
||||||
|
*/
|
||||||
public void formatRaw() {
|
public void formatRaw() {
|
||||||
formatRaw = true;
|
formatRaw = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method to set the formatRaw to false and also set the length of the desired paragraph length in order to limit
|
||||||
|
* the paragraph length when printing the text.
|
||||||
|
*
|
||||||
|
* @param length the maximum length of each paragraph that is allowed to be displayed when printing the text.
|
||||||
|
*/
|
||||||
public void formatFix(int length) {
|
public void formatFix(int length) {
|
||||||
formatRaw = false;
|
formatRaw = false;
|
||||||
columnWidth = length;
|
columnWidth = length;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method to give out the Error "Invalid String".
|
||||||
|
*/
|
||||||
public void errorInvalidString() {
|
public void errorInvalidString() {
|
||||||
System.err.println("Invalid String");
|
System.err.println("Invalid String");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method to give out the Error "Invalid Command".
|
||||||
|
*/
|
||||||
public void errorInvalidCommand() {
|
public void errorInvalidCommand() {
|
||||||
System.err.println("Invalid Command");
|
System.err.println("Invalid Command");
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,7 +15,7 @@ class TextOutputTest {
|
||||||
text = new ArrayList<>();
|
text = new ArrayList<>();
|
||||||
text.add("123456789");
|
text.add("123456789");
|
||||||
text.add("1234");
|
text.add("1234");
|
||||||
text.add("12 12 12");
|
text.add("12 12 12 12 12 12");
|
||||||
text.add(" ");
|
text.add(" ");
|
||||||
text.add("1eeeeeee8597389751");
|
text.add("1eeeeeee8597389751");
|
||||||
text.add("TextTextTextTextTextTextTextTextTextTextTextTextTextText TextTextTextTextTextTextTextTextTextTextTextText TextTextText");
|
text.add("TextTextTextTextTextTextTextTextTextTextTextTextTextText TextTextTextTextTextTextTextTextTextTextTextText TextTextText");
|
||||||
|
|
Loading…
Reference in New Issue