Advertisement
moreiramota

Untitled

Dec 16th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package lapr.project.controller;
  7.  
  8. import java.sql.Timestamp;
  9. import lapr.project.model.Bicycle;
  10. import lapr.project.model.Journey;
  11. import lapr.project.model.Park;
  12. import lapr.project.model.ParkingSlot;
  13.  
  14. /**
  15. *
  16. * @author morei
  17. */
  18. public class ReturnRentedBicycleController {
  19.  
  20. private ReturnRentedBicycleController() {
  21.  
  22. }
  23.  
  24. public static boolean returnRentendBicycle(String email, Timestamp inicialDate, Timestamp finalDate, Park destinationPark) {
  25.  
  26. Journey journey = getJorney(email, inicialDate);
  27. Bicycle bicycle = getBicycleById(journey);
  28.  
  29. ParkingSlot parkingSlot = getParkingSlot(destinationPark, bicycle);
  30. if (parkingSlot != null) {
  31. updateParkingSlot(parkingSlot, bicycle.getId_bicycle());
  32. parkingSlot.saveParkingSlot();
  33. }
  34. updateJourney(journey, finalDate, destinationPark);
  35. Journey.updateJourney(journey);
  36.  
  37. Park originPark = journey.getOriginPark();
  38. updatePark(destinationPark, originPark, bicycle);
  39. originPark.save();
  40. destinationPark.save();
  41.  
  42. return true;
  43. }
  44.  
  45. public static boolean updateParkingSlot(ParkingSlot parkingSlot, int idBicycle) {
  46. parkingSlot.setIdBicycle(idBicycle);
  47. return true;
  48. }
  49.  
  50. public static boolean updateJourney(Journey journey, Timestamp finalDate, Park destinationPark) {
  51. journey.setFinalDate(finalDate);
  52. journey.setDestinationPark(destinationPark);
  53. return true;
  54. }
  55.  
  56. public static boolean updatePark(Park destinationPark, Park originPark, Bicycle bicycle) {
  57. if (bicycle.getType() == 0) {
  58. destinationPark.setElectricOccupation(destinationPark.getElectricOccupation() + 1);
  59. originPark.setElectricOccupation(originPark.getElectricOccupation() - 1);
  60. } else {
  61. destinationPark.setNonElectricOccupation(destinationPark.getNonElectricOccupation() + 1);
  62. originPark.setNonElectricOccupation(originPark.getNonElectricOccupation() - 1);
  63. }
  64. return true;
  65. }
  66.  
  67. public static Bicycle getBicycleById(Journey journey) {
  68. return Bicycle.getBicycle(journey.getIdBicycle());
  69. }
  70.  
  71. public static Journey getJorney(String email, Timestamp inicialDate) {
  72. return Journey.getJourney(email, inicialDate);
  73. }
  74.  
  75. public static ParkingSlot getParkingSlot(Park p, Bicycle b) {
  76. ParkingSlot pa = null;
  77. for (ParkingSlot ps : p.getSlots()) {
  78. if (ps.getIdType() == b.getType() && ps.getIdBicycle() == 0) {
  79. pa = ps;
  80. }
  81. }
  82. return pa;
  83. }
  84.  
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement