splitted print class into printUnfromated and printFormated.

This commit is contained in:
Leonardo Brandenberger 2021-11-05 15:08:25 +01:00
parent 9afb8458c3
commit 9378bf259a
6 changed files with 67 additions and 54 deletions

View File

@ -79,19 +79,23 @@ In der Grundfunktionalität sieht sie folgendermassen aus:
```java ```java
import java.util.Scanner; import java.util.Scanner;
/** /**
* Vereinfachte Demonstration Scanner-Funktion für Konsole * Modifiziert von BlueJ InputReader.java * Vereinfachte Demonstration Scanner-Funktion für Konsole * Modifiziert von BlueJ InputReader.java
* https://bluej.org/objects-first/resources/projects.zip * @author Für die Anpassungen: berp * https://bluej.org/objects-first/resources/projects.zip * @author Für die Anpassungen: berp
* @version 2019-10-27 * @version 2019-10-27
*/ */
public class InputReaderMod { public class InputReaderMod {
private Scanner scanner; private final Scanner scanner;
/** /**
* Main-Methode * Main-Methode
* @param args Eingabe vom Typ String * @param args Eingabe vom Typ String
*/ */
public static void main(String[] args) { public static void main(String[] args) {
InputReaderMod reader = new InputReaderMod(); System.out.print("Please enter your input: "); System.out.print("Input was: "+reader.getInput()+"\n"); InputReaderMod reader = new InputReaderMod();
System.out.print("Please enter your input: ");
System.out.print("Input was: " + reader.getInput() + "\n");
} }
/** /**

View File

@ -4,7 +4,7 @@ import java.util.*;
public class Text { public class Text {
private ArrayList<String> text = new ArrayList<>(); private final ArrayList<String> text = new ArrayList<>();
private static final String dummyText = "The standard Lorem Ipsum passage, used since the 1500s \"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.\" Section 1.10.32 of \"de Finibus Bonorum et Malorum\", written by Cicero in 45 BC \"Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?"; private static final String dummyText = "The standard Lorem Ipsum passage, used since the 1500s \"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.\" Section 1.10.32 of \"de Finibus Bonorum et Malorum\", written by Cicero in 45 BC \"Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?";
private HashMap<String, ArrayList<Integer>> wordbook = new HashMap<>(); private HashMap<String, ArrayList<Integer>> wordbook = new HashMap<>();
@ -165,7 +165,7 @@ public class Text {
*/ */
private HashMap<String, ArrayList<Integer>> createWordlist() { private HashMap<String, ArrayList<Integer>> createWordlist() {
for (int i = 0; i < text.size(); i++) { for (int i = 0; i < text.size(); i++) {
String[] woerter = text.get(i).trim().toLowerCase().split("[ :;.,!?><\'/]+"); String[] woerter = text.get(i).trim().toLowerCase().split("[ :;.,!?><'/]+");
for (String wort : woerter) { for (String wort : woerter) {
String firstLetter = wort.substring(0,1); String firstLetter = wort.substring(0,1);
String restLetters = wort.substring(1); String restLetters = wort.substring(1);

View File

@ -2,7 +2,7 @@ import java.util.Scanner;
public class TextInput { public class TextInput {
private static Scanner sc = new Scanner(""); private static final Scanner sc = new Scanner("");
public static String[] checkForInput() { public static String[] checkForInput() {
return sc.nextLine().split(""); return sc.nextLine().split("");

View File

@ -1,9 +1,9 @@
public class TextLogik { public class TextLogik {
private Text text; private final Text text;
public TextLogik() { public TextLogik() {
text = new Text(); text = new Text();
String command[]; String[] command;
do { do {
command = TextInput.checkForInput(); command = TextInput.checkForInput();

View File

@ -2,15 +2,24 @@ import java.util.ArrayList;
public class TextOutput { public class TextOutput {
private boolean formatRaw; private boolean formatRaw;
private int columnWidth; private int columnWidth = 10;
public void print(ArrayList<String> text) { public void print(ArrayList<String> text) {
if (formatRaw) { if (formatRaw) {
printFormated(text);
}
else {
printUnformated(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) + ">");
} }
} }
else { private void printUnformated(ArrayList<String> text) {
for (String paragraph : text) { for (String paragraph : text) {
String[] words = paragraph.split(" "); String[] words = paragraph.split(" ");
int currentLength = 0; int currentLength = 0;
@ -34,24 +43,14 @@ public class TextOutput {
while (letterLenght > columnWidth); while (letterLenght > columnWidth);
} }
else { else {
if (word.length() >= columnWidth - currentLength) { if (word.length() >= columnWidth - currentLength) {
System.out.println();
currentLength = 0; currentLength = 0;
System.out.println();
} }
System.out.print(word + " "); System.out.print(word + " ");
currentLength += word.length() + 1; currentLength += word.length() + 1;
if(words.length == word
}
}
} }
} }
} }
@ -64,4 +63,11 @@ public class TextOutput {
formatRaw = false; formatRaw = false;
columnWidth = length; columnWidth = length;
} }
public void errorInvalidString() {
System.err.println("Invalid String");
}
public void errorInvalidCommand() {
System.err.println("Invalid Command");
}
} }

View File

@ -17,8 +17,9 @@ class TextOutputTest {
textOutput.formatFix(9); textOutput.formatFix(9);
//textOutput.formatRaw(); //textOutput.formatRaw();
text = new ArrayList<>(); text = new ArrayList<>();
text.add("123 45678 "); text.add("1234 12345");
text.add("123456789"); text.add("1234");
text.add("12 12 12");
text.add("TextTextTextTextTextTextTextTextTextTextTextTextTextText TextTextTextTextTextTextTextTextTextTextTextText TextTextText"); text.add("TextTextTextTextTextTextTextTextTextTextTextTextTextText TextTextTextTextTextTextTextTextTextTextTextText TextTextText");
} }
@ -27,4 +28,6 @@ class TextOutputTest {
textOutput.print(text); textOutput.print(text);
} }
} }