From 24eb5e5e4881792acc04ca465505c3048e1f63c1 Mon Sep 17 00:00:00 2001 From: Kay Date: Tue, 4 Sep 2018 13:46:01 -0700 Subject: [PATCH 01/46] guard is working and required in spec folder --- Guardfile | 2 +- spec/reservation_spec.rb | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 spec/reservation_spec.rb diff --git a/Guardfile b/Guardfile index 6760f9177..6cfa4fedd 100644 --- a/Guardfile +++ b/Guardfile @@ -1,4 +1,4 @@ -guard :minitest, bundler: false, rubygems: false do +guard :minitest, bundler: false, autorun: true, rubygems: false do # with Minitest::Spec watch(%r{^spec/(.*)_spec\.rb$}) watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" } diff --git a/spec/reservation_spec.rb b/spec/reservation_spec.rb new file mode 100644 index 000000000..e5db536d9 --- /dev/null +++ b/spec/reservation_spec.rb @@ -0,0 +1,9 @@ + +require_relative 'spec_helper' + +describe 'Reservation' do + it 'does not work' do + expect(false).must_equal true + end + +end From 3cb6812ec520079d6546ea55abb3778d02d8ca53 Mon Sep 17 00:00:00 2001 From: Kay Date: Tue, 4 Sep 2018 14:50:30 -0700 Subject: [PATCH 02/46] set up project files and specs for reservation, room, tracking_system classes --- lib/reservation.rb | 4 ++++ lib/room.rb | 4 ++++ lib/tracking_system.rb | 7 +++++++ spec/reservation_spec.rb | 33 ++++++++++++++++++++++++++++----- spec/room_spec.rb | 19 +++++++++++++++++++ spec/tracking_system_spec.rb | 36 ++++++++++++++++++++++++++++++++++++ 6 files changed, 98 insertions(+), 5 deletions(-) create mode 100644 lib/reservation.rb create mode 100644 lib/room.rb create mode 100644 lib/tracking_system.rb create mode 100644 spec/room_spec.rb create mode 100644 spec/tracking_system_spec.rb diff --git a/lib/reservation.rb b/lib/reservation.rb new file mode 100644 index 000000000..4c4bcbea5 --- /dev/null +++ b/lib/reservation.rb @@ -0,0 +1,4 @@ +class Reservation + def initialize + end +end diff --git a/lib/room.rb b/lib/room.rb new file mode 100644 index 000000000..d9f39dbce --- /dev/null +++ b/lib/room.rb @@ -0,0 +1,4 @@ +class Room + def initialize + end +end diff --git a/lib/tracking_system.rb b/lib/tracking_system.rb new file mode 100644 index 000000000..887faca8e --- /dev/null +++ b/lib/tracking_system.rb @@ -0,0 +1,7 @@ +class TrackingSystem + attr_reader :available_rooms + + def initialize + @available_rooms = [] + end +end diff --git a/spec/reservation_spec.rb b/spec/reservation_spec.rb index e5db536d9..eeddf5ce7 100644 --- a/spec/reservation_spec.rb +++ b/spec/reservation_spec.rb @@ -1,9 +1,32 @@ - require_relative 'spec_helper' +require_relative '../lib/reservation' + +# Wave 1 Tracking Reservations +# 1. access the list of all of the rooms in the hotel +# 2. reserve a room for a given date range +# 3. access the list of reservations for a specific date +# 4. get the total cost for a given reservation +# +# The hotel has 20 rooms, and they are numbered 1 through 20 +# Every room is identical, and a room always costs $200/night +# The last day of a reservation is the checkout day, so the guest should not be charged for that night +# For this wave, any room can be reserved at any time, and you don't need to check whether reservations conflict with each other (this will come in wave 2!) + +describe 'Reservation class' do + + describe "#initialize" do + before do + @reservation = Reservation.new + end + + it 'is an instance of Reservation' do + expect(@reservation).must_be_kind_of Reservation + end + -describe 'Reservation' do - it 'does not work' do - expect(false).must_equal true end - + + + + end diff --git a/spec/room_spec.rb b/spec/room_spec.rb new file mode 100644 index 000000000..f5b6512f0 --- /dev/null +++ b/spec/room_spec.rb @@ -0,0 +1,19 @@ +require_relative 'spec_helper' +require_relative '../lib/room' + +describe 'Room class' do + + describe "#initialize" do + before do + @room = Room.new + end + + it 'is an instance of Room' do + expect(@room).must_be_kind_of Room + end + end + + + + +end diff --git a/spec/tracking_system_spec.rb b/spec/tracking_system_spec.rb new file mode 100644 index 000000000..dbc8528b0 --- /dev/null +++ b/spec/tracking_system_spec.rb @@ -0,0 +1,36 @@ +require_relative 'spec_helper' +require_relative '../lib/tracking_system' + +# Wave 1 Tracking Reservations +# 1. access the list of all of the rooms in the hotel +# 2. reserve a room for a given date range +# 3. access the list of reservations for a specific date +# 4. get the total cost for a given reservation +# +# The hotel has 20 rooms, and they are numbered 1 through 20 +# Every room is identical, and a room always costs $200/night +# The last day of a reservation is the checkout day, so the guest should not be charged for that night +# For this wave, any room can be reserved at any time, and you don't need to check whether reservations conflict with each other (this will come in wave 2!) + + +describe 'TrackingSystem class' do + + describe "#initialize" do + before do + @tracker = TrackingSystem.new + end + + it 'is an instance of TrackingSystem' do + expect(@tracker).must_be_kind_of TrackingSystem + end + + it 'has a list of available rooms' do + expect(@tracker.available_rooms).must_be_instance_of Array + end + + end + + + + +end From 1c9fc082bffff81a0837982fb6e5f141744c7022 Mon Sep 17 00:00:00 2001 From: Kay Date: Tue, 4 Sep 2018 16:43:51 -0700 Subject: [PATCH 03/46] in the process of writing 'make_reservatoin' on Room class --- lib/reservation.rb | 10 +++++++++- lib/room.rb | 14 ++++++++++++-- lib/tracking_system.rb | 27 ++++++++++++++++++++++++++- spec/reservation_spec.rb | 31 +++++++++++++++++++------------ spec/room_spec.rb | 23 ++++++++++++++++------- spec/tracking_system_spec.rb | 35 +++++++++++++++++++++++++++-------- 6 files changed, 109 insertions(+), 31 deletions(-) diff --git a/lib/reservation.rb b/lib/reservation.rb index 4c4bcbea5..4230891df 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -1,4 +1,12 @@ class Reservation - def initialize + attr_reader :room_num, :checkin_time, :checkout_time, :price, :customer + #need to write a test for these + + def initialize(input) + @room_num = input[:room_num] + @checkin_time = input[:checkin_time] + @checkout_time = input[:checkout_time] + @price = input[:price] + @customer = input[:customer] end end diff --git a/lib/room.rb b/lib/room.rb index d9f39dbce..5d210ba9f 100644 --- a/lib/room.rb +++ b/lib/room.rb @@ -1,4 +1,14 @@ class Room - def initialize - end + attr_reader :room_num, :price, :availability + + def initialize(input) + @room_num = input[:room_num] + @price = input[:price] + @availability = input[:availability] + end end + + +# it "creates a new instance of Reservation" do +# expect(@room.reserve(checkin_time: Date.new(2018,8,1), checkout_time: Date.new(2018,8,25))).must_be_kind_of Reservation +# end diff --git a/lib/tracking_system.rb b/lib/tracking_system.rb index 887faca8e..d0d0208d3 100644 --- a/lib/tracking_system.rb +++ b/lib/tracking_system.rb @@ -1,7 +1,32 @@ +require_relative 'room' +require_relative 'reservation' + class TrackingSystem - attr_reader :available_rooms + attr_reader :available_rooms, :unavailable_rooms, :all_rooms def initialize @available_rooms = [] + @unavailable_rooms = [] + @all_rooms = [] #Admin can access the list of all of the rooms in the hotel, should i << avail and unavail? end + # + # def make_reservation(checkin_time: nil, checkout_time: nil) + # Reservation.new({room_num: assign_available_room, checkin_time:) + # end + end + +#reservation attributes +# @room_num = input[:room_num] +# @checkin_time = input[:checkin_time] +# @checkout_time = input[:checkout_time] +# @price = input[:price] +# @customer = input[:customer] + + +# The hotel has 20 rooms, and they are numbered 1 through 20 +# Every room is identical, and a room always costs $200/night +# The last day of a reservation is the checkout day, so the +# guest should not be charged for that night +# For this wave, any room can be reserved at any time, and +# you don't need to check whether reservations conflict with each other (this will come in wave 2!) diff --git a/spec/reservation_spec.rb b/spec/reservation_spec.rb index eeddf5ce7..872eb10be 100644 --- a/spec/reservation_spec.rb +++ b/spec/reservation_spec.rb @@ -1,32 +1,39 @@ require_relative 'spec_helper' require_relative '../lib/reservation' -# Wave 1 Tracking Reservations -# 1. access the list of all of the rooms in the hotel -# 2. reserve a room for a given date range -# 3. access the list of reservations for a specific date -# 4. get the total cost for a given reservation -# -# The hotel has 20 rooms, and they are numbered 1 through 20 -# Every room is identical, and a room always costs $200/night -# The last day of a reservation is the checkout day, so the guest should not be charged for that night -# For this wave, any room can be reserved at any time, and you don't need to check whether reservations conflict with each other (this will come in wave 2!) describe 'Reservation class' do describe "#initialize" do before do - @reservation = Reservation.new + @reservation = Reservation.new( + {room_num: 1, + checkin_time: Date.new(2018,8,1), + checkout_time: Date.new(2018,9,1), + price: 200.0, + customer: "Koehler" + }) + end it 'is an instance of Reservation' do expect(@reservation).must_be_kind_of Reservation end + it "is set up for specific attributes and data types" do + [:room_num, :checkin_time, :checkout_time, :price, :customer].each do |attribute| + expect(@reservation).must_respond_to attribute + end - end + expect(@reservation.room_num).must_be_kind_of Integer + expect(@reservation.checkin_time).must_be_kind_of Date + expect(@reservation.checkout_time).must_be_kind_of Date + expect(@reservation.price).must_be_kind_of Float + expect(@reservation.customer).must_be_kind_of String + end + end end diff --git a/spec/room_spec.rb b/spec/room_spec.rb index f5b6512f0..ae99b8811 100644 --- a/spec/room_spec.rb +++ b/spec/room_spec.rb @@ -5,15 +5,24 @@ describe "#initialize" do before do - @room = Room.new - end + @room = Room.new( + {room_num: 1, price: 200.0, availability: :available }) + end - it 'is an instance of Room' do - expect(@room).must_be_kind_of Room - end - end + it 'is an instance of Room' do + expect(@room).must_be_kind_of Room + end + it "is set up for specific attributes and data types" do + [:room_num, :price, :availability].each do |attribute| + expect(@room).must_respond_to attribute + end + expect(@room.room_num).must_be_kind_of Integer + expect(@room.price).must_be_kind_of Float + expect(@room.availability).must_be_kind_of Symbol + end + end -end +end #the Room class block diff --git a/spec/tracking_system_spec.rb b/spec/tracking_system_spec.rb index dbc8528b0..313cc8c03 100644 --- a/spec/tracking_system_spec.rb +++ b/spec/tracking_system_spec.rb @@ -1,5 +1,8 @@ require_relative 'spec_helper' require_relative '../lib/tracking_system' +require_relative '../lib/room' +require_relative '../lib/reservation' + # Wave 1 Tracking Reservations # 1. access the list of all of the rooms in the hotel @@ -7,11 +10,8 @@ # 3. access the list of reservations for a specific date # 4. get the total cost for a given reservation # -# The hotel has 20 rooms, and they are numbered 1 through 20 -# Every room is identical, and a room always costs $200/night -# The last day of a reservation is the checkout day, so the guest should not be charged for that night -# For this wave, any room can be reserved at any time, and you don't need to check whether reservations conflict with each other (this will come in wave 2!) - +#NOTES ON POSSIBLE TESTS TO ADD: +#making sure each instance variable in initialization is the correct class describe 'TrackingSystem class' do @@ -26,11 +26,30 @@ it 'has a list of available rooms' do expect(@tracker.available_rooms).must_be_instance_of Array - end + end - end + it 'has a list of unavailable rooms' do + expect(@tracker.unavailable_rooms).must_be_instance_of Array + end + + it 'has a list of all rooms' do + expect(@tracker.all_rooms).must_be_instance_of Array + end + end + #method for making a reservation + # Admin can reserve a room for a given date range + describe "#make_reservation" do + before do + @room = Room.new({room_num: 1, price: 200.0, availability: :available }) + end + it "creates a new instance of Reservation" do + expect(@room.make_reservation(checkin_time: Date.new(2018,8,1), checkout_time: Date.new(2018,8,25))).must_be_kind_of Reservation + end -end + # it "changes availability to :unavailable for a given date range" do + # end + end +end #end of class method From 267e85cc7f073714b966bf636ba27127fb1d90bc Mon Sep 17 00:00:00 2001 From: Kay Date: Tue, 4 Sep 2018 21:48:29 -0700 Subject: [PATCH 04/46] created add_rooms method to TrackingSystem class, and created make_reservatoin method in TrackingSystem class, tests passing but still need to refactor for more reasonable usability --- lib/reservation.rb | 18 ++++++++++++------ lib/room.rb | 14 ++++++++++---- lib/tracking_system.rb | 32 ++++++++++++++++++++++++-------- spec/room_spec.rb | 2 +- spec/tracking_system_spec.rb | 35 +++++++++++++++++++++++++---------- 5 files changed, 72 insertions(+), 29 deletions(-) diff --git a/lib/reservation.rb b/lib/reservation.rb index 4230891df..234de46db 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -2,11 +2,17 @@ class Reservation attr_reader :room_num, :checkin_time, :checkout_time, :price, :customer #need to write a test for these - def initialize(input) - @room_num = input[:room_num] - @checkin_time = input[:checkin_time] - @checkout_time = input[:checkout_time] - @price = input[:price] - @customer = input[:customer] + def initialize(attributes = {}) + @room_num = attributes[:room_num] + @checkin_time = attributes[:checkin_time] + @checkout_time = attributes[:checkout_time] + @price = attributes[:price] + @customer = attributes[:customer] end end + +#when a reservation is made a room number needs to be assigned + +#create a cost method +#def total_cost +# end diff --git a/lib/room.rb b/lib/room.rb index 5d210ba9f..ff1dbae5a 100644 --- a/lib/room.rb +++ b/lib/room.rb @@ -1,10 +1,10 @@ class Room attr_reader :room_num, :price, :availability - def initialize(input) - @room_num = input[:room_num] - @price = input[:price] - @availability = input[:availability] + def initialize(attributes = {}) + @room_num = attributes[:room_num] + @price = attributes[:price] + @availability = attributes[:availability] #reserved or not end end @@ -12,3 +12,9 @@ def initialize(input) # it "creates a new instance of Reservation" do # expect(@room.reserve(checkin_time: Date.new(2018,8,1), checkout_time: Date.new(2018,8,25))).must_be_kind_of Reservation # end + + +#how to return all attributes in a Hash +# # def attributes +# Hash[instance_variables.map { |name, _| [name.to_s.sub!(%r{@}, '').to_sym, instance_variable_get(name)] }] +# end diff --git a/lib/tracking_system.rb b/lib/tracking_system.rb index d0d0208d3..32663178f 100644 --- a/lib/tracking_system.rb +++ b/lib/tracking_system.rb @@ -1,21 +1,37 @@ require_relative 'room' require_relative 'reservation' +NUMBER_OF_ROOMS = 20 +STANDARD_ROOM_PRICE = 200.0 + class TrackingSystem - attr_reader :available_rooms, :unavailable_rooms, :all_rooms + attr_reader :all_rooms, :reservations def initialize - @available_rooms = [] - @unavailable_rooms = [] - @all_rooms = [] #Admin can access the list of all of the rooms in the hotel, should i << avail and unavail? + @all_rooms = add_rooms #should i add << avail and unavail? + @reservations = [] + end + + def add_rooms + all_rooms = [] + NUMBER_OF_ROOMS.times do |i| + i += 1 + i = Room.new({room_num: i, price: STANDARD_ROOM_PRICE, availability: :AVAILABLE}) + all_rooms << i + end + return all_rooms + end + + def make_reservation(checkin_time: nil, checkout_time: nil, customer: "") + Reservation.new(attributes = {room_num: 0, checkin_time: Date.new, checkout_time: Date.new, price: STANDARD_ROOM_PRICE, customer: ""}) end - # - # def make_reservation(checkin_time: nil, checkout_time: nil) - # Reservation.new({room_num: assign_available_room, checkin_time:) - # end + + end + + #reservation attributes # @room_num = input[:room_num] # @checkin_time = input[:checkin_time] diff --git a/spec/room_spec.rb b/spec/room_spec.rb index ae99b8811..12c534c9c 100644 --- a/spec/room_spec.rb +++ b/spec/room_spec.rb @@ -6,7 +6,7 @@ describe "#initialize" do before do @room = Room.new( - {room_num: 1, price: 200.0, availability: :available }) + {room_num: 1, price: 200.0, availability: :RESERVED }) end it 'is an instance of Room' do diff --git a/spec/tracking_system_spec.rb b/spec/tracking_system_spec.rb index 313cc8c03..e341a79fd 100644 --- a/spec/tracking_system_spec.rb +++ b/spec/tracking_system_spec.rb @@ -14,7 +14,7 @@ #making sure each instance variable in initialization is the correct class describe 'TrackingSystem class' do - +###### WAVE 1 ################################################################## describe "#initialize" do before do @tracker = TrackingSystem.new @@ -24,29 +24,44 @@ expect(@tracker).must_be_kind_of TrackingSystem end - it 'has a list of available rooms' do - expect(@tracker.available_rooms).must_be_instance_of Array + it 'has a list of all rooms' do + expect(@tracker.all_rooms).must_be_instance_of Array end - it 'has a list of unavailable rooms' do - expect(@tracker.unavailable_rooms).must_be_instance_of Array + it 'has a list of reservations' do + expect(@tracker.reservations).must_be_instance_of Array end - it 'has a list of all rooms' do + end + + describe "access the list of all 20 rooms in the hotel" do + before do + @tracker = TrackingSystem.new + end + + it 'must return an Array' do expect(@tracker.all_rooms).must_be_instance_of Array end + it 'must contain 20 elements' do + expect(@tracker.all_rooms.length).must_equal 20 + end + + it 'must contain only instances of Room' do + expect(@tracker.all_rooms.all? {|room| room.class == Room}).must_equal true + end end - #method for making a reservation - # Admin can reserve a room for a given date range + # #method for making a reservation + # # Admin can reserve a room for a given date range describe "#make_reservation" do before do - @room = Room.new({room_num: 1, price: 200.0, availability: :available }) + @tracker = TrackingSystem.new + # @room = Room.new({room_num: 1, price: 200.0, availability: :available }) end it "creates a new instance of Reservation" do - expect(@room.make_reservation(checkin_time: Date.new(2018,8,1), checkout_time: Date.new(2018,8,25))).must_be_kind_of Reservation + expect(@tracker.make_reservation(checkin_time: Date.new(2018,8,1), checkout_time: Date.new(2018,8,25))).must_be_kind_of Reservation end # it "changes availability to :unavailable for a given date range" do From 3ab0b97db8fec8342a0163162e73bce3c2676666 Mon Sep 17 00:00:00 2001 From: Kay Date: Tue, 4 Sep 2018 22:10:00 -0700 Subject: [PATCH 05/46] refactored #make_reservation method within TrackerSystem to add its return value to the @reservations list --- lib/tracking_system.rb | 3 ++- spec/tracking_system_spec.rb | 14 ++++++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/lib/tracking_system.rb b/lib/tracking_system.rb index 32663178f..d21f4556c 100644 --- a/lib/tracking_system.rb +++ b/lib/tracking_system.rb @@ -23,7 +23,8 @@ def add_rooms end def make_reservation(checkin_time: nil, checkout_time: nil, customer: "") - Reservation.new(attributes = {room_num: 0, checkin_time: Date.new, checkout_time: Date.new, price: STANDARD_ROOM_PRICE, customer: ""}) + reservation = Reservation.new(attributes = {room_num: 0, checkin_time: Date.new, checkout_time: Date.new, price: STANDARD_ROOM_PRICE, customer: ""}) + @reservations << reservation end diff --git a/spec/tracking_system_spec.rb b/spec/tracking_system_spec.rb index e341a79fd..4b32e11a2 100644 --- a/spec/tracking_system_spec.rb +++ b/spec/tracking_system_spec.rb @@ -61,10 +61,20 @@ end it "creates a new instance of Reservation" do - expect(@tracker.make_reservation(checkin_time: Date.new(2018,8,1), checkout_time: Date.new(2018,8,25))).must_be_kind_of Reservation + @reservation = @tracker.make_reservation(checkin_time: Date.new(2018,8,1), checkout_time: Date.new(2018,8,25)) + + expect(@reservation.sample).must_be_kind_of Reservation + end + + it "increases the number of reservations in the reservations list" do + num_of_reservations = @tracker.reservations.length + @reservation = @tracker.make_reservation(checkin_time: Date.new(2018,8,1), checkout_time: Date.new(2018,8,25)) + updated_num_of_reservations = @tracker.reservations.length + + expect(updated_num_of_reservations - num_of_reservations).must_equal 1 end - # it "changes availability to :unavailable for a given date range" do + # it "changes availability of the reserved room to :RESERVED for a given date range" do # end end end #end of class method From 1de166602231779cca0b122e3c4d726c05d91c33 Mon Sep 17 00:00:00 2001 From: Kay Date: Wed, 5 Sep 2018 16:21:31 -0700 Subject: [PATCH 06/46] modified make_reservation method in TrackingSystem, still working on refactoring this code and creating helper methods" --- lib/room.rb | 7 ++++--- lib/tracking_system.rb | 27 +++++++++++++++++++++++---- spec/reservation_spec.rb | 4 +--- spec/room_spec.rb | 8 +++++--- spec/tracking_system_spec.rb | 14 ++++++++++++++ 5 files changed, 47 insertions(+), 13 deletions(-) diff --git a/lib/room.rb b/lib/room.rb index ff1dbae5a..581316cd9 100644 --- a/lib/room.rb +++ b/lib/room.rb @@ -1,10 +1,11 @@ class Room - attr_reader :room_num, :price, :availability + attr_reader :room_num, :price, :reserved_dates, :block_status - def initialize(attributes = {}) + def initialize(attributes) @room_num = attributes[:room_num] @price = attributes[:price] - @availability = attributes[:availability] #reserved or not + @reserved_dates = attributes[:reserved_dates] + @block_status = :AVAILABLE end end diff --git a/lib/tracking_system.rb b/lib/tracking_system.rb index d21f4556c..0a7d38049 100644 --- a/lib/tracking_system.rb +++ b/lib/tracking_system.rb @@ -16,21 +16,40 @@ def add_rooms all_rooms = [] NUMBER_OF_ROOMS.times do |i| i += 1 - i = Room.new({room_num: i, price: STANDARD_ROOM_PRICE, availability: :AVAILABLE}) + i = Room.new({room_num: i, price: STANDARD_ROOM_PRICE, reserved_dates: []}) all_rooms << i end return all_rooms end - def make_reservation(checkin_time: nil, checkout_time: nil, customer: "") - reservation = Reservation.new(attributes = {room_num: 0, checkin_time: Date.new, checkout_time: Date.new, price: STANDARD_ROOM_PRICE, customer: ""}) + #access the list of all of the rooms in the hotel + def view_all_rooms + return @all_rooms + end + + # reserve a room for a given date range + def make_reservation(checkin_time: Date.new, checkout_time: Date.new) + # 1. pick a room that is available. the next available room? + @all_rooms.each do |room| + if room.reserved_dates.reduce + # go in @all_rooms and find the first room_num whos checkOUT_time is earlier than input's checkin_time(this is gonna work because i'll store peoples checout time internally as ending a day before) + # raise argument error if inside @all_rooms no room is available on this date range (then admin would need to input a new date range) + # else..make the new reservation below + reservation = Reservation.new(date_range: {checkin_time: Date.new, checkout_time: Date.new -1}, room: room) # <----room object contains {room_num:, price: STANDARD_ROOM_PRICE, customer: ""} @reservations << reservation + room.reserved_dates << {checkin_time: nil, checkout_time: nil} end + #access the list of reservations for a specific date + #get the total cost for a given reservation + private #helper methods below + def view_two_dates_as_range() + def check_if_rooms_available_on(date_range) + ############################# ############################# ############################# end - +#think about how each room can be reserved thru time #reservation attributes diff --git a/spec/reservation_spec.rb b/spec/reservation_spec.rb index 872eb10be..4c332a4cd 100644 --- a/spec/reservation_spec.rb +++ b/spec/reservation_spec.rb @@ -11,7 +11,6 @@ checkin_time: Date.new(2018,8,1), checkout_time: Date.new(2018,9,1), price: 200.0, - customer: "Koehler" }) end @@ -21,7 +20,7 @@ end it "is set up for specific attributes and data types" do - [:room_num, :checkin_time, :checkout_time, :price, :customer].each do |attribute| + [:room_num, :checkin_time, :checkout_time, :price].each do |attribute| expect(@reservation).must_respond_to attribute end @@ -29,7 +28,6 @@ expect(@reservation.checkin_time).must_be_kind_of Date expect(@reservation.checkout_time).must_be_kind_of Date expect(@reservation.price).must_be_kind_of Float - expect(@reservation.customer).must_be_kind_of String end diff --git a/spec/room_spec.rb b/spec/room_spec.rb index 12c534c9c..ca45ef6d0 100644 --- a/spec/room_spec.rb +++ b/spec/room_spec.rb @@ -6,7 +6,7 @@ describe "#initialize" do before do @room = Room.new( - {room_num: 1, price: 200.0, availability: :RESERVED }) + {room_num: 1, price: 200.0, reserved_dates: [], block_status: :AVAILABLE }) end it 'is an instance of Room' do @@ -14,13 +14,15 @@ end it "is set up for specific attributes and data types" do - [:room_num, :price, :availability].each do |attribute| + [:room_num, :price, :reserved_dates, :block_status].each do |attribute| expect(@room).must_respond_to attribute end expect(@room.room_num).must_be_kind_of Integer expect(@room.price).must_be_kind_of Float - expect(@room.availability).must_be_kind_of Symbol + expect(@room.reserved_dates).must_be_kind_of Array + expect(@room.block_status).must_be_kind_of Symbol + end end diff --git a/spec/tracking_system_spec.rb b/spec/tracking_system_spec.rb index 4b32e11a2..b5a7c430a 100644 --- a/spec/tracking_system_spec.rb +++ b/spec/tracking_system_spec.rb @@ -76,5 +76,19 @@ # it "changes availability of the reserved room to :RESERVED for a given date range" do # end + # it "saves the checkout_time as a day before " do + # end + end + + describe "#view_all_rooms" do + before do + @tracker = TrackingSystem.new + end + + it "returns a list" do + expect(@tracker.view_all_rooms).must_be_kind_of Array + end + end + end #end of class method From 4e4c735e9f85fa84125e1a68dbdca8126bc4e577 Mon Sep 17 00:00:00 2001 From: Kay Date: Wed, 5 Sep 2018 22:55:40 -0700 Subject: [PATCH 07/46] updated pseudocode for make_reservation class, need to write tests --- lib/reservation.rb | 11 ++++++----- lib/room.rb | 2 +- lib/tracking_system.rb | 11 +++++++---- spec/reservation_spec.rb | 3 +++ 4 files changed, 17 insertions(+), 10 deletions(-) diff --git a/lib/reservation.rb b/lib/reservation.rb index 234de46db..a64f80ebf 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -1,16 +1,17 @@ class Reservation - attr_reader :room_num, :checkin_time, :checkout_time, :price, :customer + attr_reader :room_num, :checkin_time, :checkout_time, :price, #need to write a test for these - def initialize(attributes = {}) - @room_num = attributes[:room_num] + def initialize(attributes) #attributes is a hash , so theoretically from CSV it would be converted into a hash + @room_num = attributes[:room_num] #the value of room_num could be an array later for rooms @checkin_time = attributes[:checkin_time] @checkout_time = attributes[:checkout_time] @price = attributes[:price] - @customer = attributes[:customer] end end - +reservation = Reservation.new +(date_range: {checkin_time: Date.new, checkout_time: Date.new -1}, room: room) # <----room object contains {room_num:, price: STANDARD_ROOM_PRICE, customer: ""} +# #when a reservation is made a room number needs to be assigned #create a cost method diff --git a/lib/room.rb b/lib/room.rb index 581316cd9..b5c08e3dc 100644 --- a/lib/room.rb +++ b/lib/room.rb @@ -4,7 +4,7 @@ class Room def initialize(attributes) @room_num = attributes[:room_num] @price = attributes[:price] - @reserved_dates = attributes[:reserved_dates] + @reserved_dates = [] #<--- this should be a list of date_ranges @block_status = :AVAILABLE end end diff --git a/lib/tracking_system.rb b/lib/tracking_system.rb index 0a7d38049..f1825221a 100644 --- a/lib/tracking_system.rb +++ b/lib/tracking_system.rb @@ -28,16 +28,19 @@ def view_all_rooms end # reserve a room for a given date range - def make_reservation(checkin_time: Date.new, checkout_time: Date.new) + def make_reservation(checkin_time: checkin, checkout_time: checkout) # 1. pick a room that is available. the next available room? @all_rooms.each do |room| - if room.reserved_dates.reduce + room.reserved_dates.each do |reserved_date| # this is a hash containing {checkin_time: checkin, checkout_time: checkout} + if !(reserved_date[checkin_time:]..reserved_date[checkout_time:]).include?(checkin_time: checkin) + + #you can use a range to see if a date is included in that range (Date..Datenow) # go in @all_rooms and find the first room_num whos checkOUT_time is earlier than input's checkin_time(this is gonna work because i'll store peoples checout time internally as ending a day before) # raise argument error if inside @all_rooms no room is available on this date range (then admin would need to input a new date range) # else..make the new reservation below - reservation = Reservation.new(date_range: {checkin_time: Date.new, checkout_time: Date.new -1}, room: room) # <----room object contains {room_num:, price: STANDARD_ROOM_PRICE, customer: ""} + reservation = Reservation.new(date_range: {checkin_time: checkin, checkout_time: checkout -1}, room: room) # <----room object contains {room_num:, price: STANDARD_ROOM_PRICE, customer: ""} @reservations << reservation - room.reserved_dates << {checkin_time: nil, checkout_time: nil} + room.reserved_dates << {checkin_time: checkin, checkout_time: checkout} end #access the list of reservations for a specific date diff --git a/spec/reservation_spec.rb b/spec/reservation_spec.rb index 4c332a4cd..5950ec8e1 100644 --- a/spec/reservation_spec.rb +++ b/spec/reservation_spec.rb @@ -29,6 +29,9 @@ expect(@reservation.checkout_time).must_be_kind_of Date expect(@reservation.price).must_be_kind_of Float end + # + # it "checkin_time must be before checkout_time" do + # end end From 8172d81c865576561cdad99c3262573a6ce3d574 Mon Sep 17 00:00:00 2001 From: Kay Date: Wed, 5 Sep 2018 23:44:36 -0700 Subject: [PATCH 08/46] created spec/test_data/ folder with test data --- lib/tracking_system.rb | 31 +++++++++++++++++++++------- spec/test_data/reservations_test.csv | 2 ++ spec/test_data/rooms_test.csv | 2 ++ 3 files changed, 27 insertions(+), 8 deletions(-) create mode 100644 spec/test_data/reservations_test.csv create mode 100644 spec/test_data/rooms_test.csv diff --git a/lib/tracking_system.rb b/lib/tracking_system.rb index f1825221a..e0601a887 100644 --- a/lib/tracking_system.rb +++ b/lib/tracking_system.rb @@ -31,23 +31,38 @@ def view_all_rooms def make_reservation(checkin_time: checkin, checkout_time: checkout) # 1. pick a room that is available. the next available room? @all_rooms.each do |room| - room.reserved_dates.each do |reserved_date| # this is a hash containing {checkin_time: checkin, checkout_time: checkout} + room.reserved_dates.each do |reserved_date| # this is a hash containing {checkin_time: checkin, checkout_time: checkout}, can replace some fo this with helper method if !(reserved_date[checkin_time:]..reserved_date[checkout_time:]).include?(checkin_time: checkin) - - #you can use a range to see if a date is included in that range (Date..Datenow) - # go in @all_rooms and find the first room_num whos checkOUT_time is earlier than input's checkin_time(this is gonna work because i'll store peoples checout time internally as ending a day before) - # raise argument error if inside @all_rooms no room is available on this date range (then admin would need to input a new date range) + #create a new reservation with |room| that is iterated in and fits the reqs + else + # raise argument error if inside @all_rooms no room is available on this date range (then admin would need to input a new date range) # else..make the new reservation below - reservation = Reservation.new(date_range: {checkin_time: checkin, checkout_time: checkout -1}, room: room) # <----room object contains {room_num:, price: STANDARD_ROOM_PRICE, customer: ""} + reservation = Reservation.new(date_range: view_two_dates_as_range {checkin_time: checkin, checkout_time: checkout -1}, room: room) # <----room object contains {room_num:, price: STANDARD_ROOM_PRICE, customer: ""} @reservations << reservation room.reserved_dates << {checkin_time: checkin, checkout_time: checkout} end #access the list of reservations for a specific date - #get the total cost for a given reservation + def view_reservations_on(date) + # list_of_res_on this date above = [] + #@reservations.each do |reservation| + #if (reservation.checkin_time..reservation.checkout_time).include?(date) + #then list_of_res_on (date) << reservation + #return the array list_of_res_on (date) + # end + + def view_available_rooms_on(date) + #available_rooms = [] + # @all_rooms.each do |room| + # room.reserved_dates.each do |date_range| <---date_range could be a hash like {checkin_time: checkin, checkout_time: checkout} + ### if the date_range doesn't include date aka: + # if !date_range.include?(date) + # available_rooms << room + # end + private #helper methods below - def view_two_dates_as_range() + def view_two_dates_as_range() #<--put params {checkin_time: checkin, checkout_time: checkout} def check_if_rooms_available_on(date_range) ############################# ############################# ############################# diff --git a/spec/test_data/reservations_test.csv b/spec/test_data/reservations_test.csv new file mode 100644 index 000000000..6ffb258ba --- /dev/null +++ b/spec/test_data/reservations_test.csv @@ -0,0 +1,2 @@ +room_num, checkin_time, checkout_time, price +1, 2018-09-05, 2018-09-13, 200 diff --git a/spec/test_data/rooms_test.csv b/spec/test_data/rooms_test.csv new file mode 100644 index 000000000..1291152b3 --- /dev/null +++ b/spec/test_data/rooms_test.csv @@ -0,0 +1,2 @@ +room_num, price, block_status +1, 200, AVAILABLE From fbd7297c9059753e001c8868244818ae2869ef47 Mon Sep 17 00:00:00 2001 From: Kay Date: Thu, 6 Sep 2018 14:39:42 -0700 Subject: [PATCH 09/46] updated Resevation attribute from @room_num to @rooms to accomodate multiple rooms in one reservation --- lib/reservation.rb | 8 +++++-- lib/room.rb | 2 +- lib/tracking_system.rb | 52 +++++++++++++++++++++++------------------- 3 files changed, 36 insertions(+), 26 deletions(-) diff --git a/lib/reservation.rb b/lib/reservation.rb index a64f80ebf..2762cf52e 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -1,9 +1,9 @@ class Reservation - attr_reader :room_num, :checkin_time, :checkout_time, :price, + attr_reader :rooms, :checkin_time, :checkout_time, :price, #need to write a test for these def initialize(attributes) #attributes is a hash , so theoretically from CSV it would be converted into a hash - @room_num = attributes[:room_num] #the value of room_num could be an array later for rooms + @rooms = [] #the value of room_num could be an array later for rooms @checkin_time = attributes[:checkin_time] @checkout_time = attributes[:checkout_time] @price = attributes[:price] @@ -12,6 +12,10 @@ def initialize(attributes) #attributes is a hash , so theoretically from CSV it reservation = Reservation.new (date_range: {checkin_time: Date.new, checkout_time: Date.new -1}, room: room) # <----room object contains {room_num:, price: STANDARD_ROOM_PRICE, customer: ""} # +# 1. get the total cost for a given reservation +# def total_cost + + #when a reservation is made a room number needs to be assigned #create a cost method diff --git a/lib/room.rb b/lib/room.rb index b5c08e3dc..515fa9a11 100644 --- a/lib/room.rb +++ b/lib/room.rb @@ -4,7 +4,7 @@ class Room def initialize(attributes) @room_num = attributes[:room_num] @price = attributes[:price] - @reserved_dates = [] #<--- this should be a list of date_ranges + @reserved_dates = [] #<--- this should be a list of date_range hashes? like {checkin_time: checkin, checkout_time: checkout} @block_status = :AVAILABLE end end diff --git a/lib/tracking_system.rb b/lib/tracking_system.rb index e0601a887..64c65cec2 100644 --- a/lib/tracking_system.rb +++ b/lib/tracking_system.rb @@ -27,12 +27,12 @@ def view_all_rooms return @all_rooms end - # reserve a room for a given date range - def make_reservation(checkin_time: checkin, checkout_time: checkout) + # reserve an available room for a given date range + def make_reservation(start_time: nil, end_time: nil, number_of_rooms) # 1. pick a room that is available. the next available room? @all_rooms.each do |room| - room.reserved_dates.each do |reserved_date| # this is a hash containing {checkin_time: checkin, checkout_time: checkout}, can replace some fo this with helper method - if !(reserved_date[checkin_time:]..reserved_date[checkout_time:]).include?(checkin_time: checkin) + room.reserved_dates.each do |reserved_dates| # this is a hash containing {checkin_time: checkin, checkout_time: checkout}, can replace some fo this with helper method + if !(reserved_dates[:checkin_time]..reserved_dates[:checkout_time]).include?(:start_time) && room.block_status == :NA #create a new reservation with |room| that is iterated in and fits the reqs else # raise argument error if inside @all_rooms no room is available on this date range (then admin would need to input a new date range) @@ -42,28 +42,34 @@ def make_reservation(checkin_time: checkin, checkout_time: checkout) room.reserved_dates << {checkin_time: checkin, checkout_time: checkout} end - #access the list of reservations for a specific date - def view_reservations_on(date) - # list_of_res_on this date above = [] - #@reservations.each do |reservation| - #if (reservation.checkin_time..reservation.checkout_time).include?(date) - #then list_of_res_on (date) << reservation - #return the array list_of_res_on (date) - # end + # view a list of rooms that are not reserved(aka available) for a given date range + # def view_available_rooms_on(checkin_time, checkout_time) + # #available_rooms = [] + # # @all_rooms.each do |room| + # # room.reserved_dates.each do |date_range| <---date_range could be a hash like {checkin_time: checkin, checkout_time: checkout} + # ### if the date_range doesn't include date aka: + # # if !date_range.include?(date) + # # available_rooms << room + # # end + # # return available_rooms (this is an array of rooms avail on 'date' passed as param) + # - def view_available_rooms_on(date) - #available_rooms = [] - # @all_rooms.each do |room| - # room.reserved_dates.each do |date_range| <---date_range could be a hash like {checkin_time: checkin, checkout_time: checkout} - ### if the date_range doesn't include date aka: - # if !date_range.include?(date) - # available_rooms << room - # end + # #access the list of reservations for a specific date + # def view_reservations_on(date) + # # list_of_res_on this date ^ above = []<--create emtpy array + # #@reservations.each do |reservation| + # #if (reservation.checkin_time..reservation.checkout_time).include?(date) + # #then list_of_res_on (date) << reservation + # #return the array list_of_res_on (date) + # # end + # +# - private #helper methods below - def view_two_dates_as_range() #<--put params {checkin_time: checkin, checkout_time: checkout} - def check_if_rooms_available_on(date_range) + # + # private #helper methods below + # def view_two_dates_as_range() #<--put params {checkin_time: checkin, checkout_time: checkout} + # def check_if_rooms_available_on(date_range) ############################# ############################# ############################# end From d6de8f4edbe128b44117cada31b9bf2d0e9424c4 Mon Sep 17 00:00:00 2001 From: Kay Date: Thu, 6 Sep 2018 14:42:10 -0700 Subject: [PATCH 10/46] deleted Room attribute @price because Reervation already has the price attribute --- spec/room_spec.rb | 5 ++--- spec/test_data/rooms_test.csv | 4 ++-- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/spec/room_spec.rb b/spec/room_spec.rb index ca45ef6d0..0fab454da 100644 --- a/spec/room_spec.rb +++ b/spec/room_spec.rb @@ -6,7 +6,7 @@ describe "#initialize" do before do @room = Room.new( - {room_num: 1, price: 200.0, reserved_dates: [], block_status: :AVAILABLE }) + {room_num: 1, reserved_dates: [], block_status: :NA }) end it 'is an instance of Room' do @@ -14,12 +14,11 @@ end it "is set up for specific attributes and data types" do - [:room_num, :price, :reserved_dates, :block_status].each do |attribute| + [:room_num, :reserved_dates, :block_status].each do |attribute| expect(@room).must_respond_to attribute end expect(@room.room_num).must_be_kind_of Integer - expect(@room.price).must_be_kind_of Float expect(@room.reserved_dates).must_be_kind_of Array expect(@room.block_status).must_be_kind_of Symbol diff --git a/spec/test_data/rooms_test.csv b/spec/test_data/rooms_test.csv index 1291152b3..e0a3184c4 100644 --- a/spec/test_data/rooms_test.csv +++ b/spec/test_data/rooms_test.csv @@ -1,2 +1,2 @@ -room_num, price, block_status -1, 200, AVAILABLE +room_num, block_status +1, AVAILABLE From 9f211edfadbee897d0b65cb4c3ac353438ce4727 Mon Sep 17 00:00:00 2001 From: Kay Date: Thu, 6 Sep 2018 16:48:16 -0700 Subject: [PATCH 11/46] fixed bug in Reservation instantiation,unwanted comma. 13 tests, 25 assertions, 2 failures(failures surrounding create reservation ) --- lib/reservation.rb | 10 ++++------ lib/room.rb | 5 ++--- lib/tracking_system.rb | 37 +++++++++++++++++++++--------------- spec/reservation_spec.rb | 18 +++++++----------- spec/room_spec.rb | 6 +++--- spec/tracking_system_spec.rb | 29 +++++++++++++++++++++++----- 6 files changed, 62 insertions(+), 43 deletions(-) diff --git a/lib/reservation.rb b/lib/reservation.rb index 2762cf52e..9fcc3bde7 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -1,17 +1,15 @@ class Reservation - attr_reader :rooms, :checkin_time, :checkout_time, :price, + attr_reader :rooms, :start_time, :end_time, :price #need to write a test for these def initialize(attributes) #attributes is a hash , so theoretically from CSV it would be converted into a hash @rooms = [] #the value of room_num could be an array later for rooms - @checkin_time = attributes[:checkin_time] - @checkout_time = attributes[:checkout_time] + @start_time = attributes[:start_time] + @end_time = attributes[:end_time] @price = attributes[:price] end end -reservation = Reservation.new -(date_range: {checkin_time: Date.new, checkout_time: Date.new -1}, room: room) # <----room object contains {room_num:, price: STANDARD_ROOM_PRICE, customer: ""} -# + # 1. get the total cost for a given reservation # def total_cost diff --git a/lib/room.rb b/lib/room.rb index 515fa9a11..237ddf5d8 100644 --- a/lib/room.rb +++ b/lib/room.rb @@ -1,11 +1,10 @@ class Room - attr_reader :room_num, :price, :reserved_dates, :block_status + attr_reader :room_num, :reserved_dates, :block def initialize(attributes) @room_num = attributes[:room_num] - @price = attributes[:price] @reserved_dates = [] #<--- this should be a list of date_range hashes? like {checkin_time: checkin, checkout_time: checkout} - @block_status = :AVAILABLE + @block = :NA #room should have a letter A~Z to indicate which rooms are with what block, NA is default end end diff --git a/lib/tracking_system.rb b/lib/tracking_system.rb index 64c65cec2..8ccbd4029 100644 --- a/lib/tracking_system.rb +++ b/lib/tracking_system.rb @@ -16,7 +16,7 @@ def add_rooms all_rooms = [] NUMBER_OF_ROOMS.times do |i| i += 1 - i = Room.new({room_num: i, price: STANDARD_ROOM_PRICE, reserved_dates: []}) + i = Room.new({room_num: i, reserved_dates: []}) all_rooms << i end return all_rooms @@ -28,22 +28,29 @@ def view_all_rooms end # reserve an available room for a given date range - def make_reservation(start_time: nil, end_time: nil, number_of_rooms) - # 1. pick a room that is available. the next available room? - @all_rooms.each do |room| - room.reserved_dates.each do |reserved_dates| # this is a hash containing {checkin_time: checkin, checkout_time: checkout}, can replace some fo this with helper method - if !(reserved_dates[:checkin_time]..reserved_dates[:checkout_time]).include?(:start_time) && room.block_status == :NA - #create a new reservation with |room| that is iterated in and fits the reqs - else - # raise argument error if inside @all_rooms no room is available on this date range (then admin would need to input a new date range) - # else..make the new reservation below - reservation = Reservation.new(date_range: view_two_dates_as_range {checkin_time: checkin, checkout_time: checkout -1}, room: room) # <----room object contains {room_num:, price: STANDARD_ROOM_PRICE, customer: ""} - @reservations << reservation - room.reserved_dates << {checkin_time: checkin, checkout_time: checkout} + # ****************************************************************************************** + def make_reservation(start_time: nil, end_time: nil, rooms: num) + # # 1. pick a room that is available. the next available room? + # @all_rooms.each do |room| + # room.reserved_dates.each do |reserved_dates| # this is a hash containing {checkin_time: checkin, checkout_time: checkout}, can replace some fo this with helper method + # if !(reserved_dates[:start_time]..reserved_dates[:end_time]).include?(:checkin) && room.block == :NA + # #create a new reservation with |room| that is iterated in and fits the reqs + # else + # # raise argument error if inside @all_rooms no room is available on this date range (then admin would need to input a new date range) + # # else..make the new reservation below + # reservation = Reservation.new(date_range: view_two_dates_as_range {start_time: checkin, end_time: checkout -1}, room: room) # <----room object contains {room_num:, price: STANDARD_ROOM_PRICE, customer: ""} + # @reservations << reservation + # room.reserved_dates << {start_time: checkin, end_time: checkout} end - +# ********************************************************************************************************* + #block method idea + #this method below is going to return a value that can into the Block.rooms << (aka list of rooms in a block), but how/when do i assign these available rooms a block id? Ah i think that i will + # i will create another method called create_block, call view_available_rooms_on(start_time, end_time) within that + # and then in that create_block method, update a block status for each room (aka block :A) corresponding to the Block's ID (id: A,B,C etc..) + # # view a list of rooms that are not reserved(aka available) for a given date range - # def view_available_rooms_on(checkin_time, checkout_time) + + # def view_available_rooms_on(start_time, end_time) # #available_rooms = [] # # @all_rooms.each do |room| # # room.reserved_dates.each do |date_range| <---date_range could be a hash like {checkin_time: checkin, checkout_time: checkout} diff --git a/spec/reservation_spec.rb b/spec/reservation_spec.rb index 5950ec8e1..2bb1dabe0 100644 --- a/spec/reservation_spec.rb +++ b/spec/reservation_spec.rb @@ -6,12 +6,8 @@ describe "#initialize" do before do - @reservation = Reservation.new( - {room_num: 1, - checkin_time: Date.new(2018,8,1), - checkout_time: Date.new(2018,9,1), - price: 200.0, - }) + attributes = {rooms:[],start_time: Date.new(2018,8,1),end_time: Date.new(2018,9,1),price: 200.0} + @reservation = Reservation.new(attributes) end @@ -20,16 +16,16 @@ end it "is set up for specific attributes and data types" do - [:room_num, :checkin_time, :checkout_time, :price].each do |attribute| + [:rooms, :start_time, :end_time, :price].each do |attribute| expect(@reservation).must_respond_to attribute end - expect(@reservation.room_num).must_be_kind_of Integer - expect(@reservation.checkin_time).must_be_kind_of Date - expect(@reservation.checkout_time).must_be_kind_of Date + expect(@reservation.rooms).must_be_kind_of Array + expect(@reservation.start_time).must_be_kind_of Date + expect(@reservation.end_time).must_be_kind_of Date expect(@reservation.price).must_be_kind_of Float end - # + # # it "checkin_time must be before checkout_time" do # end diff --git a/spec/room_spec.rb b/spec/room_spec.rb index 0fab454da..ec860eb64 100644 --- a/spec/room_spec.rb +++ b/spec/room_spec.rb @@ -6,7 +6,7 @@ describe "#initialize" do before do @room = Room.new( - {room_num: 1, reserved_dates: [], block_status: :NA }) + {room_num: 1, reserved_dates: [], block: :NA }) end it 'is an instance of Room' do @@ -14,13 +14,13 @@ end it "is set up for specific attributes and data types" do - [:room_num, :reserved_dates, :block_status].each do |attribute| + [:room_num, :reserved_dates, :block].each do |attribute| expect(@room).must_respond_to attribute end expect(@room.room_num).must_be_kind_of Integer expect(@room.reserved_dates).must_be_kind_of Array - expect(@room.block_status).must_be_kind_of Symbol + expect(@room.block).must_be_kind_of Symbol end end diff --git a/spec/tracking_system_spec.rb b/spec/tracking_system_spec.rb index b5a7c430a..549b3d497 100644 --- a/spec/tracking_system_spec.rb +++ b/spec/tracking_system_spec.rb @@ -57,29 +57,48 @@ describe "#make_reservation" do before do @tracker = TrackingSystem.new - # @room = Room.new({room_num: 1, price: 200.0, availability: :available }) + # @room = Room.new({room_num: 1, availability: :available }) end it "creates a new instance of Reservation" do - @reservation = @tracker.make_reservation(checkin_time: Date.new(2018,8,1), checkout_time: Date.new(2018,8,25)) + @reservation = @tracker.make_reservation(start_time: Date.new(2018,8,1), end_time: Date.new(2018,8,25), rooms: 1) #<---1 is the number of rooms - expect(@reservation.sample).must_be_kind_of Reservation + expect(@reservation).must_be_kind_of Reservation end it "increases the number of reservations in the reservations list" do num_of_reservations = @tracker.reservations.length - @reservation = @tracker.make_reservation(checkin_time: Date.new(2018,8,1), checkout_time: Date.new(2018,8,25)) + @reservation = @tracker.make_reservation(start_time: Date.new(2018,8,1), end_time: Date.new(2018,8,25), rooms: 1) updated_num_of_reservations = @tracker.reservations.length expect(updated_num_of_reservations - num_of_reservations).must_equal 1 end - # it "changes availability of the reserved room to :RESERVED for a given date range" do + # it "changes block_status of the reserved room to :RESERVED for a given date range" do # end # it "saves the checkout_time as a day before " do # end end + # def make_reservation(start_time: nil, end_time: nil, number_of_rooms) + # # 1. pick a room that is available. the next available room? + # @all_rooms.each do |room| + # room.reserved_dates.each do |reserved_dates| # this is a hash containing {checkin_time: checkin, checkout_time: checkout}, can replace some fo this with helper method + # if !(reserved_dates[:start_time]..reserved_dates[:end_time]).include?(:checkin) && room.block == :NA + # #create a new reservation with |room| that is iterated in and fits the reqs + # else + # # raise argument error if inside @all_rooms no room is available on this date range (then admin would need to input a new date range) + # # else..make the new reservation below + # reservation = Reservation.new(date_range: view_two_dates_as_range {start_time: checkin, end_time: checkout -1}, room: room) # <----room object contains {room_num:, price: STANDARD_ROOM_PRICE, customer: ""} + # @reservations << reservation + # room.reserved_dates << {start_time: checkin, end_time: checkout} + # end + + + + + + describe "#view_all_rooms" do before do @tracker = TrackingSystem.new From 052e51f5c90e3c72a72927de541190e64526f7c8 Mon Sep 17 00:00:00 2001 From: Kay Date: Fri, 7 Sep 2018 08:58:20 -0700 Subject: [PATCH 12/46] all tests passing right now, need to work on creating new tests for make_reservation method --- lib/tracking_system.rb | 30 ++++++++++++++++++------------ spec/tracking_system_spec.rb | 6 +++--- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/lib/tracking_system.rb b/lib/tracking_system.rb index 8ccbd4029..62f478f36 100644 --- a/lib/tracking_system.rb +++ b/lib/tracking_system.rb @@ -29,18 +29,24 @@ def view_all_rooms # reserve an available room for a given date range # ****************************************************************************************** - def make_reservation(start_time: nil, end_time: nil, rooms: num) - # # 1. pick a room that is available. the next available room? - # @all_rooms.each do |room| - # room.reserved_dates.each do |reserved_dates| # this is a hash containing {checkin_time: checkin, checkout_time: checkout}, can replace some fo this with helper method - # if !(reserved_dates[:start_time]..reserved_dates[:end_time]).include?(:checkin) && room.block == :NA - # #create a new reservation with |room| that is iterated in and fits the reqs - # else - # # raise argument error if inside @all_rooms no room is available on this date range (then admin would need to input a new date range) - # # else..make the new reservation below - # reservation = Reservation.new(date_range: view_two_dates_as_range {start_time: checkin, end_time: checkout -1}, room: room) # <----room object contains {room_num:, price: STANDARD_ROOM_PRICE, customer: ""} - # @reservations << reservation - # room.reserved_dates << {start_time: checkin, end_time: checkout} + def make_reservation(start_time, end_time, num) + @all_rooms.each do |room| + if room.reserved_dates.empty? + reservation = Reservation.new({rooms: [room.room_num], start_time: start_time, end_time: end_time, price: 200.0}) + @reservations << reservation + room.reserved_dates << {start_time: start_time, end_time: end_time} + else + room.reserved_dates.each do |reserved_dates| + if !(reserved_dates[:start_time]..reserved_dates[:end_time]).include?(start_time) #&& room.block == :NA + reservation = Reservation.new({rooms: [room], start_time: start_time, end_time: end_time, price: 200.0}) + @reservations << reservation + room.reserved_dates << {start_time: start_time, end_time: end_time} + else raise ArgumentError.new"There is no room available on that date, out of all 20 rooms" + end + end + end + return @reservations + end end # ********************************************************************************************************* #block method idea diff --git a/spec/tracking_system_spec.rb b/spec/tracking_system_spec.rb index 549b3d497..b00d7d909 100644 --- a/spec/tracking_system_spec.rb +++ b/spec/tracking_system_spec.rb @@ -61,14 +61,14 @@ end it "creates a new instance of Reservation" do - @reservation = @tracker.make_reservation(start_time: Date.new(2018,8,1), end_time: Date.new(2018,8,25), rooms: 1) #<---1 is the number of rooms + @reservation = @tracker.make_reservation(Date.new(2018,8,1), Date.new(2018,8,25), 1) #<---1 is the number of rooms - expect(@reservation).must_be_kind_of Reservation + expect(@reservation[0]).must_be_kind_of Reservation end it "increases the number of reservations in the reservations list" do num_of_reservations = @tracker.reservations.length - @reservation = @tracker.make_reservation(start_time: Date.new(2018,8,1), end_time: Date.new(2018,8,25), rooms: 1) + @reservation = @tracker.make_reservation(Date.new(2018,8,1),Date.new(2018,8,25),1) updated_num_of_reservations = @tracker.reservations.length expect(updated_num_of_reservations - num_of_reservations).must_equal 1 From 6f14c02fdc130ed43ced4575db8b38770d5b743d Mon Sep 17 00:00:00 2001 From: Kay Date: Fri, 7 Sep 2018 09:02:46 -0700 Subject: [PATCH 13/46] require simplecov and SimpleCov.start --- spec/spec_helper.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 4d1e3fdc8..6697142df 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,3 +1,5 @@ +require 'simplecov' +SimpleCov.start require 'minitest' require 'minitest/autorun' require 'minitest/reporters' From 1e4bee322b007b67e42185cd89665d7fa3d47b77 Mon Sep 17 00:00:00 2001 From: Kay Date: Fri, 7 Sep 2018 09:05:53 -0700 Subject: [PATCH 14/46] require files in lib in the spec_helper --- spec/spec_helper.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 6697142df..ec3dcda5d 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -8,3 +8,5 @@ Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new # Require_relative your lib files here! +require_relative '../lib/room.rb' +require_relative '../lib/reservation.rb' From 3a0501365d6963118413a82a1e06237d237f347c Mon Sep 17 00:00:00 2001 From: Kay Date: Fri, 7 Sep 2018 09:20:01 -0700 Subject: [PATCH 15/46] fixed bug in make_reservation method, now Reservation.rooms array collects the room number that is reserved --- lib/reservation.rb | 2 +- lib/tracking_system.rb | 2 +- spec/tracking_system_spec.rb | 29 ++++++++++++++++++++++++++--- 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/lib/reservation.rb b/lib/reservation.rb index 9fcc3bde7..77d7f6f22 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -3,7 +3,7 @@ class Reservation #need to write a test for these def initialize(attributes) #attributes is a hash , so theoretically from CSV it would be converted into a hash - @rooms = [] #the value of room_num could be an array later for rooms + @rooms = attributes[:rooms] #the value of room_num could be an array later for rooms @start_time = attributes[:start_time] @end_time = attributes[:end_time] @price = attributes[:price] diff --git a/lib/tracking_system.rb b/lib/tracking_system.rb index 62f478f36..eec60252e 100644 --- a/lib/tracking_system.rb +++ b/lib/tracking_system.rb @@ -45,7 +45,7 @@ def make_reservation(start_time, end_time, num) end end end - return @reservations + return reservation end end # ********************************************************************************************************* diff --git a/spec/tracking_system_spec.rb b/spec/tracking_system_spec.rb index b00d7d909..36560a2be 100644 --- a/spec/tracking_system_spec.rb +++ b/spec/tracking_system_spec.rb @@ -2,6 +2,7 @@ require_relative '../lib/tracking_system' require_relative '../lib/room' require_relative '../lib/reservation' +require 'pry' # Wave 1 Tracking Reservations @@ -14,7 +15,7 @@ #making sure each instance variable in initialization is the correct class describe 'TrackingSystem class' do -###### WAVE 1 ################################################################## + ###### WAVE 1 ################################################################## describe "#initialize" do before do @tracker = TrackingSystem.new @@ -62,8 +63,8 @@ it "creates a new instance of Reservation" do @reservation = @tracker.make_reservation(Date.new(2018,8,1), Date.new(2018,8,25), 1) #<---1 is the number of rooms - - expect(@reservation[0]).must_be_kind_of Reservation +# binding.pry + expect(@reservation).must_be_kind_of Reservation end it "increases the number of reservations in the reservations list" do @@ -78,6 +79,28 @@ # end # it "saves the checkout_time as a day before " do # end + # it "adds a hash of start/end time to each room.reserved_dates array" do + # end + + # it "only selects rooms that are available " do + # expect(@reservation.) + # end + # + # it "creates reservations for the number_of_rooms requested " do + # end + # + # it "iterates through @all_rooms to find the first available rooms" do + # end + # + # it "raises ArgumentError if inside @all_rooms no room is available on this date range" do + # end + # + # it "creates a new instance of Reservation" do + # @reservation = @tracker.make_reservation(start_time: Date.new(2018,8,1), end_time: Date.new(2018,8,25), rooms: 1) #<---1 is the number of rooms + # + # expect(@reservation).must_be_kind_of Reservation + # end + end # def make_reservation(start_time: nil, end_time: nil, number_of_rooms) From 7a7a95760826fec99160e9e27d1b9d06191f4068 Mon Sep 17 00:00:00 2001 From: Kay Date: Fri, 7 Sep 2018 12:07:34 -0700 Subject: [PATCH 16/46] added view_available_rooms_on method in TrackerSystem, and created two tests, all tests passing --- lib/tracking_system.rb | 112 +++++++++++-------- spec/tracking_system_spec.rb | 204 ++++++++++++++++++++--------------- 2 files changed, 183 insertions(+), 133 deletions(-) diff --git a/lib/tracking_system.rb b/lib/tracking_system.rb index eec60252e..d97609a9f 100644 --- a/lib/tracking_system.rb +++ b/lib/tracking_system.rb @@ -1,5 +1,6 @@ require_relative 'room' require_relative 'reservation' +require 'pry' NUMBER_OF_ROOMS = 20 STANDARD_ROOM_PRICE = 200.0 @@ -29,26 +30,40 @@ def view_all_rooms # reserve an available room for a given date range # ****************************************************************************************** + # def overlap?(x,y) + # (x.first - y.end) * (y.first - x.end) > 0 + # end + + def ranges_overlap?(r1, r2) + r1.include?(r2.first) || r2.include?(r1.first) + end + + # def reserve(start_time: start_time, end_time: end_time, num_of_rooms: num) + # + # end + def make_reservation(start_time, end_time, num) @all_rooms.each do |room| - if room.reserved_dates.empty? - reservation = Reservation.new({rooms: [room.room_num], start_time: start_time, end_time: end_time, price: 200.0}) - @reservations << reservation - room.reserved_dates << {start_time: start_time, end_time: end_time} - else - room.reserved_dates.each do |reserved_dates| - if !(reserved_dates[:start_time]..reserved_dates[:end_time]).include?(start_time) #&& room.block == :NA - reservation = Reservation.new({rooms: [room], start_time: start_time, end_time: end_time, price: 200.0}) - @reservations << reservation - room.reserved_dates << {start_time: start_time, end_time: end_time} - else raise ArgumentError.new"There is no room available on that date, out of all 20 rooms" + num.times do + if room.reserved_dates.empty? + reservation = Reservation.new({rooms: [room.room_num], start_time: start_time, end_time: end_time, price: 200.0}) + @reservations << reservation + room.reserved_dates << {start_time: start_time, end_time: end_time} + else + room.reserved_dates.each do |reserved_dates| + if !(reserved_dates[:start_time]...reserved_dates[:end_time]).include?(start_time) #&& room.block == :NA + reservation = Reservation.new({rooms: [room], start_time: start_time, end_time: end_time, price: 200.0}) + @reservations << reservation + room.reserved_dates << {start_time: start_time, end_time: end_time} + else raise ArgumentError.new"There is no room available on that date, out of all 20 rooms" + end end end + return reservation end - return reservation end end -# ********************************************************************************************************* + # ********************************************************************************************************* #block method idea #this method below is going to return a value that can into the Block.rooms << (aka list of rooms in a block), but how/when do i assign these available rooms a block id? Ah i think that i will # i will create another method called create_block, call view_available_rooms_on(start_time, end_time) within that @@ -56,45 +71,52 @@ def make_reservation(start_time, end_time, num) # # view a list of rooms that are not reserved(aka available) for a given date range - # def view_available_rooms_on(start_time, end_time) - # #available_rooms = [] - # # @all_rooms.each do |room| - # # room.reserved_dates.each do |date_range| <---date_range could be a hash like {checkin_time: checkin, checkout_time: checkout} - # ### if the date_range doesn't include date aka: - # # if !date_range.include?(date) - # # available_rooms << room - # # end - # # return available_rooms (this is an array of rooms avail on 'date' passed as param) - # + def view_available_rooms_on(start_time: start_time, end_time: end_time) + raise ArgumentError.new"start_time must be before end_time" unless start_time < end_time + available_rooms = [] + @all_rooms.each do |room| + room.reserved_dates.each do |dates_hash| #<---date_range could be a hash like {checkin_time: checkin, checkout_time: checkout} + if ranges_overlap?((dates_hash[:start_time]...dates_hash[:end_time]).to_a, (start_time..end_time).to_a) == false + available_rooms << room + end + binding.pry - # #access the list of reservations for a specific date - # def view_reservations_on(date) - # # list_of_res_on this date ^ above = []<--create emtpy array - # #@reservations.each do |reservation| - # #if (reservation.checkin_time..reservation.checkout_time).include?(date) - # #then list_of_res_on (date) << reservation - # #return the array list_of_res_on (date) - # # end - # + end + return available_rooms + end + end +end +# if !(dates_hash[:start_time]...dates_hash[:end_time]).include +# if !dates_hash.include?(date) +# # available_rooms << room +# # end +# # return available_rooms (this is an array of rooms avail on 'date' passed as param) +# def ranges_overlap?(r1, r2) +# r1.include?(r2.first) || r2.include?(r1.first) +# end +# +# #access the list of reservations for a specific date +# def view_reservations_on(date) +# # list_of_res_on this date ^ above = []<--create emtpy array +# #@reservations.each do |reservation| +# #if (reservation.checkin_time..reservation.checkout_time).include?(date) +# #then list_of_res_on (date) << reservation +# #return the array list_of_res_on (date) +# # end # - # - # private #helper methods below - # def view_two_dates_as_range() #<--put params {checkin_time: checkin, checkout_time: checkout} - # def check_if_rooms_available_on(date_range) +# - ############################# ############################# ############################# -end -#think about how each room can be reserved thru time +# +# private #helper methods below +# def view_two_dates_as_range() #<--put params {checkin_time: checkin, checkout_time: checkout} +# def check_if_rooms_available_on(date_range) +############################# ############################# ############################# + +#think about how each room can be reserved thru time -#reservation attributes -# @room_num = input[:room_num] -# @checkin_time = input[:checkin_time] -# @checkout_time = input[:checkout_time] -# @price = input[:price] -# @customer = input[:customer] # The hotel has 20 rooms, and they are numbered 1 through 20 diff --git a/spec/tracking_system_spec.rb b/spec/tracking_system_spec.rb index 36560a2be..b582021c9 100644 --- a/spec/tracking_system_spec.rb +++ b/spec/tracking_system_spec.rb @@ -19,118 +19,146 @@ describe "#initialize" do before do @tracker = TrackingSystem.new - end + # @tracker.all_rooms.each do |room| room.reserved_dates << {checkin_time: Date.new(2018,8,1), checkout_time: Date.new(2018,8,5)} + end - it 'is an instance of TrackingSystem' do - expect(@tracker).must_be_kind_of TrackingSystem - end + it 'is an instance of TrackingSystem' do + expect(@tracker).must_be_kind_of TrackingSystem + end - it 'has a list of all rooms' do - expect(@tracker.all_rooms).must_be_instance_of Array - end - - it 'has a list of reservations' do - expect(@tracker.reservations).must_be_instance_of Array - end + it 'has a list of all rooms' do + expect(@tracker.all_rooms).must_be_instance_of Array + end - end + it 'has a list of reservations' do + expect(@tracker.reservations).must_be_instance_of Array + end - describe "access the list of all 20 rooms in the hotel" do - before do - @tracker = TrackingSystem.new end - it 'must return an Array' do - expect(@tracker.all_rooms).must_be_instance_of Array - end + describe "access the list of all 20 rooms in the hotel" do + before do + @tracker = TrackingSystem.new + end - it 'must contain 20 elements' do - expect(@tracker.all_rooms.length).must_equal 20 - end + it 'must return an Array' do + expect(@tracker.all_rooms).must_be_instance_of Array + end - it 'must contain only instances of Room' do - expect(@tracker.all_rooms.all? {|room| room.class == Room}).must_equal true - end - end - - # #method for making a reservation - # # Admin can reserve a room for a given date range - describe "#make_reservation" do - before do - @tracker = TrackingSystem.new - # @room = Room.new({room_num: 1, availability: :available }) - end + it 'must contain 20 elements' do + expect(@tracker.all_rooms.length).must_equal 20 + end - it "creates a new instance of Reservation" do - @reservation = @tracker.make_reservation(Date.new(2018,8,1), Date.new(2018,8,25), 1) #<---1 is the number of rooms -# binding.pry - expect(@reservation).must_be_kind_of Reservation + it 'must contain only instances of Room' do + expect(@tracker.all_rooms.all? {|room| room.class == Room}).must_equal true + end end - it "increases the number of reservations in the reservations list" do - num_of_reservations = @tracker.reservations.length - @reservation = @tracker.make_reservation(Date.new(2018,8,1),Date.new(2018,8,25),1) - updated_num_of_reservations = @tracker.reservations.length + # #method for making a reservation + # # Admin can reserve a room for a given date range + describe "#make_reservation" do + before do + @tracker = TrackingSystem.new + # @room = Room.new({room_num: 1, availability: :available }) + end + + it "creates a new instance of Reservation" do + @reservation = @tracker.make_reservation(Date.new(2018,8,1), Date.new(2018,8,25), 1) #<---1 is the number of rooms + # binding.pry + expect(@reservation).must_be_kind_of Reservation + end + + # it "increases the number of reservations in the reservations list" do + # num_of_reservations = @tracker.reservations.length + # @reservation = @tracker.make_reservation(Date.new(2018,8,1),Date.new(2018,8,25),1) + # @reservation2 = @tracker.make_reservation(Date.new(2018,8,1),Date.new(2018,8,25),1) + # updated_num_of_reservations = @tracker.reservations.length + # binding.pry + # expect(updated_num_of_reservations - num_of_reservations).must_equal 1 + # end + + # it "changes block_status of the reserved room to :RESERVED for a given date range" do + # end + # it "saves the checkout_time as a day before " do + # end + # it "adds a hash of start/end time to each room.reserved_dates array" do + # end + + # it "only selects rooms that are available " do + # expect(@reservation.) + # end + # + # it "creates reservations for the number_of_rooms requested " do + # end + # + # it "iterates through @all_rooms to find the first available rooms" do + # end + # + # it "raises ArgumentError if inside @all_rooms no room is available on this date range" do + # end + # + # it "creates a new instance of Reservation" do + # @reservation = @tracker.make_reservation(start_time: Date.new(2018,8,1), end_time: Date.new(2018,8,25), rooms: 1) #<---1 is the number of rooms + # + # expect(@reservation).must_be_kind_of Reservation + # end - expect(updated_num_of_reservations - num_of_reservations).must_equal 1 end - # it "changes block_status of the reserved room to :RESERVED for a given date range" do - # end - # it "saves the checkout_time as a day before " do + # def make_reservation(start_time: nil, end_time: nil, number_of_rooms) + # # 1. pick a room that is available. the next available room? + # @all_rooms.each do |room| + # room.reserved_dates.each do |reserved_dates| # this is a hash containing {checkin_time: checkin, checkout_time: checkout}, can replace some fo this with helper method + # if !(reserved_dates[:start_time]..reserved_dates[:end_time]).include?(:checkin) && room.block == :NA + # #create a new reservation with |room| that is iterated in and fits the reqs + # else + # # raise argument error if inside @all_rooms no room is available on this date range (then admin would need to input a new date range) + # # else..make the new reservation below + # reservation = Reservation.new(date_range: view_two_dates_as_range {start_time: checkin, end_time: checkout -1}, room: room) # <----room object contains {room_num:, price: STANDARD_ROOM_PRICE, customer: ""} + # @reservations << reservation + # room.reserved_dates << {start_time: checkin, end_time: checkout} # end - # it "adds a hash of start/end time to each room.reserved_dates array" do - # end - - # it "only selects rooms that are available " do - # expect(@reservation.) - # end - # - # it "creates reservations for the number_of_rooms requested " do - # end - # - # it "iterates through @all_rooms to find the first available rooms" do - # end - # - # it "raises ArgumentError if inside @all_rooms no room is available on this date range" do - # end - # - # it "creates a new instance of Reservation" do - # @reservation = @tracker.make_reservation(start_time: Date.new(2018,8,1), end_time: Date.new(2018,8,25), rooms: 1) #<---1 is the number of rooms + # def view_available_rooms_on(start_time, end_time) + # #available_rooms = [] + # # @all_rooms.each do |room| + # # room.reserved_dates.each do |date_range| <---date_range could be a hash like {checkin_time: checkin, checkout_time: checkout} + # ### if the date_range doesn't include date aka: + # # if !date_range.include?(date) + # # available_rooms << room + # # end + # # return available_rooms (this is an array of rooms avail on 'date' passed as param) # - # expect(@reservation).must_be_kind_of Reservation - # end + describe "#view_available_rooms_on" do + before do + end - end + it "returns an array" do + @tracker = TrackingSystem.new + @available_rooms = @tracker.view_available_rooms_on(start_time: Date.new(2018,8,1),end_time:Date.new(2018,9,5)) + expect(@available_rooms).must_be_kind_of Array + end - # def make_reservation(start_time: nil, end_time: nil, number_of_rooms) - # # 1. pick a room that is available. the next available room? - # @all_rooms.each do |room| - # room.reserved_dates.each do |reserved_dates| # this is a hash containing {checkin_time: checkin, checkout_time: checkout}, can replace some fo this with helper method - # if !(reserved_dates[:start_time]..reserved_dates[:end_time]).include?(:checkin) && room.block == :NA - # #create a new reservation with |room| that is iterated in and fits the reqs - # else - # # raise argument error if inside @all_rooms no room is available on this date range (then admin would need to input a new date range) - # # else..make the new reservation below - # reservation = Reservation.new(date_range: view_two_dates_as_range {start_time: checkin, end_time: checkout -1}, room: room) # <----room object contains {room_num:, price: STANDARD_ROOM_PRICE, customer: ""} - # @reservations << reservation - # room.reserved_dates << {start_time: checkin, end_time: checkout} - # end + it "raises ArgumentError if start_time is > end_time" do + @tracker = TrackingSystem.new + expect{@tracker.view_available_rooms_on(start_time: Date.new(2018,10,5),end_time:Date.new(2018,9,5))}.must_raise ArgumentError + end + end - describe "#view_all_rooms" do - before do - @tracker = TrackingSystem.new - end - it "returns a list" do - expect(@tracker.view_all_rooms).must_be_kind_of Array - end + describe "#view_all_rooms" do + before do + @tracker = TrackingSystem.new + end - end + it "returns a list" do + expect(@tracker.view_all_rooms).must_be_kind_of Array + end + + end -end #end of class method + end #end of class method From ba3c5610335ed82019c2bfe08c3333ae83382eb6 Mon Sep 17 00:00:00 2001 From: Kay Date: Fri, 7 Sep 2018 13:10:25 -0700 Subject: [PATCH 17/46] fixed bug that was in view_available_rooms_on in TrackingSystem class. now the method returns an array of available rooms --- lib/room.rb | 2 +- lib/tracking_system.rb | 14 ++++++++------ spec/tracking_system_spec.rb | 3 ++- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/lib/room.rb b/lib/room.rb index 237ddf5d8..c1f48a8ce 100644 --- a/lib/room.rb +++ b/lib/room.rb @@ -3,7 +3,7 @@ class Room def initialize(attributes) @room_num = attributes[:room_num] - @reserved_dates = [] #<--- this should be a list of date_range hashes? like {checkin_time: checkin, checkout_time: checkout} + @reserved_dates = attributes[:reserved_dates] #<--- this should be a list of date_range hashes? like {checkin_time: checkin, checkout_time: checkout} @block = :NA #room should have a letter A~Z to indicate which rooms are with what block, NA is default end end diff --git a/lib/tracking_system.rb b/lib/tracking_system.rb index d97609a9f..a8ec70bab 100644 --- a/lib/tracking_system.rb +++ b/lib/tracking_system.rb @@ -75,15 +75,17 @@ def view_available_rooms_on(start_time: start_time, end_time: end_time) raise ArgumentError.new"start_time must be before end_time" unless start_time < end_time available_rooms = [] @all_rooms.each do |room| - room.reserved_dates.each do |dates_hash| #<---date_range could be a hash like {checkin_time: checkin, checkout_time: checkout} - if ranges_overlap?((dates_hash[:start_time]...dates_hash[:end_time]).to_a, (start_time..end_time).to_a) == false - available_rooms << room + if room.reserved_dates.empty? + available_rooms << room + else + room.reserved_dates.each do |dates_hash| #<---date_range could be a hash like {checkin_time: checkin, checkout_time: checkout} + if ranges_overlap?((dates_hash[:start_time]...dates_hash[:end_time]).to_a, (start_time..end_time).to_a) == false + available_rooms << room + end end - binding.pry - end - return available_rooms end + return available_rooms end end # if !(dates_hash[:start_time]...dates_hash[:end_time]).include diff --git a/spec/tracking_system_spec.rb b/spec/tracking_system_spec.rb index b582021c9..54ddd3e7d 100644 --- a/spec/tracking_system_spec.rb +++ b/spec/tracking_system_spec.rb @@ -128,7 +128,7 @@ # # end # # return available_rooms (this is an array of rooms avail on 'date' passed as param) # - describe "#view_available_rooms_on" do + describe "#view_available_rooms_on specifc date range" do before do end @@ -136,6 +136,7 @@ @tracker = TrackingSystem.new @available_rooms = @tracker.view_available_rooms_on(start_time: Date.new(2018,8,1),end_time:Date.new(2018,9,5)) expect(@available_rooms).must_be_kind_of Array + # binding.pry end it "raises ArgumentError if start_time is > end_time" do From de0cc70c244a6e41287b707c9815d8f12e47aeff Mon Sep 17 00:00:00 2001 From: Kay Date: Fri, 7 Sep 2018 13:29:38 -0700 Subject: [PATCH 18/46] created new tests for .view_available_rooms_on method, just one test failing, need to increase coverage --- spec/tracking_system_spec.rb | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/spec/tracking_system_spec.rb b/spec/tracking_system_spec.rb index 54ddd3e7d..c833670ce 100644 --- a/spec/tracking_system_spec.rb +++ b/spec/tracking_system_spec.rb @@ -136,7 +136,18 @@ @tracker = TrackingSystem.new @available_rooms = @tracker.view_available_rooms_on(start_time: Date.new(2018,8,1),end_time:Date.new(2018,9,5)) expect(@available_rooms).must_be_kind_of Array - # binding.pry + end + + it "returns an array containing instances of Room" do + @tracker = TrackingSystem.new + @available_rooms = @tracker.view_available_rooms_on(start_time: Date.new(2018,8,1),end_time:Date.new(2018,9,5)) + expect(@available_rooms[0]).must_be_kind_of Room + end + + it "raises ArgumentError if no rooms are available on these dates" do + @tracker = TrackingSystem.new + #need to create make_reservation and then call it 20 times on the same date then the 21st time it'll raise an error + expect{@tracker.view_available_rooms_on(start_time: Date.new(2018,1,1),end_time:Date.new(2018,1,2))}.must_raise ArgumentError end it "raises ArgumentError if start_time is > end_time" do From 4d5d1611037d4f5f9cb44fb953941e06c0694438 Mon Sep 17 00:00:00 2001 From: Kay Date: Fri, 7 Sep 2018 13:45:28 -0700 Subject: [PATCH 19/46] tests passing, in #view_available_rooms_on specific date range, argument error is raised appropriately --- lib/tracking_system.rb | 87 +++++++++++++++++++----------------- spec/tracking_system_spec.rb | 5 ++- 2 files changed, 51 insertions(+), 41 deletions(-) diff --git a/lib/tracking_system.rb b/lib/tracking_system.rb index a8ec70bab..3f5e390d0 100644 --- a/lib/tracking_system.rb +++ b/lib/tracking_system.rb @@ -74,6 +74,7 @@ def make_reservation(start_time, end_time, num) def view_available_rooms_on(start_time: start_time, end_time: end_time) raise ArgumentError.new"start_time must be before end_time" unless start_time < end_time available_rooms = [] + unavailable_count = 0 @all_rooms.each do |room| if room.reserved_dates.empty? available_rooms << room @@ -81,49 +82,55 @@ def view_available_rooms_on(start_time: start_time, end_time: end_time) room.reserved_dates.each do |dates_hash| #<---date_range could be a hash like {checkin_time: checkin, checkout_time: checkout} if ranges_overlap?((dates_hash[:start_time]...dates_hash[:end_time]).to_a, (start_time..end_time).to_a) == false available_rooms << room + else + unavailable_count += 1 end end end end - return available_rooms + if unavailable_count == all_rooms.length + raise ArgumentError.new"no rooms available on that date range" + else + return available_rooms + end end end -# if !(dates_hash[:start_time]...dates_hash[:end_time]).include -# if !dates_hash.include?(date) -# # available_rooms << room -# # end -# # return available_rooms (this is an array of rooms avail on 'date' passed as param) -# def ranges_overlap?(r1, r2) -# r1.include?(r2.first) || r2.include?(r1.first) -# end -# - -# #access the list of reservations for a specific date -# def view_reservations_on(date) -# # list_of_res_on this date ^ above = []<--create emtpy array -# #@reservations.each do |reservation| -# #if (reservation.checkin_time..reservation.checkout_time).include?(date) -# #then list_of_res_on (date) << reservation -# #return the array list_of_res_on (date) -# # end -# - -# - -# -# private #helper methods below -# def view_two_dates_as_range() #<--put params {checkin_time: checkin, checkout_time: checkout} -# def check_if_rooms_available_on(date_range) - -############################# ############################# ############################# - -#think about how each room can be reserved thru time - - - -# The hotel has 20 rooms, and they are numbered 1 through 20 -# Every room is identical, and a room always costs $200/night -# The last day of a reservation is the checkout day, so the -# guest should not be charged for that night -# For this wave, any room can be reserved at any time, and -# you don't need to check whether reservations conflict with each other (this will come in wave 2!) + # if !(dates_hash[:start_time]...dates_hash[:end_time]).include + # if !dates_hash.include?(date) + # # available_rooms << room + # # end + # # return available_rooms (this is an array of rooms avail on 'date' passed as param) + # def ranges_overlap?(r1, r2) + # r1.include?(r2.first) || r2.include?(r1.first) + # end + # + + # #access the list of reservations for a specific date + # def view_reservations_on(date) + # # list_of_res_on this date ^ above = []<--create emtpy array + # #@reservations.each do |reservation| + # #if (reservation.checkin_time..reservation.checkout_time).include?(date) + # #then list_of_res_on (date) << reservation + # #return the array list_of_res_on (date) + # # end + # + + # + + # + # private #helper methods below + # def view_two_dates_as_range() #<--put params {checkin_time: checkin, checkout_time: checkout} + # def check_if_rooms_available_on(date_range) + + ############################# ############################# ############################# + + #think about how each room can be reserved thru time + + + + # The hotel has 20 rooms, and they are numbered 1 through 20 + # Every room is identical, and a room always costs $200/night + # The last day of a reservation is the checkout day, so the + # guest should not be charged for that night + # For this wave, any room can be reserved at any time, and + # you don't need to check whether reservations conflict with each other (this will come in wave 2!) diff --git a/spec/tracking_system_spec.rb b/spec/tracking_system_spec.rb index c833670ce..a8af980c5 100644 --- a/spec/tracking_system_spec.rb +++ b/spec/tracking_system_spec.rb @@ -146,7 +146,10 @@ it "raises ArgumentError if no rooms are available on these dates" do @tracker = TrackingSystem.new - #need to create make_reservation and then call it 20 times on the same date then the 21st time it'll raise an error + @tracker.all_rooms.each do |room| + room.reserved_dates << {start_time: Date.new(2018,1,1), end_time: Date.new(2018,1,2)} + end + #need to create make_reservation and then call it 20 times on the same date then the 21st time it'll raise an error expect{@tracker.view_available_rooms_on(start_time: Date.new(2018,1,1),end_time:Date.new(2018,1,2))}.must_raise ArgumentError end From 0fac79efbba85d0061aa8b7f60f62177b7613910 Mon Sep 17 00:00:00 2001 From: Kay Date: Fri, 7 Sep 2018 13:54:34 -0700 Subject: [PATCH 20/46] made some small changes, cleaning up tracking_system.rb to get rid of unnecessary comments --- lib/tracking_system.rb | 46 +++++++++++++----------------------------- 1 file changed, 14 insertions(+), 32 deletions(-) diff --git a/lib/tracking_system.rb b/lib/tracking_system.rb index 3f5e390d0..5d4ea65a1 100644 --- a/lib/tracking_system.rb +++ b/lib/tracking_system.rb @@ -9,7 +9,7 @@ class TrackingSystem attr_reader :all_rooms, :reservations def initialize - @all_rooms = add_rooms #should i add << avail and unavail? + @all_rooms = add_rooms @reservations = [] end @@ -28,20 +28,7 @@ def view_all_rooms return @all_rooms end - # reserve an available room for a given date range - # ****************************************************************************************** - # def overlap?(x,y) - # (x.first - y.end) * (y.first - x.end) > 0 - # end - - def ranges_overlap?(r1, r2) - r1.include?(r2.first) || r2.include?(r1.first) - end - - # def reserve(start_time: start_time, end_time: end_time, num_of_rooms: num) - # - # end - + #reserve an available room for a given date range def make_reservation(start_time, end_time, num) @all_rooms.each do |room| num.times do @@ -88,24 +75,15 @@ def view_available_rooms_on(start_time: start_time, end_time: end_time) end end end - if unavailable_count == all_rooms.length + if unavailable_count == NUMBER_OF_ROOMS raise ArgumentError.new"no rooms available on that date range" else return available_rooms end end end - # if !(dates_hash[:start_time]...dates_hash[:end_time]).include - # if !dates_hash.include?(date) - # # available_rooms << room - # # end - # # return available_rooms (this is an array of rooms avail on 'date' passed as param) - # def ranges_overlap?(r1, r2) - # r1.include?(r2.first) || r2.include?(r1.first) - # end - # - # #access the list of reservations for a specific date +#access the list of reservations for a specific date # def view_reservations_on(date) # # list_of_res_on this date ^ above = []<--create emtpy array # #@reservations.each do |reservation| @@ -115,22 +93,26 @@ def view_available_rooms_on(start_time: start_time, end_time: end_time) # # end # - # - # + + ############################# ############################# ############################# # private #helper methods below # def view_two_dates_as_range() #<--put params {checkin_time: checkin, checkout_time: checkout} - # def check_if_rooms_available_on(date_range) - + # def check_if_rooms_available_on(date_range) <--or does the view_available_rooms_on() already do this pretty much? + def ranges_overlap?(r1, r2) + r1.include?(r2.first) || r2.include?(r1.first) + end ############################# ############################# ############################# #think about how each room can be reserved thru time - - # The hotel has 20 rooms, and they are numbered 1 through 20 # Every room is identical, and a room always costs $200/night # The last day of a reservation is the checkout day, so the # guest should not be charged for that night # For this wave, any room can be reserved at any time, and # you don't need to check whether reservations conflict with each other (this will come in wave 2!) + + # def overlap?(x,y) + # (x.first - y.end) * (y.first - x.end) > 0 + # end From e4a83e4f785579eae3a90536eb350a1b3c1901bf Mon Sep 17 00:00:00 2001 From: Kay Date: Fri, 7 Sep 2018 14:02:46 -0700 Subject: [PATCH 21/46] updated make_reservation method with corrected keyword arguments --- lib/tracking_system.rb | 7 ++++--- spec/tracking_system_spec.rb | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/tracking_system.rb b/lib/tracking_system.rb index 5d4ea65a1..a1243d47f 100644 --- a/lib/tracking_system.rb +++ b/lib/tracking_system.rb @@ -29,9 +29,9 @@ def view_all_rooms end #reserve an available room for a given date range - def make_reservation(start_time, end_time, num) + def make_reservation(start_time: Date.now, end_time: Date.now + 1, number_of_rooms: 1) @all_rooms.each do |room| - num.times do + number_of_rooms.times do if room.reserved_dates.empty? reservation = Reservation.new({rooms: [room.room_num], start_time: start_time, end_time: end_time, price: 200.0}) @reservations << reservation @@ -58,8 +58,9 @@ def make_reservation(start_time, end_time, num) # # view a list of rooms that are not reserved(aka available) for a given date range - def view_available_rooms_on(start_time: start_time, end_time: end_time) + def view_available_rooms_on(start_time: Date.now, end_time: Date.now + 1) raise ArgumentError.new"start_time must be before end_time" unless start_time < end_time + #another test to create is making sure its an instance of Date available_rooms = [] unavailable_count = 0 @all_rooms.each do |room| diff --git a/spec/tracking_system_spec.rb b/spec/tracking_system_spec.rb index a8af980c5..6080ad3fe 100644 --- a/spec/tracking_system_spec.rb +++ b/spec/tracking_system_spec.rb @@ -63,7 +63,7 @@ end it "creates a new instance of Reservation" do - @reservation = @tracker.make_reservation(Date.new(2018,8,1), Date.new(2018,8,25), 1) #<---1 is the number of rooms + @reservation = @tracker.make_reservation(start_time: Date.new(2018,8,1), end_time: Date.new(2018,8,25), number_of_rooms:1) #<---1 is the number of rooms # binding.pry expect(@reservation).must_be_kind_of Reservation end From deda7ecfc2747203ae1b17395ed7b457d284f49c Mon Sep 17 00:00:00 2001 From: Kay Date: Fri, 7 Sep 2018 14:26:11 -0700 Subject: [PATCH 22/46] updated Reservation.rooms to Reservation.room to be a kind of Integer rather than Array. Each reservation just stores one room --- lib/reservation.rb | 4 ++-- lib/tracking_system.rb | 8 +++++++- make_reservation_method.rb | 23 +++++++++++++++++++++++ spec/reservation_spec.rb | 6 +++--- 4 files changed, 35 insertions(+), 6 deletions(-) create mode 100644 make_reservation_method.rb diff --git a/lib/reservation.rb b/lib/reservation.rb index 77d7f6f22..298dd768a 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -1,9 +1,9 @@ class Reservation - attr_reader :rooms, :start_time, :end_time, :price + attr_reader :room, :start_time, :end_time, :price #need to write a test for these def initialize(attributes) #attributes is a hash , so theoretically from CSV it would be converted into a hash - @rooms = attributes[:rooms] #the value of room_num could be an array later for rooms + @room = attributes[:room] #the value of room should be an Integer @start_time = attributes[:start_time] @end_time = attributes[:end_time] @price = attributes[:price] diff --git a/lib/tracking_system.rb b/lib/tracking_system.rb index a1243d47f..7da2b9c57 100644 --- a/lib/tracking_system.rb +++ b/lib/tracking_system.rb @@ -30,6 +30,12 @@ def view_all_rooms #reserve an available room for a given date range def make_reservation(start_time: Date.now, end_time: Date.now + 1, number_of_rooms: 1) +# available_rooms = view_available_rooms_on(start_time, end_time) #<--returns an array of available rooms! +# raise ArgumentError.new"Not enough rooms available on those dates" if available_rooms.length < number_of_rooms +# number_of_rooms.times do |i| +# @reservations << Reservation.new({rooms: [available_rooms[i].room_num], start_time: start_time, end_time: end_time, price: 200.0}) +# #need to come back here and create i instances of reservation..but first need to change Reservation.rooms to hold an Integer instead of Array + # available_rooms.each do |room| @all_rooms.each do |room| number_of_rooms.times do if room.reserved_dates.empty? @@ -42,7 +48,7 @@ def make_reservation(start_time: Date.now, end_time: Date.now + 1, number_of_roo reservation = Reservation.new({rooms: [room], start_time: start_time, end_time: end_time, price: 200.0}) @reservations << reservation room.reserved_dates << {start_time: start_time, end_time: end_time} - else raise ArgumentError.new"There is no room available on that date, out of all 20 rooms" + else raise ArgumentError.new"There is no room available on that date" end end end diff --git a/make_reservation_method.rb b/make_reservation_method.rb new file mode 100644 index 000000000..f07b2b0a9 --- /dev/null +++ b/make_reservation_method.rb @@ -0,0 +1,23 @@ +#reserve an available room for a given date range +def make_reservation(start_time: Date.now, end_time: Date.now + 1, number_of_rooms: 1) + # view_available_rooms_on(start_time, end_time) + @all_rooms.each do |room| + number_of_rooms.times do + if room.reserved_dates.empty? + reservation = Reservation.new({rooms: [room.room_num], start_time: start_time, end_time: end_time, price: 200.0}) + @reservations << reservation + room.reserved_dates << {start_time: start_time, end_time: end_time} + else + room.reserved_dates.each do |reserved_dates| + if !(reserved_dates[:start_time]...reserved_dates[:end_time]).include?(start_time) #&& room.block == :NA + reservation = Reservation.new({rooms: [room], start_time: start_time, end_time: end_time, price: 200.0}) + @reservations << reservation + room.reserved_dates << {start_time: start_time, end_time: end_time} + else raise ArgumentError.new"There is no room available on that date" + end + end + end + return reservation + end + end +end diff --git a/spec/reservation_spec.rb b/spec/reservation_spec.rb index 2bb1dabe0..927ba9972 100644 --- a/spec/reservation_spec.rb +++ b/spec/reservation_spec.rb @@ -6,7 +6,7 @@ describe "#initialize" do before do - attributes = {rooms:[],start_time: Date.new(2018,8,1),end_time: Date.new(2018,9,1),price: 200.0} + attributes = {room: 1 ,start_time: Date.new(2018,8,1),end_time: Date.new(2018,9,1),price: 200.0} @reservation = Reservation.new(attributes) end @@ -16,11 +16,11 @@ end it "is set up for specific attributes and data types" do - [:rooms, :start_time, :end_time, :price].each do |attribute| + [:room, :start_time, :end_time, :price].each do |attribute| expect(@reservation).must_respond_to attribute end - expect(@reservation.rooms).must_be_kind_of Array + expect(@reservation.room).must_be_kind_of Integer expect(@reservation.start_time).must_be_kind_of Date expect(@reservation.end_time).must_be_kind_of Date expect(@reservation.price).must_be_kind_of Float From 2ba796f7c344602b7036d19949005adfc4a825f3 Mon Sep 17 00:00:00 2001 From: Kay Date: Fri, 7 Sep 2018 14:45:25 -0700 Subject: [PATCH 23/46] fixed bug in #add_reservation method, all tests passing, refactored this method as well --- lib/tracking_system.rb | 54 +++++++++++++++++++----------------- spec/tracking_system_spec.rb | 18 ++++++------ 2 files changed, 38 insertions(+), 34 deletions(-) diff --git a/lib/tracking_system.rb b/lib/tracking_system.rb index 7da2b9c57..6dc33f026 100644 --- a/lib/tracking_system.rb +++ b/lib/tracking_system.rb @@ -29,33 +29,37 @@ def view_all_rooms end #reserve an available room for a given date range - def make_reservation(start_time: Date.now, end_time: Date.now + 1, number_of_rooms: 1) -# available_rooms = view_available_rooms_on(start_time, end_time) #<--returns an array of available rooms! -# raise ArgumentError.new"Not enough rooms available on those dates" if available_rooms.length < number_of_rooms -# number_of_rooms.times do |i| -# @reservations << Reservation.new({rooms: [available_rooms[i].room_num], start_time: start_time, end_time: end_time, price: 200.0}) -# #need to come back here and create i instances of reservation..but first need to change Reservation.rooms to hold an Integer instead of Array - # available_rooms.each do |room| - @all_rooms.each do |room| - number_of_rooms.times do - if room.reserved_dates.empty? - reservation = Reservation.new({rooms: [room.room_num], start_time: start_time, end_time: end_time, price: 200.0}) - @reservations << reservation - room.reserved_dates << {start_time: start_time, end_time: end_time} - else - room.reserved_dates.each do |reserved_dates| - if !(reserved_dates[:start_time]...reserved_dates[:end_time]).include?(start_time) #&& room.block == :NA - reservation = Reservation.new({rooms: [room], start_time: start_time, end_time: end_time, price: 200.0}) - @reservations << reservation - room.reserved_dates << {start_time: start_time, end_time: end_time} - else raise ArgumentError.new"There is no room available on that date" - end - end - end - return reservation - end + def add_reservation(start_time: Date.now, end_time: Date.now + 1, number_of_rooms: 1) + available_rooms = view_available_rooms_on(start_time: start_time, end_time: end_time) #<--returns an array of available rooms! + raise ArgumentError.new"Not enough rooms available on those dates" if available_rooms.length < number_of_rooms + number_of_rooms.times do |i| + @reservations << Reservation.new({room: [available_rooms[i].room_num], start_time: start_time, end_time: end_time, price: 200.0}) + available_rooms[i].reserved_dates << {start_time: start_time, end_time: end_time} end + @reservations end +#need to come back here and create i instances of reservation..but first need to change Reservation.rooms to hold an Integer instead of Array + # available_rooms.each do |room| + # @all_rooms.each do |room| + # number_of_rooms.times do + # if room.reserved_dates.empty? + # reservation = Reservation.new({room: [room.room_num], start_time: start_time, end_time: end_time, price: 200.0}) + # @reservations << reservation + # room.reserved_dates << {start_time: start_time, end_time: end_time} + # else + # room.reserved_dates.each do |reserved_dates| + # if !(reserved_dates[:start_time]...reserved_dates[:end_time]).include?(start_time) #&& room.block == :NA + # reservation = Reservation.new({room: not array anymore[room], start_time: start_time, end_time: end_time, price: 200.0}) + # @reservations << reservation + # room.reserved_dates << {start_time: start_time, end_time: end_time} + # else raise ArgumentError.new"There is no room available on that date" + # end + # end + # end + # return reservation + # end + # end + # end # ********************************************************************************************************* #block method idea #this method below is going to return a value that can into the Block.rooms << (aka list of rooms in a block), but how/when do i assign these available rooms a block id? Ah i think that i will diff --git a/spec/tracking_system_spec.rb b/spec/tracking_system_spec.rb index 6080ad3fe..501e9295a 100644 --- a/spec/tracking_system_spec.rb +++ b/spec/tracking_system_spec.rb @@ -56,22 +56,22 @@ # #method for making a reservation # # Admin can reserve a room for a given date range - describe "#make_reservation" do + describe "#add_reservation" do before do @tracker = TrackingSystem.new # @room = Room.new({room_num: 1, availability: :available }) end it "creates a new instance of Reservation" do - @reservation = @tracker.make_reservation(start_time: Date.new(2018,8,1), end_time: Date.new(2018,8,25), number_of_rooms:1) #<---1 is the number of rooms + @reservation = @tracker.add_reservation(start_time: Date.new(2018,8,1), end_time: Date.new(2018,8,25), number_of_rooms:1) #<---1 is the number of rooms # binding.pry - expect(@reservation).must_be_kind_of Reservation + expect(@reservation).must_be_kind_of Array end # it "increases the number of reservations in the reservations list" do # num_of_reservations = @tracker.reservations.length - # @reservation = @tracker.make_reservation(Date.new(2018,8,1),Date.new(2018,8,25),1) - # @reservation2 = @tracker.make_reservation(Date.new(2018,8,1),Date.new(2018,8,25),1) + # @reservation = @tracker.add_reservation(Date.new(2018,8,1),Date.new(2018,8,25),1) + # @reservation2 = @tracker.add_reservation(Date.new(2018,8,1),Date.new(2018,8,25),1) # updated_num_of_reservations = @tracker.reservations.length # binding.pry # expect(updated_num_of_reservations - num_of_reservations).must_equal 1 @@ -98,14 +98,14 @@ # end # # it "creates a new instance of Reservation" do - # @reservation = @tracker.make_reservation(start_time: Date.new(2018,8,1), end_time: Date.new(2018,8,25), rooms: 1) #<---1 is the number of rooms + # @reservation = @tracker.add_reservation(start_time: Date.new(2018,8,1), end_time: Date.new(2018,8,25), rooms: 1) #<---1 is the number of rooms # # expect(@reservation).must_be_kind_of Reservation # end end - # def make_reservation(start_time: nil, end_time: nil, number_of_rooms) + # def add_reservation(start_time: nil, end_time: nil, number_of_rooms) # # 1. pick a room that is available. the next available room? # @all_rooms.each do |room| # room.reserved_dates.each do |reserved_dates| # this is a hash containing {checkin_time: checkin, checkout_time: checkout}, can replace some fo this with helper method @@ -113,7 +113,7 @@ # #create a new reservation with |room| that is iterated in and fits the reqs # else # # raise argument error if inside @all_rooms no room is available on this date range (then admin would need to input a new date range) - # # else..make the new reservation below + # # else..add the new reservation below # reservation = Reservation.new(date_range: view_two_dates_as_range {start_time: checkin, end_time: checkout -1}, room: room) # <----room object contains {room_num:, price: STANDARD_ROOM_PRICE, customer: ""} # @reservations << reservation # room.reserved_dates << {start_time: checkin, end_time: checkout} @@ -149,7 +149,7 @@ @tracker.all_rooms.each do |room| room.reserved_dates << {start_time: Date.new(2018,1,1), end_time: Date.new(2018,1,2)} end - #need to create make_reservation and then call it 20 times on the same date then the 21st time it'll raise an error + #need to create add_reservation and then call it 20 times on the same date then the 21st time it'll raise an error expect{@tracker.view_available_rooms_on(start_time: Date.new(2018,1,1),end_time:Date.new(2018,1,2))}.must_raise ArgumentError end From d9e2e519c51c6fdc4f5317cfa6988c60ac4337db Mon Sep 17 00:00:00 2001 From: Kay Date: Fri, 7 Sep 2018 15:30:05 -0700 Subject: [PATCH 24/46] wrote tests for #add_reservation, tests passing --- lib/tracking_system.rb | 3 +++ spec/tracking_system_spec.rb | 17 +++++++++++++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/lib/tracking_system.rb b/lib/tracking_system.rb index 6dc33f026..4cf927526 100644 --- a/lib/tracking_system.rb +++ b/lib/tracking_system.rb @@ -13,6 +13,9 @@ def initialize @reservations = [] end + # def reservation_total_cost + # end + def add_rooms all_rooms = [] NUMBER_OF_ROOMS.times do |i| diff --git a/spec/tracking_system_spec.rb b/spec/tracking_system_spec.rb index 501e9295a..ab1cc991f 100644 --- a/spec/tracking_system_spec.rb +++ b/spec/tracking_system_spec.rb @@ -62,12 +62,25 @@ # @room = Room.new({room_num: 1, availability: :available }) end - it "creates a new instance of Reservation" do - @reservation = @tracker.add_reservation(start_time: Date.new(2018,8,1), end_time: Date.new(2018,8,25), number_of_rooms:1) #<---1 is the number of rooms + it "returns an array of reservations" do + @reservation = @tracker.add_reservation(start_time: Date.new(2018,8,1), end_time: Date.new(2018,8,25), number_of_rooms:1) # binding.pry expect(@reservation).must_be_kind_of Array end + it "raises ArgumentError if there are not enough available-rooms on the requested date" do + expect{@tracker.add_reservation(start_time: Date.new(2018,1,1),end_time:Date.new(2018,1,2),number_of_rooms:350)}.must_raise ArgumentError + end + + # def add_reservation(start_time: Date.now, end_time: Date.now + 1, number_of_rooms: 1) + # available_rooms = view_available_rooms_on(start_time: start_time, end_time: end_time) #<--returns an array of available rooms! + # raise ArgumentError.new"Not enough rooms available on those dates" if available_rooms.length < number_of_rooms + # number_of_rooms.times do |i| + # @reservations << Reservation.new({room: [available_rooms[i].room_num], start_time: start_time, end_time: end_time, price: 200.0}) + # available_rooms[i].reserved_dates << {start_time: start_time, end_time: end_time} + # end + # @reservations + # end # it "increases the number of reservations in the reservations list" do # num_of_reservations = @tracker.reservations.length # @reservation = @tracker.add_reservation(Date.new(2018,8,1),Date.new(2018,8,25),1) From 1239005a136474ff68f00b02a78796b6551dbf93 Mon Sep 17 00:00:00 2001 From: Kay Date: Fri, 7 Sep 2018 15:40:47 -0700 Subject: [PATCH 25/46] working on #total_cost_of_reservation method, writing test1,all is going as expected --- lib/tracking_system.rb | 4 +-- spec/tracking_system_spec.rb | 47 +++++++++++------------------------- 2 files changed, 16 insertions(+), 35 deletions(-) diff --git a/lib/tracking_system.rb b/lib/tracking_system.rb index 4cf927526..4137aa402 100644 --- a/lib/tracking_system.rb +++ b/lib/tracking_system.rb @@ -13,8 +13,8 @@ def initialize @reservations = [] end - # def reservation_total_cost - # end + def total_cost_of_reservation + end def add_rooms all_rooms = [] diff --git a/spec/tracking_system_spec.rb b/spec/tracking_system_spec.rb index ab1cc991f..7b7d942ad 100644 --- a/spec/tracking_system_spec.rb +++ b/spec/tracking_system_spec.rb @@ -71,16 +71,8 @@ it "raises ArgumentError if there are not enough available-rooms on the requested date" do expect{@tracker.add_reservation(start_time: Date.new(2018,1,1),end_time:Date.new(2018,1,2),number_of_rooms:350)}.must_raise ArgumentError end + end - # def add_reservation(start_time: Date.now, end_time: Date.now + 1, number_of_rooms: 1) - # available_rooms = view_available_rooms_on(start_time: start_time, end_time: end_time) #<--returns an array of available rooms! - # raise ArgumentError.new"Not enough rooms available on those dates" if available_rooms.length < number_of_rooms - # number_of_rooms.times do |i| - # @reservations << Reservation.new({room: [available_rooms[i].room_num], start_time: start_time, end_time: end_time, price: 200.0}) - # available_rooms[i].reserved_dates << {start_time: start_time, end_time: end_time} - # end - # @reservations - # end # it "increases the number of reservations in the reservations list" do # num_of_reservations = @tracker.reservations.length # @reservation = @tracker.add_reservation(Date.new(2018,8,1),Date.new(2018,8,25),1) @@ -116,31 +108,8 @@ # expect(@reservation).must_be_kind_of Reservation # end - end - # def add_reservation(start_time: nil, end_time: nil, number_of_rooms) - # # 1. pick a room that is available. the next available room? - # @all_rooms.each do |room| - # room.reserved_dates.each do |reserved_dates| # this is a hash containing {checkin_time: checkin, checkout_time: checkout}, can replace some fo this with helper method - # if !(reserved_dates[:start_time]..reserved_dates[:end_time]).include?(:checkin) && room.block == :NA - # #create a new reservation with |room| that is iterated in and fits the reqs - # else - # # raise argument error if inside @all_rooms no room is available on this date range (then admin would need to input a new date range) - # # else..add the new reservation below - # reservation = Reservation.new(date_range: view_two_dates_as_range {start_time: checkin, end_time: checkout -1}, room: room) # <----room object contains {room_num:, price: STANDARD_ROOM_PRICE, customer: ""} - # @reservations << reservation - # room.reserved_dates << {start_time: checkin, end_time: checkout} - # end - # def view_available_rooms_on(start_time, end_time) - # #available_rooms = [] - # # @all_rooms.each do |room| - # # room.reserved_dates.each do |date_range| <---date_range could be a hash like {checkin_time: checkin, checkout_time: checkout} - # ### if the date_range doesn't include date aka: - # # if !date_range.include?(date) - # # available_rooms << room - # # end - # # return available_rooms (this is an array of rooms avail on 'date' passed as param) - # + describe "#view_available_rooms_on specifc date range" do before do end @@ -173,6 +142,18 @@ end + describe "#total_cost_of_reservation" do + before do + @tracker = TrackingSystem.new + end + + it "returns an instance of Float" do + expect(@tracker.total_cost_of_reservation).must_be_kind_of Float + end + + + end + From f6481eb65df86ae47e9d2109b7d73be93ff6e387 Mon Sep 17 00:00:00 2001 From: Kay Date: Fri, 7 Sep 2018 17:30:51 -0700 Subject: [PATCH 26/46] need to edit #total_cost_of_reservation to sum the nightly fee --- lib/reservation.rb | 4 +- lib/tracking_system.rb | 13 +- spec/reservation_spec.rb | 6 +- spec/tracking_system_spec.rb | 242 ++++++++++++++++++----------------- 4 files changed, 143 insertions(+), 122 deletions(-) diff --git a/lib/reservation.rb b/lib/reservation.rb index 298dd768a..7212eca7b 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -1,9 +1,9 @@ class Reservation - attr_reader :room, :start_time, :end_time, :price + attr_reader :room_num, :start_time, :end_time, :price #need to write a test for these def initialize(attributes) #attributes is a hash , so theoretically from CSV it would be converted into a hash - @room = attributes[:room] #the value of room should be an Integer + @room_num = attributes[:room_num] #the value of room should be an Integer @start_time = attributes[:start_time] @end_time = attributes[:end_time] @price = attributes[:price] diff --git a/lib/tracking_system.rb b/lib/tracking_system.rb index 4137aa402..a8aab772b 100644 --- a/lib/tracking_system.rb +++ b/lib/tracking_system.rb @@ -13,7 +13,16 @@ def initialize @reservations = [] end - def total_cost_of_reservation + def total_cost_of_reservation(room_num) + raise ArgumentError.new"There are no reservations" if @reservations.empty? == true + @reservations.each do |reservation| + # binding.pry + if reservation.room_num == nil + raise ArgumentError.new"Room number #{room_num} has no current reservations" + else reservation.room_num == room_num + return reservation.price + end + end end def add_rooms @@ -36,7 +45,7 @@ def add_reservation(start_time: Date.now, end_time: Date.now + 1, number_of_room available_rooms = view_available_rooms_on(start_time: start_time, end_time: end_time) #<--returns an array of available rooms! raise ArgumentError.new"Not enough rooms available on those dates" if available_rooms.length < number_of_rooms number_of_rooms.times do |i| - @reservations << Reservation.new({room: [available_rooms[i].room_num], start_time: start_time, end_time: end_time, price: 200.0}) + @reservations << Reservation.new({room: available_rooms[i].room_num, start_time: start_time, end_time: end_time, price: 200.0}) available_rooms[i].reserved_dates << {start_time: start_time, end_time: end_time} end @reservations diff --git a/spec/reservation_spec.rb b/spec/reservation_spec.rb index 927ba9972..040500108 100644 --- a/spec/reservation_spec.rb +++ b/spec/reservation_spec.rb @@ -6,7 +6,7 @@ describe "#initialize" do before do - attributes = {room: 1 ,start_time: Date.new(2018,8,1),end_time: Date.new(2018,9,1),price: 200.0} + attributes = {room_num: 1 ,start_time: Date.new(2018,8,1),end_time: Date.new(2018,9,1),price: 200.0} @reservation = Reservation.new(attributes) end @@ -16,11 +16,11 @@ end it "is set up for specific attributes and data types" do - [:room, :start_time, :end_time, :price].each do |attribute| + [:room_num, :start_time, :end_time, :price].each do |attribute| expect(@reservation).must_respond_to attribute end - expect(@reservation.room).must_be_kind_of Integer + expect(@reservation.room_num).must_be_kind_of Integer expect(@reservation.start_time).must_be_kind_of Date expect(@reservation.end_time).must_be_kind_of Date expect(@reservation.price).must_be_kind_of Float diff --git a/spec/tracking_system_spec.rb b/spec/tracking_system_spec.rb index 7b7d942ad..0964a688f 100644 --- a/spec/tracking_system_spec.rb +++ b/spec/tracking_system_spec.rb @@ -20,154 +20,166 @@ before do @tracker = TrackingSystem.new # @tracker.all_rooms.each do |room| room.reserved_dates << {checkin_time: Date.new(2018,8,1), checkout_time: Date.new(2018,8,5)} - end - - it 'is an instance of TrackingSystem' do - expect(@tracker).must_be_kind_of TrackingSystem - end + end - it 'has a list of all rooms' do - expect(@tracker.all_rooms).must_be_instance_of Array - end + it 'is an instance of TrackingSystem' do + expect(@tracker).must_be_kind_of TrackingSystem + end - it 'has a list of reservations' do - expect(@tracker.reservations).must_be_instance_of Array - end + it 'has a list of all rooms' do + expect(@tracker.all_rooms).must_be_instance_of Array + end + it 'has a list of reservations' do + expect(@tracker.reservations).must_be_instance_of Array end - describe "access the list of all 20 rooms in the hotel" do - before do - @tracker = TrackingSystem.new - end + end - it 'must return an Array' do - expect(@tracker.all_rooms).must_be_instance_of Array - end + describe "access the list of all 20 rooms in the hotel" do + before do + @tracker = TrackingSystem.new + end - it 'must contain 20 elements' do - expect(@tracker.all_rooms.length).must_equal 20 - end + it 'must return an Array' do + expect(@tracker.all_rooms).must_be_instance_of Array + end - it 'must contain only instances of Room' do - expect(@tracker.all_rooms.all? {|room| room.class == Room}).must_equal true - end + it 'must contain 20 elements' do + expect(@tracker.all_rooms.length).must_equal 20 end - # #method for making a reservation - # # Admin can reserve a room for a given date range - describe "#add_reservation" do - before do - @tracker = TrackingSystem.new - # @room = Room.new({room_num: 1, availability: :available }) - end + it 'must contain only instances of Room' do + expect(@tracker.all_rooms.all? {|room| room.class == Room}).must_equal true + end + end - it "returns an array of reservations" do - @reservation = @tracker.add_reservation(start_time: Date.new(2018,8,1), end_time: Date.new(2018,8,25), number_of_rooms:1) - # binding.pry - expect(@reservation).must_be_kind_of Array - end + # #method for making a reservation + # # Admin can reserve a room for a given date range + describe "#add_reservation" do + before do + @tracker = TrackingSystem.new + # @room = Room.new({room_num: 1, availability: :available }) + end - it "raises ArgumentError if there are not enough available-rooms on the requested date" do - expect{@tracker.add_reservation(start_time: Date.new(2018,1,1),end_time:Date.new(2018,1,2),number_of_rooms:350)}.must_raise ArgumentError - end + it "returns an array of reservations" do + @reservation = @tracker.add_reservation(start_time: Date.new(2018,8,1), end_time: Date.new(2018,8,25), number_of_rooms:1) + # binding.pry + expect(@reservation).must_be_kind_of Array end - # it "increases the number of reservations in the reservations list" do - # num_of_reservations = @tracker.reservations.length - # @reservation = @tracker.add_reservation(Date.new(2018,8,1),Date.new(2018,8,25),1) - # @reservation2 = @tracker.add_reservation(Date.new(2018,8,1),Date.new(2018,8,25),1) - # updated_num_of_reservations = @tracker.reservations.length - # binding.pry - # expect(updated_num_of_reservations - num_of_reservations).must_equal 1 - # end - - # it "changes block_status of the reserved room to :RESERVED for a given date range" do - # end - # it "saves the checkout_time as a day before " do - # end - # it "adds a hash of start/end time to each room.reserved_dates array" do - # end - - # it "only selects rooms that are available " do - # expect(@reservation.) - # end - # - # it "creates reservations for the number_of_rooms requested " do - # end - # - # it "iterates through @all_rooms to find the first available rooms" do - # end - # - # it "raises ArgumentError if inside @all_rooms no room is available on this date range" do - # end - # - # it "creates a new instance of Reservation" do - # @reservation = @tracker.add_reservation(start_time: Date.new(2018,8,1), end_time: Date.new(2018,8,25), rooms: 1) #<---1 is the number of rooms - # - # expect(@reservation).must_be_kind_of Reservation - # end - - - - describe "#view_available_rooms_on specifc date range" do - before do - end + it "raises ArgumentError if there are not enough available-rooms on the requested date" do + expect{@tracker.add_reservation(start_time: Date.new(2018,1,1),end_time:Date.new(2018,1,2),number_of_rooms:350)}.must_raise ArgumentError + end + end + + # it "increases the number of reservations in the reservations list" do + # num_of_reservations = @tracker.reservations.length + # @reservation = @tracker.add_reservation(Date.new(2018,8,1),Date.new(2018,8,25),1) + # @reservation2 = @tracker.add_reservation(Date.new(2018,8,1),Date.new(2018,8,25),1) + # updated_num_of_reservations = @tracker.reservations.length + # binding.pry + # expect(updated_num_of_reservations - num_of_reservations).must_equal 1 + # end + + # it "changes block_status of the reserved room to :RESERVED for a given date range" do + # end + # it "saves the checkout_time as a day before " do + # end + # it "adds a hash of start/end time to each room.reserved_dates array" do + # end + + # it "only selects rooms that are available " do + # expect(@reservation.) + # end + # + # it "creates reservations for the number_of_rooms requested " do + # end + # + # it "iterates through @all_rooms to find the first available rooms" do + # end + # + # it "raises ArgumentError if inside @all_rooms no room is available on this date range" do + # end + # + # it "creates a new instance of Reservation" do + # @reservation = @tracker.add_reservation(start_time: Date.new(2018,8,1), end_time: Date.new(2018,8,25), rooms: 1) #<---1 is the number of rooms + # + # expect(@reservation).must_be_kind_of Reservation + # end + + + + describe "#view_available_rooms_on specifc date range" do + before do + end - it "returns an array" do - @tracker = TrackingSystem.new - @available_rooms = @tracker.view_available_rooms_on(start_time: Date.new(2018,8,1),end_time:Date.new(2018,9,5)) - expect(@available_rooms).must_be_kind_of Array - end + it "returns an array" do + @tracker = TrackingSystem.new + @available_rooms = @tracker.view_available_rooms_on(start_time: Date.new(2018,8,1),end_time:Date.new(2018,9,5)) + expect(@available_rooms).must_be_kind_of Array + end - it "returns an array containing instances of Room" do - @tracker = TrackingSystem.new - @available_rooms = @tracker.view_available_rooms_on(start_time: Date.new(2018,8,1),end_time:Date.new(2018,9,5)) - expect(@available_rooms[0]).must_be_kind_of Room - end + it "returns an array containing instances of Room" do + @tracker = TrackingSystem.new + @available_rooms = @tracker.view_available_rooms_on(start_time: Date.new(2018,8,1),end_time:Date.new(2018,9,5)) + expect(@available_rooms[0]).must_be_kind_of Room + end - it "raises ArgumentError if no rooms are available on these dates" do - @tracker = TrackingSystem.new - @tracker.all_rooms.each do |room| - room.reserved_dates << {start_time: Date.new(2018,1,1), end_time: Date.new(2018,1,2)} - end - #need to create add_reservation and then call it 20 times on the same date then the 21st time it'll raise an error - expect{@tracker.view_available_rooms_on(start_time: Date.new(2018,1,1),end_time:Date.new(2018,1,2))}.must_raise ArgumentError + it "raises ArgumentError if no rooms are available on these dates" do + @tracker = TrackingSystem.new + @tracker.all_rooms.each do |room| + room.reserved_dates << {start_time: Date.new(2018,1,1), end_time: Date.new(2018,1,2)} end + #need to create add_reservation and then call it 20 times on the same date then the 21st time it'll raise an error + expect{@tracker.view_available_rooms_on(start_time: Date.new(2018,1,1),end_time:Date.new(2018,1,2))}.must_raise ArgumentError + end - it "raises ArgumentError if start_time is > end_time" do - @tracker = TrackingSystem.new - expect{@tracker.view_available_rooms_on(start_time: Date.new(2018,10,5),end_time:Date.new(2018,9,5))}.must_raise ArgumentError - end + it "raises ArgumentError if start_time is > end_time" do + @tracker = TrackingSystem.new + expect{@tracker.view_available_rooms_on(start_time: Date.new(2018,10,5),end_time:Date.new(2018,9,5))}.must_raise ArgumentError + end + end + + describe "#total_cost_of_reservation" do + before do + @tracker = TrackingSystem.new end - describe "#total_cost_of_reservation" do - before do - @tracker = TrackingSystem.new - end + it "returns an instance of Float" do + @reservation = @tracker.add_reservation(start_time: Date.new(2018,8,1), end_time: Date.new(2018,8,25), number_of_rooms:20) + #the reservation above booked 20 rooms so that means all rooms have reservations, at at $200 + expect(@tracker.total_cost_of_reservation(1)).must_be_kind_of Float + end - it "returns an instance of Float" do - expect(@tracker.total_cost_of_reservation).must_be_kind_of Float - end + it "raises an ArgumentError if no reservations exist" do + expect{@tracker.total_cost_of_reservation(1)}.must_raise ArgumentError + end + it "raises an ArgumentError the room number has no reservation" do + #create a room that has no reservations + @reservation = @tracker.add_reservation(start_time: Date.new(2018,8,1), end_time: Date.new(2018,8,25), number_of_rooms:19) + expect{@tracker.total_cost_of_reservation(20)}.must_raise ArgumentError end + end - describe "#view_all_rooms" do - before do - @tracker = TrackingSystem.new - end - it "returns a list" do - expect(@tracker.view_all_rooms).must_be_kind_of Array - end + describe "#view_all_rooms" do + before do + @tracker = TrackingSystem.new + end + it "returns a list" do + expect(@tracker.view_all_rooms).must_be_kind_of Array end - end #end of class method + end + +end #end of class method From 72ca3bb3854367f487b2e9e15c1665a22a7abddf Mon Sep 17 00:00:00 2001 From: Kay Date: Fri, 7 Sep 2018 18:03:34 -0700 Subject: [PATCH 27/46] fixed bug in #add_reservation, when creating new reservation the room_num: was num: --- lib/tracking_system.rb | 5 +++-- spec/tracking_system_spec.rb | 6 ++---- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/lib/tracking_system.rb b/lib/tracking_system.rb index a8aab772b..84f1123d4 100644 --- a/lib/tracking_system.rb +++ b/lib/tracking_system.rb @@ -16,10 +16,11 @@ def initialize def total_cost_of_reservation(room_num) raise ArgumentError.new"There are no reservations" if @reservations.empty? == true @reservations.each do |reservation| - # binding.pry if reservation.room_num == nil + binding.pry raise ArgumentError.new"Room number #{room_num} has no current reservations" else reservation.room_num == room_num + #insert part where calculation happens return reservation.price end end @@ -45,7 +46,7 @@ def add_reservation(start_time: Date.now, end_time: Date.now + 1, number_of_room available_rooms = view_available_rooms_on(start_time: start_time, end_time: end_time) #<--returns an array of available rooms! raise ArgumentError.new"Not enough rooms available on those dates" if available_rooms.length < number_of_rooms number_of_rooms.times do |i| - @reservations << Reservation.new({room: available_rooms[i].room_num, start_time: start_time, end_time: end_time, price: 200.0}) + @reservations << Reservation.new({room_num: available_rooms[i].room_num, start_time: start_time, end_time: end_time, price: 200.0}) available_rooms[i].reserved_dates << {start_time: start_time, end_time: end_time} end @reservations diff --git a/spec/tracking_system_spec.rb b/spec/tracking_system_spec.rb index 0964a688f..66bb63288 100644 --- a/spec/tracking_system_spec.rb +++ b/spec/tracking_system_spec.rb @@ -148,7 +148,7 @@ end it "returns an instance of Float" do - @reservation = @tracker.add_reservation(start_time: Date.new(2018,8,1), end_time: Date.new(2018,8,25), number_of_rooms:20) + @reservation = @tracker.add_reservation(start_time: Date.new(2018,8,1), end_time: Date.new(2018,8,2), number_of_rooms:20) #the reservation above booked 20 rooms so that means all rooms have reservations, at at $200 expect(@tracker.total_cost_of_reservation(1)).must_be_kind_of Float end @@ -158,9 +158,7 @@ end it "raises an ArgumentError the room number has no reservation" do - #create a room that has no reservations - @reservation = @tracker.add_reservation(start_time: Date.new(2018,8,1), end_time: Date.new(2018,8,25), number_of_rooms:19) - + #in this it block no reservations have been made so the room expect{@tracker.total_cost_of_reservation(20)}.must_raise ArgumentError end From e22a508209989bd84a13bc6717ac8e5915e22846 Mon Sep 17 00:00:00 2001 From: Kay Date: Fri, 7 Sep 2018 18:24:08 -0700 Subject: [PATCH 28/46] moved #total_cost method to Reservation class, but still keeping some commented out code blocks in TrackingSystem to see if I'll call on that method from TS later --- lib/reservation.rb | 8 +++++-- lib/tracking_system.rb | 29 +++++++++++++++---------- spec/tracking_system_spec.rb | 42 ++++++++++++++++++------------------ 3 files changed, 45 insertions(+), 34 deletions(-) diff --git a/lib/reservation.rb b/lib/reservation.rb index 7212eca7b..37fb9992c 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -8,10 +8,14 @@ def initialize(attributes) #attributes is a hash , so theoretically from CSV it @end_time = attributes[:end_time] @price = attributes[:price] end + + def total_cost + return total_cost = (end_time - start_time) * price + end + end -# 1. get the total cost for a given reservation -# def total_cost + #when a reservation is made a room number needs to be assigned diff --git a/lib/tracking_system.rb b/lib/tracking_system.rb index 84f1123d4..e0ec38c9a 100644 --- a/lib/tracking_system.rb +++ b/lib/tracking_system.rb @@ -13,18 +13,25 @@ def initialize @reservations = [] end - def total_cost_of_reservation(room_num) - raise ArgumentError.new"There are no reservations" if @reservations.empty? == true - @reservations.each do |reservation| - if reservation.room_num == nil - binding.pry - raise ArgumentError.new"Room number #{room_num} has no current reservations" - else reservation.room_num == room_num - #insert part where calculation happens - return reservation.price - end - end + # def total_cost_of_reservation(room_num) + # raise ArgumentError.new"There are no reservations" if @reservations.empty? == true + # @reservations.each do |reservation| + # if reservation.room_num == nil + # raise ArgumentError.new"Room number #{room_num} has no current reservations" + # else reservation.room_num == room_num + # + # #insert part where calculation happens + # return reservation.price + # end + # end + # end +########################################################### +#this method doesn't have tests yet because i'm not sure if im going to combine it with the method above + def total_cost_of_reservation(reservation) + return reservation.total_cost end +########################################################### + def add_rooms all_rooms = [] diff --git a/spec/tracking_system_spec.rb b/spec/tracking_system_spec.rb index 66bb63288..89cdb2e29 100644 --- a/spec/tracking_system_spec.rb +++ b/spec/tracking_system_spec.rb @@ -142,27 +142,27 @@ end - describe "#total_cost_of_reservation" do - before do - @tracker = TrackingSystem.new - end - - it "returns an instance of Float" do - @reservation = @tracker.add_reservation(start_time: Date.new(2018,8,1), end_time: Date.new(2018,8,2), number_of_rooms:20) - #the reservation above booked 20 rooms so that means all rooms have reservations, at at $200 - expect(@tracker.total_cost_of_reservation(1)).must_be_kind_of Float - end - - it "raises an ArgumentError if no reservations exist" do - expect{@tracker.total_cost_of_reservation(1)}.must_raise ArgumentError - end - - it "raises an ArgumentError the room number has no reservation" do - #in this it block no reservations have been made so the room - expect{@tracker.total_cost_of_reservation(20)}.must_raise ArgumentError - end - - end + # describe "#total_cost_of_reservation" do + # before do + # @tracker = TrackingSystem.new + # end + # + # it "returns an instance of Float" do + # @reservation = @tracker.add_reservation(start_time: Date.new(2018,8,1), end_time: Date.new(2018,8,2), number_of_rooms:20) + # #the reservation above booked 20 rooms so that means all rooms have reservations, at at $200 + # expect(@tracker.total_cost_of_reservation(1)).must_be_kind_of Float + # end + # + # it "raises an ArgumentError if no reservations exist" do + # expect{@tracker.total_cost_of_reservation(1)}.must_raise ArgumentError + # end + # + # it "raises an ArgumentError the room number has no reservation" do + # #in this it block no reservations have been made so the room + # expect{@tracker.total_cost_of_reservation(20)}.must_raise ArgumentError + # end + # + # end From adc68d50cfda9c3a6956a4094886ea5d881b9da3 Mon Sep 17 00:00:00 2001 From: Kay Date: Fri, 7 Sep 2018 18:53:56 -0700 Subject: [PATCH 29/46] created #view_reserations_on(date) method in TrackingSystem, ned to update tests --- lib/tracking_system.rb | 72 ++++++++++++++++++++---------------- spec/tracking_system_spec.rb | 20 ++++++++++ 2 files changed, 61 insertions(+), 31 deletions(-) diff --git a/lib/tracking_system.rb b/lib/tracking_system.rb index e0ec38c9a..4ceff44a8 100644 --- a/lib/tracking_system.rb +++ b/lib/tracking_system.rb @@ -25,12 +25,12 @@ def initialize # end # end # end -########################################################### -#this method doesn't have tests yet because i'm not sure if im going to combine it with the method above + ########################################################### + #this method doesn't have tests yet because i'm not sure if im going to combine it with the method above def total_cost_of_reservation(reservation) return reservation.total_cost end -########################################################### + ########################################################### def add_rooms @@ -58,8 +58,8 @@ def add_reservation(start_time: Date.now, end_time: Date.now + 1, number_of_room end @reservations end -#need to come back here and create i instances of reservation..but first need to change Reservation.rooms to hold an Integer instead of Array - # available_rooms.each do |room| + #need to come back here and create i instances of reservation..but first need to change Reservation.rooms to hold an Integer instead of Array + # available_rooms.each do |room| # @all_rooms.each do |room| # number_of_rooms.times do # if room.reserved_dates.empty? @@ -112,38 +112,48 @@ def view_available_rooms_on(start_time: Date.now, end_time: Date.now + 1) return available_rooms end end -end - -#access the list of reservations for a specific date - # def view_reservations_on(date) - # # list_of_res_on this date ^ above = []<--create emtpy array - # #@reservations.each do |reservation| - # #if (reservation.checkin_time..reservation.checkout_time).include?(date) - # #then list_of_res_on (date) << reservation - # #return the array list_of_res_on (date) - # # end - # + #access the list of reservations for a specific date <---not date range + def view_reservations_on(date) + all_reservations = [] + @reservations.each do |reservation| + if (reservation[:start_time]...reservation[:end_time]).include? date + all_reservations << reservation + end + if all_reservations.empty? + raise ArgumentError.new"No reservations on this date" + end + end + end - ############################# ############################# ############################# - # private #helper methods below - # def view_two_dates_as_range() #<--put params {checkin_time: checkin, checkout_time: checkout} - # def check_if_rooms_available_on(date_range) <--or does the view_available_rooms_on() already do this pretty much? def ranges_overlap?(r1, r2) r1.include?(r2.first) || r2.include?(r1.first) end - ############################# ############################# ############################# - #think about how each room can be reserved thru time +end + - # The hotel has 20 rooms, and they are numbered 1 through 20 - # Every room is identical, and a room always costs $200/night - # The last day of a reservation is the checkout day, so the - # guest should not be charged for that night - # For this wave, any room can be reserved at any time, and - # you don't need to check whether reservations conflict with each other (this will come in wave 2!) +# - # def overlap?(x,y) - # (x.first - y.end) * (y.first - x.end) > 0 - # end + + +############################# ############################# ############################# +# private #helper methods below +# def view_two_dates_as_range() #<--put params {checkin_time: checkin, checkout_time: checkout} +# def check_if_rooms_available_on(date_range) <--or does the view_available_rooms_on() already do this pretty much? + +############################# ############################# ############################# + +#think about how each room can be reserved thru time + +# The hotel has 20 rooms, and they are numbered 1 through 20 +# Every room is identical, and a room always costs $200/night +# The last day of a reservation is the checkout day, so the +# guest should not be charged for that night +# For this wave, any room can be reserved at any time, and +# you don't need to check whether reservations conflict with each other (this will come in wave 2!) + +# def overlap?(x,y) +# (x.first - y.end) * (y.first - x.end) > 0 +# end diff --git a/spec/tracking_system_spec.rb b/spec/tracking_system_spec.rb index 89cdb2e29..217a98578 100644 --- a/spec/tracking_system_spec.rb +++ b/spec/tracking_system_spec.rb @@ -112,6 +112,7 @@ describe "#view_available_rooms_on specifc date range" do before do + @tracker = TrackingSystem.new end it "returns an array" do @@ -164,6 +165,25 @@ # # end + # def view_reservations_on(start_time: Date.now, end_time: Date.now + 1) + # # list_of_res_on this date ^ above = []<--create emtpy array + # #@reservations.each do |reservation| + # #if (reservation.checkin_time..reservation.checkout_time).include?(date) + # #then list_of_res_on (date) << reservation + # #return the array list_of_res_on (date) + # # end + # + describe "#view_reservations_on" do + before do + @tracker = TrackingSystem.new + end + + it "returns an array" do + expect(@tracker.view_reservations_on(start_time: Date.new(2018,10,5), end_time: Date.new(2018,10,6))).must_be_kind_of Array + end + + end + From bc3947009891ab7411d183e0bfc97c61a15d4081 Mon Sep 17 00:00:00 2001 From: Kay Date: Fri, 7 Sep 2018 21:57:05 -0700 Subject: [PATCH 30/46] nothing much changed in view_reservations_on method, preparing for push --- lib/tracking_system.rb | 3 ++- spec/tracking_system_spec.rb | 20 ++++++++++++++++++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/lib/tracking_system.rb b/lib/tracking_system.rb index 4ceff44a8..9b243c177 100644 --- a/lib/tracking_system.rb +++ b/lib/tracking_system.rb @@ -115,13 +115,14 @@ def view_available_rooms_on(start_time: Date.now, end_time: Date.now + 1) #access the list of reservations for a specific date <---not date range def view_reservations_on(date) + raise ArgumentError.new"" unless date.instance_of? == Date all_reservations = [] @reservations.each do |reservation| if (reservation[:start_time]...reservation[:end_time]).include? date all_reservations << reservation end if all_reservations.empty? - raise ArgumentError.new"No reservations on this date" + raise ArgumentError.new"No reservations on #{date}" end end end diff --git a/spec/tracking_system_spec.rb b/spec/tracking_system_spec.rb index 217a98578..26e3ad5e6 100644 --- a/spec/tracking_system_spec.rb +++ b/spec/tracking_system_spec.rb @@ -179,12 +179,28 @@ end it "returns an array" do - expect(@tracker.view_reservations_on(start_time: Date.new(2018,10,5), end_time: Date.new(2018,10,6))).must_be_kind_of Array + expect(@tracker.view_reservations_on(Date.new(2018,10,5))).must_be_kind_of Array end - end + it "raises an ArgumentError if no reservations are available on given date" do + expect{@tracker.view_reservations_on(Date.new(2333,10,5))}.must_raise ArgumentError + end + end + + # + # def view_reservations_on(date) + # all_reservations = [] + # @reservations.each do |reservation| + # if (reservation[:start_time]...reservation[:end_time]).include? date + # all_reservations << reservation + # end + # if all_reservations.empty? + # raise ArgumentError.new"No reservations on this date" + # end + # end + # end From d0fc68d3dca471f1acdf78d6e2285108092b99d3 Mon Sep 17 00:00:00 2001 From: Kay Date: Sat, 8 Sep 2018 11:17:25 -0700 Subject: [PATCH 31/46] fixed bug in tests for #view_reservations_on(date) method, now there are 3 tests for this method, all tests passing --- lib/reservation.rb | 1 + lib/tracking_system.rb | 14 ++++++++------ spec/tracking_system_spec.rb | 9 ++++++++- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/lib/reservation.rb b/lib/reservation.rb index 37fb9992c..6e5c516ac 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -10,6 +10,7 @@ def initialize(attributes) #attributes is a hash , so theoretically from CSV it end def total_cost + total_cost = 0 return total_cost = (end_time - start_time) * price end diff --git a/lib/tracking_system.rb b/lib/tracking_system.rb index 9b243c177..4e674d8f2 100644 --- a/lib/tracking_system.rb +++ b/lib/tracking_system.rb @@ -115,15 +115,17 @@ def view_available_rooms_on(start_time: Date.now, end_time: Date.now + 1) #access the list of reservations for a specific date <---not date range def view_reservations_on(date) - raise ArgumentError.new"" unless date.instance_of? == Date + raise ArgumentError.new"#{date} must be instance of Date" unless date.instance_of?(Date) all_reservations = [] @reservations.each do |reservation| - if (reservation[:start_time]...reservation[:end_time]).include? date + if (reservation.start_time...reservation.end_time).include?(date) #maybe this boolean can be deleted all_reservations << reservation end - if all_reservations.empty? - raise ArgumentError.new"No reservations on #{date}" - end + end + if all_reservations.empty? + raise ArgumentError.new"There are no reservations on #{date}" + else + return all_reservations end end @@ -132,7 +134,7 @@ def ranges_overlap?(r1, r2) r1.include?(r2.first) || r2.include?(r1.first) end -end +end #class end # diff --git a/spec/tracking_system_spec.rb b/spec/tracking_system_spec.rb index 26e3ad5e6..fb0c201ea 100644 --- a/spec/tracking_system_spec.rb +++ b/spec/tracking_system_spec.rb @@ -179,13 +179,20 @@ end it "returns an array" do + @reservation = @tracker.add_reservation(start_time: Date.new(2018,10,5), end_time: Date.new(2018,10,10), number_of_rooms:1) + @reservation1 = @tracker.add_reservation(start_time: Date.new(2018,10,5), end_time: Date.new(2018,10,10), number_of_rooms:1) expect(@tracker.view_reservations_on(Date.new(2018,10,5))).must_be_kind_of Array end it "raises an ArgumentError if no reservations are available on given date" do - expect{@tracker.view_reservations_on(Date.new(2333,10,5))}.must_raise ArgumentError + expect{@tracker.view_reservations_on(Date.new(3333,10,5))}.must_raise ArgumentError end + it "raises an ArgumentError if given date is not an instance of Date class" do + expect{@tracker.view_reservations_on(2018,2,1)}.must_raise ArgumentError + end + + end From a256d49235e000917d3deb6a744d242064774c03 Mon Sep 17 00:00:00 2001 From: Kay Date: Sat, 8 Sep 2018 12:16:56 -0700 Subject: [PATCH 32/46] added extra test to #add_reservation method, all tests passing, 99.41% coverage --- lib/reservation.rb | 2 +- lib/tracking_system.rb | 3 +- spec/reservation_spec.rb | 29 +++++++-- spec/tracking_system_spec.rb | 116 ++++++++++++++--------------------- 4 files changed, 74 insertions(+), 76 deletions(-) diff --git a/lib/reservation.rb b/lib/reservation.rb index 6e5c516ac..a3cfd843a 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -11,7 +11,7 @@ def initialize(attributes) #attributes is a hash , so theoretically from CSV it def total_cost total_cost = 0 - return total_cost = (end_time - start_time) * price + return total_cost = ((end_time - start_time) * price).round(2) end end diff --git a/lib/tracking_system.rb b/lib/tracking_system.rb index 4e674d8f2..5c39177d9 100644 --- a/lib/tracking_system.rb +++ b/lib/tracking_system.rb @@ -3,7 +3,7 @@ require 'pry' NUMBER_OF_ROOMS = 20 -STANDARD_ROOM_PRICE = 200.0 +STANDARD_ROOM_PRICE = 200.00 class TrackingSystem attr_reader :all_rooms, :reservations @@ -28,6 +28,7 @@ def initialize ########################################################### #this method doesn't have tests yet because i'm not sure if im going to combine it with the method above def total_cost_of_reservation(reservation) + raise ArgumentError.new"#{reservation} is an invalid argument type" unless reservation.instance_of? Reservation return reservation.total_cost end ########################################################### diff --git a/spec/reservation_spec.rb b/spec/reservation_spec.rb index 040500108..9c2ba859e 100644 --- a/spec/reservation_spec.rb +++ b/spec/reservation_spec.rb @@ -8,7 +8,6 @@ before do attributes = {room_num: 1 ,start_time: Date.new(2018,8,1),end_time: Date.new(2018,9,1),price: 200.0} @reservation = Reservation.new(attributes) - end it 'is an instance of Reservation' do @@ -25,12 +24,34 @@ expect(@reservation.end_time).must_be_kind_of Date expect(@reservation.price).must_be_kind_of Float end - # - # it "checkin_time must be before checkout_time" do - # end + end + + describe "#total_cost" do + before do + attributes = {room_num: 1 ,start_time: Date.new(2018,8,1),end_time: Date.new(2018,8,2),price: 200.23111111} + @reservation = Reservation.new(attributes) + end + it "returns an instance of Float" do + expect(@reservation.total_cost).must_be_kind_of Float + end + it "rounds the return value to 2 decimals" do + expect(@reservation.total_cost).must_equal 200.23 + end end + # def total_cost + # total_cost = 0 + # return total_cost = ((end_time - start_time) * price).round(2) + # end + # + # + # it "checkin_time must be before checkout_time" do + # end + + + + end diff --git a/spec/tracking_system_spec.rb b/spec/tracking_system_spec.rb index fb0c201ea..53f4089fc 100644 --- a/spec/tracking_system_spec.rb +++ b/spec/tracking_system_spec.rb @@ -4,16 +4,6 @@ require_relative '../lib/reservation' require 'pry' - -# Wave 1 Tracking Reservations -# 1. access the list of all of the rooms in the hotel -# 2. reserve a room for a given date range -# 3. access the list of reservations for a specific date -# 4. get the total cost for a given reservation -# -#NOTES ON POSSIBLE TESTS TO ADD: -#making sure each instance variable in initialization is the correct class - describe 'TrackingSystem class' do ###### WAVE 1 ################################################################## describe "#initialize" do @@ -54,8 +44,6 @@ end end - # #method for making a reservation - # # Admin can reserve a room for a given date range describe "#add_reservation" do before do @tracker = TrackingSystem.new @@ -63,29 +51,41 @@ end it "returns an array of reservations" do - @reservation = @tracker.add_reservation(start_time: Date.new(2018,8,1), end_time: Date.new(2018,8,25), number_of_rooms:1) - # binding.pry - expect(@reservation).must_be_kind_of Array + @reservations = @tracker.add_reservation(start_time: Date.new(2018,8,1), end_time: Date.new(2018,8,25), number_of_rooms:1) + @reservations = @tracker.add_reservation(start_time: Date.new(2018,8,1), end_time: Date.new(2018,8,25), number_of_rooms:6) + list = @tracker.reservations + expect(@reservations).must_be_kind_of Array end it "raises ArgumentError if there are not enough available-rooms on the requested date" do expect{@tracker.add_reservation(start_time: Date.new(2018,1,1),end_time:Date.new(2018,1,2),number_of_rooms:350)}.must_raise ArgumentError end - end - # it "increases the number of reservations in the reservations list" do - # num_of_reservations = @tracker.reservations.length - # @reservation = @tracker.add_reservation(Date.new(2018,8,1),Date.new(2018,8,25),1) - # @reservation2 = @tracker.add_reservation(Date.new(2018,8,1),Date.new(2018,8,25),1) - # updated_num_of_reservations = @tracker.reservations.length - # binding.pry - # expect(updated_num_of_reservations - num_of_reservations).must_equal 1 - # end + it "increases the number of reservations in the reservations list" do + @reservations = @tracker.add_reservation(start_time: Date.new(2018,8,1), end_time: Date.new(2018,8,25), number_of_rooms:1) + original_num_of_reservations = @tracker.reservations + @reservations = @tracker.add_reservation(start_time: Date.new(2018,8,1), end_time: Date.new(2018,8,25), number_of_rooms:1) + updated_num_of_reservations = @tracker.reservations + expect(updated_num_of_reservations.length).must_equal 2 + end + end #end of describe + + + + + + + # @reservation = @tracker.add_reservation(start_time: Date.new(2018,8,1), end_time: Date.new(2018,8,25), number_of_rooms:1) + # #check the length of reservations + # updated_num_of_reservations = @tracker.reservations.length + # # #make another reservation + # # @reservation2 = @tracker.add_reservation(start_time: Date.new(2018,8,1), end_time: Date.new(2018,8,2), number_of_rooms:1) + # # #check the length and the compare them + # # updated_num_of_reservations = @tracker.reservations.length + # # # binding.pry + + - # it "changes block_status of the reserved room to :RESERVED for a given date range" do - # end - # it "saves the checkout_time as a day before " do - # end # it "adds a hash of start/end time to each room.reserved_dates array" do # end @@ -143,36 +143,8 @@ end - # describe "#total_cost_of_reservation" do - # before do - # @tracker = TrackingSystem.new - # end - # - # it "returns an instance of Float" do - # @reservation = @tracker.add_reservation(start_time: Date.new(2018,8,1), end_time: Date.new(2018,8,2), number_of_rooms:20) - # #the reservation above booked 20 rooms so that means all rooms have reservations, at at $200 - # expect(@tracker.total_cost_of_reservation(1)).must_be_kind_of Float - # end - # - # it "raises an ArgumentError if no reservations exist" do - # expect{@tracker.total_cost_of_reservation(1)}.must_raise ArgumentError - # end - # - # it "raises an ArgumentError the room number has no reservation" do - # #in this it block no reservations have been made so the room - # expect{@tracker.total_cost_of_reservation(20)}.must_raise ArgumentError - # end - # - # end - # def view_reservations_on(start_time: Date.now, end_time: Date.now + 1) - # # list_of_res_on this date ^ above = []<--create emtpy array - # #@reservations.each do |reservation| - # #if (reservation.checkin_time..reservation.checkout_time).include?(date) - # #then list_of_res_on (date) << reservation - # #return the array list_of_res_on (date) - # # end - # + describe "#view_reservations_on" do before do @tracker = TrackingSystem.new @@ -191,25 +163,29 @@ it "raises an ArgumentError if given date is not an instance of Date class" do expect{@tracker.view_reservations_on(2018,2,1)}.must_raise ArgumentError end + end + describe "#total_cost_of_reservation" do + before do + @tracker = TrackingSystem.new + attributes = {room_num: 1 ,start_time: Date.new(2018,8,1),end_time: Date.new(2018,9,1),price: 200.00} + @reservation = Reservation.new(attributes) + end - end + it "raises ArgumentError unless argument is an instance of Reservation" do + expect{@tracker.total_cost_of_reservation("hello there!")}.must_raise ArgumentError + end - # - # def view_reservations_on(date) - # all_reservations = [] - # @reservations.each do |reservation| - # if (reservation[:start_time]...reservation[:end_time]).include? date - # all_reservations << reservation - # end - # if all_reservations.empty? - # raise ArgumentError.new"No reservations on this date" - # end - # end - # end + it "returns an instance of a Float" do + expect(@tracker.total_cost_of_reservation(@reservation)).must_be_kind_of Float + end + it "returns a postive number" do + expect(@tracker.total_cost_of_reservation(@reservation)).must_equal 6200.00 + end + end describe "#view_all_rooms" do From a3883d535ba55689cbeab9ee2e3820bb6672b4ba Mon Sep 17 00:00:00 2001 From: Kay Date: Sat, 8 Sep 2018 15:04:03 -0700 Subject: [PATCH 33/46] added some block functionalities/limitations to view_available_rooms_on method --- lib/tracking_system.rb | 7 ++++--- spec/tracking_system_spec.rb | 16 +++------------- 2 files changed, 7 insertions(+), 16 deletions(-) diff --git a/lib/tracking_system.rb b/lib/tracking_system.rb index 5c39177d9..93c4b554b 100644 --- a/lib/tracking_system.rb +++ b/lib/tracking_system.rb @@ -50,11 +50,12 @@ def view_all_rooms end #reserve an available room for a given date range + #this method ONLY adds reservations for rooms that aren't in a block def add_reservation(start_time: Date.now, end_time: Date.now + 1, number_of_rooms: 1) - available_rooms = view_available_rooms_on(start_time: start_time, end_time: end_time) #<--returns an array of available rooms! + available_rooms = view_available_rooms_on(start_time: start_time, end_time: end_time) #<--returns an array of available rooms that also aren't in a block raise ArgumentError.new"Not enough rooms available on those dates" if available_rooms.length < number_of_rooms number_of_rooms.times do |i| - @reservations << Reservation.new({room_num: available_rooms[i].room_num, start_time: start_time, end_time: end_time, price: 200.0}) + @reservations << Reservation.new({room_num: available_rooms[i].room_num, start_time: start_time, end_time: end_time, price: 200.00}) available_rooms[i].reserved_dates << {start_time: start_time, end_time: end_time} end @reservations @@ -99,7 +100,7 @@ def view_available_rooms_on(start_time: Date.now, end_time: Date.now + 1) available_rooms << room else room.reserved_dates.each do |dates_hash| #<---date_range could be a hash like {checkin_time: checkin, checkout_time: checkout} - if ranges_overlap?((dates_hash[:start_time]...dates_hash[:end_time]).to_a, (start_time..end_time).to_a) == false + if ranges_overlap?((dates_hash[:start_time]...dates_hash[:end_time]).to_a, (start_time..end_time).to_a) == false && room.block == :NA available_rooms << room else unavailable_count += 1 diff --git a/spec/tracking_system_spec.rb b/spec/tracking_system_spec.rb index 53f4089fc..194164a76 100644 --- a/spec/tracking_system_spec.rb +++ b/spec/tracking_system_spec.rb @@ -73,19 +73,6 @@ - - - # @reservation = @tracker.add_reservation(start_time: Date.new(2018,8,1), end_time: Date.new(2018,8,25), number_of_rooms:1) - # #check the length of reservations - # updated_num_of_reservations = @tracker.reservations.length - # # #make another reservation - # # @reservation2 = @tracker.add_reservation(start_time: Date.new(2018,8,1), end_time: Date.new(2018,8,2), number_of_rooms:1) - # # #check the length and the compare them - # # updated_num_of_reservations = @tracker.reservations.length - # # # binding.pry - - - # it "adds a hash of start/end time to each room.reserved_dates array" do # end @@ -141,6 +128,9 @@ expect{@tracker.view_available_rooms_on(start_time: Date.new(2018,10,5),end_time:Date.new(2018,9,5))}.must_raise ArgumentError end + # it "ensures that room is not in a block" do + # end + end From 2d42e21945d47a1800a0279628acf6b4728044cf Mon Sep 17 00:00:00 2001 From: Kay Date: Sat, 8 Sep 2018 16:56:46 -0700 Subject: [PATCH 34/46] created #add_block() method with a few tests so far --- lib/block.rb | 15 ++++++++++ lib/room.rb | 5 ++-- lib/tracking_system.rb | 29 ++++++++++++++++-- spec/block_spec.rb | 28 +++++++++++++++++ spec/tracking_system_spec.rb | 58 ++++++++++++++++++++++++++++++++++-- 5 files changed, 129 insertions(+), 6 deletions(-) create mode 100644 lib/block.rb create mode 100644 spec/block_spec.rb diff --git a/lib/block.rb b/lib/block.rb new file mode 100644 index 000000000..5a68f5cf3 --- /dev/null +++ b/lib/block.rb @@ -0,0 +1,15 @@ +class Block + attr_reader :rooms, :start_time, :end_time, :discount + attr_accessor :block + #need to write a test for these + + def initialize(attributes) + @rooms = attributes[:rooms] #array of rooms + @start_time = attributes[:start_time] + @end_time = attributes[:end_time] + @discount = attributes[:discount] + @block = attributes[:block] ||= :NA + end + +# I can reserve a room from within a block of rooms +end diff --git a/lib/room.rb b/lib/room.rb index c1f48a8ce..6029e550e 100644 --- a/lib/room.rb +++ b/lib/room.rb @@ -1,10 +1,11 @@ class Room - attr_reader :room_num, :reserved_dates, :block + attr_reader :room_num, :reserved_dates + attr_accessor :block def initialize(attributes) @room_num = attributes[:room_num] @reserved_dates = attributes[:reserved_dates] #<--- this should be a list of date_range hashes? like {checkin_time: checkin, checkout_time: checkout} - @block = :NA #room should have a letter A~Z to indicate which rooms are with what block, NA is default + @block = attributes[:status] ||= :NA #room should have a letter A~Z to indicate which rooms are with what block, NA is default end end diff --git a/lib/tracking_system.rb b/lib/tracking_system.rb index 93c4b554b..0e98c4863 100644 --- a/lib/tracking_system.rb +++ b/lib/tracking_system.rb @@ -1,16 +1,18 @@ require_relative 'room' require_relative 'reservation' +require_relative 'block' require 'pry' NUMBER_OF_ROOMS = 20 STANDARD_ROOM_PRICE = 200.00 class TrackingSystem - attr_reader :all_rooms, :reservations + attr_reader :all_rooms, :reservations, :blocks def initialize @all_rooms = add_rooms @reservations = [] + @blocks = [] end # def total_cost_of_reservation(room_num) @@ -50,7 +52,7 @@ def view_all_rooms end #reserve an available room for a given date range - #this method ONLY adds reservations for rooms that aren't in a block + #this method ONLY adds reservations for rooms that aren't in a block def add_reservation(start_time: Date.now, end_time: Date.now + 1, number_of_rooms: 1) available_rooms = view_available_rooms_on(start_time: start_time, end_time: end_time) #<--returns an array of available rooms that also aren't in a block raise ArgumentError.new"Not enough rooms available on those dates" if available_rooms.length < number_of_rooms @@ -60,6 +62,27 @@ def add_reservation(start_time: Date.now, end_time: Date.now + 1, number_of_room end @reservations end + + def generate_block_id + (0..3).map { (65 + rand(26)).chr }.join + end + + def add_block(start_time: Date.today + 7, end_time: Date.today.next_month, number_of_rooms: 5, discount: 10) + raise ArgumentError unless discount.instance_of? Integer + raise ArgumentError.new"start_time must be before end_time" unless start_time < end_time + raise ArgumentError.new"number_of_rooms must be >= 1 && <= 5" unless number_of_rooms >= 1 && number_of_rooms <=5 + available_rooms = view_available_rooms_on(start_time: start_time, end_time: end_time) #makes sure dates dont overlap and block status is :na + raise ArgumentError.new"not enough available rooms for this date range" if available_rooms.length < number_of_rooms + block_id = generate_block_id.to_sym + block = Block.new({rooms: [], start_time: start_time, end_time: end_time, discount: discount, block: block_id }) + number_of_rooms.times do |i| + available_rooms[i].block = block_id + block.rooms << available_rooms[i] + end + @blocks << block + binding.pry + return @blocks + end #need to come back here and create i instances of reservation..but first need to change Reservation.rooms to hold an Integer instead of Array # available_rooms.each do |room| # @all_rooms.each do |room| @@ -90,6 +113,8 @@ def add_reservation(start_time: Date.now, end_time: Date.now + 1, number_of_room # # view a list of rooms that are not reserved(aka available) for a given date range + + # If a room is set aside in a block, it is not available for reservation by the general public, nor can it be included in another block def view_available_rooms_on(start_time: Date.now, end_time: Date.now + 1) raise ArgumentError.new"start_time must be before end_time" unless start_time < end_time #another test to create is making sure its an instance of Date diff --git a/spec/block_spec.rb b/spec/block_spec.rb new file mode 100644 index 000000000..ed259c3f7 --- /dev/null +++ b/spec/block_spec.rb @@ -0,0 +1,28 @@ +require_relative 'spec_helper' +require_relative '../lib/block' + +describe 'Block class' do + + describe "#initialize" do + before do + attributes = {rooms: [] ,start_time: Date.new(2018,8,1),end_time: Date.new(2018,9,1), discount: 10, block: :A} + @block = Block.new(attributes) + end + + it 'is an instance of Reservation' do + expect(@block).must_be_kind_of Block + end + + it "is set up for specific attributes and data types" do + [:rooms, :start_time, :end_time, :discount, :block].each do |attribute| + expect(@block).must_respond_to attribute + end + + expect(@block.rooms).must_be_kind_of Array + expect(@block.start_time).must_be_kind_of Date + expect(@block.end_time).must_be_kind_of Date + expect(@block.discount).must_be_kind_of Integer + expect(@block.block).must_be_kind_of Symbol + end + end +end diff --git a/spec/tracking_system_spec.rb b/spec/tracking_system_spec.rb index 194164a76..226918dfb 100644 --- a/spec/tracking_system_spec.rb +++ b/spec/tracking_system_spec.rb @@ -2,6 +2,7 @@ require_relative '../lib/tracking_system' require_relative '../lib/room' require_relative '../lib/reservation' +require_relative '../lib/block' require 'pry' describe 'TrackingSystem class' do @@ -24,6 +25,10 @@ expect(@tracker.reservations).must_be_instance_of Array end + it 'has a list of blocks' do + expect(@tracker.blocks).must_be_instance_of Array + end + end describe "access the list of all 20 rooms in the hotel" do @@ -70,6 +75,54 @@ end end #end of describe + describe "#add_block" do + before do + @tracker = TrackingSystem.new + end + + #this test is always passing no matter what, its not working + # it "raises ArgumentError if discount-rate is not an Integer" do + # expect{@tracker.add_block(start_time: Date.new(2018,8,1), end_time: Date.new(2018,8,5), rooms: 1, discount: "hi")}.must_raise ArgumentError + # end + # + # it "raises ArgumentError if rooms > 5 or <= 1" do + # @tracker = TrackingSystem.new + # expect{@tracker.add_block(start_time: Date.new(2018,8,5),end_time:Date.new(2018,9,5), rooms: 20, discount: 10)}.must_raise ArgumentError + # end + + it "returns an array of blocks" do + @block = @tracker.add_block(start_time: Date.new(2018,8,1), end_time: Date.new(2018,8,25), number_of_rooms:5) + @block = @tracker.add_block(start_time: Date.new(2018,7,1), end_time: Date.new(2018,7,10), number_of_rooms:4) + expect(@tracker.blocks).must_be_kind_of Array + end + + it "raises ArgumentError if start_time is > end_time" do + @tracker = TrackingSystem.new + expect{@tracker.add_block(start_time: Date.new(2018,10,5),end_time:Date.new(2018,9,5))}.must_raise ArgumentError + end + end + + # def add_block(start_time: Date.today + 7, end_time: Date.today.next_month, number_of_rooms: 5, discount: 10) + # # raise ArgumentError unless discount.instance_of? Integer + # raise ArgumentError.new"start_time must be before end_time" unless start_time < end_time + # raise ArgumentError.new"number_of_rooms must be >= 1 && <= 5" unless number_of_rooms >= 1 && number_of_rooms <=5 + # available_rooms = view_available_rooms_on(start_time: start_time, end_time: end_time) #makes sure dates dont overlap and block status is :na + # raise ArgumentError.new"not enough available rooms for this date range" if available_rooms.length < number_of_rooms + # block_id = generate_block_id + # block = Block.new({rooms: [], start_time: start_time, end_time: end_time, discount: discount, block: block_id }) + # number_of_rooms.times do |i| + # available_rooms[i].block = block_id + # block.rooms << available_rooms[i] + # end + # @blocks << block + # return @blocks + # end + + + + + + @@ -128,7 +181,7 @@ expect{@tracker.view_available_rooms_on(start_time: Date.new(2018,10,5),end_time:Date.new(2018,9,5))}.must_raise ArgumentError end - # it "ensures that room is not in a block" do + # it "ensures that room is not in a block" do # end end @@ -186,7 +239,8 @@ it "returns a list" do expect(@tracker.view_all_rooms).must_be_kind_of Array end - end + + end #end of class method From d02dec3159dd9d4767067b205458c714dc5992ca Mon Sep 17 00:00:00 2001 From: Kay Date: Sat, 8 Sep 2018 17:37:55 -0700 Subject: [PATCH 35/46] wrote tests for #rooms_available_in_block and fixed bugs in method --- lib/block.rb | 4 +- lib/tracking_system.rb | 28 ++++++- spec/tracking_system_spec.rb | 145 ++++++++++++++++++----------------- 3 files changed, 101 insertions(+), 76 deletions(-) diff --git a/lib/block.rb b/lib/block.rb index 5a68f5cf3..ef43482e7 100644 --- a/lib/block.rb +++ b/lib/block.rb @@ -1,6 +1,6 @@ class Block - attr_reader :rooms, :start_time, :end_time, :discount - attr_accessor :block + attr_reader :start_time, :end_time, :rooms + attr_accessor :block, :discount #need to write a test for these def initialize(attributes) diff --git a/lib/tracking_system.rb b/lib/tracking_system.rb index 0e98c4863..e3536141e 100644 --- a/lib/tracking_system.rb +++ b/lib/tracking_system.rb @@ -68,7 +68,7 @@ def generate_block_id end def add_block(start_time: Date.today + 7, end_time: Date.today.next_month, number_of_rooms: 5, discount: 10) - raise ArgumentError unless discount.instance_of? Integer + raise ArgumentError.new"discount rate must be integer" unless discount.instance_of? Integer raise ArgumentError.new"start_time must be before end_time" unless start_time < end_time raise ArgumentError.new"number_of_rooms must be >= 1 && <= 5" unless number_of_rooms >= 1 && number_of_rooms <=5 available_rooms = view_available_rooms_on(start_time: start_time, end_time: end_time) #makes sure dates dont overlap and block status is :na @@ -80,7 +80,6 @@ def add_block(start_time: Date.today + 7, end_time: Date.today.next_month, numbe block.rooms << available_rooms[i] end @blocks << block - binding.pry return @blocks end #need to come back here and create i instances of reservation..but first need to change Reservation.rooms to hold an Integer instead of Array @@ -161,6 +160,31 @@ def ranges_overlap?(r1, r2) r1.include?(r2.first) || r2.include?(r1.first) end + def rooms_available_in_block(block_id) #block id + available_rooms = 0 + @blocks.each do |a_block| + if a_block.block == block_id + a_block.rooms.each do |room| + if room.reserved_dates.empty? + available_rooms += 1 + end + end + end + end + return available_rooms + end + + + + # As an administrator, I can check whether a given block has any rooms available + # #if block.rooms.each do |room|, if room.reservations.empty? == true , + # available_rooms = [], available_rooms << room , + # return available_rooms.length (returns the number of rooms still available in this block + + + + + end #class end diff --git a/spec/tracking_system_spec.rb b/spec/tracking_system_spec.rb index 226918dfb..16c1304a3 100644 --- a/spec/tracking_system_spec.rb +++ b/spec/tracking_system_spec.rb @@ -75,81 +75,10 @@ end end #end of describe - describe "#add_block" do - before do - @tracker = TrackingSystem.new - end - - #this test is always passing no matter what, its not working - # it "raises ArgumentError if discount-rate is not an Integer" do - # expect{@tracker.add_block(start_time: Date.new(2018,8,1), end_time: Date.new(2018,8,5), rooms: 1, discount: "hi")}.must_raise ArgumentError - # end - # - # it "raises ArgumentError if rooms > 5 or <= 1" do - # @tracker = TrackingSystem.new - # expect{@tracker.add_block(start_time: Date.new(2018,8,5),end_time:Date.new(2018,9,5), rooms: 20, discount: 10)}.must_raise ArgumentError - # end - - it "returns an array of blocks" do - @block = @tracker.add_block(start_time: Date.new(2018,8,1), end_time: Date.new(2018,8,25), number_of_rooms:5) - @block = @tracker.add_block(start_time: Date.new(2018,7,1), end_time: Date.new(2018,7,10), number_of_rooms:4) - expect(@tracker.blocks).must_be_kind_of Array - end - - it "raises ArgumentError if start_time is > end_time" do - @tracker = TrackingSystem.new - expect{@tracker.add_block(start_time: Date.new(2018,10,5),end_time:Date.new(2018,9,5))}.must_raise ArgumentError - end - end - - # def add_block(start_time: Date.today + 7, end_time: Date.today.next_month, number_of_rooms: 5, discount: 10) - # # raise ArgumentError unless discount.instance_of? Integer - # raise ArgumentError.new"start_time must be before end_time" unless start_time < end_time - # raise ArgumentError.new"number_of_rooms must be >= 1 && <= 5" unless number_of_rooms >= 1 && number_of_rooms <=5 - # available_rooms = view_available_rooms_on(start_time: start_time, end_time: end_time) #makes sure dates dont overlap and block status is :na - # raise ArgumentError.new"not enough available rooms for this date range" if available_rooms.length < number_of_rooms - # block_id = generate_block_id - # block = Block.new({rooms: [], start_time: start_time, end_time: end_time, discount: discount, block: block_id }) - # number_of_rooms.times do |i| - # available_rooms[i].block = block_id - # block.rooms << available_rooms[i] - # end - # @blocks << block - # return @blocks - # end - - - - - - # it "adds a hash of start/end time to each room.reserved_dates array" do - # end - - # it "only selects rooms that are available " do - # expect(@reservation.) - # end - # - # it "creates reservations for the number_of_rooms requested " do - # end - # - # it "iterates through @all_rooms to find the first available rooms" do - # end - # - # it "raises ArgumentError if inside @all_rooms no room is available on this date range" do - # end - # - # it "creates a new instance of Reservation" do - # @reservation = @tracker.add_reservation(start_time: Date.new(2018,8,1), end_time: Date.new(2018,8,25), rooms: 1) #<---1 is the number of rooms - # - # expect(@reservation).must_be_kind_of Reservation - # end - - - describe "#view_available_rooms_on specifc date range" do before do @tracker = TrackingSystem.new @@ -183,7 +112,6 @@ # it "ensures that room is not in a block" do # end - end @@ -243,4 +171,77 @@ + describe "#add_block" do + before do + @tracker = TrackingSystem.new + end + + #this test is always passing no matter what, its not working + # it "raises ArgumentError if discount-rate is not an Integer" do + # expect{@tracker.add_block(start_time: Date.new(2018,8,1), end_time: Date.new(2018,8,5), rooms: 1, discount: "hi")}.must_raise ArgumentError + # end + # + # it "raises ArgumentError if rooms > 5 or <= 1" do + # @tracker = TrackingSystem.new + # expect{@tracker.add_block(start_time: Date.new(2018,8,5),end_time:Date.new(2018,9,5), rooms: 20, discount: 10)}.must_raise ArgumentError + # end + + it "returns an array of blocks" do + @block = @tracker.add_block(start_time: Date.new(2018,8,1), end_time: Date.new(2018,8,25), number_of_rooms:5) + @block = @tracker.add_block(start_time: Date.new(2018,7,1), end_time: Date.new(2018,7,10), number_of_rooms:4) + expect(@tracker.blocks).must_be_kind_of Array + end + + it "raises ArgumentError if start_time is > end_time" do + @tracker = TrackingSystem.new + expect{@tracker.add_block(start_time: Date.new(2018,10,5),end_time:Date.new(2018,9,5))}.must_raise ArgumentError + end + end + + + + + + + describe "#rooms_available_in_block" do + before do + @tracker = TrackingSystem.new + @block = @tracker.add_block(start_time: Date.new(2018,10,5), end_time: Date.new(2018,10,10), number_of_rooms:5) + @blockid = @tracker.blocks[0].block + end + + it "returns the number of rooms available in a block" do + @reservation1 = @tracker.add_reservation(start_time: Date.new(2018,10,5), end_time: Date.new(2018,10,10), number_of_rooms:1) + @reservation2 = @tracker.add_reservation(start_time: Date.new(2018,10,5), end_time: Date.new(2018,10,10), number_of_rooms:1) + @reservation3 = @tracker.add_reservation(start_time: Date.new(2018,10,5), end_time: Date.new(2018,10,10), number_of_rooms:1) + expect(@tracker.rooms_available_in_block(@blockid)).must_equal 2 + end + end + + + + end #end of class method + + +# it "adds a hash of start/end time to each room.reserved_dates array" do +# end + +# it "only selects rooms that are available " do +# expect(@reservation.) +# end +# +# it "creates reservations for the number_of_rooms requested " do +# end +# +# it "iterates through @all_rooms to find the first available rooms" do +# end +# +# it "raises ArgumentError if inside @all_rooms no room is available on this date range" do +# end +# +# it "creates a new instance of Reservation" do +# @reservation = @tracker.add_reservation(start_time: Date.new(2018,8,1), end_time: Date.new(2018,8,25), rooms: 1) #<---1 is the number of rooms +# +# expect(@reservation).must_be_kind_of Reservation +# end From b851b5e9a8d4531627971bb5308ddcb3cce6a441 Mon Sep 17 00:00:00 2001 From: Kay Date: Sun, 9 Sep 2018 10:53:41 -0700 Subject: [PATCH 36/46] fixed bug in #rooms_available_in_block --- lib/tracking_system.rb | 229 ++++++++++++++++------------------- spec/tracking_system_spec.rb | 44 ++++++- 2 files changed, 148 insertions(+), 125 deletions(-) diff --git a/lib/tracking_system.rb b/lib/tracking_system.rb index e3536141e..0e16c1710 100644 --- a/lib/tracking_system.rb +++ b/lib/tracking_system.rb @@ -15,26 +15,6 @@ def initialize @blocks = [] end - # def total_cost_of_reservation(room_num) - # raise ArgumentError.new"There are no reservations" if @reservations.empty? == true - # @reservations.each do |reservation| - # if reservation.room_num == nil - # raise ArgumentError.new"Room number #{room_num} has no current reservations" - # else reservation.room_num == room_num - # - # #insert part where calculation happens - # return reservation.price - # end - # end - # end - ########################################################### - #this method doesn't have tests yet because i'm not sure if im going to combine it with the method above - def total_cost_of_reservation(reservation) - raise ArgumentError.new"#{reservation} is an invalid argument type" unless reservation.instance_of? Reservation - return reservation.total_cost - end - ########################################################### - def add_rooms all_rooms = [] @@ -46,74 +26,39 @@ def add_rooms return all_rooms end - #access the list of all of the rooms in the hotel + # Wave1 (1~4) + # 1. As an administrator, I can access the list of all of the rooms in the hotel def view_all_rooms return @all_rooms end - #reserve an available room for a given date range - #this method ONLY adds reservations for rooms that aren't in a block - def add_reservation(start_time: Date.now, end_time: Date.now + 1, number_of_rooms: 1) - available_rooms = view_available_rooms_on(start_time: start_time, end_time: end_time) #<--returns an array of available rooms that also aren't in a block - raise ArgumentError.new"Not enough rooms available on those dates" if available_rooms.length < number_of_rooms - number_of_rooms.times do |i| - @reservations << Reservation.new({room_num: available_rooms[i].room_num, start_time: start_time, end_time: end_time, price: 200.00}) - available_rooms[i].reserved_dates << {start_time: start_time, end_time: end_time} + # 2. (wave 1 num 2 is the same as wave 2 num 2) + # 3. As an administrator, I can access the list of reservations for a specific date + def view_reservations_on(date) + raise ArgumentError.new"#{date} must be instance of Date" unless date.instance_of?(Date) + all_reservations = [] + @reservations.each do |reservation| + if (reservation.start_time...reservation.end_time).include?(date) #maybe this boolean can be deleted + all_reservations << reservation + end + end + if all_reservations.empty? + raise ArgumentError.new"There are no reservations on #{date}" + else + return all_reservations end - @reservations end - - def generate_block_id - (0..3).map { (65 + rand(26)).chr }.join + # 4. As an administrator, I can get the total cost for a given reservation + def total_cost_of_reservation(reservation) + raise ArgumentError.new"#{reservation} is an invalid argument type" unless reservation.instance_of? Reservation + return reservation.total_cost end - def add_block(start_time: Date.today + 7, end_time: Date.today.next_month, number_of_rooms: 5, discount: 10) - raise ArgumentError.new"discount rate must be integer" unless discount.instance_of? Integer - raise ArgumentError.new"start_time must be before end_time" unless start_time < end_time - raise ArgumentError.new"number_of_rooms must be >= 1 && <= 5" unless number_of_rooms >= 1 && number_of_rooms <=5 - available_rooms = view_available_rooms_on(start_time: start_time, end_time: end_time) #makes sure dates dont overlap and block status is :na - raise ArgumentError.new"not enough available rooms for this date range" if available_rooms.length < number_of_rooms - block_id = generate_block_id.to_sym - block = Block.new({rooms: [], start_time: start_time, end_time: end_time, discount: discount, block: block_id }) - number_of_rooms.times do |i| - available_rooms[i].block = block_id - block.rooms << available_rooms[i] - end - @blocks << block - return @blocks - end - #need to come back here and create i instances of reservation..but first need to change Reservation.rooms to hold an Integer instead of Array - # available_rooms.each do |room| - # @all_rooms.each do |room| - # number_of_rooms.times do - # if room.reserved_dates.empty? - # reservation = Reservation.new({room: [room.room_num], start_time: start_time, end_time: end_time, price: 200.0}) - # @reservations << reservation - # room.reserved_dates << {start_time: start_time, end_time: end_time} - # else - # room.reserved_dates.each do |reserved_dates| - # if !(reserved_dates[:start_time]...reserved_dates[:end_time]).include?(start_time) #&& room.block == :NA - # reservation = Reservation.new({room: not array anymore[room], start_time: start_time, end_time: end_time, price: 200.0}) - # @reservations << reservation - # room.reserved_dates << {start_time: start_time, end_time: end_time} - # else raise ArgumentError.new"There is no room available on that date" - # end - # end - # end - # return reservation - # end - # end - # end - # ********************************************************************************************************* - #block method idea - #this method below is going to return a value that can into the Block.rooms << (aka list of rooms in a block), but how/when do i assign these available rooms a block id? Ah i think that i will - # i will create another method called create_block, call view_available_rooms_on(start_time, end_time) within that - # and then in that create_block method, update a block status for each room (aka block :A) corresponding to the Block's ID (id: A,B,C etc..) - # - # view a list of rooms that are not reserved(aka available) for a given date range - # If a room is set aside in a block, it is not available for reservation by the general public, nor can it be included in another block + + # Wave2 (1~2) + # 1. As an administrator, I can view a list of rooms that are not reserved for a given date range def view_available_rooms_on(start_time: Date.now, end_time: Date.now + 1) raise ArgumentError.new"start_time must be before end_time" unless start_time < end_time #another test to create is making sure its an instance of Date @@ -139,20 +84,20 @@ def view_available_rooms_on(start_time: Date.now, end_time: Date.now + 1) end end - #access the list of reservations for a specific date <---not date range - def view_reservations_on(date) - raise ArgumentError.new"#{date} must be instance of Date" unless date.instance_of?(Date) - all_reservations = [] - @reservations.each do |reservation| - if (reservation.start_time...reservation.end_time).include?(date) #maybe this boolean can be deleted - all_reservations << reservation - end - end - if all_reservations.empty? - raise ArgumentError.new"There are no reservations on #{date}" - else - return all_reservations + #2. As an administrator, I can reserve an available room for a given date range + #this method ONLY adds reservations for rooms that aren't in a block + def add_reservation(start_time: Date.now, end_time: Date.now + 1, number_of_rooms: 1) + available_rooms = view_available_rooms_on(start_time: start_time, end_time: end_time) #<--returns an array of available rooms that also aren't in a block + raise ArgumentError.new"Not enough rooms available on those dates" if available_rooms.length < number_of_rooms + number_of_rooms.times do |i| + @reservations << Reservation.new({room_num: available_rooms[i].room_num, start_time: start_time, end_time: end_time, price: 200.00}) + available_rooms[i].reserved_dates << {start_time: start_time, end_time: end_time} end + @reservations + end + + def generate_block_id + (0..3).map { (65 + rand(26)).chr }.join end @@ -160,27 +105,84 @@ def ranges_overlap?(r1, r2) r1.include?(r2.first) || r2.include?(r1.first) end + # Wave3 (1~3) + # 1. As an administrator, I can create a block of rooms + # A block can contain a maximum of 5 rooms + # When a room is reserved from a block of rooms, the reservation dates will always match the date range of the block + # To create a block you need a date range, collection of rooms and a discounted room rate + # The collection of rooms should only include rooms that are available for the given date range + # If a room is set aside in a block, it is not available for reservation by the general public, nor can it be included in another block + # All of the availability checking logic from Wave 2 should now respect room blocks as well as individual reservations + def add_block(start_time: Date.today + 7, end_time: Date.today.next_month, number_of_rooms: 5, discount: 10) + # raise ArgumentError.new"discount rate must be integer" unless discount.instance_of? Integer + # raise ArgumentError.new"start_time must be before end_time" unless start_time < end_time + raise ArgumentError.new"number_of_rooms must be >= 1 && <= 5" unless number_of_rooms >= 1 && number_of_rooms <=5 + available_rooms = view_available_rooms_on(start_time: start_time, end_time: end_time) #makes sure dates dont overlap and block status is :na + raise ArgumentError.new"not enough available rooms for this date range" if available_rooms.length < number_of_rooms + block_id = generate_block_id.to_sym + block = Block.new({rooms: [], start_time: start_time, end_time: end_time, discount: discount, block: block_id }) + number_of_rooms.times do |i| + available_rooms[i].block = block_id #assigns each room to this new block (by block_id) + block.rooms << available_rooms[i] #shovels each room into the block + end + @blocks << block #puts new block in list of blocks + return @blocks + end + + def retrieve_block_dates(block_id) + dates_hash = {} + range_array = [] + @blocks.each do |block| + if block.block == block_id + dates_hash[:start_time] = block.start_time + dates_hash[:end_time] = block.end_time + range_array = (dates_hash[:start_time]...dates_hash[:end_time]).to_a + end + end + raise ArgumentError if dates_hash.empty? + return range_array #returns array of teh date range + end + # 2. As an administrator, I can check whether a given block has any rooms available def rooms_available_in_block(block_id) #block id - available_rooms = 0 - @blocks.each do |a_block| - if a_block.block == block_id - a_block.rooms.each do |room| - if room.reserved_dates.empty? - available_rooms += 1 + raise ArgumentError.new"#{block_id} must be a Symbol" unless block_id.instance_of? Symbol + available_rooms = [] + @blocks.each do |individual_block| + if individual_block.block == block_id + individual_block.rooms.each do |room| + room.reserved_dates.each do |dates_hash| + if (dates_hash[:start_time]...dates_hash[:end_time]).to_a != retrieve_block_dates(block_id) + available_rooms << room + end end end end + return available_rooms end - return available_rooms + end + #i can combine this method with making a reservation if it returns a list of rooms available + #a block has a : start, end, rooms, discount, block(id) + #how do i get the discount rate applied? + # 3. As an administrator, I can reserve a room from within a block of rooms + def add_reservation_in_block(start_time: Date.now, end_time: Date.now + 1, number_of_rooms: 1, block: :NA) #block(id) defaults to :NA already but just clarifying here + available_rooms = rooms_available_in_block(block) #<--returns an array of available rooms in this sepcifci block + raise ArgumentError.new"Not enough rooms available on those dates" if available_rooms.length < number_of_rooms + #call method taht gets block discount here + discount = retrieve_block_discount(block) + number_of_rooms.times do |i| + @reservations << Reservation.new({room_num: available_rooms[i].room_num, start_time: start_time, end_time: end_time, price: 200.00 -(200.00 * discount)}) + available_rooms[i].reserved_dates << {start_time: start_time, end_time: end_time} + end + @reservations end - - - # As an administrator, I can check whether a given block has any rooms available - # #if block.rooms.each do |room|, if room.reservations.empty? == true , - # available_rooms = [], available_rooms << room , - # return available_rooms.length (returns the number of rooms still available in this block - + def retrieve_block_discount(block_id) + @blocks.each do |individual_block| + if individual_block.block == block_id + return individual_block.discount / 100 + end + end + end + #create helper method that finds the block discount rate by block id? @@ -188,26 +190,9 @@ def rooms_available_in_block(block_id) #block id end #class end -# - - - ############################# ############################# ############################# # private #helper methods below # def view_two_dates_as_range() #<--put params {checkin_time: checkin, checkout_time: checkout} # def check_if_rooms_available_on(date_range) <--or does the view_available_rooms_on() already do this pretty much? ############################# ############################# ############################# - -#think about how each room can be reserved thru time - -# The hotel has 20 rooms, and they are numbered 1 through 20 -# Every room is identical, and a room always costs $200/night -# The last day of a reservation is the checkout day, so the -# guest should not be charged for that night -# For this wave, any room can be reserved at any time, and -# you don't need to check whether reservations conflict with each other (this will come in wave 2!) - -# def overlap?(x,y) -# (x.first - y.end) * (y.first - x.end) > 0 -# end diff --git a/spec/tracking_system_spec.rb b/spec/tracking_system_spec.rb index 16c1304a3..d69c4ab8b 100644 --- a/spec/tracking_system_spec.rb +++ b/spec/tracking_system_spec.rb @@ -79,11 +79,14 @@ - describe "#view_available_rooms_on specifc date range" do + describe "#view_available_rooms_on" do before do @tracker = TrackingSystem.new end + it "makes sure date ranges dont overlap " do + end + it "returns an array" do @tracker = TrackingSystem.new @available_rooms = @tracker.view_available_rooms_on(start_time: Date.new(2018,8,1),end_time:Date.new(2018,9,5)) @@ -210,16 +213,51 @@ @blockid = @tracker.blocks[0].block end - it "returns the number of rooms available in a block" do + it "returns list of rooms that are available in a block" do @reservation1 = @tracker.add_reservation(start_time: Date.new(2018,10,5), end_time: Date.new(2018,10,10), number_of_rooms:1) @reservation2 = @tracker.add_reservation(start_time: Date.new(2018,10,5), end_time: Date.new(2018,10,10), number_of_rooms:1) @reservation3 = @tracker.add_reservation(start_time: Date.new(2018,10,5), end_time: Date.new(2018,10,10), number_of_rooms:1) - expect(@tracker.rooms_available_in_block(@blockid)).must_equal 2 + expect(@tracker.rooms_available_in_block(@blockid)).must_be_kind_of Array + end + + it "raises ArgumentError unless argument is instance of Symbol" do + expect{@tracker.rooms_available_in_block("Hi")}.must_raise ArgumentError + end + end + + describe "#add_reservation_in_block" do + before do + @tracker = TrackingSystem.new + @block = @tracker.add_block(start_time: Date.new(2018,10,5), end_time: Date.new(2018,10,10), number_of_rooms:5) + @blockid = @tracker.blocks[0].block + end + + it "Adds a reservation to each room's list of reservations" do + # expect(@tracker.add_reservation_in_block) end end + describe "#retrieve_block_discount" do + it "returns a block's discount rate" do + + end + + it "raises ArgumentError unless argument is a Symbol" do + end + end + # def retrieve_block_discount(block_id) + # @blocks.each do |individual_block| + # if individual_block.block == block_id + # return individual_block.discount / 100 + # end + # end + # end + + + + end #end of class method From 5e012170abb113ea2fa0757c6e3ba5bfb54968e6 Mon Sep 17 00:00:00 2001 From: Kay Date: Sun, 9 Sep 2018 12:29:13 -0700 Subject: [PATCH 37/46] cleaned up some comments from tests/lib, writing more tests for methods --- lib/reservation.rb | 14 +--- lib/room.rb | 11 --- lib/tracking_system.rb | 9 ++- spec/reservation_spec.rb | 18 +---- spec/tracking_system_spec.rb | 139 +++++++++++++++++------------------ 5 files changed, 78 insertions(+), 113 deletions(-) diff --git a/lib/reservation.rb b/lib/reservation.rb index a3cfd843a..b67f14509 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -1,9 +1,8 @@ class Reservation attr_reader :room_num, :start_time, :end_time, :price - #need to write a test for these - def initialize(attributes) #attributes is a hash , so theoretically from CSV it would be converted into a hash - @room_num = attributes[:room_num] #the value of room should be an Integer + def initialize(attributes) + @room_num = attributes[:room_num] @start_time = attributes[:start_time] @end_time = attributes[:end_time] @price = attributes[:price] @@ -15,12 +14,3 @@ def total_cost end end - - - - -#when a reservation is made a room number needs to be assigned - -#create a cost method -#def total_cost -# end diff --git a/lib/room.rb b/lib/room.rb index 6029e550e..e0dba1209 100644 --- a/lib/room.rb +++ b/lib/room.rb @@ -8,14 +8,3 @@ def initialize(attributes) @block = attributes[:status] ||= :NA #room should have a letter A~Z to indicate which rooms are with what block, NA is default end end - - -# it "creates a new instance of Reservation" do -# expect(@room.reserve(checkin_time: Date.new(2018,8,1), checkout_time: Date.new(2018,8,25))).must_be_kind_of Reservation -# end - - -#how to return all attributes in a Hash -# # def attributes -# Hash[instance_variables.map { |name, _| [name.to_s.sub!(%r{@}, '').to_sym, instance_variable_get(name)] }] -# end diff --git a/lib/tracking_system.rb b/lib/tracking_system.rb index 0e16c1710..a1b02105c 100644 --- a/lib/tracking_system.rb +++ b/lib/tracking_system.rb @@ -35,10 +35,10 @@ def view_all_rooms # 2. (wave 1 num 2 is the same as wave 2 num 2) # 3. As an administrator, I can access the list of reservations for a specific date def view_reservations_on(date) - raise ArgumentError.new"#{date} must be instance of Date" unless date.instance_of?(Date) + raise ArgumentError.new"#{date} must be instance of Date" unless date.instance_of? Date all_reservations = [] @reservations.each do |reservation| - if (reservation.start_time...reservation.end_time).include?(date) #maybe this boolean can be deleted + if (reservation.start_time...reservation.end_time).include?(date) all_reservations << reservation end end @@ -48,9 +48,10 @@ def view_reservations_on(date) return all_reservations end end + # 4. As an administrator, I can get the total cost for a given reservation def total_cost_of_reservation(reservation) - raise ArgumentError.new"#{reservation} is an invalid argument type" unless reservation.instance_of? Reservation + raise ArgumentError.new"#{reservation} must be instance of Reservation" unless reservation.instance_of? Reservation return reservation.total_cost end @@ -70,7 +71,7 @@ def view_available_rooms_on(start_time: Date.now, end_time: Date.now + 1) else room.reserved_dates.each do |dates_hash| #<---date_range could be a hash like {checkin_time: checkin, checkout_time: checkout} if ranges_overlap?((dates_hash[:start_time]...dates_hash[:end_time]).to_a, (start_time..end_time).to_a) == false && room.block == :NA - available_rooms << room + available_rooms << room #not quite sure how to write a test for this conditional, "ensures rooms not in a block", "ensures rooms dates dont overlap" else unavailable_count += 1 end diff --git a/spec/reservation_spec.rb b/spec/reservation_spec.rb index 9c2ba859e..c71cc0d0b 100644 --- a/spec/reservation_spec.rb +++ b/spec/reservation_spec.rb @@ -28,7 +28,7 @@ describe "#total_cost" do before do - attributes = {room_num: 1 ,start_time: Date.new(2018,8,1),end_time: Date.new(2018,8,2),price: 200.23111111} + attributes = {room_num: 1 ,start_time: Date.new(2018,8,1),end_time: Date.new(2018,8,4),price: 200.23111111} @reservation = Reservation.new(attributes) end @@ -37,21 +37,7 @@ end it "rounds the return value to 2 decimals" do - expect(@reservation.total_cost).must_equal 200.23 + expect(@reservation.total_cost).must_equal 600.69 end end - - # def total_cost - # total_cost = 0 - # return total_cost = ((end_time - start_time) * price).round(2) - # end - # - # - # it "checkin_time must be before checkout_time" do - # end - - - - - end diff --git a/spec/tracking_system_spec.rb b/spec/tracking_system_spec.rb index d69c4ab8b..3ac3ecd49 100644 --- a/spec/tracking_system_spec.rb +++ b/spec/tracking_system_spec.rb @@ -6,7 +6,6 @@ require 'pry' describe 'TrackingSystem class' do - ###### WAVE 1 ################################################################## describe "#initialize" do before do @tracker = TrackingSystem.new @@ -31,52 +30,67 @@ end - describe "access the list of all 20 rooms in the hotel" do + + describe "#view_all_rooms" do before do @tracker = TrackingSystem.new end - it 'must return an Array' do - expect(@tracker.all_rooms).must_be_instance_of Array + it "returns a list" do + expect(@tracker.view_all_rooms).must_be_kind_of Array end it 'must contain 20 elements' do - expect(@tracker.all_rooms.length).must_equal 20 + expect(@tracker.view_all_rooms.length).must_equal 20 end it 'must contain only instances of Room' do - expect(@tracker.all_rooms.all? {|room| room.class == Room}).must_equal true + expect(@tracker.view_all_rooms.all? {|room| room.class == Room}).must_equal true end end - describe "#add_reservation" do + + describe "#view_reservations_on(date)" do before do @tracker = TrackingSystem.new - # @room = Room.new({room_num: 1, availability: :available }) end - it "returns an array of reservations" do - @reservations = @tracker.add_reservation(start_time: Date.new(2018,8,1), end_time: Date.new(2018,8,25), number_of_rooms:1) - @reservations = @tracker.add_reservation(start_time: Date.new(2018,8,1), end_time: Date.new(2018,8,25), number_of_rooms:6) - list = @tracker.reservations - expect(@reservations).must_be_kind_of Array + it "returns an array" do + @reservation = @tracker.add_reservation(start_time: Date.new(2018,10,5), end_time: Date.new(2018,10,10), number_of_rooms:1) + @reservation1 = @tracker.add_reservation(start_time: Date.new(2018,10,5), end_time: Date.new(2018,10,10), number_of_rooms:1) + expect(@tracker.view_reservations_on(Date.new(2018,10,5))).must_be_kind_of Array end - - it "raises ArgumentError if there are not enough available-rooms on the requested date" do - expect{@tracker.add_reservation(start_time: Date.new(2018,1,1),end_time:Date.new(2018,1,2),number_of_rooms:350)}.must_raise ArgumentError + #this one is working 3333,10,5 + it "raises an ArgumentError if no reservations are available on given date" do + expect{@tracker.view_reservations_on(Date.new(3333,10,5))}.must_raise ArgumentError end + #this one is not working!!!!!! + it "raises an ArgumentError if given date is not an instance of Date class" do + expect{@tracker.view_reservations_on("some date")}.must_raise ArgumentError + end + end - it "increases the number of reservations in the reservations list" do - @reservations = @tracker.add_reservation(start_time: Date.new(2018,8,1), end_time: Date.new(2018,8,25), number_of_rooms:1) - original_num_of_reservations = @tracker.reservations - @reservations = @tracker.add_reservation(start_time: Date.new(2018,8,1), end_time: Date.new(2018,8,25), number_of_rooms:1) - updated_num_of_reservations = @tracker.reservations - expect(updated_num_of_reservations.length).must_equal 2 + + + describe "#total_cost_of_reservation" do + before do + @tracker = TrackingSystem.new + attributes = {room_num: 1 ,start_time: Date.new(2018,8,1),end_time: Date.new(2018,9,1),price: 200.00} + @reservation = Reservation.new(attributes) end - end #end of describe + it "raises ArgumentError unless argument is an instance of Reservation" do + expect{@tracker.total_cost_of_reservation("hello there!")}.must_raise ArgumentError + end + it "returns an instance of a Float" do + expect(@tracker.total_cost_of_reservation(@reservation)).must_be_kind_of Float + end + it "returns a postive number" do + expect(@tracker.total_cost_of_reservation(@reservation)).must_equal 6200.00 + end + end describe "#view_available_rooms_on" do @@ -84,9 +98,12 @@ @tracker = TrackingSystem.new end - it "makes sure date ranges dont overlap " do + it "raises ArgumentError if start_time is > end_time" do + @tracker = TrackingSystem.new + expect{@tracker.view_available_rooms_on(start_time: Date.new(2018,10,5),end_time:Date.new(2018,9,5))}.must_raise ArgumentError end + it "returns an array" do @tracker = TrackingSystem.new @available_rooms = @tracker.view_available_rooms_on(start_time: Date.new(2018,8,1),end_time:Date.new(2018,9,5)) @@ -99,78 +116,64 @@ expect(@available_rooms[0]).must_be_kind_of Room end - it "raises ArgumentError if no rooms are available on these dates" do + it "raises ArgumentError if no rooms are available on these dates(aka they're in a block or reserved)" do @tracker = TrackingSystem.new @tracker.all_rooms.each do |room| room.reserved_dates << {start_time: Date.new(2018,1,1), end_time: Date.new(2018,1,2)} end - #need to create add_reservation and then call it 20 times on the same date then the 21st time it'll raise an error expect{@tracker.view_available_rooms_on(start_time: Date.new(2018,1,1),end_time:Date.new(2018,1,2))}.must_raise ArgumentError end - it "raises ArgumentError if start_time is > end_time" do - @tracker = TrackingSystem.new - expect{@tracker.view_available_rooms_on(start_time: Date.new(2018,10,5),end_time:Date.new(2018,9,5))}.must_raise ArgumentError - end - - # it "ensures that room is not in a block" do + # it "ensures rooms returned aren't in a block" do + # #make a new block for that date, reserve rooms that are in that block, + # # then call view_availa rooms on that date + # @tracker.add_block(start_time: Date.new(2018,1,1), end_time: Date.new(2018,1,29), number_of_rooms: 5, discount: 10) + # block_id = @tracker.blocks[0].block + # @tracker.add_reservation_in_block(start_time: Date.new(2018,1,1), end_time: Date.new(2018,1,29), number_of_rooms: 5, block: block_id) + # available_rooms = @tracker.view_available_rooms_on(start_time: Date.new(2018,1,1), end_time: Date.new(2018,1,29)) + # binding.pry # end + end - describe "#view_reservations_on" do + describe "#add_reservation" do before do @tracker = TrackingSystem.new + # @room = Room.new({room_num: 1, availability: :available }) end - it "returns an array" do - @reservation = @tracker.add_reservation(start_time: Date.new(2018,10,5), end_time: Date.new(2018,10,10), number_of_rooms:1) - @reservation1 = @tracker.add_reservation(start_time: Date.new(2018,10,5), end_time: Date.new(2018,10,10), number_of_rooms:1) - expect(@tracker.view_reservations_on(Date.new(2018,10,5))).must_be_kind_of Array + it "returns an array of reservations" do + @reservations = @tracker.add_reservation(start_time: Date.new(2018,8,1), end_time: Date.new(2018,8,25), number_of_rooms:1) + @reservations = @tracker.add_reservation(start_time: Date.new(2018,8,1), end_time: Date.new(2018,8,25), number_of_rooms:6) + list = @tracker.reservations + expect(@reservations).must_be_kind_of Array end - it "raises an ArgumentError if no reservations are available on given date" do - expect{@tracker.view_reservations_on(Date.new(3333,10,5))}.must_raise ArgumentError + it "raises ArgumentError if there are not enough available-rooms on the requested date" do + expect{@tracker.add_reservation(start_time: Date.new(2018,1,1),end_time:Date.new(2018,1,2),number_of_rooms:350)}.must_raise ArgumentError end - it "raises an ArgumentError if given date is not an instance of Date class" do - expect{@tracker.view_reservations_on(2018,2,1)}.must_raise ArgumentError + it "increases the number of reservations in the reservations list" do + @reservations = @tracker.add_reservation(start_time: Date.new(2018,8,1), end_time: Date.new(2018,8,25), number_of_rooms:1) + original_num_of_reservations = @tracker.reservations + @reservations = @tracker.add_reservation(start_time: Date.new(2018,8,1), end_time: Date.new(2018,8,25), number_of_rooms:1) + updated_num_of_reservations = @tracker.reservations + expect(updated_num_of_reservations.length).must_equal 2 end - end + end #end of describe + + - describe "#total_cost_of_reservation" do - before do - @tracker = TrackingSystem.new - attributes = {room_num: 1 ,start_time: Date.new(2018,8,1),end_time: Date.new(2018,9,1),price: 200.00} - @reservation = Reservation.new(attributes) - end - it "raises ArgumentError unless argument is an instance of Reservation" do - expect{@tracker.total_cost_of_reservation("hello there!")}.must_raise ArgumentError - end - it "returns an instance of a Float" do - expect(@tracker.total_cost_of_reservation(@reservation)).must_be_kind_of Float - end - it "returns a postive number" do - expect(@tracker.total_cost_of_reservation(@reservation)).must_equal 6200.00 - end - end - describe "#view_all_rooms" do - before do - @tracker = TrackingSystem.new - end - it "returns a list" do - expect(@tracker.view_all_rooms).must_be_kind_of Array - end - end @@ -202,10 +205,6 @@ end - - - - describe "#rooms_available_in_block" do before do @tracker = TrackingSystem.new From 9e39a86238851dde480ad2d8d5cb4534f5bc9420 Mon Sep 17 00:00:00 2001 From: Kay Date: Sun, 9 Sep 2018 12:47:24 -0700 Subject: [PATCH 38/46] fixed bug in #add_reservation method so that it does not return rooms that are already in a block --- lib/tracking_system.rb | 24 ++++++++++++++---------- spec/tracking_system_spec.rb | 9 +-------- 2 files changed, 15 insertions(+), 18 deletions(-) diff --git a/lib/tracking_system.rb b/lib/tracking_system.rb index a1b02105c..bda190c4b 100644 --- a/lib/tracking_system.rb +++ b/lib/tracking_system.rb @@ -66,11 +66,11 @@ def view_available_rooms_on(start_time: Date.now, end_time: Date.now + 1) available_rooms = [] unavailable_count = 0 @all_rooms.each do |room| - if room.reserved_dates.empty? + if room.reserved_dates.empty? && room.block == :NA available_rooms << room else room.reserved_dates.each do |dates_hash| #<---date_range could be a hash like {checkin_time: checkin, checkout_time: checkout} - if ranges_overlap?((dates_hash[:start_time]...dates_hash[:end_time]).to_a, (start_time..end_time).to_a) == false && room.block == :NA + if ranges_overlap?((dates_hash[:start_time]...dates_hash[:end_time]).to_a, (start_time..end_time).to_a) == false available_rooms << room #not quite sure how to write a test for this conditional, "ensures rooms not in a block", "ensures rooms dates dont overlap" else unavailable_count += 1 @@ -97,14 +97,6 @@ def add_reservation(start_time: Date.now, end_time: Date.now + 1, number_of_room @reservations end - def generate_block_id - (0..3).map { (65 + rand(26)).chr }.join - end - - - def ranges_overlap?(r1, r2) - r1.include?(r2.first) || r2.include?(r1.first) - end # Wave3 (1~3) # 1. As an administrator, I can create a block of rooms @@ -185,12 +177,24 @@ def retrieve_block_discount(block_id) end #create helper method that finds the block discount rate by block id? + private + + def generate_block_id + (0..3).map { (65 + rand(26)).chr }.join + end + + def ranges_overlap?(r1, r2) + r1.include?(r2.first) || r2.include?(r1.first) + end + end #class end + + ############################# ############################# ############################# # private #helper methods below # def view_two_dates_as_range() #<--put params {checkin_time: checkin, checkout_time: checkout} diff --git a/spec/tracking_system_spec.rb b/spec/tracking_system_spec.rb index 3ac3ecd49..16097496b 100644 --- a/spec/tracking_system_spec.rb +++ b/spec/tracking_system_spec.rb @@ -169,14 +169,6 @@ - - - - - - - - describe "#add_block" do before do @tracker = TrackingSystem.new @@ -195,6 +187,7 @@ it "returns an array of blocks" do @block = @tracker.add_block(start_time: Date.new(2018,8,1), end_time: Date.new(2018,8,25), number_of_rooms:5) @block = @tracker.add_block(start_time: Date.new(2018,7,1), end_time: Date.new(2018,7,10), number_of_rooms:4) + # binding.pry expect(@tracker.blocks).must_be_kind_of Array end From 7083fb00919b8cbc1f4c7fa216044386aff02d7c Mon Sep 17 00:00:00 2001 From: Kay Date: Sun, 9 Sep 2018 13:22:05 -0700 Subject: [PATCH 39/46] added tests to #retrieve_block_dates --- lib/tracking_system.rb | 4 ++- spec/tracking_system_spec.rb | 52 +++++++++++++++++++++++++++++++----- 2 files changed, 48 insertions(+), 8 deletions(-) diff --git a/lib/tracking_system.rb b/lib/tracking_system.rb index bda190c4b..c86cd6343 100644 --- a/lib/tracking_system.rb +++ b/lib/tracking_system.rb @@ -70,7 +70,7 @@ def view_available_rooms_on(start_time: Date.now, end_time: Date.now + 1) available_rooms << room else room.reserved_dates.each do |dates_hash| #<---date_range could be a hash like {checkin_time: checkin, checkout_time: checkout} - if ranges_overlap?((dates_hash[:start_time]...dates_hash[:end_time]).to_a, (start_time..end_time).to_a) == false + if ranges_overlap?((dates_hash[:start_time]...dates_hash[:end_time]).to_a, (start_time..end_time).to_a) == false && room.block == :NA available_rooms << room #not quite sure how to write a test for this conditional, "ensures rooms not in a block", "ensures rooms dates dont overlap" else unavailable_count += 1 @@ -123,6 +123,7 @@ def add_block(start_time: Date.today + 7, end_time: Date.today.next_month, numbe end def retrieve_block_dates(block_id) + raise ArgumentError.new"#{block_id} must be a Symbol" unless block_id.instance_of? Symbol dates_hash = {} range_array = [] @blocks.each do |block| @@ -132,6 +133,7 @@ def retrieve_block_dates(block_id) range_array = (dates_hash[:start_time]...dates_hash[:end_time]).to_a end end + # binding.pry raise ArgumentError if dates_hash.empty? return range_array #returns array of teh date range end diff --git a/spec/tracking_system_spec.rb b/spec/tracking_system_spec.rb index 16097496b..cc9576343 100644 --- a/spec/tracking_system_spec.rb +++ b/spec/tracking_system_spec.rb @@ -195,6 +195,13 @@ @tracker = TrackingSystem.new expect{@tracker.add_block(start_time: Date.new(2018,10,5),end_time:Date.new(2018,9,5))}.must_raise ArgumentError end + + it "modifys each room's block id to the same block id as its Block" do + @block = @tracker.add_block(start_time: Date.new(2018,8,1), end_time: Date.new(2018,8,25), number_of_rooms:5) + @blockid = @tracker.blocks[0].block + @room_block = @tracker.blocks[0].rooms[0].block + expect(@blockid == @room_block).must_equal true + end end @@ -229,6 +236,44 @@ end end + describe "#retrieve_block_dates" do + before do + @tracker = TrackingSystem.new + @block = @tracker.add_block(start_time: Date.new(2018,7,5), end_time: Date.new(2018,7,10), number_of_rooms:5) + + end + + it "returns the date range as an Array " do + @blockid = @tracker.blocks[0].block + @date_range = @tracker.retrieve_block_dates(@blockid) + expect(@date_range).must_be_kind_of Array + end + + it "returns date range that excludes the end date " do + @blockid = @tracker.blocks[0].block + @date_range = @tracker.retrieve_block_dates(@blockid) + expect(@date_range.last).must_equal Date.new(2018,7,9) + end + +#this isn't working either, can't figure out how to make it fail when it should + it "raises ArgumentError unless argument is a Symbol" do + expect{@tracker.retrieve_block_dates("hello")}.must_raise ArgumentError + end + # def retrieve_block_dates(block_id) + # dates_hash = {} + # range_array = [] + # @blocks.each do |block| + # if block.block == block_id + # dates_hash[:start_time] = block.start_time + # dates_hash[:end_time] = block.end_time + # range_array = (dates_hash[:start_time]...dates_hash[:end_time]).to_a + # end + # end + # raise ArgumentError if dates_hash.empty? + # return range_array #returns array of teh date range + # end + end + describe "#retrieve_block_discount" do @@ -239,13 +284,6 @@ it "raises ArgumentError unless argument is a Symbol" do end end - # def retrieve_block_discount(block_id) - # @blocks.each do |individual_block| - # if individual_block.block == block_id - # return individual_block.discount / 100 - # end - # end - # end From 8c93299a5963ca756706c700cfd78d5e09b385d6 Mon Sep 17 00:00:00 2001 From: Kay Date: Sun, 9 Sep 2018 14:00:06 -0700 Subject: [PATCH 40/46] fixed bug in #rooms_available_in_block, it needed a condition for if all rooms were available --- lib/tracking_system.rb | 66 +++++++++++++++--------------- spec/tracking_system_spec.rb | 78 ++++++++++++++++-------------------- 2 files changed, 68 insertions(+), 76 deletions(-) diff --git a/lib/tracking_system.rb b/lib/tracking_system.rb index c86cd6343..239ca5ba8 100644 --- a/lib/tracking_system.rb +++ b/lib/tracking_system.rb @@ -133,7 +133,6 @@ def retrieve_block_dates(block_id) range_array = (dates_hash[:start_time]...dates_hash[:end_time]).to_a end end - # binding.pry raise ArgumentError if dates_hash.empty? return range_array #returns array of teh date range end @@ -142,10 +141,12 @@ def rooms_available_in_block(block_id) #block id raise ArgumentError.new"#{block_id} must be a Symbol" unless block_id.instance_of? Symbol available_rooms = [] @blocks.each do |individual_block| - if individual_block.block == block_id - individual_block.rooms.each do |room| - room.reserved_dates.each do |dates_hash| - if (dates_hash[:start_time]...dates_hash[:end_time]).to_a != retrieve_block_dates(block_id) + if individual_block.block == block_id #if block id matches + individual_block.rooms.each do |room| #find all rooms in that block( none of the rooms are reerved) + if room.reserved_dates.empty? + available_rooms << room + else room.reserved_dates.each do |dates_hash| #see the reserved dates of each room + if (dates_hash[:start_time]...dates_hash[:end_time]).to_a.sort == retrieve_block_dates(block_id).sort #checks if both date ranges are the same available_rooms << room end end @@ -154,40 +155,41 @@ def rooms_available_in_block(block_id) #block id return available_rooms end end - #i can combine this method with making a reservation if it returns a list of rooms available - #a block has a : start, end, rooms, discount, block(id) - #how do i get the discount rate applied? - # 3. As an administrator, I can reserve a room from within a block of rooms - def add_reservation_in_block(start_time: Date.now, end_time: Date.now + 1, number_of_rooms: 1, block: :NA) #block(id) defaults to :NA already but just clarifying here - available_rooms = rooms_available_in_block(block) #<--returns an array of available rooms in this sepcifci block - raise ArgumentError.new"Not enough rooms available on those dates" if available_rooms.length < number_of_rooms - #call method taht gets block discount here - discount = retrieve_block_discount(block) - number_of_rooms.times do |i| - @reservations << Reservation.new({room_num: available_rooms[i].room_num, start_time: start_time, end_time: end_time, price: 200.00 -(200.00 * discount)}) - available_rooms[i].reserved_dates << {start_time: start_time, end_time: end_time} - end - @reservations +end +#i can combine this method with making a reservation if it returns a list of rooms available +#a block has a : start, end, rooms, discount, block(id) +#how do i get the discount rate applied? +# 3. As an administrator, I can reserve a room from within a block of rooms +def add_reservation_in_block(start_time: Date.now, end_time: Date.now + 1, number_of_rooms: 1, block: :NA) #block(id) defaults to :NA already but just clarifying here + available_rooms = rooms_available_in_block(block) #<--returns an array of available rooms in this sepcifci block + raise ArgumentError.new"Not enough rooms available on those dates" if available_rooms.length < number_of_rooms + #call method taht gets block discount here + discount = retrieve_block_discount(block) + number_of_rooms.times do |i| + @reservations << Reservation.new({room_num: available_rooms[i].room_num, start_time: start_time, end_time: end_time, price: 200.00 -(200.00 * discount)}) + available_rooms[i].reserved_dates << {start_time: start_time, end_time: end_time} end + @reservations +end - def retrieve_block_discount(block_id) - @blocks.each do |individual_block| - if individual_block.block == block_id - return individual_block.discount / 100 - end +def retrieve_block_discount(block_id) + @blocks.each do |individual_block| + if individual_block.block == block_id + return individual_block.discount / 100 end end - #create helper method that finds the block discount rate by block id? +end +#create helper method that finds the block discount rate by block id? - private +private - def generate_block_id - (0..3).map { (65 + rand(26)).chr }.join - end +def generate_block_id + (0..3).map { (65 + rand(26)).chr }.join +end - def ranges_overlap?(r1, r2) - r1.include?(r2.first) || r2.include?(r1.first) - end +def ranges_overlap?(r1, r2) + r1.include?(r2.first) || r2.include?(r1.first) +end diff --git a/spec/tracking_system_spec.rb b/spec/tracking_system_spec.rb index cc9576343..ebdc8c8ee 100644 --- a/spec/tracking_system_spec.rb +++ b/spec/tracking_system_spec.rb @@ -187,7 +187,6 @@ it "returns an array of blocks" do @block = @tracker.add_block(start_time: Date.new(2018,8,1), end_time: Date.new(2018,8,25), number_of_rooms:5) @block = @tracker.add_block(start_time: Date.new(2018,7,1), end_time: Date.new(2018,7,10), number_of_rooms:4) - # binding.pry expect(@tracker.blocks).must_be_kind_of Array end @@ -204,78 +203,69 @@ end end - - describe "#rooms_available_in_block" do + describe "#retrieve_block_dates" do before do @tracker = TrackingSystem.new - @block = @tracker.add_block(start_time: Date.new(2018,10,5), end_time: Date.new(2018,10,10), number_of_rooms:5) + @block = @tracker.add_block(start_time: Date.new(2018,7,5), end_time: Date.new(2018,7,10), number_of_rooms:5) + + end + + it "returns the date range as an Array " do @blockid = @tracker.blocks[0].block + @date_range = @tracker.retrieve_block_dates(@blockid) + expect(@date_range).must_be_kind_of Array end - it "returns list of rooms that are available in a block" do - @reservation1 = @tracker.add_reservation(start_time: Date.new(2018,10,5), end_time: Date.new(2018,10,10), number_of_rooms:1) - @reservation2 = @tracker.add_reservation(start_time: Date.new(2018,10,5), end_time: Date.new(2018,10,10), number_of_rooms:1) - @reservation3 = @tracker.add_reservation(start_time: Date.new(2018,10,5), end_time: Date.new(2018,10,10), number_of_rooms:1) - expect(@tracker.rooms_available_in_block(@blockid)).must_be_kind_of Array + it "returns date range that excludes the end date " do + @blockid = @tracker.blocks[0].block + @date_range = @tracker.retrieve_block_dates(@blockid) + expect(@date_range.last).must_equal Date.new(2018,7,9) end - it "raises ArgumentError unless argument is instance of Symbol" do - expect{@tracker.rooms_available_in_block("Hi")}.must_raise ArgumentError + #this isn't working either, can't figure out how to make it fail when it should + it "raises ArgumentError unless argument is a Symbol" do + expect{@tracker.retrieve_block_dates("hello")}.must_raise ArgumentError end end - describe "#add_reservation_in_block" do + + describe "#rooms_available_in_block" do before do @tracker = TrackingSystem.new @block = @tracker.add_block(start_time: Date.new(2018,10,5), end_time: Date.new(2018,10,10), number_of_rooms:5) @blockid = @tracker.blocks[0].block end - it "Adds a reservation to each room's list of reservations" do - # expect(@tracker.add_reservation_in_block) + it "raises ArgumentError unless argument is instance of Symbol" do + expect{@tracker.rooms_available_in_block("Hi")}.must_raise ArgumentError + end + + it "returns list of rooms that are available in a block" do + # add_reservation_in_block(start_time: Date.now, end_time: Date.now + 1, number_of_rooms: 1, block: :NA) + @reservation1 = @tracker.add_reservation_in_block(start_time: Date.new(2018,10,5), end_time: Date.new(2018,10,10), number_of_rooms:1, block: @blockid) + @reservation2 = @tracker.add_reservation_in_block(start_time: Date.new(2018,10,5), end_time: Date.new(2018,10,10), number_of_rooms:1, block: @blockid) + @reservation3 = @tracker.add_reservation_in_block(start_time: Date.new(2018,10,5), end_time: Date.new(2018,10,10), number_of_rooms:1, block: @blockid) + date_range = @tracker.retrieve_block_dates(@blockid) + expect(@tracker.rooms_available_in_block(@blockid)).must_be_kind_of Array end end - describe "#retrieve_block_dates" do + describe "#add_reservation_in_block" do before do @tracker = TrackingSystem.new - @block = @tracker.add_block(start_time: Date.new(2018,7,5), end_time: Date.new(2018,7,10), number_of_rooms:5) - - end - - it "returns the date range as an Array " do - @blockid = @tracker.blocks[0].block - @date_range = @tracker.retrieve_block_dates(@blockid) - expect(@date_range).must_be_kind_of Array - end - - it "returns date range that excludes the end date " do + @block = @tracker.add_block(start_time: Date.new(2018,10,5), end_time: Date.new(2018,10,10), number_of_rooms:5) @blockid = @tracker.blocks[0].block - @date_range = @tracker.retrieve_block_dates(@blockid) - expect(@date_range.last).must_equal Date.new(2018,7,9) end -#this isn't working either, can't figure out how to make it fail when it should - it "raises ArgumentError unless argument is a Symbol" do - expect{@tracker.retrieve_block_dates("hello")}.must_raise ArgumentError + it "Adds a reservation to each room's list of reservations" do + # expect(@tracker.add_reservation_in_block) end - # def retrieve_block_dates(block_id) - # dates_hash = {} - # range_array = [] - # @blocks.each do |block| - # if block.block == block_id - # dates_hash[:start_time] = block.start_time - # dates_hash[:end_time] = block.end_time - # range_array = (dates_hash[:start_time]...dates_hash[:end_time]).to_a - # end - # end - # raise ArgumentError if dates_hash.empty? - # return range_array #returns array of teh date range - # end end + + describe "#retrieve_block_discount" do it "returns a block's discount rate" do From 93077a0358377746925eb4cb9757cd35ee9fd5ac Mon Sep 17 00:00:00 2001 From: Kay Date: Sun, 9 Sep 2018 14:13:46 -0700 Subject: [PATCH 41/46] fixed another bug in #rooms_available_in_block, boolean on line 149 in TrackingSystem class should be != --- lib/tracking_system.rb | 2 +- spec/tracking_system_spec.rb | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/tracking_system.rb b/lib/tracking_system.rb index 239ca5ba8..d9872a075 100644 --- a/lib/tracking_system.rb +++ b/lib/tracking_system.rb @@ -146,7 +146,7 @@ def rooms_available_in_block(block_id) #block id if room.reserved_dates.empty? available_rooms << room else room.reserved_dates.each do |dates_hash| #see the reserved dates of each room - if (dates_hash[:start_time]...dates_hash[:end_time]).to_a.sort == retrieve_block_dates(block_id).sort #checks if both date ranges are the same + if (dates_hash[:start_time]...dates_hash[:end_time]).to_a.sort != retrieve_block_dates(block_id).sort #checks if both date ranges are the same available_rooms << room end end diff --git a/spec/tracking_system_spec.rb b/spec/tracking_system_spec.rb index ebdc8c8ee..4fe58f286 100644 --- a/spec/tracking_system_spec.rb +++ b/spec/tracking_system_spec.rb @@ -243,8 +243,6 @@ it "returns list of rooms that are available in a block" do # add_reservation_in_block(start_time: Date.now, end_time: Date.now + 1, number_of_rooms: 1, block: :NA) @reservation1 = @tracker.add_reservation_in_block(start_time: Date.new(2018,10,5), end_time: Date.new(2018,10,10), number_of_rooms:1, block: @blockid) - @reservation2 = @tracker.add_reservation_in_block(start_time: Date.new(2018,10,5), end_time: Date.new(2018,10,10), number_of_rooms:1, block: @blockid) - @reservation3 = @tracker.add_reservation_in_block(start_time: Date.new(2018,10,5), end_time: Date.new(2018,10,10), number_of_rooms:1, block: @blockid) date_range = @tracker.retrieve_block_dates(@blockid) expect(@tracker.rooms_available_in_block(@blockid)).must_be_kind_of Array end From 63bbd8e24a928c9aadaef789a4c3d48f277367eb Mon Sep 17 00:00:00 2001 From: Kay Date: Sun, 9 Sep 2018 15:35:17 -0700 Subject: [PATCH 42/46] test for #add_reservation_in_block was having issues on line 270 didn't recognize id as a Symbol --- lib/tracking_system.rb | 13 +++++++++---- spec/tracking_system_spec.rb | 34 ++++++++++++++++++++++++++++------ 2 files changed, 37 insertions(+), 10 deletions(-) diff --git a/lib/tracking_system.rb b/lib/tracking_system.rb index d9872a075..237a17d7d 100644 --- a/lib/tracking_system.rb +++ b/lib/tracking_system.rb @@ -166,18 +166,23 @@ def add_reservation_in_block(start_time: Date.now, end_time: Date.now + 1, numbe #call method taht gets block discount here discount = retrieve_block_discount(block) number_of_rooms.times do |i| - @reservations << Reservation.new({room_num: available_rooms[i].room_num, start_time: start_time, end_time: end_time, price: 200.00 -(200.00 * discount)}) + # binding.pry + @reservations << Reservation.new({room_num: available_rooms[i].room_num, start_time: start_time, end_time: end_time, price: STANDARD_ROOM_PRICE - (STANDARD_ROOM_PRICE * discount)}) available_rooms[i].reserved_dates << {start_time: start_time, end_time: end_time} end @reservations end def retrieve_block_discount(block_id) - @blocks.each do |individual_block| - if individual_block.block == block_id - return individual_block.discount / 100 + discount = 0 + @blocks.length.times do |i| + if @blocks[i].block == block_id + discount = (@blocks[i].discount / 100) + else + puts "hello " end end + return discount end #create helper method that finds the block discount rate by block id? diff --git a/spec/tracking_system_spec.rb b/spec/tracking_system_spec.rb index 4fe58f286..e8419ee48 100644 --- a/spec/tracking_system_spec.rb +++ b/spec/tracking_system_spec.rb @@ -240,11 +240,12 @@ expect{@tracker.rooms_available_in_block("Hi")}.must_raise ArgumentError end - it "returns list of rooms that are available in a block" do - # add_reservation_in_block(start_time: Date.now, end_time: Date.now + 1, number_of_rooms: 1, block: :NA) + it "returns correct number of rooms that are available in a block" do + @block = @tracker.add_block(start_time: Date.new(2018,10,5), end_time: Date.new(2018,10,10), number_of_rooms:5) + @starting_num_of_availablitilies = (@tracker.rooms_available_in_block(@blockid)).length @reservation1 = @tracker.add_reservation_in_block(start_time: Date.new(2018,10,5), end_time: Date.new(2018,10,10), number_of_rooms:1, block: @blockid) - date_range = @tracker.retrieve_block_dates(@blockid) - expect(@tracker.rooms_available_in_block(@blockid)).must_be_kind_of Array + @updated_num_of_availablitilies = (@tracker.rooms_available_in_block(@blockid)).length + expect(@starting_num_of_availablitilies - @updated_num_of_availablitilies).must_equal 1 end end @@ -255,13 +256,34 @@ @blockid = @tracker.blocks[0].block end - it "Adds a reservation to each room's list of reservations" do - # expect(@tracker.add_reservation_in_block) + # it "Adds a reservation to each room's list of reservations" do + # + # end + + it "raises ArgumentError if the number of rooms requested is greater than the number of available rooms " do + expect{@tracker.add_reservation_in_block(start_time: Date.new(2018,10,5), end_time: Date.new(2018,10,10), number_of_rooms:555, block: @blockid)}.must_raise ArgumentError + end + + it "returns an Array of reservations" do + # @block = @tracker.add_block(start_time: Date.new(2018,10,5), end_time: Date.new(2018,10,10), number_of_rooms:5) + @blockid = @tracker.blocks[0].block + expect(@tracker.add_reservation_in_block(start_time: Date.new(2018,10,5), end_time: Date.new(2018,10,10), number_of_rooms:2, block: @blockid)).must_be_kind_of Array end end + # def add_reservation_in_block(start_time: Date.today, end_time: Date.today + 1, number_of_rooms: 1, block: :NA) #block(id) defaults to :NA already but just clarifying here + # available_rooms = rooms_available_in_block(block) #<--returns an array of available rooms in this sepcifci block + # raise ArgumentError.new"Not enough rooms available on those dates" if available_rooms.length < number_of_rooms + # #call method taht gets block discount here + # discount = retrieve_block_discount(block) + # number_of_rooms.times do |i| + # @reservations << Reservation.new({room_num: available_rooms[i].room_num, start_time: start_time, end_time: end_time, price: 200.00 -(200.00 * discount)}) + # available_rooms[i].reserved_dates << {start_time: start_time, end_time: end_time} + # end + # @reservations + # end describe "#retrieve_block_discount" do From 1a82988ea31dd0979fb365dde40a87868fb26ddd Mon Sep 17 00:00:00 2001 From: Kay Date: Mon, 10 Sep 2018 00:30:21 -0700 Subject: [PATCH 43/46] created refactors.txt --- lib/block.rb | 4 +- lib/room.rb | 4 +- lib/tracking_system.rb | 102 +++++++++------------------ make_reservation_method.rb | 23 ------ refactors.txt | 12 ++++ spec/block_spec.rb | 2 +- spec/test_data/reservations_test.csv | 2 - spec/test_data/rooms_test.csv | 2 - spec/tracking_system_spec.rb | 99 ++++---------------------- 9 files changed, 62 insertions(+), 188 deletions(-) delete mode 100644 make_reservation_method.rb create mode 100644 refactors.txt delete mode 100644 spec/test_data/reservations_test.csv delete mode 100644 spec/test_data/rooms_test.csv diff --git a/lib/block.rb b/lib/block.rb index ef43482e7..09b6a0c6c 100644 --- a/lib/block.rb +++ b/lib/block.rb @@ -1,15 +1,13 @@ class Block attr_reader :start_time, :end_time, :rooms attr_accessor :block, :discount - #need to write a test for these def initialize(attributes) - @rooms = attributes[:rooms] #array of rooms + @rooms = attributes[:rooms] @start_time = attributes[:start_time] @end_time = attributes[:end_time] @discount = attributes[:discount] @block = attributes[:block] ||= :NA end -# I can reserve a room from within a block of rooms end diff --git a/lib/room.rb b/lib/room.rb index e0dba1209..47b7567f6 100644 --- a/lib/room.rb +++ b/lib/room.rb @@ -4,7 +4,7 @@ class Room def initialize(attributes) @room_num = attributes[:room_num] - @reserved_dates = attributes[:reserved_dates] #<--- this should be a list of date_range hashes? like {checkin_time: checkin, checkout_time: checkout} - @block = attributes[:status] ||= :NA #room should have a letter A~Z to indicate which rooms are with what block, NA is default + @reserved_dates = attributes[:reserved_dates] + @block = attributes[:status] ||= :NA end end diff --git a/lib/tracking_system.rb b/lib/tracking_system.rb index 237a17d7d..5bc702b0b 100644 --- a/lib/tracking_system.rb +++ b/lib/tracking_system.rb @@ -26,14 +26,11 @@ def add_rooms return all_rooms end - # Wave1 (1~4) - # 1. As an administrator, I can access the list of all of the rooms in the hotel + def view_all_rooms return @all_rooms end - # 2. (wave 1 num 2 is the same as wave 2 num 2) - # 3. As an administrator, I can access the list of reservations for a specific date def view_reservations_on(date) raise ArgumentError.new"#{date} must be instance of Date" unless date.instance_of? Date all_reservations = [] @@ -49,29 +46,22 @@ def view_reservations_on(date) end end - # 4. As an administrator, I can get the total cost for a given reservation def total_cost_of_reservation(reservation) raise ArgumentError.new"#{reservation} must be instance of Reservation" unless reservation.instance_of? Reservation return reservation.total_cost end - - - - # Wave2 (1~2) - # 1. As an administrator, I can view a list of rooms that are not reserved for a given date range def view_available_rooms_on(start_time: Date.now, end_time: Date.now + 1) raise ArgumentError.new"start_time must be before end_time" unless start_time < end_time - #another test to create is making sure its an instance of Date available_rooms = [] unavailable_count = 0 @all_rooms.each do |room| if room.reserved_dates.empty? && room.block == :NA available_rooms << room else - room.reserved_dates.each do |dates_hash| #<---date_range could be a hash like {checkin_time: checkin, checkout_time: checkout} + room.reserved_dates.each do |dates_hash| if ranges_overlap?((dates_hash[:start_time]...dates_hash[:end_time]).to_a, (start_time..end_time).to_a) == false && room.block == :NA - available_rooms << room #not quite sure how to write a test for this conditional, "ensures rooms not in a block", "ensures rooms dates dont overlap" + available_rooms << room else unavailable_count += 1 end @@ -85,10 +75,9 @@ def view_available_rooms_on(start_time: Date.now, end_time: Date.now + 1) end end - #2. As an administrator, I can reserve an available room for a given date range - #this method ONLY adds reservations for rooms that aren't in a block + def add_reservation(start_time: Date.now, end_time: Date.now + 1, number_of_rooms: 1) - available_rooms = view_available_rooms_on(start_time: start_time, end_time: end_time) #<--returns an array of available rooms that also aren't in a block + available_rooms = view_available_rooms_on(start_time: start_time, end_time: end_time) raise ArgumentError.new"Not enough rooms available on those dates" if available_rooms.length < number_of_rooms number_of_rooms.times do |i| @reservations << Reservation.new({room_num: available_rooms[i].room_num, start_time: start_time, end_time: end_time, price: 200.00}) @@ -98,27 +87,19 @@ def add_reservation(start_time: Date.now, end_time: Date.now + 1, number_of_room end - # Wave3 (1~3) - # 1. As an administrator, I can create a block of rooms - # A block can contain a maximum of 5 rooms - # When a room is reserved from a block of rooms, the reservation dates will always match the date range of the block - # To create a block you need a date range, collection of rooms and a discounted room rate - # The collection of rooms should only include rooms that are available for the given date range - # If a room is set aside in a block, it is not available for reservation by the general public, nor can it be included in another block - # All of the availability checking logic from Wave 2 should now respect room blocks as well as individual reservations def add_block(start_time: Date.today + 7, end_time: Date.today.next_month, number_of_rooms: 5, discount: 10) - # raise ArgumentError.new"discount rate must be integer" unless discount.instance_of? Integer - # raise ArgumentError.new"start_time must be before end_time" unless start_time < end_time + raise ArgumentError.new"discount rate must be integer" unless discount.instance_of? Integer + raise ArgumentError.new"start_time must be before end_time" unless start_time < end_time raise ArgumentError.new"number_of_rooms must be >= 1 && <= 5" unless number_of_rooms >= 1 && number_of_rooms <=5 - available_rooms = view_available_rooms_on(start_time: start_time, end_time: end_time) #makes sure dates dont overlap and block status is :na + available_rooms = view_available_rooms_on(start_time: start_time, end_time: end_time) raise ArgumentError.new"not enough available rooms for this date range" if available_rooms.length < number_of_rooms block_id = generate_block_id.to_sym block = Block.new({rooms: [], start_time: start_time, end_time: end_time, discount: discount, block: block_id }) number_of_rooms.times do |i| - available_rooms[i].block = block_id #assigns each room to this new block (by block_id) - block.rooms << available_rooms[i] #shovels each room into the block + available_rooms[i].block = block_id + block.rooms << available_rooms[i] end - @blocks << block #puts new block in list of blocks + @blocks << block return @blocks end @@ -134,79 +115,62 @@ def retrieve_block_dates(block_id) end end raise ArgumentError if dates_hash.empty? - return range_array #returns array of teh date range + return range_array end - # 2. As an administrator, I can check whether a given block has any rooms available - def rooms_available_in_block(block_id) #block id + + def rooms_available_in_block(block_id) raise ArgumentError.new"#{block_id} must be a Symbol" unless block_id.instance_of? Symbol available_rooms = [] @blocks.each do |individual_block| - if individual_block.block == block_id #if block id matches - individual_block.rooms.each do |room| #find all rooms in that block( none of the rooms are reerved) + if individual_block.block == block_id + individual_block.rooms.each do |room| if room.reserved_dates.empty? available_rooms << room - else room.reserved_dates.each do |dates_hash| #see the reserved dates of each room - if (dates_hash[:start_time]...dates_hash[:end_time]).to_a.sort != retrieve_block_dates(block_id).sort #checks if both date ranges are the same + else room.reserved_dates.each do |dates_hash| + if (dates_hash[:start_time]...dates_hash[:end_time]).to_a.sort != retrieve_block_dates(block_id).sort available_rooms << room end end end end - return available_rooms end end + return available_rooms end -#i can combine this method with making a reservation if it returns a list of rooms available -#a block has a : start, end, rooms, discount, block(id) -#how do i get the discount rate applied? -# 3. As an administrator, I can reserve a room from within a block of rooms -def add_reservation_in_block(start_time: Date.now, end_time: Date.now + 1, number_of_rooms: 1, block: :NA) #block(id) defaults to :NA already but just clarifying here - available_rooms = rooms_available_in_block(block) #<--returns an array of available rooms in this sepcifci block + + + def add_reservation_in_block(start_time: Date.now, end_time: Date.now + 1, number_of_rooms: 1, block: :NA) + available_rooms = rooms_available_in_block(block) raise ArgumentError.new"Not enough rooms available on those dates" if available_rooms.length < number_of_rooms - #call method taht gets block discount here discount = retrieve_block_discount(block) number_of_rooms.times do |i| - # binding.pry @reservations << Reservation.new({room_num: available_rooms[i].room_num, start_time: start_time, end_time: end_time, price: STANDARD_ROOM_PRICE - (STANDARD_ROOM_PRICE * discount)}) available_rooms[i].reserved_dates << {start_time: start_time, end_time: end_time} end @reservations -end + end -def retrieve_block_discount(block_id) + def retrieve_block_discount(block_id) discount = 0 @blocks.length.times do |i| if @blocks[i].block == block_id discount = (@blocks[i].discount / 100) - else - puts "hello " - end + end # end return discount -end -#create helper method that finds the block discount rate by block id? + end + private -def generate_block_id + def generate_block_id (0..3).map { (65 + rand(26)).chr }.join -end + end -def ranges_overlap?(r1, r2) + def ranges_overlap?(r1, r2) r1.include?(r2.first) || r2.include?(r1.first) -end - + end -end #class end - - - - -############################# ############################# ############################# -# private #helper methods below -# def view_two_dates_as_range() #<--put params {checkin_time: checkin, checkout_time: checkout} -# def check_if_rooms_available_on(date_range) <--or does the view_available_rooms_on() already do this pretty much? - -############################# ############################# ############################# +end diff --git a/make_reservation_method.rb b/make_reservation_method.rb deleted file mode 100644 index f07b2b0a9..000000000 --- a/make_reservation_method.rb +++ /dev/null @@ -1,23 +0,0 @@ -#reserve an available room for a given date range -def make_reservation(start_time: Date.now, end_time: Date.now + 1, number_of_rooms: 1) - # view_available_rooms_on(start_time, end_time) - @all_rooms.each do |room| - number_of_rooms.times do - if room.reserved_dates.empty? - reservation = Reservation.new({rooms: [room.room_num], start_time: start_time, end_time: end_time, price: 200.0}) - @reservations << reservation - room.reserved_dates << {start_time: start_time, end_time: end_time} - else - room.reserved_dates.each do |reserved_dates| - if !(reserved_dates[:start_time]...reserved_dates[:end_time]).include?(start_time) #&& room.block == :NA - reservation = Reservation.new({rooms: [room], start_time: start_time, end_time: end_time, price: 200.0}) - @reservations << reservation - room.reserved_dates << {start_time: start_time, end_time: end_time} - else raise ArgumentError.new"There is no room available on that date" - end - end - end - return reservation - end - end -end diff --git a/refactors.txt b/refactors.txt new file mode 100644 index 000000000..50b3bd124 --- /dev/null +++ b/refactors.txt @@ -0,0 +1,12 @@ +1. name change --> .block method to .block_id in the Block, and Room classes +2. the program logic surrounding blocks may be wrong, not sure if I understood the requirements correctly for: "If a room is set aside in a block, it is not available for reservation by the general public, nor can it be included in another block" + Does this mean if rooms are in a block that they cannot be reserved for that period of time? +3. possibly create a helper method that returns two dates as a range --> def view_two_dates_as_range() #<--put params {checkin_time: checkin, checkout_time: checkout} +4. create a test data file with test data so that I have an visualization of how the information is coming in/should be modified for the program +5. clean up the unnecessary test variables and combine them into a single "before do" block +6. write more edge cases for each method in TrackingSystem +7. for the methods that begin with title "view....", change to "return.." because they're actually returning arrays/values +8. refactor methods in TrackingSystem that know about the structure of Block, Reservation, Room 's instance variables, a lot of them are going into arrays and hashes +9. create helper methods for iterating through block lists and reservation lists and dates +10. create helper methods to lessen the number of loops in methods +11. use enumerables rather than iterating with .each to avoid lengthy loops diff --git a/spec/block_spec.rb b/spec/block_spec.rb index ed259c3f7..878e44edd 100644 --- a/spec/block_spec.rb +++ b/spec/block_spec.rb @@ -9,7 +9,7 @@ @block = Block.new(attributes) end - it 'is an instance of Reservation' do + it 'is an instance of Block' do expect(@block).must_be_kind_of Block end diff --git a/spec/test_data/reservations_test.csv b/spec/test_data/reservations_test.csv deleted file mode 100644 index 6ffb258ba..000000000 --- a/spec/test_data/reservations_test.csv +++ /dev/null @@ -1,2 +0,0 @@ -room_num, checkin_time, checkout_time, price -1, 2018-09-05, 2018-09-13, 200 diff --git a/spec/test_data/rooms_test.csv b/spec/test_data/rooms_test.csv deleted file mode 100644 index e0a3184c4..000000000 --- a/spec/test_data/rooms_test.csv +++ /dev/null @@ -1,2 +0,0 @@ -room_num, block_status -1, AVAILABLE diff --git a/spec/tracking_system_spec.rb b/spec/tracking_system_spec.rb index e8419ee48..ecf5635a9 100644 --- a/spec/tracking_system_spec.rb +++ b/spec/tracking_system_spec.rb @@ -9,7 +9,6 @@ describe "#initialize" do before do @tracker = TrackingSystem.new - # @tracker.all_rooms.each do |room| room.reserved_dates << {checkin_time: Date.new(2018,8,1), checkout_time: Date.new(2018,8,5)} end it 'is an instance of TrackingSystem' do @@ -60,11 +59,12 @@ @reservation1 = @tracker.add_reservation(start_time: Date.new(2018,10,5), end_time: Date.new(2018,10,10), number_of_rooms:1) expect(@tracker.view_reservations_on(Date.new(2018,10,5))).must_be_kind_of Array end - #this one is working 3333,10,5 + it "raises an ArgumentError if no reservations are available on given date" do expect{@tracker.view_reservations_on(Date.new(3333,10,5))}.must_raise ArgumentError end - #this one is not working!!!!!! + +# it "raises an ArgumentError if given date is not an instance of Date class" do expect{@tracker.view_reservations_on("some date")}.must_raise ArgumentError end @@ -123,25 +123,12 @@ end expect{@tracker.view_available_rooms_on(start_time: Date.new(2018,1,1),end_time:Date.new(2018,1,2))}.must_raise ArgumentError end - - # it "ensures rooms returned aren't in a block" do - # #make a new block for that date, reserve rooms that are in that block, - # # then call view_availa rooms on that date - # @tracker.add_block(start_time: Date.new(2018,1,1), end_time: Date.new(2018,1,29), number_of_rooms: 5, discount: 10) - # block_id = @tracker.blocks[0].block - # @tracker.add_reservation_in_block(start_time: Date.new(2018,1,1), end_time: Date.new(2018,1,29), number_of_rooms: 5, block: block_id) - # available_rooms = @tracker.view_available_rooms_on(start_time: Date.new(2018,1,1), end_time: Date.new(2018,1,29)) - # binding.pry - # end - end - describe "#add_reservation" do before do @tracker = TrackingSystem.new - # @room = Room.new({room_num: 1, availability: :available }) end it "returns an array of reservations" do @@ -162,10 +149,7 @@ updated_num_of_reservations = @tracker.reservations expect(updated_num_of_reservations.length).must_equal 2 end - end #end of describe - - - + end @@ -173,16 +157,15 @@ before do @tracker = TrackingSystem.new end +# + it "raises ArgumentError if discount-rate is not an Integer" do + expect{@tracker.add_block(start_time: Date.new(2018,8,1), end_time: Date.new(2018,8,5), rooms: 1, discount: "hi")}.must_raise ArgumentError + end - #this test is always passing no matter what, its not working - # it "raises ArgumentError if discount-rate is not an Integer" do - # expect{@tracker.add_block(start_time: Date.new(2018,8,1), end_time: Date.new(2018,8,5), rooms: 1, discount: "hi")}.must_raise ArgumentError - # end - # - # it "raises ArgumentError if rooms > 5 or <= 1" do - # @tracker = TrackingSystem.new - # expect{@tracker.add_block(start_time: Date.new(2018,8,5),end_time:Date.new(2018,9,5), rooms: 20, discount: 10)}.must_raise ArgumentError - # end + it "raises ArgumentError if rooms > 5 or <= 1" do + @tracker = TrackingSystem.new + expect{@tracker.add_block(start_time: Date.new(2018,8,5),end_time:Date.new(2018,9,5), rooms: 20, discount: 10)}.must_raise ArgumentError + end it "returns an array of blocks" do @block = @tracker.add_block(start_time: Date.new(2018,8,1), end_time: Date.new(2018,8,25), number_of_rooms:5) @@ -222,7 +205,6 @@ expect(@date_range.last).must_equal Date.new(2018,7,9) end - #this isn't working either, can't figure out how to make it fail when it should it "raises ArgumentError unless argument is a Symbol" do expect{@tracker.retrieve_block_dates("hello")}.must_raise ArgumentError end @@ -256,70 +238,15 @@ @blockid = @tracker.blocks[0].block end - # it "Adds a reservation to each room's list of reservations" do - # - # end it "raises ArgumentError if the number of rooms requested is greater than the number of available rooms " do expect{@tracker.add_reservation_in_block(start_time: Date.new(2018,10,5), end_time: Date.new(2018,10,10), number_of_rooms:555, block: @blockid)}.must_raise ArgumentError end it "returns an Array of reservations" do - # @block = @tracker.add_block(start_time: Date.new(2018,10,5), end_time: Date.new(2018,10,10), number_of_rooms:5) @blockid = @tracker.blocks[0].block expect(@tracker.add_reservation_in_block(start_time: Date.new(2018,10,5), end_time: Date.new(2018,10,10), number_of_rooms:2, block: @blockid)).must_be_kind_of Array end end - - - # def add_reservation_in_block(start_time: Date.today, end_time: Date.today + 1, number_of_rooms: 1, block: :NA) #block(id) defaults to :NA already but just clarifying here - # available_rooms = rooms_available_in_block(block) #<--returns an array of available rooms in this sepcifci block - # raise ArgumentError.new"Not enough rooms available on those dates" if available_rooms.length < number_of_rooms - # #call method taht gets block discount here - # discount = retrieve_block_discount(block) - # number_of_rooms.times do |i| - # @reservations << Reservation.new({room_num: available_rooms[i].room_num, start_time: start_time, end_time: end_time, price: 200.00 -(200.00 * discount)}) - # available_rooms[i].reserved_dates << {start_time: start_time, end_time: end_time} - # end - # @reservations - # end - - - describe "#retrieve_block_discount" do - it "returns a block's discount rate" do - - end - - it "raises ArgumentError unless argument is a Symbol" do - end - end - - - - - -end #end of class method - - -# it "adds a hash of start/end time to each room.reserved_dates array" do -# end - -# it "only selects rooms that are available " do -# expect(@reservation.) -# end -# -# it "creates reservations for the number_of_rooms requested " do -# end -# -# it "iterates through @all_rooms to find the first available rooms" do -# end -# -# it "raises ArgumentError if inside @all_rooms no room is available on this date range" do -# end -# -# it "creates a new instance of Reservation" do -# @reservation = @tracker.add_reservation(start_time: Date.new(2018,8,1), end_time: Date.new(2018,8,25), rooms: 1) #<---1 is the number of rooms -# -# expect(@reservation).must_be_kind_of Reservation -# end +end From efbadfa88ecf6c8046bd493398042b3eb436a58a Mon Sep 17 00:00:00 2001 From: Kay Date: Sun, 30 Sep 2018 15:15:19 -0700 Subject: [PATCH 44/46] added design-activity --- design-activity.md | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 design-activity.md diff --git a/design-activity.md b/design-activity.md new file mode 100644 index 000000000..bdc0b623c --- /dev/null +++ b/design-activity.md @@ -0,0 +1,42 @@ +1. What classes does each implementation include? Are the lists the same? +Each implementation includes the classes: CartEntry, ShoppingCart, and Order. +No, the lists aren't the same. In A, the instance variable @entries is both readable and writable. In B, @entries is not readable or writable. + + + +2. Write down a sentence to describe each class. +CartEntry --- it is the blueprint for new CartEntry objects, each object will be instantiated with @unit_price and @quantity attributes. (In A, the attributes are both readable and writable, but not in B.) + +ShoppingCart --- it contains the list of entries as an instance variable @entries. + +Order --- it is the blueprint for Order objects, each order is instantiated with a @cart attribute which is a list of @entries. (Each time an Order object is instantiated, it calls ShoppingCart.new and assigns it to the local variable @cart.) + + +3. How do the classes relate to each other? It might be helpful to draw a diagram on a whiteboard or piece of paper. +?? how can @cart call on entries? i thought it was set to equal ?? + +CartEntry is the factory for all entries, ShoppingCart stores all entries as a list, Order has a method that iterates through the list of entries, and performs a math operation on each entry to find the total price of an Order (by multiplying entry.unit_price by entry.quantity then summing the price of all entries). + + + +4. What data does each class store? How (if at all) does this differ between the two implementations? +ShoppingCart is the only class that stores data, it stores a list of entries. +In A, the instance variable @entries stores the list of entries and it is both readable and writable outside of the ShoppingCart class. +In B, the instance variable @entries stores the list of entries but is not accessible outside of the ShoppingCart class. + + + +5. What methods does each class have? How (if at all) does this differ between the two implementations? +Both classes have initialize methods which initialize CartEntry with @unit_price and @quantity, ShoppingCart with @entries as an array, and Order with @cart as ShoppingCart.new(which is basically an array). + +In B, CartEntry has an additional method that A doesn't have called #price which performs a math operation between @unit_price and @quantities (instance variables defined in its own class) and returns the price of an entry. +In B, ShoppingCart also has a method that A doesn't have called #price which iterates through the list of @entries (the instance variable defined in its own class) to add up and return the sum of all entries. +The Order class in A and B both contain #total_price. In A, the instance variable @cart needs call on entries to then iterate through each entry in order to first view their unit_price and quantities and then multiply by the tax to perform the math operation to find the total price of the order. In B, the instance variable @cart needs to just call price then multiply by the tax to find the total price. + + +6. Consider the Order#total_price method. In each implementation: +Is logic to compute the price delegated to "lower level" classes like ShoppingCart and CartEntry, or is it retained in Order? +Does total_price directly manipulate the instance variables of other classes? +If we decide items are cheaper if bought in bulk, how would this change the code? Which implementation is easier to modify? +Which implementation better adheres to the single responsibility principle? +Bonus question once you've read Metz ch. 3: Which implementation is more loosely coupled? From 41eb3d2843e52ddb40d577103d04d87ba81cce05 Mon Sep 17 00:00:00 2001 From: Kay Date: Sun, 30 Sep 2018 15:18:24 -0700 Subject: [PATCH 45/46] added design-activity.md --- design-activity.md | 12 ++++++++---- lib/tracking_system.rb | 2 +- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/design-activity.md b/design-activity.md index bdc0b623c..c4b84fb6e 100644 --- a/design-activity.md +++ b/design-activity.md @@ -35,8 +35,12 @@ The Order class in A and B both contain #total_price. In A, the instance variabl 6. Consider the Order#total_price method. In each implementation: -Is logic to compute the price delegated to "lower level" classes like ShoppingCart and CartEntry, or is it retained in Order? -Does total_price directly manipulate the instance variables of other classes? -If we decide items are cheaper if bought in bulk, how would this change the code? Which implementation is easier to modify? -Which implementation better adheres to the single responsibility principle? +6.1) Is logic to compute the price delegated to "lower level" classes like ShoppingCart and CartEntry, or is it retained in Order? In A it is retained in Order, in B it is delegated. + +6.2) Does total_price directly manipulate the instance variables of other classes? Yes it does in A but not in B. + +6.3) If we decide items are cheaper if bought in bulk, how would this change the code? Which implementation is easier to modify? B is easier to modify. + +6.4) Which implementation better adheres to the single responsibility principle? Bonus question once you've read Metz ch. 3: Which implementation is more loosely coupled? +B is more loosely coupled and adheres better to the single responsibility principle. diff --git a/lib/tracking_system.rb b/lib/tracking_system.rb index 5bc702b0b..9eea9bcc1 100644 --- a/lib/tracking_system.rb +++ b/lib/tracking_system.rb @@ -11,7 +11,7 @@ class TrackingSystem def initialize @all_rooms = add_rooms - @reservations = [] + @reservations = [] #when a block is created, would i instead add that number of rooms into the reservations list & change their block status?, and then when it's reserved add one set of {dates} to the reservation @blocks = [] end From 0f2a7dcbcb252b57383a8edf21758e73d0cffca9 Mon Sep 17 00:00:00 2001 From: Kay Date: Sun, 30 Sep 2018 22:49:45 -0700 Subject: [PATCH 46/46] design activity changes --- design-activity.md | 31 ++++++++++++++++++++++++++++++- lib/reservation.rb | 35 +++++++++++++++++++++++++++++++++++ lib/tracking_system.rb | 17 ++--------------- 3 files changed, 67 insertions(+), 16 deletions(-) diff --git a/design-activity.md b/design-activity.md index c4b84fb6e..ad31dac3c 100644 --- a/design-activity.md +++ b/design-activity.md @@ -39,8 +39,37 @@ The Order class in A and B both contain #total_price. In A, the instance variabl 6.2) Does total_price directly manipulate the instance variables of other classes? Yes it does in A but not in B. -6.3) If we decide items are cheaper if bought in bulk, how would this change the code? Which implementation is easier to modify? B is easier to modify. +6.3) If we decide items are cheaper if bought in bulk, how would this change the code? Which implementation is easier to modify? B is easier to modify. 6.4) Which implementation better adheres to the single responsibility principle? Bonus question once you've read Metz ch. 3: Which implementation is more loosely coupled? B is more loosely coupled and adheres better to the single responsibility principle. + + +~~ Refactor Activity: ~~ +Based on the answers to each set of the above questions, identify one place in your Hotel project where a class takes on multiple roles, or directly modifies the attributes of another class. Describe in design-activity.md what changes you would need to make to improve this design, and how the resulting design would be an improvement. + +One design issue I have in TrackingSystem is that it has too many jobs. One design change I can make to improve this is by moving some date checking logic out of this class and into the Reservation class. In TrackingSystem I'm currently iterating through the list of reservations in order to be able to perform +date checking logic, and it could just be done in the Reservation class itself. Moving the date checking logic to the Reservation class will be an improvement because the TrackingSystem will need to know less about the structure and variables of the Reservation class and it will DRY up the code in certain methods that iterate through +the list of reservations. + +Extra notes... + +Questions to keep in mind: +-should the instance variables be readable and writable? +-should there be a class thats job is just to store data? +-can some computing logic be delegated to lower classes? + +1. The TrackingSystem class takes on multiple roles + a. it creates all the rooms + b. it stores the list of blocks, list of reservations, and list of rooms + *c*. it contains date checking logic that checks on a reservation to determines a room and block's availability + d. it adds new blocks and new reservations to its data + +2. The TrackingSystem directly modifies the attributes of + a. a room's reserved dates and its block status + b. a reservation's price + c. a block's reserved dates, block status and rooms inside it + +3. The Room class takes on multiple roles + a. it keeps track of a list of dates that it is unavailable which is doing too much it should let the TrackingSystem class be the only place where reservations data is kept. I realized that most of the methods in TrackingSystem are going through the Room objects to check the reservation status, when it should be checking that through Reservation objects. diff --git a/lib/reservation.rb b/lib/reservation.rb index b67f14509..3aa8f87c8 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -13,4 +13,39 @@ def total_cost return total_cost = ((end_time - start_time) * price).round(2) end + def date_range + return self.start_time...self.end_time + end + def reservations_on(date) + raise ArgumentError.new"#{date} must be instance of Date" unless date.instance_of? Date + reservations_on_date = [] + TrackingSystem.reservations.each do |reservation| + if reservation.include?(date) + reservations_on_date << reservation + end + end + if reservations_on_date.empty? + raise ArgumentError.new"There are no reservations on #{date}" + else + return reservations_on_date + end + end + + def ranges_overlap?(other_date_range) + self.date_range.include?(other_date_range.first) || other_date_range.include?(self.start_time) + end + + def include?(date) + if self.date_range.include?(date) + return true + else + return false + end + end + + def number_of_nights + return (self.end_time - self.start_time).to_i + end + + end diff --git a/lib/tracking_system.rb b/lib/tracking_system.rb index 9eea9bcc1..e4b92aab9 100644 --- a/lib/tracking_system.rb +++ b/lib/tracking_system.rb @@ -11,7 +11,7 @@ class TrackingSystem def initialize @all_rooms = add_rooms - @reservations = [] #when a block is created, would i instead add that number of rooms into the reservations list & change their block status?, and then when it's reserved add one set of {dates} to the reservation + @reservations = [] #when a block is created, would i instead add that number of rooms into the reservations list & change their block status?, and then when it's reserved add one set of {dates} to the reservation @blocks = [] end @@ -31,20 +31,7 @@ def view_all_rooms return @all_rooms end - def view_reservations_on(date) - raise ArgumentError.new"#{date} must be instance of Date" unless date.instance_of? Date - all_reservations = [] - @reservations.each do |reservation| - if (reservation.start_time...reservation.end_time).include?(date) - all_reservations << reservation - end - end - if all_reservations.empty? - raise ArgumentError.new"There are no reservations on #{date}" - else - return all_reservations - end - end + def total_cost_of_reservation(reservation) raise ArgumentError.new"#{reservation} must be instance of Reservation" unless reservation.instance_of? Reservation