Advertisement
iuliaa

Untitled

Jun 18th, 2020
336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.54 KB | None | 0 0
  1. package org.bsa.service;
  2.  
  3. import com.fasterxml.jackson.core.type.TypeReference;
  4. import com.fasterxml.jackson.databind.ObjectMapper;
  5. import javafx.collections.FXCollections;
  6. import javafx.collections.ObservableList;
  7. import javafx.geometry.Pos;
  8. import javafx.scene.Scene;
  9. import javafx.scene.control.Button;
  10. import javafx.scene.control.Label;
  11. import javafx.scene.layout.VBox;
  12. import javafx.stage.Modality;
  13. import javafx.stage.Stage;
  14. import org.apache.commons.io.FileUtils;
  15. import org.bsa.controllers.CustomerCartController;
  16. import org.bsa.exceptions.EqualHour;
  17. import org.bsa.model.Appointment;
  18. import org.bsa.model.Employee;
  19. import org.bsa.model.Service;
  20.  
  21. import java.io.File;
  22. import java.io.IOException;
  23. import java.nio.file.Files;
  24. import java.nio.file.Path;
  25. import java.util.ArrayList;
  26. import java.util.List;
  27.  
  28. public class AppointmentService {
  29.     private static List<Appointment> appointments;
  30.     static String usr;
  31.     static String clientusr;
  32.     private static final Path A_PATH=FileSystemService.getPathToFile("config","appointments.json");
  33.     public static void writeAppointment() {
  34.         ArrayList<Appointment> appointments=new ArrayList<>();
  35.         ArrayList<Service> s1=new ArrayList<>();
  36.         ArrayList<Service> s2=new ArrayList<>();
  37.         ArrayList<Service> s3=new ArrayList<>();
  38.         s1.add(new Service("Bridal Makeup",75,"Iulia"));
  39.         s1.add(new Service("Simple Makeup",20,"Iulia"));
  40.         s2.add(new Service("Short Hairstyle",40,"Bia"));
  41.         s2.add(new Service("Long Hairstyle",55,"Bia"));
  42.         s3.add(new Service("Long Hairstyle",55,"Bia"));
  43.         appointments.add(new Appointment(true,"2020:06:12 12:00","Iulia","A",s1));
  44.         appointments.add(new Appointment(false,"2020:06:12 14:00","Iulia","A",s1));
  45.         appointments.add(new Appointment(false,"2020:06:12 16:00","Bia","B",s3));
  46.         appointments.add(new Appointment(true,"2020:06:12 15:00","Bia","B",s3));
  47.         appointments.add(new Appointment(false,"2020:06:12 8:00","Bia","A",s2));
  48.         appointments.add(new Appointment(false,"2020:06:12 10:00","Bia","A",s3));
  49.         appointments.add(new Appointment(true,"2020:06:12 15:00","Iulia","B",s1));
  50.         appointments.add(new Appointment(false,"2020:06:12 17:00","Iulia","B",s1));
  51.  
  52.         Path A_PATH = FileSystemService.getPathToFile("config", "appointments.json");
  53.         try{
  54.             ObjectMapper objectMapper = new ObjectMapper();
  55.             objectMapper.writeValue(new File("src\\main\\resources\\appointments.json"),appointments);
  56.         }catch (IOException e){e.printStackTrace();}
  57.     }
  58.  
  59.     public static void loadAppointments() throws IOException{
  60.         if(!Files.exists(A_PATH)){
  61.             FileUtils.copyURLToFile(AppointmentService.class.getClassLoader().getResource("appointments.json"), A_PATH.toFile());
  62.         }
  63.         ObjectMapper objectMapper = new ObjectMapper();
  64.         appointments=objectMapper.readValue(new File("src\\main\\resources\\appointments.json"), new TypeReference<List<Appointment>>() {});
  65.     }
  66.  
  67.  
  68.     public static ObservableList<Appointment> returnAppointments(){
  69.         ObservableList<Appointment> aux=FXCollections.observableArrayList();
  70.         for(Appointment a:appointments)
  71.             aux.add(a);
  72.         return aux;
  73.     }
  74.     public static ObservableList<Appointment> returnCertainAppointment(){
  75.         ObservableList<Appointment> aux=FXCollections.observableArrayList();
  76.         for(Appointment a:appointments)
  77.         {
  78.             if(a.getEmpl().equals(usr) && a.getStatus().equals(true)) {
  79.                 aux.add(a);
  80.             }
  81.         }
  82.         return aux;
  83.     }
  84.     public static ObservableList<Appointment> returnCancelledAppointment(){
  85.         ObservableList<Appointment> aux=FXCollections.observableArrayList();
  86.         for(Appointment a:appointments)
  87.         {
  88.             if(a.getEmpl().equals(usr) && a.getStatus().equals(false)) {
  89.                 aux.add(a);
  90.             }
  91.         }
  92.         return aux;
  93.     }
  94.  
  95.     public static String getUsr() {
  96.         return usr;
  97.     }
  98.  
  99.     public static void setUsr(String a) {
  100.     usr = a;
  101.     }
  102.     public static String getClientusr() {
  103.         return clientusr;
  104.     }
  105.  
  106.     public static void setClientusr(String a) {
  107.         clientusr = a;
  108.     }
  109.     public static void removeCancelled(Appointment a)  {
  110.       // ObservableList<Appointment> appointments;
  111.         appointments=returnAppointments();
  112.         appointments.removeIf(app->app.equals(a));
  113.         try{
  114.             ObjectMapper objectMapper = new ObjectMapper();
  115.             File file = new File("src\\main\\resources\\appointments.json");
  116.             objectMapper.writeValue(file,appointments);
  117.         }catch(IOException e){
  118.             e.printStackTrace();
  119.         }
  120.     }
  121.  
  122.     public static void addAppointment(ArrayList<Appointment> appointms) throws EqualHour, IOException,NullPointerException {
  123.         appointments=returnAppointments();
  124.       //CustomerCartController c=git stanew CustomerCartController();
  125.      //ObservableList<Appointment> ap=FXCollections.observableArrayList();
  126.      //ap=c.handleFinishButton();
  127.       appointments.addAll(appointms);
  128.         try{
  129.             ObjectMapper objectMapper = new ObjectMapper();
  130.             File file = new File("src\\main\\resources\\appointments.json");
  131.             objectMapper.writeValue(file,appointments);
  132.         }catch(IOException e){
  133.             e.printStackTrace();
  134.         }
  135.  
  136.     }
  137.     /*public static void removeAppsfromCancelled( String user, Appointment a) //String usr,
  138.     {
  139.         ObservableList<Appointment> apps=FXCollections.observableArrayList();
  140.         apps=returnAppointments();
  141.        // System.out.println("cancelled"+apps.toString());
  142.         ObservableList<Appointment> toRemove=FXCollections.observableArrayList();
  143.             for (Appointment appointment : apps) {
  144.                 if (appointment.equals(a) && appointment.getEmpl().equals(usr))// && a.getEmpl().equals(usr)  appointment.getDate().equals(a.getDate())
  145.                 {
  146.                    toRemove.add(appointment);
  147.                    // System.out.println("\ncancelled"+a.toString());
  148.                     //AppointmentService.removeCancelled(appointment);
  149.  
  150.                 }
  151.                 apps.remove(toRemove);
  152.             }
  153.             //apps.remove(toRemove);
  154.         try{
  155.             ObjectMapper objectMapper = new ObjectMapper();
  156.             File file = new File("src/main/resources/appointments.json");
  157.             objectMapper.writeValue(file,apps);
  158.         }catch (IOException exception){exception.printStackTrace();}
  159.     }*/
  160.  
  161.  
  162.  
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement