@@ -26,8 +26,10 @@ export class Room {
26
26
isFree ( arrival : Date , departure : Date ) {
27
27
const booked = this . bookings . find (
28
28
( booking ) =>
29
- ( arrival > booking . date . arrival && arrival < booking . date . departure ) ||
30
- ( departure > booking . date . arrival && departure < booking . date . departure )
29
+ ( arrival >= booking . date . arrival &&
30
+ arrival <= booking . date . departure ) ||
31
+ ( departure >= booking . date . arrival &&
32
+ departure <= booking . date . departure )
31
33
) ;
32
34
33
35
return booked ? false : true ;
@@ -40,7 +42,21 @@ export class Registry {
40
42
this . rooms = rooms ;
41
43
}
42
44
43
- write = ( ) => { } ;
45
+ write = (
46
+ clientId : string ,
47
+ roomName : string ,
48
+ date : { arrival : Date ; departure : Date }
49
+ ) => {
50
+ const roomIndex = this . rooms . findIndex ( ( room ) => room . name === roomName ) ;
51
+
52
+ // If no room with roomName
53
+ if ( roomIndex === - 1 ) {
54
+ return ;
55
+ }
56
+
57
+ this . rooms [ roomIndex ] . bookings . push ( new Booking ( clientId , date ) ) ;
58
+ } ;
59
+
44
60
read = ( filters ?: {
45
61
date ?: { arrival : Date ; departure : Date } ;
46
62
roomName ?: string ;
@@ -62,14 +78,9 @@ export class Registry {
62
78
) ;
63
79
return freeRooms ;
64
80
}
65
- } ;
66
81
67
- bookARoom = (
68
- clientId : string ,
69
- roomName : string ,
70
- arrivalDate : Date ,
71
- departureDate : Date
72
- ) => { } ;
82
+ return [ ] ;
83
+ } ;
73
84
}
74
85
75
86
export class Hotel {
@@ -79,28 +90,75 @@ export class Hotel {
79
90
this . name = name ;
80
91
this . registry = registry ;
81
92
}
93
+
94
+ bookARoom = (
95
+ clientId : string ,
96
+ roomName : string ,
97
+ date : { arrival : Date ; departure : Date }
98
+ ) : boolean => {
99
+ const room = this . registry . rooms . find ( ( room ) => room . name === roomName ) ;
100
+
101
+ // If room does not exist or is busy:
102
+ if ( ! room || ! room . isFree ( date . arrival , date . departure ) ) {
103
+ return false ;
104
+ }
105
+
106
+ this . registry . write ( clientId , roomName , date ) ;
107
+
108
+ return true ;
109
+ } ;
82
110
}
83
111
84
- /*
85
- // == USER STORY ==
112
+ // == USER STORIES ==
86
113
if ( require . main === module ) {
114
+ // == PREPARE ==
87
115
const rooms = [
88
116
new Room ( "Red room" ) ,
89
117
new Room ( "French room" ) ,
90
118
new Room ( "NSFW room" ) ,
91
119
] ;
92
- const hotel = new Hotel("Suniron Hotel", rooms);
120
+ const registry = new Registry ( rooms ) ;
121
+ const hotel = new Hotel ( "Suniron Hotel" , registry ) ;
122
+ // Add some bookings:
123
+ hotel . registry . rooms [ 0 ] . bookings = [
124
+ new Booking ( "mrWhite" , {
125
+ arrival : new Date ( "2020-12-12T14:00:00" ) ,
126
+ departure : new Date ( "2020-12-13T10:00:00" ) ,
127
+ } ) ,
128
+ new Booking ( "#mrBlanc" , {
129
+ arrival : new Date ( "2020-12-14T14:00:00" ) ,
130
+ departure : new Date ( "2020-12-16T10:00:00" ) ,
131
+ } ) ,
132
+ ] ;
133
+
134
+ // == STORY 1 ==
135
+ console . log ( "\n******************************" ) ;
93
136
console . log ( `** WELCOME TO ${ hotel . name . toLocaleUpperCase ( ) } **` ) ;
94
- console.log(" ==== STORY 1 ====");
95
- console.log("User: Hey, i want to see all free rooms.");
96
- console.log("Suniron: No problem, look this screen:");
137
+ console . log ( "******************************" ) ;
138
+ console . log ( "\n==== STORY 1 ====" ) ;
139
+ console . log ( "User: Hey, i want to see all free rooms!" ) ;
140
+ console . log (
141
+ "Receptionist: Ok, we got these:" ,
142
+ ( hotel . registry . read ( {
143
+ date : {
144
+ arrival : new Date ( "2020-12-12T15:00:00" ) ,
145
+ departure : new Date ( "2020-12-13T10:00:00" ) ,
146
+ } ,
147
+ } ) as Array < Room > )
148
+ . map ( ( freeRoom ) => freeRoom . name )
149
+ . join ( ", " ) + "."
150
+ ) ;
151
+
152
+ // == STORY 2 ==
153
+ console . log ( "\n==== STORY 2 ====" ) ;
154
+ console . log ( "User: I want to book French Room tonight, is it possible?" ) ;
97
155
console . log (
98
- hotel
99
- .getFreeRooms()
100
- .map((room) => room)
101
- .join("\n")
156
+ "Receptionist: Well, i'm booking that for you ;-). Please, wait..."
102
157
) ;
103
- console.log(" ==== STORY 2 ====");
104
- console.log("TODO...");
158
+ // Book the room:
159
+ hotel . bookARoom ( "Mr BLANC" , "French Room" , {
160
+ arrival : new Date ( Date . now ( ) ) ,
161
+ departure : new Date ( Date . now ( ) + 1 ) ,
162
+ } ) ;
163
+ console . log ( "Receptionist: ... OK! I give you the keys <8-)" ) ;
105
164
}
106
- */
0 commit comments