2020-07-19 16:51:16 +05:30
|
|
|
package com.ashishtrivedi16.transactionscript;
|
|
|
|
|
2020-07-26 15:56:55 +05:30
|
|
|
import java.util.Optional;
|
2020-07-19 22:20:15 +05:30
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
2020-07-19 16:51:16 +05:30
|
|
|
public class Hotel {
|
2020-07-26 15:56:55 +05:30
|
|
|
private static final Logger LOGGER = LoggerFactory.getLogger(TransactionScriptApp.class);
|
2020-07-19 16:51:16 +05:30
|
|
|
|
2020-07-26 15:56:55 +05:30
|
|
|
private HotelDaoImpl hotelDao;
|
2020-07-19 22:20:15 +05:30
|
|
|
|
2020-07-26 15:56:55 +05:30
|
|
|
public Hotel(HotelDaoImpl hotelDao) {
|
|
|
|
this.hotelDao = hotelDao;
|
|
|
|
}
|
2020-07-19 22:20:15 +05:30
|
|
|
|
2020-07-26 15:56:55 +05:30
|
|
|
/**
|
|
|
|
* Book a room.
|
|
|
|
*
|
|
|
|
* @param roomNumber room to book
|
|
|
|
* @throws Exception if any error
|
|
|
|
*/
|
|
|
|
public void bookRoom(int roomNumber) throws Exception {
|
|
|
|
|
|
|
|
Optional<Room> room = hotelDao.getById(roomNumber);
|
|
|
|
|
|
|
|
if (room.isEmpty()) {
|
2020-07-26 17:38:33 +05:30
|
|
|
throw new Exception("Room number: " + roomNumber + " does not exist");
|
2020-07-26 15:56:55 +05:30
|
|
|
} else {
|
|
|
|
if (room.get().isBooked()) {
|
2020-07-26 17:38:33 +05:30
|
|
|
throw new Exception("Room already booked!");
|
2020-07-26 15:56:55 +05:30
|
|
|
} else {
|
|
|
|
Room updateRoomBooking = room.get();
|
|
|
|
updateRoomBooking.setBooked(true);
|
|
|
|
hotelDao.update(updateRoomBooking);
|
|
|
|
}
|
2020-07-19 16:51:16 +05:30
|
|
|
}
|
2020-07-26 15:56:55 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cancel a room booking.
|
|
|
|
*
|
|
|
|
* @param roomNumber room to cancel booking
|
|
|
|
* @throws Exception if any error
|
|
|
|
*/
|
|
|
|
public void cancelRoomBooking(int roomNumber) throws Exception {
|
|
|
|
|
|
|
|
Optional<Room> room = hotelDao.getById(roomNumber);
|
2020-07-19 16:51:16 +05:30
|
|
|
|
2020-07-26 15:56:55 +05:30
|
|
|
if (room.isEmpty()) {
|
2020-07-26 17:38:33 +05:30
|
|
|
throw new Exception("Room number: " + roomNumber + " does not exist");
|
2020-07-26 15:56:55 +05:30
|
|
|
} else {
|
|
|
|
if (room.get().isBooked()) {
|
|
|
|
Room updateRoomBooking = room.get();
|
|
|
|
updateRoomBooking.setBooked(false);
|
|
|
|
int refundAmount = updateRoomBooking.getPrice();
|
|
|
|
hotelDao.update(updateRoomBooking);
|
2020-07-19 20:33:52 +05:30
|
|
|
|
2020-07-26 15:56:55 +05:30
|
|
|
LOGGER.info("Booking cancelled for room number: " + roomNumber);
|
|
|
|
LOGGER.info(refundAmount + " is refunded");
|
|
|
|
} else {
|
2020-07-26 17:38:33 +05:30
|
|
|
throw new Exception("No booking for the room exists");
|
2020-07-26 15:56:55 +05:30
|
|
|
}
|
2020-07-19 16:51:16 +05:30
|
|
|
}
|
2020-07-26 15:56:55 +05:30
|
|
|
}
|
2020-07-19 16:51:16 +05:30
|
|
|
}
|