Skip to content
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.
35 changes: 35 additions & 0 deletions lib/hotel_block.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
require 'date'
require_relative 'room'
require_relative 'reservations'

module Hotel
class HotelBlock
attr_accessor :date_in, :date_out, :number_of_rooms, :rooms_block

@@increment_id = 1
def initialize(date_in:,date_out:,number_of_rooms:, rooms_block:[], discounted_room_rate:)
@id = @@increment_id
@@increment_id += 1
@date_in = date_in
@date_out = date_out
@number_of_rooms = number_of_rooms
# this range does not work, its an array of numbers
#raise argument error unless (2..5).include? number_of_rooms
@rooms_block =[]
end
Comment on lines +6 to +19

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I always get suspicious if a class ends up with just initialize. It usually means that the class in question is having its methods 'stolen' by other classes.



# def room_aside_for_date_range(start_date,end_date,room)
# range = date_in...date_out
# rooms_block.each do |room_in_block|
# if room_in_block == room
# if range.include?(date)
# return :UNAVAILABLE
# end
# end
# end
# else return :AVAILABLE
# end
Comment on lines +22 to +32

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Don't forget to remove dead code before submission! As long as you have committed the code you can get old methods like this back.


end
end
123 changes: 123 additions & 0 deletions lib/hotel_manager.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
require 'date'
require_relative 'room'
require_relative 'reservations'
require_relative 'hotel_block'


module Hotel
class HotelManager
attr_accessor :rooms, :blocks

def initialize(rooms:,blocks:)
@rooms = @Hotel::Room.list_of_rooms

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

When you are inside the scope of module Hotel you don't need to do Hotel::. The @Hotel ends up causing a bunch of warnings.

@blocks = []
end

def book_a_room(date_in,date_out,room)
if room.daterange_availability(date_in,date_out) == :AVAILABLE && (checks_room_availability_in_blocks(date_in,date_out,room) == :AVAILABLE || (get_block_overlap(date_in,date_out) == []))
reservation = Hotel::Reservations.new(date_in: date_in , date_out: date_out, room_id: room.id, room: room)
add_a_reservation_to_room(room,reservation)
else
raise ArgumentError, 'Room is UNAVAILABLE'
end
end

def add_a_reservation_to_room(room, reservation)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I would define this inside of Room, because managing a class's data makes the most sense as a responsibility of that class. Also, it removes bloat from your HotelManager.

room.reservations<< reservation
end

def list_reservations_on_specific_date(date)
list =[]
@rooms.each do |room|
unless room.specific_date_reservations(date) == []
room.specific_date_reservations(date).each do |reservation|
list<< reservation
end
end
end
return list
end

def list_available_rooms_on_specific_date(date)
list =[]
@rooms.each do |room|
if room.specific_date_availability(date) == []
list<< room
end
end
return list
end

def total_cost(reservation, rate= 200)
return reservation.total_number_of_nights_per_reservation * rate
end

def book_a_block(date_in,date_out,numberofrooms,rate)
block = Hotel::HotelBlock.new(date_in: date_in ,date_out: date_out, number_of_rooms: numberofrooms, rooms_block: [], discounted_room_rate: rate)
add_rooms_to_block(block,numberofrooms)
@blocks<< block
return block
end

def add_rooms_to_block(hotelblock,number_of_rooms_needed)
number_of_available_rooms = 0
rooms.each do |room|
if room.test_overlap(hotelblock.date_in,hotelblock.date_out) == :AVAILABLE
number_of_available_rooms +=1
end
end
if number_of_available_rooms < number_of_rooms_needed
raise ArgumentError, "Not enough room AVAILABILITY for this Block! Only #{number_of_available_rooms} AVAILABLE"
else
rooms_added = 0
rooms.each do |room|
if room.test_overlap(hotelblock.date_in,hotelblock.date_out) == :AVAILABLE
hotelblock.rooms_block<< room
rooms_added +=1
if rooms_added == number_of_rooms_needed
return
end
end
end
end
end
Comment on lines +62 to +83

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Again, this is probably the responsibility of HotelBlock, as it changes data inside the Block.


def get_block_overlap(start_date,end_date)
overlap_block = []
blocks.each do |block|
overlap1 = start_date >= block.date_in && start_date < block.date_out
overlap2 = end_date > block.date_in && end_date < block.date_out
overlap3 = start_date >= block.date_in && end_date < block.date_out
overlap4 = start_date <= block.date_in && end_date >= block.date_out
if (overlap1 == true) || (overlap2 == true) || (overlap3 == true) || (overlap4 == true)
overlap_block<< block
end
end
return overlap_block
end

