From cb02ee6df0a1e08d730b622ba01f90d85c268dc8 Mon Sep 17 00:00:00 2001 From: Kaitlin Forsman Date: Mon, 5 Mar 2018 12:47:38 -0800 Subject: [PATCH 01/34] initial setup of lib and spec files so get ready to write tests and solutions --- Rakefile | 9 +++++++++ lib/reservation.rb | 6 ++++++ lib/user.rb | 6 ++++++ specs/reservation_spec.rb | 2 ++ specs/spec_helper.rb | 13 +++++++++++++ specs/user_spec.rb | 2 ++ 6 files changed, 38 insertions(+) create mode 100644 Rakefile create mode 100644 lib/reservation.rb create mode 100644 lib/user.rb create mode 100644 specs/reservation_spec.rb create mode 100644 specs/spec_helper.rb create mode 100644 specs/user_spec.rb diff --git a/Rakefile b/Rakefile new file mode 100644 index 000000000..deb52f2cd --- /dev/null +++ b/Rakefile @@ -0,0 +1,9 @@ +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/reservation.rb b/lib/reservation.rb new file mode 100644 index 000000000..5b3a2fb3b --- /dev/null +++ b/lib/reservation.rb @@ -0,0 +1,6 @@ +require 'date' + +module Hotel + class Reservation + end +end diff --git a/lib/user.rb b/lib/user.rb new file mode 100644 index 000000000..3d7846fed --- /dev/null +++ b/lib/user.rb @@ -0,0 +1,6 @@ +require 'date' + +module Hotel + class User + end +end diff --git a/specs/reservation_spec.rb b/specs/reservation_spec.rb new file mode 100644 index 000000000..04488af39 --- /dev/null +++ b/specs/reservation_spec.rb @@ -0,0 +1,2 @@ +require_relative 'spec_helper' +require 'date' diff --git a/specs/spec_helper.rb b/specs/spec_helper.rb new file mode 100644 index 000000000..90790c660 --- /dev/null +++ b/specs/spec_helper.rb @@ -0,0 +1,13 @@ +require 'simplecov' +SimpleCov.start + + +require 'minitest' +require 'minitest/autorun' +require 'minitest/reporters' +require 'minitest/skip_dsl' + +Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new + +require_relative '../lib/user' +require_relative '../lib/reservation' diff --git a/specs/user_spec.rb b/specs/user_spec.rb new file mode 100644 index 000000000..04488af39 --- /dev/null +++ b/specs/user_spec.rb @@ -0,0 +1,2 @@ +require_relative 'spec_helper' +require 'date' From 385592ed403be8032b1df5e0fde8a3ea8bc6b57a Mon Sep 17 00:00:00 2001 From: Kaitlin Forsman Date: Mon, 5 Mar 2018 13:26:26 -0800 Subject: [PATCH 02/34] stubbed some tests for user class and wrote tests and code for user initialize method approach --- lib/user.rb | 8 ++++++++ specs/user_spec.rb | 31 +++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/lib/user.rb b/lib/user.rb index 3d7846fed..4b25b036e 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -1,6 +1,14 @@ require 'date' +require 'pry' module Hotel + TOTAL_ROOMS = 20 + COST_PER_NIGHT = 200 class User + attr_reader :rooms + def initialize + @rooms = [] + TOTAL_ROOMS.times { | room_num | @rooms << room_num + 1} + end end end diff --git a/specs/user_spec.rb b/specs/user_spec.rb index 04488af39..8e017e787 100644 --- a/specs/user_spec.rb +++ b/specs/user_spec.rb @@ -1,2 +1,33 @@ require_relative 'spec_helper' require 'date' + +describe 'User' do + describe 'initialization' do + before do + @admin = Hotel::User.new + end + it 'can be initialized' do + @admin.must_be_instance_of Hotel::User + end + it 'has a list of rooms' do + # hotel_rooms = @admin.rooms + + @admin.must_respond_to :rooms + end + end + xdescribe 'reserve_room' do + it 'throws StandardError for invalide dates' do + end + it 'returns an instance of a reservation' do + end + end + xdescribe 'find_reservations_for_given_date' do + it 'returns array of reservations for a given date' do + end + end + xdescribe 'calculate_reservation_cost' do + it 'returns cost for reservation' do + end + end + +end From b9f7f432b685f5f46bebb4922702a1cb0e1b1b11 Mon Sep 17 00:00:00 2001 From: Kaitlin Forsman Date: Mon, 5 Mar 2018 14:21:35 -0800 Subject: [PATCH 03/34] added errors for invalid inputs to reserve_room method --- lib/user.rb | 9 +++++++++ specs/user_spec.rb | 17 +++++++++++++---- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/lib/user.rb b/lib/user.rb index 4b25b036e..bf6dd3b6a 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -10,5 +10,14 @@ def initialize @rooms = [] TOTAL_ROOMS.times { | room_num | @rooms << room_num + 1} end + def reserve_room(guest, start_date, end_date) + if start_date.class != Date || end_date.class != Date + raise StandardError.new("not a date") + elsif end_date < start_date + raise ArgumentError.new("End date (#{end_date}) comes before start date (#{start_date})") + elsif end_date == start_date + raise ArgumentError.new("End date (#{end_date}) is same as start date (#{start_date})") + end + end end end diff --git a/specs/user_spec.rb b/specs/user_spec.rb index 8e017e787..8f7db3533 100644 --- a/specs/user_spec.rb +++ b/specs/user_spec.rb @@ -10,13 +10,22 @@ @admin.must_be_instance_of Hotel::User end it 'has a list of rooms' do - # hotel_rooms = @admin.rooms - @admin.must_respond_to :rooms end end - xdescribe 'reserve_room' do - it 'throws StandardError for invalide dates' do + describe 'reserve_room' do + before do + @admin = Hotel::User.new + end + it 'throws StandardError for invalidate dates' do + proc { @admin.reserve_room("Jade Poe", nil, Date.new(2018,3,5))}.must_raise StandardError + proc { @admin.reserve_room("Jade Poe", Date.new(2018,3,5), nil)}.must_raise StandardError + end + it 'throws ArgumentError if end_date occurs before start_date' do + proc { @admin.reserve_room("Jade Poe", Date.new(2018, 3, 10), Date.new(2018, 3, 5))}.must_raise ArgumentError + end + it 'throws ArgumentError if start_date is same as end_date' do + proc { @admin.reserve_room("Jade Poe", Date.new(2018, 3, 10), Date.new(2018, 3, 10))}.must_raise ArgumentError end it 'returns an instance of a reservation' do end From 40a50e51047e5eabc4ab89226dfb760c9fbd14cf Mon Sep 17 00:00:00 2001 From: Kaitlin Forsman Date: Mon, 5 Mar 2018 14:49:13 -0800 Subject: [PATCH 04/34] small refactor of tests and code --- lib/user.rb | 4 ++-- specs/user_spec.rb | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/user.rb b/lib/user.rb index bf6dd3b6a..098be36b1 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -14,9 +14,9 @@ def reserve_room(guest, start_date, end_date) if start_date.class != Date || end_date.class != Date raise StandardError.new("not a date") elsif end_date < start_date - raise ArgumentError.new("End date (#{end_date}) comes before start date (#{start_date})") + raise StandardError.new("End date (#{end_date}) comes before start date (#{start_date})") elsif end_date == start_date - raise ArgumentError.new("End date (#{end_date}) is same as start date (#{start_date})") + raise StandardError.new("End date (#{end_date}) is same as start date (#{start_date})") end end end diff --git a/specs/user_spec.rb b/specs/user_spec.rb index 8f7db3533..593c254ac 100644 --- a/specs/user_spec.rb +++ b/specs/user_spec.rb @@ -22,10 +22,10 @@ proc { @admin.reserve_room("Jade Poe", Date.new(2018,3,5), nil)}.must_raise StandardError end it 'throws ArgumentError if end_date occurs before start_date' do - proc { @admin.reserve_room("Jade Poe", Date.new(2018, 3, 10), Date.new(2018, 3, 5))}.must_raise ArgumentError + proc { @admin.reserve_room("Jade Poe", Date.new(2018, 3, 10), Date.new(2018, 3, 5))}.must_raise StandardError end it 'throws ArgumentError if start_date is same as end_date' do - proc { @admin.reserve_room("Jade Poe", Date.new(2018, 3, 10), Date.new(2018, 3, 10))}.must_raise ArgumentError + proc { @admin.reserve_room("Jade Poe", Date.new(2018, 3, 10), Date.new(2018, 3, 10))}.must_raise StandardError end it 'returns an instance of a reservation' do end From 31315fbe7fd82d6344f8cdafa20b9b56ad728d62 Mon Sep 17 00:00:00 2001 From: Kaitlin Forsman Date: Mon, 5 Mar 2018 15:07:06 -0800 Subject: [PATCH 05/34] initialized reservation class with attributes: id, room_num, guest, start_date, end_date --- lib/reservation.rb | 8 ++++++++ specs/reservation_spec.rb | 26 ++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/lib/reservation.rb b/lib/reservation.rb index 5b3a2fb3b..8cf617d70 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -2,5 +2,13 @@ module Hotel class Reservation + attr_reader :id, :room_num, :guest, :start_date, :end_date + def initialize id, room_num, guest, start_date, end_date + @id = id + @room_num = room_num + @guest = guest + @start_date = start_date + @end_date = end_date + end end end diff --git a/specs/reservation_spec.rb b/specs/reservation_spec.rb index 04488af39..b5a4a00f2 100644 --- a/specs/reservation_spec.rb +++ b/specs/reservation_spec.rb @@ -1,2 +1,28 @@ require_relative 'spec_helper' require 'date' + +describe 'Reservation' do + before do + @reservation = Hotel::Reservation.new(1, 4, "Bob", Date.new(2018,3,26), Date.new(2018,3,18)) + end + describe 'initialization' do + it 'can be initialized' do + @reservation.must_be_instance_of Hotel::Reservation + end + it 'has attributes: id, room_num, guest, start_date, end_date' do + @reservation.id.must_equal 1 + @reservation.id.must_be_kind_of Integer + @reservation.room_num.must_equal 4 + @reservation.room_num.must_be_kind_of Integer + @reservation.guest.must_equal "Bob" + @reservation.guest.must_be_kind_of String + @reservation.start_date.must_be_instance_of Date + @reservation.end_date.must_be_instance_of Date + end + end + describe 'calculate_nights' do + it 'returns the number of nights' do + + end + end +end From 3994a2342f4f45da0470baef21fb53e1d42c2fa9 Mon Sep 17 00:00:00 2001 From: Kaitlin Forsman Date: Mon, 5 Mar 2018 15:25:54 -0800 Subject: [PATCH 06/34] added night calculator method to reservation class --- lib/reservation.rb | 4 ++++ specs/reservation_spec.rb | 6 ++++++ 2 files changed, 10 insertions(+) diff --git a/lib/reservation.rb b/lib/reservation.rb index 8cf617d70..00fbe4195 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -10,5 +10,9 @@ def initialize id, room_num, guest, start_date, end_date @start_date = start_date @end_date = end_date end + + def calculate_nights + (start_date - end_date).to_i + end end end diff --git a/specs/reservation_spec.rb b/specs/reservation_spec.rb index b5a4a00f2..f967bcdef 100644 --- a/specs/reservation_spec.rb +++ b/specs/reservation_spec.rb @@ -21,8 +21,14 @@ end end describe 'calculate_nights' do + before do + @reservation = Hotel::Reservation.new(1, 4, "Bob", Date.new(2018,3,26), Date.new(2018,3,18)) + end it 'returns the number of nights' do + nights = @reservation.calculate_nights + nights.must_be_kind_of Integer + nights.must_equal 8 end end end From 9b9e88f706222c6e4e87c5074d84f4e91b479532 Mon Sep 17 00:00:00 2001 From: Kaitlin Forsman Date: Mon, 5 Mar 2018 16:10:40 -0800 Subject: [PATCH 07/34] trying to structure how to store dates and reservations in relation to each other and within the user class --- lib/reservation.rb | 13 ++++++++++++- lib/user.rb | 13 ++++++++++++- specs/reservation_spec.rb | 13 +++++++++++-- specs/user_spec.rb | 15 +++++++++++---- 4 files changed, 46 insertions(+), 8 deletions(-) diff --git a/lib/reservation.rb b/lib/reservation.rb index 00fbe4195..4a892a7b1 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -12,7 +12,18 @@ def initialize id, room_num, guest, start_date, end_date end def calculate_nights - (start_date - end_date).to_i + (end_date - start_date).to_i end + + def find_all_dates + date_range = [] + date = @start_date + while date <= @end_date + date_range << date + date += 1 + end + date_range + end + end end diff --git a/lib/user.rb b/lib/user.rb index 098be36b1..8b53e985e 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -8,9 +8,19 @@ class User attr_reader :rooms def initialize @rooms = [] + @reservations = [] + @calendar = {} TOTAL_ROOMS.times { | room_num | @rooms << room_num + 1} end - def reserve_room(guest, start_date, end_date) + def reserve_room(room_num, guest, start_date, end_date) + valid_dates(start_date, end_date) + id = @reservations.length + new_reservation = Reservation.new(id, room_num, guest, start_date, end_date) + @reservations << new_reservation + new_reservation + end + + def valid_dates(start_date, end_date) if start_date.class != Date || end_date.class != Date raise StandardError.new("not a date") elsif end_date < start_date @@ -19,5 +29,6 @@ def reserve_room(guest, start_date, end_date) raise StandardError.new("End date (#{end_date}) is same as start date (#{start_date})") end end + end end diff --git a/specs/reservation_spec.rb b/specs/reservation_spec.rb index f967bcdef..f91714468 100644 --- a/specs/reservation_spec.rb +++ b/specs/reservation_spec.rb @@ -3,7 +3,7 @@ describe 'Reservation' do before do - @reservation = Hotel::Reservation.new(1, 4, "Bob", Date.new(2018,3,26), Date.new(2018,3,18)) + @reservation = Hotel::Reservation.new(1, 4, "Bob", Date.new(2018,3,18), Date.new(2018,3,26)) end describe 'initialization' do it 'can be initialized' do @@ -22,7 +22,7 @@ end describe 'calculate_nights' do before do - @reservation = Hotel::Reservation.new(1, 4, "Bob", Date.new(2018,3,26), Date.new(2018,3,18)) + @reservation = Hotel::Reservation.new(1, 4, "Bob", Date.new(2018,3,18), Date.new(2018,3,26)) end it 'returns the number of nights' do nights = @reservation.calculate_nights @@ -31,4 +31,13 @@ nights.must_equal 8 end end + describe 'find_all_dates' do + it 'returns an array of all dates in reservation' do + reservation = Hotel::Reservation.new(1, 4, "Kaeli", Date.new(2018,3,20), Date.new(2018,3,22)) + all_dates = reservation.find_all_dates + + all_dates.must_be_kind_of Array + all_dates.must_equal [Date.new(2018,3,20), Date.new(2018,3,21), Date.new(2018,3,22)] + end + end end diff --git a/specs/user_spec.rb b/specs/user_spec.rb index 593c254ac..4a39261f1 100644 --- a/specs/user_spec.rb +++ b/specs/user_spec.rb @@ -16,18 +16,24 @@ describe 'reserve_room' do before do @admin = Hotel::User.new + @new_reservation = @admin.reserve_room(5, "Jade Poe", Date.new(2018, 3, 20), Date.new(2018, 3, 25)) + end it 'throws StandardError for invalidate dates' do - proc { @admin.reserve_room("Jade Poe", nil, Date.new(2018,3,5))}.must_raise StandardError - proc { @admin.reserve_room("Jade Poe", Date.new(2018,3,5), nil)}.must_raise StandardError + proc { @admin.reserve_room(3, "Jade Poe", nil, Date.new(2018,3,5))}.must_raise StandardError + proc { @admin.reserve_room(4, "Jade Poe", Date.new(2018,3,5), nil)}.must_raise StandardError end it 'throws ArgumentError if end_date occurs before start_date' do - proc { @admin.reserve_room("Jade Poe", Date.new(2018, 3, 10), Date.new(2018, 3, 5))}.must_raise StandardError + proc { @admin.reserve_room(1, "Jade Poe", Date.new(2018, 3, 10), Date.new(2018, 3, 5))}.must_raise StandardError end it 'throws ArgumentError if start_date is same as end_date' do - proc { @admin.reserve_room("Jade Poe", Date.new(2018, 3, 10), Date.new(2018, 3, 10))}.must_raise StandardError + proc { @admin.reserve_room(2, "Jade Poe", Date.new(2018, 3, 10), Date.new(2018, 3, 10))}.must_raise StandardError end it 'returns an instance of a reservation' do + @new_reservation.must_be_instance_of Hotel::Reservation + end + xit 'adds new reservation to array of reservations' do + end end xdescribe 'find_reservations_for_given_date' do @@ -36,6 +42,7 @@ end xdescribe 'calculate_reservation_cost' do it 'returns cost for reservation' do + end end From 1166bc7934511a8c5833fd45fe67543ac083ab36 Mon Sep 17 00:00:00 2001 From: Kaitlin Forsman Date: Mon, 5 Mar 2018 16:30:12 -0800 Subject: [PATCH 08/34] finished main method and helper method that allows user to look up all reservations for a given day --- lib/user.rb | 12 ++++++++++++ specs/user_spec.rb | 13 +++++++++++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/lib/user.rb b/lib/user.rb index 8b53e985e..dbf883e40 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -16,6 +16,7 @@ def reserve_room(room_num, guest, start_date, end_date) valid_dates(start_date, end_date) id = @reservations.length new_reservation = Reservation.new(id, room_num, guest, start_date, end_date) + add_to_calendar(new_reservation) @reservations << new_reservation new_reservation end @@ -30,5 +31,16 @@ def valid_dates(start_date, end_date) end end + def add_to_calendar(reservation) + date_range = reservation.find_all_dates + date_range.each do |date| + @calendar[date] ? @calendar[date].push(reservation) : @calendar[date] = [reservation] + end + end + + def find_reservations_for_given_date(date) + @calendar[date] + end + end end diff --git a/specs/user_spec.rb b/specs/user_spec.rb index 4a39261f1..fd07b5c64 100644 --- a/specs/user_spec.rb +++ b/specs/user_spec.rb @@ -17,7 +17,6 @@ before do @admin = Hotel::User.new @new_reservation = @admin.reserve_room(5, "Jade Poe", Date.new(2018, 3, 20), Date.new(2018, 3, 25)) - end it 'throws StandardError for invalidate dates' do proc { @admin.reserve_room(3, "Jade Poe", nil, Date.new(2018,3,5))}.must_raise StandardError @@ -36,8 +35,18 @@ end end - xdescribe 'find_reservations_for_given_date' do + describe 'find_reservations_for_given_date' do + before do + @admin = Hotel::User.new + @new_reservation = @admin.reserve_room(5, "Jade Poe", Date.new(2018, 3, 20), Date.new(2018, 3, 25)) + @new_reservation_2 = @admin.reserve_room(8, "Kal Smith", Date.new(2018, 3, 20), Date.new(2018, 3, 25)) + @date = Date.new(2018, 3, 23) + end it 'returns array of reservations for a given date' do + reservations_of_day = @admin.find_reservations_for_given_date(@date) + + reservations_of_day.must_be_kind_of Array + reservations_of_day.must_equal [@new_reservation, @new_reservation_2] end end xdescribe 'calculate_reservation_cost' do From 95561846c302f33971bd97152180949719fc7405 Mon Sep 17 00:00:00 2001 From: Kaitlin Forsman Date: Mon, 5 Mar 2018 16:50:32 -0800 Subject: [PATCH 09/34] finished approach to calculating reservation cost and how user accesses it --- lib/reservation.rb | 3 +++ lib/user.rb | 9 ++++++++- specs/reservation_spec.rb | 8 ++++++++ specs/user_spec.rb | 10 +++++----- 4 files changed, 24 insertions(+), 6 deletions(-) diff --git a/lib/reservation.rb b/lib/reservation.rb index 4a892a7b1..3c910a6ba 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -25,5 +25,8 @@ def find_all_dates date_range end + def calculate_reservation_cost + calculate_nights * COST_PER_NIGHT + end end end diff --git a/lib/user.rb b/lib/user.rb index dbf883e40..6d05779b7 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -14,7 +14,7 @@ def initialize end def reserve_room(room_num, guest, start_date, end_date) valid_dates(start_date, end_date) - id = @reservations.length + id = @reservations.length + 1 new_reservation = Reservation.new(id, room_num, guest, start_date, end_date) add_to_calendar(new_reservation) @reservations << new_reservation @@ -42,5 +42,12 @@ def find_reservations_for_given_date(date) @calendar[date] end + def find_reservation_cost(reservation_id) + reservation = find_reservation(reservation_id) + reservation.calculate_reservation_cost + end + def find_reservation(reservation_id) + found_reservation = @reservations.find { |reservation| reservation.id == reservation_id } + end end end diff --git a/specs/reservation_spec.rb b/specs/reservation_spec.rb index f91714468..b3915421f 100644 --- a/specs/reservation_spec.rb +++ b/specs/reservation_spec.rb @@ -40,4 +40,12 @@ all_dates.must_equal [Date.new(2018,3,20), Date.new(2018,3,21), Date.new(2018,3,22)] end end + + describe 'calculate_reservation_cost' do + it 'returns the cost of the reservation' do + reservation = Hotel::Reservation.new(1, 4, "Bob", Date.new(2018,3,20), Date.new(2018,3,25)) + + reservation.calculate_reservation_cost.must_equal 1000 + end + end end diff --git a/specs/user_spec.rb b/specs/user_spec.rb index fd07b5c64..d5e12eb59 100644 --- a/specs/user_spec.rb +++ b/specs/user_spec.rb @@ -31,9 +31,6 @@ it 'returns an instance of a reservation' do @new_reservation.must_be_instance_of Hotel::Reservation end - xit 'adds new reservation to array of reservations' do - - end end describe 'find_reservations_for_given_date' do before do @@ -49,9 +46,12 @@ reservations_of_day.must_equal [@new_reservation, @new_reservation_2] end end - xdescribe 'calculate_reservation_cost' do - it 'returns cost for reservation' do + describe 'find_reservation_cost' do + it 'returns cost for reservation with reservation id' do + admin = Hotel::User.new + new_reservation = admin.reserve_room(5, "Kaeli Poe", Date.new(2018, 3, 20), Date.new(2018, 3, 25)) + admin.find_reservation_cost(1).must_equal 1000 end end From 9d711e94be734e80ff793e58b463adb0a78e56b0 Mon Sep 17 00:00:00 2001 From: Kaitlin Forsman Date: Tue, 6 Mar 2018 13:38:00 -0800 Subject: [PATCH 10/34] created a room class that has a number and calendar that dates can be added to --- lib/room.rb | 17 +++++++++++++++++ specs/room_spec.rb | 32 ++++++++++++++++++++++++++++++++ specs/spec_helper.rb | 1 + 3 files changed, 50 insertions(+) create mode 100644 lib/room.rb create mode 100644 specs/room_spec.rb diff --git a/lib/room.rb b/lib/room.rb new file mode 100644 index 000000000..2da8209d8 --- /dev/null +++ b/lib/room.rb @@ -0,0 +1,17 @@ +require 'date' +require 'pry' + +module Hotel + class Room + attr_reader :room_num, :calendar + def initialize(room_num) + @room_num = room_num + @calendar = [] + end + def add_to_calendar(date_range) + dates = date_range.to_a + + dates.each { | date | @calendar << date } + end + end +end diff --git a/specs/room_spec.rb b/specs/room_spec.rb new file mode 100644 index 000000000..d94e98fbf --- /dev/null +++ b/specs/room_spec.rb @@ -0,0 +1,32 @@ +require_relative 'spec_helper' +require 'date' + +describe 'Room class' do + describe 'initialize' do + it 'can be initialized' do + room_2 = Hotel::Room.new(2) + end + + it 'has empty calendar and room number' do + room_2 = Hotel::Room.new(2) + + room_2.must_respond_to :room_num + room_2.room_num.must_be_kind_of Integer + room_2.room_num.must_equal 2 + room_2.must_respond_to :calendar + room_2.calendar.must_be_kind_of Array + room_2.calendar.must_equal [] + end + end + describe 'add_to_calendar' do + it 'can add a date_range to its calendar' do + room = Hotel::Room.new(2) + date_range = (Date.new(2018,3,8)...Date.new(2018,3,10)) + + room.add_to_calendar(date_range) + + room.calendar.must_include Date.new(2018,3,9) + room.calendar.wont_include Date.new(2018,3,10) + end + end +end diff --git a/specs/spec_helper.rb b/specs/spec_helper.rb index 90790c660..05d7334da 100644 --- a/specs/spec_helper.rb +++ b/specs/spec_helper.rb @@ -11,3 +11,4 @@ require_relative '../lib/user' require_relative '../lib/reservation' +require_relative '../lib/room' From d5ca5a90f458d6d59802df22a22f1e88a3cddd81 Mon Sep 17 00:00:00 2001 From: Kaitlin Forsman Date: Tue, 6 Mar 2018 13:42:29 -0800 Subject: [PATCH 11/34] cleaned up unused but assigned variables and included a forgotten test --- lib/user.rb | 2 +- specs/room_spec.rb | 2 ++ specs/user_spec.rb | 8 +++++--- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/user.rb b/lib/user.rb index 6d05779b7..eb6f70a52 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -47,7 +47,7 @@ def find_reservation_cost(reservation_id) reservation.calculate_reservation_cost end def find_reservation(reservation_id) - found_reservation = @reservations.find { |reservation| reservation.id == reservation_id } + @reservations.find { |reservation| reservation.id == reservation_id } end end end diff --git a/specs/room_spec.rb b/specs/room_spec.rb index d94e98fbf..410c41129 100644 --- a/specs/room_spec.rb +++ b/specs/room_spec.rb @@ -5,6 +5,8 @@ describe 'initialize' do it 'can be initialized' do room_2 = Hotel::Room.new(2) + + room_2.must_be_instance_of Hotel::Room end it 'has empty calendar and room number' do diff --git a/specs/user_spec.rb b/specs/user_spec.rb index d5e12eb59..1e1373f5e 100644 --- a/specs/user_spec.rb +++ b/specs/user_spec.rb @@ -22,12 +22,14 @@ proc { @admin.reserve_room(3, "Jade Poe", nil, Date.new(2018,3,5))}.must_raise StandardError proc { @admin.reserve_room(4, "Jade Poe", Date.new(2018,3,5), nil)}.must_raise StandardError end - it 'throws ArgumentError if end_date occurs before start_date' do + it 'throws StandardError if end_date occurs before start_date' do proc { @admin.reserve_room(1, "Jade Poe", Date.new(2018, 3, 10), Date.new(2018, 3, 5))}.must_raise StandardError end - it 'throws ArgumentError if start_date is same as end_date' do + it 'throws StandardError if start_date is same as end_date' do proc { @admin.reserve_room(2, "Jade Poe", Date.new(2018, 3, 10), Date.new(2018, 3, 10))}.must_raise StandardError end + it 'throws StandardError if room has any other reservation that overlaps with new reservation' do + end it 'returns an instance of a reservation' do @new_reservation.must_be_instance_of Hotel::Reservation end @@ -49,7 +51,7 @@ describe 'find_reservation_cost' do it 'returns cost for reservation with reservation id' do admin = Hotel::User.new - new_reservation = admin.reserve_room(5, "Kaeli Poe", Date.new(2018, 3, 20), Date.new(2018, 3, 25)) + admin.reserve_room(5, "Kaeli Poe", Date.new(2018, 3, 20), Date.new(2018, 3, 25)) admin.find_reservation_cost(1).must_equal 1000 end From fa322f7aad605971d5376e39227b969263f08198 Mon Sep 17 00:00:00 2001 From: Kaitlin Forsman Date: Tue, 6 Mar 2018 14:30:37 -0800 Subject: [PATCH 12/34] refactored all room and reservation to take Range of dates instead of start_date and end_date, and raises an exception for trying to reserve a room that has preexisting reservation in date range --- lib/reservation.rb | 17 +++++------------ lib/user.rb | 16 ++++++++++++++-- specs/reservation_spec.rb | 19 +++++++++++-------- specs/user_spec.rb | 4 ++++ 4 files changed, 34 insertions(+), 22 deletions(-) diff --git a/lib/reservation.rb b/lib/reservation.rb index 3c910a6ba..ee5268307 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -2,27 +2,20 @@ module Hotel class Reservation - attr_reader :id, :room_num, :guest, :start_date, :end_date - def initialize id, room_num, guest, start_date, end_date + attr_reader :id, :room_num, :guest, :date_range + def initialize id, room_num, guest, date_range @id = id @room_num = room_num @guest = guest - @start_date = start_date - @end_date = end_date + @date_range = date_range end def calculate_nights - (end_date - start_date).to_i + @date_range.to_a.length end def find_all_dates - date_range = [] - date = @start_date - while date <= @end_date - date_range << date - date += 1 - end - date_range + @date_range.to_a end def calculate_reservation_cost diff --git a/lib/user.rb b/lib/user.rb index eb6f70a52..c826af2b8 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -10,13 +10,17 @@ def initialize @rooms = [] @reservations = [] @calendar = {} - TOTAL_ROOMS.times { | room_num | @rooms << room_num + 1} + TOTAL_ROOMS.times { | room_num | @rooms << Room.new(room_num + 1)} end def reserve_room(room_num, guest, start_date, end_date) valid_dates(start_date, end_date) + date_range = (start_date...end_date) + check_room_availibility(room_num, date_range) + room = @rooms[room_num - 1] id = @reservations.length + 1 - new_reservation = Reservation.new(id, room_num, guest, start_date, end_date) + new_reservation = Reservation.new(id, room, guest, date_range) add_to_calendar(new_reservation) + room.add_to_calendar(date_range) @reservations << new_reservation new_reservation end @@ -31,6 +35,14 @@ def valid_dates(start_date, end_date) end end + def check_room_availibility(room_num, date_range) + calendar = @rooms[room_num - 1].calendar + return true if calendar.empty? + calendar.each do |date| + raise StandardError.new("room already reserved") if date_range.include?(date) + end + end + def add_to_calendar(reservation) date_range = reservation.find_all_dates date_range.each do |date| diff --git a/specs/reservation_spec.rb b/specs/reservation_spec.rb index b3915421f..2ae19efd6 100644 --- a/specs/reservation_spec.rb +++ b/specs/reservation_spec.rb @@ -3,26 +3,27 @@ describe 'Reservation' do before do - @reservation = Hotel::Reservation.new(1, 4, "Bob", Date.new(2018,3,18), Date.new(2018,3,26)) + date_range = (Date.new(2018,3,18)...Date.new(2018,3,26)) + @reservation = Hotel::Reservation.new(1, 4, "Bob", date_range) end describe 'initialization' do it 'can be initialized' do @reservation.must_be_instance_of Hotel::Reservation end - it 'has attributes: id, room_num, guest, start_date, end_date' do + it 'has attributes: id, room_num, guest, date_range' do @reservation.id.must_equal 1 @reservation.id.must_be_kind_of Integer @reservation.room_num.must_equal 4 @reservation.room_num.must_be_kind_of Integer @reservation.guest.must_equal "Bob" @reservation.guest.must_be_kind_of String - @reservation.start_date.must_be_instance_of Date - @reservation.end_date.must_be_instance_of Date + @reservation.date_range.must_be_kind_of Range end end describe 'calculate_nights' do before do - @reservation = Hotel::Reservation.new(1, 4, "Bob", Date.new(2018,3,18), Date.new(2018,3,26)) + date_range = (Date.new(2018,3,18)...Date.new(2018,3,26)) + @reservation = Hotel::Reservation.new(1, 4, "Bob", date_range) end it 'returns the number of nights' do nights = @reservation.calculate_nights @@ -33,17 +34,19 @@ end describe 'find_all_dates' do it 'returns an array of all dates in reservation' do - reservation = Hotel::Reservation.new(1, 4, "Kaeli", Date.new(2018,3,20), Date.new(2018,3,22)) + date_range = (Date.new(2018,3,20)...Date.new(2018,3,22)) + reservation = Hotel::Reservation.new(1, 4, "Kaeli", date_range) all_dates = reservation.find_all_dates all_dates.must_be_kind_of Array - all_dates.must_equal [Date.new(2018,3,20), Date.new(2018,3,21), Date.new(2018,3,22)] + all_dates.must_equal [Date.new(2018,3,20), Date.new(2018,3,21)] end end describe 'calculate_reservation_cost' do it 'returns the cost of the reservation' do - reservation = Hotel::Reservation.new(1, 4, "Bob", Date.new(2018,3,20), Date.new(2018,3,25)) + date_range = (Date.new(2018,3,20)...Date.new(2018,3,25)) + reservation = Hotel::Reservation.new(1, 4, "Bob", date_range) reservation.calculate_reservation_cost.must_equal 1000 end diff --git a/specs/user_spec.rb b/specs/user_spec.rb index 1e1373f5e..4ef6b3f90 100644 --- a/specs/user_spec.rb +++ b/specs/user_spec.rb @@ -11,6 +11,8 @@ end it 'has a list of rooms' do @admin.must_respond_to :rooms + @admin.rooms[0].must_be_instance_of Hotel::Room + @admin.rooms.last.must_be_instance_of Hotel::Room end end describe 'reserve_room' do @@ -29,6 +31,8 @@ proc { @admin.reserve_room(2, "Jade Poe", Date.new(2018, 3, 10), Date.new(2018, 3, 10))}.must_raise StandardError end it 'throws StandardError if room has any other reservation that overlaps with new reservation' do + @admin.reserve_room(2, "Jace Poe", Date.new(2018, 3, 10), Date.new(2018, 3, 12)) + proc { @admin.reserve_room(2, "Jade Poe", Date.new(2018, 3, 10), Date.new(2018, 3, 12))}.must_raise StandardError end it 'returns an instance of a reservation' do @new_reservation.must_be_instance_of Hotel::Reservation From 8cc9a312393db4d4a6a92f94786204c7b7810d78 Mon Sep 17 00:00:00 2001 From: Kaitlin Forsman Date: Tue, 6 Mar 2018 15:16:43 -0800 Subject: [PATCH 13/34] reworked, restructured, wrote tests and codes for method to present a list of available rooms for a given date_range --- lib/reservation.rb | 6 ++--- lib/room.rb | 1 - lib/user.rb | 10 ++++++++ specs/reservation_spec.rb | 17 ++++++++------ specs/user_spec.rb | 48 ++++++++++++++++++++++++++++++++------- 5 files changed, 63 insertions(+), 19 deletions(-) diff --git a/lib/reservation.rb b/lib/reservation.rb index ee5268307..457509296 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -2,10 +2,10 @@ module Hotel class Reservation - attr_reader :id, :room_num, :guest, :date_range - def initialize id, room_num, guest, date_range + attr_reader :id, :room, :guest, :date_range + def initialize id, room, guest, date_range @id = id - @room_num = room_num + @room = room @guest = guest @date_range = date_range end diff --git a/lib/room.rb b/lib/room.rb index 2da8209d8..9aab4f682 100644 --- a/lib/room.rb +++ b/lib/room.rb @@ -10,7 +10,6 @@ def initialize(room_num) end def add_to_calendar(date_range) dates = date_range.to_a - dates.each { | date | @calendar << date } end end diff --git a/lib/user.rb b/lib/user.rb index c826af2b8..9b97a4dee 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -12,6 +12,16 @@ def initialize @calendar = {} TOTAL_ROOMS.times { | room_num | @rooms << Room.new(room_num + 1)} end + def find_available_rooms(start_date, end_date) + date_range = (start_date...end_date) + available_rooms = [] + @rooms.each do |room| + next if room.calendar.any?(date_range) + available_rooms << room + end + available_rooms + end + def reserve_room(room_num, guest, start_date, end_date) valid_dates(start_date, end_date) date_range = (start_date...end_date) diff --git a/specs/reservation_spec.rb b/specs/reservation_spec.rb index 2ae19efd6..a12a0d124 100644 --- a/specs/reservation_spec.rb +++ b/specs/reservation_spec.rb @@ -4,17 +4,17 @@ describe 'Reservation' do before do date_range = (Date.new(2018,3,18)...Date.new(2018,3,26)) - @reservation = Hotel::Reservation.new(1, 4, "Bob", date_range) + room = Hotel::Room.new(4) + @reservation = Hotel::Reservation.new(1, room, "Bob", date_range) end describe 'initialization' do it 'can be initialized' do @reservation.must_be_instance_of Hotel::Reservation end - it 'has attributes: id, room_num, guest, date_range' do + it 'has attributes: id, room, guest, date_range' do @reservation.id.must_equal 1 @reservation.id.must_be_kind_of Integer - @reservation.room_num.must_equal 4 - @reservation.room_num.must_be_kind_of Integer + @reservation.room.must_be_instance_of Hotel::Room @reservation.guest.must_equal "Bob" @reservation.guest.must_be_kind_of String @reservation.date_range.must_be_kind_of Range @@ -23,7 +23,8 @@ describe 'calculate_nights' do before do date_range = (Date.new(2018,3,18)...Date.new(2018,3,26)) - @reservation = Hotel::Reservation.new(1, 4, "Bob", date_range) + room = Hotel::Room.new(4) + @reservation = Hotel::Reservation.new(1, room, "Bob", date_range) end it 'returns the number of nights' do nights = @reservation.calculate_nights @@ -35,7 +36,8 @@ describe 'find_all_dates' do it 'returns an array of all dates in reservation' do date_range = (Date.new(2018,3,20)...Date.new(2018,3,22)) - reservation = Hotel::Reservation.new(1, 4, "Kaeli", date_range) + room = Hotel::Room.new(4) + reservation = Hotel::Reservation.new(1, room, "Kaeli", date_range) all_dates = reservation.find_all_dates all_dates.must_be_kind_of Array @@ -46,7 +48,8 @@ describe 'calculate_reservation_cost' do it 'returns the cost of the reservation' do date_range = (Date.new(2018,3,20)...Date.new(2018,3,25)) - reservation = Hotel::Reservation.new(1, 4, "Bob", date_range) + room = Hotel::Room.new(4) + reservation = Hotel::Reservation.new(1, room, "Bob", date_range) reservation.calculate_reservation_cost.must_equal 1000 end diff --git a/specs/user_spec.rb b/specs/user_spec.rb index 4ef6b3f90..50ef098f1 100644 --- a/specs/user_spec.rb +++ b/specs/user_spec.rb @@ -15,6 +15,38 @@ @admin.rooms.last.must_be_instance_of Hotel::Room end end + describe 'find_available_rooms' do + before do + @admin = Hotel::User.new + @start_date = Date.new(2018,3,15) + @end_date = Date.new(2018,3,20) + end + it 'returns a list of rooms that are available for a range of dates' do + available_rooms = @admin.find_available_rooms(@start_date, @end_date) + + available_rooms.must_be_kind_of Array + available_rooms.length.must_equal 20 + available_rooms[0].must_be_instance_of Hotel::Room + available_rooms[8].must_be_instance_of Hotel::Room + available_rooms[-1].must_be_instance_of Hotel::Room + end + it 'excludes rooms that arent available for given dates' do + reservation1 = @admin.reserve_room(1, "Jane Doe", Date.new(2018,3,12), Date.new(2018,3,18)) + reservation2 = @admin.reserve_room(2, "John Smith", Date.new(2018,3,18), Date.new(2018,3,25)) + reservation3 = @admin.reserve_room(3, "Sam Sole", Date.new(2018,3,1), Date.new(2018,3,30)) + reservation4 = @admin.reserve_room(4, "Alex Whitt", Date.new(2018,3,12), Date.new(2018,3,16)) + + available_rooms = @admin.find_available_rooms(@start_date, @end_date) + + available_rooms.wont_include reservation1.room + available_rooms.wont_include reservation2.room + available_rooms.wont_include reservation3.room + available_rooms.wont_include reservation4.room + end + it 'includes rooms that have same end_date as new start date' do + + end + end describe 'reserve_room' do before do @admin = Hotel::User.new @@ -38,6 +70,14 @@ @new_reservation.must_be_instance_of Hotel::Reservation end end + describe 'find_reservation_cost' do + it 'returns cost for reservation with reservation id' do + admin = Hotel::User.new + admin.reserve_room(5, "Kaeli Poe", Date.new(2018, 3, 20), Date.new(2018, 3, 25)) + + admin.find_reservation_cost(1).must_equal 1000 + end + end describe 'find_reservations_for_given_date' do before do @admin = Hotel::User.new @@ -52,13 +92,5 @@ reservations_of_day.must_equal [@new_reservation, @new_reservation_2] end end - describe 'find_reservation_cost' do - it 'returns cost for reservation with reservation id' do - admin = Hotel::User.new - admin.reserve_room(5, "Kaeli Poe", Date.new(2018, 3, 20), Date.new(2018, 3, 25)) - - admin.find_reservation_cost(1).must_equal 1000 - end - end end From 1ec19e762ccb3f424d4e3b937a1b70c546cfc74d Mon Sep 17 00:00:00 2001 From: Kaitlin Forsman Date: Tue, 6 Mar 2018 16:03:36 -0800 Subject: [PATCH 14/34] added couple more tests to user_spec to check availability at the end_date and it allows user to make new reservation on a room starting same day as another reservations end_date --- specs/user_spec.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/specs/user_spec.rb b/specs/user_spec.rb index 50ef098f1..7b3f68753 100644 --- a/specs/user_spec.rb +++ b/specs/user_spec.rb @@ -44,7 +44,13 @@ available_rooms.wont_include reservation4.room end it 'includes rooms that have same end_date as new start date' do + reservation1 = @admin.reserve_room(3, "Sam Sole", Date.new(2018,3,1), Date.new(2018,3,30)) + reservation2 = @admin.reserve_room(4, "Alex Whitt", Date.new(2018,3,12), Date.new(2018,3,15)) + available_rooms = @admin.find_available_rooms(@start_date, @end_date) + + available_rooms.wont_include reservation1.room + available_rooms.must_include reservation2.room end end describe 'reserve_room' do @@ -69,6 +75,12 @@ it 'returns an instance of a reservation' do @new_reservation.must_be_instance_of Hotel::Reservation end + it 'allows reservation of room with same start_date as previous reservations end_date' do + next_reservation = @admin.reserve_room(5, "Kaeli Smit", Date.new(2018, 3, 25), Date.new(2018, 3, 27)) + + next_reservation.must_be_instance_of Hotel::Reservation + next_reservation.date_range.must_equal (Date.new(2018, 3, 25)...Date.new(2018, 3, 27)) + end end describe 'find_reservation_cost' do it 'returns cost for reservation with reservation id' do From 2d777b51b09086f7bc42fafef3eb85549f51f1ad Mon Sep 17 00:00:00 2001 From: Kaitlin Forsman Date: Wed, 7 Mar 2018 15:27:31 -0800 Subject: [PATCH 15/34] double-checked functionality of find_available room: it works and does what it's supposed....why? --- lib/user.rb | 4 +++- specs/user_spec.rb | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/user.rb b/lib/user.rb index 9b97a4dee..19ccb78b1 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -16,7 +16,9 @@ def find_available_rooms(start_date, end_date) date_range = (start_date...end_date) available_rooms = [] @rooms.each do |room| - next if room.calendar.any?(date_range) + next if room.calendar.any?(date_range) # this apparently works + # next if room.calendar.any?{ |date| date_range.include?(date) } + # ^ alternative solution that actually make more sense to me ^ available_rooms << room end available_rooms diff --git a/specs/user_spec.rb b/specs/user_spec.rb index 7b3f68753..f4478a89d 100644 --- a/specs/user_spec.rb +++ b/specs/user_spec.rb @@ -34,7 +34,8 @@ reservation1 = @admin.reserve_room(1, "Jane Doe", Date.new(2018,3,12), Date.new(2018,3,18)) reservation2 = @admin.reserve_room(2, "John Smith", Date.new(2018,3,18), Date.new(2018,3,25)) reservation3 = @admin.reserve_room(3, "Sam Sole", Date.new(2018,3,1), Date.new(2018,3,30)) - reservation4 = @admin.reserve_room(4, "Alex Whitt", Date.new(2018,3,12), Date.new(2018,3,16)) + reservation4 = @admin.reserve_room(4, "Alex Whitt", Date.new(2018,3,15), Date.new(2018,3,20)) + reservation5 = @admin.reserve_room(5, "Kael Dear", Date.new(2018,3,16), Date.new(2018,3,18)) available_rooms = @admin.find_available_rooms(@start_date, @end_date) @@ -42,6 +43,7 @@ available_rooms.wont_include reservation2.room available_rooms.wont_include reservation3.room available_rooms.wont_include reservation4.room + available_rooms.wont_include reservation5.room end it 'includes rooms that have same end_date as new start date' do reservation1 = @admin.reserve_room(3, "Sam Sole", Date.new(2018,3,1), Date.new(2018,3,30)) From 228946568f124ddc6104e540eee00bf5616e7ec6 Mon Sep 17 00:00:00 2001 From: Kaitlin Forsman Date: Wed, 7 Mar 2018 15:31:11 -0800 Subject: [PATCH 16/34] correct version of helper spec and set up of new class files for a Block --- lib/block.rb | 7 +++++++ specs/block_spec.rb | 5 +++++ specs/spec_helper.rb | 1 + 3 files changed, 13 insertions(+) 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..9db74787f --- /dev/null +++ b/lib/block.rb @@ -0,0 +1,7 @@ +require 'date' +require 'pry' + +module Hotel + class Block < User + end +end diff --git a/specs/block_spec.rb b/specs/block_spec.rb new file mode 100644 index 000000000..d53ffd2f5 --- /dev/null +++ b/specs/block_spec.rb @@ -0,0 +1,5 @@ +require_relative 'spec_helper' +require 'date' + +describe 'Block class' do +end diff --git a/specs/spec_helper.rb b/specs/spec_helper.rb index 05d7334da..b883b0291 100644 --- a/specs/spec_helper.rb +++ b/specs/spec_helper.rb @@ -12,3 +12,4 @@ require_relative '../lib/user' require_relative '../lib/reservation' require_relative '../lib/room' +require_relative '../lib/block' From 977b8dd97b3385995721d9335bf30d60e0e3433c Mon Sep 17 00:00:00 2001 From: Kaitlin Forsman Date: Wed, 7 Mar 2018 16:47:13 -0800 Subject: [PATCH 17/34] initialized Block spec and added notes to user for tests and content that still needs to be dealt with --- lib/block.rb | 9 +++++++++ lib/user.rb | 1 + specs/block_spec.rb | 43 +++++++++++++++++++++++++++++++++++++++++++ specs/user_spec.rb | 2 ++ 4 files changed, 55 insertions(+) diff --git a/lib/block.rb b/lib/block.rb index 9db74787f..2944bb2c0 100644 --- a/lib/block.rb +++ b/lib/block.rb @@ -3,5 +3,14 @@ module Hotel class Block < User + attr_reader :id + def initialize(id, rooms, party, date_range, discount) + @id = id + @rooms = rooms + @party = party + @date_range = date_range + @discount = discount + @reservations = [] + end end end diff --git a/lib/user.rb b/lib/user.rb index 19ccb78b1..fc405690a 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -22,6 +22,7 @@ def find_available_rooms(start_date, end_date) available_rooms << room end available_rooms + # need to deal with no rooms available end def reserve_room(room_num, guest, start_date, end_date) diff --git a/specs/block_spec.rb b/specs/block_spec.rb index d53ffd2f5..098c57d96 100644 --- a/specs/block_spec.rb +++ b/specs/block_spec.rb @@ -2,4 +2,47 @@ require 'date' describe 'Block class' do + describe 'instantiation' do + before do + rooms = [] + 4.times {|x| rooms << Hotel::Room.new(x+1) } + date_range = (Date.new(2018,3,22)...Date.new(2018,3,26)) + @block = Hotel::Block.new(1, rooms, "Fanime", date_range, 0.2) + end + it 'can be initialized' do + @block.must_be_instance_of Hotel::Block + end + it 'inherits from User' do + @block.must_be_kind_of Hotel::User + end + it 'has attributes: id, list of some rooms, reservations, party, date range, discount' do + # may not need this test stub.... + @block.must_respond_to :id + @block.id.must_equal 1 + end + end + describe 'check_room_availibility' do + it 'returns array of rooms available in block' do + + end + it 'handles (throw exception or returns nil) no rooms available' do + + end + it 'exclude rooms that are already reserved' do + + end + end + describe 'reserve_room' do + it 'returns a reservation for an available room' do + + end + it 'throws exception if room is unavailable' do + + end + end + describe 'find_reservation_cost' do + it 'returns discounted cost for the reservation' do + + end + end end diff --git a/specs/user_spec.rb b/specs/user_spec.rb index f4478a89d..49e10f8ea 100644 --- a/specs/user_spec.rb +++ b/specs/user_spec.rb @@ -30,6 +30,8 @@ available_rooms[8].must_be_instance_of Hotel::Room available_rooms[-1].must_be_instance_of Hotel::Room end + it 'handles (throw exception or returns nil) if no rooms available' do + end it 'excludes rooms that arent available for given dates' do reservation1 = @admin.reserve_room(1, "Jane Doe", Date.new(2018,3,12), Date.new(2018,3,18)) reservation2 = @admin.reserve_room(2, "John Smith", Date.new(2018,3,18), Date.new(2018,3,25)) From 4035cf4c29483de7f7d89a6793608e9351f3bbec Mon Sep 17 00:00:00 2001 From: Kaitlin Forsman Date: Wed, 7 Mar 2018 17:37:39 -0800 Subject: [PATCH 18/34] added initial method for block rooms availibility --- lib/block.rb | 12 ++++++++++++ specs/block_spec.rb | 18 ++++++++++++++---- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/lib/block.rb b/lib/block.rb index 2944bb2c0..2efb0a2ac 100644 --- a/lib/block.rb +++ b/lib/block.rb @@ -12,5 +12,17 @@ def initialize(id, rooms, party, date_range, discount) @discount = discount @reservations = [] end + + def find_available_rooms + available_rooms = [] + @rooms.each do |room| + next if room.calendar.any?(@date_range) # this apparently works + # next if room.calendar.any?{ |date| @date_range.include?(date) } + # ^ alternative solution that actually make more sense to me ^ + available_rooms << room + end + available_rooms + # need to deal with no rooms available + end end end diff --git a/specs/block_spec.rb b/specs/block_spec.rb index 098c57d96..7e6138a08 100644 --- a/specs/block_spec.rb +++ b/specs/block_spec.rb @@ -21,14 +21,24 @@ @block.id.must_equal 1 end end - describe 'check_room_availibility' do + describe 'find_available_rooms' do + before do + @rooms = [] + 4.times {|x| @rooms << Hotel::Room.new(x+1) } + date_range = (Date.new(2018,3,22)...Date.new(2018,3,26)) + @block = Hotel::Block.new(1, @rooms, "Fanime", date_range, 0.2) + end it 'returns array of rooms available in block' do + available_rooms_in_block = @block.find_available_rooms + available_rooms_in_block.must_be_kind_of Array + available_rooms_in_block.must_equal @rooms end - it 'handles (throw exception or returns nil) no rooms available' do - + xit 'handles (throw exception or returns nil) no rooms available' do + 4.times { |x| @block.reserve_room(x+1, "Guest #{x+1}") } + proc { available_rooms_in_block = @block.find_available_rooms }.must_raise StandardError end - it 'exclude rooms that are already reserved' do + xit 'exclude rooms that are already reserved' do end end From c99adbc3fbc3353829cf84fb0693b9fb14e3df6b Mon Sep 17 00:00:00 2001 From: Kaitlin Forsman Date: Wed, 7 Mar 2018 22:19:46 -0800 Subject: [PATCH 19/34] finished block versions of find_available_rooms and reserve_room with tests (so much refactoring to consider...) --- lib/block.rb | 18 +++++++++++++----- specs/block_spec.rb | 29 ++++++++++++++++++++++++----- 2 files changed, 37 insertions(+), 10 deletions(-) diff --git a/lib/block.rb b/lib/block.rb index 2efb0a2ac..3c0f7c85a 100644 --- a/lib/block.rb +++ b/lib/block.rb @@ -3,7 +3,7 @@ module Hotel class Block < User - attr_reader :id + attr_reader :id, :reservations def initialize(id, rooms, party, date_range, discount) @id = id @rooms = rooms @@ -15,14 +15,22 @@ def initialize(id, rooms, party, date_range, discount) def find_available_rooms available_rooms = [] + return @rooms if @reservations.empty? @rooms.each do |room| - next if room.calendar.any?(@date_range) # this apparently works - # next if room.calendar.any?{ |date| @date_range.include?(date) } - # ^ alternative solution that actually make more sense to me ^ + next if @reservations.any? { |reservation| reservation.room == room } available_rooms << room end + raise StandardError.new("no rooms available") if available_rooms.empty? available_rooms - # need to deal with no rooms available + end + + def reserve_room(room_num, guest) + room = @rooms[room_num - 1] + raise StandardError.new("room not available") if !find_available_rooms.include?(room) + id = @reservations.length + 1 + new_reservation = Reservation.new(id, room, guest, @date_range) + @reservations << new_reservation + new_reservation end end end diff --git a/specs/block_spec.rb b/specs/block_spec.rb index 7e6138a08..602ed0986 100644 --- a/specs/block_spec.rb +++ b/specs/block_spec.rb @@ -1,5 +1,6 @@ require_relative 'spec_helper' require 'date' +require 'pry' describe 'Block class' do describe 'instantiation' do @@ -34,20 +35,38 @@ available_rooms_in_block.must_be_kind_of Array available_rooms_in_block.must_equal @rooms end - xit 'handles (throw exception or returns nil) no rooms available' do - 4.times { |x| @block.reserve_room(x+1, "Guest #{x+1}") } - proc { available_rooms_in_block = @block.find_available_rooms }.must_raise StandardError + it 'handles (throw exception or returns nil) no rooms available' do + 4.times do |x| + @block.reserve_room(x+1, "fan #{x+1}") + end + + proc { @block.find_available_rooms }.must_raise StandardError end - xit 'exclude rooms that are already reserved' do + it 'exclude rooms that are already reserved' do + @block.reserve_room(1, "Fan 543") + @block.reserve_room(3, "Fan 564") + + available_rooms_in_block = @block.find_available_rooms + available_rooms_in_block.wont_include @rooms[0] + available_rooms_in_block.wont_include @rooms[2] end end describe 'reserve_room' do + before do + rooms = [] + 4.times {|x| rooms << Hotel::Room.new(x+1) } + date_range = (Date.new(2018,3,22)...Date.new(2018,3,26)) + @block = Hotel::Block.new(1, rooms, "Fanime", date_range, 0.2) + end it 'returns a reservation for an available room' do + reservation = @block.reserve_room(1, "Fan 1") + reservation.must_be_instance_of Hotel::Reservation end it 'throws exception if room is unavailable' do - + @block.reserve_room(2, "Jace Poe") + proc { @block.reserve_room(2, "Jade Poe")}.must_raise StandardError end end describe 'find_reservation_cost' do From 686504872496c0b21710b9f25ec42226c3cbc159 Mon Sep 17 00:00:00 2001 From: Kaitlin Forsman Date: Wed, 7 Mar 2018 22:28:59 -0800 Subject: [PATCH 20/34] finished discounted calculate_reservation_cost method to finish first version of the Block Class --- lib/block.rb | 4 ++++ specs/block_spec.rb | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/lib/block.rb b/lib/block.rb index 3c0f7c85a..79b9579f8 100644 --- a/lib/block.rb +++ b/lib/block.rb @@ -32,5 +32,9 @@ def reserve_room(room_num, guest) @reservations << new_reservation new_reservation end + + def find_reservation_cost(reservation_id) + ((1 - @discount) * super(reservation_id)).round(2) + end end end diff --git a/specs/block_spec.rb b/specs/block_spec.rb index 602ed0986..8b523ad63 100644 --- a/specs/block_spec.rb +++ b/specs/block_spec.rb @@ -70,8 +70,16 @@ end end describe 'find_reservation_cost' do + before do + rooms = [] + 4.times {|x| rooms << Hotel::Room.new(x+1) } + date_range = (Date.new(2018,3,22)...Date.new(2018,3,26)) + @block = Hotel::Block.new(1, rooms, "Fanime", date_range, 0.2) + end it 'returns discounted cost for the reservation' do + @block.reserve_room(1, "Meka Starbright") + @block.find_reservation_cost(1).must_equal 640 end end end From f84fde863ae1d962efc40abaeb990cf76cf80c7c Mon Sep 17 00:00:00 2001 From: Kaitlin Forsman Date: Wed, 7 Mar 2018 23:01:04 -0800 Subject: [PATCH 21/34] stubbed out remaining tests I could think of for Block with the User class --- specs/user_spec.rb | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/specs/user_spec.rb b/specs/user_spec.rb index 49e10f8ea..e94158868 100644 --- a/specs/user_spec.rb +++ b/specs/user_spec.rb @@ -108,5 +108,33 @@ reservations_of_day.must_equal [@new_reservation, @new_reservation_2] end end - + describe 'create_room_block' do + before do + end + it 'creates a block that is stored in reservations' do + end + it 'doesnt allow more than 5 rooms per block' do + end + it 'only includes rooms that are available when block is made' do + end + it 'deals with if there are not enough rooms for the block' do + # edge case to think about + end + it 'doesnt allow general public to reserve a room from the block' do + end + it 'doesnt allow rooms to be used for another block' do + end + end + describe 'reserve_room_from_block' do + it 'allows reservation of a room within a block' do + end + it 'doesnt allow reservation within block to have any other date range than blocks' do + end + end + describe 'check_block_room_availibility' do + it 'returns an array of rooms' do + available_rooms_in_block.must_be_kind_of Array + end + # rest of this method is tested in the block_spec + end end From 6b2607a490e63b408839b5b2b841b650024b670a Mon Sep 17 00:00:00 2001 From: Kaitlin Forsman Date: Wed, 7 Mar 2018 23:01:04 -0800 Subject: [PATCH 22/34] stubbed out remaining tests I could think of for Block within the User class --- specs/user_spec.rb | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/specs/user_spec.rb b/specs/user_spec.rb index 49e10f8ea..c1ffbfa2d 100644 --- a/specs/user_spec.rb +++ b/specs/user_spec.rb @@ -108,5 +108,33 @@ reservations_of_day.must_equal [@new_reservation, @new_reservation_2] end end - + describe 'create_room_block' do + before do + end + it 'creates a block that is stored in reservations' do + end + it 'doesnt allow more than 5 rooms per block' do + end + it 'only includes rooms that are available when block is made' do + end + it 'deals with if there are not enough rooms for the block' do + # edge case to think about + end + it 'doesnt allow general public to reserve a room from the block' do + end + it 'doesnt allow rooms to be used for another block' do + end + end + describe 'reserve_room_from_block' do + it 'allows reservation of a room within a block' do + end + it 'doesnt allow reservation within block to have any other date range than blocks' do + end + end + describe 'check_block_room_availibility' do + it 'returns an array of rooms' do + # available_rooms_in_block.must_be_kind_of Array + end + # rest of this method is tested in the block_spec + end end From 89358fb92f442d3d732ab7f50a60e66b9f07ee77 Mon Sep 17 00:00:00 2001 From: Kaitlin Forsman Date: Thu, 8 Mar 2018 10:22:20 -0800 Subject: [PATCH 23/34] finished functionality for creating a block within User and adjusting user to include blocks within availibility --- lib/user.rb | 19 ++++++++++++++++--- specs/user_spec.rb | 37 +++++++++++++++++++++++++++++++++---- 2 files changed, 49 insertions(+), 7 deletions(-) diff --git a/lib/user.rb b/lib/user.rb index fc405690a..675b24d37 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -32,7 +32,7 @@ def reserve_room(room_num, guest, start_date, end_date) room = @rooms[room_num - 1] id = @reservations.length + 1 new_reservation = Reservation.new(id, room, guest, date_range) - add_to_calendar(new_reservation) + add_to_calendar(new_reservation, date_range) room.add_to_calendar(date_range) @reservations << new_reservation new_reservation @@ -56,8 +56,7 @@ def check_room_availibility(room_num, date_range) end end - def add_to_calendar(reservation) - date_range = reservation.find_all_dates + def add_to_calendar(reservation, date_range) date_range.each do |date| @calendar[date] ? @calendar[date].push(reservation) : @calendar[date] = [reservation] end @@ -74,5 +73,19 @@ def find_reservation_cost(reservation_id) def find_reservation(reservation_id) @reservations.find { |reservation| reservation.id == reservation_id } end + + def create_room_block(rooms, party, start_date, end_date, discount) + raise StandardError.new('too many rooms for a block') if rooms.length > 5 + raise StandardError.new('too few rooms for a block') if rooms.length < 2 + valid_dates(start_date, end_date) + date_range = (start_date...end_date) + rooms.each { |room| check_room_availibility(room.room_num, date_range) } + id = @reservations.length + 1 + new_block = Block.new(id, rooms, party, date_range, discount) + add_to_calendar(new_block, date_range) + rooms.each { |room| room.add_to_calendar(date_range) } + @reservations << new_block + new_block + end end end diff --git a/specs/user_spec.rb b/specs/user_spec.rb index c1ffbfa2d..2b26f2eb8 100644 --- a/specs/user_spec.rb +++ b/specs/user_spec.rb @@ -110,19 +110,46 @@ end describe 'create_room_block' do before do + @admin = Hotel::User.new + @start_date = Date.new(2018,3,15) + @end_date = Date.new(2018,3,20) + @available_rooms = @admin.find_available_rooms(@start_date, @end_date) end it 'creates a block that is stored in reservations' do + rooms = @available_rooms.first(5) + block = @admin.create_room_block(rooms, "Comicon", @start_date, @end_date, 0.2) + + block.must_be_instance_of Hotel::Block + @admin.find_reservations_for_given_date(@start_date).must_include block end it 'doesnt allow more than 5 rooms per block' do + rooms = @available_rooms.first(6) + proc { @admin.create_room_block(rooms, "Comicon", @start_date, @end_date, 0.2)}.must_raise StandardError end - it 'only includes rooms that are available when block is made' do + it 'doesnt allow less than 2 rooms per block' do + rooms = @available_rooms.first + + proc { @admin.create_room_block(rooms, "Comicon", @start_date, @end_date, 0.2)}.must_raise StandardError + end + it 'doesnt allow rooms that are unavailable when block is made' do + 20.times { |x| @admin.reserve_room(x+1, 'Guest #{x+1}', @start_date, @end_date) } + rooms = @available_rooms.first(5) + + proc {@admin.create_room_block(rooms, "Comicon", @start_date, @end_date, 0.2)}.must_raise StandardError end it 'deals with if there are not enough rooms for the block' do # edge case to think about + 17.times { |x| @admin.reserve_room(x+1, 'Guest #{x+1}', @start_date, @end_date) } + rooms = @available_rooms.last(5) + + proc {@admin.create_room_block(rooms, "Comicon", @start_date, @end_date, 0.2)}.must_raise StandardError end - it 'doesnt allow general public to reserve a room from the block' do - end - it 'doesnt allow rooms to be used for another block' do + it 'doesnt allow rooms to be used for another block or single reservation' do + rooms = @available_rooms.first(5) + @admin.create_room_block(rooms, "Comicon", @start_date, @end_date, 0.2) + + proc {@admin.create_room_block(rooms, "Fanime", @start_date, @end_date, 0.2)}.must_raise StandardError + proc {@admin.reserve_room(rooms.first, "Random Author", @start_date, @end_date)}.must_raise StandardError end end describe 'reserve_room_from_block' do @@ -130,6 +157,8 @@ end it 'doesnt allow reservation within block to have any other date range than blocks' do end + it 'doesnt allow general public to reserve a room from the block' do + end end describe 'check_block_room_availibility' do it 'returns an array of rooms' do From 001caa3553b6e3990b498f329c323874951a3a77 Mon Sep 17 00:00:00 2001 From: Kaitlin Forsman Date: Fri, 9 Mar 2018 19:31:28 -0800 Subject: [PATCH 24/34] finished functionality for reserving a room from the block within the User class --- lib/user.rb | 4 ++++ specs/user_spec.rb | 19 +++++++++++++++---- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/lib/user.rb b/lib/user.rb index 675b24d37..b6df712d1 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -87,5 +87,9 @@ def create_room_block(rooms, party, start_date, end_date, discount) @reservations << new_block new_block end + + def reserve_room_from_block(block, room_num, guest) + block.reserve_room(room_num, guest) + end end end diff --git a/specs/user_spec.rb b/specs/user_spec.rb index 2b26f2eb8..50304f9ca 100644 --- a/specs/user_spec.rb +++ b/specs/user_spec.rb @@ -144,7 +144,7 @@ proc {@admin.create_room_block(rooms, "Comicon", @start_date, @end_date, 0.2)}.must_raise StandardError end - it 'doesnt allow rooms to be used for another block or single reservation' do + it 'doesnt allow rooms to be used for another block or single reservation from general public' do rooms = @available_rooms.first(5) @admin.create_room_block(rooms, "Comicon", @start_date, @end_date, 0.2) @@ -153,11 +153,22 @@ end end describe 'reserve_room_from_block' do - it 'allows reservation of a room within a block' do + before do + @admin = Hotel::User.new + @start_date = Date.new(2018,3,15) + @end_date = Date.new(2018,3,20) + @available_rooms = @admin.find_available_rooms(@start_date, @end_date) + @block = @admin.create_room_block(@available_rooms.first(5), "Comicon", @start_date, @end_date, 0.2) end - it 'doesnt allow reservation within block to have any other date range than blocks' do + it 'returns reservation of a room within a block' do + room_num = @available_rooms[0].room_num + reservation = @admin.reserve_room_from_block(@block, room_num, "Spiderman") + + reservation.must_be_instance_of Hotel::Reservation + reservation.room.must_equal @available_rooms[0] end - it 'doesnt allow general public to reserve a room from the block' do + it 'doesnt allow reservation within block to have any other date range than blocks' do + # something I can't really test because method doesn't take a date_range from user end end describe 'check_block_room_availibility' do From daa21aa2c949eb08b0a1926cd17c94150c01ec62 Mon Sep 17 00:00:00 2001 From: Kaitlin Forsman Date: Fri, 9 Mar 2018 19:54:02 -0800 Subject: [PATCH 25/34] finished last piece of functionality, allowing User to search for available rooms in a block -- finished first attempt at all basic requirements for this assignment --- lib/user.rb | 4 ++++ specs/user_spec.rb | 17 ++++++++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/lib/user.rb b/lib/user.rb index b6df712d1..4a27b0bd8 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -91,5 +91,9 @@ def create_room_block(rooms, party, start_date, end_date, discount) def reserve_room_from_block(block, room_num, guest) block.reserve_room(room_num, guest) end + + def check_block_room_availibility(block) + block.find_available_rooms + end end end diff --git a/specs/user_spec.rb b/specs/user_spec.rb index 50304f9ca..2244a33a2 100644 --- a/specs/user_spec.rb +++ b/specs/user_spec.rb @@ -172,8 +172,23 @@ end end describe 'check_block_room_availibility' do + before do + @admin = Hotel::User.new + @start_date = Date.new(2018,3,15) + @end_date = Date.new(2018,3,20) + @available_rooms = @admin.find_available_rooms(@start_date, @end_date) + @block = @admin.create_room_block(@available_rooms.first(5), "Comicon", @start_date, @end_date, 0.2) + end it 'returns an array of rooms' do - # available_rooms_in_block.must_be_kind_of Array + before_available_rooms_in_block = @admin.check_block_room_availibility(@block) + room_num = @available_rooms[0].room_num + reservation = @admin.reserve_room_from_block(@block, room_num, "Spiderman") + after_available_rooms_in_block = @admin.check_block_room_availibility(@block) + + before_available_rooms_in_block.length.must_equal 5 + before_available_rooms_in_block.must_include @available_rooms[0] + after_available_rooms_in_block.length.must_equal 4 + after_available_rooms_in_block.wont_include @available_rooms[0] end # rest of this method is tested in the block_spec end From a87ae3085a9a7c917a37052ed91b8cdd97bf4ff8 Mon Sep 17 00:00:00 2001 From: Kaitlin Forsman Date: Mon, 12 Mar 2018 00:37:03 -0700 Subject: [PATCH 26/34] while everything is still working: refactoring, pulled rooms out of initialize to use dependency injection instead and trying to separate other dependencies --- lib/block.rb | 11 ++++++++--- lib/reservation.rb | 2 +- lib/room.rb | 5 +++-- lib/user.rb | 9 +++++---- specs/block_spec.rb | 14 +++++++------- specs/reservation_spec.rb | 8 ++++---- specs/room_spec.rb | 6 +++--- specs/user_spec.rb | 32 ++++++++++++++++++++++++-------- 8 files changed, 55 insertions(+), 32 deletions(-) diff --git a/lib/block.rb b/lib/block.rb index 79b9579f8..bc928bac8 100644 --- a/lib/block.rb +++ b/lib/block.rb @@ -2,7 +2,7 @@ require 'pry' module Hotel - class Block < User + class Block < Reservation attr_reader :id, :reservations def initialize(id, rooms, party, date_range, discount) @id = id @@ -33,8 +33,13 @@ def reserve_room(room_num, guest) new_reservation end - def find_reservation_cost(reservation_id) - ((1 - @discount) * super(reservation_id)).round(2) + def calculate_reservation_cost(reservation_id) + reservation = find_reservation(reservation_id) + ((1 - @discount) * reservation.calculate_reservation_cost).round(2) + end + + def find_reservation(reservation_id) + @reservations.find { |reservation| reservation.id == reservation_id } end end end diff --git a/lib/reservation.rb b/lib/reservation.rb index 457509296..f029da7e1 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -19,7 +19,7 @@ def find_all_dates end def calculate_reservation_cost - calculate_nights * COST_PER_NIGHT + calculate_nights * @room.cost end end end diff --git a/lib/room.rb b/lib/room.rb index 9aab4f682..d91a74ee7 100644 --- a/lib/room.rb +++ b/lib/room.rb @@ -3,9 +3,10 @@ module Hotel class Room - attr_reader :room_num, :calendar - def initialize(room_num) + attr_reader :room_num, :calendar, :cost + def initialize(room_num, cost) @room_num = room_num + @cost = cost @calendar = [] end def add_to_calendar(date_range) diff --git a/lib/user.rb b/lib/user.rb index 4a27b0bd8..9e3bbc01b 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -3,14 +3,15 @@ module Hotel TOTAL_ROOMS = 20 - COST_PER_NIGHT = 200 class User attr_reader :rooms - def initialize - @rooms = [] + def initialize(rooms) + if rooms.size != TOTAL_ROOMS + raise StandardError.new("incorrect number of rooms") + end + @rooms = rooms @reservations = [] @calendar = {} - TOTAL_ROOMS.times { | room_num | @rooms << Room.new(room_num + 1)} end def find_available_rooms(start_date, end_date) date_range = (start_date...end_date) diff --git a/specs/block_spec.rb b/specs/block_spec.rb index 8b523ad63..aae700c19 100644 --- a/specs/block_spec.rb +++ b/specs/block_spec.rb @@ -6,7 +6,7 @@ describe 'instantiation' do before do rooms = [] - 4.times {|x| rooms << Hotel::Room.new(x+1) } + 4.times {|x| rooms << Hotel::Room.new(x+1, 200) } date_range = (Date.new(2018,3,22)...Date.new(2018,3,26)) @block = Hotel::Block.new(1, rooms, "Fanime", date_range, 0.2) end @@ -14,7 +14,7 @@ @block.must_be_instance_of Hotel::Block end it 'inherits from User' do - @block.must_be_kind_of Hotel::User + @block.must_be_kind_of Hotel::Reservation end it 'has attributes: id, list of some rooms, reservations, party, date range, discount' do # may not need this test stub.... @@ -25,7 +25,7 @@ describe 'find_available_rooms' do before do @rooms = [] - 4.times {|x| @rooms << Hotel::Room.new(x+1) } + 4.times {|x| @rooms << Hotel::Room.new(x+1, 200) } date_range = (Date.new(2018,3,22)...Date.new(2018,3,26)) @block = Hotel::Block.new(1, @rooms, "Fanime", date_range, 0.2) end @@ -55,7 +55,7 @@ describe 'reserve_room' do before do rooms = [] - 4.times {|x| rooms << Hotel::Room.new(x+1) } + 4.times {|x| rooms << Hotel::Room.new(x+1, 200) } date_range = (Date.new(2018,3,22)...Date.new(2018,3,26)) @block = Hotel::Block.new(1, rooms, "Fanime", date_range, 0.2) end @@ -69,17 +69,17 @@ proc { @block.reserve_room(2, "Jade Poe")}.must_raise StandardError end end - describe 'find_reservation_cost' do + describe 'calculate_reservation_cost' do before do rooms = [] - 4.times {|x| rooms << Hotel::Room.new(x+1) } + 4.times {|x| rooms << Hotel::Room.new(x+1, 200) } date_range = (Date.new(2018,3,22)...Date.new(2018,3,26)) @block = Hotel::Block.new(1, rooms, "Fanime", date_range, 0.2) end it 'returns discounted cost for the reservation' do @block.reserve_room(1, "Meka Starbright") - @block.find_reservation_cost(1).must_equal 640 + @block.calculate_reservation_cost(1).must_equal 640 end end end diff --git a/specs/reservation_spec.rb b/specs/reservation_spec.rb index a12a0d124..b3aa7fda0 100644 --- a/specs/reservation_spec.rb +++ b/specs/reservation_spec.rb @@ -4,7 +4,7 @@ describe 'Reservation' do before do date_range = (Date.new(2018,3,18)...Date.new(2018,3,26)) - room = Hotel::Room.new(4) + room = Hotel::Room.new(4, 200) @reservation = Hotel::Reservation.new(1, room, "Bob", date_range) end describe 'initialization' do @@ -23,7 +23,7 @@ describe 'calculate_nights' do before do date_range = (Date.new(2018,3,18)...Date.new(2018,3,26)) - room = Hotel::Room.new(4) + room = Hotel::Room.new(4, 200) @reservation = Hotel::Reservation.new(1, room, "Bob", date_range) end it 'returns the number of nights' do @@ -36,7 +36,7 @@ describe 'find_all_dates' do it 'returns an array of all dates in reservation' do date_range = (Date.new(2018,3,20)...Date.new(2018,3,22)) - room = Hotel::Room.new(4) + room = Hotel::Room.new(4, 200) reservation = Hotel::Reservation.new(1, room, "Kaeli", date_range) all_dates = reservation.find_all_dates @@ -48,7 +48,7 @@ describe 'calculate_reservation_cost' do it 'returns the cost of the reservation' do date_range = (Date.new(2018,3,20)...Date.new(2018,3,25)) - room = Hotel::Room.new(4) + room = Hotel::Room.new(4, 200) reservation = Hotel::Reservation.new(1, room, "Bob", date_range) reservation.calculate_reservation_cost.must_equal 1000 diff --git a/specs/room_spec.rb b/specs/room_spec.rb index 410c41129..827b3bc57 100644 --- a/specs/room_spec.rb +++ b/specs/room_spec.rb @@ -4,13 +4,13 @@ describe 'Room class' do describe 'initialize' do it 'can be initialized' do - room_2 = Hotel::Room.new(2) + room_2 = Hotel::Room.new(2, 200) room_2.must_be_instance_of Hotel::Room end it 'has empty calendar and room number' do - room_2 = Hotel::Room.new(2) + room_2 = Hotel::Room.new(2, 200) room_2.must_respond_to :room_num room_2.room_num.must_be_kind_of Integer @@ -22,7 +22,7 @@ end describe 'add_to_calendar' do it 'can add a date_range to its calendar' do - room = Hotel::Room.new(2) + room = Hotel::Room.new(2, 200) date_range = (Date.new(2018,3,8)...Date.new(2018,3,10)) room.add_to_calendar(date_range) diff --git a/specs/user_spec.rb b/specs/user_spec.rb index 2244a33a2..dc078632e 100644 --- a/specs/user_spec.rb +++ b/specs/user_spec.rb @@ -4,7 +4,9 @@ describe 'User' do describe 'initialization' do before do - @admin = Hotel::User.new + rooms = [] + 20.times {|x| rooms << Hotel::Room.new(x+1, 200) } + @admin = Hotel::User.new(rooms) end it 'can be initialized' do @admin.must_be_instance_of Hotel::User @@ -17,7 +19,9 @@ end describe 'find_available_rooms' do before do - @admin = Hotel::User.new + rooms = [] + 20.times {|x| rooms << Hotel::Room.new(x+1, 200) } + @admin = Hotel::User.new(rooms) @start_date = Date.new(2018,3,15) @end_date = Date.new(2018,3,20) end @@ -59,7 +63,9 @@ end describe 'reserve_room' do before do - @admin = Hotel::User.new + rooms = [] + 20.times {|x| rooms << Hotel::Room.new(x+1, 200) } + @admin = Hotel::User.new(rooms) @new_reservation = @admin.reserve_room(5, "Jade Poe", Date.new(2018, 3, 20), Date.new(2018, 3, 25)) end it 'throws StandardError for invalidate dates' do @@ -88,7 +94,9 @@ end describe 'find_reservation_cost' do it 'returns cost for reservation with reservation id' do - admin = Hotel::User.new + rooms = [] + 20.times {|x| rooms << Hotel::Room.new(x+1, 200) } + admin = Hotel::User.new(rooms) admin.reserve_room(5, "Kaeli Poe", Date.new(2018, 3, 20), Date.new(2018, 3, 25)) admin.find_reservation_cost(1).must_equal 1000 @@ -96,7 +104,9 @@ end describe 'find_reservations_for_given_date' do before do - @admin = Hotel::User.new + rooms = [] + 20.times {|x| rooms << Hotel::Room.new(x+1, 200) } + @admin = Hotel::User.new(rooms) @new_reservation = @admin.reserve_room(5, "Jade Poe", Date.new(2018, 3, 20), Date.new(2018, 3, 25)) @new_reservation_2 = @admin.reserve_room(8, "Kal Smith", Date.new(2018, 3, 20), Date.new(2018, 3, 25)) @date = Date.new(2018, 3, 23) @@ -110,7 +120,9 @@ end describe 'create_room_block' do before do - @admin = Hotel::User.new + rooms = [] + 20.times {|x| rooms << Hotel::Room.new(x+1, 200) } + @admin = Hotel::User.new(rooms) @start_date = Date.new(2018,3,15) @end_date = Date.new(2018,3,20) @available_rooms = @admin.find_available_rooms(@start_date, @end_date) @@ -154,7 +166,9 @@ end describe 'reserve_room_from_block' do before do - @admin = Hotel::User.new + rooms = [] + 20.times {|x| rooms << Hotel::Room.new(x+1, 200) } + @admin = Hotel::User.new(rooms) @start_date = Date.new(2018,3,15) @end_date = Date.new(2018,3,20) @available_rooms = @admin.find_available_rooms(@start_date, @end_date) @@ -173,7 +187,9 @@ end describe 'check_block_room_availibility' do before do - @admin = Hotel::User.new + rooms = [] + 20.times {|x| rooms << Hotel::Room.new(x+1, 200) } + @admin = Hotel::User.new(rooms) @start_date = Date.new(2018,3,15) @end_date = Date.new(2018,3,20) @available_rooms = @admin.find_available_rooms(@start_date, @end_date) From dcd77c7f6160802a094c8a72c0f564417f96b80c Mon Sep 17 00:00:00 2001 From: Kaitlin Forsman Date: Sat, 31 Mar 2018 23:19:37 -0700 Subject: [PATCH 27/34] finished answering question for code analysis of Online Shopping Cart for first part of the Design Activity --- design-activity.md | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 design-activity.md diff --git a/design-activity.md b/design-activity.md new file mode 100644 index 000000000..c3158be59 --- /dev/null +++ b/design-activity.md @@ -0,0 +1,34 @@ +#Hotel Revisited +***** +### Questions for Online Shopping Cart Code Implementations: +* What classes does each implementation include? Are the lists the same? + - Both implementations have the same classes: CartEntry, ShoppingCart, and Order. +* Write down a sentence to describe each class. + - CartEntry appears to be a class for containing information about a specific item or product. + - ShoppingCart appears to be a class that stores a list of items or products. + - Order appears to be class for calculating total cost of the ShoppingCart, including SALES_TAX. +* How do the classes relate to each other? It might be helpful to draw a diagram. + - There doesn't appear to be any explicit relationship between the CartEntry and any of the other classes, but I would assume a ShoppingCart could potentially have one or more CartEntry instances. + - An instance of a Order within this implementation has-a ShoppingCart. +* What data does each class store? How (if at all) does this differ between the two implementations? + - The appears to be no difference between the two implementations for the data each class stores: CartEntry stores the \@quantity (of the product) and \@unit_price (cost per product), ShoppingCart stores \@entries (a list of some entries, in this code, like CartEntry instances), and Order stores \@cart (a ShoppingCart) and the constant, SALES_TAX. +* What methods does each class have? How (if at all) does this differ between the two implementations? + - Each implementation as an initialize method for the three classes. + - Implementation A: uses helper methods in each "lower level" class, CartEntry and ShoppingCart. Then, Order#total_price calculates the sum all entries together and adds on sales tax. + - Implementation B: uses no helper methods in any class. Instead, CartEntry and ShoppingCart have price instance methods that calculate the their own prices. Then, Order only has to add the sales tax cost to the price in Order#total_price. +* 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? + - In Implementation A, I would say the logic is solely retained in Order. + - In Implementation B, the logic is shared between all three classes, as such delegated to the "lower level" classes: ShoppingCart and CartEntry. + * Does total_price directly manipulate the instance variables of other classes? + - In Implementation A, yes, it does (helper methods). + - In Implementation B, no, it does not seem to. +* If we decide items are cheaper if bought in bulk, how would this change the code? Which implementation is easier to modify? + - You would need to add some conditional if quantity > some_amount then unit_price = unit_price * some_discount. This code could be added into the each loop of total_price for Implementation A or the instance method for price of CartEntry in Implementation B. + - As far as difficulty to modify the code to make this possible, I think it would be possible to apply to either method just as easily. It also depends on how complicated the discount logic is. You would need some type of discount for above a certain quantity. If the discount is the same no matter what the item, either method can be changed as easily as the other. If the discount is changes item to item, I think it would be easier to change Implementation B. +* Which implementation better adheres to the single responsibility principle? + - I believe Implementation B adheres better. The Order doesn't have to the responsibility of calculating each CartEntry price then summing all the Entries in the ShoppingCart together to then add on sales tax. +* Bonus question once you've read Metz ch. 3: Which implementation is more loosely coupled? + - Also, Implementation B. It doesn't have to call on the instance variables of CartEntry or ShoppingCart to find the total price for the order. +***** +### The Hotel Revision From 2192c676c12f9b6ed862ee1a16dd1d2b1a2a173f Mon Sep 17 00:00:00 2001 From: Kaitlin Forsman Date: Sun, 1 Apr 2018 16:05:20 -0700 Subject: [PATCH 28/34] adjusted id assignment for blocks and reservationswith a class variable, so the id isn't dependent on the length of the array, which might lead to duplicate ids --- lib/block.rb | 16 ++++++++-------- lib/reservation.rb | 12 ++++++------ lib/user.rb | 7 +++++-- specs/block_spec.rb | 12 ++++++------ specs/reservation_spec.rb | 10 ++++------ specs/user_spec.rb | 7 ++----- 6 files changed, 31 insertions(+), 33 deletions(-) diff --git a/lib/block.rb b/lib/block.rb index bc928bac8..80fd40afb 100644 --- a/lib/block.rb +++ b/lib/block.rb @@ -3,13 +3,13 @@ module Hotel class Block < Reservation + @@id = 1 attr_reader :id, :reservations - def initialize(id, rooms, party, date_range, discount) - @id = id - @rooms = rooms - @party = party - @date_range = date_range - @discount = discount + def initialize block_args + super(block_args) + @party = block_args[:party] + @rooms = block_args[:rooms] + @discount = block_args[:discount] @reservations = [] end @@ -27,8 +27,8 @@ def find_available_rooms def reserve_room(room_num, guest) room = @rooms[room_num - 1] raise StandardError.new("room not available") if !find_available_rooms.include?(room) - id = @reservations.length + 1 - new_reservation = Reservation.new(id, room, guest, @date_range) + new_reservation = Reservation.new({id: @@id, room: room, guest: guest, date_range: @date_range}) + @@id += 1 @reservations << new_reservation new_reservation end diff --git a/lib/reservation.rb b/lib/reservation.rb index f029da7e1..97e5fe99b 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -2,12 +2,12 @@ module Hotel class Reservation - attr_reader :id, :room, :guest, :date_range - def initialize id, room, guest, date_range - @id = id - @room = room - @guest = guest - @date_range = date_range + attr_reader :id, :room, :date_range + def initialize reservation_args + @id = reservation_args[:id] + @room = reservation_args[:room] + @guest = reservation_args[:guest] + @date_range = reservation_args[:date_range] end def calculate_nights diff --git a/lib/user.rb b/lib/user.rb index 9e3bbc01b..03ee32327 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -4,6 +4,7 @@ module Hotel TOTAL_ROOMS = 20 class User + @@id = 1 attr_reader :rooms def initialize(rooms) if rooms.size != TOTAL_ROOMS @@ -32,7 +33,8 @@ def reserve_room(room_num, guest, start_date, end_date) check_room_availibility(room_num, date_range) room = @rooms[room_num - 1] id = @reservations.length + 1 - new_reservation = Reservation.new(id, room, guest, date_range) + new_reservation = Reservation.new({id: @@id, room: room, guest: guest, date_range: date_range}) + @@id += 1 add_to_calendar(new_reservation, date_range) room.add_to_calendar(date_range) @reservations << new_reservation @@ -82,7 +84,8 @@ def create_room_block(rooms, party, start_date, end_date, discount) date_range = (start_date...end_date) rooms.each { |room| check_room_availibility(room.room_num, date_range) } id = @reservations.length + 1 - new_block = Block.new(id, rooms, party, date_range, discount) + new_block = Block.new({id: @@id, rooms: rooms, party: party, date_range: date_range, discount: discount}) + @@id += 1 add_to_calendar(new_block, date_range) rooms.each { |room| room.add_to_calendar(date_range) } @reservations << new_block diff --git a/specs/block_spec.rb b/specs/block_spec.rb index aae700c19..00e74192c 100644 --- a/specs/block_spec.rb +++ b/specs/block_spec.rb @@ -8,7 +8,7 @@ rooms = [] 4.times {|x| rooms << Hotel::Room.new(x+1, 200) } date_range = (Date.new(2018,3,22)...Date.new(2018,3,26)) - @block = Hotel::Block.new(1, rooms, "Fanime", date_range, 0.2) + @block = Hotel::Block.new({id: 1, rooms: rooms, party: "Fanime", date_range: date_range, discount: 0.2}) end it 'can be initialized' do @block.must_be_instance_of Hotel::Block @@ -27,7 +27,7 @@ @rooms = [] 4.times {|x| @rooms << Hotel::Room.new(x+1, 200) } date_range = (Date.new(2018,3,22)...Date.new(2018,3,26)) - @block = Hotel::Block.new(1, @rooms, "Fanime", date_range, 0.2) + @block = Hotel::Block.new({id: 1, rooms: @rooms, party: "Fanime", date_range: date_range, discount: 0.2}) end it 'returns array of rooms available in block' do available_rooms_in_block = @block.find_available_rooms @@ -57,7 +57,7 @@ rooms = [] 4.times {|x| rooms << Hotel::Room.new(x+1, 200) } date_range = (Date.new(2018,3,22)...Date.new(2018,3,26)) - @block = Hotel::Block.new(1, rooms, "Fanime", date_range, 0.2) + @block = Hotel::Block.new({id: 1, rooms: rooms, party: "Fanime", date_range: date_range, discount: 0.2}) end it 'returns a reservation for an available room' do reservation = @block.reserve_room(1, "Fan 1") @@ -74,12 +74,12 @@ rooms = [] 4.times {|x| rooms << Hotel::Room.new(x+1, 200) } date_range = (Date.new(2018,3,22)...Date.new(2018,3,26)) - @block = Hotel::Block.new(1, rooms, "Fanime", date_range, 0.2) + @block = Hotel::Block.new({id: 1, rooms: rooms, party: "Fanime", date_range: date_range, discount: 0.2}) end it 'returns discounted cost for the reservation' do - @block.reserve_room(1, "Meka Starbright") + reservation = @block.reserve_room(1, "Meka Starbright") - @block.calculate_reservation_cost(1).must_equal 640 + @block.calculate_reservation_cost(reservation.id).must_equal 640 end end end diff --git a/specs/reservation_spec.rb b/specs/reservation_spec.rb index b3aa7fda0..c3013721d 100644 --- a/specs/reservation_spec.rb +++ b/specs/reservation_spec.rb @@ -5,7 +5,7 @@ before do date_range = (Date.new(2018,3,18)...Date.new(2018,3,26)) room = Hotel::Room.new(4, 200) - @reservation = Hotel::Reservation.new(1, room, "Bob", date_range) + @reservation = Hotel::Reservation.new({id: 1, room: room, guest: "Bob", date_range: date_range}) end describe 'initialization' do it 'can be initialized' do @@ -15,8 +15,6 @@ @reservation.id.must_equal 1 @reservation.id.must_be_kind_of Integer @reservation.room.must_be_instance_of Hotel::Room - @reservation.guest.must_equal "Bob" - @reservation.guest.must_be_kind_of String @reservation.date_range.must_be_kind_of Range end end @@ -24,7 +22,7 @@ before do date_range = (Date.new(2018,3,18)...Date.new(2018,3,26)) room = Hotel::Room.new(4, 200) - @reservation = Hotel::Reservation.new(1, room, "Bob", date_range) + @reservation = Hotel::Reservation.new({id: 1, room: room, guest: "Bob", date_range: date_range}) end it 'returns the number of nights' do nights = @reservation.calculate_nights @@ -37,7 +35,7 @@ it 'returns an array of all dates in reservation' do date_range = (Date.new(2018,3,20)...Date.new(2018,3,22)) room = Hotel::Room.new(4, 200) - reservation = Hotel::Reservation.new(1, room, "Kaeli", date_range) + reservation = Hotel::Reservation.new({id: 1, room: room, guest: "Kaeli", date_range: date_range}) all_dates = reservation.find_all_dates all_dates.must_be_kind_of Array @@ -49,7 +47,7 @@ it 'returns the cost of the reservation' do date_range = (Date.new(2018,3,20)...Date.new(2018,3,25)) room = Hotel::Room.new(4, 200) - reservation = Hotel::Reservation.new(1, room, "Bob", date_range) + reservation = Hotel::Reservation.new({id: 1, room: room, guest: "Bob", date_range: date_range}) reservation.calculate_reservation_cost.must_equal 1000 end diff --git a/specs/user_spec.rb b/specs/user_spec.rb index dc078632e..ce22ba8da 100644 --- a/specs/user_spec.rb +++ b/specs/user_spec.rb @@ -97,9 +97,9 @@ rooms = [] 20.times {|x| rooms << Hotel::Room.new(x+1, 200) } admin = Hotel::User.new(rooms) - admin.reserve_room(5, "Kaeli Poe", Date.new(2018, 3, 20), Date.new(2018, 3, 25)) + reservation = admin.reserve_room(5, "Kaeli Poe", Date.new(2018, 3, 20), Date.new(2018, 3, 25)) - admin.find_reservation_cost(1).must_equal 1000 + admin.find_reservation_cost(reservation.id).must_equal 1000 end end describe 'find_reservations_for_given_date' do @@ -181,9 +181,6 @@ reservation.must_be_instance_of Hotel::Reservation reservation.room.must_equal @available_rooms[0] end - it 'doesnt allow reservation within block to have any other date range than blocks' do - # something I can't really test because method doesn't take a date_range from user - end end describe 'check_block_room_availibility' do before do From 659b5d0e15f263129f351e84c1564082616fe7dd Mon Sep 17 00:00:00 2001 From: Kaitlin Forsman Date: Sun, 1 Apr 2018 16:33:07 -0700 Subject: [PATCH 29/34] changed code to eliminate other User's direct interaction with each Room instance's calendar --- lib/room.rb | 7 ++++++- lib/user.rb | 11 ++++------- specs/room_spec.rb | 9 +++------ 3 files changed, 13 insertions(+), 14 deletions(-) diff --git a/lib/room.rb b/lib/room.rb index d91a74ee7..956692724 100644 --- a/lib/room.rb +++ b/lib/room.rb @@ -3,7 +3,7 @@ module Hotel class Room - attr_reader :room_num, :calendar, :cost + attr_reader :room_num, :cost def initialize(room_num, cost) @room_num = room_num @cost = cost @@ -13,5 +13,10 @@ def add_to_calendar(date_range) dates = date_range.to_a dates.each { | date | @calendar << date } end + + def is_available date_range + return false if @calendar.any?(date_range) + true + end end end diff --git a/lib/user.rb b/lib/user.rb index 03ee32327..99a1801a7 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -18,10 +18,7 @@ def find_available_rooms(start_date, end_date) date_range = (start_date...end_date) available_rooms = [] @rooms.each do |room| - next if room.calendar.any?(date_range) # this apparently works - # next if room.calendar.any?{ |date| date_range.include?(date) } - # ^ alternative solution that actually make more sense to me ^ - available_rooms << room + available_rooms << room if room.is_available(date_range) end available_rooms # need to deal with no rooms available @@ -52,9 +49,9 @@ def valid_dates(start_date, end_date) end def check_room_availibility(room_num, date_range) - calendar = @rooms[room_num - 1].calendar - return true if calendar.empty? - calendar.each do |date| + calendar = @rooms[room_num - 1].is_available(date_range) + return true if calendar + if !calendar raise StandardError.new("room already reserved") if date_range.include?(date) end end diff --git a/specs/room_spec.rb b/specs/room_spec.rb index 827b3bc57..2a808b0a9 100644 --- a/specs/room_spec.rb +++ b/specs/room_spec.rb @@ -1,7 +1,7 @@ require_relative 'spec_helper' require 'date' -describe 'Room class' do +xdescribe 'Room class' do describe 'initialize' do it 'can be initialized' do room_2 = Hotel::Room.new(2, 200) @@ -15,9 +15,6 @@ room_2.must_respond_to :room_num room_2.room_num.must_be_kind_of Integer room_2.room_num.must_equal 2 - room_2.must_respond_to :calendar - room_2.calendar.must_be_kind_of Array - room_2.calendar.must_equal [] end end describe 'add_to_calendar' do @@ -27,8 +24,8 @@ room.add_to_calendar(date_range) - room.calendar.must_include Date.new(2018,3,9) - room.calendar.wont_include Date.new(2018,3,10) + room.is_available(Date.new(2018,3,9)).must_equal false + room.is_available(Date.new(2018,3,10)).must_equal true end end end From cdcff7b6b8709954655b5180ea85e344ab0d0151 Mon Sep 17 00:00:00 2001 From: Kaitlin Forsman Date: Sun, 1 Apr 2018 16:48:17 -0700 Subject: [PATCH 30/34] adjusted User class so it doesnt need to directly know a reservations date_range (more like adjusted the tests because that seemed to be the main use of the date_range) --- lib/reservation.rb | 2 +- specs/reservation_spec.rb | 1 - specs/user_spec.rb | 5 +---- 3 files changed, 2 insertions(+), 6 deletions(-) diff --git a/lib/reservation.rb b/lib/reservation.rb index 97e5fe99b..7b63a6774 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -2,7 +2,7 @@ module Hotel class Reservation - attr_reader :id, :room, :date_range + attr_reader :id, :room def initialize reservation_args @id = reservation_args[:id] @room = reservation_args[:room] diff --git a/specs/reservation_spec.rb b/specs/reservation_spec.rb index c3013721d..a2bc53e1c 100644 --- a/specs/reservation_spec.rb +++ b/specs/reservation_spec.rb @@ -15,7 +15,6 @@ @reservation.id.must_equal 1 @reservation.id.must_be_kind_of Integer @reservation.room.must_be_instance_of Hotel::Room - @reservation.date_range.must_be_kind_of Range end end describe 'calculate_nights' do diff --git a/specs/user_spec.rb b/specs/user_spec.rb index ce22ba8da..8c40dd295 100644 --- a/specs/user_spec.rb +++ b/specs/user_spec.rb @@ -86,10 +86,7 @@ @new_reservation.must_be_instance_of Hotel::Reservation end it 'allows reservation of room with same start_date as previous reservations end_date' do - next_reservation = @admin.reserve_room(5, "Kaeli Smit", Date.new(2018, 3, 25), Date.new(2018, 3, 27)) - - next_reservation.must_be_instance_of Hotel::Reservation - next_reservation.date_range.must_equal (Date.new(2018, 3, 25)...Date.new(2018, 3, 27)) + proc { @admin.reserve_room(5, "Kaeli Smit", Date.new(2018, 3, 25), Date.new(2018, 3, 27)) }.must_be_silent end end describe 'find_reservation_cost' do From cfbcbec626c66d4fd11de159c3255dd4502d2c1b Mon Sep 17 00:00:00 2001 From: Kaitlin Forsman Date: Sun, 1 Apr 2018 17:08:58 -0700 Subject: [PATCH 31/34] deleted attr_reader for @reservations in Block class. it was used no where in the code by other classes, so didn't need to change anything else --- lib/block.rb | 8 +++++--- lib/reservation.rb | 2 +- specs/reservation_spec.rb | 2 +- specs/user_spec.rb | 15 +++++++-------- 4 files changed, 14 insertions(+), 13 deletions(-) diff --git a/lib/block.rb b/lib/block.rb index 80fd40afb..5ddd52a91 100644 --- a/lib/block.rb +++ b/lib/block.rb @@ -4,20 +4,21 @@ module Hotel class Block < Reservation @@id = 1 - attr_reader :id, :reservations + attr_reader :id def initialize block_args super(block_args) @party = block_args[:party] @rooms = block_args[:rooms] @discount = block_args[:discount] @reservations = [] + @reserved_rooms = [] end def find_available_rooms available_rooms = [] - return @rooms if @reservations.empty? + return @rooms if @reserved_rooms.empty? @rooms.each do |room| - next if @reservations.any? { |reservation| reservation.room == room } + next if @reserved_rooms.any? { |reserved_room| reserved_room == room } available_rooms << room end raise StandardError.new("no rooms available") if available_rooms.empty? @@ -29,6 +30,7 @@ def reserve_room(room_num, guest) raise StandardError.new("room not available") if !find_available_rooms.include?(room) new_reservation = Reservation.new({id: @@id, room: room, guest: guest, date_range: @date_range}) @@id += 1 + @reserved_rooms << room @reservations << new_reservation new_reservation end diff --git a/lib/reservation.rb b/lib/reservation.rb index 7b63a6774..1601ccd31 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -2,7 +2,7 @@ module Hotel class Reservation - attr_reader :id, :room + attr_reader :id def initialize reservation_args @id = reservation_args[:id] @room = reservation_args[:room] diff --git a/specs/reservation_spec.rb b/specs/reservation_spec.rb index a2bc53e1c..c57ce1fdf 100644 --- a/specs/reservation_spec.rb +++ b/specs/reservation_spec.rb @@ -14,7 +14,7 @@ it 'has attributes: id, room, guest, date_range' do @reservation.id.must_equal 1 @reservation.id.must_be_kind_of Integer - @reservation.room.must_be_instance_of Hotel::Room + # @reservation.room.must_be_instance_of Hotel::Room end end describe 'calculate_nights' do diff --git a/specs/user_spec.rb b/specs/user_spec.rb index 8c40dd295..3e87336d3 100644 --- a/specs/user_spec.rb +++ b/specs/user_spec.rb @@ -45,11 +45,11 @@ available_rooms = @admin.find_available_rooms(@start_date, @end_date) - available_rooms.wont_include reservation1.room - available_rooms.wont_include reservation2.room - available_rooms.wont_include reservation3.room - available_rooms.wont_include reservation4.room - available_rooms.wont_include reservation5.room + available_rooms.wont_include @admin.rooms[0] + available_rooms.wont_include @admin.rooms[1] + available_rooms.wont_include @admin.rooms[2] + available_rooms.wont_include @admin.rooms[3] + available_rooms.wont_include @admin.rooms[4] end it 'includes rooms that have same end_date as new start date' do reservation1 = @admin.reserve_room(3, "Sam Sole", Date.new(2018,3,1), Date.new(2018,3,30)) @@ -57,8 +57,8 @@ available_rooms = @admin.find_available_rooms(@start_date, @end_date) - available_rooms.wont_include reservation1.room - available_rooms.must_include reservation2.room + available_rooms.wont_include @admin.rooms[2] + available_rooms.must_include @admin.rooms[3] end end describe 'reserve_room' do @@ -176,7 +176,6 @@ reservation = @admin.reserve_room_from_block(@block, room_num, "Spiderman") reservation.must_be_instance_of Hotel::Reservation - reservation.room.must_equal @available_rooms[0] end end describe 'check_block_room_availibility' do From b4cee383ed224a986b00a61ca483281f5282cbd3 Mon Sep 17 00:00:00 2001 From: Kaitlin Forsman Date: Sun, 1 Apr 2018 17:12:17 -0700 Subject: [PATCH 32/34] altered Room and Reservation class, so that reservation does directly interact with the room's instance variable for cost --- lib/reservation.rb | 2 +- lib/room.rb | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/reservation.rb b/lib/reservation.rb index 1601ccd31..2d3725769 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -19,7 +19,7 @@ def find_all_dates end def calculate_reservation_cost - calculate_nights * @room.cost + @room.calculate_cost(calculate_nights) end end end diff --git a/lib/room.rb b/lib/room.rb index 956692724..9e21981f0 100644 --- a/lib/room.rb +++ b/lib/room.rb @@ -3,7 +3,7 @@ module Hotel class Room - attr_reader :room_num, :cost + attr_reader :room_num def initialize(room_num, cost) @room_num = room_num @cost = cost @@ -18,5 +18,9 @@ def is_available date_range return false if @calendar.any?(date_range) true end + + def calculate_cost nights + @cost * nights + end end end From cd3e93be14fb1b40b4f0cbdead6b12c64ed22d5d Mon Sep 17 00:00:00 2001 From: Kaitlin Forsman Date: Sun, 1 Apr 2018 17:20:54 -0700 Subject: [PATCH 33/34] updated design activity with what discussion of how to improve hotel's design and what I tried to do to implement it --- design-activity.md | 3 +++ lib/room.rb | 1 + 2 files changed, 4 insertions(+) diff --git a/design-activity.md b/design-activity.md index c3158be59..32b9e9338 100644 --- a/design-activity.md +++ b/design-activity.md @@ -32,3 +32,6 @@ - Also, Implementation B. It doesn't have to call on the instance variables of CartEntry or ShoppingCart to find the total price for the order. ***** ### The Hotel Revision +I had a few unnecessary helper methods, so want to change the code, so the classes only have access to the ids of the other classes. This means changing the code so Room doesn't respond to :calendar or :cost, Reservation doesn't respond to :room or :date_range, and Block doesn't respond to :reservations. This is better because if later I change all the information in a class, I can just change that method within the class and the other classes wont know what know any different as long as it is getting the information it needs. + +To revise Hotel, I changed the initialize arguments for Block and Reservation to take a hash instead of separate arguments. I changed the way the classes assign and track ids. Then, I worked on changing all the different classes approach to instance variables of other classes, so that except for ids and room_num, the other classes don't know too much about the others. (hopefully) diff --git a/lib/room.rb b/lib/room.rb index 9e21981f0..205f7260f 100644 --- a/lib/room.rb +++ b/lib/room.rb @@ -9,6 +9,7 @@ def initialize(room_num, cost) @cost = cost @calendar = [] end + def add_to_calendar(date_range) dates = date_range.to_a dates.each { | date | @calendar << date } From e4ff02c9423fe5cd955be97e810019da0846c8a8 Mon Sep 17 00:00:00 2001 From: Kaitlin Forsman Date: Sun, 1 Apr 2018 17:27:34 -0700 Subject: [PATCH 34/34] added some tests to up coverage percentage --- specs/room_spec.rb | 2 +- specs/user_spec.rb | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/specs/room_spec.rb b/specs/room_spec.rb index 2a808b0a9..7d68cedc7 100644 --- a/specs/room_spec.rb +++ b/specs/room_spec.rb @@ -1,7 +1,7 @@ require_relative 'spec_helper' require 'date' -xdescribe 'Room class' do +describe 'Room class' do describe 'initialize' do it 'can be initialized' do room_2 = Hotel::Room.new(2, 200) diff --git a/specs/user_spec.rb b/specs/user_spec.rb index 3e87336d3..6d8ab5667 100644 --- a/specs/user_spec.rb +++ b/specs/user_spec.rb @@ -16,6 +16,16 @@ @admin.rooms[0].must_be_instance_of Hotel::Room @admin.rooms.last.must_be_instance_of Hotel::Room end + + it 'wont allow User to initialize with different number of rooms from TOTAL_ROOMS' do + rooms_1 = [] + rooms_2 = [] + 19.times {|x| rooms_1 << Hotel::Room.new(x+1, 200) } + 21.times {|x| rooms_2 << Hotel::Room.new(x+1, 200) } + + proc { Hotel::User.new(rooms_1) }.must_raise StandardError + proc { Hotel::User.new(rooms_2) }.must_raise StandardError + end end describe 'find_available_rooms' do before do