Melissa O'Hearn : Hotel : Edges#34
Conversation
…heck-out time is before check-in
…tes and data types
…ests. added a test to make sure it's an instance of a Reservation Class
…st of stay, with corresponding tests in spec.rb
…ts for booking_system
HotelWhat We're Looking For
|
| module Hotel | ||
| class Blocks | ||
|
|
||
| # As an administrator, I can create a block of rooms |
There was a problem hiding this comment.
Since each instance of this class represents one block of rooms, we would typically use a singular name like Block.
| def make_reservation(cost_per_night, check_in, check_out) | ||
| available_rooms = list_available_rooms(Hotel::DateRange.new(check_in, check_out)) | ||
| if available_rooms.empty? | ||
| raise StandardError, "There's no room in the inn" |
There was a problem hiding this comment.
You create a DateRange here inline, then pass the check_in and check_out into the Reservation which creates another DateRange for the same dates. It might be a little cleaner if you made one instance and passed it into Reservation.new:
def make_reservation(cost_per_night, check_in, check_out)
date_range = Hotel::DateRange.new(check_in, check_out)
available_rooms = list_available_rooms(date_range)
# ...
reservation = Hotel::Reservation.new(room_number, cost_per_night, date_range)Of course, you would have to adjust your constructor for Reservation.
| # list reservations for a specific date range | ||
| def reservations_by_date_range(date_range) | ||
| res_by_date_range = @reservations.select do |res| | ||
| res.date_range.overlaps?(date_range) |
There was a problem hiding this comment.
I love that you broke this out as a separate method - it makes list_available_rooms much easier to read.
There was a problem hiding this comment.
This is also a great example of loosely coupled code - instead of having to do math on the individual dates stored in the reservation, this code is able to call another method (DateRange#overlap?) that knows how to do the work.
|
|
||
| # - Generate an reservation ID | ||
| def self.generate_id | ||
| rand_array = [] |
There was a problem hiding this comment.
This is rather a lot of work, and more importantly it's not guaranteed to give you a unique ID. Another way to approach this would be to assign numeric IDs sequentially, starting at 1 and counting up - this is what Rails does. Something like this:
@@next_id = 1
def assign_id
id = @@next_id
@@next_id += 1
return id
end|
|
||
| unless @check_out > @check_in | ||
| raise StandardError.new("Check-out can not be before check-in. Chech-out is: #{@check_out} check-in is #{@check_in}") | ||
| end |
There was a problem hiding this comment.
Instead of raising a StandardError, best practice is to define your own appropriately named exception type that inherits from StandardError and raise that. The reason is that StandardError is very broad, and if the code that calls this method has to rescue StandardError, that will catch things like ArgumentErrors and NameErrors that indicate true bugs.
| @rooms = | ||
| [ | ||
| {room_number: 1}, {room_number: 2}, {room_number: 3}, {room_number: 4}, {room_number: 5}, {room_number: 6}, {room_number: 7}, {room_number: 8}, {room_number: 9}, {room_number: 10}, {room_number: 11}, {room_number: 12}, {room_number: 13}, {room_number: 14}, {room_number: 15}, {room_number: 16}, {room_number: 17}, {room_number: 18}, {room_number: 19}, {room_number: 20} | ||
| ] |
There was a problem hiding this comment.
This data structure is a little more complex than it needs to be - I would probably keep a list of integers rather than making them all hashes. This ends up making some of your code below more complex as well.
| def overlaps?(date_range) | ||
| overlap = @check_in < date_range.check_out && date_range.check_in < @check_out | ||
| return overlap | ||
| end |
There was a problem hiding this comment.
Good work getting this tricky bit of logic figured out.
| # puts @booking.reservations.first.reservation_id | ||
| puts @booking.reservations.first.total_cost | ||
| puts @booking.reservations | ||
| expect(@booking.reservations.length).must_equal 1 |
There was a problem hiding this comment.
You might check that the length was 0 before making the reservation.
| it "raises an error if no rooms are available" do | ||
| 20.times do | ||
| @booking.make_reservation(200, "2018-02-03", "2018-02-06") | ||
| end |
There was a problem hiding this comment.
Good work getting this edge case!
| describe "initialize" do | ||
| before do | ||
| @dates = Hotel::DateRange.new("2018-02-03", "2018-02-06") | ||
| end |
There was a problem hiding this comment.
You've done a good job making sure your list of test cases is exhaustive. Many of these were provided in class, but there's still something to be said for diligently making sure you've gotten each one (and for having a design where it's straightforward to implement the tests)
Hotel
Congratulations! You're submitting your assignment!
Comprehension Questions