146 lines
4.9 KiB
Java
Raw Normal View History

2020-07-26 18:17:00 +05:30
/*
* The MIT License
2021-02-14 15:27:57 +05:30
* Copyright © 2014-2021 Ilkka Seppälä
2020-07-26 18:17:00 +05:30
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
2020-08-11 17:17:25 +03:00
package com.iluwatar.transactionscript;
2020-07-19 20:33:52 +05:30
import java.util.List;
2020-07-19 22:20:15 +05:30
import javax.sql.DataSource;
import org.h2.jdbcx.JdbcDataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
2020-07-19 20:33:52 +05:30
2020-08-11 00:21:46 +05:30
/**
* Transaction Script (TS) is one of the simplest domain logic pattern.
* It needs less work to implement than other domain logic patterns and therefore
* its perfect fit for smaller applications that don't need big architecture behind them.
*
* <p>In this example we will use the TS pattern to implement booking and cancellation
* methods for a Hotel management App. The main method will initialise an instance of
* {@link Hotel} and add rooms to it. After that it will book and cancel a couple of rooms
* and that will be printed by the logger.</p>
*
* <p>The thing we have to note here is that all the operations related to booking or cancelling
* a room like checking the database if the room exists, checking the booking status or the
* room, calculating refund price are all clubbed inside a single transaction script method.</p>
*/
2020-08-10 13:02:35 +05:30
public class App {
private static final String H2_DB_URL = "jdbc:h2:~/test";
2020-08-10 13:06:46 +05:30
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
2020-07-19 20:33:52 +05:30
/**
* Program entry point.
2020-08-11 00:21:46 +05:30
* Initialises an instance of Hotel and adds rooms to it.
* Carries out booking and cancel booking transactions.
* @param args command line arguments
* @throws Exception if any error occurs
*/
public static void main(String[] args) throws Exception {
2020-07-19 20:33:52 +05:30
final var dataSource = createDataSource();
deleteSchema(dataSource);
createSchema(dataSource);
final var dao = new HotelDaoImpl(dataSource);
2020-07-27 00:39:53 +05:30
2020-08-11 00:21:46 +05:30
// Add rooms
addRooms(dao);
2020-07-19 22:20:15 +05:30
2020-08-11 00:21:46 +05:30
// Print room booking status
getRoomStatus(dao);
var hotel = new Hotel(dao);
2020-07-19 22:20:15 +05:30
2020-08-11 00:21:46 +05:30
// Book rooms
hotel.bookRoom(1);
hotel.bookRoom(2);
hotel.bookRoom(3);
hotel.bookRoom(4);
hotel.bookRoom(5);
hotel.bookRoom(6);
2020-07-19 22:20:15 +05:30
2020-08-11 00:21:46 +05:30
// Cancel booking for a few rooms
hotel.cancelRoomBooking(1);
hotel.cancelRoomBooking(3);
hotel.cancelRoomBooking(5);
2020-07-19 22:20:15 +05:30
getRoomStatus(dao);
2020-07-19 20:33:52 +05:30
deleteSchema(dataSource);
2020-07-19 20:33:52 +05:30
}
private static void getRoomStatus(HotelDaoImpl dao) throws Exception {
try (var customerStream = dao.getAll()) {
customerStream.forEach((customer) -> LOGGER.info(customer.toString()));
2020-07-19 20:33:52 +05:30
}
}
2020-07-19 20:33:52 +05:30
private static void deleteSchema(DataSource dataSource) throws java.sql.SQLException {
try (var connection = dataSource.getConnection();
var statement = connection.createStatement()) {
statement.execute(RoomSchemaSql.DELETE_SCHEMA_SQL);
2020-07-19 20:33:52 +05:30
}
}
private static void createSchema(DataSource dataSource) throws Exception {
try (var connection = dataSource.getConnection();
var statement = connection.createStatement()) {
statement.execute(RoomSchemaSql.CREATE_SCHEMA_SQL);
2020-07-26 18:17:00 +05:30
} catch (Exception e) {
2020-08-10 13:21:24 +05:30
throw new Exception(e.getMessage(), e);
2020-07-19 20:33:52 +05:30
}
}
/**
* Get database.
*
* @return h2 datasource
*/
2020-07-27 00:39:53 +05:30
private static DataSource createDataSource() {
var dataSource = new JdbcDataSource();
dataSource.setUrl(H2_DB_URL);
return dataSource;
}
private static void addRooms(HotelDaoImpl hotelDao) throws Exception {
for (var room : generateSampleRooms()) {
hotelDao.add(room);
2020-07-19 20:33:52 +05:30
}
}
/**
* Generate rooms.
*
* @return list of rooms
*/
2020-07-27 00:39:53 +05:30
private static List<Room> generateSampleRooms() {
final var room1 = new Room(1, "Single", 50, false);
final var room2 = new Room(2, "Double", 80, false);
final var room3 = new Room(3, "Queen", 120, false);
final var room4 = new Room(4, "King", 150, false);
final var room5 = new Room(5, "Single", 50, false);
final var room6 = new Room(6, "Double", 80, false);
return List.of(room1, room2, room3, room4, room5, room6);
}
}