gruppe06-hufflepuff-projekt.../src/TextOutput.java

78 lines
2.4 KiB
Java
Raw Normal View History

import java.util.ArrayList;
public class TextOutput {
private boolean formatRaw;
private int columnWidth = 10;
public void print(ArrayList<String> text) {
if (formatRaw) {
printFormated(text);
}
else {
printUnformated(text);
}
}
private void printFormated(ArrayList<String> text) {
for (int counter = 0; counter < text.size(); counter++) {
System.out.println("<" + (counter + 1) + ">: <" + text.get(counter) + ">");
}
}
private void printUnformated(ArrayList<String> text) {
for (String paragraph : text) {
String[] words = paragraph.split(" ");
int currentLength = 0;
for (String word : words) {
if (word.length() > columnWidth) {
String[] letters = word.split("");
int letterLenght = letters.length;
int lettersPrinted = 0;
do {
//System.out.println();
currentLength = 0;
for (int i = 0; i < columnWidth; i++) {
if (letterLenght > 0) {
System.out.print(letters[lettersPrinted]);
letterLenght--;
lettersPrinted++;
currentLength++;
}
}
if(!(letterLenght == 0)){
System.out.println();
}
}
while (letterLenght >= columnWidth);
}
else {
if (word.length() >= columnWidth - currentLength) {
currentLength = 0;
System.out.println();
}
System.out.print(word + " ");
currentLength += word.length() + 1;
}
}
System.out.println(); //added
}
}
public void formatRaw() {
formatRaw = true;
}
public void formatFix(int length) {
formatRaw = false;
columnWidth = length;
}
public void errorInvalidString() {
System.err.println("Invalid String");
}
public void errorInvalidCommand() {
System.err.println("Invalid Command");
}
}