diff --git a/src/main/java/com/example/vpr_javafx/Data.java b/src/main/java/com/example/vpr_javafx/Data.java index 1fd7dd3..d7be404 100644 --- a/src/main/java/com/example/vpr_javafx/Data.java +++ b/src/main/java/com/example/vpr_javafx/Data.java @@ -3,7 +3,9 @@ package com.example.vpr_javafx; import javafx.scene.control.Alert; import javafx.scene.control.PasswordField; import javafx.scene.control.TextField; +import org.w3c.dom.Text; +import java.awt.*; import java.io.File; import java.io.FileWriter; import java.io.IOException; @@ -285,8 +287,11 @@ public class Data { /** * The method validates the user input * - * @return boolean inputValid - * @author Kevin Maier, Kevin Pfannenstiel + * @param tfPhone Phone number the user typed in + * @param pfPassword Password the user typed in + * @param controller Controller + * @return phoneNumberValid && passwordValid + * @author Kevin Maier, Kevin Pfannenstiel */ public boolean validateData(TextField tfPhone, PasswordField pfPassword, HelloController controller) { @@ -295,6 +300,8 @@ public class Data { boolean phoneNumberValid = false; boolean passwordValid = false; + Alert alert = new Alert(Alert.AlertType.WARNING); + HashMap users = readLoginData(); String phoneNumber = tfPhone.getText(); @@ -305,10 +312,8 @@ public class Data { if (password.isEmpty() || phoneNumber.isEmpty()) { - Alert alert = new Alert(Alert.AlertType.WARNING); - alert.setContentText("Login fehlgeschlagen. Es wurde nichts eingegeben."); + alert.setContentText("Login fehlgeschlagen. Anmeldedaten unvollständig."); alert.show(); - return false; } Matcher phoneNumberMatcher = phoneNumberPattern.matcher(phoneNumber); @@ -319,22 +324,18 @@ public class Data { if (!users.containsKey(phoneNumber) || !phoneNumberMatchFound || phoneNumber.length() >= 15) { - Alert alert = new Alert(Alert.AlertType.WARNING); alert.setContentText("Login fehlgeschlagen. Die eingegebenen Daten sind falsch."); alert.show(); - return false; } else { phoneNumberValid = true; } - if (!users.get(phoneNumber).equals(password) || !passwordMatchFound || password.length() <8) + if (!users.get(phoneNumber).equals(password) || !passwordMatchFound) { - Alert alert = new Alert(Alert.AlertType.WARNING); alert.setContentText("Login fehlgeschlagen. Die eingegebenen Daten sind falsch."); alert.show(); - return false; } else { @@ -345,6 +346,78 @@ public class Data { return phoneNumberValid && passwordValid; } + /** + * + * @param tfPhone + * @param pfPassword + * @param controller + * @author Kevin Maier + */ + public boolean validateRegistration(TextField tfName, TextField tfPhone, PasswordField pfPassword, TextField tfStreet, TextField tfHouseNumber, TextField tfPostalCode, TextField tfCity, TextField tfChild, HelloController controller) + { + boolean phoneNumberMatchFound; + boolean passwordMatchFound; + boolean postalCodeFound; + boolean phoneNumberValid = false; + boolean passwordValid = false; + boolean postalCodeValid = false; + + Alert alert = new Alert(Alert.AlertType.WARNING); + + HashMap users = readLoginData(); + + String phoneNumber = tfPhone.getText(); + String password = pfPassword.getText(); + String postalCode = tfPostalCode.getText(); + + Pattern phoneNumberPattern = Pattern.compile("[0-9]*"); + Pattern passwordPattern = Pattern.compile("^(?=.*[A-Za-z])(?=.*\\d)[A-Za-z\\d]{8,}$"); + Pattern postalCodePattern = Pattern.compile("[0-9]{5}"); + + if (tfName.getText().isEmpty() || tfPhone.getText().isEmpty() || pfPassword.getText().isEmpty() || tfStreet.getText().isEmpty() || tfHouseNumber.getText().isEmpty() || tfPostalCode.getText().isEmpty() || tfCity.getText().isEmpty() || tfChild.getText().isEmpty()) + { + alert.setContentText("Registrierung fehlgeschlagen. Registrierungsdaten unvollständig."); + alert.show(); + } + + Matcher phoneNumberMatcher = phoneNumberPattern.matcher(phoneNumber); + Matcher passwordMatcher = passwordPattern.matcher(password); + Matcher postalCodeMatcher = postalCodePattern.matcher(postalCode); + + phoneNumberMatchFound = phoneNumberMatcher.find(); + passwordMatchFound = passwordMatcher.find(); + postalCodeFound = postalCodeMatcher.find(); + + if (users.containsKey(phoneNumber) || !phoneNumberMatchFound || phoneNumber.length() >= 15) + { + alert.setContentText("Registrierung fehlgeschlagen. Die eingegebenen Telefonnummer ist bereits vergeben oder ist nicht korrekt."); + alert.show(); + } + else + { + phoneNumberValid = true; + } + if (!passwordMatchFound) + { + alert.setContentText("Registrierung fehlgeschlagen. Das Passwort muss mindestens 8 Zeichen lang sein und mindestens 1 Ziffer und 1 Zeichen beinhalten."); + alert.show(); + } + else + { + passwordValid = true; + } + if (!postalCodeFound) + { + alert.setContentText("Registrierung fehlgeschlagen. Die Postleitzahl ist nicht richtig."); + } + else + { + postalCodeValid = true; + } + + return phoneNumberValid && passwordValid && postalCodeValid; + } + /** * The method changes the orders.txt file by removing canceled orders diff --git a/src/main/java/com/example/vpr_javafx/HelloController.java b/src/main/java/com/example/vpr_javafx/HelloController.java index f4e89cc..361a657 100644 --- a/src/main/java/com/example/vpr_javafx/HelloController.java +++ b/src/main/java/com/example/vpr_javafx/HelloController.java @@ -304,7 +304,7 @@ public class HelloController { } /** - * OnRegistrationButton() lässt einen neuen Nutzer anlegen + * OnRegistrationButton() creates a new user * @param event * @throws IOException * @author Kevin Maier @@ -313,17 +313,18 @@ public class HelloController { protected void OnRegistrationButton(ActionEvent event) throws IOException { Alert alert = new Alert(Alert.AlertType.WARNING); - Data validData = new Data("user.txt"); + Data validRegistration = new Data("user.txt"); - if (tfName.getText().isEmpty() || tfPhone.getText().isEmpty() || tfChild.getText().isEmpty()) + if (validRegistration.validateRegistration(tfName, tfPhone, pfPassword, tfStreet, tfHouseNumber, tfPostalCode, tfCity, tfChild, this)) { - alert.setContentText("Registrierungsdaten unvollständig"); - } - else if (validData.validateData(tfPhone, pfPassword, this)) - { - try (FileWriter writer = new FileWriter("user.txt")){ - writer.write(tfName.getText() + ";" + tfStreet.getText() + " " + tfHouseNumber.getText() + "," + tfPostalCode + " " + tfCity.getText() + ";" + tfPhone.getText() + ";" + tfChild.getText() + ";" + pfPassword.getText()); - writer.flush(); + try (FileWriter writer = new FileWriter("user.txt", true)){ + writer.write(tfName.getText() + ";" + tfStreet.getText() + " " + tfHouseNumber.getText() + "," + tfPostalCode.getText() + " " + tfCity.getText() + ";" + tfPhone.getText() + ";" + tfChild.getText() + ";" + pfPassword.getText() + System.lineSeparator()); + + Parent root = FXMLLoader.load(getClass().getResource("MenuOverview-view.fxml")); + Stage stage = (Stage) ((Node) event.getSource()).getScene().getWindow(); + Scene scene = new Scene(root); + stage.setScene(scene); + stage.show(); } catch (IOException e) { diff --git a/target/classes/com/example/vpr_javafx/Data.class b/target/classes/com/example/vpr_javafx/Data.class index 3666df4..0fcf1aa 100644 Binary files a/target/classes/com/example/vpr_javafx/Data.class and b/target/classes/com/example/vpr_javafx/Data.class differ diff --git a/target/classes/com/example/vpr_javafx/HelloController.class b/target/classes/com/example/vpr_javafx/HelloController.class index 3e7d74d..1634636 100644 Binary files a/target/classes/com/example/vpr_javafx/HelloController.class and b/target/classes/com/example/vpr_javafx/HelloController.class differ diff --git a/target/classes/com/example/vpr_javafx/Registration-view.fxml b/target/classes/com/example/vpr_javafx/Registration-view.fxml index 7a41d50..537e19a 100644 --- a/target/classes/com/example/vpr_javafx/Registration-view.fxml +++ b/target/classes/com/example/vpr_javafx/Registration-view.fxml @@ -1,14 +1,16 @@ + - + - - + + + @@ -17,18 +19,28 @@ + + -