Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
04b264f
Created and added DateRange class and tests, set up the environment v…
mheshmati-tech Mar 3, 2020
1c3207f
Added Reservation class, and wrote tests to instantiate the Reservati…
mheshmati-tech Mar 3, 2020
28a098d
Modified the attr_reader method for start_date and end_date so that i…
mheshmati-tech Mar 3, 2020
fc4f91f
Added the .duration method and wrote tests for DateRange class
mheshmati-tech Mar 3, 2020
9c7d241
created cost in Reservation and wrote tests
mheshmati-tech Mar 3, 2020
bde53d4
wrote duration method for DateRange and tests
mheshmati-tech Mar 3, 2020
651618f
added .all_dates to DateRange class and wrote tests for it- cleaned u…
mheshmati-tech Mar 3, 2020
f385e12
created and implemented some instance variables in the FrontDesk clas…
mheshmati-tech Mar 3, 2020
abad59a
made a room class, update some method within FrontDesk and Reservatio…
mheshmati-tech Mar 4, 2020
7af4c60
Finished wave 2- still no tests written. wrote add_reservation_to_roo…
mheshmati-tech Mar 5, 2020
6d1fcbc
finalized DateRange class and its tests, has two method for returning…
mheshmati-tech Mar 7, 2020
7d2e649
finished wave 2- room has a method to return the dates it's reserved …
mheshmati-tech Mar 7, 2020
120647c
finished wave 2 - reservation calculates the cost, has a helper metho…
mheshmati-tech Mar 7, 2020
ab3f7b1
finished wave 2- front desk methods include: making a reservation, se…
mheshmati-tech Mar 7, 2020
b5c50d0
added a new method to Room to check if room is available for reservat…
mheshmati-tech Mar 7, 2020
27acec1
used the is_available_during method from room to dry up the code in f…
mheshmati-tech Mar 7, 2020
4a29680
final refactoring- no major changes in method or testing
mheshmati-tech Mar 9, 2020
62a8ef6
added a text file for what needs to be refactored
mheshmati-tech Mar 9, 2020
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
23 changes: 23 additions & 0 deletions lib/date_range.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require "date"

module Hotel
class DateRange
attr_reader :start_date, :end_date

def initialize(start_date, end_date)
raise(ArgumentError, "The end date #{end_date} is smaller or same as the start date #{start_date}.") unless end_date > start_date
Copy link
Contributor

Choose a reason for hiding this comment

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

Great use of a guard clause!

@start_date = start_date
@end_date = end_date
end

#for future refactoring- does_match_date(date)
#this method will elaborate on the current method so it takes a date and returns a true or flase depending on if that date exists/overlaps all the days in the date range
def all_dates
return (@start_date...@end_date).to_a
end

def duration
return all_dates.length
end
end
end
45 changes: 45 additions & 0 deletions lib/front_desk.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
module Hotel
class FrontDesk
attr_reader :all_rooms, :all_reservations

def initialize
@all_rooms = []
20.times do |i|
@all_rooms << Hotel::Room.new("room #{i + 1}") #instantiate Room object
end

@all_reservations = []
end

def list_of_available_rooms(start_date, end_date)
return @all_rooms.select do |room|
room.is_available_during(start_date, end_date)
end
end

def reserve_room(start_date, end_date, guest_name:, room:)
raise(ArgumentError, "This room #{room} does not exist in our hotel:(") unless @all_rooms.any? do |individual_room|
individual_room.room_number == room
end

reservation_room = @all_rooms.find do |room_object|
room_object.room_number == room
end
raise(ArgumentError, "This room #{room} is unavailable for reservation for given dates #{start_date} - #{end_date}") unless reservation_room.is_available_during(start_date, end_date)

new_res = Hotel::Reservation.new(start_date, end_date, guest_name: guest_name, room: room)

reservation_room.add_reservation_to_room(new_res)
@all_reservations << new_res
return new_res
end

def list_of_reservations(date)
return @all_reservations.select do |reservation|
reservation.reservation_dates.all_dates.any? do |day|
date == day
end
end
end
end
end
19 changes: 19 additions & 0 deletions lib/reservation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module Hotel
class Reservation
attr_reader :room, :guest_name
def initialize(start_date, end_date, room:, guest_name:)
@start_date = start_date
@end_date = end_date
@room = room
@guest_name = guest_name.to_s
end

def reservation_dates
return Hotel::DateRange.new(@start_date, @end_date)
end

def cost
return reservation_dates.duration * 200
end
end
end
33 changes: 33 additions & 0 deletions lib/room.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
module Hotel
class Room
attr_reader :room_number, :room_reservations

def initialize(room_number)
@room_number = room_number
@room_reservations = []
end

