Merge remote-tracking branch 'origin/main' into main

This commit is contained in:
MikeZyeman 2021-11-11 21:18:34 +01:00
commit 7683868668
8 changed files with 66 additions and 71 deletions

File diff suppressed because one or more lines are too long

View File

@ -25,6 +25,9 @@ public class Text {
* @return returns true if the given paragraph exists and is added successfully * @return returns true if the given paragraph exists and is added successfully
*/ */
public boolean add(int paragraphNumber, String text) { public boolean add(int paragraphNumber, String text) {
if(text.length() < 1) {
return false;
}
if (paragraphExists(paragraphNumber)) { if (paragraphExists(paragraphNumber)) {
this.text.add((paragraphNumber - 1), text); this.text.add((paragraphNumber - 1), text);
return true; return true;
@ -39,7 +42,10 @@ public class Text {
* @return returns true if the paragraph is added successfully * @return returns true if the paragraph is added successfully
*/ */
public boolean add(String text) { public boolean add(String text) {
this.text.add(text); if(text.length() < 1) {
return false;
}
this.text.add(text.replaceAll("[^A-Za-z0-9 .,:?!\"'-]",""));
return true; return true;
} }
@ -95,9 +101,12 @@ public class Text {
* @return returns true if the paragraph is changed successfully * @return returns true if the paragraph is changed successfully
*/ */
public boolean replace(String oldChar, String newChar) { public boolean replace(String oldChar, String newChar) {
if(paragraphExists((text.size()))) {
text.set((text.size() - 1), text.get(text.size() - 1).replace(oldChar, newChar)); text.set((text.size() - 1), text.get(text.size() - 1).replace(oldChar, newChar));
return true; return true;
} }
return false;
}
/** /**
* This method deletes a specific Paragraph. As a Parameter it uses a int. * This method deletes a specific Paragraph. As a Parameter it uses a int.
@ -120,9 +129,12 @@ public class Text {
* @return True: if paragraph has been deleted. * @return True: if paragraph has been deleted.
*/ */
public boolean del() { public boolean del() {
if(paragraphExists(text.size())){
text.remove(text.size() - 1); text.remove(text.size() - 1);
return true; return true;
} }
return false;
}
/** /**
* This method creates a ArrayList<String>. * This method creates a ArrayList<String>.
@ -167,7 +179,7 @@ public class Text {
String firstLetter; String firstLetter;
String restLetters; String restLetters;
for (int i = 0; i < text.size(); i++) { for (int i = 0; i < text.size(); i++) {
String[] words = text.get(i).trim().toLowerCase().split("[ :;.,!?><'/\n]+"); String[] words = text.get(i).trim().toLowerCase().split("[ :;.,!?><+*}{)('/\n]+");
for (String word : words) { for (String word : words) {
//Words get formatted consistently //Words get formatted consistently
counter = 1; counter = 1;

View File

@ -5,7 +5,7 @@ public class TextInput {
private static final Scanner sc = new Scanner(System.in); private static final Scanner sc = new Scanner(System.in);
public static String[] checkForInput() { public static String[] checkForInput() {
return sc.nextLine().split(" "); return sc.nextLine().toUpperCase().split(" ");
} }
public static String getTextInput() { public static String getTextInput() {

View File

@ -12,11 +12,16 @@ public class TextLogik {
textOutput = new TextOutput(); textOutput = new TextOutput();
String[] command; String[] command;
textOutput.userInfoOutput("######################");
textOutput.userInfoOutput("#WELCOME TO THE EDITOR#");
textOutput.userInfoOutput("######################");
do { do {
textOutput.userInfoOutput("Please enter a Command: ");
command = TextInput.checkForInput(); command = TextInput.checkForInput();
switch (command[0]) { switch (command[0]) {
case "ADD": case "ADD":
textOutput.userInfoOutput("Please enter your text: ");
if (command.length == 1) { if (command.length == 1) {
checkIfSuccess(text.add(TextInput.getTextInput())); checkIfSuccess(text.add(TextInput.getTextInput()));
} else if (isNumeric(command[1])) { } else if (isNumeric(command[1])) {
@ -29,10 +34,10 @@ public class TextLogik {
break; break;
case "DEL": case "DEL":
if (command.length == 1) { if (command.length == 1) {
text.del(); checkIfSuccess(text.del());
} else if (isNumeric(command[1])) { } else if (isNumeric(command[1])) {
int line = Integer.parseInt(command[1]); int line = Integer.parseInt(command[1]);
text.del(line); checkIfSuccess(text.del(line));
} else { } else {
textOutput.errorInvalidCommand(); textOutput.errorInvalidCommand();
} }
@ -51,9 +56,9 @@ public class TextLogik {
break; break;
case "FORMAT": case "FORMAT":
if (command.length > 1 && "RAW".equals(command[1])) { if (command.length > 1 && "RAW".equals(command[1])) {
textOutput.formatRaw(); checkIfSuccess(textOutput.formatRaw());
} else if (command.length > 2 && "FIX".equals(command[1]) && isNumeric(command[2])) { } else if (command.length > 2 && "FIX".equals(command[1]) && isNumeric(command[2])) {
textOutput.formatFix(Integer.parseInt(command[2])); checkIfSuccess(textOutput.formatFix(Integer.parseInt(command[2])));
} else { } else {
textOutput.errorInvalidCommand(); textOutput.errorInvalidCommand();
} }
@ -63,17 +68,22 @@ public class TextLogik {
textOutput.indexOutput(text.index()); textOutput.indexOutput(text.index());
break; break;
case "PRINT": case "PRINT":
textOutput.print(text.getText()); textOutput.print(text.getText());
break; break;
case "REPLACE": case "REPLACE":
if (command.length == 1){ String oldChar = "";
checkIfSuccess(text.replace(TextInput.getTextInput(), TextInput.getTextInput())); while (oldChar.length() == 0) {
}else if(isNumeric(command[1])) { textOutput.userInfoOutput("Please enter your text to replace: ");
oldChar = TextInput.getTextInput();
}
textOutput.userInfoOutput("Please enter the new text: ");
String newChar = TextInput.getTextInput();
if (command.length == 1) {
checkIfSuccess(text.replace(oldChar, newChar));
} else if (isNumeric(command[1])) {
int line = Integer.parseInt(command[1]); int line = Integer.parseInt(command[1]);
checkIfSuccess(text.replace(line, TextInput.getTextInput(), TextInput.getTextInput())); checkIfSuccess(text.replace(line, oldChar, newChar));
}else { } else {
textOutput.errorInvalidCommand(); textOutput.errorInvalidCommand();
} }
@ -97,9 +107,9 @@ public class TextLogik {
} }
private void checkIfSuccess(boolean method) { private void checkIfSuccess(boolean method) {
if(method) { if (method) {
textOutput.userInfoOutput("Command was successfully"); textOutput.userInfoOutput("Command was successfull");
}else { } else {
textOutput.errorInvalidParagraph(); textOutput.errorInvalidParagraph();
} }
} }

View File

@ -33,43 +33,6 @@ public class TextOutput {
} }
private void toFormatold(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 {
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
}
}
/** /**
* Method to print out the paragraphs with the length taken from columnWidth and adding leftover words to the next * Method to print out the paragraphs with the length taken from columnWidth and adding leftover words to the next
* paragraph. * paragraph.
@ -77,8 +40,8 @@ public class TextOutput {
* @param text the ArrayList which is used for the output. * @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;
for (String paragraph : text) { for (String paragraph : text) {
int currentLength = 0;
String[] words = paragraph.split(" "); String[] words = paragraph.split(" ");
int lastWordNumber = words.length; int lastWordNumber = words.length;
int currentWordNumber = 0; int currentWordNumber = 0;
@ -86,7 +49,16 @@ public class TextOutput {
currentWordNumber++; currentWordNumber++;
if (word.length()<= columnWidth - currentLength) { if (word.length()<= columnWidth - currentLength) {
System.out.print(word); System.out.print(word);
if (!(lastWordNumber == currentWordNumber)) { }
else {
if(currentWordNumber != 1) {
System.out.println();
currentLength = 0;
}
System.out.print(word);
}
if(!(currentWordNumber == lastWordNumber)) {
System.out.print(" "); System.out.print(" ");
currentLength = currentLength + word.length() + 1; currentLength = currentLength + word.length() + 1;
} }
@ -94,15 +66,6 @@ public class TextOutput {
System.out.println(); System.out.println();
} }
} }
else {
System.out.println();
System.out.print(word);
if(!(currentWordNumber == lastWordNumber)) {
System.out.print(" ");
currentLength = word.length();
}
}
}
} }
} }

View File

@ -27,5 +27,9 @@ class TextOutputTest {
@Test @Test
public void print() { public void print() {
textOutput.print(text); textOutput.print(text);
System.out.println("fertig");
textOutput.formatRaw();
textOutput.print(text);
System.out.println("fertig");
} }
} }

View File

@ -92,4 +92,10 @@ public class TextTest {
stringListe = txt.index(); stringListe = txt.index();
Assertions.assertEquals(0,stringListe.size()); Assertions.assertEquals(0,stringListe.size());
} }
@Test
void testing() {
txt.add("");
txt.index();
}
} }