-
Notifications
You must be signed in to change notification settings - Fork 46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Katrina - Edges - Hotel #39
base: master
Are you sure you want to change the base?
Conversation
HotelWhat We're Looking For
|
lib/booking_system.rb
Outdated
|
||
def list_rooms | ||
return @rooms | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is doing the same thing as attr_reader :rooms
, just with a different name. I would say that the attr_reader
hits the project requirement, and you could omit this method.
|
||
def list_reservations(date_range) | ||
return @reservations.select { |res| res.date_range == date_range } | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good use of select
here.
lib/booking_system.rb
Outdated
return @rooms if @reservations.empty? | ||
unavailable_rooms = @reservations.map { |res| res.room_number if date_range.dates_overlap?(res.date_range) } | ||
|
||
unavailable_rooms_block = @block_rooms.map { |res| res.block_rooms if date_range.dates_overlap?(res.date_range)}.flatten |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code has some problems. On line 31, you'll end up with a bunch of nil
s in your array, since map
must always produce a value even if none of the code in the block executes.
On line 33, not only will you have nil
, but the elements in your array are more arrays, since that's what a BlockRoom
stores. That means that after line 35 has run, you'll have an array like:
[nil, 3, nil, nil, 5, nil, [8, 9, 10], nil]
A side effect of this is that your code does not respect blocks when making reservations!
You could use a combination of Array#flatten
and Array#compact
to fix it, or it could be that map
isn't a great choice for this problem.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do love the way this code flows though - logic problems aside, this is an excellent example of loose coupling. Rather than pulling the dates out of the reservation and doing a bunch of math here, you call a method and let the date range take care of it. That makes this code easier to read, and it means if something about how overlaps are calculated changes this code won't have to. Good work!
def find_room_in_block(date_range) | ||
found_block = find_block(date_range) | ||
room_block_rooms = found_block.first.block_rooms | ||
available_rooms = find_available_rooms(date_range) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if there's more than one block for a date range?
lib/date_range.rb
Outdated
|
||
raise StandardError, "Invalid dates entered" if @start_date == nil || @end_date == nil | ||
|
||
raise StandardError, "End date cannot be before start date" if (@end_date - @start_date) <= 0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 ArgumentError
s and NameError
s that indicate true bugs.
lib/date_range.rb
Outdated
if new_dates.first < booked_dates.first && new_dates.last > booked_dates.last | ||
return true | ||
# back end | ||
elsif new_dates.first < booked_dates.last && new_dates.last > booked_dates.last |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the way you've placed this logic on the DateRange
class - good division of responsibilities.
However, this check can be done in a single line! I challenge you to figure out how. Or you can go read the instructor implementation.
it 'Raises an error if there are no available rooms for the date range' do | ||
hotel_booked = Hotel::BookingSystem.new() | ||
|
||
20.times do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good work catching this error case!
describe 'dates_overlap? method' do | ||
before do | ||
@date_range = @reservation_1.reservations[0].date_range | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Though the list of these checks may originally have come from the instructors, you did the work of interpreting them and implementing them as tests. I appreciate your diligence.
Hotel
Congratulations! You're submitting your assignment!
Comprehension Questions