From c861cf4358e4706158ce465a04687fae78e8096d Mon Sep 17 00:00:00 2001 From: Hannah Cameron Date: Mon, 5 Mar 2018 13:49:50 -0800 Subject: [PATCH 01/18] set up file structure for current classes; wrote tests for HotelManager#initialize - code passing currently. 100% coverage --- Rakefile | 10 ++++++++++ lib/hotel_manager.rb | 21 +++++++++++++++++++++ lib/reservation.rb | 4 ++++ specs/hotel_manager_spec.rb | 20 ++++++++++++++++++++ specs/reservation_spec.rb | 1 + specs/spec_helper.rb | 12 ++++++++++++ 6 files changed, 68 insertions(+) create mode 100644 Rakefile create mode 100644 lib/hotel_manager.rb create mode 100644 lib/reservation.rb create mode 100644 specs/hotel_manager_spec.rb create mode 100644 specs/reservation_spec.rb create mode 100644 specs/spec_helper.rb diff --git a/Rakefile b/Rakefile new file mode 100644 index 000000000..3391136e7 --- /dev/null +++ b/Rakefile @@ -0,0 +1,10 @@ +require 'rake/testtask' + +Rake::TestTask.new do |t| + t.libs = ["lib"] + t.warning = true + t.test_files = FileList['specs/*_spec.rb'] +end + +task default: :test + diff --git a/lib/hotel_manager.rb b/lib/hotel_manager.rb new file mode 100644 index 000000000..0596c5843 --- /dev/null +++ b/lib/hotel_manager.rb @@ -0,0 +1,21 @@ +module Hotel + class HotelManager + + RATE = 200 + + attr_accessor :room_list, :reservations + + def initialize + + @room_list = load_rooms + @reservations = [] + + end + + def load_rooms + room_list = Array('1'..'20') + return room_list + end + + end # end of class +end # end of module diff --git a/lib/reservation.rb b/lib/reservation.rb new file mode 100644 index 000000000..aaee95297 --- /dev/null +++ b/lib/reservation.rb @@ -0,0 +1,4 @@ +module Hotel + class Reservation + end # end of class +end # end of module diff --git a/specs/hotel_manager_spec.rb b/specs/hotel_manager_spec.rb new file mode 100644 index 000000000..13cb45443 --- /dev/null +++ b/specs/hotel_manager_spec.rb @@ -0,0 +1,20 @@ +require_relative "spec_helper.rb" + +describe 'HotelManager' do + + it 'can create a new instance of HotelManager' do + new_hotel = Hotel::HotelManager.new + new_hotel.must_be_instance_of Hotel::HotelManager + end # end of can create new instance + + it 'contains a list of all 20 rooms' do + new_hotel = Hotel::HotelManager.new + new_hotel.room_list.length.must_equal 20 + end + + it 'contains an empty array of hotel reservations' do + new_hotel = Hotel::HotelManager.new + new_hotel.reservations.length.must_equal 0 + end + +end # end of describe HotelManager diff --git a/specs/reservation_spec.rb b/specs/reservation_spec.rb new file mode 100644 index 000000000..c450636f5 --- /dev/null +++ b/specs/reservation_spec.rb @@ -0,0 +1 @@ +require_relative "spec_helper.rb" diff --git a/specs/spec_helper.rb b/specs/spec_helper.rb new file mode 100644 index 000000000..a67812ec4 --- /dev/null +++ b/specs/spec_helper.rb @@ -0,0 +1,12 @@ +require 'simplecov' +SimpleCov.start +require 'minitest' +require 'minitest/autorun' +require 'minitest/reporters' +# Add simplecov + +Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new + +# Require_relative your lib files here! +require_relative '../lib/hotel_manager' +require_relative '../lib/reservation' From 7e44b097ee4739ac123264fd78e9cb5a0317c7cf Mon Sep 17 00:00:00 2001 From: Hannah Cameron Date: Mon, 5 Mar 2018 14:13:12 -0800 Subject: [PATCH 02/18] added in testing for Reservation#initialize - no tests passing yet --- .DS_Store | Bin 0 -> 6148 bytes lib/hotel_manager.rb | 3 ++- lib/reservation.rb | 3 +++ specs/reservation_spec.rb | 19 +++++++++++++++++++ 4 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..1ad899dbcdc748036dd701eacb0ec11e243703d4 GIT binary patch literal 6148 zcmeHKyJ`bL3>+mc4$`I(2Sbbi#2M3J zT*oXyY@Q(Y!ZDE|1kgGlC+ZoQs7@HV6)|Fx!@~RZ=JlH_u58Z)4k@C?#6Xc7@{2$qaAbO f?f5y0vab1>=e=-D3_9~cC+cUwb&*MdzgFN1-fR_w literal 0 HcmV?d00001 diff --git a/lib/hotel_manager.rb b/lib/hotel_manager.rb index 0596c5843..e67827785 100644 --- a/lib/hotel_manager.rb +++ b/lib/hotel_manager.rb @@ -3,7 +3,8 @@ class HotelManager RATE = 200 - attr_accessor :room_list, :reservations + attr_reader :room_list + attr_writer :reservations def initialize diff --git a/lib/reservation.rb b/lib/reservation.rb index aaee95297..29550c524 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -1,4 +1,7 @@ module Hotel class Reservation + + attr_accessor :reservation_id, :room_id, :check_in, :check_out + end # end of class end # end of module diff --git a/specs/reservation_spec.rb b/specs/reservation_spec.rb index c450636f5..0b97801cf 100644 --- a/specs/reservation_spec.rb +++ b/specs/reservation_spec.rb @@ -1 +1,20 @@ require_relative "spec_helper.rb" + +describe 'reservation' do + describe 'initialize' do + it "can call class properties when instantiated" do + new_reservataion = Hotel::Reservation.new + [reservation_id, room_id, check_in, check_out].each do |prop| + new_reservataion.must_respond_to prop + end + end + + it 'instantiates the class properties as the correct class type' do + new_reservataion.reservation_id.must_be_kind_of Integer + new_reservataion.room_id.must_be_kind_of Integer + new_reservataion.check_in.must_be_kind_of Date + new_reservataion.check_out.must_be_kind_of Date + end + end # end of initialize + +end # end of describe Reservation From 66e3210cab9ff05912debcb860e485a388b2215d Mon Sep 17 00:00:00 2001 From: Hannah Cameron Date: Mon, 5 Mar 2018 14:31:26 -0800 Subject: [PATCH 03/18] reformatted testing in hotel_manager_spec to match formatterin in reservation_spec; appropriately failing tests in reservation_spec --- lib/hotel_manager.rb | 2 +- lib/reservation.rb | 7 +++++++ specs/hotel_manager_spec.rb | 24 ++++++++++++++---------- specs/reservation_spec.rb | 21 ++++++++++++--------- 4 files changed, 34 insertions(+), 20 deletions(-) diff --git a/lib/hotel_manager.rb b/lib/hotel_manager.rb index e67827785..2a6551ac3 100644 --- a/lib/hotel_manager.rb +++ b/lib/hotel_manager.rb @@ -4,7 +4,7 @@ class HotelManager RATE = 200 attr_reader :room_list - attr_writer :reservations + attr_accessor :reservations def initialize diff --git a/lib/reservation.rb b/lib/reservation.rb index 29550c524..4d9d33734 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -3,5 +3,12 @@ class Reservation attr_accessor :reservation_id, :room_id, :check_in, :check_out + def initialize + @reservation_id = get_reservation_id + @room_id = room_id + @check_in = check_in + @check_out = check_out + end + end # end of class end # end of module diff --git a/specs/hotel_manager_spec.rb b/specs/hotel_manager_spec.rb index 13cb45443..4da91ee52 100644 --- a/specs/hotel_manager_spec.rb +++ b/specs/hotel_manager_spec.rb @@ -1,20 +1,24 @@ require_relative "spec_helper.rb" describe 'HotelManager' do - + before do + @new_hotel = Hotel::HotelManager.new + end it 'can create a new instance of HotelManager' do - new_hotel = Hotel::HotelManager.new - new_hotel.must_be_instance_of Hotel::HotelManager + @new_hotel.must_be_instance_of Hotel::HotelManager end # end of can create new instance - it 'contains a list of all 20 rooms' do - new_hotel = Hotel::HotelManager.new - new_hotel.room_list.length.must_equal 20 - end + # it 'can call class properties when instantiated' do + # [room_list, reservations].each do |prop| + # @new_hotel.must_respond_to prop + # end + # end - it 'contains an empty array of hotel reservations' do - new_hotel = Hotel::HotelManager.new - new_hotel.reservations.length.must_equal 0 + it 'instantiates the properties correctly' do + @new_hotel.room_list.must_be_kind_of Array + @new_hotel.room_list.length.must_equal 20 + @new_hotel.reservations.must_be_kind_of Array + @new_hotel.reservations.length.must_equal 0 end end # end of describe HotelManager diff --git a/specs/reservation_spec.rb b/specs/reservation_spec.rb index 0b97801cf..74d42eb43 100644 --- a/specs/reservation_spec.rb +++ b/specs/reservation_spec.rb @@ -2,19 +2,22 @@ describe 'reservation' do describe 'initialize' do - it "can call class properties when instantiated" do - new_reservataion = Hotel::Reservation.new - [reservation_id, room_id, check_in, check_out].each do |prop| - new_reservataion.must_respond_to prop - end + before do + @new_reservataion = Hotel::Reservation.new end + # it "can call class properties when instantiated" do + # [reservation_id, room_id, check_in, check_out].each do |prop| + # @new_reservataion.must_respond_to prop + # end + # end it 'instantiates the class properties as the correct class type' do - new_reservataion.reservation_id.must_be_kind_of Integer - new_reservataion.room_id.must_be_kind_of Integer - new_reservataion.check_in.must_be_kind_of Date - new_reservataion.check_out.must_be_kind_of Date + @new_reservataion.reservation_id.must_be_kind_of Integer + @new_reservataion.room_id.must_be_kind_of Integer + @new_reservataion.check_in.must_be_kind_of Date + @new_reservataion.check_out.must_be_kind_of Date end + end # end of initialize end # end of describe Reservation From eb1c8d5a9d285106e4d67df8eba99a72b366187c Mon Sep 17 00:00:00 2001 From: Hannah Cameron Date: Mon, 5 Mar 2018 15:04:46 -0800 Subject: [PATCH 04/18] rewrote tests for reservation#initialize that take into account changes to class structure; changes made to HotelManager --- lib/hotel_manager.rb | 15 +++++++++++++++ lib/reservation.rb | 14 ++++++++------ specs/reservation_spec.rb | 11 ++++++----- 3 files changed, 29 insertions(+), 11 deletions(-) diff --git a/lib/hotel_manager.rb b/lib/hotel_manager.rb index 2a6551ac3..881190c89 100644 --- a/lib/hotel_manager.rb +++ b/lib/hotel_manager.rb @@ -18,5 +18,20 @@ def load_rooms return room_list end + def get_reservation_id + next_reservation_id = reservations.length + 1 + return next_reservation_id + end + + def get_available_room + next_room_id = room_list.sample + end + + def add_reservation(check_in_date, check_out_date) + new_reservataion = Hotel::Reservation.new({reservation_id: get_reservation_id, room_id: get_available_room, check_in: check_in_date, check_out: check_out_date, cost: calculate_cost}) + end + + + end # end of class end # end of module diff --git a/lib/reservation.rb b/lib/reservation.rb index 4d9d33734..d3ab6582f 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -1,14 +1,16 @@ module Hotel class Reservation - attr_accessor :reservation_id, :room_id, :check_in, :check_out + attr_accessor :reservation_id, :room_id, :check_in, :check_out, :cost - def initialize - @reservation_id = get_reservation_id - @room_id = room_id - @check_in = check_in - @check_out = check_out + def initialize(input) + @reservation_id = input[:reservation_id] + @room_id = input[:room_id] + @check_in = input[:check_in] + @check_out = input[:check_out] + @cost = input[:cost] end + end # end of class end # end of module diff --git a/specs/reservation_spec.rb b/specs/reservation_spec.rb index 74d42eb43..051b8243b 100644 --- a/specs/reservation_spec.rb +++ b/specs/reservation_spec.rb @@ -3,7 +3,7 @@ describe 'reservation' do describe 'initialize' do before do - @new_reservataion = Hotel::Reservation.new + @new_reservation = Hotel::Reservation.new({reservation_id: 1, room_id: 2, check_in: Date.new(2018, 5, 25), check_out: Date.new(2018, 5, 27), cost: 400}) end # it "can call class properties when instantiated" do # [reservation_id, room_id, check_in, check_out].each do |prop| @@ -12,10 +12,11 @@ # end it 'instantiates the class properties as the correct class type' do - @new_reservataion.reservation_id.must_be_kind_of Integer - @new_reservataion.room_id.must_be_kind_of Integer - @new_reservataion.check_in.must_be_kind_of Date - @new_reservataion.check_out.must_be_kind_of Date + @new_reservation.reservation_id.must_be_kind_of Integer + @new_reservation.room_id.must_be_kind_of Integer + @new_reservation.check_in.must_be_kind_of Date + @new_reservation.check_out.must_be_kind_of Date + @new_reservation.cost.must_be_kind_of Integer end end # end of initialize From e862d291c09d8085e35f8f3241f01365bf19bafa Mon Sep 17 00:00:00 2001 From: Hannah Cameron Date: Mon, 5 Mar 2018 16:03:54 -0800 Subject: [PATCH 05/18] wrote tests for helper methods #get_reservation_id and #get_available_room. Passing all tests --- lib/hotel_manager.rb | 17 ++++++-- specs/hotel_manager_spec.rb | 78 ++++++++++++++++++++++++++++--------- 2 files changed, 72 insertions(+), 23 deletions(-) diff --git a/lib/hotel_manager.rb b/lib/hotel_manager.rb index 881190c89..4fac8ed27 100644 --- a/lib/hotel_manager.rb +++ b/lib/hotel_manager.rb @@ -1,3 +1,5 @@ +require 'pry' + module Hotel class HotelManager @@ -14,7 +16,7 @@ def initialize end def load_rooms - room_list = Array('1'..'20') + room_list = Array(1..20) return room_list end @@ -25,11 +27,18 @@ def get_reservation_id def get_available_room next_room_id = room_list.sample + # binding.pry + return next_room_id end - def add_reservation(check_in_date, check_out_date) - new_reservataion = Hotel::Reservation.new({reservation_id: get_reservation_id, room_id: get_available_room, check_in: check_in_date, check_out: check_out_date, cost: calculate_cost}) - end + # def calculate_cost + # + # return reservation_cost + # end + + # def add_reservation(check_in_date, check_out_date) + # new_reservataion = Hotel::Reservation.new({reservation_id: get_reservation_id, room_id: get_available_room, check_in: check_in_date, check_out: check_out_date, cost: calculate_cost}) + # end diff --git a/specs/hotel_manager_spec.rb b/specs/hotel_manager_spec.rb index 4da91ee52..cff29007b 100644 --- a/specs/hotel_manager_spec.rb +++ b/specs/hotel_manager_spec.rb @@ -1,24 +1,64 @@ +require 'pry' require_relative "spec_helper.rb" + describe 'HotelManager' do - before do - @new_hotel = Hotel::HotelManager.new - end - it 'can create a new instance of HotelManager' do - @new_hotel.must_be_instance_of Hotel::HotelManager - end # end of can create new instance - - # it 'can call class properties when instantiated' do - # [room_list, reservations].each do |prop| - # @new_hotel.must_respond_to prop - # end - # end - - it 'instantiates the properties correctly' do - @new_hotel.room_list.must_be_kind_of Array - @new_hotel.room_list.length.must_equal 20 - @new_hotel.reservations.must_be_kind_of Array - @new_hotel.reservations.length.must_equal 0 - end + describe 'initialize' do + before do + @new_hotel = Hotel::HotelManager.new + end + it 'can create a new instance of HotelManager' do + @new_hotel.must_be_instance_of Hotel::HotelManager + end # end of can create new instance + + # it 'can call class properties when instantiated' do + # [room_list, reservations].each do |prop| + # @new_hotel.must_respond_to prop + # end + # end + + it 'instantiates the properties correctly' do + @new_hotel.room_list.must_be_kind_of Array + @new_hotel.reservations.must_be_kind_of Array + @new_hotel.reservations.length.must_equal 0 + end + end # end of describe initialize + + describe 'load_rooms' do + it 'correctly creates room_list array' do + @new_hotel = Hotel::HotelManager.new + @new_hotel.room_list.length.must_equal 20 + end + end # end of describe load_rooms + + describe 'get_reservation_id' do + it 'correctly generates the next reservation_id when no reservations' do + @new_hotel = Hotel::HotelManager.new + @new_hotel.reservations.length.must_equal 0 + @new_hotel.get_reservation_id.must_equal 1 + end + it 'correctly generates the next reservation_id when reservations already exist' do + @new_hotel = Hotel::HotelManager.new + @new_hotel.reservations = [1, 2, 3] + @new_hotel.reservations.length.must_equal 3 + @new_hotel.get_reservation_id.must_equal 4 + end + end # end of describe 'get_reservation_id' + + describe 'get_available_room' do + it 'must return an appropriate room id' do + @new_hotel = Hotel::HotelManager.new + room_id = @new_hotel.get_available_room + # binding.pry + room_id.must_be_kind_of Integer + room_id.must_be :>, 0 + room_id.must_be :<, 21 + + end + end # end of get_available_room end # end of describe HotelManager + +# check_in_date = Date.new(2018, 4, 15) +# check_out_date = Date.new(2018, 4, 24) +# @new_hotel.add_reservation(check_in_date, check_out_date) From f808cb8916d6a1db848aae8f3f652fbffb82c714 Mon Sep 17 00:00:00 2001 From: Hannah Cameron Date: Mon, 5 Mar 2018 16:51:11 -0800 Subject: [PATCH 06/18] all helper methods for #add_reservation passing tests; tests written and passing for #add_reservation --- lib/hotel_manager.rb | 26 +++++++++++++++++-------- specs/hotel_manager_spec.rb | 38 +++++++++++++++++++++++++++++++++++-- 2 files changed, 54 insertions(+), 10 deletions(-) diff --git a/lib/hotel_manager.rb b/lib/hotel_manager.rb index 4fac8ed27..12a9023b3 100644 --- a/lib/hotel_manager.rb +++ b/lib/hotel_manager.rb @@ -1,4 +1,5 @@ require 'pry' +require 'date' module Hotel class HotelManager @@ -27,18 +28,27 @@ def get_reservation_id def get_available_room next_room_id = room_list.sample - # binding.pry return next_room_id end - # def calculate_cost - # - # return reservation_cost - # end + def calculate_duration_of_stay(check_in_date, check_out_date) + duration = Integer(check_out_date - check_in_date) + return duration + end + + def calculate_cost(duration) + reservation_cost = duration * RATE + return reservation_cost + end - # def add_reservation(check_in_date, check_out_date) - # new_reservataion = Hotel::Reservation.new({reservation_id: get_reservation_id, room_id: get_available_room, check_in: check_in_date, check_out: check_out_date, cost: calculate_cost}) - # end + def add_reservation(check_in_date, check_out_date) + duration = calculate_duration_of_stay(check_in_date, check_out_date) + stay_cost = calculate_cost(duration) + + new_reservation = Hotel::Reservation.new({reservation_id: get_reservation_id, room_id: get_available_room, check_in: check_in_date, check_out: check_out_date, cost: stay_cost}) + reservations << new_reservation + return new_reservation + end diff --git a/specs/hotel_manager_spec.rb b/specs/hotel_manager_spec.rb index cff29007b..1d70670cb 100644 --- a/specs/hotel_manager_spec.rb +++ b/specs/hotel_manager_spec.rb @@ -49,14 +49,48 @@ it 'must return an appropriate room id' do @new_hotel = Hotel::HotelManager.new room_id = @new_hotel.get_available_room - # binding.pry room_id.must_be_kind_of Integer room_id.must_be :>, 0 room_id.must_be :<, 21 - end end # end of get_available_room + describe 'calculate_duration_of_stay' do + before do + @new_hotel = Hotel::HotelManager.new + end + + it 'returns an integer of the correct duration' do + duration = @new_hotel.calculate_duration_of_stay(Date.new(2018, 3, 25), Date.new(2018, 3, 28)) + duration.must_be_kind_of Integer + duration.must_equal 3 + end + + end + + describe 'calculate_cost' do + it 'returns an Integer' do + @new_hotel = Hotel::HotelManager.new + cost = @new_hotel.calculate_cost(5) + cost.must_equal 1000 + end + end # end of describe calculate_cost + + describe 'add_reservation' do + it 'creates an instance of Reservation' do + @new_hotel = Hotel::HotelManager.new + + @new_hotel.add_reservation(Date.new(2018, 3, 25), Date.new(2018, 3, 28)).must_be_instance_of Hotel::Reservation + end + + it 'adds the reservation to array of reservations' do + @new_hotel = Hotel::HotelManager.new + @new_hotel.reservations.length.must_equal 0 + @new_hotel.add_reservation(Date.new(2018, 3, 25), Date.new(2018, 3, 28)) + @new_hotel.reservations.length.must_equal 1 + end + end # end of describe add_reservation + end # end of describe HotelManager # check_in_date = Date.new(2018, 4, 15) From a440a6d0fa5006ebba5cc42c404d2aca87070111 Mon Sep 17 00:00:00 2001 From: Hannah Cameron Date: Tue, 6 Mar 2018 11:22:47 -0800 Subject: [PATCH 07/18] added tests for reservation#stay_date_list; method passing all tests --- lib/hotel_manager.rb | 4 +++- lib/reservation.rb | 5 +++++ specs/reservation_spec.rb | 13 +++++++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/lib/hotel_manager.rb b/lib/hotel_manager.rb index 12a9023b3..37e33749b 100644 --- a/lib/hotel_manager.rb +++ b/lib/hotel_manager.rb @@ -50,7 +50,9 @@ def add_reservation(check_in_date, check_out_date) return new_reservation end - + # def reservations_by_date + # + # end end # end of class end # end of module diff --git a/lib/reservation.rb b/lib/reservation.rb index d3ab6582f..46c5acfe2 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -11,6 +11,11 @@ def initialize(input) @cost = input[:cost] end + def stay_date_list + list_of_dates = (@check_in..@check_out).to_a + return list_of_dates + end + end # end of class end # end of module diff --git a/specs/reservation_spec.rb b/specs/reservation_spec.rb index 051b8243b..e96732936 100644 --- a/specs/reservation_spec.rb +++ b/specs/reservation_spec.rb @@ -21,4 +21,17 @@ end # end of initialize + describe 'stay_date_list' do + before do + @new_reservation = Hotel::Reservation.new({reservation_id: 1, room_id: 2, check_in: Date.new(2018, 5, 25), check_out: Date.new(2018, 5, 27), cost: 400}) + end + + it 'returns a list of dates that are covered by a reservation' do + date_list = @new_reservation.stay_date_list + date_list.must_be_kind_of Array + date_list[0].must_be_instance_of Date + date_list.length.must_equal 3 + end + + end end # end of describe Reservation From a00cc4be13e83dc50d48ec1f0cc00cc7b724f9bf Mon Sep 17 00:00:00 2001 From: Hannah Cameron Date: Tue, 6 Mar 2018 12:10:34 -0800 Subject: [PATCH 08/18] shifted method to calculate cost of reservation to the reservation class. re-wrote tests, all passing. Meets all requirements of wave 2, 100% code coverege --- lib/hotel_manager.rb | 29 ++++++++-------------- lib/reservation.rb | 10 ++++++-- specs/hotel_manager_spec.rb | 48 +++++++++++++++++++++---------------- specs/reservation_spec.rb | 18 +++++++++++--- 4 files changed, 60 insertions(+), 45 deletions(-) diff --git a/lib/hotel_manager.rb b/lib/hotel_manager.rb index 37e33749b..6125baf5a 100644 --- a/lib/hotel_manager.rb +++ b/lib/hotel_manager.rb @@ -4,8 +4,6 @@ module Hotel class HotelManager - RATE = 200 - attr_reader :room_list attr_accessor :reservations @@ -31,28 +29,21 @@ def get_available_room return next_room_id end - def calculate_duration_of_stay(check_in_date, check_out_date) - duration = Integer(check_out_date - check_in_date) - return duration - end - - def calculate_cost(duration) - reservation_cost = duration * RATE - return reservation_cost - end - def add_reservation(check_in_date, check_out_date) - duration = calculate_duration_of_stay(check_in_date, check_out_date) - stay_cost = calculate_cost(duration) - - new_reservation = Hotel::Reservation.new({reservation_id: get_reservation_id, room_id: get_available_room, check_in: check_in_date, check_out: check_out_date, cost: stay_cost}) + new_reservation = Hotel::Reservation.new({reservation_id: get_reservation_id, room_id: get_available_room, check_in: check_in_date, check_out: check_out_date}) reservations << new_reservation return new_reservation end - # def reservations_by_date - # - # end + def reservations_by_date(date) + reservation_list = [] + @reservations.each do |reservation| + if reservation.stay_date_list.include?(date) + reservation_list << reservation + end + end + return reservation_list + end end # end of class end # end of module diff --git a/lib/reservation.rb b/lib/reservation.rb index 46c5acfe2..2eac7a03d 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -1,14 +1,15 @@ module Hotel class Reservation - attr_accessor :reservation_id, :room_id, :check_in, :check_out, :cost + RATE = 200.00 + + attr_accessor :reservation_id, :room_id, :check_in, :check_out def initialize(input) @reservation_id = input[:reservation_id] @room_id = input[:room_id] @check_in = input[:check_in] @check_out = input[:check_out] - @cost = input[:cost] end def stay_date_list @@ -16,6 +17,11 @@ def stay_date_list return list_of_dates end + def calculate_cost + duration = Integer(check_out- check_in) + reservation_cost = duration * RATE + return reservation_cost + end end # end of class end # end of module diff --git a/specs/hotel_manager_spec.rb b/specs/hotel_manager_spec.rb index 1d70670cb..cd7d65d8b 100644 --- a/specs/hotel_manager_spec.rb +++ b/specs/hotel_manager_spec.rb @@ -55,27 +55,6 @@ end end # end of get_available_room - describe 'calculate_duration_of_stay' do - before do - @new_hotel = Hotel::HotelManager.new - end - - it 'returns an integer of the correct duration' do - duration = @new_hotel.calculate_duration_of_stay(Date.new(2018, 3, 25), Date.new(2018, 3, 28)) - duration.must_be_kind_of Integer - duration.must_equal 3 - end - - end - - describe 'calculate_cost' do - it 'returns an Integer' do - @new_hotel = Hotel::HotelManager.new - cost = @new_hotel.calculate_cost(5) - cost.must_equal 1000 - end - end # end of describe calculate_cost - describe 'add_reservation' do it 'creates an instance of Reservation' do @new_hotel = Hotel::HotelManager.new @@ -91,6 +70,33 @@ end end # end of describe add_reservation + describe 'reservations_by_date' do + before do + @overlap_date = Date.new(2018, 5, 26) + @new_hotel = Hotel::HotelManager.new + + # Two should contain the overlap date + @new_hotel.add_reservation(@overlap_date - 1, @overlap_date + 1) + @new_hotel.add_reservation(@overlap_date - 5, @overlap_date + 3) + + # One should not + @new_hotel.add_reservation(@overlap_date - 20, @overlap_date - 15) + end + + it 'returns an array of reservations for a specific date' do + reservation_list = @new_hotel.reservations_by_date(@overlap_date) + reservation_list.must_be_instance_of Array + reservation_list.length.must_equal 2 + reservation_list.each do |res| + res.must_be_instance_of Hotel::Reservation + end + end + + # it '' do + + # end + end # end of describe reservations_by_date + end # end of describe HotelManager # check_in_date = Date.new(2018, 4, 15) diff --git a/specs/reservation_spec.rb b/specs/reservation_spec.rb index e96732936..600324de8 100644 --- a/specs/reservation_spec.rb +++ b/specs/reservation_spec.rb @@ -3,7 +3,7 @@ describe 'reservation' do describe 'initialize' do before do - @new_reservation = Hotel::Reservation.new({reservation_id: 1, room_id: 2, check_in: Date.new(2018, 5, 25), check_out: Date.new(2018, 5, 27), cost: 400}) + @new_reservation = Hotel::Reservation.new({reservation_id: 1, room_id: 2, check_in: Date.new(2018, 5, 25), check_out: Date.new(2018, 5, 27)}) end # it "can call class properties when instantiated" do # [reservation_id, room_id, check_in, check_out].each do |prop| @@ -16,14 +16,13 @@ @new_reservation.room_id.must_be_kind_of Integer @new_reservation.check_in.must_be_kind_of Date @new_reservation.check_out.must_be_kind_of Date - @new_reservation.cost.must_be_kind_of Integer end end # end of initialize describe 'stay_date_list' do before do - @new_reservation = Hotel::Reservation.new({reservation_id: 1, room_id: 2, check_in: Date.new(2018, 5, 25), check_out: Date.new(2018, 5, 27), cost: 400}) + @new_reservation = Hotel::Reservation.new({reservation_id: 1, room_id: 2, check_in: Date.new(2018, 5, 25), check_out: Date.new(2018, 5, 27)}) end it 'returns a list of dates that are covered by a reservation' do @@ -34,4 +33,17 @@ end end + + describe 'calculate_duration_of_stay' do + before do + @new_reservation = Hotel::Reservation.new({reservation_id: 1, room_id: 2, check_in: Date.new(2018, 5, 25), check_out: Date.new(2018, 5, 27)}) + end + + it 'returns a float of the correct cost' do + cost = @new_reservation.calculate_cost + cost.must_be_kind_of Float + cost.must_equal 400.00 + end + end # end of describe calculate_cost + end # end of describe Reservation From 7bf7fec1994f6a0ae71a126b524aa0137718254b Mon Sep 17 00:00:00 2001 From: Hannah Cameron Date: Tue, 6 Mar 2018 12:28:56 -0800 Subject: [PATCH 09/18] added in error handling for date range. wave 1* requirements met. moving to wave 2 --- lib/reservation.rb | 7 +++++++ specs/reservation_spec.rb | 18 +++++++++++++----- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/lib/reservation.rb b/lib/reservation.rb index 2eac7a03d..b33ed8d7f 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -10,6 +10,13 @@ def initialize(input) @room_id = input[:room_id] @check_in = input[:check_in] @check_out = input[:check_out] + + if check_in == check_out + raise StandardError.new('The start date and the end date cannot be the same date.') + elsif check_in > check_out + raise StandardError.new('The end date cannot be before the start date.') + end + end def stay_date_list diff --git a/specs/reservation_spec.rb b/specs/reservation_spec.rb index 600324de8..e5b06679b 100644 --- a/specs/reservation_spec.rb +++ b/specs/reservation_spec.rb @@ -5,11 +5,6 @@ before do @new_reservation = Hotel::Reservation.new({reservation_id: 1, room_id: 2, check_in: Date.new(2018, 5, 25), check_out: Date.new(2018, 5, 27)}) end - # it "can call class properties when instantiated" do - # [reservation_id, room_id, check_in, check_out].each do |prop| - # @new_reservataion.must_respond_to prop - # end - # end it 'instantiates the class properties as the correct class type' do @new_reservation.reservation_id.must_be_kind_of Integer @@ -18,6 +13,19 @@ @new_reservation.check_out.must_be_kind_of Date end + it 'raises an error if provided an invalid date range' do + + # the start and end dates are the same + proc{ + @new_reservation = Hotel::Reservation.new({reservation_id: 1, room_id: 2, check_in: Date.new(2018, 5, 25), check_out: Date.new(2018, 5, 25)}) + }.must_raise StandardError + + # the start date is after the end date + proc{ + @new_reservation1 = Hotel::Reservation.new({reservation_id: 1, room_id: 2, check_in: Date.new(2018, 5, 25), check_out: Date.new(2018, 5, 19)}) + }.must_raise StandardError + end + end # end of initialize describe 'stay_date_list' do From b0cbbb52959106c39d59de7667323b8541169ec6 Mon Sep 17 00:00:00 2001 From: Hannah Cameron Date: Tue, 6 Mar 2018 16:00:32 -0800 Subject: [PATCH 10/18] added tests for HotelManager#find_available_rooms. code passing tests; 97% covered --- lib/hotel_manager.rb | 26 ++++++++++++++++++++++++++ specs/hotel_manager_spec.rb | 35 ++++++++++++++++++++++++++++++++--- 2 files changed, 58 insertions(+), 3 deletions(-) diff --git a/lib/hotel_manager.rb b/lib/hotel_manager.rb index 6125baf5a..6a69bbd3e 100644 --- a/lib/hotel_manager.rb +++ b/lib/hotel_manager.rb @@ -45,5 +45,31 @@ def reservations_by_date(date) return reservation_list end + def find_available_rooms(start_date, end_date) + if start_date == end_date + raise StandardError.new('The start date and the end date cannot be the same date.') + elsif start_date > end_date + raise StandardError.new('The end date cannot be before the start date.') + end + + potential_dates = (start_date..end_date).to_a + + conflicting_reservations = [] + + potential_dates.each do |date| + conflicting_reservations += reservations_by_date(date) + end + unavailable_rooms = [] + if conflicting_reservations.length > 0 + conflicting_reservations.each do |reservation| + unavailable_rooms << reservation.room_id + available_rooms = @room_list - unavailable_rooms + return available_rooms + end + else + return @room_list + end + end + end # end of class end # end of module diff --git a/specs/hotel_manager_spec.rb b/specs/hotel_manager_spec.rb index cd7d65d8b..8178bdd2e 100644 --- a/specs/hotel_manager_spec.rb +++ b/specs/hotel_manager_spec.rb @@ -92,13 +92,42 @@ end end - # it '' do - - # end end # end of describe reservations_by_date + describe 'find_available_rooms' do + before do + @begin_date = Date.new(2018, 5, 26) + @end_date = Date.new(2018, 5, 28) + @new_hotel = Hotel::HotelManager.new + end + + it 'returns correct size list when 1 overlapping date' do + # one reservation should affect returned array + @new_hotel.add_reservation(@begin_date - 2, @end_date + 3) + @new_hotel.add_reservation(@begin_date + 45, @end_date + 47) + @new_hotel.find_available_rooms(@begin_date, @end_date).length.must_equal 19 + end + + it 'returns correct size list when no overlapping dates' do + # neither reservation should affect returned array + @new_hotel.add_reservation(@begin_date - 20, @end_date - 15) + @new_hotel.add_reservation(@begin_date + 45, @end_date + 47) + @new_hotel.find_available_rooms(@begin_date, @end_date).length.must_equal 20 + end + + end # end of describe find_available_rooms + end # end of describe HotelManager + + + # Two should contain the overlap date + # @new_hotel.add_reservation(@begin_date - 1, @overlap_date + 1) + # @new_hotel.add_reservation(@begin_date- 5, @end_date + 3) + # + # # One should not + # @new_hotel.add_reservation(@begin_date - 20, @end_date - 15) + # check_in_date = Date.new(2018, 4, 15) # check_out_date = Date.new(2018, 4, 24) # @new_hotel.add_reservation(check_in_date, check_out_date) From e0f4fa7e598fb9e16c609c9ce2dd116e802aaaeb Mon Sep 17 00:00:00 2001 From: Hannah Cameron Date: Tue, 6 Mar 2018 16:36:00 -0800 Subject: [PATCH 11/18] modified HotelManager#reservations_by_date and #find_available_rooms to not include the last day when looking for reservation overlap --- lib/hotel_manager.rb | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/lib/hotel_manager.rb b/lib/hotel_manager.rb index 6a69bbd3e..688bb5077 100644 --- a/lib/hotel_manager.rb +++ b/lib/hotel_manager.rb @@ -29,16 +29,10 @@ def get_available_room return next_room_id end - def add_reservation(check_in_date, check_out_date) - new_reservation = Hotel::Reservation.new({reservation_id: get_reservation_id, room_id: get_available_room, check_in: check_in_date, check_out: check_out_date}) - reservations << new_reservation - return new_reservation - end - def reservations_by_date(date) reservation_list = [] @reservations.each do |reservation| - if reservation.stay_date_list.include?(date) + if reservation.stay_date_list[0..-2].include?(date) reservation_list << reservation end end @@ -52,14 +46,15 @@ def find_available_rooms(start_date, end_date) raise StandardError.new('The end date cannot be before the start date.') end - potential_dates = (start_date..end_date).to_a - + potential_dates = (start_date..(end_date - 1)).to_a conflicting_reservations = [] potential_dates.each do |date| conflicting_reservations += reservations_by_date(date) end + unavailable_rooms = [] + if conflicting_reservations.length > 0 conflicting_reservations.each do |reservation| unavailable_rooms << reservation.room_id @@ -67,9 +62,18 @@ def find_available_rooms(start_date, end_date) return available_rooms end else - return @room_list + available_rooms = @room_list + return available_rooms end end + + def add_reservation(check_in_date, check_out_date) + new_reservation = Hotel::Reservation.new({reservation_id: get_reservation_id, room_id: get_available_room, check_in: check_in_date, check_out: check_out_date}) + reservations << new_reservation + return new_reservation + end + + end # end of class end # end of module From 397f0584c783bdb58830515647bd8d9497454e0a Mon Sep 17 00:00:00 2001 From: Hannah Cameron Date: Wed, 7 Mar 2018 17:30:32 -0800 Subject: [PATCH 12/18] fixed failure of testing for HotelManager#Get_available_room; added in file structure for Block class --- lib/block.rb | 9 ++++++++ lib/hotel_manager.rb | 45 +++++++++++++++++++++---------------- specs/block_spec.rb | 4 ++++ specs/hotel_manager_spec.rb | 18 ++++++++++++++- specs/spec_helper.rb | 4 +++- 5 files changed, 59 insertions(+), 21 deletions(-) create mode 100644 lib/block.rb create mode 100644 specs/block_spec.rb diff --git a/lib/block.rb b/lib/block.rb new file mode 100644 index 000000000..72d2d8b9b --- /dev/null +++ b/lib/block.rb @@ -0,0 +1,9 @@ +# module Hotel +# class Reservation < Block +# +# def initialize +# super +# @ +# +# end +# end diff --git a/lib/hotel_manager.rb b/lib/hotel_manager.rb index 688bb5077..fda0f8380 100644 --- a/lib/hotel_manager.rb +++ b/lib/hotel_manager.rb @@ -1,5 +1,5 @@ -require 'pry' require 'date' +require 'pry' module Hotel class HotelManager @@ -8,10 +8,9 @@ class HotelManager attr_accessor :reservations def initialize - @room_list = load_rooms - @reservations = [] - + @reservations = Array.new + # puts "This is to check how often this is happening." end def load_rooms @@ -20,13 +19,13 @@ def load_rooms end def get_reservation_id - next_reservation_id = reservations.length + 1 - return next_reservation_id - end - - def get_available_room - next_room_id = room_list.sample - return next_room_id + if @reservations == [] + next_reservation_id = 1 + return next_reservation_id + else + next_reservation_id = @reservations.length + 1 + return next_reservation_id + end end def reservations_by_date(date) @@ -40,37 +39,45 @@ def reservations_by_date(date) end def find_available_rooms(start_date, end_date) - if start_date == end_date - raise StandardError.new('The start date and the end date cannot be the same date.') - elsif start_date > end_date - raise StandardError.new('The end date cannot be before the start date.') - end potential_dates = (start_date..(end_date - 1)).to_a + conflicting_reservations = [] potential_dates.each do |date| conflicting_reservations += reservations_by_date(date) end - + puts "*conflicting_reservations are #{conflicting_reservations}" unavailable_rooms = [] if conflicting_reservations.length > 0 conflicting_reservations.each do |reservation| unavailable_rooms << reservation.room_id available_rooms = @room_list - unavailable_rooms + puts "**Since the following are conflicting_reservations #{conflicting_reservations}, the available_rooms list is #{available_rooms}" return available_rooms end else available_rooms = @room_list + puts "**no conflicting_reservations, so available_rooms is #{available_rooms}" return available_rooms end end + def get_available_room(check_in_date, check_out_date) + potential_rooms = find_available_rooms(check_in_date, check_out_date) + puts "There are now #{potential_rooms.length} rooms" + if potential_rooms.length >= 1 + next_room_id = potential_rooms.first + return next_room_id + else + raise StandardError.new('There are no available rooms at this time.') + end + end def add_reservation(check_in_date, check_out_date) - new_reservation = Hotel::Reservation.new({reservation_id: get_reservation_id, room_id: get_available_room, check_in: check_in_date, check_out: check_out_date}) - reservations << new_reservation + new_reservation = Hotel::Reservation.new({reservation_id: get_reservation_id, room_id: get_available_room(check_in_date, check_out_date), check_in: check_in_date, check_out: check_out_date}) + @reservations.push(new_reservation) return new_reservation end diff --git a/specs/block_spec.rb b/specs/block_spec.rb new file mode 100644 index 000000000..d56bdcd4c --- /dev/null +++ b/specs/block_spec.rb @@ -0,0 +1,4 @@ +require_relative "spec_helper.rb" + +describe 'reservation' do +end diff --git a/specs/hotel_manager_spec.rb b/specs/hotel_manager_spec.rb index 8178bdd2e..6ff67caac 100644 --- a/specs/hotel_manager_spec.rb +++ b/specs/hotel_manager_spec.rb @@ -43,12 +43,15 @@ @new_hotel.reservations.length.must_equal 3 @new_hotel.get_reservation_id.must_equal 4 end + end # end of describe 'get_reservation_id' describe 'get_available_room' do it 'must return an appropriate room id' do @new_hotel = Hotel::HotelManager.new - room_id = @new_hotel.get_available_room + @start_date = Date.new(2018, 5, 26) + @end_date = Date.new(2018, 5, 28) + room_id = @new_hotel.get_available_room(@start_date, @end_date) room_id.must_be_kind_of Integer room_id.must_be :>, 0 room_id.must_be :<, 21 @@ -81,9 +84,11 @@ # One should not @new_hotel.add_reservation(@overlap_date - 20, @overlap_date - 15) + # puts "This is to check what is in #{@new_hotel}" end it 'returns an array of reservations for a specific date' do + # binding.pry reservation_list = @new_hotel.reservations_by_date(@overlap_date) reservation_list.must_be_instance_of Array reservation_list.length.must_equal 2 @@ -115,6 +120,17 @@ @new_hotel.find_available_rooms(@begin_date, @end_date).length.must_equal 20 end + it 'raises an error if there are no available rooms' do + proc{ + @new_hotel = Hotel::HotelManager.new + check_in_date = Date.new(2018, 3, 12) + check_out_date = Date.new(2018, 3, 20) + 21.times do + @new_hotel.add_reservation(check_in_date, check_out_date) + end + @new_hotel.find_available_rooms(Date.new(2018, 3, 16), Date.new(2018, 3, 18)) + }.must_raise StandardError + end end # end of describe find_available_rooms end # end of describe HotelManager diff --git a/specs/spec_helper.rb b/specs/spec_helper.rb index a67812ec4..2e2f3f59d 100644 --- a/specs/spec_helper.rb +++ b/specs/spec_helper.rb @@ -1,5 +1,7 @@ require 'simplecov' -SimpleCov.start +SimpleCov.start do + add_filter "/specs/" +end require 'minitest' require 'minitest/autorun' require 'minitest/reporters' From 297eae8f586a2b7586f99ee55501a55234361904 Mon Sep 17 00:00:00 2001 From: Hannah Cameron Date: Thu, 8 Mar 2018 17:09:19 -0800 Subject: [PATCH 13/18] fixed my errors with hotelmanager#get_available_rooms. passing all tests for wave 2 - meeting all requirements --- lib/hotel_manager.rb | 72 ++++++++++++++++++++++++------------- lib/reservation.rb | 1 + specs/block_spec.rb | 8 ++--- specs/hotel_manager_spec.rb | 10 +++--- specs/reservation_spec.rb | 2 +- 5 files changed, 60 insertions(+), 33 deletions(-) diff --git a/lib/hotel_manager.rb b/lib/hotel_manager.rb index fda0f8380..c4f108047 100644 --- a/lib/hotel_manager.rb +++ b/lib/hotel_manager.rb @@ -10,7 +10,7 @@ class HotelManager def initialize @room_list = load_rooms @reservations = Array.new - # puts "This is to check how often this is happening." + puts "A new hotel manager has been created." end def load_rooms @@ -21,9 +21,11 @@ def load_rooms def get_reservation_id if @reservations == [] next_reservation_id = 1 + puts "The next reservation will be #{next_reservation_id}" return next_reservation_id else next_reservation_id = @reservations.length + 1 + puts "The next reservation will be #{next_reservation_id}" return next_reservation_id end end @@ -31,7 +33,7 @@ def get_reservation_id def reservations_by_date(date) reservation_list = [] @reservations.each do |reservation| - if reservation.stay_date_list[0..-2].include?(date) + if reservation.stay_date_list.include?(date) reservation_list << reservation end end @@ -42,42 +44,64 @@ def find_available_rooms(start_date, end_date) potential_dates = (start_date..(end_date - 1)).to_a - conflicting_reservations = [] - - potential_dates.each do |date| - conflicting_reservations += reservations_by_date(date) - end - puts "*conflicting_reservations are #{conflicting_reservations}" unavailable_rooms = [] - if conflicting_reservations.length > 0 - conflicting_reservations.each do |reservation| + @reservations.each do |reservation| + # date_check_list is list of all dates in a reservation + date_check_list = reservation.stay_date_list + checked_date_list = date_check_list - potential_dates + if checked_date_list.length < date_check_list.length unavailable_rooms << reservation.room_id - available_rooms = @room_list - unavailable_rooms - puts "**Since the following are conflicting_reservations #{conflicting_reservations}, the available_rooms list is #{available_rooms}" - return available_rooms end - else - available_rooms = @room_list - puts "**no conflicting_reservations, so available_rooms is #{available_rooms}" - return available_rooms end + + if unavailable_rooms.length == 20 + raise StandardError.new('There are no available rooms at this time.') + elsif unavailable_rooms.length > 0 + available_rooms = @room_list - unavailable_rooms + else + available_rooms = @room_list + end + return available_rooms end + # potential_dates.each do |date| + # if + # conflicting_reservations += reservations_by_date(date) + # end + # puts "*There are #{conflicting_reservations.length} conflicting_reservations " + # unavailable_rooms = [] + # + # if unavailable_rooms.length == 20 + # raise StandardError.new('There are no available rooms at this time.') + # elsif conflicting_reservations.length > 0 + # conflicting_reservations.each do |reservation| + # unavailable_rooms << reservation.room_id + # available_rooms = @room_list - unavailable_rooms + # puts "**There are #{unavailable_rooms.length} unavailable rooms, the available_rooms list is #{available_rooms}" + # return available_rooms + # end + # else + # available_rooms = @room_list + # puts "**no conflicting_reservations, so available_rooms is #{available_rooms}" + # return available_rooms + # end + def get_available_room(check_in_date, check_out_date) potential_rooms = find_available_rooms(check_in_date, check_out_date) - puts "There are now #{potential_rooms.length} rooms" - if potential_rooms.length >= 1 - next_room_id = potential_rooms.first - return next_room_id - else - raise StandardError.new('There are no available rooms at this time.') - end + # if potential_rooms.length >= 1 + next_room_id = potential_rooms.first + puts "the next room id will be #{next_room_id}" + return next_room_id + # else + # raise StandardError.new('There are no available rooms at this time.') + # end end def add_reservation(check_in_date, check_out_date) new_reservation = Hotel::Reservation.new({reservation_id: get_reservation_id, room_id: get_available_room(check_in_date, check_out_date), check_in: check_in_date, check_out: check_out_date}) @reservations.push(new_reservation) + puts "the new reservation is #{new_reservation}." return new_reservation end diff --git a/lib/reservation.rb b/lib/reservation.rb index b33ed8d7f..b3668b4d0 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -21,6 +21,7 @@ def initialize(input) def stay_date_list list_of_dates = (@check_in..@check_out).to_a + list_of_dates = list_of_dates[0..-2] return list_of_dates end diff --git a/specs/block_spec.rb b/specs/block_spec.rb index d56bdcd4c..8bd1db1fe 100644 --- a/specs/block_spec.rb +++ b/specs/block_spec.rb @@ -1,4 +1,4 @@ -require_relative "spec_helper.rb" - -describe 'reservation' do -end +# require_relative "spec_helper.rb" +# +# describe 'reservation' do +# end diff --git a/specs/hotel_manager_spec.rb b/specs/hotel_manager_spec.rb index 6ff67caac..31b1be6b1 100644 --- a/specs/hotel_manager_spec.rb +++ b/specs/hotel_manager_spec.rb @@ -100,13 +100,12 @@ end # end of describe reservations_by_date describe 'find_available_rooms' do - before do + + it 'returns correct size list when 1 overlapping date' do @begin_date = Date.new(2018, 5, 26) @end_date = Date.new(2018, 5, 28) @new_hotel = Hotel::HotelManager.new - end - it 'returns correct size list when 1 overlapping date' do # one reservation should affect returned array @new_hotel.add_reservation(@begin_date - 2, @end_date + 3) @new_hotel.add_reservation(@begin_date + 45, @end_date + 47) @@ -114,6 +113,9 @@ end it 'returns correct size list when no overlapping dates' do + @begin_date = Date.new(2018, 5, 26) + @end_date = Date.new(2018, 5, 28) + @new_hotel = Hotel::HotelManager.new # neither reservation should affect returned array @new_hotel.add_reservation(@begin_date - 20, @end_date - 15) @new_hotel.add_reservation(@begin_date + 45, @end_date + 47) @@ -128,7 +130,7 @@ 21.times do @new_hotel.add_reservation(check_in_date, check_out_date) end - @new_hotel.find_available_rooms(Date.new(2018, 3, 16), Date.new(2018, 3, 18)) + @new_hotel.find_available_rooms(Date.new(2018, 3, 12), Date.new(2018, 3, 20)) }.must_raise StandardError end end # end of describe find_available_rooms diff --git a/specs/reservation_spec.rb b/specs/reservation_spec.rb index e5b06679b..598c2d150 100644 --- a/specs/reservation_spec.rb +++ b/specs/reservation_spec.rb @@ -37,7 +37,7 @@ date_list = @new_reservation.stay_date_list date_list.must_be_kind_of Array date_list[0].must_be_instance_of Date - date_list.length.must_equal 3 + date_list.length.must_equal 2 end end From 1dedcdedf46c819461325dcbe48a7927926b279a Mon Sep 17 00:00:00 2001 From: Hannah Cameron Date: Thu, 8 Mar 2018 17:18:45 -0800 Subject: [PATCH 14/18] cleaned up spacing and indentation for hotelmanager.rb and reservaion.rb --- lib/hotel_manager.rb | 34 ++-------------------------------- lib/reservation.rb | 2 +- 2 files changed, 3 insertions(+), 33 deletions(-) diff --git a/lib/hotel_manager.rb b/lib/hotel_manager.rb index c4f108047..9f6c82aa0 100644 --- a/lib/hotel_manager.rb +++ b/lib/hotel_manager.rb @@ -19,6 +19,7 @@ def load_rooms end def get_reservation_id + if @reservations == [] next_reservation_id = 1 puts "The next reservation will be #{next_reservation_id}" @@ -41,13 +42,9 @@ def reservations_by_date(date) end def find_available_rooms(start_date, end_date) - potential_dates = (start_date..(end_date - 1)).to_a - unavailable_rooms = [] - @reservations.each do |reservation| - # date_check_list is list of all dates in a reservation date_check_list = reservation.stay_date_list checked_date_list = date_check_list - potential_dates if checked_date_list.length < date_check_list.length @@ -64,38 +61,12 @@ def find_available_rooms(start_date, end_date) end return available_rooms end - # potential_dates.each do |date| - # if - # conflicting_reservations += reservations_by_date(date) - # end - # puts "*There are #{conflicting_reservations.length} conflicting_reservations " - # unavailable_rooms = [] - # - # if unavailable_rooms.length == 20 - # raise StandardError.new('There are no available rooms at this time.') - # elsif conflicting_reservations.length > 0 - # conflicting_reservations.each do |reservation| - # unavailable_rooms << reservation.room_id - # available_rooms = @room_list - unavailable_rooms - # puts "**There are #{unavailable_rooms.length} unavailable rooms, the available_rooms list is #{available_rooms}" - # return available_rooms - # end - # else - # available_rooms = @room_list - # puts "**no conflicting_reservations, so available_rooms is #{available_rooms}" - # return available_rooms - # end - def get_available_room(check_in_date, check_out_date) potential_rooms = find_available_rooms(check_in_date, check_out_date) - # if potential_rooms.length >= 1 next_room_id = potential_rooms.first puts "the next room id will be #{next_room_id}" return next_room_id - # else - # raise StandardError.new('There are no available rooms at this time.') - # end end def add_reservation(check_in_date, check_out_date) @@ -104,7 +75,6 @@ def add_reservation(check_in_date, check_out_date) puts "the new reservation is #{new_reservation}." return new_reservation end - - + end # end of class end # end of module diff --git a/lib/reservation.rb b/lib/reservation.rb index b3668b4d0..5a39a06ff 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -1,7 +1,7 @@ module Hotel class Reservation - RATE = 200.00 + RATE = 200.00 attr_accessor :reservation_id, :room_id, :check_in, :check_out From 99742bb07d9b695835654bd5d511eaf3f46044e5 Mon Sep 17 00:00:00 2001 From: Hannah Cameron Date: Sun, 11 Mar 2018 12:45:46 -0700 Subject: [PATCH 15/18] added in initialize tests for block - passing all tests. minor formatting changes made in hotelmanager and reservation --- lib/block.rb | 29 ++++++++++++++++++++--------- lib/hotel_manager.rb | 2 +- lib/reservation.rb | 3 +++ specs/block_spec.rb | 22 ++++++++++++++++++---- specs/hotel_manager_spec.rb | 2 -- specs/spec_helper.rb | 1 + 6 files changed, 43 insertions(+), 16 deletions(-) diff --git a/lib/block.rb b/lib/block.rb index 72d2d8b9b..5323b74e5 100644 --- a/lib/block.rb +++ b/lib/block.rb @@ -1,9 +1,20 @@ -# module Hotel -# class Reservation < Block -# -# def initialize -# super -# @ -# -# end -# end +require 'date' +require 'pry' + +module Hotel + class Block < Reservation + + RATE = 175.00 + + attr_accessor :block_size, :reserved_rooms + + def initialize(input) + super + + @block_size = input[:block_size] + @reserved_rooms = Array.new + + end + + end # end of block +end # end of module diff --git a/lib/hotel_manager.rb b/lib/hotel_manager.rb index 9f6c82aa0..af8765472 100644 --- a/lib/hotel_manager.rb +++ b/lib/hotel_manager.rb @@ -75,6 +75,6 @@ def add_reservation(check_in_date, check_out_date) puts "the new reservation is #{new_reservation}." return new_reservation end - + end # end of class end # end of module diff --git a/lib/reservation.rb b/lib/reservation.rb index 5a39a06ff..f2a242e2d 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -1,3 +1,6 @@ +require 'date' +require 'pry' + module Hotel class Reservation diff --git a/specs/block_spec.rb b/specs/block_spec.rb index 8bd1db1fe..d1ab0f65a 100644 --- a/specs/block_spec.rb +++ b/specs/block_spec.rb @@ -1,4 +1,18 @@ -# require_relative "spec_helper.rb" -# -# describe 'reservation' do -# end +require_relative "spec_helper.rb" + +describe 'block' do + describe 'initialize' do + before do + input = {reservation_id: 1, block_size: 2, check_in: Date.new(2018, 7, 06), check_out: Date.new(2018, 7, 10)} + @new_block = Hotel::Block.new(input) + end + it 'instantiates the class properties as the correct class type' do + @new_block.reservation_id.must_be_kind_of Integer + @new_block.block_size.must_be_kind_of Integer + @new_block.check_in.must_be_kind_of Date + @new_block.check_out.must_be_kind_of Date + @new_block.reserved_rooms.must_be_kind_of Array + end + end # end of describe initialize + +end # end of describe block diff --git a/specs/hotel_manager_spec.rb b/specs/hotel_manager_spec.rb index 31b1be6b1..9875c0f8f 100644 --- a/specs/hotel_manager_spec.rb +++ b/specs/hotel_manager_spec.rb @@ -1,7 +1,5 @@ -require 'pry' require_relative "spec_helper.rb" - describe 'HotelManager' do describe 'initialize' do before do diff --git a/specs/spec_helper.rb b/specs/spec_helper.rb index 2e2f3f59d..0ce377c57 100644 --- a/specs/spec_helper.rb +++ b/specs/spec_helper.rb @@ -12,3 +12,4 @@ # Require_relative your lib files here! require_relative '../lib/hotel_manager' require_relative '../lib/reservation' +require_relative '../lib/block' From 0ebad6ea44ec856b874bfc40afd976291ed37838 Mon Sep 17 00:00:00 2001 From: Hannah Cameron Date: Sun, 11 Mar 2018 13:00:42 -0700 Subject: [PATCH 16/18] block#calculate_cost method passing tests --- lib/block.rb | 8 +++++++- lib/hotel_manager.rb | 15 ++++++++++----- specs/block_spec.rb | 27 ++++++++++++++++++++++++--- 3 files changed, 41 insertions(+), 9 deletions(-) diff --git a/lib/block.rb b/lib/block.rb index 5323b74e5..740541a98 100644 --- a/lib/block.rb +++ b/lib/block.rb @@ -4,7 +4,7 @@ module Hotel class Block < Reservation - RATE = 175.00 + RATE_DISCOUNT = 0.8 attr_accessor :block_size, :reserved_rooms @@ -16,5 +16,11 @@ def initialize(input) end + def calculate_cost + duration = Integer(check_out- check_in) + reservation_cost = duration * (RATE * RATE_DISCOUNT) + return reservation_cost + end + end # end of block end # end of module diff --git a/lib/hotel_manager.rb b/lib/hotel_manager.rb index af8765472..2dd81131b 100644 --- a/lib/hotel_manager.rb +++ b/lib/hotel_manager.rb @@ -10,7 +10,7 @@ class HotelManager def initialize @room_list = load_rooms @reservations = Array.new - puts "A new hotel manager has been created." + # puts "A new hotel manager has been created." end def load_rooms @@ -22,11 +22,11 @@ def get_reservation_id if @reservations == [] next_reservation_id = 1 - puts "The next reservation will be #{next_reservation_id}" + # puts "The next reservation will be #{next_reservation_id}" return next_reservation_id else next_reservation_id = @reservations.length + 1 - puts "The next reservation will be #{next_reservation_id}" + # puts "The next reservation will be #{next_reservation_id}" return next_reservation_id end end @@ -65,16 +65,21 @@ def find_available_rooms(start_date, end_date) def get_available_room(check_in_date, check_out_date) potential_rooms = find_available_rooms(check_in_date, check_out_date) next_room_id = potential_rooms.first - puts "the next room id will be #{next_room_id}" + # puts "the next room id will be #{next_room_id}" return next_room_id end def add_reservation(check_in_date, check_out_date) new_reservation = Hotel::Reservation.new({reservation_id: get_reservation_id, room_id: get_available_room(check_in_date, check_out_date), check_in: check_in_date, check_out: check_out_date}) @reservations.push(new_reservation) - puts "the new reservation is #{new_reservation}." + # puts "the new reservation is #{new_reservation}." return new_reservation end + def add_block(block_size, check_in_date, check_out_date) + new_block = Hotel::Block.new({reservation_id: get_reservation_id, block_size: block_size, check_in: check_in_date, check_out: check_out_date}) + @reservations.push(new_reservation) + end + end # end of class end # end of module diff --git a/specs/block_spec.rb b/specs/block_spec.rb index d1ab0f65a..11ec43a76 100644 --- a/specs/block_spec.rb +++ b/specs/block_spec.rb @@ -2,17 +2,38 @@ describe 'block' do describe 'initialize' do - before do + it 'instantiates the class properties as the correct class type' do input = {reservation_id: 1, block_size: 2, check_in: Date.new(2018, 7, 06), check_out: Date.new(2018, 7, 10)} @new_block = Hotel::Block.new(input) - end - it 'instantiates the class properties as the correct class type' do + @new_block.reservation_id.must_be_kind_of Integer @new_block.block_size.must_be_kind_of Integer @new_block.check_in.must_be_kind_of Date @new_block.check_out.must_be_kind_of Date @new_block.reserved_rooms.must_be_kind_of Array end + + it 'raises an error if provided an invalid date range' do + + # the start and end dates are the same + proc{ + @new_block = Hotel::Block({reservation_id: 1, block_size: 2, check_in: Date.new(2018, 5, 25), check_out: Date.new(2018, 5, 25)}) + }.must_raise StandardError + + # the start date is after the end date + proc{ + @new_block1 = Hotel::Block({reservation_id: 1, block_size: 2, check_in: Date.new(2018, 5, 25), check_out: Date.new(2018, 5, 19)}) + }.must_raise StandardError + end end # end of describe initialize + describe 'calculate_cost' do + it 'calculates cost based on block rate not reservation rate' do + input = {reservation_id: 1, block_size: 2, check_in: Date.new(2018, 7, 06), check_out: Date.new(2018, 7, 10)} + @new_block = Hotel::Block.new(input) + + @new_block.calculate_cost.must_equal 640 + end + end + end # end of describe block From 44b1b751b9934bc24c67915aaa01e983a2bcf191 Mon Sep 17 00:00:00 2001 From: Hannah Cameron Date: Sun, 11 Mar 2018 13:48:45 -0700 Subject: [PATCH 17/18] added in exception handling for if a block size is over 5. --- lib/block.rb | 8 ++++++++ lib/hotel_manager.rb | 2 +- specs/block_spec.rb | 11 ++++++++++- 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/block.rb b/lib/block.rb index 740541a98..199b68abd 100644 --- a/lib/block.rb +++ b/lib/block.rb @@ -14,6 +14,12 @@ def initialize(input) @block_size = input[:block_size] @reserved_rooms = Array.new + if block_size > 5 + raise StandardError.new('The maximum bock size is 5 rooms.') + # elsif block_size < 2 + # raise StandardError.new('The minimum block size is 2 rooms.') + end + end def calculate_cost @@ -22,5 +28,7 @@ def calculate_cost return reservation_cost end + + end # end of block end # end of module diff --git a/lib/hotel_manager.rb b/lib/hotel_manager.rb index 2dd81131b..0f7f199cc 100644 --- a/lib/hotel_manager.rb +++ b/lib/hotel_manager.rb @@ -78,7 +78,7 @@ def add_reservation(check_in_date, check_out_date) def add_block(block_size, check_in_date, check_out_date) new_block = Hotel::Block.new({reservation_id: get_reservation_id, block_size: block_size, check_in: check_in_date, check_out: check_out_date}) - @reservations.push(new_reservation) + @reservations.push(new_block) end end # end of class diff --git a/specs/block_spec.rb b/specs/block_spec.rb index 11ec43a76..db6347f7e 100644 --- a/specs/block_spec.rb +++ b/specs/block_spec.rb @@ -25,6 +25,14 @@ @new_block1 = Hotel::Block({reservation_id: 1, block_size: 2, check_in: Date.new(2018, 5, 25), check_out: Date.new(2018, 5, 19)}) }.must_raise StandardError end + + it 'raises an error if provided an invalid date range' do + + # the start and end dates are the same + proc{ + @new_block = Hotel::Block({reservation_id: 1, block_size: 6, check_in: Date.new(2018, 5, 25), check_out: Date.new(2018, 5, 26)}) + }.must_raise StandardError + end end # end of describe initialize describe 'calculate_cost' do @@ -36,4 +44,5 @@ end end -end # end of describe block + +end # end of describe Block From 005dff67bd90347349184e4790785ff3a5607945 Mon Sep 17 00:00:00 2001 From: Hannah Cameron Date: Sun, 11 Mar 2018 13:52:10 -0700 Subject: [PATCH 18/18] added in exception handling for if a block size is under 2 --- lib/block.rb | 4 ++-- specs/block_spec.rb | 7 +++++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/lib/block.rb b/lib/block.rb index 199b68abd..25e892b12 100644 --- a/lib/block.rb +++ b/lib/block.rb @@ -16,8 +16,8 @@ def initialize(input) if block_size > 5 raise StandardError.new('The maximum bock size is 5 rooms.') - # elsif block_size < 2 - # raise StandardError.new('The minimum block size is 2 rooms.') + elsif block_size < 2 + raise StandardError.new('The minimum block size is 2 rooms.') end end diff --git a/specs/block_spec.rb b/specs/block_spec.rb index db6347f7e..c9c3f4252 100644 --- a/specs/block_spec.rb +++ b/specs/block_spec.rb @@ -26,12 +26,15 @@ }.must_raise StandardError end - it 'raises an error if provided an invalid date range' do + it 'raises an error if provided an invalid block size' do - # the start and end dates are the same proc{ @new_block = Hotel::Block({reservation_id: 1, block_size: 6, check_in: Date.new(2018, 5, 25), check_out: Date.new(2018, 5, 26)}) }.must_raise StandardError + + proc{ + @new_block = Hotel::Block({reservation_id: 1, block_size: 1, check_in: Date.new(2018, 5, 25), check_out: Date.new(2018, 5, 26)}) + }.must_raise StandardError end end # end of describe initialize