Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package sandwichshopsystem;
- import java.util.function.UnaryOperator;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- import javafx.application.Application;
- import javafx.event.ActionEvent;
- import javafx.event.EventHandler;
- import javafx.geometry.Insets;
- import javafx.geometry.Pos;
- import javafx.scene.Node;
- import javafx.scene.Scene;
- import javafx.scene.control.Alert;
- import javafx.scene.control.Button;
- import javafx.scene.control.Label;
- import javafx.scene.control.TextField;
- import javafx.scene.control.TextFormatter;
- import javafx.scene.image.Image;
- import javafx.scene.image.ImageView;
- import javafx.scene.layout.BorderPane;
- import javafx.scene.layout.GridPane;
- import javafx.scene.layout.StackPane;
- import javafx.stage.Modality;
- import javafx.stage.Stage;
- /**
- *
- * @author 115465872
- */
- public class RegisterNewLoyaltyCustomer {
- public static void display()
- {
- Stage primaryStage = new Stage();
- primaryStage.setTitle("Payment");
- BorderPane pane = new BorderPane();
- pane.setStyle("-fx-padding: 10; -fx-background-color: #28ABE3" );
- //here we are creating a new image which will be our create new logo
- Image image = new Image("Image/NewCust.png");
- StackPane LogoBackGround = new StackPane(); //create a new stack pane to hold this image and allow us
- //to center it within the border top pane
- LogoBackGround.setPrefSize(75,75);//setting the size we want
- ImageView Logo = new ImageView(image); //creating a new image view that will be used
- Logo.setFitHeight(75); //fitting the image to the image view
- Logo.setFitWidth(75);
- LogoBackGround.setPadding(new Insets(30,0,0,0));
- LogoBackGround.getChildren().add(Logo); //adding the logo to the stack pane
- pane.setTop(LogoBackGround); //adding the stack pane to the top region of the borderpane
- pane.setCenter(CenterLayout());
- pane.setAlignment(CenterLayout(),Pos.CENTER);
- Scene scene = new Scene(pane);
- primaryStage.setResizable(false);
- primaryStage.setScene(scene);
- primaryStage.showAndWait();
- }
- private static StackPane CenterLayout(){
- StackPane CenterPane = new StackPane();
- CenterPane.setStyle("-fx-Background-color: White; -fx-border-color:Black;");
- TextField tfName = new TextField();
- //declaring a textformatter and setting the textformatter to the textfield. this intercepts the users input before it is written
- TextFormatter<String> textFormatter = getTextFormatter();
- tfName.setTextFormatter(textFormatter);
- TextField tfEmail = new TextField();
- //declaring a textformatter and setting the textformatter to the textfield. this intercepts the users input before it is written
- TextFormatter<String> textFormatter1 = getTextFormatter1();
- tfEmail.setTextFormatter(textFormatter1);
- Button Registerbutton = new Button("Register Customer");
- Button CancelButton = new Button("Cancel");
- GridPane p1 = new GridPane();
- p1.setAlignment(Pos.CENTER);
- p1.setHgap(5);
- p1.setVgap(5);
- p1.add(new Label("Name:"),0, 0);
- p1.add(new Label("Email:"), 0, 1);
- p1.add(tfName, 1, 0);
- p1.add(tfEmail, 1, 1);
- p1.add(Registerbutton, 0, 2);
- p1.add(CancelButton, 1, 2);
- Registerbutton.setOnAction(e ->
- {
- if(ValidateName(tfName.getText())&& ValidateEmail(tfEmail.getText())){
- ((Node)e.getSource()).getScene().getWindow().hide();
- }
- }
- );
- CancelButton.setOnAction(e ->
- {
- tfName.clear();
- tfEmail.clear();
- ((Node)e.getSource()).getScene().getWindow().hide();
- }
- );
- CenterPane.getChildren().add(p1);
- return CenterPane;
- }
- //creating validation for Name so that it must be atleast 3 characters and at must 50
- //if the matched text is valid the method is true
- //if text does not match the format then the method is returned false and an alert is given to user
- private static boolean ValidateName(String name){
- Pattern p2 = Pattern.compile("[a-zA-Z ]{3,30}");
- Matcher m1 = p2.matcher(name);
- if (m1.find() && m1.group().equals(name)){
- return true;
- }
- else
- {
- Alert alert = new Alert(Alert.AlertType.WARNING);
- alert.setTitle("Validate Fields");
- alert.setHeaderText(null);
- alert.setContentText("Please enter a valid Name. Atleast 3 characters and no more than 30.");
- alert.showAndWait();
- return false;
- }
- }
- //creating validation for email so that the email must be in the format email@email.com etc or email@email.co.uk etc
- //if the matched text is valid the method is true
- //if text does not match the format then the method is returned false and an alert is given to user
- private static boolean ValidateEmail(String Number){
- Pattern p2 = Pattern.compile("[a-zA-Z0-9][a-zA-Z0-9._]*@[a-zA-Z0-9]+([.][a-zA-Z]+)+");
- Matcher m1 = p2.matcher(Number);
- if (m1.find() && m1.group().equals(Number)){
- return true;
- }
- else
- {
- Alert alert = new Alert(Alert.AlertType.WARNING);
- alert.setTitle("Validate Fields");
- alert.setHeaderText(null);
- alert.setContentText("Please enter a valid email address.");
- alert.showAndWait();
- return false;
- }
- }
- // creating a textFormatter method getTextFormatter() - and setting that textformatter
- //to add a filer
- private static TextFormatter<String> getTextFormatter() {
- UnaryOperator<TextFormatter.Change> filter = getFilter();
- TextFormatter<String> textFormatter = new TextFormatter<>(filter);
- return textFormatter;
- }
- //Creates a new Formatter with the provided filter. numbers only.
- private static UnaryOperator<TextFormatter.Change> getFilter() {
- return change -> {
- String text = change.getText();
- if (!change.isContentChange()) {
- return change;
- }
- if (text.matches("[a-zA-Z ]*") || text.isEmpty()) {
- return change;
- }
- return null;
- };
- }
- // creating a textFormatter method getTextFormatter() - and setting that textformatter
- //to add a filer
- private static TextFormatter<String> getTextFormatter1() {
- UnaryOperator<TextFormatter.Change> filter = getFilter1();
- TextFormatter<String> textFormatter = new TextFormatter<>(filter);
- return textFormatter;
- }
- //Creates a new Formatter with the provided filter. numbers only.
- private static UnaryOperator<TextFormatter.Change> getFilter1() {
- return change -> {
- String text = change.getText();
- if (!change.isContentChange()) {
- return change;
- }
- if (text.matches("[a-zA-Z0-9._@]*") || text.isEmpty()) {
- return change;
- }
- return null;
- };
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement