Conversation
…ager.rb; Passes all tests
…. All tests passing
… day. Fix resulting bugs
…f room has a reservation with the same date range as the given arguments
…es query's check_in date
…an array of available rooms, or a string in the event of no availabilities
…ting functionality to 'list_available_rooms'
… error if selected room is unavailable during the date range
…m that's unavailable
HotelWhat We're Looking For
|
| def initialize | ||
| @rooms = (1..20).map do |room_number| | ||
| Hotel::Room.new(room_number: room_number) | ||
| end |
There was a problem hiding this comment.
Since we're inside the Hotel module already, you don't need the Hotel:: here.
| available_room_list = list_available_rooms(check_in, check_out) | ||
|
|
||
| if available_room_list == [] | ||
| return available_room_list |
There was a problem hiding this comment.
I'm not sure that returning an empty array is the right approach here. You might consider either raising an exception or returning nil.
| available_room_list.each do |room| | ||
| available_room = room if room.room_number == room_selection | ||
| end | ||
| if available_room == "" |
There was a problem hiding this comment.
I think it might be possible to tighten up this code a little.
| @rooms.each do |room| | ||
| if room.is_available?(booking_range) | ||
| list << room | ||
| end |
There was a problem hiding this comment.
This code shows me that you've done a good job breaking up functionality between classes. You could imagine having Manager do a bunch of date math here, but instead it asks the room whether it's available. This is what Metz is talking about when she says you should "ask for what instead of telling how".
| def total_cost(check_in, check_out, cost_per_night) | ||
| number_of_nights = number_of_nights(check_in, check_out) | ||
| total_cost = cost_per_night * number_of_nights | ||
|
|
There was a problem hiding this comment.
I like the decision to make this a method rather than storing the cost as an instance variable. That way if the number of nights or the cost per night were to change (say in a future version of the program), the total cost will automatically be correct.
| @reservations.each do |reservation| | ||
| check_in = reservation.check_in | ||
| check_out = reservation.check_out | ||
|
|
There was a problem hiding this comment.
The .select enumerable might be helpful to clean up this code.
| list << reservation | ||
| end | ||
| end | ||
| end |
There was a problem hiding this comment.
This method has a bug! You do all this work in the each loop, but you don't return list at the end. So, you end up implicitly returning the result of .each, which is the original array.
$ pry -r ./lib/manager.rb
pry> m = Hotel::Manager.new
pry> m.reserve_room('2019-04-07', '2019-04-11')
pry> m.list_reservations_on('2020-01-01')
=> [#<Hotel::Reservation:0x00007feaa1922280
@check_in=#<Date: 2019-04-07 ((2458581j,0s,0n),+0s,2299161j)>,
@check_out=#<Date: 2019-04-11 ((2458585j,0s,0n),+0s,2299161j)>,
@id=1,
@room=#<Hotel::Room:0x00007feaa10dcd28>]There was a problem hiding this comment.
This is something that your tests should have caught.
|
|
||
| it "raises an ArgumentError if someone tries to reserve a specific room that's unavailable" do | ||
| expect { @manager.reserve_room("2019-3-20", "2019-3-21", room_selection: 1) }.must_raise ArgumentError | ||
| end |
There was a problem hiding this comment.
What if you try to reserve without a specific room, and the whole hotel is booked?
| describe "list_reservations_on method" do | ||
| it "can list reservations associated with a specific date" do | ||
| 5.times do | ||
| reservation = @manager.reserve_room("2019-3-20", "2019-3-25") |
There was a problem hiding this comment.
What about reservations that aren't associated with that date?
| it "returns false if the given date range STARTS on reservaton's check_in day and ENDS DURING" do | ||
| check_in = Date.parse(@check_in) | ||
| check_out = Date.parse("2020-03-22") | ||
| date_range = (check_in..check_out) |
There was a problem hiding this comment.
Good work laying out all of these different test cases.
Since you recreate them so frequently, it might make sense to store the instances of Date as instance variables in the before block as well. Then in the tests, you could do something like:
date_range = @check_in_date..(@check_out_date - 2)…ut dates in tests
…ze' method in Manager class
…reserve_room' for Manager class
Hotel
Congratulations! You're submitting your assignment!
Comprehension Questions