def add_reservation_to_room(reservation)
raise(ArgumentError, "this reservation#{reservation} is not for #{@room_number}.") unless reservation.room == @room_number
@room_reservations << reservation
end

#refactoring
#combine dates_unavailable and is_available_during method so the method only outputs true or false depending on it's availability
def dates_unavailable
dates_booked = @room_reservations.map do |reservation|
reservation.reservation_dates.all_dates
end
return dates_booked.flatten
end

def is_available_during(start_date, end_date)
test_date_range = (start_date...end_date).to_a
if (dates_unavailable & test_date_range).length > 0
return false
else
return true
end
Comment on lines +26 to +30
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 just directly return the condition here (once you negate it), since it's a boolean value:

Suggested change
if (dates_unavailable & test_date_range).length > 0
return false
else
return true
end
return !((dates_unavailable & test_date_range).length > 0)

Or even better:

Suggested change
if (dates_unavailable & test_date_range).length > 0
return false
else
return true
end
return (dates_unavailable & test_date_range).length <= 0

end
end
end
13 changes: 13 additions & 0 deletions refactors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
change the method in date range to does_match(date) - this will return true or false depening if the date is included in the date range array
combine the dates_unavailable and is_available_during methods into one method
room probably doesn't need to keep track of it's reservations- only reservation should know about room since room doesn't change whereas reservation does

overall- keep methods that belong to a class within that class to iliminate the dependency between classes







write wave 3- make a block reservation class
63 changes: 63 additions & 0 deletions test/date_range_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
require_relative "test_helper"

describe "DateRange" do
before do
@start_date = Date.new(2020, 2, 2)
@end_date = Date.new(2020, 2, 5)
@date = Hotel::DateRange.new(@start_date, @end_date)
@all_dates = [Date.new(2020, 2, 2), Date.new(2020, 2, 3), Date.new(2020, 2, 4)]
end

describe "initialize" do
it "Should create an instance of DateRange when given a start_date and end_date" do
expect(@date).must_be_instance_of Hotel::DateRange
end
it "Should keep track of start and end date" do
expect(@date.start_date).must_equal @start_date
expect(@date.end_date).must_equal @end_date
end
it "Raises an ArgumentError if end_date is before start_date" do
start_date = Date.new(2020, 2, 2)
end_date = start_date - 3
expect { Hotel::DateRange.new(start_date, end_date) }.must_raise ArgumentError
end
it "Raises an ArgumentError for 0-length date range" do
start_date = Date.new(2020, 2, 2)
end_date = start_date
expect { Hotel::DateRange.new(start_date, end_date) }.must_raise ArgumentError
end
end

describe "duration" do
it "Should have the .duration method" do
expect(@date).must_respond_to :duration
end

it "Should calculate the correct amount of durations" do
expect(@date.duration).must_equal 3
end
end

describe "all_dates" do
it "Should have the .all_dates method" do
expect(@date).must_respond_to :all_dates
end

it "Should give an array of all dates" do
expect(@date.all_dates).must_equal @all_dates
expect(@date.all_dates).must_be_kind_of Array
end

it "Should include date that exists within date range" do
expect(@date.all_dates).must_include Date.new(2020, 2, 3)
end

it "Should not include the departure date" do
expect(@date.all_dates).wont_include @end_date
end

it "Should include the arrival date" do
expect(@date.all_dates).must_include @start_date
end
end
end
114 changes: 114 additions & 0 deletions test/front_desk_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
require_relative "test_helper"

describe "front_desk" do
before do
@pineapple_villa = Hotel::FrontDesk.new()

@start_date = Date.new(2020, 2, 2)
@end_date = @start_date + 3

@spongebob_reservation = @pineapple_villa.reserve_room(@start_date, @end_date, guest_name: "spongebob squarepants", room: "room 2")

@patrick_reservation = @pineapple_villa.reserve_room(@start_date, @end_date, guest_name: "patrick star", room: "room 1")

@room_1 = @pineapple_villa.all_rooms.find do |room_object|
room_object.room_number == "room 1"
end

@room_2 = @pineapple_villa.all_rooms.find do |room_object|
room_object.room_number == "room 2"
end

@list_of_reservation_for_given_date = @pineapple_villa.list_of_reservations(@start_date)
end

describe "initiate" do
it "Should be instantiated with no given Arguments" do
expect(@pineapple_villa).must_be_instance_of Hotel::FrontDesk
end
end

describe "instance variable" do
it "Should have all_reservations that is an array of all hotel reservations" do
expect(@pineapple_villa).must_respond_to :all_reservations
expect(@pineapple_villa.all_reservations).must_be_kind_of Array
@pineapple_villa.all_reservations.each do |reservation|
expect(reservation).must_be_instance_of Hotel::Reservation
end
end