def checks_room_availability_in_blocks(start_date,end_date,room)
get_block_overlap(start_date,end_date).each do |block|
block.rooms_block.each do |room_in_block|
if room_in_block == room
return :UNAVAILABLE
end
end
end
return :AVAILABLE
end

def book_a_room_in_block(hotelblock,date_in,date_out)
hotelblock.rooms_block.each do |room|
if room.daterange_availability(date_in,date_out) == :AVAILABLE || checks_room_availability_in_blocks(date_in,date_out,room) == :AVAILABLE || blocks == []
reservation = Hotel::Reservations.new(date_in: date_in , date_out: date_out, room_id: room.id, room: room)
add_a_reservation_to_room(room,reservation)
else
raise ArgumentError, 'No Available Rooms in Block!'
end
end
end

end
end

33 changes: 33 additions & 0 deletions lib/reservations.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require 'date'
require_relative 'room'
require_relative 'hotel_manager'

module Hotel
class Reservations

attr_accessor :id, :date_in, :date_out, :room_id

@@increment_id = 1
def initialize(date_in:, date_out:, room_id: , room: )
@id = @@increment_id
@@increment_id += 1
@date_in = date_in
@date_out = date_out
@room_id = room_id
@room = room

unless date_out >= date_in
raise ArgumentError, 'you cannot have date out come before date in'
end

if room.test_overlap(date_in,date_out) == :UNAVAILABLE
raise ArgumentError, 'there is an overlap'
end
end

def total_number_of_nights_per_reservation
return date_out - date_in
end

Comment on lines +7 to +31

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This class does a great job knowing what it's responsible for and having a good selection of methods for returning important info.

end
end
74 changes: 74 additions & 0 deletions lib/room.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
require_relative 'reservations'
require 'date'

module Hotel
class Room
attr_accessor :id, :reservations
def initialize(id:, reservations:)
@id = id
@reservations = []
end

def self.list_of_rooms
rooms = []
id = 1
20.times do
room = Hotel::Room.new(id: id, reservations: [])
rooms << room
id += 1
end
return rooms
end

def list_of_reservations_for_data_range(start_date,end_date)
return reservations.select do |reservation|
overlap1 = start_date >= reservation.date_in && start_date < reservation.date_out
overlap2 = end_date > reservation.date_in && end_date < reservation.date_out
overlap3 = start_date >= reservation.date_in && end_date < reservation.date_out
overlap4 = start_date <= reservation.date_in && end_date >= reservation.date_out

overlap1 || overlap2 || overlap3 || overlap4
Comment on lines +25 to +30

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

There is a simpler way of doing this date checking! Make a timeline, put an existing reservation on the timeline, and then see where you can draw lines that don't conflict!

end
end

def specific_date_reservations(date)
return reservations.select do |reservation|
range = reservation.date_in...reservation.date_out
range.include?(date)
end
end

def daterange_availability(date_in,date_out)
if list_of_reservations_for_data_range(date_in,date_out) == []
return :AVAILABLE
else
return :UNAVAILABLE
end
end

def specific_date_availability(date)
list = []
reservations.each do |reservation|
range = reservation.date_in...reservation.date_out
if range.include?(date)
list<< false
end
end
return list
end

def test_overlap(start_date,end_date)
reservations.each do |reservation|
overlap1 = start_date >= reservation.date_in && start_date < reservation.date_out
overlap2 = end_date > reservation.date_in && end_date < reservation.date_out
overlap3 = start_date >= reservation.date_in && end_date < reservation.date_out
overlap4 = start_date <= reservation.date_in && end_date >= reservation.date_out
if (overlap1 == true) || (overlap2 == true) || (overlap3 == true) || (overlap4 == true)
return :UNAVAILABLE
end
end
return :AVAILABLE
end

end
end
3 changes: 3 additions & 0 deletions refactors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
changes I would make
1. add a date range class to clean my hotel_manager class, and room class
2. make sure what I call date_in and date_out is consistent in all tests and methods
22 changes: 22 additions & 0 deletions test/hotel_block_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require_relative 'test_helper'
require 'date'


describe "HotelBlock class" do

describe "Initializer" do
before do
date_in1 = DateTime.new(2020,2,3.5)
date_out1 = DateTime.new(2020,2,5.5)
date_in2 = DateTime.new(2020,2,7.5)
date_out2 = DateTime.new(2020,2,14.5)
date_in3 = DateTime.new(2020,2,14.5)
date_out3 = DateTime.new(2020,2,16.5)
@block = Hotel::HotelBlock.new(date_in: date_in1,date_out: date_out1 ,number_of_rooms: (2..5), rooms_block:[],discounted_room_rate: 150)
end

it "is an instance of FrontDesk" do

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Test name?

expect(@block).must_be_kind_of Hotel::HotelBlock
end
end
end
Loading