Skip to content
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

quin - time #36

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
26 changes: 26 additions & 0 deletions lib/date_range.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module HotelSystem
class DateRange
attr_accessor :start_date, :end_date

def initialize(start_date, end_date)
@start_date = start_date
@end_date = end_date
end

def overlap?(other)
return false
end

def include?(date)
return false
end

def nights
return 3
end

end



end
Comment on lines +1 to +26
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like it's left over from the scaffolding.

In the future if you're not using code you should remove it.

163 changes: 163 additions & 0 deletions lib/hotel.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
require_relative 'room'
require_relative 'reservation'

module HotelSystem
class Hotel
attr_accessor :rooms

def initialize
@rooms = []
20.times do |i|
@rooms << HotelSystem::Room.new(i+1, 200)
end
end
#the block can only be the last 5 rooms 15-20
# def make_reservation_block(start_date, end_date, rooms_numbers, rate)
# rooms = []
# rooms_numbers.each do |num|
# if num > 20 || num < 15
# raise ArgumentError.new("you can only reserve rooms 15-20 for a block reservation")
# end
# rooms << find_room(num)
# end

# rooms.each do |room|
# room.cost_per_night = cost_with_discount(rate, room)
# make_reservation(room.room_number, start_date, end_date)
# end
# end

def find_reservations_with_room_number(room_number, start_date, end_date)
room = find_room(room_number)
list_reservations = []
room.reservations.each do |reservation|
if is_between_two_dates([start_date, end_date], reservation.start_date) || is_between_two_dates([start_date, end_date], reservation.end_date)
list_reservations << reservation
end
end
return list_reservations
end

def list_reservations_dates(start_date, end_date)
list_reservations = []
@rooms.each do |room|
room.reservations.each do |reservation|
if is_between_two_dates([start_date, end_date], reservation.start_date) || is_between_two_dates([start_date, end_date], reservation.end_date)
list_reservations << reservation
end
end
end
return list_reservations
end

def find_room(room_number)
@rooms.each do |room|
if room_number == room.room_number
return room
end
end
Comment on lines +54 to +58
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can simplify this using find:

Suggested change
@rooms.each do |room|
if room_number == room.room_number
return room
end
end
@rooms.find do |room|
room_number == room.room_number
end

end

def find_reservation(reservation_number)
@rooms.each do |room|
room.reservations.each do |reservation|
if reservation.reservation_number == reservation_number
return reservation
end
end
end
Comment on lines +62 to +68
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you're going to have reservations stored in Room you should have this functionality inside of a method there instead of here:

Suggested change
@rooms.each do |room|
room.reservations.each do |reservation|
if reservation.reservation_number == reservation_number
return reservation
end
end
end
@rooms.each do |room|
room.find_reservation(reservation_number)
end

That properly encapsulates this functionality and means that Hotel has to know less about Room.

end

def total_cost(reservation_number)
return find_reservation(reservation_number).total_cost
end

def available_rooms(start_date, end_date)
list_rooms = []
@rooms.each do |room|
if room.reservations.length == 0 || room.reservations == nil
list_rooms << room
elsif no_shared_days?(start_date, end_date, room.reservations.last) === true
list_rooms << room
end
end
return list_rooms
end


def make_reservation(room_number, start_date, end_date)
room = find_room(room_number)

true_for_all = []

if room.reservations.length == 0
room.reservations << HotelSystem::Reservation.new(next_reservation_number, start_date, end_date)
else
room.reservations.each do |res|
if no_shared_days?(start_date, end_date, res) === true
true_for_all << true
end
end
end

if true_for_all.length == room.reservations.length
room.reservations << HotelSystem::Reservation.new(next_reservation_number, start_date, end_date)
else
raise ArgumentError.new("you can't overlap reservations")
end
end


#there may be some cases where a reservation starts when one ends, but trust me it still works the method name isn't very accurate
def no_shared_days?(start_date, end_date, reservation)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Putting this sort of functionality inside of DateRange could have helped with cleaning up this code and with following the Single Responsibility Principle more closely.

count = 0
reservation_start = reservation.start_date
reservation_end = reservation.end_date
while start_date < end_date
while reservation_start < reservation_end
if start_date == reservation_start
count += 1
end
reservation_start = reservation_start + 1
end
reservation_start = reservation.start_date
start_date += 1
end
if (reservation.end_date - reservation.start_date == 1) && reservation_start == start_date
return false
else
return count < 2
end
Comment on lines +113 to +130
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This logic is very complex. Please comment the individual parts of something like this in the future.

end

private

#used for fine_reservations_for_room and list_reservations_with_date
def is_between_two_dates(range_wanted, date)
return date >= range_wanted[0] && date <= range_wanted[1];
end
Comment on lines +136 to +138
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method would have been cleaner if it compared two DateRange objects. (Instead of a two element array of Date objects with a single date.)


def next_reservation_number
max = 0
@rooms.each do |room|
room.reservations.each do |reservation|
if max < reservation.reservation_number
max = reservation.reservation_number
end

end
end
return max + 1
end
Comment on lines +140 to +151
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be simplified using max:

Suggested change
def next_reservation_number
max = 0
@rooms.each do |room|
room.reservations.each do |reservation|
if max < reservation.reservation_number
max = reservation.reservation_number
end
end
end
return max + 1
end
def next_reservation_number
max = @rooms.max do |room|
room.reservations.max do |reservation|
reservation.reservation_number
end
end
return max + 1
end


def cost_with_discount(rate, room)
return room.cost_per_night - (room.cost_per_night*(rate/100))
end
end
end


def main
end

main
Comment on lines +160 to +163
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove method stubs like this in the future.

26 changes: 26 additions & 0 deletions lib/reservation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require 'date'

module HotelSystem
class Reservation
attr_reader :start_date, :end_date, :total_cost
attr_accessor :reservation_number

def initialize(reservation_number, start_date, end_date)
@reservation_number = reservation_number
check_dates(start_date, end_date)
@start_date = start_date
@end_date = end_date
@total_cost = ((@end_date - @start_date)) * 200
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra parens:

Suggested change
@total_cost = ((@end_date - @start_date)) * 200
@total_cost = (@end_date - @start_date) * 200

end

def get_total_cost
return @total_cost
end

def check_dates(start_date, end_date)
if Date.today > start_date || Date.today > end_date
raise ArgumentError.new("Invalid date, you can't start or end a reservation in the past")
end
Comment on lines +21 to +23
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you're going to have a check like this please make sure that your example dates in your tests are in the future. (Either by placing them far in the future or making them relative to Date.today.)

Several of your tests fail because dates are now in the past.

end
end
end
26 changes: 26 additions & 0 deletions lib/room.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

require 'awesome_print'

module HotelSystem
class Room

attr_accessor :reservations,:room_number, :cost_per_night

def initialize(room_number, cost_per_night, reservations=[])
@room_number = room_number
@cost_per_night = cost_per_night
@reservations = []
end

def add_reservation(reservation)
@reservations << reservation
end

def list_reservations
return @reservations
end

end

end

17 changes: 17 additions & 0 deletions lib/roomblock.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module HotelSystem
class RoomBlock

attr_accessor :block

def initialize(block, discount_rate)
if @block.length > 5
raise ArgumentError.new("you can't reserve more than 5 rooms in a block")
end
@block = block #array or rooms
@cost_per_night = (@block.length * 200) - ((@block.length) * 200) * ((@discount_rate/100) )
end
end



end
Empty file added test/date_range_test.rb
Empty file.
Loading