Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
QZero_0
java-design-patterns
提交
7acc5fbf
J
java-design-patterns
项目概览
QZero_0
/
java-design-patterns
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
J
java-design-patterns
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
7acc5fbf
编写于
8月 09, 2020
作者:
A
Ashish_Trivedi
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
#1321
Resolved conflicts and used var wherever possible
上级
bdf2145b
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
33 addition
and
35 deletion
+33
-35
pom.xml
pom.xml
+1
-0
transaction-script/src/main/java/com/ashishtrivedi16/transactionscript/Hotel.java
...ain/java/com/ashishtrivedi16/transactionscript/Hotel.java
+5
-5
transaction-script/src/main/java/com/ashishtrivedi16/transactionscript/HotelDaoImpl.java
...a/com/ashishtrivedi16/transactionscript/HotelDaoImpl.java
+16
-17
transaction-script/src/main/java/com/ashishtrivedi16/transactionscript/SqlException.java
...a/com/ashishtrivedi16/transactionscript/SqlException.java
+4
-4
transaction-script/src/main/java/com/ashishtrivedi16/transactionscript/TransactionScriptApp.java
...hishtrivedi16/transactionscript/TransactionScriptApp.java
+5
-6
transaction-script/src/test/java/com/ashishtrivedi16/transactionscript/HotelTest.java
...java/com/ashishtrivedi16/transactionscript/HotelTest.java
+2
-3
未找到文件。
pom.xml
浏览文件 @
7acc5fbf
...
...
@@ -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>
...
...
transaction-script/src/main/java/com/ashishtrivedi16/transactionscript/Hotel.java
浏览文件 @
7acc5fbf
...
...
@@ -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
);
...
...
transaction-script/src/main/java/com/ashishtrivedi16/transactionscript/HotelDaoImpl.java
浏览文件 @
7acc5fbf
...
...
@@ -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
(
SQL
Exception
e
)
{
}
catch
(
Exception
e
)
{
throw
new
RuntimeException
(
e
);
// NOSONAR
}
}
...
...
@@ -71,8 +70,8 @@ public class HotelDaoImpl implements HotelDao {
e
.
printStackTrace
();
}
});
}
catch
(
SQL
Exception
e
)
{
throw
new
Custom
Exception
(
e
.
getMessage
(),
e
);
}
catch
(
Exception
e
)
{
throw
new
Sql
Exception
(
e
.
getMessage
(),
e
);
}
}
...
...
@@ -90,8 +89,8 @@ public class HotelDaoImpl implements HotelDao {
}
else
{
return
Optional
.
empty
();
}
}
catch
(
SQL
Exception
ex
)
{
throw
new
Custom
Exception
(
ex
.
getMessage
(),
ex
);
}
catch
(
Exception
ex
)
{
throw
new
Sql
Exception
(
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
(
SQL
Exception
ex
)
{
throw
new
Custom
Exception
(
ex
.
getMessage
(),
ex
);
}
catch
(
Exception
ex
)
{
throw
new
Sql
Exception
(
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
(
SQL
Exception
ex
)
{
throw
new
Custom
Exception
(
ex
.
getMessage
(),
ex
);
}
catch
(
Exception
ex
)
{
throw
new
Sql
Exception
(
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
(
SQL
Exception
ex
)
{
throw
new
Custom
Exception
(
ex
.
getMessage
(),
ex
);
}
catch
(
Exception
ex
)
{
throw
new
Sql
Exception
(
ex
.
getMessage
(),
ex
);
}
}
private
Connection
getConnection
()
throws
SQL
Exception
{
private
Connection
getConnection
()
throws
Exception
{
return
dataSource
.
getConnection
();
}
...
...
@@ -156,12 +155,12 @@ public class HotelDaoImpl implements HotelDao {
resultSet
.
close
();
statement
.
close
();
connection
.
close
();
}
catch
(
SQL
Exception
e
)
{
throw
new
Custom
Exception
(
e
.
getMessage
(),
e
);
}
catch
(
Exception
e
)
{
throw
new
Sql
Exception
(
e
.
getMessage
(),
e
);
}
}
private
Room
createRoom
(
ResultSet
resultSet
)
throws
SQL
Exception
{
private
Room
createRoom
(
ResultSet
resultSet
)
throws
Exception
{
return
new
Room
(
resultSet
.
getInt
(
"ID"
),
resultSet
.
getString
(
"ROOM_TYPE"
),
resultSet
.
getInt
(
"PRICE"
),
...
...
transaction-script/src/main/java/com/ashishtrivedi16/transactionscript/
Custom
Exception.java
→
transaction-script/src/main/java/com/ashishtrivedi16/transactionscript/
Sql
Exception.java
浏览文件 @
7acc5fbf
...
...
@@ -26,18 +26,18 @@ package com.ashishtrivedi16.transactionscript;
/**
* Custom exception.
*/
public
class
Custom
Exception
extends
Exception
{
public
class
Sql
Exception
extends
Exception
{
private
static
final
long
serialVersionUID
=
1L
;
public
Custom
Exception
()
{
public
Sql
Exception
()
{
}
public
Custom
Exception
(
String
message
)
{
public
Sql
Exception
(
String
message
)
{
super
(
message
);
}
public
Custom
Exception
(
String
message
,
Throwable
cause
)
{
public
Sql
Exception
(
String
message
,
Throwable
cause
)
{
super
(
message
,
cause
);
}
}
transaction-script/src/main/java/com/ashishtrivedi16/transactionscript/TransactionScriptApp.java
浏览文件 @
7acc5fbf
...
...
@@ -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
Custom
Exception
(
e
.
getMessage
(),
e
);
throw
new
Sql
Exception
(
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
;
}
...
...
transaction-script/src/test/java/com/ashishtrivedi16/transactionscript/HotelTest.java
浏览文件 @
7acc5fbf
...
...
@@ -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
Custom
Exception
(
e
.
getMessage
(),
e
);
throw
new
Sql
Exception
(
e
.
getMessage
(),
e
);
}
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录