Commit ab7828b7 by Antonio Rueda

Implementación de reservas y tests asociados.

parent 073061c5
package es.ujaen.dae.reservahoteles.excepciones;
/**
*
* @author ajrueda
*/
public class ReservaNoValida extends RuntimeException {
}
......@@ -30,16 +30,24 @@ public class ServicioReservas {
Map<Integer, Hotel> hoteles;
Map<String, Cliente> clientes;
private static int nReserva = 1;
private static final Cliente direccion = new Cliente("direccion", "-", "670343332", "direccion@hotelxyz.es", "SeCrEtO");
public ServicioReservas() {
hoteles = new TreeMap<>();
clientes = new TreeMap<>();
}
public void nuevoHotel(@Valid Hotel hotel) {
public void nuevoHotel(Cliente direccion, @Valid Hotel hotel) {
hoteles.put(hotel.id(), hotel);
}
public void nuevoCliente(@Valid Cliente cliente) {
// Evitar que se cree un usuario con la cuenta de direccion
if (cliente.email().equals(direccion.email()))
throw new ClienteYaRegistrado();
if (clientes.containsKey(cliente.email()))
throw new ClienteYaRegistrado();
......@@ -51,6 +59,10 @@ public class ServicioReservas {
// return Optional.ofNullable(clientes.get(email))
// .filter(cliente -> cliente.clave().equals(clave));
// Caso especial de login de la direccion
if (direccion.email().equals(email) && direccion.clave().equals(clave))
return Optional.of(direccion);
Cliente cliente = clientes.get(email);
return (cliente != null && cliente.clave().equals(clave)) ? Optional.of(cliente): Optional.empty();
}
......@@ -94,25 +106,38 @@ public class ServicioReservas {
/**
* Comprueba si hay disponibilidad en las fechas indicadas
* @param hotel hotel donde se compreuba la disponibilidad
* @param fechaInicio fecha de inicio de la reserva
* @param fechaFin fecha de final de la reserva
* @param numHabSimple número de habitaciones simples solicitadas
* @param numHabDoble número de habitaciones dobles solicitadas
* @return true si hay disponibilidad, false en caso contrario
*/
public boolean disponible(Hotel h,
public boolean disponible(Hotel hotel,
@FutureOrPresent LocalDate fechaInicio,
@FutureOrPresent LocalDate fechaFin,
@PositiveOrZero int numHabSimple,
@PositiveOrZero int numHabDoble) {
return h.disponible(fechaInicio, fechaFin, numHabSimple, numHabDoble);
return hotel.disponible(fechaInicio, fechaFin, numHabSimple, numHabDoble);
}
/**
* Realiza una reserva en un hotel. La reserva debe ser correcta y haber disponibilidad.
*
* @param cliente cliente que realiza la reserva
* @param hotel hotel donde se realiza la reserva.
* @param fechaInicio fecha de inicio de la reserva
* @param fechaFin fecha de final de la reserva
* @param numHabSimple número de habitaciones simples solicitadas
* @param numHabDoble número de habitaciones dobles solicitadas
* @return la reserva recien creada en caso de éxito
*/
public Reserva reserva(Cliente cliente, Hotel hotel,
LocalDate fechaInicio, LocalDate fechaFin,
int numHabSimple, int numHabDoble) {
@PositiveOrZero int numHabSimple, @PositiveOrZero int numHabDoble) {
var reserva = new Reserva(nReserva++, cliente, fechaInicio, fechaFin, numHabSimple, numHabDoble);
hotel.nuevaReserva(reserva);
// Por implementar
throw new UnsupportedOperationException();
return reserva;
}
}
......@@ -4,6 +4,7 @@ package es.ujaen.dae.reservahoteles.servicios;
import es.ujaen.dae.reservahoteles.entidades.Cliente;
import es.ujaen.dae.reservahoteles.entidades.Hotel;
import es.ujaen.dae.reservahoteles.excepciones.ClienteYaRegistrado;
import es.ujaen.dae.reservahoteles.excepciones.NoDisponibilidadReserva;
import jakarta.validation.ConstraintViolationException;
import java.time.LocalDate;
import java.util.List;
......@@ -56,8 +57,10 @@ public class TestServicioReservas {
var hotel1 = new Hotel(1, "Gran Hotel Almería", "Almería", "Almería", "04001", 25, 50, 100, 180);
var hotel2 = new Hotel(2, "Hotel Infanta Cristina", "Jaén", "Jaén", "23009", 30, 60, 120, 200);
servicio.nuevoHotel(hotel1);
servicio.nuevoHotel(hotel2);
var direccion = servicio.login("direccion@hotelxyz.es", "SeCrEtO").get();
servicio.nuevoHotel(direccion, hotel1);
servicio.nuevoHotel(direccion, hotel2);
List<Hotel> hoteles = servicio.buscarHotel("gran hotel", " almeria");
......@@ -71,10 +74,12 @@ public class TestServicioReservas {
var hotel1 = new Hotel(1, "Gran Hotel Almería", "Almería", "Almería", "04001", 25, 50, 100, 180);
var hotel2 = new Hotel(2, "Hotel Espejo del Mar", "Almería", "Almería", "04001", 15, 35, 80, 110);
var hotel3 = new Hotel(3, "Hotel Infanta Cristina", "Jaén", "Jaén", "23009", 30, 60, 120, 200);
var direccion = servicio.login("direccion@hotelxyz.es", "SeCrEtO").get();
servicio.nuevoHotel(hotel1);
servicio.nuevoHotel(hotel2);
servicio.nuevoHotel(hotel3);
servicio.nuevoHotel(direccion, hotel1);
servicio.nuevoHotel(direccion, hotel2);
servicio.nuevoHotel(direccion, hotel3);
List<Hotel> hoteles = servicio.buscarHotelesDisponiblesPorLocalidad("almeria",
LocalDate.now().plusDays(7),
......@@ -84,5 +89,42 @@ public class TestServicioReservas {
assertThat(hoteles).hasSize(2);
assertThat(hoteles.getFirst().id()).isEqualTo(1);
assertThat(hoteles.getLast().id()).isEqualTo(2);
}
}
@Test
@DirtiesContext
void testReservaHotel() {
var direccion = servicio.login("direccion@hotelxyz.es", "SeCrEtO").get();
servicio.nuevoHotel(direccion, new Hotel(1, "Bed and Breakfast Almería", "Almería", "Almería", "04001", 2, 2, 60, 100));
servicio.nuevoCliente(new Cliente("Pedro", "Jaén Jaén", "611203025", "pjaen@gmail.com", "miClAvE"));
var hotel = servicio.buscarHotel("bed and breakfast", "almeria").getFirst();
var cliente = servicio.login("pjaen@gmail.com", "miClAvE").get();
var reserva = servicio.reserva(cliente, hotel, LocalDate.now().plusDays(7), LocalDate.now().plusDays(10), 0, 1);
assertThat(reserva.fechaInicio()).isEqualTo(LocalDate.now().plusDays(7));
assertThat(reserva.fechaFin()).isEqualTo(LocalDate.now().plusDays(10));
assertThat(reserva.numHabDoble()).isEqualTo(1);
}
@Test
@DirtiesContext
void testReservaHotelSinDisponibilidad() {
var direccion = servicio.login("direccion@hotelxyz.es", "SeCrEtO").get();
servicio.nuevoHotel(direccion, new Hotel(1, "Bed and Breakfast Almería", "Almería", "Almería", "04001", 2, 2, 60, 100));
servicio.nuevoCliente(new Cliente("Pedro", "Jaén Jaén", "611203025", "pjaen@gmail.com", "miClAvE"));
var hotel = servicio.buscarHotel("bed and breakfast", "almeria").getFirst();
var cliente = servicio.login("pjaen@gmail.com", "miClAvE").get();
// Reservar 2 habitaciones dobles
servicio.reserva(cliente, hotel, LocalDate.now().plusDays(7), LocalDate.now().plusDays(10), 0, 2);
// Reservar una simple y otra doble: no hay disponibilidad para fechas solapadas con la anterior reserva
assertThatThrownBy(()-> servicio.reserva(cliente, hotel, LocalDate.now().plusDays(5), LocalDate.now().plusDays(8), 1, 2))
.isInstanceOf(NoDisponibilidadReserva.class);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment