Skip to content

Commit

Permalink
VOL RESERVATION CONFIRM
Browse files Browse the repository at this point in the history
  • Loading branch information
e.elhjoujy committed Oct 22, 2023
1 parent 9b2f68f commit 4ce971e
Show file tree
Hide file tree
Showing 15 changed files with 249 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static void main(String[] args) {
conditions.put("date_2", "2023-12-31");

ArrayList<ReservationEntity> reservationEntities =
reservationDao.AfficheLesStatistiques(conditions, ReservationStatus.PENDING);
reservationDao.AfficheLesStatistiques(conditions, ReservationStatus.EN_ATTENTE);
Print.log(reservationEntities.size());


Expand Down
12 changes: 12 additions & 0 deletions src/main/java/ma/yc/airafraik/core/Util.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ma.yc.airafraik.core;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.Scanner;
Expand All @@ -21,6 +22,17 @@ public static Long generatedLong() {
return leftLimit + (long) (Math.random() * (rightLimit - leftLimit));
}

public static String generateFlightTicketCode(String airlineCode, Long flightNumber) {
// Get the current timestamp
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
String timestamp = dateFormat.format(new Date());

// Generate a unique code using airline code, flight number, and timestamp
String ticketCode = airlineCode + flightNumber + timestamp;

return ticketCode;
}


public static Date readDate(String key , Scanner scanner){
Print.log(key+" : ");
Expand Down
14 changes: 13 additions & 1 deletion src/main/java/ma/yc/airafraik/dao/Impl/ReservationDaoImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import ma.yc.airafraik.connection.HyperJpa;
import ma.yc.airafraik.dao.ReservationDao;
import ma.yc.airafraik.entities.ReservationEntity;
import ma.yc.airafraik.entities.VolEntity;
import ma.yc.airafraik.enums.ReservationStatus;

import java.util.ArrayList;
Expand All @@ -18,6 +19,17 @@ public class ReservationDaoImpl implements ReservationDao{
EntityTransaction transaction = entityManager.getTransaction();
@Override
public void ajouterReservation(ReservationEntity reservationEntity) {
try {
transaction.begin();
entityManager.persist(reservationEntity.getClient());
for (VolEntity vol:reservationEntity.getVolEntities()){
entityManager.persist(vol);
}
entityManager.persist(reservationEntity);
transaction.commit();
}catch (Exception e){
transaction.rollback();
}

}

Expand Down Expand Up @@ -75,7 +87,7 @@ public ArrayList<ReservationEntity> AfficheLesStatistiques(HashMap<String, Strin
Query query = entityManager.createNativeQuery("SELECT * FROM reservation WHERE date_reservation BETWEEN :date_1 AND :date_2 AND status = :etat_reservation", ReservationEntity.class);
query.setParameter("date_1", conditions.get("date_1"));
query.setParameter("date_2", conditions.get("date_2"));
query.setParameter("etat_reservation", ReservationStatus.CANCELLED.toString());
query.setParameter("etat_reservation", ReservationStatus.ANNULER.toString());
ArrayList<ReservationEntity> reservationEntities = (ArrayList<ReservationEntity>) query.getResultList();
return reservationEntities;
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/ma/yc/airafraik/entities/ClientEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class ClientEntity {
private String adresse;
@Email
private String email ;
@Pattern(regexp="(^$|[0-9]{10})")
// @Pattern(regexp="(^$|[0-9]{10})")
private String telephone;
private String password ;
private String profile_image;
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/ma/yc/airafraik/entities/ReservationEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import lombok.*;
import lombok.extern.java.Log;
import ma.yc.airafraik.enums.ReservationStatus;
import ma.yc.airafraik.enums.ReserveType;

import java.sql.Timestamp;
import java.util.ArrayList;
Expand All @@ -19,6 +20,7 @@
@ToString
public class ReservationEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int Id ;
private String code ;
@Column(name = "price_total")
Expand Down Expand Up @@ -60,6 +62,11 @@ public class ReservationEntity {
@Column(name = "status")
private ReservationStatus status ;

@Enumerated(EnumType.STRING)
@Column(name = "flight_type")
private ReserveType flightType;


@Column(name = "cancelled")
private boolean cancelled;

Expand All @@ -74,4 +81,5 @@ public class ReservationEntity {




}
4 changes: 2 additions & 2 deletions src/main/java/ma/yc/airafraik/enums/ReservationStatus.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

public enum ReservationStatus {
CONFIRMED,
CANCELLED,
EN_ATTENTE,
PENDING
ANNULER ,
EXPIRE

}
7 changes: 7 additions & 0 deletions src/main/java/ma/yc/airafraik/enums/ReserveType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package ma.yc.airafraik.enums;

public enum ReserveType {
ALLER, // Represents a one-way flight
RETOUR, // Represents a return (round-trip) flight
ALLER_RETOUR // Represents a round-trip flight
}
6 changes: 4 additions & 2 deletions src/main/java/ma/yc/airafraik/service/ReservationService.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package ma.yc.airafraik.service;

import ma.yc.airafraik.entities.ReservationEntity;

public interface ReservationService {
public double confirmationReservation(Object object);
public double annulationReservation(Object object);
public double confirmationReservation(ReservationEntity reservationEntity);
public double annulationReservation(ReservationEntity reservationEntity);
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,62 @@
package ma.yc.airafraik.service.impl;

import ma.yc.airafraik.core.Util;
import ma.yc.airafraik.dao.Impl.ReservationDaoImpl;
import ma.yc.airafraik.dao.ReservationDao;
import ma.yc.airafraik.entities.ReservationEntity;
import ma.yc.airafraik.enums.ReserveType;
import ma.yc.airafraik.service.PaiementService;
import ma.yc.airafraik.service.ReservationService;

public class ReservationServiceImpl implements ReservationService {

private PaiementService paiementService ;
private ReservationDao reservationDao;

public ReservationServiceImpl() {
this.reservationDao = new ReservationDaoImpl();
}

public ReservationServiceImpl(PaiementService paiementService) {
this.paiementService = paiementService;
}
@Override
public double confirmationReservation(Object object) {
//TODO : une réduction de 19% s'applique pour les clients qui réservant aller/retour
return this.confirmationReservation("");
public double confirmationReservation(ReservationEntity reservationEntity) {
if (reservationEntity == null) {
// Handle the case where the reservation is null
return 0;
}

reservationEntity.setCode(Util.generateFlightTicketCode(Util.generateRandomCode(),
Util.generatedLong()
)
);


reservationEntity.setFlightType(ReserveType.ALLER_RETOUR);


double totalCost = reservationEntity.getPrixTotal();

if (reservationEntity.getFlightType() == ReserveType.ALLER) {
// This reservation is for an "aller" (one-way) flight
} else if (reservationEntity.getFlightType() == ReserveType.RETOUR) {
// This reservation is for a "retour" (round-trip) flight
} else if (reservationEntity.getFlightType() == ReserveType.ALLER_RETOUR) {
// This reservation is for an "aller_retour" (round-trip) flight
double discount = 0.10 * totalCost;
totalCost -= discount;
reservationEntity.setPrixTotal(totalCost);
} else {
// Handle the case where the flight type is none of the specified values
}

this.reservationDao.ajouterReservation(reservationEntity);
return totalCost;
}

@Override
public double annulationReservation(Object object) {
public double annulationReservation(ReservationEntity reservationEntity) {
//TODO : annuler sa réservation après la confirmation de son vol dans le cas d'annulation:
// 8% des frais est retenue pour la société AirAfrika > 24h avant le vol

Expand Down
29 changes: 21 additions & 8 deletions src/main/java/ma/yc/airafraik/web/RechercheVolController.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ma.yc.airafraik.web;

import jakarta.servlet.ServletConfig;
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
Expand All @@ -17,8 +18,14 @@
@WebServlet(name = "RechercheVolController", value = "/recherche")
public class RechercheVolController extends HttpServlet {

private SearchVolsService searchVolsService = new SearchVolsServiceImpl();
private SearchVolsService searchVolsService ;
private ServletContext context;

@Override
public void init(ServletConfig config) throws ServletException {
this.searchVolsService = new SearchVolsServiceImpl();
this.context = config.getServletContext();
}

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Expand All @@ -30,6 +37,19 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws Se
String retourdate = req.getParameter("return-date");
//TODO : if those information are null then we will redirect to the home page

Integer numberDeAdultes = Integer.parseInt(req.getParameter("numberDeAdultes"));
Integer numberDeEnfants = Integer.parseInt(req.getParameter("numberDeEnfants"));
Integer numberDeBebes = Integer.parseInt(req.getParameter("numberDeBebes"));

this.context.setAttribute("numberDeAdultes",numberDeAdultes);
this.context.setAttribute("numberDeEnfants",numberDeEnfants);
this.context.setAttribute("numberDeBebes",numberDeBebes);








if (depart == null || origin == null || departuredate == null ) {
Expand All @@ -42,9 +62,6 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws Se
}





}

@Override
Expand All @@ -62,8 +79,4 @@ public void destroy() {
super.destroy();
}

@Override
public void init(ServletConfig config) throws ServletException {
super.init(config);
}
}
Original file line number Diff line number Diff line change
@@ -1,47 +1,114 @@
package ma.yc.airafraik.web;

import jakarta.servlet.ServletConfig;
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import ma.yc.airafraik.core.Print;
import ma.yc.airafraik.entities.ClientEntity;
import ma.yc.airafraik.entities.ReservationEntity;
import ma.yc.airafraik.entities.VolEntity;
import ma.yc.airafraik.enums.ReservationStatus;
import ma.yc.airafraik.service.ReservationService;
import ma.yc.airafraik.service.SearchVolsService;
import ma.yc.airafraik.service.impl.ReservationServiceImpl;
import ma.yc.airafraik.service.impl.SearchVolsServiceImpl;
import org.hibernate.Session;

import java.io.IOException;
import java.sql.Timestamp;
import java.util.Collection;

@WebServlet(name = "RservationConfirmationController", value = "/reservation-confirmation")
public class RservationConfirmationController extends HttpServlet {

private SearchVolsService searchVolsService ;
private ReservationService reservationService;
private HttpSession session;
private ServletContext context;
@Override
public void init(ServletConfig config) throws ServletException {
super.init(config);
this.searchVolsService = new SearchVolsServiceImpl();
this.context = config.getServletContext();
this.reservationService = new ReservationServiceImpl();
}

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
HttpSession session = req.getSession(); // Get the session associated with the request

String id = req.getParameter("id");
Double prixTotal = Double.parseDouble(req.getParameter("prixTotal"));

if (id != null) {
Print.log("id : " + id);
VolEntity vols = searchVolsService.searchVolParId(id);
req.setAttribute("vols",vols);
// reservationEntity.setVolEntities((Collection<VolEntity>) req.getSession().getAttribute("vols"));
// reservationEntity.setPrixTotal((Double) req.getSession().getAttribute("prixTotal"));
// reservationEntity.setDate_Reservation(new Timestamp(System.currentTimeMillis()));
// reservationEntity.setNumberDeAdulets((Integer) req.getSession().getAttribute("numberDeAdulets"));
// reservationEntity.setNumberDeEnfants((Integer) req.getSession().getAttribute("numberDeEnfants"));
// reservationEntity.setNumberDeBebes((Integer) req.getSession().getAttribute("numberDeBebe"));

session.setAttribute("vols", vols);
prixTotal = vols.getPrix() * (Integer) session.getAttribute("numberDeAdultes") + vols.getPrix() * (Integer) session.getAttribute("numberDeEnfants") + vols.getPrix() * (Integer) session.getAttribute("numberDeBebes");
session.setAttribute("prixTotal", prixTotal);


req.getRequestDispatcher("reservation-confirmation.jsp").forward(req, resp);

}else{
resp.sendRedirect("/");
}
super.doGet(req, resp);
}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
// Get the values from the form


String nom = req.getParameter("nom");
String prenom = req.getParameter("prenom");
String adresse = req.getParameter("adresse");
String email = req.getParameter("email");
String telephone = req.getParameter("telephone");

//TODO : CALL THE SERVICE TO SAVE THE RESERVATION

ClientEntity client = new ClientEntity();
client.setNom(nom);
client.setPrenom(prenom);
client.setAdresse(adresse);
client.setEmail(email);
client.setTelephone(telephone);

ReservationEntity reservationEntity = new ReservationEntity();
reservationEntity.setClient(client);
VolEntity vol = (VolEntity) req.getSession().getAttribute("vol");
reservationEntity.setStatus(ReservationStatus.EN_ATTENTE);
reservationEntity.getVolEntities().add(vol);
double prixTotal = vol.getPrix() * (Integer) context.getAttribute("numberDeAdultes")
+ vol.getPrix() * (Integer) context.getAttribute("numberDeEnfants")
+ vol.getPrix() * (Integer) context.getAttribute("numberDeBebes");
reservationEntity.setPrixTotal(prixTotal);
reservationEntity.setDate_Reservation(new Timestamp(System.currentTimeMillis()));
reservationEntity.setNumberDeAdulets(((Integer) context.getAttribute("numberDeAdultes")));
reservationEntity.setNumberDeEnfants((Integer) context.getAttribute("numberDeEnfants"));
reservationEntity.setNumberDeBebes((Integer) context.getAttribute("numberDeBebes"));



this.reservationService.confirmationReservation(reservationEntity);


resp.sendRedirect("/thank-you.jsp");

}

}

Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws Se
VolEntity vol = searchVolsService.searchVolParId(id);
req.setAttribute("vol",vol);
//TODO : we will use this attribute in the jsp page
req.getSession().setAttribute("vol",vol);

// try{
// if (reservationEntity != null){
Expand Down
Loading

0 comments on commit 4ce971e

Please sign in to comment.