This commit is contained in:
parent
52e6ea2b68
commit
c4b1b89f1f
@ -1,15 +1,41 @@
|
||||
package com.ashishtrivedi16.transactionscript;
|
||||
|
||||
public class Hotel {
|
||||
import com.ashishtrivedi16.transactionscript.db.HotelDAOImpl;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public void bookRoom(int roomNumber) {
|
||||
import java.util.Optional;
|
||||
|
||||
public class Hotel {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(TransactionScriptApp.class);
|
||||
|
||||
private HotelDAOImpl hotelDAO;
|
||||
|
||||
public Hotel(HotelDAOImpl hotelDAO) {
|
||||
this.hotelDAO = hotelDAO;
|
||||
}
|
||||
|
||||
public void bookRoom(int roomNumber) throws Exception {
|
||||
/*
|
||||
TODO
|
||||
-> Check if room is available
|
||||
-> Calculate price
|
||||
-> Book the room
|
||||
-> Commit transaction
|
||||
*/
|
||||
|
||||
Optional<Room> room = hotelDAO.getById(roomNumber);
|
||||
|
||||
if (!room.isPresent()) {
|
||||
LOGGER.info(roomNumber + " does not exist");
|
||||
} else {
|
||||
if (room.get().isBooked()) {
|
||||
LOGGER.info("Room already booked!");
|
||||
} else {
|
||||
Room updateRoomBooking = room.get();
|
||||
updateRoomBooking.setBooked(true);
|
||||
hotelDAO.update(updateRoomBooking);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void cancelRoomBooking(int roomNumber) {
|
||||
|
@ -1,15 +1,18 @@
|
||||
package com.ashishtrivedi16.transactionscript;
|
||||
|
||||
import com.ashishtrivedi16.transactionscript.db.CustomException;
|
||||
import com.ashishtrivedi16.transactionscript.db.HotelDAOImpl;
|
||||
import com.ashishtrivedi16.transactionscript.db.RoomSchemaSql;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.h2.jdbc.JdbcSQLException;
|
||||
import org.h2.jdbcx.JdbcDataSource;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
public class TransactionScriptApp {
|
||||
|
||||
private static final String H2_DB_URL = "jdbc:h2:~/test";
|
||||
@ -25,10 +28,24 @@ public class TransactionScriptApp {
|
||||
final var dataSource = createDataSource();
|
||||
createSchema(dataSource);
|
||||
final var DAO = new HotelDAOImpl(dataSource);
|
||||
|
||||
addRooms(DAO);
|
||||
try (var customerStream = DAO.getAll()) {
|
||||
customerStream.forEach((customer) -> LOGGER.info(customer.toString()));
|
||||
}
|
||||
|
||||
Hotel hotel = new Hotel(DAO);
|
||||
|
||||
hotel.bookRoom(1);
|
||||
hotel.bookRoom(2);
|
||||
hotel.bookRoom(3);
|
||||
hotel.bookRoom(4);
|
||||
hotel.bookRoom(5);
|
||||
hotel.bookRoom(6);
|
||||
|
||||
hotel.cancelRoomBooking(3);
|
||||
hotel.cancelRoomBooking(4);
|
||||
|
||||
deleteSchema(dataSource);
|
||||
|
||||
}
|
||||
@ -40,10 +57,12 @@ public class TransactionScriptApp {
|
||||
}
|
||||
}
|
||||
|
||||
private static void createSchema(DataSource dataSource) throws SQLException {
|
||||
private static void createSchema(DataSource dataSource) throws Exception {
|
||||
try (var connection = dataSource.getConnection();
|
||||
var statement = connection.createStatement()) {
|
||||
statement.execute(RoomSchemaSql.CREATE_SCHEMA_SQL);
|
||||
} catch (JdbcSQLException e) {
|
||||
throw new CustomException(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@ -68,25 +87,4 @@ public class TransactionScriptApp {
|
||||
final var room6 = new Room(6, "Double", 80, false);
|
||||
return List.of(room1, room2, room3, room4, room5, room6);
|
||||
}
|
||||
|
||||
private static void generate(final HotelDAOImpl hotelDAO) throws Exception {
|
||||
// addRooms(hotelDAO);
|
||||
// LOGGER.info(ALL_ROOMS);
|
||||
// try (var customerStream = hotelDAO.getAll()) {
|
||||
// customerStream.forEach((customer) -> LOGGER.info(customer.toString()));
|
||||
// }
|
||||
// LOGGER.info("hotelDAO.getCustomerById(2): " + hotelDAO.getById(2));
|
||||
// final var customer = new Room(4, "Dan", "Danson");
|
||||
// hotelDAO.add(customer);
|
||||
// LOGGER.info(ALL_ROOMS + hotelDAO.getAll());
|
||||
// customer.setFirstName("Daniel");
|
||||
// customer.setLastName("Danielson");
|
||||
// hotelDAO.update(customer);
|
||||
// LOGGER.info(ALL_ROOMS);
|
||||
// try (var customerStream = hotelDAO.getAll()) {
|
||||
// customerStream.forEach((cust) -> LOGGER.info(cust.toString()));
|
||||
// }
|
||||
// hotelDAO.delete(customer);
|
||||
// LOGGER.info(ALL_ROOMS + hotelDAO.getAll());
|
||||
}
|
||||
}
|
||||
|
@ -2,7 +2,6 @@ package com.ashishtrivedi16.transactionscript.db;
|
||||
|
||||
import com.ashishtrivedi16.transactionscript.Room;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
@ -12,7 +11,7 @@ public interface HotelDAO {
|
||||
|
||||
public Optional<Room> getById(int id) throws Exception;
|
||||
|
||||
public Boolean add(Room room) throws SQLException, Exception;
|
||||
public Boolean add(Room room) throws Exception;
|
||||
|
||||
public Boolean update(Room room) throws Exception;
|
||||
|
||||
|
@ -1,9 +1,6 @@
|
||||
package com.ashishtrivedi16.transactionscript.db;
|
||||
|
||||
import com.ashishtrivedi16.transactionscript.Room;
|
||||
import com.ashishtrivedi16.transactionscript.TransactionScriptApp;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.sql.Connection;
|
||||
@ -19,7 +16,6 @@ import java.util.stream.StreamSupport;
|
||||
|
||||
public class HotelDAOImpl implements HotelDAO {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(TransactionScriptApp.class);
|
||||
private final DataSource dataSource;
|
||||
|
||||
public HotelDAOImpl(DataSource dataSource) {
|
||||
@ -89,7 +85,7 @@ public class HotelDAOImpl implements HotelDAO {
|
||||
}
|
||||
|
||||
try (var connection = getConnection();
|
||||
var statement = connection.prepareStatement("INSERT INTO ROOMS VALUES (?,?,?)")) {
|
||||
var statement = connection.prepareStatement("INSERT INTO ROOMS VALUES (?,?,?,?)")) {
|
||||
statement.setInt(1, room.getId());
|
||||
statement.setString(2, room.getRoomType());
|
||||
statement.setInt(3, room.getPrice());
|
||||
|
Loading…
x
Reference in New Issue
Block a user