This commit is contained in:
Ashish Trivedi
2020-07-19 22:20:15 +05:30
parent 52e6ea2b68
commit c4b1b89f1f
4 changed files with 55 additions and 36 deletions
@@ -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());
}
}