Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.03 KB | None | 0 0
  1. package sandwichshopsystem;
  2.  
  3. import java.util.function.UnaryOperator;
  4. import java.util.regex.Matcher;
  5. import java.util.regex.Pattern;
  6. import javafx.application.Application;
  7. import javafx.event.ActionEvent;
  8. import javafx.event.EventHandler;
  9. import javafx.geometry.Insets;
  10. import javafx.geometry.Pos;
  11. import javafx.scene.Node;
  12. import javafx.scene.Scene;
  13. import javafx.scene.control.Alert;
  14. import javafx.scene.control.Button;
  15. import javafx.scene.control.Label;
  16. import javafx.scene.control.TextField;
  17. import javafx.scene.control.TextFormatter;
  18. import javafx.scene.image.Image;
  19. import javafx.scene.image.ImageView;
  20. import javafx.scene.layout.BorderPane;
  21. import javafx.scene.layout.GridPane;
  22. import javafx.scene.layout.StackPane;
  23. import javafx.stage.Modality;
  24. import javafx.stage.Stage;
  25.  
  26. /**
  27.  *
  28.  * @author 115465872
  29.  */
  30. public class RegisterNewLoyaltyCustomer  {
  31.    
  32.      public static void display()
  33.   {
  34.    
  35.         Stage primaryStage = new Stage();
  36.         primaryStage.setTitle("Payment");
  37.        
  38.         BorderPane pane = new BorderPane();
  39.         pane.setStyle("-fx-padding: 10; -fx-background-color: #28ABE3" );
  40.        
  41.         //here we are creating a new image which will be our create new logo
  42.         Image image = new Image("Image/NewCust.png");
  43.         StackPane LogoBackGround = new StackPane(); //create a new stack pane to hold this image and allow us
  44.         //to center it within the border top pane
  45.         LogoBackGround.setPrefSize(75,75);//setting the size we want
  46.                
  47.         ImageView Logo = new ImageView(image); //creating a new image view that will be used
  48.         Logo.setFitHeight(75); //fitting the image to the image view
  49.         Logo.setFitWidth(75);
  50.        
  51.         LogoBackGround.setPadding(new Insets(30,0,0,0));
  52.         LogoBackGround.getChildren().add(Logo); //adding the logo to the stack pane
  53.        
  54.         pane.setTop(LogoBackGround); //adding the stack pane to the top region of the borderpane
  55.         pane.setCenter(CenterLayout());
  56.         pane.setAlignment(CenterLayout(),Pos.CENTER);
  57.        
  58.        
  59.        
  60.        
  61.        
  62.        
  63.         Scene scene = new Scene(pane);
  64.         primaryStage.setResizable(false);
  65.         primaryStage.setScene(scene);
  66.         primaryStage.showAndWait();
  67.   }
  68.      
  69.      private static StackPane CenterLayout(){
  70.          StackPane CenterPane = new StackPane();
  71.          CenterPane.setStyle("-fx-Background-color: White; -fx-border-color:Black;");
  72.          TextField tfName = new TextField();
  73.          
  74.            //declaring a textformatter and setting the textformatter to the textfield. this intercepts the users input before it is written
  75.            TextFormatter<String> textFormatter = getTextFormatter();
  76.            tfName.setTextFormatter(textFormatter);
  77.        
  78.          
  79.         TextField tfEmail =  new TextField();
  80.          
  81.            //declaring a textformatter and setting the textformatter to the textfield. this intercepts the users input before it is written
  82.         TextFormatter<String> textFormatter1 = getTextFormatter1();
  83.            tfEmail.setTextFormatter(textFormatter1);
  84.        
  85.        
  86.         Button Registerbutton = new Button("Register Customer");
  87.         Button CancelButton = new Button("Cancel");
  88.        
  89.  GridPane p1 = new GridPane();
  90.       p1.setAlignment(Pos.CENTER);
  91.       p1.setHgap(5);
  92.       p1.setVgap(5);
  93.       p1.add(new Label("Name:"),0, 0);
  94.       p1.add(new Label("Email:"), 0, 1);
  95.       p1.add(tfName, 1, 0);
  96.       p1.add(tfEmail, 1, 1);
  97.       p1.add(Registerbutton, 0, 2);
  98.       p1.add(CancelButton, 1, 2);
  99.      
  100.      
  101.    
  102.  
  103.        
  104.        
  105.        Registerbutton.setOnAction(e ->
  106.        {
  107.          
  108.          
  109.          if(ValidateName(tfName.getText())&& ValidateEmail(tfEmail.getText())){
  110.            ((Node)e.getSource()).getScene().getWindow().hide();
  111.            }
  112.          
  113.              
  114.            }
  115.            
  116.      
  117.        
  118.        
  119.        );
  120.        CancelButton.setOnAction(e ->
  121.        {
  122.            
  123.            tfName.clear();
  124.            tfEmail.clear();
  125.            
  126.            ((Node)e.getSource()).getScene().getWindow().hide();
  127.            
  128.        }
  129.    
  130.        );
  131.        
  132.       CenterPane.getChildren().add(p1);
  133.          
  134.          return CenterPane;
  135.      }
  136.      
  137.      
  138.       //creating validation for Name so that it must be atleast 3 characters and at must 50
  139.     //if the matched text is valid the method is true
  140.     //if text does not match the format then the method is returned false and an alert is given to user
  141.         private static boolean ValidateName(String name){
  142.         Pattern p2 = Pattern.compile("[a-zA-Z ]{3,30}");
  143.            Matcher m1 = p2.matcher(name);
  144.             if (m1.find() && m1.group().equals(name)){
  145.              return true;
  146.            }
  147.            else
  148.            {
  149.                  Alert alert = new Alert(Alert.AlertType.WARNING);
  150.                alert.setTitle("Validate Fields");
  151.                alert.setHeaderText(null);
  152.                alert.setContentText("Please enter a valid Name. Atleast 3 characters and no more than 30.");
  153.                alert.showAndWait();
  154.              return false;
  155.            }
  156.            
  157.        
  158.        
  159.     }
  160.        
  161.          
  162.       //creating validation for email so that the email must be in the format email@email.com etc or email@email.co.uk etc
  163.     //if the matched text is valid the method is true
  164.     //if text does not match the format then the method is returned false and an alert is given to user
  165.        
  166.          private static boolean ValidateEmail(String Number){
  167.         Pattern p2 = Pattern.compile("[a-zA-Z0-9][a-zA-Z0-9._]*@[a-zA-Z0-9]+([.][a-zA-Z]+)+");
  168.            Matcher m1 = p2.matcher(Number);
  169.             if (m1.find() && m1.group().equals(Number)){
  170.              return true;
  171.            }
  172.            else
  173.            {
  174.                  Alert alert = new Alert(Alert.AlertType.WARNING);
  175.                alert.setTitle("Validate Fields");
  176.                alert.setHeaderText(null);
  177.                alert.setContentText("Please enter a valid email address.");
  178.                alert.showAndWait();
  179.              return false;
  180.            }
  181.            
  182.        
  183.        
  184.     }
  185.      
  186.      // creating a textFormatter method getTextFormatter() - and setting that textformatter
  187.     //to add a filer
  188.          
  189.            private static TextFormatter<String> getTextFormatter() {
  190.         UnaryOperator<TextFormatter.Change> filter = getFilter();
  191.         TextFormatter<String> textFormatter = new TextFormatter<>(filter);
  192.         return textFormatter;
  193.     }
  194.  
  195.      //Creates a new Formatter with the provided filter. numbers only.
  196.     private static UnaryOperator<TextFormatter.Change> getFilter() {
  197.         return change -> {
  198.             String text = change.getText();
  199.              
  200.            
  201.            
  202.             if (!change.isContentChange()) {
  203.                
  204.                 return change;
  205.             }
  206.  
  207.             if (text.matches("[a-zA-Z ]*") || text.isEmpty()) {
  208.                 return change;
  209.             }
  210.            
  211.          
  212.  
  213.             return null;
  214.          };
  215.     }
  216.    
  217.    
  218.       // creating a textFormatter method getTextFormatter() - and setting that textformatter
  219.     //to add a filer
  220.          
  221.            private static TextFormatter<String> getTextFormatter1() {
  222.         UnaryOperator<TextFormatter.Change> filter = getFilter1();
  223.         TextFormatter<String> textFormatter = new TextFormatter<>(filter);
  224.         return textFormatter;
  225.     }
  226.      
  227.  
  228. //Creates a new Formatter with the provided filter. numbers only.
  229.     private static UnaryOperator<TextFormatter.Change> getFilter1() {
  230.         return change -> {
  231.             String text = change.getText();
  232.              
  233.            
  234.            
  235.             if (!change.isContentChange()) {
  236.                
  237.                 return change;
  238.             }
  239.  
  240.             if (text.matches("[a-zA-Z0-9._@]*") || text.isEmpty()) {
  241.                 return change;
  242.             }
  243.            
  244.          
  245.  
  246.             return null;
  247.          };
  248.     }
  249.        
  250.        
  251. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement