From ffd84bd5c10d41b6f8260a8b1f6ca4f03b543e4b Mon Sep 17 00:00:00 2001 From: Tiffany Chio Date: Tue, 3 Sep 2019 13:09:47 -0700 Subject: [PATCH 01/38] Created all the lib files. No content yet. --- lib/date.rb | 0 lib/hotel_block.rb | 0 lib/reservation.rb | 0 lib/room.rb | 0 lib/system.rb | 0 5 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 lib/date.rb create mode 100644 lib/hotel_block.rb create mode 100644 lib/reservation.rb create mode 100644 lib/room.rb create mode 100644 lib/system.rb diff --git a/lib/date.rb b/lib/date.rb new file mode 100644 index 000000000..e69de29bb diff --git a/lib/hotel_block.rb b/lib/hotel_block.rb new file mode 100644 index 000000000..e69de29bb diff --git a/lib/reservation.rb b/lib/reservation.rb new file mode 100644 index 000000000..e69de29bb diff --git a/lib/room.rb b/lib/room.rb new file mode 100644 index 000000000..e69de29bb diff --git a/lib/system.rb b/lib/system.rb new file mode 100644 index 000000000..e69de29bb From 45c3a5c45621897c10dd19e929df7c9c938c0f42 Mon Sep 17 00:00:00 2001 From: Tiffany Chio Date: Tue, 3 Sep 2019 13:13:54 -0700 Subject: [PATCH 02/38] Created Room class. All it does is initialize Rooms --- lib/room.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lib/room.rb b/lib/room.rb index e69de29bb..9308d5e54 100644 --- a/lib/room.rb +++ b/lib/room.rb @@ -0,0 +1,12 @@ +module Hotel + class Room + + attr_reader :id, :cost + + def initialize(id, cost) + @id = id + @cost = cost + end + + end +end From b3cf64e50ec87209079e840d4c705723e8ae96e8 Mon Sep 17 00:00:00 2001 From: Tiffany Chio Date: Tue, 3 Sep 2019 13:16:47 -0700 Subject: [PATCH 03/38] Added simple cov and lib files to test_helper. --- test/test_helper.rb | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/test/test_helper.rb b/test/test_helper.rb index c3a7695cf..a620093dc 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,4 +1,8 @@ -# Add simplecov +require 'simplecov' +SimpleCov.start do + add_filter 'test/' # Tests should not be counted toward coverage. +end + require "minitest" require "minitest/autorun" require "minitest/reporters" @@ -6,3 +10,8 @@ Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new # require_relative your lib files here! +require_relative '../lib/date' +require_relative '../lib/hotel_block' +require_relative '../lib/reservation' +require_relative '../lib/room' +require_relative '../lib/system' From 5e39d57db1fdec853baec468dfbd7433fc949a81 Mon Sep 17 00:00:00 2001 From: Tiffany Chio Date: Tue, 3 Sep 2019 13:30:35 -0700 Subject: [PATCH 04/38] Created test for room.rb (initiation). Everything passes. --- lib/room.rb | 2 +- test/room_test.rb | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 test/room_test.rb diff --git a/lib/room.rb b/lib/room.rb index 9308d5e54..488a13972 100644 --- a/lib/room.rb +++ b/lib/room.rb @@ -5,7 +5,7 @@ class Room def initialize(id, cost) @id = id - @cost = cost + @cost = cost # Must be Float end end diff --git a/test/room_test.rb b/test/room_test.rb new file mode 100644 index 000000000..91bba63b3 --- /dev/null +++ b/test/room_test.rb @@ -0,0 +1,23 @@ +require_relative 'test_helper' + +describe "Room class" do + describe "Room initiation" do + before do + # Must use @ to make it accessible without a reader + @room = Hotel::Room.new(15, 200.00) + end + + it "is an instance of Room" do + expect(@room).must_be_kind_of Hotel::Room + end + + it "is set up for specific attributes and data types" do + [:id, :cost].each do |prop| + expect(@room).must_respond_to prop + + expect(@room.id).must_be_kind_of Integer + expect(@room.cost).must_be_kind_of Float + end + end + end +end From b3200790ae097afe2f813cc851d10fa1f219c3d7 Mon Sep 17 00:00:00 2001 From: Tiffany Chio Date: Tue, 3 Sep 2019 14:13:22 -0700 Subject: [PATCH 05/38] Created initialize and helper methods for reservations. Untested. --- lib/reservation.rb | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/lib/reservation.rb b/lib/reservation.rb index e69de29bb..87da44731 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -0,0 +1,37 @@ +require 'date' +require_relative 'room' + +module Hotel + class Reservation + + attr_reader :id, :room, :start_date, :end_date, :dates, :total_cost + + def initialize(id:, room:, start_date:, end_date:, total_cost:nil) + @id = id + @room = room # this is a room object + @start_date = start_date # this is already a date instance + @end_date = end_date # this is already a date instance + @dates = create_date_array # this does not include the end date, so all occupied nights + @total_cost = total_cost || self.calc_cost + end + + # Returns array of date objects for all dates + # start_date to end_date + # end_date not inclusive + def create_date_array + @dates = [] + current_date = @start_date.dup + + # TIFF: you can change it to > if you need to include end date + until current_date == @end_date + @dates << current_date + current_date += 1 + end + end + + def self.calc_cost + return @dates.length * @room.cost + end + + end +end From 783424faee7e0c8454da70e01a7a5b80d14fdcca Mon Sep 17 00:00:00 2001 From: Tiffany Chio Date: Tue, 3 Sep 2019 15:04:29 -0700 Subject: [PATCH 06/38] Fixed multi use of Date name in reservations variables. Fixed reservations overall. Created and passed all reservation initialization tests. --- lib/{date.rb => date_range.rb} | 0 lib/reservation.rb | 18 +++++++-------- test/reservation_test.rb | 42 ++++++++++++++++++++++++++++++++++ test/room_test.rb | 8 +++---- test/test_helper.rb | 4 +++- 5 files changed, 57 insertions(+), 15 deletions(-) rename lib/{date.rb => date_range.rb} (100%) create mode 100644 test/reservation_test.rb diff --git a/lib/date.rb b/lib/date_range.rb similarity index 100% rename from lib/date.rb rename to lib/date_range.rb diff --git a/lib/reservation.rb b/lib/reservation.rb index 87da44731..d233d131d 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -4,33 +4,31 @@ module Hotel class Reservation - attr_reader :id, :room, :start_date, :end_date, :dates, :total_cost + attr_reader :id, :room, :start_date, :end_date, :date_range, :total_cost - def initialize(id:, room:, start_date:, end_date:, total_cost:nil) + def initialize(id:, room:, start_date:, end_date:) @id = id @room = room # this is a room object @start_date = start_date # this is already a date instance @end_date = end_date # this is already a date instance - @dates = create_date_array # this does not include the end date, so all occupied nights - @total_cost = total_cost || self.calc_cost + @date_range = create_date_array # this does not include the end date, so all occupied nights + @total_cost = total_cost || @date_range.length * @room.cost end # Returns array of date objects for all dates # start_date to end_date # end_date not inclusive def create_date_array - @dates = [] + days = [] current_date = @start_date.dup # TIFF: you can change it to > if you need to include end date until current_date == @end_date - @dates << current_date + days << current_date current_date += 1 end - end - - def self.calc_cost - return @dates.length * @room.cost + + return days end end diff --git a/test/reservation_test.rb b/test/reservation_test.rb new file mode 100644 index 000000000..58bff2a25 --- /dev/null +++ b/test/reservation_test.rb @@ -0,0 +1,42 @@ +require_relative 'test_helper' + +describe "Reservation class" do + describe "Reservation instantiation" do + before do + room_15 = Hotel::Room.new(15, 200.00) + start_time = Date.parse('2019-09-03') + end_time = Date.parse('2019-09-08') + + @reservation = Hotel::Reservation.new(id: 101, room: room_15, start_date: start_time, end_date: end_time) + end + + it "is an instance of Reservation" do + expect(@reservation).must_be_kind_of Hotel::Reservation + end + + it "is set up for specific attributes and data types" do + [:id, :room, :start_date, :end_date, :date_range, :total_cost].each do |prop| + expect(@reservation).must_respond_to prop + end + + expect(@reservation.id).must_be_instance_of Integer + expect(@reservation.room).must_be_instance_of Hotel::Room + expect(@reservation.start_date).must_be_instance_of Date + expect(@reservation.end_date).must_be_instance_of Date + expect(@reservation.date_range).must_be_instance_of Array + expect(@reservation.total_cost).must_be_instance_of Float + end + + it "correctly calculates date_range" do + expect(@reservation.date_range.length).must_equal 5 + + expect(@reservation.date_range.first).must_equal @reservation.start_date + expect(@reservation.date_range.last).must_equal @reservation.end_date - 1 + end + + it "correctly calculates total_cost" do + expect(@reservation.total_cost).must_be_close_to 200.0*5 + end + + end +end diff --git a/test/room_test.rb b/test/room_test.rb index 91bba63b3..4bfdc7dab 100644 --- a/test/room_test.rb +++ b/test/room_test.rb @@ -1,7 +1,7 @@ require_relative 'test_helper' describe "Room class" do - describe "Room initiation" do + describe "Room instantiation" do before do # Must use @ to make it accessible without a reader @room = Hotel::Room.new(15, 200.00) @@ -14,10 +14,10 @@ it "is set up for specific attributes and data types" do [:id, :cost].each do |prop| expect(@room).must_respond_to prop - - expect(@room.id).must_be_kind_of Integer - expect(@room.cost).must_be_kind_of Float end + + expect(@room.id).must_be_instance_of Integer + expect(@room.cost).must_be_instance_of Float end end end diff --git a/test/test_helper.rb b/test/test_helper.rb index a620093dc..e046f15b8 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -3,6 +3,8 @@ add_filter 'test/' # Tests should not be counted toward coverage. end +require 'date' + require "minitest" require "minitest/autorun" require "minitest/reporters" @@ -10,7 +12,7 @@ Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new # require_relative your lib files here! -require_relative '../lib/date' +require_relative '../lib/date_range' require_relative '../lib/hotel_block' require_relative '../lib/reservation' require_relative '../lib/room' From 092ea0316f96f5f63dd8bfb9f7e236aeb9c8ea28 Mon Sep 17 00:00:00 2001 From: Tiffany Chio Date: Tue, 3 Sep 2019 15:13:46 -0700 Subject: [PATCH 07/38] added argument error checks for start and end date. Tests created and passed for that item. --- lib/reservation.rb | 4 ++++ test/reservation_test.rb | 17 +++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/lib/reservation.rb b/lib/reservation.rb index d233d131d..29bd5f1a1 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -7,6 +7,10 @@ class Reservation attr_reader :id, :room, :start_date, :end_date, :date_range, :total_cost def initialize(id:, room:, start_date:, end_date:) + if end_date <= start_date + raise ArgumentError, 'End date must be after start date.' + end + @id = id @room = room # this is a room object @start_date = start_date # this is already a date instance diff --git a/test/reservation_test.rb b/test/reservation_test.rb index 58bff2a25..c3db7ec44 100644 --- a/test/reservation_test.rb +++ b/test/reservation_test.rb @@ -36,7 +36,24 @@ it "correctly calculates total_cost" do expect(@reservation.total_cost).must_be_close_to 200.0*5 + end + end + + describe "raises an exception when an invalid date range is provided" do + it "raises an error if end date is before start date" do + room_15 = Hotel::Room.new(15, 200.00) + start_time = Date.parse('2019-09-03') + end_time = Date.parse('2019-09-01') + + expect{Hotel::Reservation.new(id: 101, room: room_15, start_date: start_time, end_date: end_time)}.must_raise ArgumentError end + it "raises an error if end date is on same day as start date" do + room_15 = Hotel::Room.new(15, 200.00) + start_time = Date.parse('2019-09-03') + end_time = Date.parse('2019-09-03') + + expect{Hotel::Reservation.new(id: 101, room: room_15, start_date: start_time, end_date: end_time)}.must_raise ArgumentError + end end end From 023e763b3c23c4435cc5b22886a781030418c7cf Mon Sep 17 00:00:00 2001 From: Tiffany Chio Date: Tue, 3 Sep 2019 15:14:45 -0700 Subject: [PATCH 08/38] Minor fix to total_cost in reservation.rb --- lib/reservation.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/reservation.rb b/lib/reservation.rb index 29bd5f1a1..b6f5963c2 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -16,7 +16,7 @@ def initialize(id:, room:, start_date:, end_date:) @start_date = start_date # this is already a date instance @end_date = end_date # this is already a date instance @date_range = create_date_array # this does not include the end date, so all occupied nights - @total_cost = total_cost || @date_range.length * @room.cost + @total_cost = @date_range.length * @room.cost end # Returns array of date objects for all dates From 00455f0db9d55490ca9c7f21ed2a270fc4bf931b Mon Sep 17 00:00:00 2001 From: Tiffany Chio Date: Tue, 3 Sep 2019 16:09:26 -0700 Subject: [PATCH 09/38] added HotelDate class. No tests have been added or processed yet. --- lib/date_range.rb | 0 lib/hoteldate.rb | 19 +++++++++++++++++++ lib/reservation.rb | 2 +- test/test_helper.rb | 2 +- 4 files changed, 21 insertions(+), 2 deletions(-) delete mode 100644 lib/date_range.rb create mode 100644 lib/hoteldate.rb diff --git a/lib/date_range.rb b/lib/date_range.rb deleted file mode 100644 index e69de29bb..000000000 diff --git a/lib/hoteldate.rb b/lib/hoteldate.rb new file mode 100644 index 000000000..25fe88037 --- /dev/null +++ b/lib/hoteldate.rb @@ -0,0 +1,19 @@ +module Hotel + class HotelDate + + attr_reader :id, :availability + + def initialize(id, rooms_list) + @id = id # This is a date object + @availability = rooms_list.map{ |room| [room, :AVAILABLE] }.to_h + end + + # availability is a hash with key room_id and values + # :AVAILABLE or reservation instance + # Devin said that for end dates, reservation should not show up + def self.add_reservation(reservation) + @availability[reservation.room] => reservation + end + + end +end diff --git a/lib/reservation.rb b/lib/reservation.rb index b6f5963c2..fa16d4672 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -16,7 +16,7 @@ def initialize(id:, room:, start_date:, end_date:) @start_date = start_date # this is already a date instance @end_date = end_date # this is already a date instance @date_range = create_date_array # this does not include the end date, so all occupied nights - @total_cost = @date_range.length * @room.cost + @total_cost = (@end_date - @start_date) * @room.cost end # Returns array of date objects for all dates diff --git a/test/test_helper.rb b/test/test_helper.rb index e046f15b8..6eff89e11 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -12,7 +12,7 @@ Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new # require_relative your lib files here! -require_relative '../lib/date_range' +require_relative '../lib/hoteldate' require_relative '../lib/hotel_block' require_relative '../lib/reservation' require_relative '../lib/room' From 2f1037856168d201af5079f86801e1866d97433a Mon Sep 17 00:00:00 2001 From: Tiffany Chio Date: Tue, 3 Sep 2019 17:07:39 -0700 Subject: [PATCH 10/38] added HotelDate test file. All tests passed. HotelDate editted to reflect changes to fix bugs. --- lib/hoteldate.rb | 9 ++++--- lib/room.rb | 2 +- test/hoteldate_test.rb | 60 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 4 deletions(-) create mode 100644 test/hoteldate_test.rb diff --git a/lib/hoteldate.rb b/lib/hoteldate.rb index 25fe88037..9f643a431 100644 --- a/lib/hoteldate.rb +++ b/lib/hoteldate.rb @@ -1,3 +1,6 @@ +require_relative 'room' +require_relative 'reservation' + module Hotel class HotelDate @@ -5,14 +8,14 @@ class HotelDate def initialize(id, rooms_list) @id = id # This is a date object - @availability = rooms_list.map{ |room| [room, :AVAILABLE] }.to_h + @availability = rooms_list.map{ |room| [room.id, :AVAILABLE] }.to_h # This is a hash, input was array end # availability is a hash with key room_id and values # :AVAILABLE or reservation instance # Devin said that for end dates, reservation should not show up - def self.add_reservation(reservation) - @availability[reservation.room] => reservation + def add_reservation(reservation) + @availability[reservation.room.id] = reservation end end diff --git a/lib/room.rb b/lib/room.rb index 488a13972..730b1ff0d 100644 --- a/lib/room.rb +++ b/lib/room.rb @@ -4,7 +4,7 @@ class Room attr_reader :id, :cost def initialize(id, cost) - @id = id + @id = id # Must be Integer @cost = cost # Must be Float end diff --git a/test/hoteldate_test.rb b/test/hoteldate_test.rb new file mode 100644 index 000000000..f075ee7bf --- /dev/null +++ b/test/hoteldate_test.rb @@ -0,0 +1,60 @@ +require_relative 'test_helper' + +describe "HotelDate class" do + describe "HotelDate instantiation" do + before do + id = Date.parse('2019-09-03') + + rooms_list = [] + (1..5).each do |i| + rooms_list << Hotel::Room.new(i, 200.00) + end + + @date_1 = Hotel::HotelDate.new(id, rooms_list) + end + + it "is an instance of HotelDate" do + expect(@date_1).must_be_kind_of Hotel::HotelDate + end + + it "is set up for specific attributes and data types" do + [:id, :availability].each do |prop| + expect(@date_1).must_respond_to prop + end + + expect(@date_1.id).must_be_instance_of Date + expect(@date_1.availability).must_be_instance_of Hash + expect(@date_1.availability.keys.first).must_be_instance_of Integer + expect(@date_1.availability.values.first).must_be_instance_of Symbol + end + + it "correctly sets up availability hash" do + expect(@date_1.availability.length).must_equal 5 + expect(@date_1.availability[3]).must_equal :AVAILABLE + end + end + + # TIFF: I think you need to clean this up. Esp the set-up portion. + it "can add a add_reservation" do + id = Date.parse('2019-09-03') + + rooms_list = [] + (1..20).each do |i| + rooms_list << Hotel::Room.new(i, 200.00) + end + + @date_2 = Hotel::HotelDate.new(id, rooms_list) + + room_15 = Hotel::Room.new(15, 200.00) + start_time = Date.parse('2019-09-03') + end_time = Date.parse('2019-09-08') + + @reservation = Hotel::Reservation.new(id: 101, room: room_15, start_date: start_time, end_date: end_time) + + @date_2.add_reservation(@reservation) + + expect(@date_2.availability[15]).must_equal @reservation + expect(@date_2.availability[5]).must_equal :AVAILABLE + end + +end From f09ba52c66d28128aae11c218913c2fe59fbf4a1 Mon Sep 17 00:00:00 2001 From: Tiffany Chio Date: Wed, 4 Sep 2019 00:33:51 -0700 Subject: [PATCH 11/38] added make reservation method for system.rb --- lib/{hotel_block.rb => hotelblock.rb} | 0 lib/reservation.rb | 1 + lib/system.rb | 87 +++++++++++++++++++++++++++ 3 files changed, 88 insertions(+) rename lib/{hotel_block.rb => hotelblock.rb} (100%) diff --git a/lib/hotel_block.rb b/lib/hotelblock.rb similarity index 100% rename from lib/hotel_block.rb rename to lib/hotelblock.rb diff --git a/lib/reservation.rb b/lib/reservation.rb index fa16d4672..360fb3c51 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -19,6 +19,7 @@ def initialize(id:, room:, start_date:, end_date:) @total_cost = (@end_date - @start_date) * @room.cost end + # Get rid of if not in use # Returns array of date objects for all dates # start_date to end_date # end_date not inclusive diff --git a/lib/system.rb b/lib/system.rb index e69de29bb..fdbcfa4d1 100644 --- a/lib/system.rb +++ b/lib/system.rb @@ -0,0 +1,87 @@ +require 'date' + +require_relative 'room' +require_relative 'reservation' +require_relative 'hoteldate' +require_relative 'hotelblock' + +module Hotel + class System + + attr_reader :rooms, :reservations, :hotel_dates + + NUM_ROOMS = 20 + + def initialize + @rooms = generate_rooms + @reservations = [] + @hotel_dates = [] + end + + def generate_rooms + array_of_room_obj = [] + + (1..NUM_ROOMS).each do |i| + array_of_room_obj << Hotel::Room.new(i, 200.to_f) + end + + return array_of_room_obj + end + + def list_all_rooms + end + + # Assume date inputs come in string format + def make_reservation(start_date:, end_date:) + # Find a room with no overlap + room = find_room(15) + + # Create Reservation + id = @reservations.length + 1 + start_date = Date.parse(start_date) + end_date = Date.parse(end_date) + new_reservation = Hotel::Reservation.new(id: id, room: room, start_date: start_date, end_date: end_date) + + # Connect to Reservations List + @reservations << new_reservation + + # Create Date or Add to it, add to Dates list + add_to_dates(start_date, end_date, new_reservation) + + # return new_reservation + end + + def find_available_room + end + + def add_to_dates(start_date, end_date, new_reservation) + current_date = start_date.dup + + until current_date == end_date + date_obj = find_date(current_date) + if date_obj != nil + date_obj.add_reservation(new_reservation) + else + @hotel_dates << Hotel::HotelDate.new(current_date, @rooms) + find_date(current_date).add_reservation(new_reservation) + end + p current_date + current_date += 1 + end + end + + def find_date(date_obj) + return @hotel_dates.find { |hotel_date| hotel_date.id == date_obj } + end + + def find_room(room_id) + return @rooms.find { |room| room.id == room_id } + end + + def list_reservations_for(date) + end + + def find_total_cost(reservation_id) + end + end +end From 42df5a2c9796e1692452dd9fc9b4d47e37edc05b Mon Sep 17 00:00:00 2001 From: Tiffany Chio Date: Wed, 4 Sep 2019 11:21:38 -0700 Subject: [PATCH 12/38] Removed date range from reservation class and all tests still pass. --- lib/reservation.rb | 18 ------------------ lib/system.rb | 1 + test/reservation_test.rb | 10 +--------- test/test_helper.rb | 2 +- 4 files changed, 3 insertions(+), 28 deletions(-) diff --git a/lib/reservation.rb b/lib/reservation.rb index 360fb3c51..094186a03 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -15,26 +15,8 @@ def initialize(id:, room:, start_date:, end_date:) @room = room # this is a room object @start_date = start_date # this is already a date instance @end_date = end_date # this is already a date instance - @date_range = create_date_array # this does not include the end date, so all occupied nights @total_cost = (@end_date - @start_date) * @room.cost end - # Get rid of if not in use - # Returns array of date objects for all dates - # start_date to end_date - # end_date not inclusive - def create_date_array - days = [] - current_date = @start_date.dup - - # TIFF: you can change it to > if you need to include end date - until current_date == @end_date - days << current_date - current_date += 1 - end - - return days - end - end end diff --git a/lib/system.rb b/lib/system.rb index fdbcfa4d1..6113897b8 100644 --- a/lib/system.rb +++ b/lib/system.rb @@ -29,6 +29,7 @@ def generate_rooms end def list_all_rooms + # We need to create a method? Or can we just do System.rooms end # Assume date inputs come in string format diff --git a/test/reservation_test.rb b/test/reservation_test.rb index c3db7ec44..9fc441b17 100644 --- a/test/reservation_test.rb +++ b/test/reservation_test.rb @@ -15,7 +15,7 @@ end it "is set up for specific attributes and data types" do - [:id, :room, :start_date, :end_date, :date_range, :total_cost].each do |prop| + [:id, :room, :start_date, :end_date, :total_cost].each do |prop| expect(@reservation).must_respond_to prop end @@ -23,17 +23,9 @@ expect(@reservation.room).must_be_instance_of Hotel::Room expect(@reservation.start_date).must_be_instance_of Date expect(@reservation.end_date).must_be_instance_of Date - expect(@reservation.date_range).must_be_instance_of Array expect(@reservation.total_cost).must_be_instance_of Float end - it "correctly calculates date_range" do - expect(@reservation.date_range.length).must_equal 5 - - expect(@reservation.date_range.first).must_equal @reservation.start_date - expect(@reservation.date_range.last).must_equal @reservation.end_date - 1 - end - it "correctly calculates total_cost" do expect(@reservation.total_cost).must_be_close_to 200.0*5 end diff --git a/test/test_helper.rb b/test/test_helper.rb index 6eff89e11..8113a7d3d 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -13,7 +13,7 @@ # require_relative your lib files here! require_relative '../lib/hoteldate' -require_relative '../lib/hotel_block' +require_relative '../lib/hotelblock' require_relative '../lib/reservation' require_relative '../lib/room' require_relative '../lib/system' From f0c6e82286cc36743b0aad14b0cc9453aa2a97ee Mon Sep 17 00:00:00 2001 From: Tiffany Chio Date: Wed, 4 Sep 2019 15:58:42 -0700 Subject: [PATCH 13/38] Tested make reservation method in system. Works great. Haven't done overlap yet. --- lib/hoteldate.rb | 15 +++++--- lib/system.rb | 29 ++++++++------ test/hoteldate_test.rb | 33 ++++------------ test/system_test.rb | 85 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 118 insertions(+), 44 deletions(-) create mode 100644 test/system_test.rb diff --git a/lib/hoteldate.rb b/lib/hoteldate.rb index 9f643a431..6191c2a89 100644 --- a/lib/hoteldate.rb +++ b/lib/hoteldate.rb @@ -4,19 +4,22 @@ module Hotel class HotelDate - attr_reader :id, :availability + attr_reader :id, :occupied - def initialize(id, rooms_list) + def initialize(id) @id = id # This is a date object - @availability = rooms_list.map{ |room| [room.id, :AVAILABLE] }.to_h # This is a hash, input was array + @occupied = {} # This is a hash, input was array end - # availability is a hash with key room_id and values - # :AVAILABLE or reservation instance + # occupied is a hash with key room_id and values of reservation instance # Devin said that for end dates, reservation should not show up def add_reservation(reservation) - @availability[reservation.room.id] = reservation + @occupied[reservation.room.id] = reservation end + # returns array of reservation objects + def list_reservations + return @occupied.values + end end end diff --git a/lib/system.rb b/lib/system.rb index 6113897b8..12df4ab5f 100644 --- a/lib/system.rb +++ b/lib/system.rb @@ -8,21 +8,22 @@ module Hotel class System - attr_reader :rooms, :reservations, :hotel_dates + attr_reader :rooms, :reservations, :hoteldates NUM_ROOMS = 20 + COST_PER_NIGHT = 200 def initialize @rooms = generate_rooms @reservations = [] - @hotel_dates = [] + @hoteldates = [] end def generate_rooms array_of_room_obj = [] (1..NUM_ROOMS).each do |i| - array_of_room_obj << Hotel::Room.new(i, 200.to_f) + array_of_room_obj << Hotel::Room.new(i, COST_PER_NIGHT.to_f) end return array_of_room_obj @@ -35,7 +36,7 @@ def list_all_rooms # Assume date inputs come in string format def make_reservation(start_date:, end_date:) # Find a room with no overlap - room = find_room(15) + room = find_room(20 - @reservations.length) # Create Reservation id = @reservations.length + 1 @@ -50,6 +51,7 @@ def make_reservation(start_date:, end_date:) add_to_dates(start_date, end_date, new_reservation) # return new_reservation + return new_reservation end def find_available_room @@ -60,26 +62,29 @@ def add_to_dates(start_date, end_date, new_reservation) until current_date == end_date date_obj = find_date(current_date) - if date_obj != nil + + if date_obj date_obj.add_reservation(new_reservation) else - @hotel_dates << Hotel::HotelDate.new(current_date, @rooms) + @hoteldates << Hotel::HotelDate.new(current_date) find_date(current_date).add_reservation(new_reservation) end - p current_date + current_date += 1 end end - def find_date(date_obj) - return @hotel_dates.find { |hotel_date| hotel_date.id == date_obj } - end - def find_room(room_id) return @rooms.find { |room| room.id == room_id } end - def list_reservations_for(date) + def find_date(date_obj) + return @hoteldates.find { |hotel_date| hotel_date.id == date_obj } + end + + def list_reservations_for(date_string) + hotel_date = find_date(Date.parse(date_string)) + return hotel_date.list_reservations end def find_total_cost(reservation_id) diff --git a/test/hoteldate_test.rb b/test/hoteldate_test.rb index f075ee7bf..675165043 100644 --- a/test/hoteldate_test.rb +++ b/test/hoteldate_test.rb @@ -3,14 +3,7 @@ describe "HotelDate class" do describe "HotelDate instantiation" do before do - id = Date.parse('2019-09-03') - - rooms_list = [] - (1..5).each do |i| - rooms_list << Hotel::Room.new(i, 200.00) - end - - @date_1 = Hotel::HotelDate.new(id, rooms_list) + @date_1 = Hotel::HotelDate.new(Date.parse('2019-09-03')) end it "is an instance of HotelDate" do @@ -18,43 +11,31 @@ end it "is set up for specific attributes and data types" do - [:id, :availability].each do |prop| + [:id, :occupied].each do |prop| expect(@date_1).must_respond_to prop end expect(@date_1.id).must_be_instance_of Date - expect(@date_1.availability).must_be_instance_of Hash - expect(@date_1.availability.keys.first).must_be_instance_of Integer - expect(@date_1.availability.values.first).must_be_instance_of Symbol + expect(@date_1.occupied).must_be_instance_of Hash end - it "correctly sets up availability hash" do - expect(@date_1.availability.length).must_equal 5 - expect(@date_1.availability[3]).must_equal :AVAILABLE - end end # TIFF: I think you need to clean this up. Esp the set-up portion. it "can add a add_reservation" do id = Date.parse('2019-09-03') - - rooms_list = [] - (1..20).each do |i| - rooms_list << Hotel::Room.new(i, 200.00) - end - - @date_2 = Hotel::HotelDate.new(id, rooms_list) + @date_2 = Hotel::HotelDate.new(id) room_15 = Hotel::Room.new(15, 200.00) start_time = Date.parse('2019-09-03') end_time = Date.parse('2019-09-08') - @reservation = Hotel::Reservation.new(id: 101, room: room_15, start_date: start_time, end_date: end_time) @date_2.add_reservation(@reservation) - expect(@date_2.availability[15]).must_equal @reservation - expect(@date_2.availability[5]).must_equal :AVAILABLE + expect(@date_2.occupied.keys.first).must_be_instance_of Integer + expect(@date_2.occupied.values.first).must_be_instance_of Hotel::Reservation + expect(@date_2.occupied[15]).must_equal @reservation end end diff --git a/test/system_test.rb b/test/system_test.rb new file mode 100644 index 000000000..962d1b65b --- /dev/null +++ b/test/system_test.rb @@ -0,0 +1,85 @@ +require_relative 'test_helper' + +describe "System class" do + describe "System instantiation" do + before do + @sys = Hotel::System.new + end + + it "is an instance of System" do + expect(@sys).must_be_instance_of Hotel::System + end + + it "is set up for specific attributes and data types" do + [:rooms, :reservations, :hoteldates].each do |prop| + expect(@sys).must_respond_to prop + end + + expect(@sys.rooms).must_be_instance_of Array + expect(@sys.rooms.first).must_be_instance_of Hotel::Room + expect(@sys.rooms.last).must_be_instance_of Hotel::Room + expect(@sys.reservations).must_be_instance_of Array + expect(@sys.hoteldates).must_be_instance_of Array + end + + it "executes generate rooms method correctly" do + expect(@sys.rooms.length).must_equal 20 + expect(@sys.rooms[11].id).must_equal 12 + expect(@sys.rooms[11].cost).must_equal 200.0 + end + end + + describe "making a room reservation" do + before do + @sys = Hotel::System.new + @first_res = @sys.make_reservation(start_date: '2019-06-05', end_date: '2019-06-08') + end + + it "can find the correct room given a room_id" do + room_obj = @sys.find_room(16) + + expect(room_obj).must_be_instance_of Hotel::Room + expect(room_obj.id).must_equal 16 + end + + it "returns a new reservation with correct dates" do + expect(@first_res).must_be_instance_of Hotel::Reservation + + expect(@first_res.id).must_equal 1 + expect(@first_res.start_date).must_equal Date.parse('2019-06-05') + expect(@first_res.end_date).must_equal Date.parse('2019-06-08') + expect(@first_res.total_cost).must_equal (3*200.0) + end + + it "adds the new reservation to the list of reservations" do + expect(@sys.reservations.length).must_equal 1 + expect(@sys.reservations.first.id).must_equal @first_res.id + end + + it "adds the date range of the reservation for which nights are occupied to the list of dates" do + expect(@sys.hoteldates.length).must_equal 3 + expect(@sys.hoteldates[0].id).must_equal Date.parse('2019-06-05') + expect(@sys.hoteldates[1].id).must_equal Date.parse('2019-06-06') + expect(@sys.hoteldates[2].id).must_equal Date.parse('2019-06-07') + end + + it "does not create a new HotelDate object when one already exists" do + second_res = @sys.make_reservation(start_date: '2019-06-06', end_date: '2019-06-09') + + expect(@sys.reservations.length).must_equal 2 + expect(@sys.hoteldates.length).must_equal 4 + + expect(@sys.hoteldates[0].id).must_equal Date.parse('2019-06-05') + expect(@sys.hoteldates[1].id).must_equal Date.parse('2019-06-06') + expect(@sys.hoteldates[2].id).must_equal Date.parse('2019-06-07') + expect(@sys.hoteldates[3].id).must_equal Date.parse('2019-06-08') + + expect(@sys.hoteldates[0].occupied.length).must_equal 1 + expect(@sys.hoteldates[1].occupied.length).must_equal 2 + expect(@sys.hoteldates[2].occupied.length).must_equal 2 + expect(@sys.hoteldates[3].occupied.length).must_equal 1 + end + + end + +end From 15f2043b4c7f30237c0322c8d77f7a88bf75d817 Mon Sep 17 00:00:00 2001 From: Tiffany Chio Date: Wed, 4 Sep 2019 23:14:07 -0700 Subject: [PATCH 14/38] Added tests for methods of hoteldate. find available room works for system.rb --- lib/hoteldate.rb | 6 ++++- lib/system.rb | 55 ++++++++++++++++++++++++++++++++---------- test/hoteldate_test.rb | 51 +++++++++++++++++++++++++-------------- test/system_test.rb | 21 ++++++++++++++++ 4 files changed, 101 insertions(+), 32 deletions(-) diff --git a/lib/hoteldate.rb b/lib/hoteldate.rb index 6191c2a89..053ede760 100644 --- a/lib/hoteldate.rb +++ b/lib/hoteldate.rb @@ -14,12 +14,16 @@ def initialize(id) # occupied is a hash with key room_id and values of reservation instance # Devin said that for end dates, reservation should not show up def add_reservation(reservation) - @occupied[reservation.room.id] = reservation + @occupied[reservation.room] = reservation end # returns array of reservation objects def list_reservations return @occupied.values end + + def rooms_occupied + return @occupied.keys + end end end diff --git a/lib/system.rb b/lib/system.rb index 12df4ab5f..82fddc2c6 100644 --- a/lib/system.rb +++ b/lib/system.rb @@ -1,4 +1,5 @@ require 'date' +require 'pry' require_relative 'room' require_relative 'reservation' @@ -29,19 +30,18 @@ def generate_rooms return array_of_room_obj end - def list_all_rooms - # We need to create a method? Or can we just do System.rooms - end - # Assume date inputs come in string format - def make_reservation(start_date:, end_date:) - # Find a room with no overlap - room = find_room(20 - @reservations.length) - - # Create Reservation + def make_reservation(start_date:, end_date:) id = @reservations.length + 1 start_date = Date.parse(start_date) end_date = Date.parse(end_date) + + # Find a room with no overlap + room = find_available_room(start_date, end_date) + + raise ArgumentError, 'No rooms available' if room == nil + + # Create Reservation new_reservation = Hotel::Reservation.new(id: id, room: room, start_date: start_date, end_date: end_date) # Connect to Reservations List @@ -54,7 +54,29 @@ def make_reservation(start_date:, end_date:) return new_reservation end - def find_available_room + def find_available_room(start_date, end_date) + current_date = start_date.dup + all_occupied_rooms = [] + + until current_date == end_date + hotel_date = find_date(current_date) + + if hotel_date + all_occupied_rooms += hotel_date.rooms_occupied + end + + current_date += 1 + end + + all_occupied_rooms.uniq! + + @rooms.each do |room| + unless all_occupied_rooms.include? room + return room + end + end + + return nil end def add_to_dates(start_date, end_date, new_reservation) @@ -84,10 +106,17 @@ def find_date(date_obj) def list_reservations_for(date_string) hotel_date = find_date(Date.parse(date_string)) - return hotel_date.list_reservations + if hotel_date + return hotel_date.list_reservations + else + return nil + end end - def find_total_cost(reservation_id) - end end end + +# sys = Hotel::System.new +# first_res = sys.make_reservation(start_date: '2019-06-05', end_date: '2019-06-08') +# second_res = sys.make_reservation(start_date: '2019-06-06', end_date: '2019-06-09') +# third_res = sys.make_reservation(start_date: '2019-06-05', end_date: '2019-06-09') diff --git a/test/hoteldate_test.rb b/test/hoteldate_test.rb index 675165043..ad997e505 100644 --- a/test/hoteldate_test.rb +++ b/test/hoteldate_test.rb @@ -3,39 +3,54 @@ describe "HotelDate class" do describe "HotelDate instantiation" do before do - @date_1 = Hotel::HotelDate.new(Date.parse('2019-09-03')) + @hotel_date = Hotel::HotelDate.new(Date.parse('2019-09-03')) end it "is an instance of HotelDate" do - expect(@date_1).must_be_kind_of Hotel::HotelDate + expect(@hotel_date).must_be_kind_of Hotel::HotelDate end it "is set up for specific attributes and data types" do [:id, :occupied].each do |prop| - expect(@date_1).must_respond_to prop + expect(@hotel_date).must_respond_to prop end - expect(@date_1.id).must_be_instance_of Date - expect(@date_1.occupied).must_be_instance_of Hash + expect(@hotel_date.id).must_be_instance_of Date + expect(@hotel_date.occupied).must_be_instance_of Hash end end - # TIFF: I think you need to clean this up. Esp the set-up portion. - it "can add a add_reservation" do - id = Date.parse('2019-09-03') - @date_2 = Hotel::HotelDate.new(id) + describe "HotelDate instance methods" do + before do + @hotel_date = Hotel::HotelDate.new(Date.parse('2019-09-03')) + + @room_15 = Hotel::Room.new(15, 200.00) + start_time = Date.parse('2019-09-03') + end_time = Date.parse('2019-09-08') + @reservation = Hotel::Reservation.new(id: 101, room: @room_15, start_date: start_time, end_date: end_time) + + @hotel_date.add_reservation(@reservation) + end - room_15 = Hotel::Room.new(15, 200.00) - start_time = Date.parse('2019-09-03') - end_time = Date.parse('2019-09-08') - @reservation = Hotel::Reservation.new(id: 101, room: room_15, start_date: start_time, end_date: end_time) + it "can add a reservation" do + expect(@hotel_date.occupied.keys.first).must_be_instance_of Hotel::Room + expect(@hotel_date.occupied.values.first).must_be_instance_of Hotel::Reservation + expect(@hotel_date.occupied[@room_15]).must_equal @reservation + end - @date_2.add_reservation(@reservation) + it "returns an array of reservations under a given date" do + expect(@hotel_date.list_reservations).must_be_instance_of Array + + expect(@hotel_date.list_reservations.length).must_equal 1 + expect(@hotel_date.list_reservations[0]).must_equal @reservation + end - expect(@date_2.occupied.keys.first).must_be_instance_of Integer - expect(@date_2.occupied.values.first).must_be_instance_of Hotel::Reservation - expect(@date_2.occupied[15]).must_equal @reservation + it "returns an array of occupied rooms under a given date" do + expect(@hotel_date.rooms_occupied).must_be_instance_of Array + + expect(@hotel_date.rooms_occupied.length).must_equal 1 + expect(@hotel_date.rooms_occupied[0]).must_equal @room_15 + end end - end diff --git a/test/system_test.rb b/test/system_test.rb index 962d1b65b..33d0e1be9 100644 --- a/test/system_test.rb +++ b/test/system_test.rb @@ -79,7 +79,28 @@ expect(@sys.hoteldates[2].occupied.length).must_equal 2 expect(@sys.hoteldates[3].occupied.length).must_equal 1 end + end + + describe "list_reservations_for method" do + before do + @sys = Hotel::System.new + end + it "returns nil when no reservations have been made yet" do + res_list = @sys.list_reservations_for('2020-08-09') + + expect(res_list).must_equal nil + end + + it "returns an array of reservations for a given date" do + first_res = @sys.make_reservation(start_date: '2019-06-05', end_date: '2019-06-08') + second_res = @sys.make_reservation(start_date: '2019-06-06', end_date: '2019-06-09') + + res_list = @sys.list_reservations_for('2019-06-06') + expect(res_list.length).must_equal 2 + expect(res_list.include? first_res).must_equal true + expect(res_list.include? second_res).must_equal true + end end end From 86b7cdb0bf9fd035e01e16a187ca99a34d85cb87 Mon Sep 17 00:00:00 2001 From: Tiffany Chio Date: Thu, 5 Sep 2019 00:27:13 -0700 Subject: [PATCH 15/38] Completed all tests for wave 2. Everything passes. --- lib/system.rb | 16 +++--------- test/system_test.rb | 59 +++++++++++++++++++++++++++++++++++++++------ 2 files changed, 54 insertions(+), 21 deletions(-) diff --git a/lib/system.rb b/lib/system.rb index 82fddc2c6..872c2f3d4 100644 --- a/lib/system.rb +++ b/lib/system.rb @@ -37,7 +37,7 @@ def make_reservation(start_date:, end_date:) end_date = Date.parse(end_date) # Find a room with no overlap - room = find_available_room(start_date, end_date) + room = find_all_available_rooms(start_date, end_date)[0] raise ArgumentError, 'No rooms available' if room == nil @@ -54,7 +54,7 @@ def make_reservation(start_date:, end_date:) return new_reservation end - def find_available_room(start_date, end_date) + def find_all_available_rooms(start_date, end_date) current_date = start_date.dup all_occupied_rooms = [] @@ -70,13 +70,7 @@ def find_available_room(start_date, end_date) all_occupied_rooms.uniq! - @rooms.each do |room| - unless all_occupied_rooms.include? room - return room - end - end - - return nil + return @rooms - all_occupied_rooms end def add_to_dates(start_date, end_date, new_reservation) @@ -116,7 +110,3 @@ def list_reservations_for(date_string) end end -# sys = Hotel::System.new -# first_res = sys.make_reservation(start_date: '2019-06-05', end_date: '2019-06-08') -# second_res = sys.make_reservation(start_date: '2019-06-06', end_date: '2019-06-09') -# third_res = sys.make_reservation(start_date: '2019-06-05', end_date: '2019-06-09') diff --git a/test/system_test.rb b/test/system_test.rb index 33d0e1be9..8b866cc22 100644 --- a/test/system_test.rb +++ b/test/system_test.rb @@ -35,13 +35,6 @@ @first_res = @sys.make_reservation(start_date: '2019-06-05', end_date: '2019-06-08') end - it "can find the correct room given a room_id" do - room_obj = @sys.find_room(16) - - expect(room_obj).must_be_instance_of Hotel::Room - expect(room_obj.id).must_equal 16 - end - it "returns a new reservation with correct dates" do expect(@first_res).must_be_instance_of Hotel::Reservation @@ -79,6 +72,27 @@ expect(@sys.hoteldates[2].occupied.length).must_equal 2 expect(@sys.hoteldates[3].occupied.length).must_equal 1 end + + it "creates reservations using available rooms" do + expect(@first_res.room.id).must_equal 1 + + second_res = @sys.make_reservation(start_date: '2019-06-01', end_date: '2019-06-06') + expect(second_res.room.id).must_equal 2 + + third_res = @sys.make_reservation(start_date: '2019-06-05', end_date: '2019-06-10') + expect(third_res.room.id).must_equal 3 + + fourth_res = @sys.make_reservation(start_date: '2019-06-07', end_date: '2019-06-10') + expect(fourth_res.room.id).must_equal 2 + end + + it "returns an error when no rooms are available" do + 19.times do + @sys.make_reservation(start_date: '2019-06-05', end_date: '2019-06-08') + end + + expect{ @sys.make_reservation(start_date: '2019-06-05', end_date: '2019-06-08') }.must_raise ArgumentError + end end describe "list_reservations_for method" do @@ -89,7 +103,7 @@ it "returns nil when no reservations have been made yet" do res_list = @sys.list_reservations_for('2020-08-09') - expect(res_list).must_equal nil + expect(res_list).must_be_nil end it "returns an array of reservations for a given date" do @@ -103,4 +117,33 @@ end end + describe "FILL IN NAME" do + before do + @sys = Hotel::System.new + end + + it "can find the correct room given a room_id" do + room_obj = @sys.find_room(16) + + expect(room_obj).must_be_instance_of Hotel::Room + expect(room_obj.id).must_equal 16 + end + + it "finds all available rooms" do + 10.times do + @sys.make_reservation(start_date: '2019-06-05', end_date: '2019-06-08') + end + + start_date = Date.parse('2019-06-05') + end_date = Date.parse('2019-06-08') + + available_rooms = @sys.find_all_available_rooms(start_date, end_date) + + expect(available_rooms).must_be_instance_of Array + expect(available_rooms.length).must_equal 10 + expect(available_rooms.first.id).must_equal 11 + expect(available_rooms.last.id).must_equal 20 + end + end + end From df40439abd4cbf5a53c809b68c9673fb7ed3fd8d Mon Sep 17 00:00:00 2001 From: Tiffany Chio Date: Thu, 5 Sep 2019 14:53:30 -0700 Subject: [PATCH 16/38] Changed Reservation instantiation from total cost to cost) --- lib/hoteldate.rb | 2 +- lib/reservation.rb | 11 ++++++++--- lib/system.rb | 2 -- test/reservation_test.rb | 11 ++++++++--- test/system_test.rb | 3 ++- 5 files changed, 19 insertions(+), 10 deletions(-) diff --git a/lib/hoteldate.rb b/lib/hoteldate.rb index 053ede760..8df2e81cb 100644 --- a/lib/hoteldate.rb +++ b/lib/hoteldate.rb @@ -8,7 +8,7 @@ class HotelDate def initialize(id) @id = id # This is a date object - @occupied = {} # This is a hash, input was array + @occupied = {} end # occupied is a hash with key room_id and values of reservation instance diff --git a/lib/reservation.rb b/lib/reservation.rb index 094186a03..2e07d48d9 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -4,9 +4,9 @@ module Hotel class Reservation - attr_reader :id, :room, :start_date, :end_date, :date_range, :total_cost + attr_reader :id, :room, :start_date, :end_date, :cost - def initialize(id:, room:, start_date:, end_date:) + def initialize(id:, room:, start_date:, end_date:, cost: nil) if end_date <= start_date raise ArgumentError, 'End date must be after start date.' end @@ -15,7 +15,12 @@ def initialize(id:, room:, start_date:, end_date:) @room = room # this is a room object @start_date = start_date # this is already a date instance @end_date = end_date # this is already a date instance - @total_cost = (@end_date - @start_date) * @room.cost + @cost = cost + @cost ||= room.cost.to_f + end + + def find_total_cost + return (@end_date - @start_date) * cost end end diff --git a/lib/system.rb b/lib/system.rb index 872c2f3d4..6a00986c1 100644 --- a/lib/system.rb +++ b/lib/system.rb @@ -1,5 +1,4 @@ require 'date' -require 'pry' require_relative 'room' require_relative 'reservation' @@ -38,7 +37,6 @@ def make_reservation(start_date:, end_date:) # Find a room with no overlap room = find_all_available_rooms(start_date, end_date)[0] - raise ArgumentError, 'No rooms available' if room == nil # Create Reservation diff --git a/test/reservation_test.rb b/test/reservation_test.rb index 9fc441b17..f60e45b0e 100644 --- a/test/reservation_test.rb +++ b/test/reservation_test.rb @@ -15,7 +15,7 @@ end it "is set up for specific attributes and data types" do - [:id, :room, :start_date, :end_date, :total_cost].each do |prop| + [:id, :room, :start_date, :end_date, :cost].each do |prop| expect(@reservation).must_respond_to prop end @@ -23,11 +23,16 @@ expect(@reservation.room).must_be_instance_of Hotel::Room expect(@reservation.start_date).must_be_instance_of Date expect(@reservation.end_date).must_be_instance_of Date - expect(@reservation.total_cost).must_be_instance_of Float + expect(@reservation.cost).must_be_instance_of Float end + it "correctly assigns cost value" do + expect(@reservation.cost).must_be_close_to 200.0 + end + + # TIFF PUT THIS SOMEWHERE ELSE it "correctly calculates total_cost" do - expect(@reservation.total_cost).must_be_close_to 200.0*5 + expect(@reservation.find_total_cost).must_be_close_to 200.0*5 end end diff --git a/test/system_test.rb b/test/system_test.rb index 8b866cc22..6e5cbc934 100644 --- a/test/system_test.rb +++ b/test/system_test.rb @@ -41,7 +41,8 @@ expect(@first_res.id).must_equal 1 expect(@first_res.start_date).must_equal Date.parse('2019-06-05') expect(@first_res.end_date).must_equal Date.parse('2019-06-08') - expect(@first_res.total_cost).must_equal (3*200.0) + expect(@first_res.cost).must_equal 200.0 + expect(@first_res.find_total_cost).must_equal (3*200.0) end it "adds the new reservation to the list of reservations" do From 4e6f127a13918bd66c7ece72911e275aee418abf Mon Sep 17 00:00:00 2001 From: Tiffany Chio Date: Thu, 5 Sep 2019 16:28:41 -0700 Subject: [PATCH 17/38] moved room generation from system to room class. started creating hotelblocks. not done yet. --- lib/hotelblock.rb | 14 ++++++++++++++ lib/room.rb | 10 ++++++++++ lib/system.rb | 44 +++++++++++++++++++++++++------------------- test/system_test.rb | 11 +++++++++++ 4 files changed, 60 insertions(+), 19 deletions(-) diff --git a/lib/hotelblock.rb b/lib/hotelblock.rb index e69de29bb..a2c28c5bd 100644 --- a/lib/hotelblock.rb +++ b/lib/hotelblock.rb @@ -0,0 +1,14 @@ +require 'date' +require_relative 'reservation' + +module Hotel + class HotelBlock < Reservation + + # id here is the id of the hotel block + # shared by all rooms within same block + def initialize(id:, room:, start_date:, end_date:, cost: ) + super + end + + end +end diff --git a/lib/room.rb b/lib/room.rb index 730b1ff0d..3b88d083f 100644 --- a/lib/room.rb +++ b/lib/room.rb @@ -8,5 +8,15 @@ def initialize(id, cost) @cost = cost # Must be Float end + def self.generate_rooms + array_of_room_obj = [] + + (1..20).each do |i| + array_of_room_obj << Hotel::Room.new(i, 200.to_f) + end + + return array_of_room_obj + end + end end diff --git a/lib/system.rb b/lib/system.rb index 6a00986c1..fc99c32c4 100644 --- a/lib/system.rb +++ b/lib/system.rb @@ -10,23 +10,11 @@ class System attr_reader :rooms, :reservations, :hoteldates - NUM_ROOMS = 20 - COST_PER_NIGHT = 200 - def initialize - @rooms = generate_rooms + @rooms = Hotel::Room.generate_rooms @reservations = [] @hoteldates = [] - end - - def generate_rooms - array_of_room_obj = [] - - (1..NUM_ROOMS).each do |i| - array_of_room_obj << Hotel::Room.new(i, COST_PER_NIGHT.to_f) - end - - return array_of_room_obj + @hotelblocks = [] end # Assume date inputs come in string format @@ -59,9 +47,7 @@ def find_all_available_rooms(start_date, end_date) until current_date == end_date hotel_date = find_date(current_date) - if hotel_date - all_occupied_rooms += hotel_date.rooms_occupied - end + all_occupied_rooms += hotel_date.rooms_occupied if hotel_date current_date += 1 end @@ -76,14 +62,12 @@ def add_to_dates(start_date, end_date, new_reservation) until current_date == end_date date_obj = find_date(current_date) - if date_obj date_obj.add_reservation(new_reservation) else @hoteldates << Hotel::HotelDate.new(current_date) find_date(current_date).add_reservation(new_reservation) end - current_date += 1 end end @@ -105,6 +89,28 @@ def list_reservations_for(date_string) end end + # Everything is a string + # hb_rooms is an array of room ids, ints + def create_hotelblock(start_date:, end_date:, hb_rooms:, discount_rate:) + start_date = Date.parse(start_date) + end_date = Date.parse(end_date) + discount_rate = discount_rate.to_f + + raise ArgumentError, 'A block can contain a maximum of 5 rooms.' if hb_rooms.length > 5 + + available_rooms = find_all_available_rooms(start_date, end_date) + + # Does block contain a room that is not part of available rooms? + # If so raise an error. + if hb_rooms.map{ |room| available_rooms.include? room}.include? false + raise ArgumentError, 'A block can contain a maximum of 5 rooms.' if hb_rooms.length > 5 + end + + hb_rooms.each do |room| + + end + end + end end diff --git a/test/system_test.rb b/test/system_test.rb index 6e5cbc934..cc5b38aa6 100644 --- a/test/system_test.rb +++ b/test/system_test.rb @@ -147,4 +147,15 @@ end end + describe "HotelBlock Creation" do + before do + @sys = Hotel::System.new + end + + it "raises an error when trying to create a block of more than 5 rooms" do + end + + it "raises an error if one of the rooms is not available for the date range" do + end + end end From fa6ab8b13456ed4e15118b530de817e94554c3f1 Mon Sep 17 00:00:00 2001 From: Tiffany Chio Date: Thu, 5 Sep 2019 18:25:55 -0700 Subject: [PATCH 18/38] tested hotelblock.rb and it passes. Still need to tweak hotelblock creation in sys. --- lib/hotelblock.rb | 8 ++++- lib/reservation.rb | 2 +- lib/system.rb | 16 ++++++++- test/hotelblock_test.rb | 75 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 98 insertions(+), 3 deletions(-) create mode 100644 test/hotelblock_test.rb diff --git a/lib/hotelblock.rb b/lib/hotelblock.rb index a2c28c5bd..7578c328b 100644 --- a/lib/hotelblock.rb +++ b/lib/hotelblock.rb @@ -4,11 +4,17 @@ module Hotel class HotelBlock < Reservation + attr_reader :status + # id here is the id of the hotel block # shared by all rooms within same block - def initialize(id:, room:, start_date:, end_date:, cost: ) + def initialize(id:, room:, start_date:, end_date:, cost:) super + @status = :AVAILABLE end + def change_status + @status = :UNAVAILABLE + end end end diff --git a/lib/reservation.rb b/lib/reservation.rb index 2e07d48d9..b40480cf9 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -20,7 +20,7 @@ def initialize(id:, room:, start_date:, end_date:, cost: nil) end def find_total_cost - return (@end_date - @start_date) * cost + return (@end_date - @start_date) * @cost.to_f end end diff --git a/lib/system.rb b/lib/system.rb index fc99c32c4..6c01cd429 100644 --- a/lib/system.rb +++ b/lib/system.rb @@ -36,6 +36,7 @@ def make_reservation(start_date:, end_date:) # Create Date or Add to it, add to Dates list add_to_dates(start_date, end_date, new_reservation) + #TIFF GET RID OF THIS AND CORRESPONDING TEST # return new_reservation return new_reservation end @@ -90,11 +91,12 @@ def list_reservations_for(date_string) end # Everything is a string - # hb_rooms is an array of room ids, ints + # hb_rooms is an array of room_ids def create_hotelblock(start_date:, end_date:, hb_rooms:, discount_rate:) start_date = Date.parse(start_date) end_date = Date.parse(end_date) discount_rate = discount_rate.to_f + hb_rooms.map! { |room_id| find_room(room_id) } raise ArgumentError, 'A block can contain a maximum of 5 rooms.' if hb_rooms.length > 5 @@ -106,11 +108,23 @@ def create_hotelblock(start_date:, end_date:, hb_rooms:, discount_rate:) raise ArgumentError, 'A block can contain a maximum of 5 rooms.' if hb_rooms.length > 5 end + hb_id = @hotelblocks.length + hb_rooms.each do |room| + new_hotel_block = Hotel::HotelBlock.new(id: hb_id, room: room, start_date: start_date, end_date: end_date, cost:discount_rate) + + @hotelblocks << new_hotel_block + add_to_dates(start_date, end_date, new_hotel_block) end end + def reserve_from_block(block_id, room_id) + # change status + # make reservation with pre-determined arg + # add res to @reservations + end + end end diff --git a/test/hotelblock_test.rb b/test/hotelblock_test.rb new file mode 100644 index 000000000..208f40495 --- /dev/null +++ b/test/hotelblock_test.rb @@ -0,0 +1,75 @@ +require_relative 'test_helper' + +describe "HotelBlock class" do + describe "HotelBlock instantiation" do + before do + room_15 = Hotel::Room.new(15, 200.00) + start_time = Date.parse('2019-09-03') + end_time = Date.parse('2019-09-08') + + @hotelblock = Hotel::HotelBlock.new(id: 101, room: room_15, start_date: start_time, end_date: end_time, cost: 100) + end + + it "is an instance of HotelBlock" do + expect(@hotelblock).must_be_kind_of Hotel::HotelBlock + end + + it "is set up for specific attributes and data types" do + [:id, :room, :start_date, :end_date, :cost, :status].each do |prop| + expect(@hotelblock).must_respond_to prop + end + + expect(@hotelblock.id).must_be_instance_of Integer + expect(@hotelblock.room).must_be_instance_of Hotel::Room + expect(@hotelblock.start_date).must_be_instance_of Date + expect(@hotelblock.end_date).must_be_instance_of Date + expect(@hotelblock.status).must_be_instance_of Symbol + end + + it "correctly assigns cost value" do + expect(@hotelblock.cost).must_be_close_to 100.0 + end + + # TIFF PUT THIS SOMEWHERE ELSE + it "correctly calculates total_cost" do + expect(@hotelblock.find_total_cost).must_be_close_to 100.0*5 + end + end + + describe "the status of hotelblock" do + before do + room_15 = Hotel::Room.new(15, 200.00) + start_time = Date.parse('2019-09-03') + end_time = Date.parse('2019-09-08') + + @hotelblock = Hotel::HotelBlock.new(id: 101, room: room_15, start_date: start_time, end_date: end_time, cost: 100) + end + + it "initializes hotelblocks with :AVAILABLE" do + expect(@hotelblock.status).must_equal :AVAILABLE + end + + it "can be changed to :UNVAILABLE with the change_status method" do + @hotelblock.change_status + expect(@hotelblock.status).must_equal :UNAVAILABLE + end + end + + describe "raises an exception when an invalid date range is provided" do + it "raises an error if end date is before start date" do + room_15 = Hotel::Room.new(15, 200.00) + start_time = Date.parse('2019-09-03') + end_time = Date.parse('2019-09-01') + + expect{Hotel::HotelBlock.new(id: 101, room: room_15, start_date: start_time, end_date: end_time, cost: 100)}.must_raise ArgumentError + end + + it "raises an error if end date is on same day as start date" do + room_15 = Hotel::Room.new(15, 200.00) + start_time = Date.parse('2019-09-03') + end_time = Date.parse('2019-09-03') + + expect{Hotel::HotelBlock.new(id: 101, room: room_15, start_date: start_time, end_date: end_time, cost: 100)}.must_raise ArgumentError + end + end +end From 48918bfb136b46dfb64ddbdfebfe1df6cb73fb0a Mon Sep 17 00:00:00 2001 From: Tiffany Chio Date: Thu, 5 Sep 2019 19:19:04 -0700 Subject: [PATCH 19/38] tested hotel block creation, appears to be working fine. --- lib/hotelblock.rb | 1 + lib/system.rb | 14 ++++++++++---- test/system_test.rb | 31 ++++++++++++++++++++++++++++--- 3 files changed, 39 insertions(+), 7 deletions(-) diff --git a/lib/hotelblock.rb b/lib/hotelblock.rb index 7578c328b..fd16443dc 100644 --- a/lib/hotelblock.rb +++ b/lib/hotelblock.rb @@ -18,3 +18,4 @@ def change_status end end end + diff --git a/lib/system.rb b/lib/system.rb index 6c01cd429..0b7c9aae7 100644 --- a/lib/system.rb +++ b/lib/system.rb @@ -8,7 +8,7 @@ module Hotel class System - attr_reader :rooms, :reservations, :hoteldates + attr_reader :rooms, :reservations, :hoteldates, :hotelblocks def initialize @rooms = Hotel::Room.generate_rooms @@ -90,22 +90,26 @@ def list_reservations_for(date_string) end end - # Everything is a string + # Dates are strings # hb_rooms is an array of room_ids + # Discount rate can be whatever number def create_hotelblock(start_date:, end_date:, hb_rooms:, discount_rate:) start_date = Date.parse(start_date) end_date = Date.parse(end_date) discount_rate = discount_rate.to_f hb_rooms.map! { |room_id| find_room(room_id) } - raise ArgumentError, 'A block can contain a maximum of 5 rooms.' if hb_rooms.length > 5 + if hb_rooms.length > 5 + raise ArgumentError, 'A block can contain a maximum of 5 rooms.' + end available_rooms = find_all_available_rooms(start_date, end_date) # Does block contain a room that is not part of available rooms? # If so raise an error. + # Hey TIFF make this part longer to nclude which room if hb_rooms.map{ |room| available_rooms.include? room}.include? false - raise ArgumentError, 'A block can contain a maximum of 5 rooms.' if hb_rooms.length > 5 + raise ArgumentError, 'Block contains room already booked.' end hb_id = @hotelblocks.length @@ -128,3 +132,5 @@ def reserve_from_block(block_id, room_id) end end + + diff --git a/test/system_test.rb b/test/system_test.rb index cc5b38aa6..d0fc2bbb0 100644 --- a/test/system_test.rb +++ b/test/system_test.rb @@ -101,7 +101,7 @@ @sys = Hotel::System.new end - it "returns nil when no reservations have been made yet" do + it "returns nil when no reservations have been made for given date" do res_list = @sys.list_reservations_for('2020-08-09') expect(res_list).must_be_nil @@ -150,12 +150,37 @@ describe "HotelBlock Creation" do before do @sys = Hotel::System.new + @sys.create_hotelblock(start_date: '2019-09-02', end_date: '2019-09-05', hb_rooms: [1,2,3,4], discount_rate: 165) end - it "raises an error when trying to create a block of more than 5 rooms" do + it "adds to the hotelblocks list" do + expect(@sys.hotelblocks.length).must_equal 4 + expect(@sys.hotelblocks.first).must_be_instance_of Hotel::HotelBlock + end + + it "updates the hoteldates list" do + expect(@sys.hoteldates.length).must_equal 3 + + expect(@sys.hoteldates.first).must_be_instance_of Hotel::HotelDate + + expect(@sys.hoteldates[0].id).must_equal Date.parse('2019-09-02') + expect(@sys.hoteldates[1].id).must_equal Date.parse('2019-09-03') + expect(@sys.hoteldates[2].id).must_equal Date.parse('2019-09-04') + end + + end + + describe "Raising Errors for HotelBlock Creation" do + before do + @sys = Hotel::System.new end - it "raises an error if one of the rooms is not available for the date range" do + it "raises an error when trying to create a block of more than 5 rooms" do + expect{@sys.create_hotelblock(start_date: '2019-09-02', end_date: '2019-09-05', hb_rooms: [1,2,3,4,5,6], discount_rate: 165)}.must_raise ArgumentError end + + # it "raises an error if one of the rooms is not available for the date range" do + # expect{@sys.create_hotelblock(start_date: '2019-09-02', end_date: '2019-09-05', hb_rooms: [1,2,3], discount_rate: 165)}.must_raise ArgumentError + # end end end From 67dbe111fd3d7afac488d39b82e1a85f4e7877f0 Mon Sep 17 00:00:00 2001 From: Tiffany Chio Date: Thu, 5 Sep 2019 21:12:14 -0700 Subject: [PATCH 20/38] last working code before we change hbs list over to hash --- lib/system.rb | 4 ++-- test/system_test.rb | 25 ++++++++++++++++--------- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/lib/system.rb b/lib/system.rb index 0b7c9aae7..3b742bc3b 100644 --- a/lib/system.rb +++ b/lib/system.rb @@ -14,7 +14,7 @@ def initialize @rooms = Hotel::Room.generate_rooms @reservations = [] @hoteldates = [] - @hotelblocks = [] + @hotelblocks = {} end # Assume date inputs come in string format @@ -107,7 +107,7 @@ def create_hotelblock(start_date:, end_date:, hb_rooms:, discount_rate:) # Does block contain a room that is not part of available rooms? # If so raise an error. - # Hey TIFF make this part longer to nclude which room + # Hey TIFF make this part longer to include which room if hb_rooms.map{ |room| available_rooms.include? room}.include? false raise ArgumentError, 'Block contains room already booked.' end diff --git a/test/system_test.rb b/test/system_test.rb index d0fc2bbb0..eae7ac7c0 100644 --- a/test/system_test.rb +++ b/test/system_test.rb @@ -168,19 +168,26 @@ expect(@sys.hoteldates[2].id).must_equal Date.parse('2019-09-04') end - end - - describe "Raising Errors for HotelBlock Creation" do - before do - @sys = Hotel::System.new + it "excludes the room from reservation during the same date range" do + new_reservation = @sys.make_reservation(start_date: '2019-09-04', end_date: '2019-09-07') + expect(new_reservation.room.id).must_equal 5 + + 15.times do + @sys.make_reservation(start_date: '2019-09-04', end_date: '2019-09-07') + end + + expect{@sys.make_reservation(start_date: '2019-09-04', end_date: '2019-09-07')}.must_raise ArgumentError + end + + it "excludes the room from be added to hotel block during the same date range" do + expect{@sys.create_hotelblock(start_date: '2019-09-04', end_date: '2019-09-07', hb_rooms: [2,5,6,7], discount_rate: 165)}.must_raise ArgumentError end it "raises an error when trying to create a block of more than 5 rooms" do expect{@sys.create_hotelblock(start_date: '2019-09-02', end_date: '2019-09-05', hb_rooms: [1,2,3,4,5,6], discount_rate: 165)}.must_raise ArgumentError end - - # it "raises an error if one of the rooms is not available for the date range" do - # expect{@sys.create_hotelblock(start_date: '2019-09-02', end_date: '2019-09-05', hb_rooms: [1,2,3], discount_rate: 165)}.must_raise ArgumentError - # end + end + + describe "" do end end From 76486fef489c255bac1ea8b231c22959cfd849ad Mon Sep 17 00:00:00 2001 From: Tiffany Chio Date: Thu, 5 Sep 2019 21:26:25 -0700 Subject: [PATCH 21/38] changed hotel blocks list over to hash. tests are still passing. --- lib/system.rb | 6 ++++-- test/system_test.rb | 10 +++++++--- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/lib/system.rb b/lib/system.rb index 3b742bc3b..525a87696 100644 --- a/lib/system.rb +++ b/lib/system.rb @@ -112,12 +112,14 @@ def create_hotelblock(start_date:, end_date:, hb_rooms:, discount_rate:) raise ArgumentError, 'Block contains room already booked.' end - hb_id = @hotelblocks.length + hb_id = @hotelblocks.length + 1 + @hotelblocks[hb_id] = [] + hb_rooms.each do |room| new_hotel_block = Hotel::HotelBlock.new(id: hb_id, room: room, start_date: start_date, end_date: end_date, cost:discount_rate) - @hotelblocks << new_hotel_block + @hotelblocks[hb_id] << new_hotel_block add_to_dates(start_date, end_date, new_hotel_block) end diff --git a/test/system_test.rb b/test/system_test.rb index eae7ac7c0..ab9d9acb4 100644 --- a/test/system_test.rb +++ b/test/system_test.rb @@ -11,7 +11,7 @@ end it "is set up for specific attributes and data types" do - [:rooms, :reservations, :hoteldates].each do |prop| + [:rooms, :reservations, :hoteldates, :hotelblocks].each do |prop| expect(@sys).must_respond_to prop end @@ -20,6 +20,7 @@ expect(@sys.rooms.last).must_be_instance_of Hotel::Room expect(@sys.reservations).must_be_instance_of Array expect(@sys.hoteldates).must_be_instance_of Array + expect(@sys.hotelblocks).must_be_instance_of Hash end it "executes generate rooms method correctly" do @@ -154,8 +155,11 @@ end it "adds to the hotelblocks list" do - expect(@sys.hotelblocks.length).must_equal 4 - expect(@sys.hotelblocks.first).must_be_instance_of Hotel::HotelBlock + expect(@sys.hotelblocks.length).must_equal 1 + expect(@sys.hotelblocks.keys.first).must_equal 1 + expect(@sys.hotelblocks.values.first).must_be_instance_of Array + expect(@sys.hotelblocks[1].first).must_be_instance_of Hotel::HotelBlock + expect(@sys.hotelblocks[1][2].room.id).must_equal 3 end it "updates the hoteldates list" do From bafa5edae7bcaef5771b8503e969f89de2c361d0 Mon Sep 17 00:00:00 2001 From: Tiffany Chio Date: Fri, 6 Sep 2019 14:57:56 -0700 Subject: [PATCH 22/38] Completed wave 3. All tests pass --- lib/reservation.rb | 2 +- lib/system.rb | 31 +++++++++++++++++++++++++++--- test/system_test.rb | 46 ++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 74 insertions(+), 5 deletions(-) diff --git a/lib/reservation.rb b/lib/reservation.rb index b40480cf9..b276c8e9d 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -16,7 +16,7 @@ def initialize(id:, room:, start_date:, end_date:, cost: nil) @start_date = start_date # this is already a date instance @end_date = end_date # this is already a date instance @cost = cost - @cost ||= room.cost.to_f + @cost ||= room.cost end def find_total_cost diff --git a/lib/system.rb b/lib/system.rb index 525a87696..1d5ec0924 100644 --- a/lib/system.rb +++ b/lib/system.rb @@ -115,7 +115,6 @@ def create_hotelblock(start_date:, end_date:, hb_rooms:, discount_rate:) hb_id = @hotelblocks.length + 1 @hotelblocks[hb_id] = [] - hb_rooms.each do |room| new_hotel_block = Hotel::HotelBlock.new(id: hb_id, room: room, start_date: start_date, end_date: end_date, cost:discount_rate) @@ -125,10 +124,36 @@ def create_hotelblock(start_date:, end_date:, hb_rooms:, discount_rate:) end end - def reserve_from_block(block_id, room_id) + def find_open_rooms_from_block(hb_id) + hotel_blocks = @hotelblocks[hb_id] + open_rooms = hotel_blocks.select { |hotel_block| hotel_block.status == :AVAILABLE} + + open_rooms.map! { |hotel_block| hotel_block.room.id} + + return open_rooms + end + + def reserve_from_block(hb_id, room_id) + # check availability + unless find_open_rooms_from_block(hb_id).include? room_id + raise ArgumentError, 'The room you are trying to book is not available.' + end + # change status + hotel_block = @hotelblocks[hb_id].find {|hb| hb.room.id == room_id} + hotel_block.change_status + # make reservation with pre-determined arg - # add res to @reservations + id = @reservations.length + 1 + room = find_room(room_id) + start_date = hotel_block.start_date + end_date = hotel_block.end_date + cost = hotel_block.cost + + new_reservation = Hotel::Reservation.new(id: id, room: room, start_date: start_date, end_date: end_date, cost: cost) + + # Connect to Reservations List + @reservations << new_reservation end end diff --git a/test/system_test.rb b/test/system_test.rb index ab9d9acb4..6bea9e97d 100644 --- a/test/system_test.rb +++ b/test/system_test.rb @@ -192,6 +192,50 @@ end end - describe "" do + describe "Reserving from a HotelBlock" do + before do + @sys = Hotel::System.new + hotel_block = @sys.create_hotelblock(start_date: '2019-09-02', end_date: '2019-09-05', hb_rooms: [7,8,9,10], discount_rate: 165) + + @sys.reserve_from_block(1, 8) + end + + it "can make a reservation from a hotel block" do + expect(@sys.find_open_rooms_from_block(1)).must_equal [7,9,10] + expect(@sys.reservations[0].room.id).must_equal 8 + end + + it "makes reservations for the duration of the hotel block" do + expect(@sys.reservations[0].start_date).must_equal Date.parse('2019-09-02') + expect(@sys.reservations[0].end_date).must_equal Date.parse('2019-09-05') + end + + it "returns all available rooms in a HotelBlock" do + available_rooms = @sys.find_open_rooms_from_block(1) + + expect(available_rooms.length).must_equal 3 + expect(available_rooms).must_equal [7,9,10] + end + + it "raises an error when reserving a room that's already reserved" do + expect{ @sys.reserve_from_block(1, 8) }.must_raise ArgumentError + end + + it "adds the reservation made from the HotelBlock into the Reservations list" do + @sys.reserve_from_block(1, 10) + + expect(@sys.reservations[1].room.id).must_equal 10 + expect(@sys.reservations[1]).must_be_instance_of Hotel::Reservation + end + + it "returns nil if no rooms are available" do + @sys.reserve_from_block(1, 10) + @sys.reserve_from_block(1, 7) + @sys.reserve_from_block(1, 9) + + available_rooms = @sys.find_open_rooms_from_block(1) + + expect(available_rooms).must_equal [] + end end end From b9b6026c27b1fce6724f4ecf4f819a947c39f24d Mon Sep 17 00:00:00 2001 From: Tiffany Chio Date: Fri, 6 Sep 2019 16:25:48 -0700 Subject: [PATCH 23/38] changed hoteldate logic to reflect both reservations and hotel blocks. --- lib/hoteldate.rb | 16 ++++++++++------ lib/room.rb | 6 +++--- lib/system.rb | 4 ++-- test/hoteldate_test.rb | 2 +- 4 files changed, 16 insertions(+), 12 deletions(-) diff --git a/lib/hoteldate.rb b/lib/hoteldate.rb index 8df2e81cb..894f50310 100644 --- a/lib/hoteldate.rb +++ b/lib/hoteldate.rb @@ -7,21 +7,25 @@ class HotelDate attr_reader :id, :occupied def initialize(id) - @id = id # This is a date object + # id is a date object + @id = id + # occupied is a hash with key room_id and values of reservation instance @occupied = {} end - # occupied is a hash with key room_id and values of reservation instance - # Devin said that for end dates, reservation should not show up - def add_reservation(reservation) + # Adds either reservation or hotel blocks to @occupied hash. + # Below the parameter is called "reservation" but + # logic also works for hotel blocks. + def add_occupancy(reservation) @occupied[reservation.room] = reservation end - # returns array of reservation objects + # Returns only reservations and not hotel blocks as per specs. def list_reservations - return @occupied.values + return @occupied.values.select { |value| value.class == Hotel::Reservation } end + # Returns all occupied rooms, either reserved or blocked. def rooms_occupied return @occupied.keys end diff --git a/lib/room.rb b/lib/room.rb index 3b88d083f..d2827477b 100644 --- a/lib/room.rb +++ b/lib/room.rb @@ -4,15 +4,15 @@ class Room attr_reader :id, :cost def initialize(id, cost) - @id = id # Must be Integer - @cost = cost # Must be Float + @id = id + @cost = cost end def self.generate_rooms array_of_room_obj = [] (1..20).each do |i| - array_of_room_obj << Hotel::Room.new(i, 200.to_f) + array_of_room_obj << Hotel::Room.new(i, 200.0) end return array_of_room_obj diff --git a/lib/system.rb b/lib/system.rb index 1d5ec0924..36038f9ce 100644 --- a/lib/system.rb +++ b/lib/system.rb @@ -64,10 +64,10 @@ def add_to_dates(start_date, end_date, new_reservation) until current_date == end_date date_obj = find_date(current_date) if date_obj - date_obj.add_reservation(new_reservation) + date_obj.add_occupancy(new_reservation) else @hoteldates << Hotel::HotelDate.new(current_date) - find_date(current_date).add_reservation(new_reservation) + find_date(current_date).add_occupancy(new_reservation) end current_date += 1 end diff --git a/test/hoteldate_test.rb b/test/hoteldate_test.rb index ad997e505..e0a9f4170 100644 --- a/test/hoteldate_test.rb +++ b/test/hoteldate_test.rb @@ -30,7 +30,7 @@ end_time = Date.parse('2019-09-08') @reservation = Hotel::Reservation.new(id: 101, room: @room_15, start_date: start_time, end_date: end_time) - @hotel_date.add_reservation(@reservation) + @hotel_date.add_occupancy(@reservation) end it "can add a reservation" do From 1c841a94343e89b9aff393d645cbfe0220b8a81a Mon Sep 17 00:00:00 2001 From: Tiffany Chio Date: Fri, 6 Sep 2019 16:39:29 -0700 Subject: [PATCH 24/38] save before changing hoteldate to date --- lib/hoteldate.rb | 1 + lib/reservation.rb | 7 +++---- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/hoteldate.rb b/lib/hoteldate.rb index 894f50310..c2d0b8eb5 100644 --- a/lib/hoteldate.rb +++ b/lib/hoteldate.rb @@ -29,5 +29,6 @@ def list_reservations def rooms_occupied return @occupied.keys end + end end diff --git a/lib/reservation.rb b/lib/reservation.rb index b276c8e9d..a5fe3d789 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -6,17 +6,16 @@ class Reservation attr_reader :id, :room, :start_date, :end_date, :cost - def initialize(id:, room:, start_date:, end_date:, cost: nil) + def initialize(id:, room:, start_date:, end_date:, cost: room.cost) if end_date <= start_date raise ArgumentError, 'End date must be after start date.' end @id = id @room = room # this is a room object - @start_date = start_date # this is already a date instance - @end_date = end_date # this is already a date instance + @start_date = start_date # this is a date instance + @end_date = end_date # this is a date instance @cost = cost - @cost ||= room.cost end def find_total_cost From dc88859af6593b211ef4c95188485bc4b4482528 Mon Sep 17 00:00:00 2001 From: Tiffany Chio Date: Fri, 6 Sep 2019 22:27:26 -0700 Subject: [PATCH 25/38] refactoring system.rb changed all the make reservations from string inputs to date object inputs. --- lib/system.rb | 56 ++++++++++++++++++++++----------------------- test/system_test.rb | 26 ++++++++++----------- 2 files changed, 40 insertions(+), 42 deletions(-) diff --git a/lib/system.rb b/lib/system.rb index 36038f9ce..ea1a3e562 100644 --- a/lib/system.rb +++ b/lib/system.rb @@ -14,14 +14,37 @@ def initialize @rooms = Hotel::Room.generate_rooms @reservations = [] @hoteldates = [] + + # keys will be hotel block ids (shared by all rooms within block) + # values will be arrays of hotel block objects within same block @hotelblocks = {} end - # Assume date inputs come in string format - def make_reservation(start_date:, end_date:) + def find_room(room_id) + return @rooms.find { |room| room.id == room_id } + end + + def find_date(date_obj) + return @hoteldates.find { |hotel_date| hotel_date.id == date_obj } + end + + def find_all_available_rooms(start_date, end_date) + current_date = start_date.dup + available_rooms = @rooms.dup + + until current_date == end_date + hotel_date = find_date(current_date) + available_rooms -= hotel_date.rooms_occupied if hotel_date + current_date += 1 + end + + return available_rooms + end + + # TIFF YOU CHANGED FROM KEYWORD TO NON-KEYWORD + # FROM STRING TO DATE OBJ + def make_reservation(start_date, end_date) id = @reservations.length + 1 - start_date = Date.parse(start_date) - end_date = Date.parse(end_date) # Find a room with no overlap room = find_all_available_rooms(start_date, end_date)[0] @@ -41,23 +64,6 @@ def make_reservation(start_date:, end_date:) return new_reservation end - def find_all_available_rooms(start_date, end_date) - current_date = start_date.dup - all_occupied_rooms = [] - - until current_date == end_date - hotel_date = find_date(current_date) - - all_occupied_rooms += hotel_date.rooms_occupied if hotel_date - - current_date += 1 - end - - all_occupied_rooms.uniq! - - return @rooms - all_occupied_rooms - end - def add_to_dates(start_date, end_date, new_reservation) current_date = start_date.dup @@ -73,14 +79,6 @@ def add_to_dates(start_date, end_date, new_reservation) end end - def find_room(room_id) - return @rooms.find { |room| room.id == room_id } - end - - def find_date(date_obj) - return @hoteldates.find { |hotel_date| hotel_date.id == date_obj } - end - def list_reservations_for(date_string) hotel_date = find_date(Date.parse(date_string)) if hotel_date diff --git a/test/system_test.rb b/test/system_test.rb index 6bea9e97d..7b16bd331 100644 --- a/test/system_test.rb +++ b/test/system_test.rb @@ -33,7 +33,7 @@ describe "making a room reservation" do before do @sys = Hotel::System.new - @first_res = @sys.make_reservation(start_date: '2019-06-05', end_date: '2019-06-08') + @first_res = @sys.make_reservation(Date.parse('2019-06-05'), Date.parse('2019-06-08')) end it "returns a new reservation with correct dates" do @@ -59,7 +59,7 @@ end it "does not create a new HotelDate object when one already exists" do - second_res = @sys.make_reservation(start_date: '2019-06-06', end_date: '2019-06-09') + second_res = @sys.make_reservation(Date.parse('2019-06-06'), Date.parse('2019-06-09')) expect(@sys.reservations.length).must_equal 2 expect(@sys.hoteldates.length).must_equal 4 @@ -78,22 +78,22 @@ it "creates reservations using available rooms" do expect(@first_res.room.id).must_equal 1 - second_res = @sys.make_reservation(start_date: '2019-06-01', end_date: '2019-06-06') + second_res = @sys.make_reservation(Date.parse('2019-06-01'), Date.parse('2019-06-06')) expect(second_res.room.id).must_equal 2 - third_res = @sys.make_reservation(start_date: '2019-06-05', end_date: '2019-06-10') + third_res = @sys.make_reservation(Date.parse('2019-06-05'), Date.parse('2019-06-10')) expect(third_res.room.id).must_equal 3 - fourth_res = @sys.make_reservation(start_date: '2019-06-07', end_date: '2019-06-10') + fourth_res = @sys.make_reservation(Date.parse('2019-06-07'), Date.parse('2019-06-10')) expect(fourth_res.room.id).must_equal 2 end it "returns an error when no rooms are available" do 19.times do - @sys.make_reservation(start_date: '2019-06-05', end_date: '2019-06-08') + @sys.make_reservation(Date.parse('2019-06-05'), Date.parse('2019-06-08')) end - expect{ @sys.make_reservation(start_date: '2019-06-05', end_date: '2019-06-08') }.must_raise ArgumentError + expect{ @sys.make_reservation(Date.parse('2019-06-05'), Date.parse('2019-06-08')) }.must_raise ArgumentError end end @@ -109,8 +109,8 @@ end it "returns an array of reservations for a given date" do - first_res = @sys.make_reservation(start_date: '2019-06-05', end_date: '2019-06-08') - second_res = @sys.make_reservation(start_date: '2019-06-06', end_date: '2019-06-09') + first_res = @sys.make_reservation(Date.parse('2019-06-05'), Date.parse('2019-06-08')) + second_res = @sys.make_reservation(Date.parse('2019-06-06'), Date.parse('2019-06-09')) res_list = @sys.list_reservations_for('2019-06-06') expect(res_list.length).must_equal 2 @@ -133,7 +133,7 @@ it "finds all available rooms" do 10.times do - @sys.make_reservation(start_date: '2019-06-05', end_date: '2019-06-08') + @sys.make_reservation(Date.parse('2019-06-05'), Date.parse('2019-06-08')) end start_date = Date.parse('2019-06-05') @@ -173,14 +173,14 @@ end it "excludes the room from reservation during the same date range" do - new_reservation = @sys.make_reservation(start_date: '2019-09-04', end_date: '2019-09-07') + new_reservation = @sys.make_reservation(Date.parse('2019-09-04'), Date.parse('2019-09-07')) expect(new_reservation.room.id).must_equal 5 15.times do - @sys.make_reservation(start_date: '2019-09-04', end_date: '2019-09-07') + @sys.make_reservation(Date.parse('2019-09-04'), Date.parse('2019-09-07')) end - expect{@sys.make_reservation(start_date: '2019-09-04', end_date: '2019-09-07')}.must_raise ArgumentError + expect{@sys.make_reservation(Date.parse('2019-09-04'), Date.parse('2019-09-07'))}.must_raise ArgumentError end it "excludes the room from be added to hotel block during the same date range" do From 746d394c57c63bdcfdb8ff2e03da2ee2d003472a Mon Sep 17 00:00:00 2001 From: Tiffany Chio Date: Fri, 6 Sep 2019 23:10:38 -0700 Subject: [PATCH 26/38] refactoring system.rb about to change list reservations over to objects --- lib/system.rb | 29 ++++++++++++++--------------- test/system_test.rb | 4 ++-- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/lib/system.rb b/lib/system.rb index ea1a3e562..ace3d8131 100644 --- a/lib/system.rb +++ b/lib/system.rb @@ -41,46 +41,44 @@ def find_all_available_rooms(start_date, end_date) return available_rooms end - # TIFF YOU CHANGED FROM KEYWORD TO NON-KEYWORD - # FROM STRING TO DATE OBJ def make_reservation(start_date, end_date) id = @reservations.length + 1 - # Find a room with no overlap + # assign the first available room to the reservation room = find_all_available_rooms(start_date, end_date)[0] - raise ArgumentError, 'No rooms available' if room == nil + raise FullOccupancyError.new('No rooms available for the date range.') if room == nil - # Create Reservation new_reservation = Hotel::Reservation.new(id: id, room: room, start_date: start_date, end_date: end_date) - # Connect to Reservations List @reservations << new_reservation - - # Create Date or Add to it, add to Dates list add_to_dates(start_date, end_date, new_reservation) - #TIFF GET RID OF THIS AND CORRESPONDING TEST - # return new_reservation return new_reservation end - def add_to_dates(start_date, end_date, new_reservation) + # new_occupancy refers to reservation or hotel block to be added to dates + def add_to_dates(start_date, end_date, new_occupancy) current_date = start_date.dup until current_date == end_date date_obj = find_date(current_date) + if date_obj - date_obj.add_occupancy(new_reservation) + date_obj.add_occupancy(new_occupancy) else - @hoteldates << Hotel::HotelDate.new(current_date) - find_date(current_date).add_occupancy(new_reservation) + new_date_obj = Hotel::HotelDate.new(current_date) + @hoteldates << new_date_obj + new_date_obj.add_occupancy(new_occupancy) end + current_date += 1 end end + # TIFF CHANGED FROM STRING TO OBJ def list_reservations_for(date_string) hotel_date = find_date(Date.parse(date_string)) + if hotel_date return hotel_date.list_reservations else @@ -157,5 +155,6 @@ def reserve_from_block(hb_id, room_id) end end - +class FullOccupancyError < StandardError +end diff --git a/test/system_test.rb b/test/system_test.rb index 7b16bd331..6ab86577c 100644 --- a/test/system_test.rb +++ b/test/system_test.rb @@ -93,7 +93,7 @@ @sys.make_reservation(Date.parse('2019-06-05'), Date.parse('2019-06-08')) end - expect{ @sys.make_reservation(Date.parse('2019-06-05'), Date.parse('2019-06-08')) }.must_raise ArgumentError + expect{ @sys.make_reservation(Date.parse('2019-06-05'), Date.parse('2019-06-08')) }.must_raise FullOccupancyError end end @@ -180,7 +180,7 @@ @sys.make_reservation(Date.parse('2019-09-04'), Date.parse('2019-09-07')) end - expect{@sys.make_reservation(Date.parse('2019-09-04'), Date.parse('2019-09-07'))}.must_raise ArgumentError + expect{@sys.make_reservation(Date.parse('2019-09-04'), Date.parse('2019-09-07'))}.must_raise FullOccupancyError end it "excludes the room from be added to hotel block during the same date range" do From 18f55d24eb0d6e3855e1fe354323cb15a5780abf Mon Sep 17 00:00:00 2001 From: Tiffany Chio Date: Fri, 6 Sep 2019 23:40:19 -0700 Subject: [PATCH 27/38] complete refactor of system.rb --- lib/system.rb | 27 ++++++++++----------------- test/system_test.rb | 20 ++++++++++++++------ 2 files changed, 24 insertions(+), 23 deletions(-) diff --git a/lib/system.rb b/lib/system.rb index ace3d8131..91e61b490 100644 --- a/lib/system.rb +++ b/lib/system.rb @@ -15,8 +15,9 @@ def initialize @reservations = [] @hoteldates = [] - # keys will be hotel block ids (shared by all rooms within block) - # values will be arrays of hotel block objects within same block + # Keys will be hotel block ids (shared by all rooms within block). + # Values will be arrays of hotel block objects within same block. + # Each array element represents one room. @hotelblocks = {} end @@ -75,9 +76,8 @@ def add_to_dates(start_date, end_date, new_occupancy) end end - # TIFF CHANGED FROM STRING TO OBJ - def list_reservations_for(date_string) - hotel_date = find_date(Date.parse(date_string)) + def list_reservations_for(date) + hotel_date = find_date(date) if hotel_date return hotel_date.list_reservations @@ -86,13 +86,8 @@ def list_reservations_for(date_string) end end - # Dates are strings # hb_rooms is an array of room_ids - # Discount rate can be whatever number def create_hotelblock(start_date:, end_date:, hb_rooms:, discount_rate:) - start_date = Date.parse(start_date) - end_date = Date.parse(end_date) - discount_rate = discount_rate.to_f hb_rooms.map! { |room_id| find_room(room_id) } if hb_rooms.length > 5 @@ -103,16 +98,15 @@ def create_hotelblock(start_date:, end_date:, hb_rooms:, discount_rate:) # Does block contain a room that is not part of available rooms? # If so raise an error. - # Hey TIFF make this part longer to include which room if hb_rooms.map{ |room| available_rooms.include? room}.include? false - raise ArgumentError, 'Block contains room already booked.' + raise ArgumentError, 'Block contains room that is already booked.' end hb_id = @hotelblocks.length + 1 @hotelblocks[hb_id] = [] hb_rooms.each do |room| - new_hotel_block = Hotel::HotelBlock.new(id: hb_id, room: room, start_date: start_date, end_date: end_date, cost:discount_rate) + new_hotel_block = Hotel::HotelBlock.new(id: hb_id, room: room, start_date: start_date, end_date: end_date, cost:discount_rate.to_f) @hotelblocks[hb_id] << new_hotel_block @@ -124,22 +118,22 @@ def find_open_rooms_from_block(hb_id) hotel_blocks = @hotelblocks[hb_id] open_rooms = hotel_blocks.select { |hotel_block| hotel_block.status == :AVAILABLE} + # Return open rooms as room IDs rather than Room objects for end user readability. open_rooms.map! { |hotel_block| hotel_block.room.id} return open_rooms end def reserve_from_block(hb_id, room_id) - # check availability unless find_open_rooms_from_block(hb_id).include? room_id raise ArgumentError, 'The room you are trying to book is not available.' end - # change status + # Change room status so room can no longer be booked. hotel_block = @hotelblocks[hb_id].find {|hb| hb.room.id == room_id} hotel_block.change_status - # make reservation with pre-determined arg + # Reservation is made using hotel block dates and cost. id = @reservations.length + 1 room = find_room(room_id) start_date = hotel_block.start_date @@ -148,7 +142,6 @@ def reserve_from_block(hb_id, room_id) new_reservation = Hotel::Reservation.new(id: id, room: room, start_date: start_date, end_date: end_date, cost: cost) - # Connect to Reservations List @reservations << new_reservation end diff --git a/test/system_test.rb b/test/system_test.rb index 6ab86577c..f5801bf01 100644 --- a/test/system_test.rb +++ b/test/system_test.rb @@ -103,7 +103,7 @@ end it "returns nil when no reservations have been made for given date" do - res_list = @sys.list_reservations_for('2020-08-09') + res_list = @sys.list_reservations_for(Date.parse('2020-08-09')) expect(res_list).must_be_nil end @@ -112,7 +112,7 @@ first_res = @sys.make_reservation(Date.parse('2019-06-05'), Date.parse('2019-06-08')) second_res = @sys.make_reservation(Date.parse('2019-06-06'), Date.parse('2019-06-09')) - res_list = @sys.list_reservations_for('2019-06-06') + res_list = @sys.list_reservations_for(Date.parse('2019-06-06')) expect(res_list.length).must_equal 2 expect(res_list.include? first_res).must_equal true expect(res_list.include? second_res).must_equal true @@ -151,7 +151,9 @@ describe "HotelBlock Creation" do before do @sys = Hotel::System.new - @sys.create_hotelblock(start_date: '2019-09-02', end_date: '2019-09-05', hb_rooms: [1,2,3,4], discount_rate: 165) + start_date = Date.parse('2019-09-02') + end_date = Date.parse('2019-09-05') + @sys.create_hotelblock(start_date: start_date, end_date: end_date, hb_rooms: [1,2,3,4], discount_rate: 165) end it "adds to the hotelblocks list" do @@ -184,18 +186,24 @@ end it "excludes the room from be added to hotel block during the same date range" do - expect{@sys.create_hotelblock(start_date: '2019-09-04', end_date: '2019-09-07', hb_rooms: [2,5,6,7], discount_rate: 165)}.must_raise ArgumentError + start_date = Date.parse('2019-09-04') + end_date = Date.parse('2019-09-07') + expect{@sys.create_hotelblock(start_date: start_date, end_date: end_date, hb_rooms: [2,5,6,7], discount_rate: 165)}.must_raise ArgumentError end it "raises an error when trying to create a block of more than 5 rooms" do - expect{@sys.create_hotelblock(start_date: '2019-09-02', end_date: '2019-09-05', hb_rooms: [1,2,3,4,5,6], discount_rate: 165)}.must_raise ArgumentError + start_date = Date.parse('2019-09-02') + end_date = Date.parse('2019-09-05') + expect{@sys.create_hotelblock(start_date: start_date, end_date: end_date, hb_rooms: [1,2,3,4,5,6], discount_rate: 165)}.must_raise ArgumentError end end describe "Reserving from a HotelBlock" do before do @sys = Hotel::System.new - hotel_block = @sys.create_hotelblock(start_date: '2019-09-02', end_date: '2019-09-05', hb_rooms: [7,8,9,10], discount_rate: 165) + start_date = Date.parse('2019-09-02') + end_date = Date.parse('2019-09-05') + hotel_block = @sys.create_hotelblock(start_date: start_date, end_date: end_date, hb_rooms: [7,8,9,10], discount_rate: 165) @sys.reserve_from_block(1, 8) end From 5a4a8e4852a9c9c31da4de85b3d3e6677661e283 Mon Sep 17 00:00:00 2001 From: Tiffany Chio Date: Sat, 7 Sep 2019 11:15:12 -0700 Subject: [PATCH 28/38] last working commit before trying to change namespaces again --- lib/custom_errors.rb | 2 ++ lib/system.rb | 4 +--- test/test_helper.rb | 1 + 3 files changed, 4 insertions(+), 3 deletions(-) create mode 100644 lib/custom_errors.rb diff --git a/lib/custom_errors.rb b/lib/custom_errors.rb new file mode 100644 index 000000000..7a3275333 --- /dev/null +++ b/lib/custom_errors.rb @@ -0,0 +1,2 @@ +class FullOccupancyError < StandardError +end diff --git a/lib/system.rb b/lib/system.rb index 91e61b490..54e8f70c3 100644 --- a/lib/system.rb +++ b/lib/system.rb @@ -4,6 +4,7 @@ require_relative 'reservation' require_relative 'hoteldate' require_relative 'hotelblock' +require_relative 'custom_errors' module Hotel class System @@ -148,6 +149,3 @@ def reserve_from_block(hb_id, room_id) end end -class FullOccupancyError < StandardError -end - diff --git a/test/test_helper.rb b/test/test_helper.rb index 8113a7d3d..65ca40400 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -17,3 +17,4 @@ require_relative '../lib/reservation' require_relative '../lib/room' require_relative '../lib/system' +require_relative '../lib/custom_errors' From 551edc34a8d50ad165db031d50c48aabdd670a2d Mon Sep 17 00:00:00 2001 From: Tiffany Chio Date: Sat, 7 Sep 2019 11:27:21 -0700 Subject: [PATCH 29/38] tentatively saving. nothing broke after changing hotelname class nomenclature... --- lib/hoteldate.rb | 2 +- lib/system.rb | 10 ++-- test/hotelblock_test.rb | 16 +++--- test/hoteldate_test.rb | 18 +++---- test/reservation_test.rb | 12 ++--- test/system_test.rb | 102 +++++++++++++++++++-------------------- 6 files changed, 80 insertions(+), 80 deletions(-) diff --git a/lib/hoteldate.rb b/lib/hoteldate.rb index c2d0b8eb5..dd84524c1 100644 --- a/lib/hoteldate.rb +++ b/lib/hoteldate.rb @@ -2,7 +2,7 @@ require_relative 'reservation' module Hotel - class HotelDate + class Date attr_reader :id, :occupied diff --git a/lib/system.rb b/lib/system.rb index 54e8f70c3..ad5f93ba1 100644 --- a/lib/system.rb +++ b/lib/system.rb @@ -9,12 +9,12 @@ module Hotel class System - attr_reader :rooms, :reservations, :hoteldates, :hotelblocks + attr_reader :rooms, :reservations, :dates, :hotelblocks def initialize @rooms = Hotel::Room.generate_rooms @reservations = [] - @hoteldates = [] + @dates = [] # Keys will be hotel block ids (shared by all rooms within block). # Values will be arrays of hotel block objects within same block. @@ -27,7 +27,7 @@ def find_room(room_id) end def find_date(date_obj) - return @hoteldates.find { |hotel_date| hotel_date.id == date_obj } + return @dates.find { |hotel_date| hotel_date.id == date_obj } end def find_all_available_rooms(start_date, end_date) @@ -68,8 +68,8 @@ def add_to_dates(start_date, end_date, new_occupancy) if date_obj date_obj.add_occupancy(new_occupancy) else - new_date_obj = Hotel::HotelDate.new(current_date) - @hoteldates << new_date_obj + new_date_obj = Hotel::Date.new(current_date) + @dates << new_date_obj new_date_obj.add_occupancy(new_occupancy) end diff --git a/test/hotelblock_test.rb b/test/hotelblock_test.rb index 208f40495..d10457d95 100644 --- a/test/hotelblock_test.rb +++ b/test/hotelblock_test.rb @@ -4,8 +4,8 @@ describe "HotelBlock instantiation" do before do room_15 = Hotel::Room.new(15, 200.00) - start_time = Date.parse('2019-09-03') - end_time = Date.parse('2019-09-08') + start_time = ::Date.parse('2019-09-03') + end_time = ::Date.parse('2019-09-08') @hotelblock = Hotel::HotelBlock.new(id: 101, room: room_15, start_date: start_time, end_date: end_time, cost: 100) end @@ -39,8 +39,8 @@ describe "the status of hotelblock" do before do room_15 = Hotel::Room.new(15, 200.00) - start_time = Date.parse('2019-09-03') - end_time = Date.parse('2019-09-08') + start_time = ::Date.parse('2019-09-03') + end_time = ::Date.parse('2019-09-08') @hotelblock = Hotel::HotelBlock.new(id: 101, room: room_15, start_date: start_time, end_date: end_time, cost: 100) end @@ -58,16 +58,16 @@ describe "raises an exception when an invalid date range is provided" do it "raises an error if end date is before start date" do room_15 = Hotel::Room.new(15, 200.00) - start_time = Date.parse('2019-09-03') - end_time = Date.parse('2019-09-01') + start_time = ::Date.parse('2019-09-03') + end_time = ::Date.parse('2019-09-01') expect{Hotel::HotelBlock.new(id: 101, room: room_15, start_date: start_time, end_date: end_time, cost: 100)}.must_raise ArgumentError end it "raises an error if end date is on same day as start date" do room_15 = Hotel::Room.new(15, 200.00) - start_time = Date.parse('2019-09-03') - end_time = Date.parse('2019-09-03') + start_time = ::Date.parse('2019-09-03') + end_time = ::Date.parse('2019-09-03') expect{Hotel::HotelBlock.new(id: 101, room: room_15, start_date: start_time, end_date: end_time, cost: 100)}.must_raise ArgumentError end diff --git a/test/hoteldate_test.rb b/test/hoteldate_test.rb index e0a9f4170..cb93162b5 100644 --- a/test/hoteldate_test.rb +++ b/test/hoteldate_test.rb @@ -1,13 +1,13 @@ require_relative 'test_helper' -describe "HotelDate class" do - describe "HotelDate instantiation" do +describe "Hotel::Date class" do + describe "Hotel::Date instantiation" do before do - @hotel_date = Hotel::HotelDate.new(Date.parse('2019-09-03')) + @hotel_date = Hotel::Date.new(::Date.parse('2019-09-03')) end - it "is an instance of HotelDate" do - expect(@hotel_date).must_be_kind_of Hotel::HotelDate + it "is an instance of Hotel::Date" do + expect(@hotel_date).must_be_kind_of Hotel::Date end it "is set up for specific attributes and data types" do @@ -21,13 +21,13 @@ end - describe "HotelDate instance methods" do + describe "Hotel::Date instance methods" do before do - @hotel_date = Hotel::HotelDate.new(Date.parse('2019-09-03')) + @hotel_date = Hotel::Date.new(::Date.parse('2019-09-03')) @room_15 = Hotel::Room.new(15, 200.00) - start_time = Date.parse('2019-09-03') - end_time = Date.parse('2019-09-08') + start_time = ::Date.parse('2019-09-03') + end_time = ::Date.parse('2019-09-08') @reservation = Hotel::Reservation.new(id: 101, room: @room_15, start_date: start_time, end_date: end_time) @hotel_date.add_occupancy(@reservation) diff --git a/test/reservation_test.rb b/test/reservation_test.rb index f60e45b0e..6ef8fceec 100644 --- a/test/reservation_test.rb +++ b/test/reservation_test.rb @@ -4,8 +4,8 @@ describe "Reservation instantiation" do before do room_15 = Hotel::Room.new(15, 200.00) - start_time = Date.parse('2019-09-03') - end_time = Date.parse('2019-09-08') + start_time = ::Date.parse('2019-09-03') + end_time = ::Date.parse('2019-09-08') @reservation = Hotel::Reservation.new(id: 101, room: room_15, start_date: start_time, end_date: end_time) end @@ -39,16 +39,16 @@ describe "raises an exception when an invalid date range is provided" do it "raises an error if end date is before start date" do room_15 = Hotel::Room.new(15, 200.00) - start_time = Date.parse('2019-09-03') - end_time = Date.parse('2019-09-01') + start_time = ::Date.parse('2019-09-03') + end_time = ::Date.parse('2019-09-01') expect{Hotel::Reservation.new(id: 101, room: room_15, start_date: start_time, end_date: end_time)}.must_raise ArgumentError end it "raises an error if end date is on same day as start date" do room_15 = Hotel::Room.new(15, 200.00) - start_time = Date.parse('2019-09-03') - end_time = Date.parse('2019-09-03') + start_time = ::Date.parse('2019-09-03') + end_time = ::Date.parse('2019-09-03') expect{Hotel::Reservation.new(id: 101, room: room_15, start_date: start_time, end_date: end_time)}.must_raise ArgumentError end diff --git a/test/system_test.rb b/test/system_test.rb index f5801bf01..794034876 100644 --- a/test/system_test.rb +++ b/test/system_test.rb @@ -11,7 +11,7 @@ end it "is set up for specific attributes and data types" do - [:rooms, :reservations, :hoteldates, :hotelblocks].each do |prop| + [:rooms, :reservations, :dates, :hotelblocks].each do |prop| expect(@sys).must_respond_to prop end @@ -19,7 +19,7 @@ expect(@sys.rooms.first).must_be_instance_of Hotel::Room expect(@sys.rooms.last).must_be_instance_of Hotel::Room expect(@sys.reservations).must_be_instance_of Array - expect(@sys.hoteldates).must_be_instance_of Array + expect(@sys.dates).must_be_instance_of Array expect(@sys.hotelblocks).must_be_instance_of Hash end @@ -33,15 +33,15 @@ describe "making a room reservation" do before do @sys = Hotel::System.new - @first_res = @sys.make_reservation(Date.parse('2019-06-05'), Date.parse('2019-06-08')) + @first_res = @sys.make_reservation(::Date.parse('2019-06-05'), ::Date.parse('2019-06-08')) end it "returns a new reservation with correct dates" do expect(@first_res).must_be_instance_of Hotel::Reservation expect(@first_res.id).must_equal 1 - expect(@first_res.start_date).must_equal Date.parse('2019-06-05') - expect(@first_res.end_date).must_equal Date.parse('2019-06-08') + expect(@first_res.start_date).must_equal ::Date.parse('2019-06-05') + expect(@first_res.end_date).must_equal ::Date.parse('2019-06-08') expect(@first_res.cost).must_equal 200.0 expect(@first_res.find_total_cost).must_equal (3*200.0) end @@ -52,48 +52,48 @@ end it "adds the date range of the reservation for which nights are occupied to the list of dates" do - expect(@sys.hoteldates.length).must_equal 3 - expect(@sys.hoteldates[0].id).must_equal Date.parse('2019-06-05') - expect(@sys.hoteldates[1].id).must_equal Date.parse('2019-06-06') - expect(@sys.hoteldates[2].id).must_equal Date.parse('2019-06-07') + expect(@sys.dates.length).must_equal 3 + expect(@sys.dates[0].id).must_equal ::Date.parse('2019-06-05') + expect(@sys.dates[1].id).must_equal ::Date.parse('2019-06-06') + expect(@sys.dates[2].id).must_equal ::Date.parse('2019-06-07') end - it "does not create a new HotelDate object when one already exists" do - second_res = @sys.make_reservation(Date.parse('2019-06-06'), Date.parse('2019-06-09')) + it "does not create a new Hotel::Date object when one already exists" do + second_res = @sys.make_reservation(::Date.parse('2019-06-06'), ::Date.parse('2019-06-09')) expect(@sys.reservations.length).must_equal 2 - expect(@sys.hoteldates.length).must_equal 4 + expect(@sys.dates.length).must_equal 4 - expect(@sys.hoteldates[0].id).must_equal Date.parse('2019-06-05') - expect(@sys.hoteldates[1].id).must_equal Date.parse('2019-06-06') - expect(@sys.hoteldates[2].id).must_equal Date.parse('2019-06-07') - expect(@sys.hoteldates[3].id).must_equal Date.parse('2019-06-08') + expect(@sys.dates[0].id).must_equal ::Date.parse('2019-06-05') + expect(@sys.dates[1].id).must_equal ::Date.parse('2019-06-06') + expect(@sys.dates[2].id).must_equal ::Date.parse('2019-06-07') + expect(@sys.dates[3].id).must_equal ::Date.parse('2019-06-08') - expect(@sys.hoteldates[0].occupied.length).must_equal 1 - expect(@sys.hoteldates[1].occupied.length).must_equal 2 - expect(@sys.hoteldates[2].occupied.length).must_equal 2 - expect(@sys.hoteldates[3].occupied.length).must_equal 1 + expect(@sys.dates[0].occupied.length).must_equal 1 + expect(@sys.dates[1].occupied.length).must_equal 2 + expect(@sys.dates[2].occupied.length).must_equal 2 + expect(@sys.dates[3].occupied.length).must_equal 1 end it "creates reservations using available rooms" do expect(@first_res.room.id).must_equal 1 - second_res = @sys.make_reservation(Date.parse('2019-06-01'), Date.parse('2019-06-06')) + second_res = @sys.make_reservation(::Date.parse('2019-06-01'), ::Date.parse('2019-06-06')) expect(second_res.room.id).must_equal 2 - third_res = @sys.make_reservation(Date.parse('2019-06-05'), Date.parse('2019-06-10')) + third_res = @sys.make_reservation(::Date.parse('2019-06-05'), ::Date.parse('2019-06-10')) expect(third_res.room.id).must_equal 3 - fourth_res = @sys.make_reservation(Date.parse('2019-06-07'), Date.parse('2019-06-10')) + fourth_res = @sys.make_reservation(::Date.parse('2019-06-07'), ::Date.parse('2019-06-10')) expect(fourth_res.room.id).must_equal 2 end it "returns an error when no rooms are available" do 19.times do - @sys.make_reservation(Date.parse('2019-06-05'), Date.parse('2019-06-08')) + @sys.make_reservation(::Date.parse('2019-06-05'), ::Date.parse('2019-06-08')) end - expect{ @sys.make_reservation(Date.parse('2019-06-05'), Date.parse('2019-06-08')) }.must_raise FullOccupancyError + expect{ @sys.make_reservation(::Date.parse('2019-06-05'), ::Date.parse('2019-06-08')) }.must_raise FullOccupancyError end end @@ -103,16 +103,16 @@ end it "returns nil when no reservations have been made for given date" do - res_list = @sys.list_reservations_for(Date.parse('2020-08-09')) + res_list = @sys.list_reservations_for(::Date.parse('2020-08-09')) expect(res_list).must_be_nil end it "returns an array of reservations for a given date" do - first_res = @sys.make_reservation(Date.parse('2019-06-05'), Date.parse('2019-06-08')) - second_res = @sys.make_reservation(Date.parse('2019-06-06'), Date.parse('2019-06-09')) + first_res = @sys.make_reservation(::Date.parse('2019-06-05'), ::Date.parse('2019-06-08')) + second_res = @sys.make_reservation(::Date.parse('2019-06-06'), ::Date.parse('2019-06-09')) - res_list = @sys.list_reservations_for(Date.parse('2019-06-06')) + res_list = @sys.list_reservations_for(::Date.parse('2019-06-06')) expect(res_list.length).must_equal 2 expect(res_list.include? first_res).must_equal true expect(res_list.include? second_res).must_equal true @@ -133,11 +133,11 @@ it "finds all available rooms" do 10.times do - @sys.make_reservation(Date.parse('2019-06-05'), Date.parse('2019-06-08')) + @sys.make_reservation(::Date.parse('2019-06-05'), ::Date.parse('2019-06-08')) end - start_date = Date.parse('2019-06-05') - end_date = Date.parse('2019-06-08') + start_date = ::Date.parse('2019-06-05') + end_date = ::Date.parse('2019-06-08') available_rooms = @sys.find_all_available_rooms(start_date, end_date) @@ -151,8 +151,8 @@ describe "HotelBlock Creation" do before do @sys = Hotel::System.new - start_date = Date.parse('2019-09-02') - end_date = Date.parse('2019-09-05') + start_date = ::Date.parse('2019-09-02') + end_date = ::Date.parse('2019-09-05') @sys.create_hotelblock(start_date: start_date, end_date: end_date, hb_rooms: [1,2,3,4], discount_rate: 165) end @@ -164,36 +164,36 @@ expect(@sys.hotelblocks[1][2].room.id).must_equal 3 end - it "updates the hoteldates list" do - expect(@sys.hoteldates.length).must_equal 3 + it "updates the hotel.dates list" do + expect(@sys.dates.length).must_equal 3 - expect(@sys.hoteldates.first).must_be_instance_of Hotel::HotelDate + expect(@sys.dates.first).must_be_instance_of Hotel::Date - expect(@sys.hoteldates[0].id).must_equal Date.parse('2019-09-02') - expect(@sys.hoteldates[1].id).must_equal Date.parse('2019-09-03') - expect(@sys.hoteldates[2].id).must_equal Date.parse('2019-09-04') + expect(@sys.dates[0].id).must_equal ::Date.parse('2019-09-02') + expect(@sys.dates[1].id).must_equal ::Date.parse('2019-09-03') + expect(@sys.dates[2].id).must_equal ::Date.parse('2019-09-04') end it "excludes the room from reservation during the same date range" do - new_reservation = @sys.make_reservation(Date.parse('2019-09-04'), Date.parse('2019-09-07')) + new_reservation = @sys.make_reservation(::Date.parse('2019-09-04'), ::Date.parse('2019-09-07')) expect(new_reservation.room.id).must_equal 5 15.times do - @sys.make_reservation(Date.parse('2019-09-04'), Date.parse('2019-09-07')) + @sys.make_reservation(::Date.parse('2019-09-04'), ::Date.parse('2019-09-07')) end - expect{@sys.make_reservation(Date.parse('2019-09-04'), Date.parse('2019-09-07'))}.must_raise FullOccupancyError + expect{@sys.make_reservation(::Date.parse('2019-09-04'), ::Date.parse('2019-09-07'))}.must_raise FullOccupancyError end it "excludes the room from be added to hotel block during the same date range" do - start_date = Date.parse('2019-09-04') - end_date = Date.parse('2019-09-07') + start_date = ::Date.parse('2019-09-04') + end_date = ::Date.parse('2019-09-07') expect{@sys.create_hotelblock(start_date: start_date, end_date: end_date, hb_rooms: [2,5,6,7], discount_rate: 165)}.must_raise ArgumentError end it "raises an error when trying to create a block of more than 5 rooms" do - start_date = Date.parse('2019-09-02') - end_date = Date.parse('2019-09-05') + start_date = ::Date.parse('2019-09-02') + end_date = ::Date.parse('2019-09-05') expect{@sys.create_hotelblock(start_date: start_date, end_date: end_date, hb_rooms: [1,2,3,4,5,6], discount_rate: 165)}.must_raise ArgumentError end end @@ -201,8 +201,8 @@ describe "Reserving from a HotelBlock" do before do @sys = Hotel::System.new - start_date = Date.parse('2019-09-02') - end_date = Date.parse('2019-09-05') + start_date = ::Date.parse('2019-09-02') + end_date = ::Date.parse('2019-09-05') hotel_block = @sys.create_hotelblock(start_date: start_date, end_date: end_date, hb_rooms: [7,8,9,10], discount_rate: 165) @sys.reserve_from_block(1, 8) @@ -214,8 +214,8 @@ end it "makes reservations for the duration of the hotel block" do - expect(@sys.reservations[0].start_date).must_equal Date.parse('2019-09-02') - expect(@sys.reservations[0].end_date).must_equal Date.parse('2019-09-05') + expect(@sys.reservations[0].start_date).must_equal ::Date.parse('2019-09-02') + expect(@sys.reservations[0].end_date).must_equal ::Date.parse('2019-09-05') end it "returns all available rooms in a HotelBlock" do From 5e246926c18e4512b0956f2ff5a22e535ab205e2 Mon Sep 17 00:00:00 2001 From: Tiffany Chio Date: Sat, 7 Sep 2019 12:10:11 -0700 Subject: [PATCH 30/38] refactored room, reservation, hotelblock test files --- lib/hoteldate.rb | 2 +- lib/system.rb | 2 +- test/hoteldate_test.rb | 21 +++++++++++++++++---- test/reservation_test.rb | 16 +++++++--------- test/room_test.rb | 17 ++++++++++++++++- 5 files changed, 42 insertions(+), 16 deletions(-) diff --git a/lib/hoteldate.rb b/lib/hoteldate.rb index dd84524c1..313c67533 100644 --- a/lib/hoteldate.rb +++ b/lib/hoteldate.rb @@ -26,7 +26,7 @@ def list_reservations end # Returns all occupied rooms, either reserved or blocked. - def rooms_occupied + def rooms_unavailable return @occupied.keys end diff --git a/lib/system.rb b/lib/system.rb index ad5f93ba1..26cae5730 100644 --- a/lib/system.rb +++ b/lib/system.rb @@ -36,7 +36,7 @@ def find_all_available_rooms(start_date, end_date) until current_date == end_date hotel_date = find_date(current_date) - available_rooms -= hotel_date.rooms_occupied if hotel_date + available_rooms -= hotel_date.rooms_unavailable if hotel_date current_date += 1 end diff --git a/test/hoteldate_test.rb b/test/hoteldate_test.rb index cb93162b5..e16639b89 100644 --- a/test/hoteldate_test.rb +++ b/test/hoteldate_test.rb @@ -30,7 +30,13 @@ end_time = ::Date.parse('2019-09-08') @reservation = Hotel::Reservation.new(id: 101, room: @room_15, start_date: start_time, end_date: end_time) + @room_9 = Hotel::Room.new(9, 200.00) + start_time = ::Date.parse('2019-09-03') + end_time = ::Date.parse('2019-09-08') + @hotel_block = Hotel::HotelBlock.new(id: 201, room: @room_9, start_date: start_time, end_date: end_time, cost:145) + @hotel_date.add_occupancy(@reservation) + @hotel_date.add_occupancy(@hotel_block) end it "can add a reservation" do @@ -39,7 +45,13 @@ expect(@hotel_date.occupied[@room_15]).must_equal @reservation end - it "returns an array of reservations under a given date" do + it "can add a hotel block" do + expect(@hotel_date.occupied.keys.last).must_be_instance_of Hotel::Room + expect(@hotel_date.occupied.values.last).must_be_instance_of Hotel::HotelBlock + expect(@hotel_date.occupied[@room_9]).must_equal @hotel_block + end + + it "returns an array of reservations for given date, excludes hotel blocks" do expect(@hotel_date.list_reservations).must_be_instance_of Array expect(@hotel_date.list_reservations.length).must_equal 1 @@ -47,10 +59,11 @@ end it "returns an array of occupied rooms under a given date" do - expect(@hotel_date.rooms_occupied).must_be_instance_of Array + expect(@hotel_date.rooms_unavailable).must_be_instance_of Array - expect(@hotel_date.rooms_occupied.length).must_equal 1 - expect(@hotel_date.rooms_occupied[0]).must_equal @room_15 + expect(@hotel_date.rooms_unavailable.length).must_equal 2 + expect(@hotel_date.rooms_unavailable[0]).must_equal @room_15 + expect(@hotel_date.rooms_unavailable[1]).must_equal @room_9 end end end diff --git a/test/reservation_test.rb b/test/reservation_test.rb index 6ef8fceec..2ee407812 100644 --- a/test/reservation_test.rb +++ b/test/reservation_test.rb @@ -30,27 +30,25 @@ expect(@reservation.cost).must_be_close_to 200.0 end - # TIFF PUT THIS SOMEWHERE ELSE it "correctly calculates total_cost" do expect(@reservation.find_total_cost).must_be_close_to 200.0*5 end end describe "raises an exception when an invalid date range is provided" do + before do + @room_15 = Hotel::Room.new(15, 200.00) + @start_time = ::Date.parse('2019-09-03') + end + it "raises an error if end date is before start date" do - room_15 = Hotel::Room.new(15, 200.00) - start_time = ::Date.parse('2019-09-03') end_time = ::Date.parse('2019-09-01') - expect{Hotel::Reservation.new(id: 101, room: room_15, start_date: start_time, end_date: end_time)}.must_raise ArgumentError + expect{Hotel::Reservation.new(id: 101, room: @room_15, start_date: @start_time, end_date: end_time)}.must_raise ArgumentError end it "raises an error if end date is on same day as start date" do - room_15 = Hotel::Room.new(15, 200.00) - start_time = ::Date.parse('2019-09-03') - end_time = ::Date.parse('2019-09-03') - - expect{Hotel::Reservation.new(id: 101, room: room_15, start_date: start_time, end_date: end_time)}.must_raise ArgumentError + expect{Hotel::Reservation.new(id: 101, room: @room_15, start_date: @start_time, end_date: @start_time)}.must_raise ArgumentError end end end diff --git a/test/room_test.rb b/test/room_test.rb index 4bfdc7dab..c2d51250a 100644 --- a/test/room_test.rb +++ b/test/room_test.rb @@ -3,7 +3,6 @@ describe "Room class" do describe "Room instantiation" do before do - # Must use @ to make it accessible without a reader @room = Hotel::Room.new(15, 200.00) end @@ -19,5 +18,21 @@ expect(@room.id).must_be_instance_of Integer expect(@room.cost).must_be_instance_of Float end + + describe "Room generation" do + before do + @room_array = Hotel::Room.generate_rooms + end + + it "generates an array of rooms" do + expect(@room_array).must_be_instance_of Array + expect(@room_array.first).must_be_kind_of Hotel::Room + expect(@room_array.last).must_be_kind_of Hotel::Room + end + + it "generates the correct number of rooms" do + expect(@room_array.length).must_equal 20 + end + end end end From 05dae1deb324bf759e6e5f49d559a88d4ecd1081 Mon Sep 17 00:00:00 2001 From: Tiffany Chio Date: Sat, 7 Sep 2019 12:17:29 -0700 Subject: [PATCH 31/38] refactored all tests except for system --- test/hotelblock_test.rb | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/test/hotelblock_test.rb b/test/hotelblock_test.rb index d10457d95..0142897c3 100644 --- a/test/hotelblock_test.rb +++ b/test/hotelblock_test.rb @@ -30,9 +30,8 @@ expect(@hotelblock.cost).must_be_close_to 100.0 end - # TIFF PUT THIS SOMEWHERE ELSE it "correctly calculates total_cost" do - expect(@hotelblock.find_total_cost).must_be_close_to 100.0*5 + expect(@hotelblock.find_total_cost).must_be_close_to 500.0 end end @@ -56,20 +55,19 @@ end describe "raises an exception when an invalid date range is provided" do + before do + @room_15 = Hotel::Room.new(15, 200.00) + @start_time = ::Date.parse('2019-09-03') + end + it "raises an error if end date is before start date" do - room_15 = Hotel::Room.new(15, 200.00) - start_time = ::Date.parse('2019-09-03') end_time = ::Date.parse('2019-09-01') - expect{Hotel::HotelBlock.new(id: 101, room: room_15, start_date: start_time, end_date: end_time, cost: 100)}.must_raise ArgumentError + expect{Hotel::HotelBlock.new(id: 101, room: @room_15, start_date: @start_time, end_date: end_time, cost: 100)}.must_raise ArgumentError end it "raises an error if end date is on same day as start date" do - room_15 = Hotel::Room.new(15, 200.00) - start_time = ::Date.parse('2019-09-03') - end_time = ::Date.parse('2019-09-03') - - expect{Hotel::HotelBlock.new(id: 101, room: room_15, start_date: start_time, end_date: end_time, cost: 100)}.must_raise ArgumentError + expect{Hotel::HotelBlock.new(id: 101, room: @room_15, start_date: @start_time, end_date: @start_time, cost: 100)}.must_raise ArgumentError end end end From be55658e8a1950c2bee8f37ce56272cee3815cdb Mon Sep 17 00:00:00 2001 From: Tiffany Chio Date: Sat, 7 Sep 2019 20:10:27 -0700 Subject: [PATCH 32/38] added edge cases --- test/system_test.rb | 45 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 39 insertions(+), 6 deletions(-) diff --git a/test/system_test.rb b/test/system_test.rb index 794034876..c027cbcfc 100644 --- a/test/system_test.rb +++ b/test/system_test.rb @@ -23,20 +23,20 @@ expect(@sys.hotelblocks).must_be_instance_of Hash end - it "executes generate rooms method correctly" do + it "allows access to the list of all rooms" do expect(@sys.rooms.length).must_equal 20 expect(@sys.rooms[11].id).must_equal 12 expect(@sys.rooms[11].cost).must_equal 200.0 end end - describe "making a room reservation" do + describe "room reservation creation" do before do @sys = Hotel::System.new @first_res = @sys.make_reservation(::Date.parse('2019-06-05'), ::Date.parse('2019-06-08')) end - it "returns a new reservation with correct dates" do + it "generates reserves a room for a given date range" do expect(@first_res).must_be_instance_of Hotel::Reservation expect(@first_res.id).must_equal 1 @@ -97,7 +97,40 @@ end end - describe "list_reservations_for method" do + describe "reservation overlap verification" do + before do + @sys = Hotel::System.new + @first_res = @sys.make_reservation(::Date.parse('2019-06-05'), ::Date.parse('2019-06-08')) + end + + it "reservations do not impact list of available rooms outside of date range" do + same_date_range = @sys.find_all_available_rooms(::Date.parse('2019-06-05'), ::Date.parse('2019-06-08')) + well_before_start_date = @sys.find_all_available_rooms(::Date.parse('2019-01-11'), ::Date.parse('2019-01-22')) + well_after_end_date = @sys.find_all_available_rooms(::Date.parse('2019-10-11'), ::Date.parse('2019-10-22')) + + expect(same_date_range.length).must_equal 19 + expect(well_before_start_date.length).must_equal 20 + expect(well_after_end_date.length).must_equal 20 + end + + it "allows reservation of room in which check-in day is the same as another reservation's check-out day" do + start_date_is_end_date = @sys.find_all_available_rooms(::Date.parse('2019-06-08'), ::Date.parse('2019-06-10')) + + expect(start_date_is_end_date.length).must_equal 20 + expect(@first_res.room.id).must_equal 1 + expect(start_date_is_end_date.include? 1).must_equal false + end + + it "excludes rooms for reservations if they have smaller pre-existing reservations within date range" do + @second_res = @sys.make_reservation(::Date.parse('2019-06-06'), ::Date.parse('2019-06-10')) + @third_res = @sys.make_reservation(::Date.parse('2019-06-07'), ::Date.parse('2019-06-09')) + + spans_all_res = @sys.find_all_available_rooms(::Date.parse('2019-06-01'), ::Date.parse('2019-06-12')) + expect(spans_all_res.length).must_equal 17 + end + end + + describe "list reservations for date method" do before do @sys = Hotel::System.new end @@ -119,7 +152,7 @@ end end - describe "FILL IN NAME" do + describe "Finds Available Rooms" do before do @sys = Hotel::System.new end @@ -131,7 +164,7 @@ expect(room_obj.id).must_equal 16 end - it "finds all available rooms" do + it "returns list of rooms that are not reserved for a given date range" do 10.times do @sys.make_reservation(::Date.parse('2019-06-05'), ::Date.parse('2019-06-08')) end From 748f2e0bb71cd82297a8634e4c17e8c95cbc4712 Mon Sep 17 00:00:00 2001 From: Tiffany Chio Date: Sat, 7 Sep 2019 20:18:19 -0700 Subject: [PATCH 33/38] added hotel block testing --- test/system_test.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/system_test.rb b/test/system_test.rb index c027cbcfc..573b82476 100644 --- a/test/system_test.rb +++ b/test/system_test.rb @@ -221,14 +221,22 @@ it "excludes the room from be added to hotel block during the same date range" do start_date = ::Date.parse('2019-09-04') end_date = ::Date.parse('2019-09-07') + expect{@sys.create_hotelblock(start_date: start_date, end_date: end_date, hb_rooms: [2,5,6,7], discount_rate: 165)}.must_raise ArgumentError end it "raises an error when trying to create a block of more than 5 rooms" do start_date = ::Date.parse('2019-09-02') end_date = ::Date.parse('2019-09-05') + expect{@sys.create_hotelblock(start_date: start_date, end_date: end_date, hb_rooms: [1,2,3,4,5,6], discount_rate: 165)}.must_raise ArgumentError end + + it "allows blocking of room in which check-in day is the same as another block's check-out day" do + @sys.create_hotelblock(start_date: ::Date.parse('2019-09-05'), end_date: ::Date.parse('2019-09-09'), hb_rooms: [1,2,3,4], discount_rate: 140) + + expect(@sys.hotelblocks[2].first.start_date).must_equal ::Date.parse('2019-09-05') + end end describe "Reserving from a HotelBlock" do From 273e3cffa8a779f5afde0a50aad6831b2dadbdcf Mon Sep 17 00:00:00 2001 From: Tiffany Chio Date: Sun, 8 Sep 2019 13:47:25 -0700 Subject: [PATCH 34/38] clean up comments --- lib/system.rb | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/system.rb b/lib/system.rb index 26cae5730..6c06e8060 100644 --- a/lib/system.rb +++ b/lib/system.rb @@ -92,12 +92,13 @@ def create_hotelblock(start_date:, end_date:, hb_rooms:, discount_rate:) hb_rooms.map! { |room_id| find_room(room_id) } if hb_rooms.length > 5 - raise ArgumentError, 'A block can contain a maximum of 5 rooms.' + raise ArgumentError, 'A block can only contain a maximum of 5 rooms.' end available_rooms = find_all_available_rooms(start_date, end_date) - # Does block contain a room that is not part of available rooms? + # Change hb_rooms into a array of boolean values for whether they are available + # Does the array contain false (room is not available)? # If so raise an error. if hb_rooms.map{ |room| available_rooms.include? room}.include? false raise ArgumentError, 'Block contains room that is already booked.' @@ -134,7 +135,7 @@ def reserve_from_block(hb_id, room_id) hotel_block = @hotelblocks[hb_id].find {|hb| hb.room.id == room_id} hotel_block.change_status - # Reservation is made using hotel block dates and cost. + # Reservation is hardcoded to use hotel block dates and cost. id = @reservations.length + 1 room = find_room(room_id) start_date = hotel_block.start_date From 9e6cbd3230f0ff9dd870bbf536819a36c27ea15f Mon Sep 17 00:00:00 2001 From: Tiffany Chio Date: Sun, 8 Sep 2019 21:50:08 -0700 Subject: [PATCH 35/38] added refactor.txt file --- lib/system.rb | 2 +- refactors.txt | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 refactors.txt diff --git a/lib/system.rb b/lib/system.rb index 6c06e8060..2ccbf5fdc 100644 --- a/lib/system.rb +++ b/lib/system.rb @@ -87,8 +87,8 @@ def list_reservations_for(date) end end - # hb_rooms is an array of room_ids def create_hotelblock(start_date:, end_date:, hb_rooms:, discount_rate:) + # hb_rooms is an array of room_ids hb_rooms.map! { |room_id| find_room(room_id) } if hb_rooms.length > 5 diff --git a/refactors.txt b/refactors.txt new file mode 100644 index 000000000..50d75b2a9 --- /dev/null +++ b/refactors.txt @@ -0,0 +1,7 @@ +Possible future changes: +- limit reservations to 30 days at a time maximum to discourage users from making long reservations + - include validation for date ranges to throw error if date range is over 30 days + +- change Hotel::HotelBlock to simply Hotel::Block + - cmd + shift + F for HotelBlock and hotel_block across all files + - change delete hotel or hotel_ (case insensitive) From 25aa0fa365be83d5feb376405b554179092a44d5 Mon Sep 17 00:00:00 2001 From: Tiffany Chio Date: Sun, 8 Sep 2019 22:00:52 -0700 Subject: [PATCH 36/38] added ideas to refactor.txt --- refactors.txt | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/refactors.txt b/refactors.txt index 50d75b2a9..97ec80681 100644 --- a/refactors.txt +++ b/refactors.txt @@ -5,3 +5,15 @@ Possible future changes: - change Hotel::HotelBlock to simply Hotel::Block - cmd + shift + F for HotelBlock and hotel_block across all files - change delete hotel or hotel_ (case insensitive) + +Possible but improbable future changes: +- remove room and date class + - requires rewriting the entire code + - reduces dependencies + - however, will shift most of the responsibilities to system class + - difficult to flesh out concrete ideas + - room class was created to allow for easier refactoring if room pricing structure changed + - date class allows for more readable code by creating another database that acts as a calendar + - tracking room availability can be done by iterating through all reservations & hotel blocks to look for date matches + - if match, mark the room as unavailable + - or if match, add the reservation to an array to be returned after iteration complete From 5f1c7b315f794b22c7be49342432576823bb20d8 Mon Sep 17 00:00:00 2001 From: Tiffany Chio Date: Thu, 26 Sep 2019 13:36:04 -0500 Subject: [PATCH 37/38] completed design_activity.md --- design-activity.md | 69 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 design-activity.md diff --git a/design-activity.md b/design-activity.md new file mode 100644 index 000000000..a1032cad5 --- /dev/null +++ b/design-activity.md @@ -0,0 +1,69 @@ +What classes does each implementation include? Are the lists the same? + +The three classes are CartEntry, ShoppingCart, and Order. Both implementations hold the same classes. + + + + +Write down a sentence to describe each class. + +CartEntry holds information about an item that has been added to a ShoppingCart. ShoppingCarts holds an array of CartEntries. Order contains a ShoppingCart and calculates the final price of the order. + + + + +How do the classes relate to each other? It might be helpful to draw a diagram on a whiteboard or piece of paper. + +All three classes have a compositional relationship to each other. ShoppingCarts holds an array of CartEntries. Order holds a ShoppingCart. + + + + +What data does each class store? How (if at all) does this differ between the two implementations? + +The data stored in each class of both implementations are the same. CartEntry stores information on net price and quantity. ShoppingCart stores information on entries. And Order stores a ShoppingCart and the sales tax. + + + + +What methods does each class have? How (if at all) does this differ between the two implementations? + +In Implementation A, CartEntry and ShoppingCart do not contain any methods. Order contains the method total price. + +In Implementation B, CartEntry contains the price method, ShoppingCart contains a different price method, and Order contains the total price method. + + + + +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 the logic to compute price is all within the Order class. In Implementation B, the logic is delegated between all classes. + + + + +Does total_price directly manipulate the instance variables of other classes? +total_price reads the instance variables of other classes but it doesn't write to them. + + + + +If we decide items are cheaper if bought in bulk, how would this change the code? Which implementation is easier to modify? +I would need more information on the logic behind bulk pricing. If it's the same discount for all products once they hit a certain quantity limit then it could go into wherever the price calculations methods are as a percent multiplier if quantity > bulk_quantity_min. Or if the discount differs from product to product, it could be stored as one or more instance variables under CartEntry (e.g. bulk_price, bulk_quantity_min, or discount). + +Implementation B would be easier to modify. + + + + + +Which implementation better adheres to the single responsibility principle? + +Implementation B. + + + + + From 78862b7b33d1fa284f07798efcb7e259339daaeb Mon Sep 17 00:00:00 2001 From: Tiffany Chio Date: Thu, 26 Sep 2019 16:30:52 -0500 Subject: [PATCH 38/38] made changes to more loosely couple objects in hotel --- design-activity.md | 8 ++++++++ lib/hotelblock.rb | 5 +++++ lib/system.rb | 11 +++++------ 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/design-activity.md b/design-activity.md index a1032cad5..08c645514 100644 --- a/design-activity.md +++ b/design-activity.md @@ -67,3 +67,11 @@ Implementation B. + +Revisiting Hotel +In my previous code, System calls on some of HotelBlock's instance variables to make a new reservation. Instead, I will now change the code so that HotelBlock makes the reservation and return the reservation to be stored in HotelBlock. + + + + + diff --git a/lib/hotelblock.rb b/lib/hotelblock.rb index fd16443dc..e08ee44ac 100644 --- a/lib/hotelblock.rb +++ b/lib/hotelblock.rb @@ -16,6 +16,11 @@ def initialize(id:, room:, start_date:, end_date:, cost:) def change_status @status = :UNAVAILABLE end + + def make_res_from_hb(reservation_id) + new_reservation = Hotel::Reservation.new(id: reservation_id, room: @room, start_date: @start_date, end_date: @end_date, cost: @cost) + return new_reservation + end end end diff --git a/lib/system.rb b/lib/system.rb index 2ccbf5fdc..9276ee357 100644 --- a/lib/system.rb +++ b/lib/system.rb @@ -14,6 +14,9 @@ class System def initialize @rooms = Hotel::Room.generate_rooms @reservations = [] + + # Hotel::Date objects hold information on reservations and hotel blocks + # made on a specific date @dates = [] # Keys will be hotel block ids (shared by all rooms within block). @@ -136,13 +139,9 @@ def reserve_from_block(hb_id, room_id) hotel_block.change_status # Reservation is hardcoded to use hotel block dates and cost. - id = @reservations.length + 1 - room = find_room(room_id) - start_date = hotel_block.start_date - end_date = hotel_block.end_date - cost = hotel_block.cost + reservation_id = @reservations.length + 1 - new_reservation = Hotel::Reservation.new(id: id, room: room, start_date: start_date, end_date: end_date, cost: cost) + new_reservation = hotel_block.make_res_from_hb(reservation_id) @reservations << new_reservation end