App.java 3.8 KB
Newer Older
A
#1321  
Ashish Trivedi 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * The MIT License
 * Copyright © 2014-2019 Ilkka Seppälä
 *
 * 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.
 */

A
#1321  
Ashish Trivedi 已提交
24 25
package com.ashishtrivedi16.transactionscript;

A
#1321  
Ashish Trivedi 已提交
26
import java.util.List;
A
#1321  
Ashish Trivedi 已提交
27 28 29 30
import javax.sql.DataSource;
import org.h2.jdbcx.JdbcDataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
A
#1321  
Ashish Trivedi 已提交
31

A
#1321  
Ashish Trivedi 已提交
32
public class App {
A
#1321  
Ashish Trivedi 已提交
33

34
  private static final String H2_DB_URL = "jdbc:h2:~/test";
A
Ashish Trivedi 已提交
35
  private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
A
#1321  
Ashish Trivedi 已提交
36

37 38 39 40 41 42 43
  /**
   * Program entry point.
   *
   * @param args command line arguments
   * @throws Exception if any error occurs
   */
  public static void main(String[] args) throws Exception {
A
#1321  
Ashish Trivedi 已提交
44

45 46 47 48
    final var dataSource = createDataSource();
    deleteSchema(dataSource);
    createSchema(dataSource);
    final var dao = new HotelDaoImpl(dataSource);
A
Ashish Trivedi 已提交
49

50
    addRooms(dao);
A
#1321  
Ashish Trivedi 已提交
51

52
    getRoomStatus(dao);
A
#1321  
Ashish_Trivedi 已提交
53 54
  
    var hotel = new Hotel(dao);
A
#1321  
Ashish Trivedi 已提交
55

56 57 58 59 60 61
    hotel.bookRoom(1);
    hotel.bookRoom(2);
    hotel.bookRoom(3);
    hotel.bookRoom(4);
    hotel.bookRoom(5);
    hotel.bookRoom(6);
A
#1321  
Ashish Trivedi 已提交
62

63 64 65
    hotel.cancelRoomBooking(1);
    hotel.cancelRoomBooking(3);
    hotel.cancelRoomBooking(5);
A
#1321  
Ashish Trivedi 已提交
66

67
    getRoomStatus(dao);
A
#1321  
Ashish Trivedi 已提交
68

69
    deleteSchema(dataSource);
A
#1321  
Ashish Trivedi 已提交
70

71
  }
A
#1321  
Ashish Trivedi 已提交
72

73 74 75
  private static void getRoomStatus(HotelDaoImpl dao) throws Exception {
    try (var customerStream = dao.getAll()) {
      customerStream.forEach((customer) -> LOGGER.info(customer.toString()));
A
#1321  
Ashish Trivedi 已提交
76
    }
77
  }
A
#1321  
Ashish Trivedi 已提交
78

A
#1321  
Ashish_Trivedi 已提交
79
  private static void deleteSchema(DataSource dataSource) throws java.sql.SQLException {
80 81 82
    try (var connection = dataSource.getConnection();
         var statement = connection.createStatement()) {
      statement.execute(RoomSchemaSql.DELETE_SCHEMA_SQL);
A
#1321  
Ashish Trivedi 已提交
83
    }
84 85 86 87 88 89
  }

  private static void createSchema(DataSource dataSource) throws Exception {
    try (var connection = dataSource.getConnection();
         var statement = connection.createStatement()) {
      statement.execute(RoomSchemaSql.CREATE_SCHEMA_SQL);
A
#1321  
Ashish Trivedi 已提交
90
    } catch (Exception e) {
A
#1321  
Ashish_Trivedi 已提交
91
      throw new SqlException(e.getMessage(), e);
A
#1321  
Ashish Trivedi 已提交
92
    }
93 94 95 96 97 98 99
  }

  /**
   * Get database.
   *
   * @return h2 datasource
   */
A
Ashish Trivedi 已提交
100
  private static DataSource createDataSource() {
A
#1321  
Ashish_Trivedi 已提交
101
    var dataSource = new JdbcDataSource();
102 103 104 105 106 107 108
    dataSource.setUrl(H2_DB_URL);
    return dataSource;
  }

  private static void addRooms(HotelDaoImpl hotelDao) throws Exception {
    for (var room : generateSampleRooms()) {
      hotelDao.add(room);
A
#1321  
Ashish Trivedi 已提交
109
    }
110 111 112 113 114 115 116
  }

  /**
   * Generate rooms.
   *
   * @return list of rooms
   */
A
Ashish Trivedi 已提交
117
  private static List<Room> generateSampleRooms() {
118 119 120 121 122 123 124 125
    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);
  }
A
#1321  
Ashish Trivedi 已提交
126
}