public class TextLogik { private final Text text; private final TextOutput textOutput; /** * Initiates a new instance of the class TextLogik. * Contains command instructions for the class Text. * Defined the different cases and appointed to specific commands. */ public TextLogik() { text = new Text(); textOutput = new TextOutput(); String[] command; textOutput.userInfoOutput("#######################"); textOutput.userInfoOutput("#WELCOME TO THE EDITOR#"); textOutput.userInfoOutput("#######################"); do { textOutput.userInfoOutput("Please enter a Command: "); command = TextInput.checkForInput(); switch (command[0]) { case "ADD": String inputText = ""; while(inputText.length() == 0) { textOutput.userInfoOutput("Please enter your text: "); inputText = TextInput.getTextInput(); } if (command.length == 1) { checkIfSuccess(text.add(inputText)); } else if (isNumeric(command[1])) { int line = Integer.parseInt(command[1]); checkIfSuccess(text.add(line, inputText)); } else { textOutput.errorInvalidCommand(); } break; case "DEL": if (command.length == 1) { checkIfSuccess(text.del()); } else if (isNumeric(command[1])) { int line = Integer.parseInt(command[1]); checkIfSuccess(text.del(line)); } else { textOutput.errorInvalidCommand(); } break; case "DUMMY": if (command.length == 1) { checkIfSuccess(text.dummy()); } else if (isNumeric(command[1])) { int line = Integer.parseInt(command[1]); checkIfSuccess(text.dummy(line)); } else { textOutput.errorInvalidCommand(); } break; case "EXIT": break; case "FORMAT": if (command.length > 1 && "RAW".equals(command[1])) { checkIfSuccess(textOutput.formatRaw()); } else if (command.length > 2 && "FIX".equals(command[1]) && isNumeric(command[2])) { textOutput.formatFix(Integer.parseInt(command[2])); } else { textOutput.errorInvalidCommand(); } break; case "INDEX": textOutput.indexOutput(text.index()); break; case "PRINT": textOutput.print(text.getText()); break; case "REPLACE": String oldChar = ""; while (oldChar.length() == 0) { 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]); checkIfSuccess(text.replace(line, oldChar, newChar)); } else { textOutput.errorInvalidCommand(); } break; default: textOutput.errorInvalidCommand(); break; } } while (!"EXIT".equals(command[0])); } /** * Method to check if a command is numeric. * * @param str command that should be a number. * @return true if the given str matches. */ private boolean isNumeric(String str) { return str.matches("\\d+"); } private void checkIfSuccess(boolean method) { if (method) { textOutput.userInfoOutput("Command was successfull"); } else { textOutput.errorInvalidParagraph(); } } }