diff --git a/pom.xml b/pom.xml index 38d8e97db..3c909312b 100644 --- a/pom.xml +++ b/pom.xml @@ -372,6 +372,7 @@ 11 11 + 3.0.0-M3 org.apache.maven.plugins diff --git a/transaction-script/src/main/java/com/ashishtrivedi16/transactionscript/Hotel.java b/transaction-script/src/main/java/com/ashishtrivedi16/transactionscript/Hotel.java index 58705e5e6..8a756f99c 100644 --- a/transaction-script/src/main/java/com/ashishtrivedi16/transactionscript/Hotel.java +++ b/transaction-script/src/main/java/com/ashishtrivedi16/transactionscript/Hotel.java @@ -44,7 +44,7 @@ public class Hotel { */ public void bookRoom(int roomNumber) throws Exception { - Optional room = hotelDao.getById(roomNumber); + var room = hotelDao.getById(roomNumber); if (room.isEmpty()) { throw new Exception("Room number: " + roomNumber + " does not exist"); @@ -52,7 +52,7 @@ public class Hotel { if (room.get().isBooked()) { throw new Exception("Room already booked!"); } else { - Room updateRoomBooking = room.get(); + var updateRoomBooking = room.get(); updateRoomBooking.setBooked(true); hotelDao.update(updateRoomBooking); } @@ -66,14 +66,14 @@ public class Hotel { * @throws Exception if any error */ public void cancelRoomBooking(int roomNumber) throws Exception { - - Optional room = hotelDao.getById(roomNumber); + + var room = hotelDao.getById(roomNumber); if (room.isEmpty()) { throw new Exception("Room number: " + roomNumber + " does not exist"); } else { if (room.get().isBooked()) { - Room updateRoomBooking = room.get(); + var updateRoomBooking = room.get(); updateRoomBooking.setBooked(false); int refundAmount = updateRoomBooking.getPrice(); hotelDao.update(updateRoomBooking); diff --git a/transaction-script/src/main/java/com/ashishtrivedi16/transactionscript/HotelDaoImpl.java b/transaction-script/src/main/java/com/ashishtrivedi16/transactionscript/HotelDaoImpl.java index f1b509416..e95363fb5 100644 --- a/transaction-script/src/main/java/com/ashishtrivedi16/transactionscript/HotelDaoImpl.java +++ b/transaction-script/src/main/java/com/ashishtrivedi16/transactionscript/HotelDaoImpl.java @@ -26,7 +26,6 @@ package com.ashishtrivedi16.transactionscript; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; -import java.sql.SQLException; import java.util.Optional; import java.util.Spliterator; import java.util.Spliterators; @@ -48,7 +47,7 @@ public class HotelDaoImpl implements HotelDao { try { var connection = getConnection(); var statement = connection.prepareStatement("SELECT * FROM ROOMS"); - ResultSet resultSet = statement.executeQuery(); // NOSONAR + var resultSet = statement.executeQuery(); // NOSONAR return StreamSupport.stream(new Spliterators.AbstractSpliterator(Long.MAX_VALUE, Spliterator.ORDERED) { @@ -60,7 +59,7 @@ public class HotelDaoImpl implements HotelDao { } action.accept(createRoom(resultSet)); return true; - } catch (SQLException e) { + } catch (Exception e) { throw new RuntimeException(e); // NOSONAR } } @@ -71,8 +70,8 @@ public class HotelDaoImpl implements HotelDao { e.printStackTrace(); } }); - } catch (SQLException e) { - throw new CustomException(e.getMessage(), e); + } catch (Exception e) { + throw new SqlException(e.getMessage(), e); } } @@ -90,8 +89,8 @@ public class HotelDaoImpl implements HotelDao { } else { return Optional.empty(); } - } catch (SQLException ex) { - throw new CustomException(ex.getMessage(), ex); + } catch (Exception ex) { + throw new SqlException(ex.getMessage(), ex); } finally { if (resultSet != null) { resultSet.close(); @@ -113,8 +112,8 @@ public class HotelDaoImpl implements HotelDao { statement.setBoolean(4, room.isBooked()); statement.execute(); return true; - } catch (SQLException ex) { - throw new CustomException(ex.getMessage(), ex); + } catch (Exception ex) { + throw new SqlException(ex.getMessage(), ex); } } @@ -130,8 +129,8 @@ public class HotelDaoImpl implements HotelDao { statement.setBoolean(3, room.isBooked()); statement.setInt(4, room.getId()); return statement.executeUpdate() > 0; - } catch (SQLException ex) { - throw new CustomException(ex.getMessage(), ex); + } catch (Exception 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 = ?")) { statement.setInt(1, room.getId()); return statement.executeUpdate() > 0; - } catch (SQLException ex) { - throw new CustomException(ex.getMessage(), ex); + } catch (Exception ex) { + throw new SqlException(ex.getMessage(), ex); } } - private Connection getConnection() throws SQLException { + private Connection getConnection() throws Exception { return dataSource.getConnection(); } @@ -156,12 +155,12 @@ public class HotelDaoImpl implements HotelDao { resultSet.close(); statement.close(); connection.close(); - } catch (SQLException e) { - throw new CustomException(e.getMessage(), e); + } catch (Exception 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"), resultSet.getString("ROOM_TYPE"), resultSet.getInt("PRICE"), diff --git a/transaction-script/src/main/java/com/ashishtrivedi16/transactionscript/CustomException.java b/transaction-script/src/main/java/com/ashishtrivedi16/transactionscript/SqlException.java similarity index 88% rename from transaction-script/src/main/java/com/ashishtrivedi16/transactionscript/CustomException.java rename to transaction-script/src/main/java/com/ashishtrivedi16/transactionscript/SqlException.java index 002ea79aa..369eb259d 100644 --- a/transaction-script/src/main/java/com/ashishtrivedi16/transactionscript/CustomException.java +++ b/transaction-script/src/main/java/com/ashishtrivedi16/transactionscript/SqlException.java @@ -26,18 +26,18 @@ package com.ashishtrivedi16.transactionscript; /** * Custom exception. */ -public class CustomException extends Exception { +public class SqlException extends Exception { private static final long serialVersionUID = 1L; - public CustomException() { + public SqlException() { } - public CustomException(String message) { + public SqlException(String message) { super(message); } - public CustomException(String message, Throwable cause) { + public SqlException(String message, Throwable cause) { super(message, cause); } } diff --git a/transaction-script/src/main/java/com/ashishtrivedi16/transactionscript/TransactionScriptApp.java b/transaction-script/src/main/java/com/ashishtrivedi16/transactionscript/TransactionScriptApp.java index 13a19dd48..e49e1d501 100644 --- a/transaction-script/src/main/java/com/ashishtrivedi16/transactionscript/TransactionScriptApp.java +++ b/transaction-script/src/main/java/com/ashishtrivedi16/transactionscript/TransactionScriptApp.java @@ -23,7 +23,6 @@ package com.ashishtrivedi16.transactionscript; -import java.sql.SQLException; import java.util.List; import javax.sql.DataSource; import org.h2.jdbcx.JdbcDataSource; @@ -51,8 +50,8 @@ public class TransactionScriptApp { addRooms(dao); getRoomStatus(dao); - - Hotel hotel = new Hotel(dao); + + var hotel = new Hotel(dao); hotel.bookRoom(1); 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(); var statement = connection.createStatement()) { statement.execute(RoomSchemaSql.DELETE_SCHEMA_SQL); @@ -89,7 +88,7 @@ public class TransactionScriptApp { var statement = connection.createStatement()) { statement.execute(RoomSchemaSql.CREATE_SCHEMA_SQL); } 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 */ private static DataSource createDataSource() { - JdbcDataSource dataSource = new JdbcDataSource(); + var dataSource = new JdbcDataSource(); dataSource.setUrl(H2_DB_URL); return dataSource; } diff --git a/transaction-script/src/test/java/com/ashishtrivedi16/transactionscript/HotelTest.java b/transaction-script/src/test/java/com/ashishtrivedi16/transactionscript/HotelTest.java index 62aad9527..86f4605b3 100644 --- a/transaction-script/src/test/java/com/ashishtrivedi16/transactionscript/HotelTest.java +++ b/transaction-script/src/test/java/com/ashishtrivedi16/transactionscript/HotelTest.java @@ -28,7 +28,6 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import javax.sql.DataSource; -import java.sql.SQLException; import java.util.List; 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(); var statement = connection.createStatement()) { statement.execute(RoomSchemaSql.DELETE_SCHEMA_SQL); @@ -115,7 +114,7 @@ public class HotelTest { var statement = connection.createStatement()) { statement.execute(RoomSchemaSql.CREATE_SCHEMA_SQL); } catch (Exception e) { - throw new CustomException(e.getMessage(), e); + throw new SqlException(e.getMessage(), e); } }