iuliaa

Untitled

Jun 18th, 2020
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.84 KB | None | 0 0
  1. package org.bsa.controllers;
  2.  
  3. import javafx.collections.FXCollections;
  4. import javafx.collections.ObservableList;
  5. import javafx.event.ActionEvent;
  6. import javafx.event.EventHandler;
  7. import javafx.fxml.FXML;
  8. import javafx.fxml.FXMLLoader;
  9. import javafx.geometry.Pos;
  10. import javafx.scene.CacheHint;
  11. import javafx.scene.Parent;
  12. import javafx.scene.Scene;
  13. import javafx.scene.control.*;
  14. import javafx.scene.control.cell.PropertyValueFactory;
  15. import javafx.scene.layout.VBox;
  16. import javafx.stage.Modality;
  17. import javafx.stage.Stage;
  18. import javafx.util.StringConverter;
  19. import javafx.util.converter.DateTimeStringConverter;
  20. import org.bsa.exceptions.EqualHour;
  21. import org.bsa.exceptions.InvalidHour;
  22. import org.bsa.model.Appointment;
  23. import org.bsa.model.Service;
  24. import org.bsa.service.AppointmentService;
  25. import org.bsa.service.EmployeeService;
  26. import org.jetbrains.annotations.Nls;
  27. import org.omg.CORBA.DATA_CONVERSION;
  28. import sun.security.krb5.internal.APOptions;
  29.  
  30. import javax.jws.HandlerChain;
  31. import javax.swing.*;
  32. import javax.swing.text.TabableView;
  33. import java.io.IOException;
  34. import java.io.Serializable;
  35. import java.text.DateFormat;
  36. import java.text.SimpleDateFormat;
  37. import java.time.LocalDate;
  38. import java.time.format.DateTimeFormatter;
  39. import java.util.ArrayList;
  40. import java.util.Calendar;
  41. import java.util.Date;
  42.  
  43.  
  44. public class CustomerCartController {
  45.     @FXML
  46.     TableView<Service> tableCart;
  47.     @FXML
  48.     TableColumn serviceColumn;
  49.     @FXML
  50.     TableColumn priceColumn;
  51.     @FXML
  52.     Button backButton;
  53.     @FXML
  54.     ChoiceBox hour = new ChoiceBox();
  55.     @FXML
  56.     Label display;
  57.     @FXML
  58.     DatePicker datePicker = new DatePicker();
  59.     @FXML
  60.     DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy:MM:dd");
  61.     // Date date = c.getTime();
  62.  
  63.     /*  @FXML
  64.              SimpleDateFormat format = new SimpleDateFormat(":mm:ss");
  65.              dateField.setTextFormatter(new TextFormatter<>(new DateTimeStringConverter(format), format.parse("00:00:00")));*/
  66.     @FXML
  67.     public void initializeChoiceBox() {
  68.  
  69.         /*(year.getItems().addAll("2020","2021");
  70.  
  71.         day.getItems().addAll(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,26,27,28,29,30,31);
  72.         day.setValue("1");
  73.         year.setValue("2020");
  74.         month.getItems().addAll("January","February","March","April","June","July","August","September","October","November","December");
  75.         month.setValue("January");*/
  76.         hour.getItems().addAll("8:00", "10:00", "11:00", "12:00", "13:00", "14:00", "15:00", "16:00", "17:00", "18:00");
  77.         hour.setValue("8:00");
  78.     }
  79.  
  80.  
  81.     /*  @FXML
  82.       SimpleDateFormat format = new SimpleDateFormat(":mm:ss");
  83.       dateField.setTextFormatter(new TextFormatter<>(new DateTimeStringConverter(format), format.parse("00:00:00")));*/
  84.     static ObservableList<Service> selectedservices = FXCollections.observableArrayList();
  85.  
  86.     /*public  void handleDate{
  87.         datePicker.setConverter(new StringConverter<LocalDate>() {
  88.             String pattern = "yyyy:MM:dd";
  89.             DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern(pattern);
  90.  
  91.             {
  92.                 datePicker.setPromptText(pattern.toLowerCase());
  93.             }
  94.  
  95.             @Override public String toString(LocalDate date) {
  96.                 if (date != null) {
  97.                     return dateFormatter.format(date);
  98.                 } else {
  99.                     return "";
  100.                 }
  101.             }
  102.  
  103.             @Override public LocalDate fromString(String string) {
  104.                 if (string != null && !string.isEmpty()) {
  105.                     return LocalDate.parse(string, dateFormatter);
  106.                 } else {
  107.                     return null;
  108.                 }
  109.             }
  110.         });
  111.  
  112.     }*/
  113.     public void initialize() throws IOException {
  114.        /* format = new SimpleDateFormat("yyyy:MM:dd HH:mm");
  115.         datePicker.setOnAction(new EventHandler<ActionEvent>() {
  116.             @Override
  117.             public void handle(ActionEvent event) {
  118.                 LocalDate date=datePicker.getValue();
  119.                 System.out.print(date);
  120.                 display.setText(date.toString());
  121.             }
  122.         });*/
  123.         AppointmentService.loadAppointments();
  124.         initializeChoiceBox();
  125.         CustomerServicesListController s = new CustomerServicesListController();
  126.         ObservableList<Service> services = FXCollections.observableArrayList();
  127.         services = s.getSelected();
  128.         tableCart.setItems(services);
  129.         // System.out.print(services+"\n ");
  130.         initCols();
  131.  
  132.     }
  133.  
  134.     public void initCols() {
  135.         serviceColumn.setCellValueFactory(new PropertyValueFactory<>("type"));
  136.         priceColumn.setCellValueFactory(new PropertyValueFactory<>("price"));
  137.  
  138.     }
  139.  
  140.     public void setSelectedservices(ObservableList<Service> s) {
  141.         selectedservices = s;
  142.     }
  143.  
  144.     public void handleBackButton() {
  145.         //System.out.println(selectedservices);
  146.         try {
  147.             Stage stage = (Stage) backButton.getScene().getWindow();
  148.             Parent viewCustomerPageRoot = FXMLLoader.load(getClass().getResource("/CustomerPage.fxml"));
  149.             Scene customerScene = new Scene(viewCustomerPageRoot, 600, 380);
  150.             stage.setScene(customerScene);
  151.         } catch (IOException e) {
  152.             e.printStackTrace();
  153.         }
  154.     }
  155.  
  156.     public String handleDate() {
  157.         // String dateString;
  158.         datePicker.setConverter(new StringConverter<LocalDate>() {
  159.             String pattern = "yyyy:MM:dd";
  160.             DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern(pattern);
  161.  
  162.             {
  163.                 datePicker.setPromptText(pattern.toLowerCase());
  164.             }
  165.  
  166.             @Override
  167.             public String toString(LocalDate date) {
  168.                 if (date != null) {
  169.                     return dateFormatter.format(date);
  170.                 } else {
  171.                     return "";
  172.                 }
  173.             }
  174.  
  175.             @Override
  176.             public LocalDate fromString(String string) {
  177.                 if (string != null && !string.isEmpty()) {
  178.                     return LocalDate.parse(string, dateFormatter);
  179.                 } else {
  180.                     return null;
  181.                 }
  182.             }
  183.         });
  184.         return datePicker.toString();
  185.     }
  186.  
  187.     public void  handleFinishButton() throws IOException, EqualHour {
  188.         CustomerServicesListController sc = new CustomerServicesListController();
  189.         ObservableList<Service> selectedservice = FXCollections.observableArrayList();
  190.         selectedservice = sc.getSelected();
  191.         boolean check=false;
  192.         String choiceBoxHour = (String) hour.getValue();
  193.         //System.out.print(" "+choiceBoxHour);
  194.        ArrayList<Appointment> appointms =new ArrayList<>();
  195.         //ObservableList<Appointment> appointments=FXCollections.observableArrayList();
  196.         ObservableList<Appointment> appointments = FXCollections.observableArrayList();
  197.         appointments = AppointmentService.returnAppointments();
  198.         Appointment ap1;
  199.         ArrayList<Service> s1 = new ArrayList<>();
  200.         ArrayList<ArrayList<Service>> s2 = new ArrayList<>();
  201.         ObservableList<String> all_employees=FXCollections.observableArrayList();
  202.         all_employees= EmployeeService.returnEmpUser();
  203.         for(String username:all_employees){
  204.             s1=new ArrayList<>();
  205.             for (Service s: selectedservice){
  206.                 if(s.getEmpl().equals(username)){
  207.                     s1.add(s);
  208.                 }
  209.             }
  210.             if(!s1.isEmpty())
  211.                 s2.add(s1);
  212.         }
  213.         //System.out.println(s2);
  214.         AppointmentService as=new AppointmentService();
  215.         String client_username=as.getClientusr();
  216.         try {
  217.         if(!datePicker.getValue().equals(null)) {
  218.             for(ArrayList<Service> aux:s2){
  219.                 ap1 = new Appointment(true, datePicker.getValue() + " " + choiceBoxHour, aux.get(0).getEmpl(), client_username,aux);
  220.                 for (Appointment ap : appointments)
  221.                         if ((ap1.getEmpl().equals(ap.getEmpl()) && ap1.getDate().equals(ap.getDate())))
  222.                             check=true;
  223.                         if(check==true)
  224.                             throw new EqualHour();
  225.                         else
  226.                 appointms.add(ap1);
  227.             }
  228.  
  229.         }
  230.  
  231.      }catch (EqualHour ee){
  232.             Stage alert = new Stage();
  233.             alert.initModality(Modality.APPLICATION_MODAL);
  234.             VBox alertscene = new VBox(20);
  235.             alertscene.setMinSize(200, 100);
  236.             Label aLabel = new Label();
  237.             aLabel.setText("The chosen date is unavailable, please choose another one!");
  238.             Button closeB = new Button("Close");
  239.             closeB.setOnAction(e -> alert.close());
  240.             alertscene.getChildren().addAll(aLabel, closeB);
  241.             alertscene.setAlignment(Pos.CENTER);
  242.             Scene scene = new Scene(alertscene);
  243.             alert.setScene(scene);
  244.             alert.show();
  245.  
  246.         } catch (NullPointerException e1) {
  247.             Stage alert = new Stage();
  248.             alert.initModality(Modality.APPLICATION_MODAL);
  249.             VBox alertscene = new VBox(20);
  250.             alertscene.setMinSize(200, 100);
  251.             Label aLabel = new Label();
  252.             aLabel.setText("Please pick a date!");
  253.             Button closeB = new Button("Close");
  254.             closeB.setOnAction(e -> alert.close());
  255.             alertscene.getChildren().addAll(aLabel, closeB);
  256.             alertscene.setAlignment(Pos.CENTER);
  257.             Scene scene = new Scene(alertscene);
  258.             alert.setScene(scene);
  259.             alert.show();
  260.         }
  261.         AppointmentService.addAppointment(appointms);
  262.         System.out.println(appointms);
  263.         //return  appointments;
  264.     }
  265. }
Add Comment
Please, Sign In to add comment