parent
bdf2145b3f
commit
7acc5fbf95
1
pom.xml
1
pom.xml
@ -372,6 +372,7 @@
|
||||
<source>11</source>
|
||||
<target>11</target>
|
||||
</configuration>
|
||||
<version>3.0.0-M3</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
|
@ -44,7 +44,7 @@ public class Hotel {
|
||||
*/
|
||||
public void bookRoom(int roomNumber) throws Exception {
|
||||
|
||||
Optional<Room> 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> 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);
|
||||
|
@ -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<Room>(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"),
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user