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(); RootBorderPane root = new RootBorderPane();
Scene scene = new Scene(root, 700, 500); Scene scene = new Scene(root, 700, 500);
primaryStage.setScene(scene); primaryStage.setScene(scene);
primaryStage.setTitle("GUI-Uebung 4"); primaryStage.setTitle("GUI-Uebung 5");
primaryStage.show(); primaryStage.show();
} catch (Exception e) { } catch (Exception e) {
showAlert(AlertType.ERROR, e.getMessage()); showAlert(AlertType.ERROR, e.getMessage());

View File

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

View File

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

View File

@ -2,58 +2,55 @@ package model;
public class Textverbinder { 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) if (text1 != null && text2 != null)
return texteVerbinden(text1, text2, ""); return texteVerbinden(text1, text2, "");
else 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) if (text1 != null && text2 != null)
return texteVerbinden(text1, text2, " "); return texteVerbinden(text1, text2, " ");
else 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 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) 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()) if (text != null && !text.isEmpty())
return true; return true;
else else
return false; return false;
} }
private static boolean checkVerbinder(String text) private static boolean checkVerbinder(String text) {
{ if (text != null && (text.equals("") || text.equals(" ") || text.equals("\n")))
if (text != null && (text.equals("") || text.equals(" ") || text.equals("\n") ))
return true; return true;
return false; return false;
} }