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,19 +123,24 @@ 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 = "";
try {
if (rbDirekt.isSelected()) {
ergebnis = Textverbinder.texteVerbindenDirekt(text1, text2);
}
if (rbLeerzeichen.isSelected()) { if (rbLeerzeichen.isSelected()) {
verbinder = " "; // spacebar ergebnis = Textverbinder.texteVerbindenLeerZeichen(text1, text2);
} }
if (rbLeerzeile.isSelected()) { if (rbLeerzeile.isSelected()) {
verbinder = "\n"; // NewLine ergebnis = Textverbinder.texteVerbindenLeerZeile(text1, text2);
} }
if (!text1.isEmpty() && !text2.isEmpty()) { if (!text1.isEmpty() && !text2.isEmpty()) {
String ergebnis = text1 + verbinder + text2;
taErgebnis.setText(ergebnis); taErgebnis.setText(ergebnis);
taErgebnis.setDisable(false); taErgebnis.setDisable(false);
} else { } else {
@ -146,7 +153,10 @@ public class RootBorderPane extends BorderPane {
Main.showAlert(AlertType.ERROR, "Text2 fehlt"); Main.showAlert(AlertType.ERROR, "Text2 fehlt");
} }
} }
}
} catch (TextException e) {
Main.showAlert(AlertType.ERROR, e.getMessage());
} }
} }

View File

@ -4,8 +4,7 @@ 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,57 +2,54 @@ 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 public static String texteVerbindenLeerZeile(String text1, String text2) throws TextException {
{
if (text1 != null && text2 != null) if (text1 != null && text2 != null)
return texteVerbinden(text1, text2, "\n"); return texteVerbinden(text1, text2, "\n");
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 + "\")");
} }
private static String texteVerbinden(String text1, String text2, String verbinder) throws TextException private static String texteVerbinden(String text1, String text2, String verbinder) throws TextException {
{
if (text1 != null && text2 != null && verbinder != null) if (text1 != null && text2 != null && verbinder != null)
if (checkTextEmpty(text1) && checkTextEmpty(text2)) if (checkTextEmpty(text1) && checkTextEmpty(text2))
if (checkVerbinder(verbinder)) if (checkVerbinder(verbinder)) {
{
StringBuilder sb = new StringBuilder().append(text1).append(verbinder).append(text2); StringBuilder sb = new StringBuilder().append(text1).append(verbinder).append(text2);
return sb.toString(); return sb.toString();
} } else
else
throw new TextException("Fehler: verbinder ist ungueltig (verbinder: \"" + verbinder + "\")"); throw new TextException("Fehler: verbinder ist ungueltig (verbinder: \"" + verbinder + "\")");
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 + "\")");
else else
throw new TextException("Fehler: null-Referenz fuer text1, text2 oder verbinder erhalten (text1: \"" + text1 + " \", text2: \"" + text2 + "\", " + ", verbinder: \"" + verbinder); throw new TextException("Fehler: null-Referenz fuer text1, text2 oder verbinder erhalten (text1: \"" + text1
+ " \", text2: \"" + text2 + "\", " + ", verbinder: \"" + verbinder);
} }
private static boolean checkTextEmpty(String text) 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;