Exception Handling & Change to MVC

This commit is contained in:
FUH22860 2022-05-20 13:16:25 +02:00
parent cfc00f77ec
commit 6bc2defbf3
4 changed files with 60 additions and 54 deletions

View File

@ -13,7 +13,7 @@ public class Main extends Application {
RootBorderPane root = new RootBorderPane();
Scene scene = new Scene(root, 700, 500);
primaryStage.setScene(scene);
primaryStage.setTitle("GUI-Uebung 4");
primaryStage.setTitle("GUI-Uebung 5");
primaryStage.show();
} catch (Exception e) {
showAlert(AlertType.ERROR, e.getMessage());

View File

@ -17,6 +17,8 @@ import javafx.scene.control.TextField;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import model.TextException;
import model.Textverbinder;
public class RootBorderPane extends BorderPane {
@ -121,22 +123,27 @@ public class RootBorderPane extends BorderPane {
private void verbinden() {
String text1 = tfText1.getText();
String text2 = tfText2.getText();
String verbinder = ""; // Direct
// String verbinder = ""; // Direct
String ergebnis = "";
if (rbLeerzeichen.isSelected()) {
verbinder = " "; // spacebar
}
try {
if (rbLeerzeile.isSelected()) {
verbinder = "\n"; // NewLine
}
if (rbDirekt.isSelected()) {
ergebnis = Textverbinder.texteVerbindenDirekt(text1, text2);
}
if (!text1.isEmpty() && !text2.isEmpty()) {
String ergebnis = text1 + verbinder + text2;
if (rbLeerzeichen.isSelected()) {
ergebnis = Textverbinder.texteVerbindenLeerZeichen(text1, text2);
}
taErgebnis.setText(ergebnis);
taErgebnis.setDisable(false);
} else {
if (rbLeerzeile.isSelected()) {
ergebnis = Textverbinder.texteVerbindenLeerZeile(text1, text2);
}
if (!text1.isEmpty() && !text2.isEmpty()) {
taErgebnis.setText(ergebnis);
taErgebnis.setDisable(false);
} else {
if (text1.isEmpty() && text2.isEmpty()) {
Main.showAlert(AlertType.ERROR, "Text1 fehlt\nText2 fehlt");
} else {
@ -145,8 +152,11 @@ public class RootBorderPane extends BorderPane {
} else {
Main.showAlert(AlertType.ERROR, "Text2 fehlt");
}
}
}
} catch (TextException e) {
Main.showAlert(AlertType.ERROR, e.getMessage());
}
}

View File

@ -1,11 +1,10 @@
package model;
public class TextException extends Exception {
private static final long serialVersionUID = 1L;
public TextException (String message)
{
public TextException(String message) {
super(message);
}

View File

@ -2,58 +2,55 @@ package model;
public class Textverbinder {
public static String texteVerbindenDirekt(String text1, String text2) throws TextException
{
public static String texteVerbindenDirekt(String text1, String text2) throws TextException {
if (text1 != null && text2 != null)
return texteVerbinden(text1, text2, "");
else
throw new TextException("Fehler: text1 oder text2 ungueltig (text1: \"" + text1 + "\", text2: \"" + text2 + "\")");
throw new TextException(
"Fehler: text1 oder text2 ungueltig (text1: \"" + text1 + "\", text2: \"" + text2 + "\")");
}
public static String texteVerbindenLeerZeichen(String text1, String text2) throws TextException
{
public static String texteVerbindenLeerZeichen(String text1, String text2) throws TextException {
if (text1 != null && text2 != null)
return texteVerbinden(text1, text2, " ");
else
throw new TextException("Fehler: text1 oder text2 ungueltig (text1: \"" + text1 + "\", text2: \"" + text2 + "\")");
}
public static String texteVerbindenLeerZeile(String text1, String text2) throws TextException
{
if (text1 != null && text2 != null)
return texteVerbinden(text1, text2, "\n");
else
throw new TextException("Fehler: text1 oder text2 ungueltig (text1: \"" + text1 + "\", text2: \"" + text2 + "\")");
}
private static String texteVerbinden(String text1, String text2, String verbinder) throws TextException
{
if (text1 != null && text2 != null && verbinder != null)
if (checkTextEmpty(text1) && checkTextEmpty(text2))
if (checkVerbinder(verbinder))
{
StringBuilder sb = new StringBuilder().append(text1).append(verbinder).append(text2);
return sb.toString();
}
else
throw new TextException("Fehler: verbinder ist ungueltig (verbinder: \"" + verbinder + "\")");
else
throw new TextException("Fehler: text1 oder text2 ungueltig (text1: \"" + text1 + "\", text2: \"" + text2 + "\")");
else
throw new TextException("Fehler: null-Referenz fuer text1, text2 oder verbinder erhalten (text1: \"" + text1 + " \", text2: \"" + text2 + "\", " + ", verbinder: \"" + verbinder);
throw new TextException(
"Fehler: text1 oder text2 ungueltig (text1: \"" + text1 + "\", text2: \"" + text2 + "\")");
}
private static boolean checkTextEmpty(String text)
{
public static String texteVerbindenLeerZeile(String text1, String text2) throws TextException {
if (text1 != null && text2 != null)
return texteVerbinden(text1, text2, "\n");
else
throw new TextException(
"Fehler: text1 oder text2 ungueltig (text1: \"" + text1 + "\", text2: \"" + text2 + "\")");
}
private static String texteVerbinden(String text1, String text2, String verbinder) throws TextException {
if (text1 != null && text2 != null && verbinder != null)
if (checkTextEmpty(text1) && checkTextEmpty(text2))
if (checkVerbinder(verbinder)) {
StringBuilder sb = new StringBuilder().append(text1).append(verbinder).append(text2);
return sb.toString();
} else
throw new TextException("Fehler: verbinder ist ungueltig (verbinder: \"" + verbinder + "\")");
else
throw new TextException(
"Fehler: text1 oder text2 ungueltig (text1: \"" + text1 + "\", text2: \"" + text2 + "\")");
else
throw new TextException("Fehler: null-Referenz fuer text1, text2 oder verbinder erhalten (text1: \"" + text1
+ " \", text2: \"" + text2 + "\", " + ", verbinder: \"" + verbinder);
}
private static boolean checkTextEmpty(String text) {
if (text != null && !text.isEmpty())
return true;
else
return false;
}
private static boolean checkVerbinder(String text)
{
if (text != null && (text.equals("") || text.equals(" ") || text.equals("\n") ))
private static boolean checkVerbinder(String text) {
if (text != null && (text.equals("") || text.equals(" ") || text.equals("\n")))
return true;
return false;
}