1
									
								
								pom.xml
									
									
									
									
									
								
							
							
						
						
									
										1
									
								
								pom.xml
									
									
									
									
									
								
							| @@ -372,6 +372,7 @@ | |||||||
|                         <source>11</source> |                         <source>11</source> | ||||||
|                         <target>11</target> |                         <target>11</target> | ||||||
|                     </configuration> |                     </configuration> | ||||||
|  |                     <version>3.0.0-M3</version> | ||||||
|                 </plugin> |                 </plugin> | ||||||
|                 <plugin> |                 <plugin> | ||||||
|                     <groupId>org.apache.maven.plugins</groupId> |                     <groupId>org.apache.maven.plugins</groupId> | ||||||
|   | |||||||
| @@ -44,7 +44,7 @@ public class Hotel { | |||||||
|    */ |    */ | ||||||
|   public void bookRoom(int roomNumber) throws Exception { |   public void bookRoom(int roomNumber) throws Exception { | ||||||
|  |  | ||||||
|     Optional<Room> room = hotelDao.getById(roomNumber); |     var room = hotelDao.getById(roomNumber); | ||||||
|  |  | ||||||
|     if (room.isEmpty()) { |     if (room.isEmpty()) { | ||||||
|       throw new Exception("Room number: " + roomNumber + " does not exist"); |       throw new Exception("Room number: " + roomNumber + " does not exist"); | ||||||
| @@ -52,7 +52,7 @@ public class Hotel { | |||||||
|       if (room.get().isBooked()) { |       if (room.get().isBooked()) { | ||||||
|         throw new Exception("Room already booked!"); |         throw new Exception("Room already booked!"); | ||||||
|       } else { |       } else { | ||||||
|         Room updateRoomBooking = room.get(); |         var updateRoomBooking = room.get(); | ||||||
|         updateRoomBooking.setBooked(true); |         updateRoomBooking.setBooked(true); | ||||||
|         hotelDao.update(updateRoomBooking); |         hotelDao.update(updateRoomBooking); | ||||||
|       } |       } | ||||||
| @@ -67,13 +67,13 @@ public class Hotel { | |||||||
|    */ |    */ | ||||||
|   public void cancelRoomBooking(int roomNumber) throws Exception { |   public void cancelRoomBooking(int roomNumber) throws Exception { | ||||||
|    |    | ||||||
|     Optional<Room> room = hotelDao.getById(roomNumber); |     var room = hotelDao.getById(roomNumber); | ||||||
|  |  | ||||||
|     if (room.isEmpty()) { |     if (room.isEmpty()) { | ||||||
|       throw new Exception("Room number: " + roomNumber + " does not exist"); |       throw new Exception("Room number: " + roomNumber + " does not exist"); | ||||||
|     } else { |     } else { | ||||||
|       if (room.get().isBooked()) { |       if (room.get().isBooked()) { | ||||||
|         Room updateRoomBooking = room.get(); |         var updateRoomBooking = room.get(); | ||||||
|         updateRoomBooking.setBooked(false); |         updateRoomBooking.setBooked(false); | ||||||
|         int refundAmount = updateRoomBooking.getPrice(); |         int refundAmount = updateRoomBooking.getPrice(); | ||||||
|         hotelDao.update(updateRoomBooking); |         hotelDao.update(updateRoomBooking); | ||||||
|   | |||||||
| @@ -26,7 +26,6 @@ package com.ashishtrivedi16.transactionscript; | |||||||
| import java.sql.Connection; | import java.sql.Connection; | ||||||
| import java.sql.PreparedStatement; | import java.sql.PreparedStatement; | ||||||
| import java.sql.ResultSet; | import java.sql.ResultSet; | ||||||
| import java.sql.SQLException; |  | ||||||
| import java.util.Optional; | import java.util.Optional; | ||||||
| import java.util.Spliterator; | import java.util.Spliterator; | ||||||
| import java.util.Spliterators; | import java.util.Spliterators; | ||||||
| @@ -48,7 +47,7 @@ public class HotelDaoImpl implements HotelDao { | |||||||
|     try { |     try { | ||||||
|       var connection = getConnection(); |       var connection = getConnection(); | ||||||
|       var statement = connection.prepareStatement("SELECT * FROM ROOMS"); |       var statement = connection.prepareStatement("SELECT * FROM ROOMS"); | ||||||
|       ResultSet resultSet = statement.executeQuery(); // NOSONAR |       var resultSet = statement.executeQuery(); // NOSONAR | ||||||
|       return StreamSupport.stream(new Spliterators.AbstractSpliterator<Room>(Long.MAX_VALUE, |       return StreamSupport.stream(new Spliterators.AbstractSpliterator<Room>(Long.MAX_VALUE, | ||||||
|           Spliterator.ORDERED) { |           Spliterator.ORDERED) { | ||||||
|  |  | ||||||
| @@ -60,7 +59,7 @@ public class HotelDaoImpl implements HotelDao { | |||||||
|             } |             } | ||||||
|             action.accept(createRoom(resultSet)); |             action.accept(createRoom(resultSet)); | ||||||
|             return true; |             return true; | ||||||
|           } catch (SQLException e) { |           } catch (Exception e) { | ||||||
|             throw new RuntimeException(e); // NOSONAR |             throw new RuntimeException(e); // NOSONAR | ||||||
|           } |           } | ||||||
|         } |         } | ||||||
| @@ -71,8 +70,8 @@ public class HotelDaoImpl implements HotelDao { | |||||||
|           e.printStackTrace(); |           e.printStackTrace(); | ||||||
|         } |         } | ||||||
|       }); |       }); | ||||||
|     } catch (SQLException e) { |     } catch (Exception e) { | ||||||
|       throw new CustomException(e.getMessage(), e); |       throw new SqlException(e.getMessage(), e); | ||||||
|     } |     } | ||||||
|   } |   } | ||||||
|  |  | ||||||
| @@ -90,8 +89,8 @@ public class HotelDaoImpl implements HotelDao { | |||||||
|       } else { |       } else { | ||||||
|         return Optional.empty(); |         return Optional.empty(); | ||||||
|       } |       } | ||||||
|     } catch (SQLException ex) { |     } catch (Exception ex) { | ||||||
|       throw new CustomException(ex.getMessage(), ex); |       throw new SqlException(ex.getMessage(), ex); | ||||||
|     } finally { |     } finally { | ||||||
|       if (resultSet != null) { |       if (resultSet != null) { | ||||||
|         resultSet.close(); |         resultSet.close(); | ||||||
| @@ -113,8 +112,8 @@ public class HotelDaoImpl implements HotelDao { | |||||||
|       statement.setBoolean(4, room.isBooked()); |       statement.setBoolean(4, room.isBooked()); | ||||||
|       statement.execute(); |       statement.execute(); | ||||||
|       return true; |       return true; | ||||||
|     } catch (SQLException ex) { |     } catch (Exception ex) { | ||||||
|       throw new CustomException(ex.getMessage(), ex); |       throw new SqlException(ex.getMessage(), ex); | ||||||
|     } |     } | ||||||
|   } |   } | ||||||
|  |  | ||||||
| @@ -130,8 +129,8 @@ public class HotelDaoImpl implements HotelDao { | |||||||
|       statement.setBoolean(3, room.isBooked()); |       statement.setBoolean(3, room.isBooked()); | ||||||
|       statement.setInt(4, room.getId()); |       statement.setInt(4, room.getId()); | ||||||
|       return statement.executeUpdate() > 0; |       return statement.executeUpdate() > 0; | ||||||
|     } catch (SQLException ex) { |     } catch (Exception ex) { | ||||||
|       throw new CustomException(ex.getMessage(), ex); |       throw new SqlException(ex.getMessage(), ex); | ||||||
|     } |     } | ||||||
|   } |   } | ||||||
|  |  | ||||||
| @@ -141,12 +140,12 @@ public class HotelDaoImpl implements HotelDao { | |||||||
|          var statement = connection.prepareStatement("DELETE FROM ROOMS WHERE ID = ?")) { |          var statement = connection.prepareStatement("DELETE FROM ROOMS WHERE ID = ?")) { | ||||||
|       statement.setInt(1, room.getId()); |       statement.setInt(1, room.getId()); | ||||||
|       return statement.executeUpdate() > 0; |       return statement.executeUpdate() > 0; | ||||||
|     } catch (SQLException ex) { |     } catch (Exception ex) { | ||||||
|       throw new CustomException(ex.getMessage(), ex); |       throw new SqlException(ex.getMessage(), ex); | ||||||
|     } |     } | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   private Connection getConnection() throws SQLException { |   private Connection getConnection() throws Exception { | ||||||
|     return dataSource.getConnection(); |     return dataSource.getConnection(); | ||||||
|   } |   } | ||||||
|  |  | ||||||
| @@ -156,12 +155,12 @@ public class HotelDaoImpl implements HotelDao { | |||||||
|       resultSet.close(); |       resultSet.close(); | ||||||
|       statement.close(); |       statement.close(); | ||||||
|       connection.close(); |       connection.close(); | ||||||
|     } catch (SQLException e) { |     } catch (Exception e) { | ||||||
|       throw new CustomException(e.getMessage(), e); |       throw new SqlException(e.getMessage(), e); | ||||||
|     } |     } | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   private Room createRoom(ResultSet resultSet) throws SQLException { |   private Room createRoom(ResultSet resultSet) throws Exception { | ||||||
|     return new Room(resultSet.getInt("ID"), |     return new Room(resultSet.getInt("ID"), | ||||||
|         resultSet.getString("ROOM_TYPE"), |         resultSet.getString("ROOM_TYPE"), | ||||||
|         resultSet.getInt("PRICE"), |         resultSet.getInt("PRICE"), | ||||||
|   | |||||||
| @@ -26,18 +26,18 @@ package com.ashishtrivedi16.transactionscript; | |||||||
| /** | /** | ||||||
|  * Custom exception. |  * Custom exception. | ||||||
|  */ |  */ | ||||||
| public class CustomException extends Exception { | public class SqlException extends Exception { | ||||||
| 
 | 
 | ||||||
|   private static final long serialVersionUID = 1L; |   private static final long serialVersionUID = 1L; | ||||||
| 
 | 
 | ||||||
|   public CustomException() { |   public SqlException() { | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   public CustomException(String message) { |   public SqlException(String message) { | ||||||
|     super(message); |     super(message); | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   public CustomException(String message, Throwable cause) { |   public SqlException(String message, Throwable cause) { | ||||||
|     super(message, cause); |     super(message, cause); | ||||||
|   } |   } | ||||||
| } | } | ||||||
| @@ -23,7 +23,6 @@ | |||||||
|  |  | ||||||
| package com.ashishtrivedi16.transactionscript; | package com.ashishtrivedi16.transactionscript; | ||||||
|  |  | ||||||
| import java.sql.SQLException; |  | ||||||
| import java.util.List; | import java.util.List; | ||||||
| import javax.sql.DataSource; | import javax.sql.DataSource; | ||||||
| import org.h2.jdbcx.JdbcDataSource; | import org.h2.jdbcx.JdbcDataSource; | ||||||
| @@ -52,7 +51,7 @@ public class TransactionScriptApp { | |||||||
|  |  | ||||||
|     getRoomStatus(dao); |     getRoomStatus(dao); | ||||||
|    |    | ||||||
|     Hotel hotel = new Hotel(dao); |     var hotel = new Hotel(dao); | ||||||
|  |  | ||||||
|     hotel.bookRoom(1); |     hotel.bookRoom(1); | ||||||
|     hotel.bookRoom(2); |     hotel.bookRoom(2); | ||||||
| @@ -77,7 +76,7 @@ public class TransactionScriptApp { | |||||||
|     } |     } | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   private static void deleteSchema(DataSource dataSource) throws SQLException { |   private static void deleteSchema(DataSource dataSource) throws java.sql.SQLException { | ||||||
|     try (var connection = dataSource.getConnection(); |     try (var connection = dataSource.getConnection(); | ||||||
|          var statement = connection.createStatement()) { |          var statement = connection.createStatement()) { | ||||||
|       statement.execute(RoomSchemaSql.DELETE_SCHEMA_SQL); |       statement.execute(RoomSchemaSql.DELETE_SCHEMA_SQL); | ||||||
| @@ -89,7 +88,7 @@ public class TransactionScriptApp { | |||||||
|          var statement = connection.createStatement()) { |          var statement = connection.createStatement()) { | ||||||
|       statement.execute(RoomSchemaSql.CREATE_SCHEMA_SQL); |       statement.execute(RoomSchemaSql.CREATE_SCHEMA_SQL); | ||||||
|     } catch (Exception e) { |     } catch (Exception e) { | ||||||
|       throw new CustomException(e.getMessage(), e); |       throw new SqlException(e.getMessage(), e); | ||||||
|     } |     } | ||||||
|   } |   } | ||||||
|  |  | ||||||
| @@ -99,7 +98,7 @@ public class TransactionScriptApp { | |||||||
|    * @return h2 datasource |    * @return h2 datasource | ||||||
|    */ |    */ | ||||||
|   private static DataSource createDataSource() { |   private static DataSource createDataSource() { | ||||||
|     JdbcDataSource dataSource = new JdbcDataSource(); |     var dataSource = new JdbcDataSource(); | ||||||
|     dataSource.setUrl(H2_DB_URL); |     dataSource.setUrl(H2_DB_URL); | ||||||
|     return dataSource; |     return dataSource; | ||||||
|   } |   } | ||||||
|   | |||||||
| @@ -28,7 +28,6 @@ import org.junit.jupiter.api.BeforeEach; | |||||||
| import org.junit.jupiter.api.Test; | import org.junit.jupiter.api.Test; | ||||||
|  |  | ||||||
| import javax.sql.DataSource; | import javax.sql.DataSource; | ||||||
| import java.sql.SQLException; |  | ||||||
| import java.util.List; | import java.util.List; | ||||||
|  |  | ||||||
| import static org.junit.jupiter.api.Assertions.*; | import static org.junit.jupiter.api.Assertions.*; | ||||||
| @@ -103,7 +102,7 @@ public class HotelTest { | |||||||
|   } |   } | ||||||
|  |  | ||||||
|  |  | ||||||
|   private static void deleteSchema(DataSource dataSource) throws SQLException { |   private static void deleteSchema(DataSource dataSource) throws java.sql.SQLException { | ||||||
|     try (var connection = dataSource.getConnection(); |     try (var connection = dataSource.getConnection(); | ||||||
|          var statement = connection.createStatement()) { |          var statement = connection.createStatement()) { | ||||||
|       statement.execute(RoomSchemaSql.DELETE_SCHEMA_SQL); |       statement.execute(RoomSchemaSql.DELETE_SCHEMA_SQL); | ||||||
| @@ -115,7 +114,7 @@ public class HotelTest { | |||||||
|          var statement = connection.createStatement()) { |          var statement = connection.createStatement()) { | ||||||
|       statement.execute(RoomSchemaSql.CREATE_SCHEMA_SQL); |       statement.execute(RoomSchemaSql.CREATE_SCHEMA_SQL); | ||||||
|     } catch (Exception e) { |     } catch (Exception e) { | ||||||
|       throw new CustomException(e.getMessage(), e); |       throw new SqlException(e.getMessage(), e); | ||||||
|     } |     } | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user