it "Should have a list of all rooms" do
expect(@pineapple_villa).must_respond_to :all_rooms
expect(@pineapple_villa.all_rooms.length).must_equal 20
end

it "all_rooms should contain instances of Room object" do
@pineapple_villa.all_rooms.each do |room|
expect(room).must_be_instance_of Hotel::Room
end
end
end

describe "list_of_available_rooms(start_date, end_date)" do
it "Should return an array of all available rooms given that date range" do
start_date = Date.new(2020, 2, 14)
end_date = start_date + 1
expect(@pineapple_villa.list_of_available_rooms(start_date, end_date).length).must_equal 20
end

it "Should reject all the rooms reserved within the date range given" do
start_date = Date.new(2020, 2, 3)
end_date = Date.new(2020, 2, 5)
expect(@pineapple_villa.list_of_available_rooms(start_date, end_date)).wont_include @room_1
expect(@pineapple_villa.list_of_available_rooms(start_date, end_date)).wont_include @room_2
expect(@pineapple_villa.list_of_available_rooms(start_date, end_date).length).must_equal 18
end
end

describe "reserve_room" do
before do
end
Comment on lines +69 to +70
Copy link
Contributor

Choose a reason for hiding this comment

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

You don't need to include a before if you don't have anything to stick in it.

it "Should create an instance of Reservation when given start_date, end_date, guest_name, room " do
expect(@spongebob_reservation).must_be_kind_of Hotel::Reservation
expect(@pineapple_villa.all_reservations).must_include @spongebob_reservation
end

it "Should raise an ArgumentError when given a wrong room number" do
start_date = Date.new(2020, 2, 1)
end_date = Date.new(2020, 2, 3)
guest_name = "Sandy Cheeks"
room = "room 99"
expect { @pineapple_villa.reserve_room(start_date, end_date, guest_name: guest_name, room: room) }.must_raise ArgumentError
end

it "Should raise an ArgumentError if the date range is taken for that room" do
start_date = Date.new(2020, 2, 3)
end_date = start_date + 1
guest_name = "Sandy Cheeks"
room = "room 2"
expect { @pineapple_villa.reserve_room(start_date, end_date, guest_name: guest_name, room: room) }.must_raise ArgumentError
end

it "should add the reservation to the rooms reservation list" do
expect(@room_1.room_reservations).must_include @patrick_reservation
expect(@room_2.room_reservations).must_include @spongebob_reservation
end

it "the reservation list of a room must be for that room only" do
@room_1.room_reservations.each do |reservation|
expect(reservation.room).must_equal "room 1"
end
end

it "Should add the reservation to the Hotel's total reservations" do
expect(@pineapple_villa.all_reservations).must_include @spongebob_reservation
expect(@pineapple_villa.all_reservations).must_include @patrick_reservation
end
end

describe "list_of_reservations(date)" do
it "Should return a list of Reservations for that date" do
expect(@list_of_reservation_for_given_date).must_equal [@spongebob_reservation, @patrick_reservation]
end
end
end
49 changes: 49 additions & 0 deletions test/reservation_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
require_relative "test_helper"

describe "Reservation" do
before do
@start_date = Date.new(2020, 2, 2)
@end_date = @start_date + 3
@room = "room 3"
@guest_name = "Mair Heshmati"
@reserve = Hotel::Reservation.new(@start_date, @end_date, room: @room, guest_name: @guest_name)
end

describe "Initialize" do
it "should create an instance of Reservation" do
expect(@reserve).must_be_instance_of Hotel::Reservation
end

it "Takes in and keeps track of the start and end date, the desired room number and guest name" do
expect(@reserve.instance_variable_get(:@start_date)).must_equal @start_date
expect(@reserve.instance_variable_get(:@end_date)).must_equal @end_date
Comment on lines +18 to +19
Copy link
Contributor

Choose a reason for hiding this comment

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

In the future don't use things like instance_variable_get instead just put an attr_reader on the class.

#make sure the start and end date is being set as instance variables

expect(@reserve).must_respond_to :room
expect(@reserve.room).must_equal @room

expect(@reserve).must_respond_to :guest_name
expect(@reserve.guest_name).must_equal @guest_name
end
end
describe "reservation_dates" do
it "Should instantiate an instance of DateRange given start and end date" do
expect(@reserve).must_respond_to :reservation_dates
expect(@reserve.reservation_dates).must_be_instance_of Hotel::DateRange
end

it "Should have the correct arrival date" do
expect(@reserve.reservation_dates.start_date).must_equal @start_date
end

it "should have the correct departure date" do
expect(@reserve.reservation_dates.end_date).must_equal @end_date
end
end

describe "cost" do
it "Calculates the correct amount of cost for reservation duration" do
expect(@reserve.cost).must_equal 600
end
end
end
Loading