diff --git a/transaction-script/Readme.md b/transaction-script/Readme.md index f378e5672..0d0383c44 100644 --- a/transaction-script/Readme.md +++ b/transaction-script/Readme.md @@ -1,19 +1,15 @@ ---- # this is so called 'Yaml Front Matter', read up on it here: http://jekyllrb.com/docs/frontmatter/ -layout: pattern # layout must always be pattern -title: Best Pattern Ever # the properly formatted title -folder: best-pattern-ever # the folder name in which this pattern lies -permalink: /patterns/best-pattern-ever/ # the permalink to the pattern, to keep this uniform please stick to /patterns/FOLDER/ -description: # short meta description that shows in Google search results - -# both categories and tags are Yaml Lists -# you can either just pick one or write a list with '-'s -# usable categories and tags are listed here: https://github.com/iluwatar/java-design-patterns/blob/gh-pages/_config.yml -categories: creational # categories of the pattern -tags: # tags of the pattern - - best - - ever - - awesome --- +layout: pattern +title: Transaction script +folder: transaction-script +permalink: /patterns/transaction-script/ +categories: Domain logic +tags: + - Data access +--- + +## Intent +Transaction script(TS) is mainly used in small applications where nothing complex is done and bigger architecture's are not needed. ## Name / classification ... @@ -21,8 +17,6 @@ tags: # tags of the pattern ## Also known as ... -## Intent -... ## Explanation ... diff --git a/transaction-script/etc/transaction-script.urm.puml b/transaction-script/etc/transaction-script.urm.puml new file mode 100644 index 000000000..4a5bf02f8 --- /dev/null +++ b/transaction-script/etc/transaction-script.urm.puml @@ -0,0 +1,65 @@ +@startuml +package com.ashishtrivedi16.transaction-script { + class TransactionScriptApp { + - H2_DB_URL : String {static} + - LOGGER : Logger {static} + - addRooms(hotelDaoImpl : HotelDaoImpl) {static} + - createDataSource() : DataSource {static} + - createSchema(dataSource : DataSource) {static} + - deleteSchema(dataSource : DataSource) {static} + - getRoomsStatus(hotelDaoImpl : HotelDaoImpl) {static} + - generateSampleRooms() : List {static} + + main(args : String[]) {static} + } + class Room { + - id: Int + - roomType: String + - price: Int + - booked: Boolean + + Customer(id : int, roomType : String, price: Int, booked: Boolean) + + getId() : int + + getRoomType() : String + + getPrice() : Int + + isBooked() : Boolean + + setId(id : int) + + setRoomType(roomType : String) + + setPrice(price : Int) + + setBooked(booked : boolean) + + equals(that : Object) : boolean + + hashCode() : int + + toString() : String + } + interface HotelDao { + + add(Room) : boolean {abstract} + + delete(Room) : boolean {abstract} + + getAll() : Stream {abstract} + + getById(int) : Optional {abstract} + + update(Room) : boolean {abstract} + } + class RoomSchemaSql { + + CREATE_SCHEMA_SQL : String {static} + + DELETE_SCHEMA_SQL : String {static} + - RoomSchemaSql() + } + class HotelDaoImpl { + - dataSource : DataSource + + HotelDaoImpl(dataSource : DataSource) + + add(room : Room) : boolean + - createRoom(resultSet : ResultSet) : Room + + delete(room : Room) : boolean + + getAll() : Stream + + getById(id : int) : Optional + - getConnection() : Connection + - mutedClose(connection : Connection, statement : PreparedStatement, resultSet : ResultSet) + + update(room : Room) : boolean + } + class Hotel { + - LOGGER : Logger {static} + - hotelDao: HotelDaoImpl + + Hotel(hotelDao: HotelDaoImpl) + + bookRoom(roomNumber: Int) + + cancelRoomBooking(roomNumber: Int) + } +} +HotelDaoImpl ..|> HotelDao +@enduml 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 22f4bcdd6..13a19dd48 100644 --- a/transaction-script/src/main/java/com/ashishtrivedi16/transactionscript/TransactionScriptApp.java +++ b/transaction-script/src/main/java/com/ashishtrivedi16/transactionscript/TransactionScriptApp.java @@ -47,6 +47,7 @@ public class TransactionScriptApp { deleteSchema(dataSource); createSchema(dataSource); final var dao = new HotelDaoImpl(dataSource); + addRooms(dao); getRoomStatus(dao); @@ -97,7 +98,7 @@ public class TransactionScriptApp { * * @return h2 datasource */ - public static DataSource createDataSource() { + private static DataSource createDataSource() { JdbcDataSource dataSource = new JdbcDataSource(); dataSource.setUrl(H2_DB_URL); return dataSource; @@ -114,7 +115,7 @@ public class TransactionScriptApp { * * @return list of rooms */ - public static List generateSampleRooms() { + private static List generateSampleRooms() { 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); diff --git a/transaction-script/src/test/java/com/ashishtrivedi16/transactionscript/HotelDaoImplTest.java b/transaction-script/src/test/java/com/ashishtrivedi16/transactionscript/HotelDaoImplTest.java index 129b64c7e..6e907fdc0 100644 --- a/transaction-script/src/test/java/com/ashishtrivedi16/transactionscript/HotelDaoImplTest.java +++ b/transaction-script/src/test/java/com/ashishtrivedi16/transactionscript/HotelDaoImplTest.java @@ -261,7 +261,6 @@ public class HotelDaoImplTest { } } - /** * An arbitrary number which does not correspond to an active Room id. * 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 ca1d7718c..62aad9527 100644 --- a/transaction-script/src/test/java/com/ashishtrivedi16/transactionscript/HotelTest.java +++ b/transaction-script/src/test/java/com/ashishtrivedi16/transactionscript/HotelTest.java @@ -63,7 +63,7 @@ public class HotelTest { @Test() public void bookingRoomWithInvalidIdShouldRaiseException() { assertThrows(Exception.class, () -> { - hotel.bookRoom(999); + hotel.bookRoom(getNonExistingRoomId()); }); } @@ -91,7 +91,7 @@ public class HotelTest { @Test public void cancelRoomBookingWithInvalidIdShouldRaiseException() { assertThrows(Exception.class, () -> { - hotel.cancelRoomBooking(999); + hotel.cancelRoomBooking(getNonExistingRoomId()); }); } @@ -140,4 +140,13 @@ public class HotelTest { final var room6 = new Room(6, "Double", 80, false); return List.of(room1, room2, room3, room4, room5, room6); } + + /** + * An arbitrary number which does not correspond to an active Room id. + * + * @return an int of a room id which doesn't exist + */ + private int getNonExistingRoomId() { + return 999; + } }