From b1eb7b4c3e5f464a31be799501aa08b3cef79054 Mon Sep 17 00:00:00 2001 From: Jocelyn Wang Date: Mon, 2 Mar 2020 08:55:00 -0800 Subject: [PATCH 01/43] created new reservation class and test for initialization, tested with guard --- lib/reservation.rb | 6 ++++++ test/reservation_test.rb | 13 +++++++++++++ test/test_helper.rb | 1 + 3 files changed, 20 insertions(+) create mode 100644 lib/reservation.rb create mode 100644 test/reservation_test.rb diff --git a/lib/reservation.rb b/lib/reservation.rb new file mode 100644 index 000000000..658032948 --- /dev/null +++ b/lib/reservation.rb @@ -0,0 +1,6 @@ + +module HotelManager + class Reservation + + end +end diff --git a/test/reservation_test.rb b/test/reservation_test.rb new file mode 100644 index 000000000..a6c8576c7 --- /dev/null +++ b/test/reservation_test.rb @@ -0,0 +1,13 @@ +require_relative 'test_helper' + +describe "Reservation Class" do + describe "initialize" do + before do + @reservation = HotelManager::Reservation.new() + end + + it "is an instance of Reservation" do + expect(@reservation).must_be_kind_of HotelManager::Reservation + end + end +end \ No newline at end of file diff --git a/test/test_helper.rb b/test/test_helper.rb index c3a7695cf..86338b3da 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -6,3 +6,4 @@ Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new # require_relative your lib files here! +require_relative '../lib/reservation.rb' \ No newline at end of file From 3cdf2cc591a830e94018cfaa298ea32d1ff9dc36 Mon Sep 17 00:00:00 2001 From: Jocelyn Wang Date: Mon, 2 Mar 2020 09:42:17 -0800 Subject: [PATCH 02/43] Add coverage directory to .gitignore file and psuedocode --- lib/hotel.rb | 11 +++++++++++ lib/reservation.rb | 9 +++++++++ lib/reservation_manager.rb | 15 +++++++++++++++ lib/room.rb | 11 +++++++++++ test/reservation_test.rb | 4 ++++ test/test_helper.rb | 6 +++++- 6 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 lib/hotel.rb create mode 100644 lib/reservation_manager.rb create mode 100644 lib/room.rb diff --git a/lib/hotel.rb b/lib/hotel.rb new file mode 100644 index 000000000..a0926d4ab --- /dev/null +++ b/lib/hotel.rb @@ -0,0 +1,11 @@ +module HotelManager + class Hotel + + # Initialize create 20 rooms + + # List out all rooms in hotel + + # + + end +end diff --git a/lib/reservation.rb b/lib/reservation.rb index 658032948..6dd57a8c8 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -1,6 +1,15 @@ +require_relative 'hotel' module HotelManager class Reservation + # Initialize reservation with start/end date, cost (default $200), customer id + # Raise exception for invalid date range is provided + + + # Calculate cost of reservation (exclusive of last date) + + # Choose first available room for reservation + end end diff --git a/lib/reservation_manager.rb b/lib/reservation_manager.rb new file mode 100644 index 000000000..e5167fd2a --- /dev/null +++ b/lib/reservation_manager.rb @@ -0,0 +1,15 @@ +require_relative 'reservation' + +module HotelManager + class ReservationManager + + # Load initial reservation data to variable + + # List out all rooms in hotel (call method from hotel) + + # List out all reservations by room and date range + + # List out reservations by specific date + + end +end diff --git a/lib/room.rb b/lib/room.rb new file mode 100644 index 000000000..76282901d --- /dev/null +++ b/lib/room.rb @@ -0,0 +1,11 @@ +module HotelManager + class Room + + # Create new room with attributes: number, status + + # + + # + + end +end diff --git a/test/reservation_test.rb b/test/reservation_test.rb index a6c8576c7..f3b429c63 100644 --- a/test/reservation_test.rb +++ b/test/reservation_test.rb @@ -9,5 +9,9 @@ it "is an instance of Reservation" do expect(@reservation).must_be_kind_of HotelManager::Reservation end + + # Raises argument error if invalid date range + # Confirms that total cost is calculated correctly + # Validate that start/end date is a date class end end \ No newline at end of file diff --git a/test/test_helper.rb b/test/test_helper.rb index 86338b3da..316c13572 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,8 +1,12 @@ -# Add simplecov require "minitest" require "minitest/autorun" require "minitest/reporters" +require 'simplecov' +SimpleCov.start do + add_filter 'test/' # Tests should not be checked for coverage. +end + Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new # require_relative your lib files here! From 98111fce766f885000a7f500fb43a2d0b4eacc47 Mon Sep 17 00:00:00 2001 From: Jocelyn Wang Date: Mon, 2 Mar 2020 11:54:08 -0800 Subject: [PATCH 03/43] Add coverage directory to .gitignore file, second attempt --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 5e1422c9c..c0ac3dc53 100644 --- a/.gitignore +++ b/.gitignore @@ -48,3 +48,4 @@ build-iPhoneSimulator/ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this: .rvmrc +coverage From 5edd84721be62efac11e4d154cf5532d5920ce37 Mon Sep 17 00:00:00 2001 From: Jocelyn Wang Date: Mon, 2 Mar 2020 11:54:41 -0800 Subject: [PATCH 04/43] created initial tests and code for res class --- lib/reservation.rb | 35 ++++++++++++++++++++++++++++++ test/reservation_test.rb | 47 ++++++++++++++++++++++++++++++---------- test/test_helper.rb | 11 ++++++---- 3 files changed, 78 insertions(+), 15 deletions(-) diff --git a/lib/reservation.rb b/lib/reservation.rb index 6dd57a8c8..48a1cd922 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -1,13 +1,48 @@ +require 'date' +require 'pry' + require_relative 'hotel' module HotelManager class Reservation + attr_reader :id, :customer_id, :start_date, :end_date, :room_cost, :room # Initialize reservation with start/end date, cost (default $200), customer id # Raise exception for invalid date range is provided + def initialize ( + id: , + customer_id: , + start_date: , + end_date: , + room_cost: 200, + room: + ) + + @id = id + @customer_id = customer_id + @start_date = start_date + @end_date = end_date + @room_cost = room_cost + @room = room + + if @start_date.class() != Date + raise ArgumentError, "Start date #{@start_date} not valid" + end + if @end_date.class() != Date + raise ArgumentError, "End date #{@end_date} not valid" + end + + if @start_date >= @end_date + raise ArgumentError, "#{@start_date} is before #{@end_date} " + end + end # Calculate cost of reservation (exclusive of last date) + def total_cost + @room_cost * (@end_date - @start_date - 1) + end + # Choose first available room for reservation diff --git a/test/reservation_test.rb b/test/reservation_test.rb index f3b429c63..847a00e91 100644 --- a/test/reservation_test.rb +++ b/test/reservation_test.rb @@ -1,17 +1,42 @@ require_relative 'test_helper' describe "Reservation Class" do - describe "initialize" do - before do - @reservation = HotelManager::Reservation.new() - end + before do + @reservation_data = { + id: 1, + customer_id: 1, + start_date: Date.new(2020,3,2), + end_date: Date.new(2020,3,5), + room_cost: 200, + room: 1 + } + @reservation = HotelManager::Reservation.new(@reservation_data) + end + + it "is an instance of Reservation" do + expect(@reservation).must_be_kind_of HotelManager::Reservation + end + + it "validates that start/end date is a date class" do + expect(@reservation.end_date).must_be_kind_of Date + expect(@reservation.start_date).must_be_kind_of Date + end + + it "raises argument error if start is not date class" do + @reservation_data[:start_date] = "Janurary 5th, 2020" + expect{HotelManager::Reservation.new(@reservation_data)}.must_raise ArgumentError + end - it "is an instance of Reservation" do - expect(@reservation).must_be_kind_of HotelManager::Reservation - end + it "raises argument error if end before start date" do + + @reservation_data[:start_date] = Date.new(2020,3,5) + @reservation_data[:end_date] = Date.new(2020,3,2) + expect{HotelManager::Reservation.new(@reservation_data)}.must_raise ArgumentError + end - # Raises argument error if invalid date range - # Confirms that total cost is calculated correctly - # Validate that start/end date is a date class + it "calculates total cost correctly" do + reservation_cost = @reservation.total_cost() + expect(reservation_cost).must_equal 400 end -end \ No newline at end of file + # How to calculate total cost if start and end date are the same +end diff --git a/test/test_helper.rb b/test/test_helper.rb index 316c13572..4c39724c0 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,12 +1,15 @@ -require "minitest" -require "minitest/autorun" -require "minitest/reporters" - require 'simplecov' SimpleCov.start do add_filter 'test/' # Tests should not be checked for coverage. end +require "minitest" +require "minitest/autorun" +require "minitest/reporters" +require "date" +require "pry" + + Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new # require_relative your lib files here! From a580e7d0c6e4bc9136ba08ba503256904e92fa62 Mon Sep 17 00:00:00 2001 From: Jocelyn Wang Date: Mon, 2 Mar 2020 12:05:56 -0800 Subject: [PATCH 05/43] added initial room class and tests --- lib/room.rb | 10 +++++----- test/room_test.rb | 18 ++++++++++++++++++ test/test_helper.rb | 3 ++- 3 files changed, 25 insertions(+), 6 deletions(-) create mode 100644 test/room_test.rb diff --git a/lib/room.rb b/lib/room.rb index 76282901d..733649b21 100644 --- a/lib/room.rb +++ b/lib/room.rb @@ -1,11 +1,11 @@ module HotelManager class Room + attr_reader # Create new room with attributes: number, status - - # - - # + def initialize (id: ) + @id = id + end end -end +end \ No newline at end of file diff --git a/test/room_test.rb b/test/room_test.rb new file mode 100644 index 000000000..e64a334c9 --- /dev/null +++ b/test/room_test.rb @@ -0,0 +1,18 @@ +require_relative 'test_helper' + +describe "Room Test" do + before do + @room_data = { + id: 1, + } + @room = HotelManager::Room.new(@room_data) + end + + it "is an instance of Room "do + expect(@room).must_be_kind_of HotelManager::Room + end +end + +# +# able to instantiate with reservation? +# able to instantiate without reservation? diff --git a/test/test_helper.rb b/test/test_helper.rb index 4c39724c0..cde234fe7 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -13,4 +13,5 @@ Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new # require_relative your lib files here! -require_relative '../lib/reservation.rb' \ No newline at end of file +require_relative '../lib/reservation.rb' +require_relative '../lib/room.rb' \ No newline at end of file From 3da32e86b9d4663c33cf989ca5b6b80f5e22ca1f Mon Sep 17 00:00:00 2001 From: Jocelyn Wang Date: Mon, 2 Mar 2020 14:37:13 -0800 Subject: [PATCH 06/43] added in reservation manager class and related tests --- lib/reservation.rb | 15 +++++ lib/reservation_manager.rb | 62 +++++++++++++++++++- lib/room.rb | 2 +- test/reservation_manager_test.rb | 99 ++++++++++++++++++++++++++++++++ test/reservation_test.rb | 2 + test/test_helper.rb | 3 +- 6 files changed, 180 insertions(+), 3 deletions(-) create mode 100644 test/reservation_manager_test.rb diff --git a/lib/reservation.rb b/lib/reservation.rb index 48a1cd922..f77589090 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -43,6 +43,21 @@ def total_cost @room_cost * (@end_date - @start_date - 1) end + # Check whether date is within range + def check_date(search_date) + (search_date >= @start_date) && (search_date <= @end_date) + end + + # Check whether reservation is within range + def check_date_range(date_one, date_two) + if date_one < date_two + first_date, second_date = date_one, date_two + else + first_date, second_date = date_two, date_one + end + + (@end_date >= first_date) && (@end_date <= second_date) + end # Choose first available room for reservation diff --git a/lib/reservation_manager.rb b/lib/reservation_manager.rb index e5167fd2a..6bd14b674 100644 --- a/lib/reservation_manager.rb +++ b/lib/reservation_manager.rb @@ -1,15 +1,75 @@ +require 'pry' +require 'date' + require_relative 'reservation' +require_relative 'room' module HotelManager class ReservationManager + attr_reader :rooms, :reservations + # Load initial reservation data to variable + def initialize + @rooms = [] + @reservations = [] + + 20.times do |index| + @rooms << HotelManager::Room.new(id: index + 1) + end + end + + # Add/create new reservation + def add_reservation(reservation) + @reservations << reservation + end - # List out all rooms in hotel (call method from hotel) + # Create connection between room and reservation when new booking is made + + # List out all rooms in hotel + def rooms_list + @rooms.map {|room| "Room: #{room.id}"}.join(", ") + end # List out all reservations by room and date range + # Error handling if date is not entered properly, or date range is invalid (store in master class?) + def search_by_room_date(room, first_date, second_date) + + if first_date.class != Date || second_date.class != Date + raise ArgumentError, "One date (#{first_date} or #{second_date}) is not valid" + end + + if @rooms.last.id < room + raise ArgumentError, "Room #{room} does not exist" + end + + reservation_by_room_date = [] + @reservations.each do |reservation| + if reservation.check_date_range(first_date, second_date) && reservation.room == room + reservation_by_room_date << reservation + end + end + + if reservation_by_room_date.empty? + return "No reservations found within date range." + else + return reservation_by_room_date + end + end # List out reservations by specific date + def search_by_date(date) + reservation_by_date = [] + @reservations.each do |reservation| + reservation_by_date << reservation if reservation.check_date(date) + end + + if reservation_by_date.empty? + return "No reservations found within date range." + else + return reservation_by_date + end + end end end diff --git a/lib/room.rb b/lib/room.rb index 733649b21..c353d0215 100644 --- a/lib/room.rb +++ b/lib/room.rb @@ -1,7 +1,7 @@ module HotelManager class Room - attr_reader + attr_reader :id # Create new room with attributes: number, status def initialize (id: ) @id = id diff --git a/test/reservation_manager_test.rb b/test/reservation_manager_test.rb new file mode 100644 index 000000000..4e07f0350 --- /dev/null +++ b/test/reservation_manager_test.rb @@ -0,0 +1,99 @@ +require_relative 'test_helper' + +describe "Reservation Manager Class" do + before do + @sample = HotelManager::ReservationManager.new() + end + + it "is an instance of Reservation" do + expect(@sample).must_be_kind_of HotelManager::ReservationManager + expect(@sample.rooms).must_be_kind_of Array + expect(@sample.reservations).must_be_kind_of Array + end + + it "confirm that hotel contains 20 rooms" do + expect(@sample.rooms.length).must_equal 20 + end + + it "able to list out all rooms in the hotel" do + expect(@sample.rooms_list()).must_be_kind_of String + expect(@sample.rooms_list()).must_include "Room: 1, Room: 2, Room: 3" + expect(@sample.rooms_list()).must_include "Room: 19, Room: 20" + end +end + +describe "Reservation Manager Class - Manipulation" do + before do + @res_one_data = { + id: 1, + customer_id: 1, + start_date: Date.new(2020,3,2), + end_date: Date.new(2020,3,5), + room_cost: 200, + room: 1 + } + @res_two_data = { + id: 2, + customer_id: 2, + start_date: Date.new(2020,3,15), + end_date: Date.new(2020,3,24), + room_cost: 200, + room: 1 + } + @res_one = HotelManager::Reservation.new(@res_one_data) + @res_two = HotelManager::Reservation.new(@res_two_data) + @sample_two = HotelManager::ReservationManager.new() + + @sample_two.add_reservation(@res_one) + @sample_two.add_reservation(@res_two) + end + + it "able to add reservations to Reservation Manager" do + expect(@sample_two.reservations.length).must_equal 2 + end + + describe "test search by room AND date range" do + it "able to list reservation by room/date range" do + @search_result = @sample_two.search_by_room_date(1, Date.new(2020,3,1), Date.new(2020,3,10)) + expect(@search_result).must_be_kind_of Array + expect(@search_result[0]).must_be_kind_of HotelManager::Reservation + expect(@search_result[0].id).must_equal 1 + end + + it "raise argument error if no room by room/date range" do + @search_result = @sample_two.search_by_room_date(1, Date.new(2020,4,1), Date.new(2020,4,10)) + expect(@search_result).must_include "No reservations found within date range." + end + + it "raise argument error if invalid room or date range" do + expect{@sample_two.search_by_room_date(30, Date.new(2020,3,1), Date.new(2020,3,10))}.must_raise ArgumentError + expect{@sample_two.search_by_room_date(1, "Date.new(2020,3,1)", Date.new(2020,3,10))}.must_raise ArgumentError + end + end + + + describe "test search by date" do + it "able to list reservation by room/date range" do + + @search_result = @sample_two.search_by_date(Date.new(2020,3,17)) + expect(@search_result).must_be_kind_of Array + expect(@search_result[0]).must_be_kind_of HotelManager::Reservation + expect(@search_result[0].id).must_equal 2 + end + + it "raise argument error if no reservations on that date" do + @search_result = @sample_two.search_by_date(Date.new(2020,4,1)) + expect(@search_result).must_include "No reservations found within date range." + end + + it "raise argument error if invalid date entered" do + expect{@sample_two.search_by_date("Date.new(2020,3,1)")}.must_raise ArgumentError + end + end + +end + +# +# +# access list of reservations for specific date +# call on total cost for givien reservation (from reservation class) \ No newline at end of file diff --git a/test/reservation_test.rb b/test/reservation_test.rb index 847a00e91..5fd309642 100644 --- a/test/reservation_test.rb +++ b/test/reservation_test.rb @@ -40,3 +40,5 @@ end # How to calculate total cost if start and end date are the same end + +# Edge case - date entered is > 31 days per month or leap year \ No newline at end of file diff --git a/test/test_helper.rb b/test/test_helper.rb index cde234fe7..8868ad4f2 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -14,4 +14,5 @@ # require_relative your lib files here! require_relative '../lib/reservation.rb' -require_relative '../lib/room.rb' \ No newline at end of file +require_relative '../lib/room.rb' +require_relative '../lib/reservation_manager.rb' \ No newline at end of file From e87d6508a1fceeb432ec85fef7999abf3f09f267 Mon Sep 17 00:00:00 2001 From: Jocelyn Wang Date: Tue, 3 Mar 2020 09:12:31 -0800 Subject: [PATCH 07/43] implemented search by date range function and error checking for room booking --- lib/reservation.rb | 21 +++++++--- lib/reservation_manager.rb | 36 ++++++++++++---- test/reservation_manager_test.rb | 71 +++++++++++++++++++++++++++----- test/reservation_test.rb | 7 ++-- 4 files changed, 109 insertions(+), 26 deletions(-) diff --git a/lib/reservation.rb b/lib/reservation.rb index f77589090..422a29516 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -6,7 +6,7 @@ module HotelManager class Reservation - attr_reader :id, :customer_id, :start_date, :end_date, :room_cost, :room + attr_reader :id, :customer_id, :start_date, :end_date, :room_cost, :room, :room_id # Initialize reservation with start/end date, cost (default $200), customer id # Raise exception for invalid date range is provided def initialize ( @@ -15,7 +15,8 @@ def initialize ( start_date: , end_date: , room_cost: 200, - room: + room: nil, + room_id: nil ) @id = id @@ -25,6 +26,15 @@ def initialize ( @room_cost = room_cost @room = room + if room + @room = room + @room_id = room_id + elsif + @room_id = room_id + else + raise ArgumentError, "Room or room_id is required" + end + if @start_date.class() != Date raise ArgumentError, "Start date #{@start_date} not valid" end @@ -56,10 +66,11 @@ def check_date_range(date_one, date_two) first_date, second_date = date_two, date_one end - (@end_date >= first_date) && (@end_date <= second_date) + (@end_date > first_date) && (@end_date <= second_date) || + (@start_date >= first_date) && (@start_date <= second_date) end - # Choose first available room for reservation - end end + +# Add in requirement that room must be populated \ No newline at end of file diff --git a/lib/reservation_manager.rb b/lib/reservation_manager.rb index 6bd14b674..7a9604a01 100644 --- a/lib/reservation_manager.rb +++ b/lib/reservation_manager.rb @@ -21,10 +21,18 @@ def initialize # Add/create new reservation def add_reservation(reservation) - @reservations << reservation + potential_rooms = self.list_room_by_range(reservation.start_date, reservation.end_date) + + if potential_rooms.include? find_room(reservation.room_id) + @reservations << reservation + else + raise ArgumentError, "Room #{reservation.room_id} has been double booked. Reservation not finalized in reservation manager." + end end - # Create connection between room and reservation when new booking is made + def find_room(id) + return @rooms.find { |room| room.id == id } + end # List out all rooms in hotel def rooms_list @@ -45,7 +53,7 @@ def search_by_room_date(room, first_date, second_date) reservation_by_room_date = [] @reservations.each do |reservation| - if reservation.check_date_range(first_date, second_date) && reservation.room == room + if reservation.check_date_range(first_date, second_date) && reservation.room_id == room reservation_by_room_date << reservation end end @@ -64,12 +72,24 @@ def search_by_date(date) reservation_by_date << reservation if reservation.check_date(date) end - if reservation_by_date.empty? - return "No reservations found within date range." - else - return reservation_by_date - end + return reservation_by_date.empty? ? "No reservations found within date range." : reservation_by_date end + def list_room_by_range(first_date, second_date) + + if first_date.class != Date || second_date.class != Date + raise ArgumentError, "One date (#{first_date} or #{second_date}) is not valid" + end + + available_rooms = @rooms.dup + + @reservations.each do |reservation| + if reservation.check_date_range(first_date, second_date) + available_rooms -= [find_room(reservation.room_id)] + end + end + + return available_rooms.empty? ? "No rooms available in this date range." : available_rooms + end end end diff --git a/test/reservation_manager_test.rb b/test/reservation_manager_test.rb index 4e07f0350..2dda0c1dc 100644 --- a/test/reservation_manager_test.rb +++ b/test/reservation_manager_test.rb @@ -30,7 +30,7 @@ start_date: Date.new(2020,3,2), end_date: Date.new(2020,3,5), room_cost: 200, - room: 1 + room_id: 1 } @res_two_data = { id: 2, @@ -38,7 +38,7 @@ start_date: Date.new(2020,3,15), end_date: Date.new(2020,3,24), room_cost: 200, - room: 1 + room_id: 1 } @res_one = HotelManager::Reservation.new(@res_one_data) @res_two = HotelManager::Reservation.new(@res_two_data) @@ -48,8 +48,42 @@ @sample_two.add_reservation(@res_two) end - it "able to add reservations to Reservation Manager" do - expect(@sample_two.reservations.length).must_equal 2 + describe "ability to add reservations to the Reservation Manager" do + it "able to add reservations to Reservation Manager" do + expect(@sample_two.reservations.length).must_equal 2 + end + + it "open room for reservation after it's end date" do + @res_four_data = { + id: 4, + customer_id: 4, + start_date: Date.new(2020,3,24), + end_date: Date.new(2020,3,28), + room_cost: 200, + room_id: 1 + } + + @res_four = HotelManager::Reservation.new(@res_four_data) + + @sample_two.add_reservation(@res_four) + + + expect(@sample_two.reservations.length).must_equal 3 + end + + it "raise argument error if overbooking attempted" do + @res_five_data = { + id: 5, + customer_id: 5, + start_date: Date.new(2020,3,23), + end_date: Date.new(2020,3,28), + room_cost: 200, + room_id: 1 + } + + @res_five = HotelManager::Reservation.new(@res_five_data) + expect{@sample_two.add_reservation(@res_five)}.must_raise ArgumentError + end end describe "test search by room AND date range" do @@ -73,8 +107,7 @@ describe "test search by date" do - it "able to list reservation by room/date range" do - + it "able to list reservation by specific date" do @search_result = @sample_two.search_by_date(Date.new(2020,3,17)) expect(@search_result).must_be_kind_of Array expect(@search_result[0]).must_be_kind_of HotelManager::Reservation @@ -91,9 +124,27 @@ end end + # see list of rooms not reserved for a given date range (see all available rooms for those days) <-- two date inputs + describe "list rooms available by date range" do + it "able to list out all available rooms by date range" do + @search_result = @sample_two.list_room_by_range(Date.new(2020,4,1),Date.new(2020,4,7)) + expect(@search_result).must_be_kind_of Array + expect(@search_result[0]).must_be_kind_of HotelManager::Room + expect(@search_result.length).must_equal 20 + end + + it "able to list out all available rooms by date range" do + @search_result = @sample_two.list_room_by_range(Date.new(2020,3,1),Date.new(2020,3,5)) + expect(@search_result).must_be_kind_of Array + expect(@search_result[0]).must_be_kind_of HotelManager::Room + expect(@search_result.length).must_equal 19 + end + + it "notify user if no rooms are available" do + #complete later, need to modify hotel room # to by dynamic (user input) + end + end end -# -# -# access list of reservations for specific date -# call on total cost for givien reservation (from reservation class) \ No newline at end of file + +# Raise exception if reservation is made on double booked room (user selects time and room) \ No newline at end of file diff --git a/test/reservation_test.rb b/test/reservation_test.rb index 5fd309642..08a9ac5a2 100644 --- a/test/reservation_test.rb +++ b/test/reservation_test.rb @@ -8,7 +8,7 @@ start_date: Date.new(2020,3,2), end_date: Date.new(2020,3,5), room_cost: 200, - room: 1 + room: HotelManager::Room.new(id: 1) } @reservation = HotelManager::Reservation.new(@reservation_data) end @@ -38,7 +38,8 @@ reservation_cost = @reservation.total_cost() expect(reservation_cost).must_equal 400 end - # How to calculate total cost if start and end date are the same + end -# Edge case - date entered is > 31 days per month or leap year \ No newline at end of file +# Edge case - date entered is > 31 days per month or leap year +# raise argument error if start and end date are the same \ No newline at end of file From 67ee9eb2596a3597e1eb92de0521a76c6129264b Mon Sep 17 00:00:00 2001 From: Jocelyn Wang Date: Tue, 3 Mar 2020 09:20:19 -0800 Subject: [PATCH 08/43] cleaned up some comments and spacing --- lib/reservation_manager.rb | 10 +++------- lib/room.rb | 2 +- test/reservation_manager_test.rb | 9 ++------- 3 files changed, 6 insertions(+), 15 deletions(-) diff --git a/lib/reservation_manager.rb b/lib/reservation_manager.rb index 7a9604a01..44f3be416 100644 --- a/lib/reservation_manager.rb +++ b/lib/reservation_manager.rb @@ -9,7 +9,7 @@ class ReservationManager attr_reader :rooms, :reservations - # Load initial reservation data to variable + # Populate hotel with 20 rooms upon initialization def initialize @rooms = [] @reservations = [] @@ -26,7 +26,7 @@ def add_reservation(reservation) if potential_rooms.include? find_room(reservation.room_id) @reservations << reservation else - raise ArgumentError, "Room #{reservation.room_id} has been double booked. Reservation not finalized in reservation manager." + raise ArgumentError, "Room #{reservation.room_id} is double booked. Reservation not finalized in reservation manager." end end @@ -58,11 +58,7 @@ def search_by_room_date(room, first_date, second_date) end end - if reservation_by_room_date.empty? - return "No reservations found within date range." - else - return reservation_by_room_date - end + return reservation_by_room_date.empty? ? "No reservations found within date range." : reservation_by_room_date end # List out reservations by specific date diff --git a/lib/room.rb b/lib/room.rb index c353d0215..3162fa76f 100644 --- a/lib/room.rb +++ b/lib/room.rb @@ -2,7 +2,7 @@ module HotelManager class Room attr_reader :id - # Create new room with attributes: number, status + def initialize (id: ) @id = id end diff --git a/test/reservation_manager_test.rb b/test/reservation_manager_test.rb index 2dda0c1dc..ed584c9e2 100644 --- a/test/reservation_manager_test.rb +++ b/test/reservation_manager_test.rb @@ -64,9 +64,7 @@ } @res_four = HotelManager::Reservation.new(@res_four_data) - @sample_two.add_reservation(@res_four) - expect(@sample_two.reservations.length).must_equal 3 end @@ -124,7 +122,7 @@ end end - # see list of rooms not reserved for a given date range (see all available rooms for those days) <-- two date inputs + # see list of rooms available for a given date range describe "list rooms available by date range" do it "able to list out all available rooms by date range" do @search_result = @sample_two.list_room_by_range(Date.new(2020,4,1),Date.new(2020,4,7)) @@ -144,7 +142,4 @@ #complete later, need to modify hotel room # to by dynamic (user input) end end -end - - -# Raise exception if reservation is made on double booked room (user selects time and room) \ No newline at end of file +end \ No newline at end of file From 72a1b8d4d6bc2cdb4fdcb73a047b97e84d56a9e0 Mon Sep 17 00:00:00 2001 From: Jocelyn Wang Date: Tue, 3 Mar 2020 10:04:08 -0800 Subject: [PATCH 09/43] wrote out pseudocode for wave 3 (hotel block) --- lib/reservation_manager.rb | 2 ++ test/reservation_test.rb | 17 ++++++++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/reservation_manager.rb b/lib/reservation_manager.rb index 44f3be416..1b7eed973 100644 --- a/lib/reservation_manager.rb +++ b/lib/reservation_manager.rb @@ -89,3 +89,5 @@ def list_room_by_range(first_date, second_date) end end end + +# diff --git a/test/reservation_test.rb b/test/reservation_test.rb index 08a9ac5a2..d98f463c2 100644 --- a/test/reservation_test.rb +++ b/test/reservation_test.rb @@ -42,4 +42,19 @@ end # Edge case - date entered is > 31 days per month or leap year -# raise argument error if start and end date are the same \ No newline at end of file +# raise argument error if start and end date are the same + +# Able to reserve a hotel block (group of rooms for a specific group of customers for a set period of time) +# Block reservation has more than one room +# Block reservation has less than five rooms +# Block reservation can be at a discount rate +# Block reservation cen be over a period of days +# List of available rooms will not include the rooms in block reservation +# Raise exception if one or more rooms is unavailable for the given date range +# Cannot double book a room for single reservation if it is reserved for a hotel block +# Cannot double book a room for another hotel block if it is reserved for a hotel block +# Correctly list out whether a given block has any rooms available? +# Able to reserve a specific room from a hotel block, but only for the full duration of the block (individual reservations still work?) +# List of reservation by date will list out both individual and block reservations +# Check to make sure that when a block is reserved, the indivudal room reservation dates will also be there +# Confirm that all availabilty checking logic from Wave 2 also respects room blocks \ No newline at end of file From f568610b99edd3081dcba79b0b27fcf32c248680 Mon Sep 17 00:00:00 2001 From: Jocelyn Wang Date: Tue, 3 Mar 2020 15:28:49 -0800 Subject: [PATCH 10/43] initial update to reservation manager to accomodate reservation blocks --- lib/reservation.rb | 7 +- lib/reservation_block.rb | 94 ++++++++++++ lib/reservation_manager.rb | 32 +++- test/reservation_block_test.rb | 81 ++++++++++ test/reservation_manager_test.rb | 256 ++++++++++++++++++++++--------- test/reservation_test.rb | 2 +- test/test_helper.rb | 7 +- 7 files changed, 400 insertions(+), 79 deletions(-) create mode 100644 lib/reservation_block.rb create mode 100644 test/reservation_block_test.rb diff --git a/lib/reservation.rb b/lib/reservation.rb index 422a29516..4936c4191 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -66,8 +66,11 @@ def check_date_range(date_one, date_two) first_date, second_date = date_two, date_one end - (@end_date > first_date) && (@end_date <= second_date) || - (@start_date >= first_date) && (@start_date <= second_date) + (first_date >= @start_date && first_date < @end_date) || (second_date < @end_date && second_date > @start_date) + + + # (@end_date > first_date) && (@end_date <= second_date) || + # (@start_date >= first_date) && (@start_date <= second_date) || (@start_date < first_date) && (@end_date <= second_date) end end diff --git a/lib/reservation_block.rb b/lib/reservation_block.rb new file mode 100644 index 000000000..706ae3aea --- /dev/null +++ b/lib/reservation_block.rb @@ -0,0 +1,94 @@ +require 'date' +require 'pry' + +require_relative 'hotel' +require_relative 'reservation' + +module HotelManager + class ReservationBlock < Reservation + + attr_reader :id, :customer_id, :start_date, :end_date, :room_cost, :rooms, :room_ids + # Initialize reservation with start/end date, cost (default $200), customer id + # Raise exception for invalid date range is provided + def initialize (id: , + customer_id: , + start_date: , + end_date: , + room_cost: 200, + rooms: nil, + room_ids: nil + ) + + @id = id + @customer_id = customer_id + @start_date = start_date + @end_date = end_date + @room_cost = room_cost + @rooms = rooms + + if rooms + @rooms = rooms + @room_ids = room_ids + if rooms.length <= 1 || rooms.length > 5 + raise ArgumentError, "#{rooms.length} is an invalid number of rooms for a hotel block" + end + elsif + @room_ids = room_ids + if room_ids.length <= 1 || room_ids.length > 5 + raise ArgumentError, "#{rooms.length} is an invalid number of rooms for a hotel block" + end + else + raise ArgumentError, "Rooms or room_ids is required" + end + + + # if room_ids.length <= 1 || room_ids.length > 5 + # raise ArgumentError, "#{rooms.length} is an invalid number of rooms for a hotel block" + # elsif rooms.length <= 1 || rooms.length > 5 + # raise ArgumentError, "#{rooms.length} is an invalid number of rooms for a hotel block" + # end + + # Move to method later to avoid needing to call here? + if @start_date.class() != Date + raise ArgumentError, "Start date #{@start_date} not valid" + end + + if @end_date.class() != Date + raise ArgumentError, "End date #{@end_date} not valid" + end + + if @start_date >= @end_date + raise ArgumentError, "#{@start_date} is before #{@end_date} " + end + end + + # Calculate cost of reservation (exclusive of last date) + def total_cost + @room_cost * (@end_date - @start_date - 1) * @rooms.length + end + + def check_date_range(date_one, date_two) + super + end + + # # Check whether date is within range + # def check_date(search_date) + # (search_date >= @start_date) && (search_date <= @end_date) + # end + + # # Check whether reservation is within range + # def check_date_range(date_one, date_two) + # if date_one < date_two + # first_date, second_date = date_one, date_two + # else + # first_date, second_date = date_two, date_one + # end + + # (@end_date > first_date) && (@end_date <= second_date) || + # (@start_date >= first_date) && (@start_date <= second_date) + # end + + end +end + +# Add in requirement that room must be populated \ No newline at end of file diff --git a/lib/reservation_manager.rb b/lib/reservation_manager.rb index 1b7eed973..f8f540d21 100644 --- a/lib/reservation_manager.rb +++ b/lib/reservation_manager.rb @@ -7,12 +7,13 @@ module HotelManager class ReservationManager - attr_reader :rooms, :reservations + attr_reader :rooms, :reservations, :reservation_blocks # Populate hotel with 20 rooms upon initialization def initialize @rooms = [] @reservations = [] + @reservation_blocks = [] 20.times do |index| @rooms << HotelManager::Room.new(id: index + 1) @@ -30,6 +31,25 @@ def add_reservation(reservation) end end + def find_room(id) + return @rooms.find { |room| room.id == id } + end + + # Add/create new block reservation + def add_reservation_block(reservation_block) + potential_rooms = self.list_room_by_range(reservation_block.start_date, reservation_block.end_date) + + found_room = reservation_block.room_ids.map do |room_id| + potential_rooms.include? find_room(room_id) + end.all? (true) + + if found_room + @reservation_blocks << reservation_block + else + raise ArgumentError, "Some of rooms #{reservation_block.room_ids} are double booked. Hotel block not finalized in reservation manager." + end + end + def find_room(id) return @rooms.find { |room| room.id == id } end @@ -80,11 +100,21 @@ def list_room_by_range(first_date, second_date) available_rooms = @rooms.dup @reservations.each do |reservation| + if reservation.check_date_range(first_date, second_date) available_rooms -= [find_room(reservation.room_id)] end end + @reservation_blocks.each do |reservation_block| + + if reservation_block.check_date_range(first_date, second_date) + reservation_block.room_ids.each do |room_id| + available_rooms -= [find_room(room_id)] + end + end + end + return available_rooms.empty? ? "No rooms available in this date range." : available_rooms end end diff --git a/test/reservation_block_test.rb b/test/reservation_block_test.rb new file mode 100644 index 000000000..058c86da6 --- /dev/null +++ b/test/reservation_block_test.rb @@ -0,0 +1,81 @@ +require_relative 'test_helper' + +describe "Reservation Block Class" do + before do + @rooms = [ + HotelManager::Room.new(id: 1), + HotelManager::Room.new(id: 2), + HotelManager::Room.new(id: 3), + HotelManager::Room.new(id: 4), + HotelManager::Room.new(id: 5) + ] + @reservation_data = { + id: 1, + customer_id: 1, + start_date: Date.new(2020,3,2), + end_date: Date.new(2020,3,8), + room_cost: 150, + rooms: @rooms + } + + @reservation = HotelManager::ReservationBlock.new(@reservation_data) + end + + describe "Reservation Block Class - Attributes" do + + it "is an instance of Reservation Block" do + expect(@reservation).must_be_kind_of HotelManager::ReservationBlock + expect(@reservation.rooms).must_be_kind_of Array + end + + it "raises argument if block only has one room" do + @rooms = [HotelManager::Room.new(id: 1)] + @reservation_data[:rooms] = @rooms + expect{HotelManager::ReservationBlock.new(@reservation_data)}.must_raise ArgumentError + end + + it "raises argument if block has more than 5 rooms" do + @rooms += [HotelManager::Room.new(id: 6)] + @reservation_data[:rooms] = @rooms + expect{HotelManager::ReservationBlock.new(@reservation_data)}.must_raise ArgumentError + end + + it "calculates total cost correctly" do + reservation_cost = @reservation.total_cost() + expect(reservation_cost).must_equal 3_750 + end + end + + describe "Reservation Block Class - Date Validation" do + + it "validates that start/end date is a date class" do + expect(@reservation.end_date).must_be_kind_of Date + expect(@reservation.start_date).must_be_kind_of Date + end + + it "raises argument error if start is not date class" do + @reservation_data[:start_date] = "Janurary 5th, 2020" + expect{HotelManager::ReservationBlock.new(@reservation_data)}.must_raise ArgumentError + end + + it "raises argument error if end before start date" do + @reservation_data[:start_date] = Date.new(2020,3,5) + @reservation_data[:end_date] = Date.new(2020,3,2) + expect{HotelManager::ReservationBlock.new(@reservation_data)}.must_raise ArgumentError + end + + it "raises argument error if start and end date are the same" do + @reservation_data[:start_date] = Date.new(2020,3,5) + @reservation_data[:end_date] = Date.new(2020,3,5) + expect{HotelManager::ReservationBlock.new(@reservation_data)}.must_raise ArgumentError + end + end +end + +# Edge case - date entered is > 31 days per month or leap year +# raise argument error if start and end date are the same + +# Able to reserve a hotel block (group of rooms for a specific group of customers for a set period of time) + + + diff --git a/test/reservation_manager_test.rb b/test/reservation_manager_test.rb index ed584c9e2..e75a3f1f3 100644 --- a/test/reservation_manager_test.rb +++ b/test/reservation_manager_test.rb @@ -1,26 +1,27 @@ require_relative 'test_helper' -describe "Reservation Manager Class" do - before do - @sample = HotelManager::ReservationManager.new() - end +# describe "Reservation Manager Class" do +# before do +# @sample = HotelManager::ReservationManager.new() +# end - it "is an instance of Reservation" do - expect(@sample).must_be_kind_of HotelManager::ReservationManager - expect(@sample.rooms).must_be_kind_of Array - expect(@sample.reservations).must_be_kind_of Array - end +# it "is an instance of Reservation" do +# expect(@sample).must_be_kind_of HotelManager::ReservationManager +# expect(@sample.rooms).must_be_kind_of Array +# expect(@sample.reservations).must_be_kind_of Array +# expect(@sample.block_reservations).must_be_kind_of Array +# end - it "confirm that hotel contains 20 rooms" do - expect(@sample.rooms.length).must_equal 20 - end +# it "confirm that hotel contains 20 rooms" do +# expect(@sample.rooms.length).must_equal 20 +# end - it "able to list out all rooms in the hotel" do - expect(@sample.rooms_list()).must_be_kind_of String - expect(@sample.rooms_list()).must_include "Room: 1, Room: 2, Room: 3" - expect(@sample.rooms_list()).must_include "Room: 19, Room: 20" - end -end +# it "able to list out all rooms in the hotel" do +# expect(@sample.rooms_list()).must_be_kind_of String +# expect(@sample.rooms_list()).must_include "Room: 1, Room: 2, Room: 3" +# expect(@sample.rooms_list()).must_include "Room: 19, Room: 20" +# end +# end describe "Reservation Manager Class - Manipulation" do before do @@ -40,20 +41,51 @@ room_cost: 200, room_id: 1 } + @rooms = [7, 8, 9] + @res_block_data = { + id: 3, + customer_id: 3, + start_date: Date.new(2020,3,2), + end_date: Date.new(2020,3,18), + room_cost: 100, + room_ids: @rooms + } + @res_one = HotelManager::Reservation.new(@res_one_data) @res_two = HotelManager::Reservation.new(@res_two_data) + + @res_block = HotelManager::ReservationBlock.new(@res_block_data) @sample_two = HotelManager::ReservationManager.new() - @sample_two.add_reservation(@res_one) + @sample_two.add_reservation(@res_one) + @sample_two.add_reservation(@res_two) + @sample_two.add_reservation_block(@res_block) end describe "ability to add reservations to the Reservation Manager" do it "able to add reservations to Reservation Manager" do expect(@sample_two.reservations.length).must_equal 2 + expect(@sample_two.reservation_blocks.length).must_equal 1 + end + + it "room available after other block end date - block" do + @res_three_data = { + id: 3, + customer_id: 3, + start_date: Date.new(2020,3,24), + end_date: Date.new(2020,3,30), + room_cost: 100, + room_ids: @rooms + } + + @res_three = HotelManager::ReservationBlock.new(@res_three_data) + @sample_two.add_reservation_block(@res_three) + + expect(@sample_two.reservations.length).must_equal 2 end - it "open room for reservation after it's end date" do + it "room available after other individual end date - individual" do @res_four_data = { id: 4, customer_id: 4, @@ -69,7 +101,40 @@ expect(@sample_two.reservations.length).must_equal 3 end - it "raise argument error if overbooking attempted" do + it "room available after individual end date - block" do + @rooms = [1,2] + @res_six_data = { + id: 3, + customer_id: 3, + start_date: Date.new(2020,3,5), + end_date: Date.new(2020,3,14), + room_cost: 100, + room_ids: @rooms + } + + @res_six = HotelManager::ReservationBlock.new(@res_six_data) + @sample_two.add_reservation_block(@res_six) + + expect(@sample_two.reservations.length).must_equal 2 + end + + it "room available after block end date - individual" do + @res_seven_data = { + id: 7, + customer_id: 7, + start_date: Date.new(2020,3,18), + end_date: Date.new(2020,3,25), + room_cost: 200, + room_id: 7 + } + + @res_seven = HotelManager::Reservation.new(@res_seven_data) + @sample_two.add_reservation(@res_seven) + + expect(@sample_two.reservations.length).must_equal 3 + end + + it "raise argument error if individual overbooking attempted on individual" do @res_five_data = { id: 5, customer_id: 5, @@ -80,66 +145,113 @@ } @res_five = HotelManager::Reservation.new(@res_five_data) - expect{@sample_two.add_reservation(@res_five)}.must_raise ArgumentError - end - end + expect{@sample_two.add_reservation(@res_five)}.must_raise ArgumentError + end - describe "test search by room AND date range" do - it "able to list reservation by room/date range" do - @search_result = @sample_two.search_by_room_date(1, Date.new(2020,3,1), Date.new(2020,3,10)) - expect(@search_result).must_be_kind_of Array - expect(@search_result[0]).must_be_kind_of HotelManager::Reservation - expect(@search_result[0].id).must_equal 1 + it "raise argument error if individual overbooking attempted on block" do + @res_five_data = { + id: 5, + customer_id: 5, + start_date: Date.new(2020,3,4), + end_date: Date.new(2020,3,10), + room_cost: 200, + room_id: 7 + } + + @res_five = HotelManager::Reservation.new(@res_five_data) + + expect{@sample_two.add_reservation(@res_five)}.must_raise ArgumentError end - it "raise argument error if no room by room/date range" do - @search_result = @sample_two.search_by_room_date(1, Date.new(2020,4,1), Date.new(2020,4,10)) - expect(@search_result).must_include "No reservations found within date range." + it "raise argument error if block overbooking attempted on block" do + @res_five = HotelManager::ReservationBlock.new(@res_block_data) + expect{@sample_two.add_reservation_block(@res_five)}.must_raise ArgumentError end - it "raise argument error if invalid room or date range" do - expect{@sample_two.search_by_room_date(30, Date.new(2020,3,1), Date.new(2020,3,10))}.must_raise ArgumentError - expect{@sample_two.search_by_room_date(1, "Date.new(2020,3,1)", Date.new(2020,3,10))}.must_raise ArgumentError - end + it "raise argument error if block overbooking attempted on individual" do + @rooms = [1,2] + @res_six_data = { + id: 3, + customer_id: 3, + start_date: Date.new(2020,3,9), + end_date: Date.new(2020,3,14), + room_cost: 100, + room_ids: @rooms + } + + @res_six = HotelManager::ReservationBlock.new(@res_six_data) + expect{@sample_two.add_reservation(@res_six)}.must_raise ArgumentError + end end - - describe "test search by date" do - it "able to list reservation by specific date" do - @search_result = @sample_two.search_by_date(Date.new(2020,3,17)) - expect(@search_result).must_be_kind_of Array - expect(@search_result[0]).must_be_kind_of HotelManager::Reservation - expect(@search_result[0].id).must_equal 2 - end + # describe "test search by room AND date range" do + # it "able to list reservation by room/date range" do + # @search_result = @sample_two.search_by_room_date(1, Date.new(2020,3,1), Date.new(2020,3,10)) + # expect(@search_result).must_be_kind_of Array + # expect(@search_result[0]).must_be_kind_of HotelManager::Reservation + # expect(@search_result[1]).must_be_kind_of HotelManager::ReservationBlock + # expect(@search_result[0].id).must_equal 1 + # expect(@search_result[1].id).must_equal 3 + # end - it "raise argument error if no reservations on that date" do - @search_result = @sample_two.search_by_date(Date.new(2020,4,1)) - expect(@search_result).must_include "No reservations found within date range." - end + # it "raise argument error if no room by room/date range" do + # @search_result = @sample_two.search_by_room_date(1, Date.new(2020,4,1), Date.new(2020,4,10)) + # expect(@search_result).must_include "No reservations found within date range." + # end - it "raise argument error if invalid date entered" do - expect{@sample_two.search_by_date("Date.new(2020,3,1)")}.must_raise ArgumentError - end - end + # it "raise argument error if invalid room or date range" do + # expect{@sample_two.search_by_room_date(30, Date.new(2020,3,1), Date.new(2020,3,10))}.must_raise ArgumentError + # expect{@sample_two.search_by_room_date(1, "Date.new(2020,3,1)", Date.new(2020,3,10))}.must_raise ArgumentError + # end + # end - # see list of rooms available for a given date range - describe "list rooms available by date range" do - it "able to list out all available rooms by date range" do - @search_result = @sample_two.list_room_by_range(Date.new(2020,4,1),Date.new(2020,4,7)) - expect(@search_result).must_be_kind_of Array - expect(@search_result[0]).must_be_kind_of HotelManager::Room - expect(@search_result.length).must_equal 20 - end + # describe "test search by date" do + # it "able to list reservation by specific date" do + # @search_result = @sample_two.search_by_date(Date.new(2020,3,17)) + # expect(@search_result).must_be_kind_of Array + # expect(@search_result[0]).must_be_kind_of HotelManager::ReservationBlock + # expect(@search_result[1]).must_be_kind_of HotelManager::Reservation + # expect(@search_result[0].id).must_equal 3 + # expect(@search_result[1].id).must_equal 2 + # end - it "able to list out all available rooms by date range" do - @search_result = @sample_two.list_room_by_range(Date.new(2020,3,1),Date.new(2020,3,5)) - expect(@search_result).must_be_kind_of Array - expect(@search_result[0]).must_be_kind_of HotelManager::Room - expect(@search_result.length).must_equal 19 - end + # it "raise argument error if no reservations on that date" do + # @search_result = @sample_two.search_by_date(Date.new(2020,4,1)) + # expect(@search_result).must_include "No reservations found within date range." + # end - it "notify user if no rooms are available" do - #complete later, need to modify hotel room # to by dynamic (user input) - end - end -end \ No newline at end of file + # it "raise argument error if invalid date entered" do + # expect{@sample_two.search_by_date("Date.new(2020,3,1)")}.must_raise ArgumentError + # end + # end + + # # see list of rooms available for a given date range + # describe "list rooms available by date range" do + # it "able to list out all available rooms by date range" do + # @search_result = @sample_two.list_room_by_range(Date.new(2020,4,1),Date.new(2020,4,7)) + # expect(@search_result).must_be_kind_of Array + # expect(@search_result.length).must_equal 20 + # end + + # it "able to list out all available rooms by date range" do + # @search_result = @sample_two.list_room_by_range(Date.new(2020,3,1),Date.new(2020,3,5)) + # expect(@search_result).must_be_kind_of Array + # expect(@search_result[0]).must_be_kind_of HotelManager::Room + # expect(@search_result.length).must_equal 15 + # end + + # it "notify user if no rooms are available" do + # #complete later, need to modify hotel room # to by dynamic (user input) + # end + # end +end + + + + +# List of available rooms will not include the rooms in block reservation +# Correctly list out whether a given block has any rooms available? +# Able to reserve a specific room from a hotel block, but only for the full duration of the block (individual reservations still work?) +# List of reservation by date will list out both individual and block reservations +# Check to make sure that when a block is reserved, the indivudal room reservation dates will also be there +# Confirm that all availabilty checking logic from Wave 2 also respects room blocks \ No newline at end of file diff --git a/test/reservation_test.rb b/test/reservation_test.rb index d98f463c2..2e00c95ee 100644 --- a/test/reservation_test.rb +++ b/test/reservation_test.rb @@ -38,7 +38,7 @@ reservation_cost = @reservation.total_cost() expect(reservation_cost).must_equal 400 end - + end # Edge case - date entered is > 31 days per month or leap year diff --git a/test/test_helper.rb b/test/test_helper.rb index 8868ad4f2..024d0a386 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -5,14 +5,15 @@ require "minitest" require "minitest/autorun" -require "minitest/reporters" +# require "minitest/reporters" require "date" require "pry" -Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new +# Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new # require_relative your lib files here! require_relative '../lib/reservation.rb' require_relative '../lib/room.rb' -require_relative '../lib/reservation_manager.rb' \ No newline at end of file +require_relative '../lib/reservation_manager.rb' +require_relative '../lib/reservation_block.rb' \ No newline at end of file From fd9cce8213502550719908ce68fae32721f28ef8 Mon Sep 17 00:00:00 2001 From: Jocelyn Wang Date: Tue, 3 Mar 2020 15:34:02 -0800 Subject: [PATCH 11/43] refactored some guard clauses --- lib/reservation_manager.rb | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/lib/reservation_manager.rb b/lib/reservation_manager.rb index f8f540d21..0aff5514e 100644 --- a/lib/reservation_manager.rb +++ b/lib/reservation_manager.rb @@ -65,11 +65,9 @@ def search_by_room_date(room, first_date, second_date) if first_date.class != Date || second_date.class != Date raise ArgumentError, "One date (#{first_date} or #{second_date}) is not valid" - end - - if @rooms.last.id < room + elsif @rooms.last.id < room raise ArgumentError, "Room #{room} does not exist" - end + end reservation_by_room_date = [] @reservations.each do |reservation| @@ -100,14 +98,12 @@ def list_room_by_range(first_date, second_date) available_rooms = @rooms.dup @reservations.each do |reservation| - if reservation.check_date_range(first_date, second_date) available_rooms -= [find_room(reservation.room_id)] end end @reservation_blocks.each do |reservation_block| - if reservation_block.check_date_range(first_date, second_date) reservation_block.room_ids.each do |room_id| available_rooms -= [find_room(room_id)] @@ -117,7 +113,6 @@ def list_room_by_range(first_date, second_date) return available_rooms.empty? ? "No rooms available in this date range." : available_rooms end + end end - -# From 5576ca67ea23dbadec15584eddd4af20f749f9a7 Mon Sep 17 00:00:00 2001 From: Jocelyn Wang Date: Tue, 3 Mar 2020 15:39:30 -0800 Subject: [PATCH 12/43] moved search date validation to separate method --- lib/reservation_manager.rb | 21 ++++++++++----------- test/reservation_manager_test.rb | 20 ++++++++++---------- 2 files changed, 20 insertions(+), 21 deletions(-) diff --git a/lib/reservation_manager.rb b/lib/reservation_manager.rb index 0aff5514e..d342dbc63 100644 --- a/lib/reservation_manager.rb +++ b/lib/reservation_manager.rb @@ -62,12 +62,8 @@ def rooms_list # List out all reservations by room and date range # Error handling if date is not entered properly, or date range is invalid (store in master class?) def search_by_room_date(room, first_date, second_date) - - if first_date.class != Date || second_date.class != Date - raise ArgumentError, "One date (#{first_date} or #{second_date}) is not valid" - elsif @rooms.last.id < room - raise ArgumentError, "Room #{room} does not exist" - end + search_date_validation(first_date,second_date) + raise ArgumentError, "Room #{room} does not exist" if @rooms.last.id < room reservation_by_room_date = [] @reservations.each do |reservation| @@ -90,10 +86,7 @@ def search_by_date(date) end def list_room_by_range(first_date, second_date) - - if first_date.class != Date || second_date.class != Date - raise ArgumentError, "One date (#{first_date} or #{second_date}) is not valid" - end + search_date_validation(first_date,second_date) available_rooms = @rooms.dup @@ -113,6 +106,12 @@ def list_room_by_range(first_date, second_date) return available_rooms.empty? ? "No rooms available in this date range." : available_rooms end - + + def search_date_validation(first_date, second_date) + if first_date.class != Date || second_date.class != Date + raise ArgumentError, "One date (#{first_date} or #{second_date}) is not valid" + end + end + end end diff --git a/test/reservation_manager_test.rb b/test/reservation_manager_test.rb index e75a3f1f3..d0c4f48a8 100644 --- a/test/reservation_manager_test.rb +++ b/test/reservation_manager_test.rb @@ -184,15 +184,15 @@ end end - # describe "test search by room AND date range" do - # it "able to list reservation by room/date range" do - # @search_result = @sample_two.search_by_room_date(1, Date.new(2020,3,1), Date.new(2020,3,10)) - # expect(@search_result).must_be_kind_of Array - # expect(@search_result[0]).must_be_kind_of HotelManager::Reservation - # expect(@search_result[1]).must_be_kind_of HotelManager::ReservationBlock - # expect(@search_result[0].id).must_equal 1 - # expect(@search_result[1].id).must_equal 3 - # end + describe "test search by room AND date range" do + # it "able to list reservation by room/date range" do + # @search_result = @sample_two.search_by_room_date(1, Date.new(2020,3,1), Date.new(2020,3,10)) + # expect(@search_result).must_be_kind_of Array + # expect(@search_result[0]).must_be_kind_of HotelManager::Reservation + # expect(@search_result[1]).must_be_kind_of HotelManager::ReservationBlock + # expect(@search_result[0].id).must_equal 1 + # expect(@search_result[1].id).must_equal 3 + # end # it "raise argument error if no room by room/date range" do # @search_result = @sample_two.search_by_room_date(1, Date.new(2020,4,1), Date.new(2020,4,10)) @@ -243,7 +243,7 @@ # it "notify user if no rooms are available" do # #complete later, need to modify hotel room # to by dynamic (user input) # end - # end + end end From 2e9819e3e7d8283df1bc76f93d82fb2186cd7c56 Mon Sep 17 00:00:00 2001 From: Jocelyn Wang Date: Wed, 4 Mar 2020 08:25:40 -0800 Subject: [PATCH 13/43] updated search by room/date range function for reservation blocks --- lib/reservation.rb | 13 ++++- lib/reservation_manager.rb | 10 +++- test/reservation_manager_test.rb | 97 ++++++++++++++++++-------------- test/reservation_test.rb | 2 +- 4 files changed, 75 insertions(+), 47 deletions(-) diff --git a/lib/reservation.rb b/lib/reservation.rb index 4936c4191..f1da0fc42 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -58,7 +58,7 @@ def check_date(search_date) (search_date >= @start_date) && (search_date <= @end_date) end - # Check whether reservation is within range + # Check whether date range overlaps with reservation def check_date_range(date_one, date_two) if date_one < date_two first_date, second_date = date_one, date_two @@ -67,10 +67,17 @@ def check_date_range(date_one, date_two) end (first_date >= @start_date && first_date < @end_date) || (second_date < @end_date && second_date > @start_date) + end + # Check whether reservation is within range + def check_reservation_range(date_one, date_two) + if date_one < date_two + first_date, second_date = date_one, date_two + else + first_date, second_date = date_two, date_one + end - # (@end_date > first_date) && (@end_date <= second_date) || - # (@start_date >= first_date) && (@start_date <= second_date) || (@start_date < first_date) && (@end_date <= second_date) + first_date < @end_date && second_date > @start_date end end diff --git a/lib/reservation_manager.rb b/lib/reservation_manager.rb index d342dbc63..68b0e7ebd 100644 --- a/lib/reservation_manager.rb +++ b/lib/reservation_manager.rb @@ -67,11 +67,19 @@ def search_by_room_date(room, first_date, second_date) reservation_by_room_date = [] @reservations.each do |reservation| - if reservation.check_date_range(first_date, second_date) && reservation.room_id == room + if reservation.check_reservation_range(first_date, second_date) && reservation.room_id == room reservation_by_room_date << reservation end end + @reservation_blocks.each do |reservation_block| + + found_room = reservation_block.room_ids.include? room + if reservation_block.check_reservation_range(first_date,second_date) && found_room + reservation_by_room_date << reservation_block + end + end + return reservation_by_room_date.empty? ? "No reservations found within date range." : reservation_by_room_date end diff --git a/test/reservation_manager_test.rb b/test/reservation_manager_test.rb index d0c4f48a8..2454ce071 100644 --- a/test/reservation_manager_test.rb +++ b/test/reservation_manager_test.rb @@ -1,27 +1,27 @@ require_relative 'test_helper' -# describe "Reservation Manager Class" do -# before do -# @sample = HotelManager::ReservationManager.new() -# end - -# it "is an instance of Reservation" do -# expect(@sample).must_be_kind_of HotelManager::ReservationManager -# expect(@sample.rooms).must_be_kind_of Array -# expect(@sample.reservations).must_be_kind_of Array -# expect(@sample.block_reservations).must_be_kind_of Array -# end - -# it "confirm that hotel contains 20 rooms" do -# expect(@sample.rooms.length).must_equal 20 -# end - -# it "able to list out all rooms in the hotel" do -# expect(@sample.rooms_list()).must_be_kind_of String -# expect(@sample.rooms_list()).must_include "Room: 1, Room: 2, Room: 3" -# expect(@sample.rooms_list()).must_include "Room: 19, Room: 20" -# end -# end +describe "Reservation Manager Class" do + before do + @sample = HotelManager::ReservationManager.new() + end + + it "is an instance of Reservation Manager" do + expect(@sample).must_be_kind_of HotelManager::ReservationManager + expect(@sample.rooms).must_be_kind_of Array + expect(@sample.reservations).must_be_kind_of Array + expect(@sample.reservation_blocks).must_be_kind_of Array + end + + it "confirm that hotel contains 20 rooms" do + expect(@sample.rooms.length).must_equal 20 + end + + it "able to list out all rooms in the hotel" do + expect(@sample.rooms_list()).must_be_kind_of String + expect(@sample.rooms_list()).must_include "Room: 1, Room: 2, Room: 3" + expect(@sample.rooms_list()).must_include "Room: 19, Room: 20" + end +end describe "Reservation Manager Class - Manipulation" do before do @@ -185,25 +185,38 @@ end describe "test search by room AND date range" do - # it "able to list reservation by room/date range" do - # @search_result = @sample_two.search_by_room_date(1, Date.new(2020,3,1), Date.new(2020,3,10)) - # expect(@search_result).must_be_kind_of Array - # expect(@search_result[0]).must_be_kind_of HotelManager::Reservation - # expect(@search_result[1]).must_be_kind_of HotelManager::ReservationBlock - # expect(@search_result[0].id).must_equal 1 - # expect(@search_result[1].id).must_equal 3 - # end - - # it "raise argument error if no room by room/date range" do - # @search_result = @sample_two.search_by_room_date(1, Date.new(2020,4,1), Date.new(2020,4,10)) - # expect(@search_result).must_include "No reservations found within date range." - # end + it "able to list reservation by room/date range" do + @rooms = [1, 2, 3] + @res_block_data = { + id: 4, + customer_id: 4, + start_date: Date.new(2020,3,6), + end_date: Date.new(2020,3,12), + room_cost: 100, + room_ids: @rooms + } - # it "raise argument error if invalid room or date range" do - # expect{@sample_two.search_by_room_date(30, Date.new(2020,3,1), Date.new(2020,3,10))}.must_raise ArgumentError - # expect{@sample_two.search_by_room_date(1, "Date.new(2020,3,1)", Date.new(2020,3,10))}.must_raise ArgumentError - # end - # end + @res_block = HotelManager::ReservationBlock.new(@res_block_data) + @sample_two.add_reservation_block(@res_block) + + @search_result = @sample_two.search_by_room_date(1, Date.new(2020,3,1), Date.new(2020,3,10)) + expect(@search_result).must_be_kind_of Array + expect(@search_result[0]).must_be_kind_of HotelManager::Reservation + expect(@search_result[1]).must_be_kind_of HotelManager::ReservationBlock + expect(@search_result[0].id).must_equal 1 + expect(@search_result[1].id).must_equal 4 + end + + it "notifies user if no room by room/date range" do + @search_result = @sample_two.search_by_room_date(1, Date.new(2020,4,1), Date.new(2020,4,10)) + expect(@search_result).must_include "No reservations found within date range." + end + + it "raise argument error if invalid room or date range" do + expect{@sample_two.search_by_room_date(30, Date.new(2020,3,1), Date.new(2020,3,10))}.must_raise ArgumentError + expect{@sample_two.search_by_room_date(1, "Date.new(2020,3,1)", Date.new(2020,3,10))}.must_raise ArgumentError + end + end # describe "test search by date" do # it "able to list reservation by specific date" do @@ -242,8 +255,8 @@ # it "notify user if no rooms are available" do # #complete later, need to modify hotel room # to by dynamic (user input) - # end - end + # # end + # end end diff --git a/test/reservation_test.rb b/test/reservation_test.rb index 2e00c95ee..d98f463c2 100644 --- a/test/reservation_test.rb +++ b/test/reservation_test.rb @@ -38,7 +38,7 @@ reservation_cost = @reservation.total_cost() expect(reservation_cost).must_equal 400 end - + end # Edge case - date entered is > 31 days per month or leap year From 234dbb44d8a00663f2907b3573f4e8d843e2a033 Mon Sep 17 00:00:00 2001 From: Jocelyn Wang Date: Wed, 4 Mar 2020 08:32:52 -0800 Subject: [PATCH 14/43] updated search by date function for block reservation --- lib/reservation_manager.rb | 4 ++++ test/reservation_manager_test.rb | 38 ++++++++++++++++---------------- 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/lib/reservation_manager.rb b/lib/reservation_manager.rb index 68b0e7ebd..e40c89928 100644 --- a/lib/reservation_manager.rb +++ b/lib/reservation_manager.rb @@ -90,6 +90,10 @@ def search_by_date(date) reservation_by_date << reservation if reservation.check_date(date) end + @reservation_blocks.each do |reservation_block| + reservation_by_date << reservation_block if reservation_block.check_date(date) + end + return reservation_by_date.empty? ? "No reservations found within date range." : reservation_by_date end diff --git a/test/reservation_manager_test.rb b/test/reservation_manager_test.rb index 2454ce071..fdd8ab688 100644 --- a/test/reservation_manager_test.rb +++ b/test/reservation_manager_test.rb @@ -218,25 +218,25 @@ end end - # describe "test search by date" do - # it "able to list reservation by specific date" do - # @search_result = @sample_two.search_by_date(Date.new(2020,3,17)) - # expect(@search_result).must_be_kind_of Array - # expect(@search_result[0]).must_be_kind_of HotelManager::ReservationBlock - # expect(@search_result[1]).must_be_kind_of HotelManager::Reservation - # expect(@search_result[0].id).must_equal 3 - # expect(@search_result[1].id).must_equal 2 - # end - - # it "raise argument error if no reservations on that date" do - # @search_result = @sample_two.search_by_date(Date.new(2020,4,1)) - # expect(@search_result).must_include "No reservations found within date range." - # end - - # it "raise argument error if invalid date entered" do - # expect{@sample_two.search_by_date("Date.new(2020,3,1)")}.must_raise ArgumentError - # end - # end + describe "test search by date" do + it "able to list reservation by specific date" do + @search_result = @sample_two.search_by_date(Date.new(2020,3,17)) + expect(@search_result).must_be_kind_of Array + expect(@search_result[0]).must_be_kind_of HotelManager::Reservation + expect(@search_result[1]).must_be_kind_of HotelManager::ReservationBlock + expect(@search_result[0].id).must_equal 2 + expect(@search_result[1].id).must_equal 3 + end + + it "raise argument error if no reservations on that date" do + @search_result = @sample_two.search_by_date(Date.new(2020,4,1)) + expect(@search_result).must_include "No reservations found within date range." + end + + it "raise argument error if invalid date entered" do + expect{@sample_two.search_by_date("Date.new(2020,3,1)")}.must_raise ArgumentError + end + end # # see list of rooms available for a given date range # describe "list rooms available by date range" do From eae6465e8df13c72d173eb198251fd796e311e1b Mon Sep 17 00:00:00 2001 From: Jocelyn Wang Date: Wed, 4 Mar 2020 09:00:55 -0800 Subject: [PATCH 15/43] updaated list by room range function for registration block --- lib/reservation.rb | 18 +++++++-------- lib/reservation_manager.rb | 5 +++-- test/reservation_manager_test.rb | 38 ++++++++++++++++---------------- test/test_helper.rb | 1 + 4 files changed, 32 insertions(+), 30 deletions(-) diff --git a/lib/reservation.rb b/lib/reservation.rb index f1da0fc42..2fcd4951a 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -58,16 +58,16 @@ def check_date(search_date) (search_date >= @start_date) && (search_date <= @end_date) end - # Check whether date range overlaps with reservation - def check_date_range(date_one, date_two) - if date_one < date_two - first_date, second_date = date_one, date_two - else - first_date, second_date = date_two, date_one - end + # # Check whether date range overlaps with reservation + # def check_date_range(date_one, date_two) + # if date_one < date_two + # first_date, second_date = date_one, date_two + # else + # first_date, second_date = date_two, date_one + # end - (first_date >= @start_date && first_date < @end_date) || (second_date < @end_date && second_date > @start_date) - end + # (first_date >= @start_date && first_date < @end_date) || (second_date < @end_date && second_date > @start_date) + # end # Check whether reservation is within range def check_reservation_range(date_one, date_two) diff --git a/lib/reservation_manager.rb b/lib/reservation_manager.rb index e40c89928..772ad1e63 100644 --- a/lib/reservation_manager.rb +++ b/lib/reservation_manager.rb @@ -103,13 +103,14 @@ def list_room_by_range(first_date, second_date) available_rooms = @rooms.dup @reservations.each do |reservation| - if reservation.check_date_range(first_date, second_date) + + if reservation.check_reservation_range(first_date, second_date) available_rooms -= [find_room(reservation.room_id)] end end @reservation_blocks.each do |reservation_block| - if reservation_block.check_date_range(first_date, second_date) + if reservation_block.check_reservation_range(first_date, second_date) reservation_block.room_ids.each do |room_id| available_rooms -= [find_room(room_id)] end diff --git a/test/reservation_manager_test.rb b/test/reservation_manager_test.rb index fdd8ab688..94888c30e 100644 --- a/test/reservation_manager_test.rb +++ b/test/reservation_manager_test.rb @@ -238,25 +238,25 @@ end end - # # see list of rooms available for a given date range - # describe "list rooms available by date range" do - # it "able to list out all available rooms by date range" do - # @search_result = @sample_two.list_room_by_range(Date.new(2020,4,1),Date.new(2020,4,7)) - # expect(@search_result).must_be_kind_of Array - # expect(@search_result.length).must_equal 20 - # end - - # it "able to list out all available rooms by date range" do - # @search_result = @sample_two.list_room_by_range(Date.new(2020,3,1),Date.new(2020,3,5)) - # expect(@search_result).must_be_kind_of Array - # expect(@search_result[0]).must_be_kind_of HotelManager::Room - # expect(@search_result.length).must_equal 15 - # end - - # it "notify user if no rooms are available" do - # #complete later, need to modify hotel room # to by dynamic (user input) - # # end - # end + # see list of rooms available for a given date range + describe "list rooms available by date range" do + it "able to list out all available rooms by date range" do + @search_result = @sample_two.list_room_by_range(Date.new(2020,4,1),Date.new(2020,4,7)) + expect(@search_result).must_be_kind_of Array + expect(@search_result.length).must_equal 20 + end + + it "able to list out all available rooms by date range" do + @search_result = @sample_two.list_room_by_range(Date.new(2020,3,1),Date.new(2020,3,5)) + expect(@search_result).must_be_kind_of Array + expect(@search_result[0]).must_be_kind_of HotelManager::Room + expect(@search_result.length).must_equal 16 + end + + it "notify user if no rooms are available" do + #complete later, need to modify hotel room # to by dynamic (user input) + end + end end diff --git a/test/test_helper.rb b/test/test_helper.rb index 024d0a386..82959e3e4 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -5,6 +5,7 @@ require "minitest" require "minitest/autorun" +require 'minitest/skip_dsl' # require "minitest/reporters" require "date" require "pry" From d1152156f97d98b449f0a9a3237e825f9ee0ffc8 Mon Sep 17 00:00:00 2001 From: Jocelyn Wang Date: Wed, 4 Mar 2020 09:23:08 -0800 Subject: [PATCH 16/43] refactored reservation class --- lib/reservation.rb | 58 ++++++++++++---------------------------------- 1 file changed, 15 insertions(+), 43 deletions(-) diff --git a/lib/reservation.rb b/lib/reservation.rb index 2fcd4951a..952b62a07 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -6,15 +6,14 @@ module HotelManager class Reservation - attr_reader :id, :customer_id, :start_date, :end_date, :room_cost, :room, :room_id - # Initialize reservation with start/end date, cost (default $200), customer id - # Raise exception for invalid date range is provided + attr_reader :id, :customer_id, :start_date, :end_date, :room_cost, :room, :room_id + def initialize ( id: , customer_id: , start_date: , end_date: , - room_cost: 200, + room_cost: 200, # Room default cost of $200 room: nil, room_id: nil ) @@ -25,62 +24,35 @@ def initialize ( @end_date = end_date @room_cost = room_cost @room = room + @room_id = room_id - if room - @room = room - @room_id = room_id - elsif - @room_id = room_id - else - raise ArgumentError, "Room or room_id is required" - end + # Raise exception for missing or invalid inputs - if @start_date.class() != Date - raise ArgumentError, "Start date #{@start_date} not valid" - end + raise ArgumentError, "Room or room_id is required" if room.nil? && room_id.nil? - if @end_date.class() != Date - raise ArgumentError, "End date #{@end_date} not valid" - end - - if @start_date >= @end_date + if @start_date.class() != Date || @end_date.class() != Date + raise ArgumentError, "Date #{@start_date} or #{@end_date} not valid" + elsif @start_date >= @end_date raise ArgumentError, "#{@start_date} is before #{@end_date} " end end - # Calculate cost of reservation (exclusive of last date) + # Calculate cost of reservation, exclusive of last date def total_cost @room_cost * (@end_date - @start_date - 1) end - # Check whether date is within range + # Checks if reservation exists on a specific date def check_date(search_date) (search_date >= @start_date) && (search_date <= @end_date) end - # # Check whether date range overlaps with reservation - # def check_date_range(date_one, date_two) - # if date_one < date_two - # first_date, second_date = date_one, date_two - # else - # first_date, second_date = date_two, date_one - # end - - # (first_date >= @start_date && first_date < @end_date) || (second_date < @end_date && second_date > @start_date) - # end - - # Check whether reservation is within range + # Checks whether reservation is within a given date range def check_reservation_range(date_one, date_two) - if date_one < date_two - first_date, second_date = date_one, date_two - else - first_date, second_date = date_two, date_one - end - + first_date = date_one < date_two ? date_one : date_two + second_date = date_one < date_two ? date_two : date_one first_date < @end_date && second_date > @start_date end end -end - -# Add in requirement that room must be populated \ No newline at end of file +end \ No newline at end of file From 6bf7df64914a87539841577c3613fd76064c9c06 Mon Sep 17 00:00:00 2001 From: Jocelyn Wang Date: Wed, 4 Mar 2020 09:39:55 -0800 Subject: [PATCH 17/43] refactored code to get rid of warning messages from minitest --- lib/reservation.rb | 21 ++++++++++----------- lib/reservation_block.rb | 15 +++++++-------- lib/reservation_manager.rb | 8 ++------ lib/room.rb | 2 +- 4 files changed, 20 insertions(+), 26 deletions(-) diff --git a/lib/reservation.rb b/lib/reservation.rb index 952b62a07..ce6424f94 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -8,16 +8,15 @@ class Reservation attr_reader :id, :customer_id, :start_date, :end_date, :room_cost, :room, :room_id - def initialize ( - id: , - customer_id: , - start_date: , - end_date: , - room_cost: 200, # Room default cost of $200 - room: nil, - room_id: nil - ) - + # Room default cost of $200 + def initialize id:, + customer_id:, + start_date:, + end_date:, + room_cost: 200, + room: nil, + room_id: nil + @id = id @customer_id = customer_id @start_date = start_date @@ -30,7 +29,7 @@ def initialize ( raise ArgumentError, "Room or room_id is required" if room.nil? && room_id.nil? - if @start_date.class() != Date || @end_date.class() != Date + if @start_date.class != Date || @end_date.class != Date raise ArgumentError, "Date #{@start_date} or #{@end_date} not valid" elsif @start_date >= @end_date raise ArgumentError, "#{@start_date} is before #{@end_date} " diff --git a/lib/reservation_block.rb b/lib/reservation_block.rb index 706ae3aea..e355e050c 100644 --- a/lib/reservation_block.rb +++ b/lib/reservation_block.rb @@ -10,14 +10,13 @@ class ReservationBlock < Reservation attr_reader :id, :customer_id, :start_date, :end_date, :room_cost, :rooms, :room_ids # Initialize reservation with start/end date, cost (default $200), customer id # Raise exception for invalid date range is provided - def initialize (id: , - customer_id: , - start_date: , - end_date: , - room_cost: 200, - rooms: nil, - room_ids: nil - ) + def initialize id: , + customer_id: , + start_date: , + end_date: , + room_cost: 200, + rooms: nil, + room_ids: nil @id = id @customer_id = customer_id diff --git a/lib/reservation_manager.rb b/lib/reservation_manager.rb index 772ad1e63..01c1eb6d7 100644 --- a/lib/reservation_manager.rb +++ b/lib/reservation_manager.rb @@ -30,10 +30,6 @@ def add_reservation(reservation) raise ArgumentError, "Room #{reservation.room_id} is double booked. Reservation not finalized in reservation manager." end end - - def find_room(id) - return @rooms.find { |room| room.id == id } - end # Add/create new block reservation def add_reservation_block(reservation_block) @@ -51,8 +47,8 @@ def add_reservation_block(reservation_block) end def find_room(id) - return @rooms.find { |room| room.id == id } - end + @rooms.find { |room| room.id == id } + end # List out all rooms in hotel def rooms_list diff --git a/lib/room.rb b/lib/room.rb index 3162fa76f..ec7990dda 100644 --- a/lib/room.rb +++ b/lib/room.rb @@ -3,7 +3,7 @@ class Room attr_reader :id - def initialize (id: ) + def initialize id: @id = id end From 8556ff7c5c279948ee3b706bfb7d6166acdc4a17 Mon Sep 17 00:00:00 2001 From: Jocelyn Wang Date: Wed, 4 Mar 2020 09:43:47 -0800 Subject: [PATCH 18/43] cleaned up require relative requests --- lib/reservation.rb | 4 +--- lib/reservation_block.rb | 1 - lib/reservation_manager.rb | 1 + test/test_helper.rb | 2 +- 4 files changed, 3 insertions(+), 5 deletions(-) diff --git a/lib/reservation.rb b/lib/reservation.rb index ce6424f94..8a968bf9f 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -1,8 +1,6 @@ require 'date' require 'pry' -require_relative 'hotel' - module HotelManager class Reservation @@ -16,7 +14,7 @@ def initialize id:, room_cost: 200, room: nil, room_id: nil - + @id = id @customer_id = customer_id @start_date = start_date diff --git a/lib/reservation_block.rb b/lib/reservation_block.rb index e355e050c..b3f8287d2 100644 --- a/lib/reservation_block.rb +++ b/lib/reservation_block.rb @@ -1,7 +1,6 @@ require 'date' require 'pry' -require_relative 'hotel' require_relative 'reservation' module HotelManager diff --git a/lib/reservation_manager.rb b/lib/reservation_manager.rb index 01c1eb6d7..a94589bed 100644 --- a/lib/reservation_manager.rb +++ b/lib/reservation_manager.rb @@ -2,6 +2,7 @@ require 'date' require_relative 'reservation' +require_relative 'reservation_block' require_relative 'room' module HotelManager diff --git a/test/test_helper.rb b/test/test_helper.rb index 82959e3e4..9496e3e24 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -5,7 +5,7 @@ require "minitest" require "minitest/autorun" -require 'minitest/skip_dsl' +require "minitest/skip_dsl" # require "minitest/reporters" require "date" require "pry" From 954cda233fab56c821887d86bb6fe9784bf5e2c0 Mon Sep 17 00:00:00 2001 From: Jocelyn Wang Date: Wed, 4 Mar 2020 09:44:32 -0800 Subject: [PATCH 19/43] removed hotel file --- lib/hotel.rb | 11 ----------- 1 file changed, 11 deletions(-) delete mode 100644 lib/hotel.rb diff --git a/lib/hotel.rb b/lib/hotel.rb deleted file mode 100644 index a0926d4ab..000000000 --- a/lib/hotel.rb +++ /dev/null @@ -1,11 +0,0 @@ -module HotelManager - class Hotel - - # Initialize create 20 rooms - - # List out all rooms in hotel - - # - - end -end From 78161cadbff2f8e8ebf982ca590b7b89444d92da Mon Sep 17 00:00:00 2001 From: Jocelyn Wang Date: Wed, 4 Mar 2020 12:45:14 -0800 Subject: [PATCH 20/43] refactored reservation block --- lib/reservation.rb | 11 ++++- lib/reservation_block.rb | 70 ++++++-------------------------- test/reservation_manager_test.rb | 4 +- 3 files changed, 25 insertions(+), 60 deletions(-) diff --git a/lib/reservation.rb b/lib/reservation.rb index 8a968bf9f..290c5bbee 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -24,9 +24,12 @@ def initialize id:, @room_id = room_id # Raise exception for missing or invalid inputs + validate_date + validate_room + end - raise ArgumentError, "Room or room_id is required" if room.nil? && room_id.nil? - + # Check input validation + def validate_date if @start_date.class != Date || @end_date.class != Date raise ArgumentError, "Date #{@start_date} or #{@end_date} not valid" elsif @start_date >= @end_date @@ -34,6 +37,10 @@ def initialize id:, end end + def validate_room + raise ArgumentError, "Room or room_id is required" if room.nil? && room_id.nil? + end + # Calculate cost of reservation, exclusive of last date def total_cost @room_cost * (@end_date - @start_date - 1) diff --git a/lib/reservation_block.rb b/lib/reservation_block.rb index b3f8287d2..5196a2bc5 100644 --- a/lib/reservation_block.rb +++ b/lib/reservation_block.rb @@ -2,13 +2,14 @@ require 'pry' require_relative 'reservation' +require_relative 'room' module HotelManager class ReservationBlock < Reservation attr_reader :id, :customer_id, :start_date, :end_date, :room_cost, :rooms, :room_ids - # Initialize reservation with start/end date, cost (default $200), customer id - # Raise exception for invalid date range is provided + + # Room default cost of $200 def initialize id: , customer_id: , start_date: , @@ -23,70 +24,25 @@ def initialize id: , @end_date = end_date @room_cost = room_cost @rooms = rooms + @room_ids = room_ids - if rooms - @rooms = rooms - @room_ids = room_ids - if rooms.length <= 1 || rooms.length > 5 - raise ArgumentError, "#{rooms.length} is an invalid number of rooms for a hotel block" - end - elsif - @room_ids = room_ids - if room_ids.length <= 1 || room_ids.length > 5 - raise ArgumentError, "#{rooms.length} is an invalid number of rooms for a hotel block" - end - else - raise ArgumentError, "Rooms or room_ids is required" - end - - - # if room_ids.length <= 1 || room_ids.length > 5 - # raise ArgumentError, "#{rooms.length} is an invalid number of rooms for a hotel block" - # elsif rooms.length <= 1 || rooms.length > 5 - # raise ArgumentError, "#{rooms.length} is an invalid number of rooms for a hotel block" - # end + validate_date + @rooms.nil? ? validate_room(@room_ids) : validate_room(@rooms) + end - # Move to method later to avoid needing to call here? - if @start_date.class() != Date - raise ArgumentError, "Start date #{@start_date} not valid" - end - if @end_date.class() != Date - raise ArgumentError, "End date #{@end_date} not valid" - end + def validate_room(attribute) + raise ArgumentError, "Room or room_id is required" if @rooms.nil? && @room_ids.nil? - if @start_date >= @end_date - raise ArgumentError, "#{@start_date} is before #{@end_date} " + if attribute.length <= 1 || attribute.length > 5 + raise ArgumentError, "#{attribute.length} is an invalid number of rooms for a hotel block" end end # Calculate cost of reservation (exclusive of last date) def total_cost - @room_cost * (@end_date - @start_date - 1) * @rooms.length + super * @rooms.length end - def check_date_range(date_one, date_two) - super - end - - # # Check whether date is within range - # def check_date(search_date) - # (search_date >= @start_date) && (search_date <= @end_date) - # end - - # # Check whether reservation is within range - # def check_date_range(date_one, date_two) - # if date_one < date_two - # first_date, second_date = date_one, date_two - # else - # first_date, second_date = date_two, date_one - # end - - # (@end_date > first_date) && (@end_date <= second_date) || - # (@start_date >= first_date) && (@start_date <= second_date) - # end - end -end - -# Add in requirement that room must be populated \ No newline at end of file +end \ No newline at end of file diff --git a/test/reservation_manager_test.rb b/test/reservation_manager_test.rb index 94888c30e..2042ae4ea 100644 --- a/test/reservation_manager_test.rb +++ b/test/reservation_manager_test.rb @@ -267,4 +267,6 @@ # Able to reserve a specific room from a hotel block, but only for the full duration of the block (individual reservations still work?) # List of reservation by date will list out both individual and block reservations # Check to make sure that when a block is reserved, the indivudal room reservation dates will also be there -# Confirm that all availabilty checking logic from Wave 2 also respects room blocks \ No newline at end of file +# Confirm that all availabilty checking logic from Wave 2 also respects room blocks + +# 2 or more days overlapping (of all dates in the range) \ No newline at end of file From df143520d0f55ab73b198998f5ae1f1945a55a55 Mon Sep 17 00:00:00 2001 From: Jocelyn Wang Date: Wed, 4 Mar 2020 13:25:29 -0800 Subject: [PATCH 21/43] consolidated add reservation and add reservation block. Updated test accordingly --- lib/reservation_block.rb | 1 - lib/reservation_manager.rb | 45 +++++++++++++++----------------- test/reservation_manager_test.rb | 31 +++++++++++----------- 3 files changed, 37 insertions(+), 40 deletions(-) diff --git a/lib/reservation_block.rb b/lib/reservation_block.rb index 5196a2bc5..18542c691 100644 --- a/lib/reservation_block.rb +++ b/lib/reservation_block.rb @@ -30,7 +30,6 @@ def initialize id: , @rooms.nil? ? validate_room(@room_ids) : validate_room(@rooms) end - def validate_room(attribute) raise ArgumentError, "Room or room_id is required" if @rooms.nil? && @room_ids.nil? diff --git a/lib/reservation_manager.rb b/lib/reservation_manager.rb index a94589bed..7a1513ead 100644 --- a/lib/reservation_manager.rb +++ b/lib/reservation_manager.rb @@ -22,28 +22,21 @@ def initialize end # Add/create new reservation - def add_reservation(reservation) + def add_reservation(reservation, class_storage) potential_rooms = self.list_room_by_range(reservation.start_date, reservation.end_date) - - if potential_rooms.include? find_room(reservation.room_id) - @reservations << reservation - else - raise ArgumentError, "Room #{reservation.room_id} is double booked. Reservation not finalized in reservation manager." - end - end - - # Add/create new block reservation - def add_reservation_block(reservation_block) - potential_rooms = self.list_room_by_range(reservation_block.start_date, reservation_block.end_date) - - found_room = reservation_block.room_ids.map do |room_id| + + if reservation.class == HotelManager::Reservation + found_room = potential_rooms.include? find_room(reservation.room_id) + elsif reservation.class == HotelManager::ReservationBlock + found_room = reservation.room_ids.map do |room_id| potential_rooms.include? find_room(room_id) end.all? (true) - + end + if found_room - @reservation_blocks << reservation_block + class_storage << reservation else - raise ArgumentError, "Some of rooms #{reservation_block.room_ids} are double booked. Hotel block not finalized in reservation manager." + raise ArgumentError, "Room(s) are double booked. Hotel block not saved in reservation manager." end end @@ -62,10 +55,10 @@ def search_by_room_date(room, first_date, second_date) search_date_validation(first_date,second_date) raise ArgumentError, "Room #{room} does not exist" if @rooms.last.id < room - reservation_by_room_date = [] + reservation_room_date = [] @reservations.each do |reservation| if reservation.check_reservation_range(first_date, second_date) && reservation.room_id == room - reservation_by_room_date << reservation + reservation_room_date << reservation end end @@ -73,11 +66,11 @@ def search_by_room_date(room, first_date, second_date) found_room = reservation_block.room_ids.include? room if reservation_block.check_reservation_range(first_date,second_date) && found_room - reservation_by_room_date << reservation_block + reservation_room_date << reservation_block end end - return reservation_by_room_date.empty? ? "No reservations found within date range." : reservation_by_room_date + return reservation_found?(reservation_room_date, "reservations") end # List out reservations by specific date @@ -91,7 +84,7 @@ def search_by_date(date) reservation_by_date << reservation_block if reservation_block.check_date(date) end - return reservation_by_date.empty? ? "No reservations found within date range." : reservation_by_date + return reservation_found?(reservation_by_date, "reservations") end def list_room_by_range(first_date, second_date) @@ -100,7 +93,6 @@ def list_room_by_range(first_date, second_date) available_rooms = @rooms.dup @reservations.each do |reservation| - if reservation.check_reservation_range(first_date, second_date) available_rooms -= [find_room(reservation.room_id)] end @@ -114,9 +106,14 @@ def list_room_by_range(first_date, second_date) end end - return available_rooms.empty? ? "No rooms available in this date range." : available_rooms + return reservation_found?(available_rooms, "rooms") + end + + def reservation_found? (tracker, type) + tracker.empty? ? "No #{type} available in date range." : tracker end + # potentially move to own class to be inherited? def search_date_validation(first_date, second_date) if first_date.class != Date || second_date.class != Date raise ArgumentError, "One date (#{first_date} or #{second_date}) is not valid" diff --git a/test/reservation_manager_test.rb b/test/reservation_manager_test.rb index 2042ae4ea..cc00815ea 100644 --- a/test/reservation_manager_test.rb +++ b/test/reservation_manager_test.rb @@ -57,10 +57,10 @@ @res_block = HotelManager::ReservationBlock.new(@res_block_data) @sample_two = HotelManager::ReservationManager.new() - @sample_two.add_reservation(@res_one) + @sample_two.add_reservation(@res_one, @sample_two.reservations) - @sample_two.add_reservation(@res_two) - @sample_two.add_reservation_block(@res_block) + @sample_two.add_reservation(@res_two, @sample_two.reservations) + @sample_two.add_reservation(@res_block, @sample_two.reservation_blocks) end describe "ability to add reservations to the Reservation Manager" do @@ -80,7 +80,7 @@ } @res_three = HotelManager::ReservationBlock.new(@res_three_data) - @sample_two.add_reservation_block(@res_three) + @sample_two.add_reservation(@res_three, @sample_two.reservation_blocks) expect(@sample_two.reservations.length).must_equal 2 end @@ -96,7 +96,7 @@ } @res_four = HotelManager::Reservation.new(@res_four_data) - @sample_two.add_reservation(@res_four) + @sample_two.add_reservation(@res_four, @sample_two.reservations) expect(@sample_two.reservations.length).must_equal 3 end @@ -113,7 +113,7 @@ } @res_six = HotelManager::ReservationBlock.new(@res_six_data) - @sample_two.add_reservation_block(@res_six) + @sample_two.add_reservation(@res_six, @sample_two.reservation_blocks) expect(@sample_two.reservations.length).must_equal 2 end @@ -129,7 +129,7 @@ } @res_seven = HotelManager::Reservation.new(@res_seven_data) - @sample_two.add_reservation(@res_seven) + @sample_two.add_reservation(@res_seven,@sample_two.reservations) expect(@sample_two.reservations.length).must_equal 3 end @@ -145,7 +145,7 @@ } @res_five = HotelManager::Reservation.new(@res_five_data) - expect{@sample_two.add_reservation(@res_five)}.must_raise ArgumentError + expect{@sample_two.add_reservation(@res_five,@sample_two.reservations)}.must_raise ArgumentError end it "raise argument error if individual overbooking attempted on block" do @@ -160,12 +160,12 @@ @res_five = HotelManager::Reservation.new(@res_five_data) - expect{@sample_two.add_reservation(@res_five)}.must_raise ArgumentError + expect{@sample_two.add_reservation(@res_five,@sample_two.reservations)}.must_raise ArgumentError end it "raise argument error if block overbooking attempted on block" do @res_five = HotelManager::ReservationBlock.new(@res_block_data) - expect{@sample_two.add_reservation_block(@res_five)}.must_raise ArgumentError + expect{@sample_two.add_reservation(@res_five,@sample_two.reservations)}.must_raise ArgumentError end it "raise argument error if block overbooking attempted on individual" do @@ -173,14 +173,15 @@ @res_six_data = { id: 3, customer_id: 3, - start_date: Date.new(2020,3,9), + start_date: Date.new(2020,3,4), end_date: Date.new(2020,3,14), room_cost: 100, room_ids: @rooms } @res_six = HotelManager::ReservationBlock.new(@res_six_data) - expect{@sample_two.add_reservation(@res_six)}.must_raise ArgumentError + + expect{@sample_two.add_reservation(@res_six, @sample_two.reservation_blocks)}.must_raise ArgumentError end end @@ -197,7 +198,7 @@ } @res_block = HotelManager::ReservationBlock.new(@res_block_data) - @sample_two.add_reservation_block(@res_block) + @sample_two.add_reservation(@res_block,@sample_two.reservation_blocks) @search_result = @sample_two.search_by_room_date(1, Date.new(2020,3,1), Date.new(2020,3,10)) expect(@search_result).must_be_kind_of Array @@ -209,7 +210,7 @@ it "notifies user if no room by room/date range" do @search_result = @sample_two.search_by_room_date(1, Date.new(2020,4,1), Date.new(2020,4,10)) - expect(@search_result).must_include "No reservations found within date range." + expect(@search_result).must_include "No reservations available in date range." end it "raise argument error if invalid room or date range" do @@ -230,7 +231,7 @@ it "raise argument error if no reservations on that date" do @search_result = @sample_two.search_by_date(Date.new(2020,4,1)) - expect(@search_result).must_include "No reservations found within date range." + expect(@search_result).must_include "No reservations available in date range." end it "raise argument error if invalid date entered" do From 76281e7bc08b69728d98eb28d949d328ac75d6b5 Mon Sep 17 00:00:00 2001 From: Jocelyn Wang Date: Wed, 4 Mar 2020 14:03:31 -0800 Subject: [PATCH 22/43] added in initial tests for assigning first available room to new reservations --- lib/reservation_manager.rb | 4 +-- test/reservation_manager_test.rb | 54 ++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 3 deletions(-) diff --git a/lib/reservation_manager.rb b/lib/reservation_manager.rb index 7a1513ead..168d340aa 100644 --- a/lib/reservation_manager.rb +++ b/lib/reservation_manager.rb @@ -36,7 +36,7 @@ def add_reservation(reservation, class_storage) if found_room class_storage << reservation else - raise ArgumentError, "Room(s) are double booked. Hotel block not saved in reservation manager." + raise ArgumentError, "#{reservation.class} double booked. Not saved to reservation manager." end end @@ -50,7 +50,6 @@ def rooms_list end # List out all reservations by room and date range - # Error handling if date is not entered properly, or date range is invalid (store in master class?) def search_by_room_date(room, first_date, second_date) search_date_validation(first_date,second_date) raise ArgumentError, "Room #{room} does not exist" if @rooms.last.id < room @@ -63,7 +62,6 @@ def search_by_room_date(room, first_date, second_date) end @reservation_blocks.each do |reservation_block| - found_room = reservation_block.room_ids.include? room if reservation_block.check_reservation_range(first_date,second_date) && found_room reservation_room_date << reservation_block diff --git a/test/reservation_manager_test.rb b/test/reservation_manager_test.rb index cc00815ea..f2d5096fe 100644 --- a/test/reservation_manager_test.rb +++ b/test/reservation_manager_test.rb @@ -260,6 +260,60 @@ end end +xdescribe "Reservation Manager Class - Create New Reservation" do + before do + @sample = HotelManager::ReservationManager.new() + @sample.save_reservation(Date.new(2020,3,1),Date.new(2020,3,5)) + @sample.save_reservation(Date.new(2020,3,1),Date.new(2020,3,5)) + @sample.save_reservation(Date.new(2020,3,1),Date.new(2020,3,5),5) + @sample.save_reservation(Date.new(2020,3,1),Date.new(2020,3,5),4) + end + + it "create and save reservation providing only dates" do + expect(@sample.reservations.length).must_equal 2 + end + + it "verify that next available rooms were used - individual" do + @sample.save_reservation(Date.new(2020,3,4),Date.new(2020,3,7)) + @sample.save_reservation(Date.new(2020,3,10),Date.new(2020,3,17)) + + expect(@sample.reservations[0].room_id).must_equal 1 + expect(@sample.reservations[1].room_id).must_equal 2 + expect(@sample.reservations[2].room_id).must_equal 12 + expect(@sample.reservations[3].room_id).must_equal 1 + end + + it "create and save reservation blocks providing only dates" do + expect(@sample.reservations_blocks.length).must_equal 2 + end + + it "verify that next available rooms were used - block" do + @sample.save_reservation(Date.new(2020,3,4),Date.new(2020,3,7),4) + @sample.save_reservation(Date.new(2020,3,10),Date.new(2020,3,5),5) + + expect(@sample.reservations[0].room_id).must_equal [3,4,5,6,7] + expect(@sample.reservations[1].room_id).must_equal [8,9,10,11] + expect(@sample.reservations[2].room_id).must_equal [12,13,14,15] + expect(@sample.reservations[3].room_id).must_equal [1,2,3,4,5] + end + + it "raises arugment error if no rooms are available - individual" do + @sample.save_reservation(Date.new(2020,3,1),Date.new(2020,3,5),5) + @sample.save_reservation(Date.new(2020,3,1),Date.new(2020,3,5),4) + + expect{@sample.save_reservation(Date.new(2020,3,1),Date.new(2020,3,5))}.must_raise ArgumentError + end + + it "raises arugment error if no rooms are available - block" do + @sample.save_reservation(Date.new(2020,3,1),Date.new(2020,3,5),5) + @sample.save_reservation(Date.new(2020,3,1),Date.new(2020,3,5),4) + + expect{@sample.save_reservation(Date.new(2020,3,1),Date.new(2020,3,5),2)}.must_raise ArgumentError + end +end + +# Be able enter date range and create reservation from that (assign room automatically) + From 32bc695dca0dbb2a5a44cce734bdc16fec586e96 Mon Sep 17 00:00:00 2001 From: Jocelyn Wang Date: Wed, 4 Mar 2020 15:00:25 -0800 Subject: [PATCH 23/43] added functionality to create new reservation given only dates --- lib/reservation_manager.rb | 47 ++++++++++++++++++++++++++++++-- test/reservation_manager_test.rb | 35 +++++++++++------------- 2 files changed, 60 insertions(+), 22 deletions(-) diff --git a/lib/reservation_manager.rb b/lib/reservation_manager.rb index 168d340aa..f0eb6322a 100644 --- a/lib/reservation_manager.rb +++ b/lib/reservation_manager.rb @@ -21,7 +21,48 @@ def initialize end end - # Add/create new reservation + # Creates and saves new reservation/block to instance variables + def save_reservation(first_date, second_date, num_of_rooms: 1, customer_id: nil, room_cost: 200) + + # change list_room_by_range to return argument rather than string + available_rooms = list_room_by_range(first_date,second_date) + + if available_rooms.is_a? Array + chosen_rooms = available_rooms.select.with_index do |room, index| + room.id if index < num_of_rooms + end + else + raise ArgumentError, "Something's not right here..." + end + + chosen_rooms.map!{|room| room.id} + + if num_of_rooms == 1 + class_storage = @reservations + new_reservation = HotelManager::Reservation.new( + id: @reservations.length + 1, + customer_id: customer_id, + start_date: first_date, + end_date: second_date, + room_cost: room_cost, + room_id: chosen_rooms[0] + ) + else + class_storage = @reservation_blocks + new_reservation = HotelManager::ReservationBlock.new( + id: @reservation_blocks.length + 1, + customer_id: customer_id, + start_date: first_date, + end_date: second_date, + room_cost: room_cost, + room_ids: chosen_rooms + ) + end + + add_reservation(new_reservation,class_storage) + end + + # Add reservation to instance variables def add_reservation(reservation, class_storage) potential_rooms = self.list_room_by_range(reservation.start_date, reservation.end_date) @@ -32,7 +73,7 @@ def add_reservation(reservation, class_storage) potential_rooms.include? find_room(room_id) end.all? (true) end - + if found_room class_storage << reservation else @@ -107,7 +148,7 @@ def list_room_by_range(first_date, second_date) return reservation_found?(available_rooms, "rooms") end - def reservation_found? (tracker, type) + def reservation_found? tracker, type tracker.empty? ? "No #{type} available in date range." : tracker end diff --git a/test/reservation_manager_test.rb b/test/reservation_manager_test.rb index f2d5096fe..3c2c0b82b 100644 --- a/test/reservation_manager_test.rb +++ b/test/reservation_manager_test.rb @@ -260,13 +260,14 @@ end end -xdescribe "Reservation Manager Class - Create New Reservation" do +describe "Reservation Manager Class - Create New Reservation" do before do @sample = HotelManager::ReservationManager.new() + @sample.save_reservation(Date.new(2020,3,1),Date.new(2020,3,5)) @sample.save_reservation(Date.new(2020,3,1),Date.new(2020,3,5)) - @sample.save_reservation(Date.new(2020,3,1),Date.new(2020,3,5),5) - @sample.save_reservation(Date.new(2020,3,1),Date.new(2020,3,5),4) + @sample.save_reservation(Date.new(2020,3,1),Date.new(2020,3,5),num_of_rooms:5) + @sample.save_reservation(Date.new(2020,3,1),Date.new(2020,3,5),num_of_rooms:4) end it "create and save reservation providing only dates" do @@ -284,38 +285,34 @@ end it "create and save reservation blocks providing only dates" do - expect(@sample.reservations_blocks.length).must_equal 2 + expect(@sample.reservation_blocks.length).must_equal 2 end it "verify that next available rooms were used - block" do - @sample.save_reservation(Date.new(2020,3,4),Date.new(2020,3,7),4) - @sample.save_reservation(Date.new(2020,3,10),Date.new(2020,3,5),5) + @sample.save_reservation(Date.new(2020,3,4),Date.new(2020,3,7),num_of_rooms:4) + @sample.save_reservation(Date.new(2020,3,10),Date.new(2020,3,15),num_of_rooms:5) - expect(@sample.reservations[0].room_id).must_equal [3,4,5,6,7] - expect(@sample.reservations[1].room_id).must_equal [8,9,10,11] - expect(@sample.reservations[2].room_id).must_equal [12,13,14,15] - expect(@sample.reservations[3].room_id).must_equal [1,2,3,4,5] + expect(@sample.reservation_blocks[0].room_ids).must_equal [3,4,5,6,7] + expect(@sample.reservation_blocks[1].room_ids).must_equal [8,9,10,11] + expect(@sample.reservation_blocks[2].room_ids).must_equal [12,13,14,15] + expect(@sample.reservation_blocks[3].room_ids).must_equal [1,2,3,4,5] end it "raises arugment error if no rooms are available - individual" do - @sample.save_reservation(Date.new(2020,3,1),Date.new(2020,3,5),5) - @sample.save_reservation(Date.new(2020,3,1),Date.new(2020,3,5),4) + @sample.save_reservation(Date.new(2020,3,1),Date.new(2020,3,5),num_of_rooms:5) + @sample.save_reservation(Date.new(2020,3,1),Date.new(2020,3,5),num_of_rooms:4) expect{@sample.save_reservation(Date.new(2020,3,1),Date.new(2020,3,5))}.must_raise ArgumentError end it "raises arugment error if no rooms are available - block" do - @sample.save_reservation(Date.new(2020,3,1),Date.new(2020,3,5),5) - @sample.save_reservation(Date.new(2020,3,1),Date.new(2020,3,5),4) + @sample.save_reservation(Date.new(2020,3,1),Date.new(2020,3,5),num_of_rooms:5) + @sample.save_reservation(Date.new(2020,3,1),Date.new(2020,3,5),num_of_rooms:4) - expect{@sample.save_reservation(Date.new(2020,3,1),Date.new(2020,3,5),2)}.must_raise ArgumentError + expect{@sample.save_reservation(Date.new(2020,3,1),Date.new(2020,3,5),num_of_rooms:2)}.must_raise ArgumentError end end -# Be able enter date range and create reservation from that (assign room automatically) - - - # List of available rooms will not include the rooms in block reservation # Correctly list out whether a given block has any rooms available? From 564b0978aefeb46945a55fe2cf25a22e353ca00a Mon Sep 17 00:00:00 2001 From: Jocelyn Wang Date: Wed, 4 Mar 2020 15:11:43 -0800 Subject: [PATCH 24/43] added more tests for reservation class --- test/reservation_test.rb | 34 ++++++++++++---------------------- 1 file changed, 12 insertions(+), 22 deletions(-) diff --git a/test/reservation_test.rb b/test/reservation_test.rb index d98f463c2..0e91c8d4d 100644 --- a/test/reservation_test.rb +++ b/test/reservation_test.rb @@ -7,7 +7,6 @@ customer_id: 1, start_date: Date.new(2020,3,2), end_date: Date.new(2020,3,5), - room_cost: 200, room: HotelManager::Room.new(id: 1) } @reservation = HotelManager::Reservation.new(@reservation_data) @@ -17,6 +16,11 @@ expect(@reservation).must_be_kind_of HotelManager::Reservation end + it "raises argument error if no room or room id provided" do + @reservation_data.delete(:room) + expect{HotelManager::Reservation.new(@reservation_data)}.must_raise ArgumentError + end + it "validates that start/end date is a date class" do expect(@reservation.end_date).must_be_kind_of Date expect(@reservation.start_date).must_be_kind_of Date @@ -28,33 +32,19 @@ end it "raises argument error if end before start date" do - @reservation_data[:start_date] = Date.new(2020,3,5) @reservation_data[:end_date] = Date.new(2020,3,2) expect{HotelManager::Reservation.new(@reservation_data)}.must_raise ArgumentError end + it "raises argument error if end and start date are the same" do + @reservation_data[:start_date] = Date.new(2020,3,5) + @reservation_data[:end_date] = Date.new(2020,3,5) + expect{HotelManager::Reservation.new(@reservation_data)}.must_raise ArgumentError + end + it "calculates total cost correctly" do reservation_cost = @reservation.total_cost() expect(reservation_cost).must_equal 400 end - -end - -# Edge case - date entered is > 31 days per month or leap year -# raise argument error if start and end date are the same - -# Able to reserve a hotel block (group of rooms for a specific group of customers for a set period of time) -# Block reservation has more than one room -# Block reservation has less than five rooms -# Block reservation can be at a discount rate -# Block reservation cen be over a period of days -# List of available rooms will not include the rooms in block reservation -# Raise exception if one or more rooms is unavailable for the given date range -# Cannot double book a room for single reservation if it is reserved for a hotel block -# Cannot double book a room for another hotel block if it is reserved for a hotel block -# Correctly list out whether a given block has any rooms available? -# Able to reserve a specific room from a hotel block, but only for the full duration of the block (individual reservations still work?) -# List of reservation by date will list out both individual and block reservations -# Check to make sure that when a block is reserved, the indivudal room reservation dates will also be there -# Confirm that all availabilty checking logic from Wave 2 also respects room blocks \ No newline at end of file +end \ No newline at end of file From e44d9cd4ed58a1967873b280edefe3a5d5023887 Mon Sep 17 00:00:00 2001 From: Jocelyn Wang Date: Wed, 4 Mar 2020 16:12:41 -0800 Subject: [PATCH 25/43] added in csv files with dummy data (reservations, reservation blocks, rooms) --- test/test_data/reservation_blocks.csv | 11 +++++++++++ test/test_data/reservations.csv | 11 +++++++++++ test/test_data/rooms.csv | 21 +++++++++++++++++++++ 3 files changed, 43 insertions(+) create mode 100644 test/test_data/reservation_blocks.csv create mode 100644 test/test_data/reservations.csv create mode 100644 test/test_data/rooms.csv diff --git a/test/test_data/reservation_blocks.csv b/test/test_data/reservation_blocks.csv new file mode 100644 index 000000000..4edffe5a8 --- /dev/null +++ b/test/test_data/reservation_blocks.csv @@ -0,0 +1,11 @@ +id,customer_id,start_date,end_date,num_of_rooms +1,1,2020-05-01 15:00:00 -0800,2020-07-10 11:00:00 -0800,2 +2,2,2020-05-08 15:00:00 -0800,2020-05-12 11:00:00 -0800,3 +3,3,2020-05-05 15:00:00 -0800,2020-05-15 11:00:00 -0800,4 +4,4,2020-05-14 15:00:00 -0800,2020-05-28 11:00:00 -0800,5 +5,2,2020-05-28 15:00:00 -0800,2020-05-30 11:00:00 -0800,2 +6,3,2020-05-25 15:00:00 -0800,2020-05-29 11:00:00 -0800,3 +7,4,2020-05-15 15:00:00 -0800,2020-05-17 11:00:00 -0800,5 +8,6,2020-05-25 15:00:00 -0800,2020-05-27 11:00:00 -0800,3 +9,7,2020-05-01 15:00:00 -0800,2020-05-16 11:00:00 -0800,2 +10,8,2020-05-25 15:00:00 -0800,2020-06-02 11:00:00 -0800,4 diff --git a/test/test_data/reservations.csv b/test/test_data/reservations.csv new file mode 100644 index 000000000..bbc7016be --- /dev/null +++ b/test/test_data/reservations.csv @@ -0,0 +1,11 @@ +id,customer_id,start_date,end_date +1,1,2020-05-01 15:00:00 -0800,2020-05-10 11:00:00 -0800 +2,2,2020-05-01 15:00:00 -0800,2020-05-04 11:00:00 -0800 +3,3,2020-05-02 15:00:00 -0800,2020-05-15 11:00:00 -0800 +4,4,2020-05-15 15:00:00 -0800,2020-05-30 11:00:00 -0800 +5,2,2020-05-28 15:00:00 -0800,2020-05-30 11:00:00 -0800 +6,3,2020-05-25 15:00:00 -0800,2020-05-29 11:00:00 -0800 +7,4,2020-05-28 15:00:00 -0800,2020-05-29 11:00:00 -0800 +8,6,2020-05-25 15:00:00 -0800,2020-05-27 11:00:00 -0800 +9,7,2020-05-01 15:00:00 -0800,2020-05-16 11:00:00 -0800 +10,8,2020-05-28 15:00:00 -0800,2020-06-01 11:00:00 -0800 diff --git a/test/test_data/rooms.csv b/test/test_data/rooms.csv new file mode 100644 index 000000000..f0f31e325 --- /dev/null +++ b/test/test_data/rooms.csv @@ -0,0 +1,21 @@ +id,price +1,150 +2,150 +3,150 +4,200 +5,200 +6,200 +7,250 +8,250 +9,250 +10,300 +11,300 +12,300 +13,300 +14,400 +15,400 +16,400 +17,500 +18,500 +19,500 +20,1000 \ No newline at end of file From 435462afc26b2af2965561b35d1f6a826de61fc2 Mon Sep 17 00:00:00 2001 From: Jocelyn Wang Date: Wed, 4 Mar 2020 16:51:52 -0800 Subject: [PATCH 26/43] cleaned up test for csv record instantiation --- lib/csv_record.rb | 23 ++++++++ test/csv_record_test.rb | 122 ++++++++++++++++++++++++++++++++++++++++ test/test_helper.rb | 3 +- 3 files changed, 147 insertions(+), 1 deletion(-) create mode 100644 lib/csv_record.rb create mode 100644 test/csv_record_test.rb diff --git a/lib/csv_record.rb b/lib/csv_record.rb new file mode 100644 index 000000000..0c2a7690c --- /dev/null +++ b/lib/csv_record.rb @@ -0,0 +1,23 @@ +require 'csv' +require 'pry' + +module HotelManager + class CsvRecord + + attr_reader :id + + def initialize(id) + # self.class.validate_id(id) + @id = id + validate_id + end + + def validate_id + if @id.nil? || @id <= 0 + raise ArgumentError, 'ID cannot be blank or less than one.' + end + end + + + end +end \ No newline at end of file diff --git a/test/csv_record_test.rb b/test/csv_record_test.rb new file mode 100644 index 000000000..be68e69ca --- /dev/null +++ b/test/csv_record_test.rb @@ -0,0 +1,122 @@ +require_relative 'test_helper' + +TEST_DATA_DIR = 'test/test_data' + +describe HotelManager::CsvRecord do + + it 'is an instance of Hotel Manager' do + record = HotelManager::CsvRecord.new(1) + expect(record.id).must_equal 1 + end + + it 'raises error if invalid id is used' do + expect {HotelManager::CsvRecord.new(-7)}.must_raise ArgumentError + expect {HotelManager::CsvRecord.new("One")}.must_raise ArgumentError + end +end + +describe "blah" do + + xdescribe 'load_all' do + it "raises an error if neither full_path nor directory is provided" do + expect { + HotelManager::CsvRecord.load_all + }.must_raise ArgumentError + end + + it "raises an error if invoked directly (without subclassing)" do + full_path = "#{TEST_DATA_DIR}/testrecords.csv" + expect { + HotelManager::CsvRecord.load_all(full_path: full_path) + }.must_raise NotImplementedError + end + end + + xdescribe 'validate_id' do + it 'accepts natural numbers' do + # Should not raise + [1, 10, 9999].each do |id| + HotelManager::CsvRecord.validate_id(id) + end + end + + it 'raises for negative numbers and 0' do + [0, -1, -10, -9999].each do |id| + expect { + HotelManager::CsvRecord.validate_id(id) + }.must_raise ArgumentError + end + end + + it 'raises for nil' do + expect { + HotelManager::CsvRecord.validate_id(nil) + }.must_raise ArgumentError + end + end + + xdescribe 'extension' do + # It's a class that's designed to be extended. + # How do you test that? Extend it! + class TestRecord < HotelManager::CsvRecord + attr_reader :name + def initialize(id:, name:) + super(id) + @name = name + end + + def self.load_all(*args, **kwargs) + @call_count = 0 + super + end + + def self.from_csv(record) + new(**record) + @call_count ||= 0 + @call_count += 1 + end + + class << self + attr_reader :call_count + end + end + + xdescribe 'load_all' do + let(:record_count) { + CSV.read("#{TEST_DATA_DIR}/testrecords.csv", headers: true).length + } + + it 'finds data given just a directory' do + records = TestRecord.load_all(directory: TEST_DATA_DIR) + + expect(records.length).must_equal record_count + end + + it 'finds data given a directory and filename' do + file_name = 'custom_filename_test.csv' + records = TestRecord.load_all(directory: TEST_DATA_DIR, file_name: file_name) + + expect(records.length).must_equal record_count + end + + it 'finds data given a full path' do + path = "#{TEST_DATA_DIR}/custom_filename_test.csv" + records = TestRecord.load_all(full_path: path) + + expect(records.length).must_equal record_count + end + + it 'calls `from_csv` for each record in the file' do + TestRecord.load_all(directory: TEST_DATA_DIR) + expect(TestRecord.call_count).must_equal record_count + end + end + end + + + +end + +# only accept data entries with ids +# loads in information from files +# argument error if no full_path or directory is provided diff --git a/test/test_helper.rb b/test/test_helper.rb index 9496e3e24..4c7eac24c 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -17,4 +17,5 @@ require_relative '../lib/reservation.rb' require_relative '../lib/room.rb' require_relative '../lib/reservation_manager.rb' -require_relative '../lib/reservation_block.rb' \ No newline at end of file +require_relative '../lib/reservation_block.rb' +require_relative '../lib/csv_record.rb' \ No newline at end of file From 199e02e1aec5eff23065c1c2b388d3a79b30286b Mon Sep 17 00:00:00 2001 From: Jocelyn Wang Date: Wed, 4 Mar 2020 17:03:17 -0800 Subject: [PATCH 27/43] updated load all section to pass tests --- lib/csv_record.rb | 22 ++++++++++++++++++++++ test/csv_record_test.rb | 12 +++++++++--- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/lib/csv_record.rb b/lib/csv_record.rb index 0c2a7690c..28ca6d0a6 100644 --- a/lib/csv_record.rb +++ b/lib/csv_record.rb @@ -18,6 +18,28 @@ def validate_id end end + def self.load_all(full_path: nil, directory: nil, file_name: nil) + full_path ||= build_path(directory, file_name) + + return CSV.read( + full_path, + headers: true, + header_converters: :symbol, + converters: :numeric + ).map { |record| from_csv(record) } + end + + def self.build_path(directory, file_name) + raise ArgumentError, "Either full_path or directory is required" if directory.nil? + + unless file_name + class_name = self.to_s.split('::').last + file_name = "#{class_name.downcase}s.csv" + end + + return "#{directory}/#{file_name}" + end + end end \ No newline at end of file diff --git a/test/csv_record_test.rb b/test/csv_record_test.rb index be68e69ca..32f67e372 100644 --- a/test/csv_record_test.rb +++ b/test/csv_record_test.rb @@ -2,9 +2,8 @@ TEST_DATA_DIR = 'test/test_data' -describe HotelManager::CsvRecord do - - it 'is an instance of Hotel Manager' do +describe 'CSV Record Class' do + it 'is an instance of CSV Record' do record = HotelManager::CsvRecord.new(1) expect(record.id).must_equal 1 end @@ -15,6 +14,13 @@ end end +describe 'CSV Record Class - Load All' do + it "raise argument error if invalid file path" do + expect {HotelManager::CsvRecord.load_all}.must_raise ArgumentError + end + +end + describe "blah" do xdescribe 'load_all' do From 4adadc292867b0f30bf9a4734f3dba8b84d7cf46 Mon Sep 17 00:00:00 2001 From: Jocelyn Wang Date: Wed, 4 Mar 2020 17:14:03 -0800 Subject: [PATCH 28/43] updated load all test to require implementation in child class --- lib/csv_record.rb | 4 ++++ test/csv_record_test.rb | 7 ++++++- test/test_data/generate_test_data.rb | 0 3 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 test/test_data/generate_test_data.rb diff --git a/lib/csv_record.rb b/lib/csv_record.rb index 28ca6d0a6..4587ecb83 100644 --- a/lib/csv_record.rb +++ b/lib/csv_record.rb @@ -29,6 +29,10 @@ def self.load_all(full_path: nil, directory: nil, file_name: nil) ).map { |record| from_csv(record) } end + def self.from_csv(record) + raise NotImplementedError, 'Implement me in a child class!' + end + def self.build_path(directory, file_name) raise ArgumentError, "Either full_path or directory is required" if directory.nil? diff --git a/test/csv_record_test.rb b/test/csv_record_test.rb index 32f67e372..6e9066e26 100644 --- a/test/csv_record_test.rb +++ b/test/csv_record_test.rb @@ -19,6 +19,11 @@ expect {HotelManager::CsvRecord.load_all}.must_raise ArgumentError end + it "raises an error if invoked directly (without subclassing)" do + full_path = "#{TEST_DATA_DIR}/reservations.csv" + expect {HotelManager::CsvRecord.load_all(full_path: full_path)}.must_raise NotImplementedError + end + end describe "blah" do @@ -31,7 +36,7 @@ end it "raises an error if invoked directly (without subclassing)" do - full_path = "#{TEST_DATA_DIR}/testrecords.csv" + full_path = "#{TEST_DATA_DIR}/reservations.csv" expect { HotelManager::CsvRecord.load_all(full_path: full_path) }.must_raise NotImplementedError diff --git a/test/test_data/generate_test_data.rb b/test/test_data/generate_test_data.rb new file mode 100644 index 000000000..e69de29bb From 89f0332dff5cef26f9181bc9bf7df0a5059d5f88 Mon Sep 17 00:00:00 2001 From: Jocelyn Wang Date: Thu, 5 Mar 2020 08:23:32 -0800 Subject: [PATCH 29/43] updated load all tests and removed the original template code --- test/csv_record_test.rb | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/test/csv_record_test.rb b/test/csv_record_test.rb index 6e9066e26..8efd9181a 100644 --- a/test/csv_record_test.rb +++ b/test/csv_record_test.rb @@ -19,6 +19,11 @@ expect {HotelManager::CsvRecord.load_all}.must_raise ArgumentError end + it "raises an error if invoked directly (without subclassing)" do + record = HotelManager::CsvRecord.new(1) + record.build_path(TEST_DATA_DIR,"") + end + it "raises an error if invoked directly (without subclassing)" do full_path = "#{TEST_DATA_DIR}/reservations.csv" expect {HotelManager::CsvRecord.load_all(full_path: full_path)}.must_raise NotImplementedError @@ -28,20 +33,6 @@ describe "blah" do - xdescribe 'load_all' do - it "raises an error if neither full_path nor directory is provided" do - expect { - HotelManager::CsvRecord.load_all - }.must_raise ArgumentError - end - - it "raises an error if invoked directly (without subclassing)" do - full_path = "#{TEST_DATA_DIR}/reservations.csv" - expect { - HotelManager::CsvRecord.load_all(full_path: full_path) - }.must_raise NotImplementedError - end - end xdescribe 'validate_id' do it 'accepts natural numbers' do From 77473d3aa5a54553e242f3f49592826d717c0ffe Mon Sep 17 00:00:00 2001 From: Jocelyn Wang Date: Thu, 5 Mar 2020 20:39:06 -0800 Subject: [PATCH 30/43] updates to comments --- lib/csv_record.rb | 33 ++++++++++++++++++--------------- lib/reservation.rb | 6 +++--- lib/reservation_manager.rb | 9 +++++---- test/csv_record_test.rb | 10 ++++++++-- 4 files changed, 34 insertions(+), 24 deletions(-) diff --git a/lib/csv_record.rb b/lib/csv_record.rb index 4587ecb83..bd192267d 100644 --- a/lib/csv_record.rb +++ b/lib/csv_record.rb @@ -3,24 +3,18 @@ module HotelManager class CsvRecord - attr_reader :id def initialize(id) - # self.class.validate_id(id) + self.class.validate_id(id) @id = id - validate_id - end - - def validate_id - if @id.nil? || @id <= 0 - raise ArgumentError, 'ID cannot be blank or less than one.' - end end - + + # Takes either full_path or directory and optional file_name + # Default file name matches class name def self.load_all(full_path: nil, directory: nil, file_name: nil) full_path ||= build_path(directory, file_name) - + return CSV.read( full_path, headers: true, @@ -29,13 +23,23 @@ def self.load_all(full_path: nil, directory: nil, file_name: nil) ).map { |record| from_csv(record) } end + def self.validate_id(id) + if id.nil? || id <= 0 + raise ArgumentError, 'ID cannot be blank or less than one.' + end + end + + private + def self.from_csv(record) raise NotImplementedError, 'Implement me in a child class!' end def self.build_path(directory, file_name) - raise ArgumentError, "Either full_path or directory is required" if directory.nil? - + unless directory + raise ArgumentError, "Either full_path or directory is required" + end + unless file_name class_name = self.to_s.split('::').last file_name = "#{class_name.downcase}s.csv" @@ -43,7 +47,6 @@ def self.build_path(directory, file_name) return "#{directory}/#{file_name}" end - - + end end \ No newline at end of file diff --git a/lib/reservation.rb b/lib/reservation.rb index 290c5bbee..3c7efe30a 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -30,10 +30,10 @@ def initialize id:, # Check input validation def validate_date - if @start_date.class != Date || @end_date.class != Date - raise ArgumentError, "Date #{@start_date} or #{@end_date} not valid" + if !@start_date.is_a?(Date) || !@end_date.is_a?(Date) + raise ArgumentError, "Expected #{@start_date} and #{@end_date} to be date" elsif @start_date >= @end_date - raise ArgumentError, "#{@start_date} is before #{@end_date} " + raise ArgumentError, "#{@start_date} must be before #{@end_date}" end end diff --git a/lib/reservation_manager.rb b/lib/reservation_manager.rb index f0eb6322a..bc5b72fa7 100644 --- a/lib/reservation_manager.rb +++ b/lib/reservation_manager.rb @@ -27,10 +27,10 @@ def save_reservation(first_date, second_date, num_of_rooms: 1, customer_id: nil, # change list_room_by_range to return argument rather than string available_rooms = list_room_by_range(first_date,second_date) - if available_rooms.is_a? Array - chosen_rooms = available_rooms.select.with_index do |room, index| - room.id if index < num_of_rooms - end + if available_rooms.is_a? Array # Change to check !.empty? + # add in error handling if available rooms < num_of_rooms + # get rid of if check after implmenting the above + chosen_rooms = available_rooms.take(num_of_rooms) else raise ArgumentError, "Something's not right here..." end @@ -148,6 +148,7 @@ def list_room_by_range(first_date, second_date) return reservation_found?(available_rooms, "rooms") end + # GET RID OF -- return available rooms as [] def reservation_found? tracker, type tracker.empty? ? "No #{type} available in date range." : tracker end diff --git a/test/csv_record_test.rb b/test/csv_record_test.rb index 8efd9181a..cd95a9de4 100644 --- a/test/csv_record_test.rb +++ b/test/csv_record_test.rb @@ -12,6 +12,12 @@ expect {HotelManager::CsvRecord.new(-7)}.must_raise ArgumentError expect {HotelManager::CsvRecord.new("One")}.must_raise ArgumentError end + + xit 'validates that id accepts natural numbers' do + [1, 10, 9999].each {|id| HotelManager::CsvRecord.validate_id(id)} + end + + end describe 'CSV Record Class - Load All' do @@ -19,7 +25,7 @@ expect {HotelManager::CsvRecord.load_all}.must_raise ArgumentError end - it "raises an error if invoked directly (without subclassing)" do + xit "raises an error if invoked directly (without subclassing)" do record = HotelManager::CsvRecord.new(1) record.build_path(TEST_DATA_DIR,"") end @@ -28,9 +34,9 @@ full_path = "#{TEST_DATA_DIR}/reservations.csv" expect {HotelManager::CsvRecord.load_all(full_path: full_path)}.must_raise NotImplementedError end - end + describe "blah" do From 22252753025481eb09ba22afec779ccbd2e052e2 Mon Sep 17 00:00:00 2001 From: Jocelyn Wang Date: Thu, 5 Mar 2020 21:10:57 -0800 Subject: [PATCH 31/43] updated tests and related code to look off reservation block rather than reservation --- lib/reservation_block.rb | 2 +- lib/reservation_manager.rb | 8 ++-- test/reservation_block_test.rb | 2 +- test/reservation_manager_test.rb | 82 ++++++++++++++++---------------- 4 files changed, 46 insertions(+), 48 deletions(-) diff --git a/lib/reservation_block.rb b/lib/reservation_block.rb index 18542c691..bee6d2052 100644 --- a/lib/reservation_block.rb +++ b/lib/reservation_block.rb @@ -33,7 +33,7 @@ def initialize id: , def validate_room(attribute) raise ArgumentError, "Room or room_id is required" if @rooms.nil? && @room_ids.nil? - if attribute.length <= 1 || attribute.length > 5 + if attribute.length < 1 || attribute.length > 5 raise ArgumentError, "#{attribute.length} is an invalid number of rooms for a hotel block" end end diff --git a/lib/reservation_manager.rb b/lib/reservation_manager.rb index bc5b72fa7..3c55e3eb5 100644 --- a/lib/reservation_manager.rb +++ b/lib/reservation_manager.rb @@ -38,14 +38,14 @@ def save_reservation(first_date, second_date, num_of_rooms: 1, customer_id: nil, chosen_rooms.map!{|room| room.id} if num_of_rooms == 1 - class_storage = @reservations - new_reservation = HotelManager::Reservation.new( - id: @reservations.length + 1, + class_storage = @reservation_blocks + new_reservation = HotelManager::ReservationBlock.new( + id: @reservation_blocks.length + 1, customer_id: customer_id, start_date: first_date, end_date: second_date, room_cost: room_cost, - room_id: chosen_rooms[0] + room_ids: chosen_rooms ) else class_storage = @reservation_blocks diff --git a/test/reservation_block_test.rb b/test/reservation_block_test.rb index 058c86da6..3c00f45fa 100644 --- a/test/reservation_block_test.rb +++ b/test/reservation_block_test.rb @@ -29,7 +29,7 @@ end it "raises argument if block only has one room" do - @rooms = [HotelManager::Room.new(id: 1)] + @rooms = nil @reservation_data[:rooms] = @rooms expect{HotelManager::ReservationBlock.new(@reservation_data)}.must_raise ArgumentError end diff --git a/test/reservation_manager_test.rb b/test/reservation_manager_test.rb index 3c2c0b82b..d51175d90 100644 --- a/test/reservation_manager_test.rb +++ b/test/reservation_manager_test.rb @@ -8,7 +8,6 @@ it "is an instance of Reservation Manager" do expect(@sample).must_be_kind_of HotelManager::ReservationManager expect(@sample.rooms).must_be_kind_of Array - expect(@sample.reservations).must_be_kind_of Array expect(@sample.reservation_blocks).must_be_kind_of Array end @@ -31,7 +30,7 @@ start_date: Date.new(2020,3,2), end_date: Date.new(2020,3,5), room_cost: 200, - room_id: 1 + room_ids: [1] } @res_two_data = { id: 2, @@ -39,7 +38,7 @@ start_date: Date.new(2020,3,15), end_date: Date.new(2020,3,24), room_cost: 200, - room_id: 1 + room_ids: [1] } @rooms = [7, 8, 9] @res_block_data = { @@ -51,22 +50,21 @@ room_ids: @rooms } - @res_one = HotelManager::Reservation.new(@res_one_data) - @res_two = HotelManager::Reservation.new(@res_two_data) + @res_one = HotelManager::ReservationBlock.new(@res_one_data) + @res_two = HotelManager::ReservationBlock.new(@res_two_data) @res_block = HotelManager::ReservationBlock.new(@res_block_data) @sample_two = HotelManager::ReservationManager.new() - @sample_two.add_reservation(@res_one, @sample_two.reservations) + @sample_two.add_reservation(@res_one, @sample_two.reservation_blocks) - @sample_two.add_reservation(@res_two, @sample_two.reservations) + @sample_two.add_reservation(@res_two, @sample_two.reservation_blocks) @sample_two.add_reservation(@res_block, @sample_two.reservation_blocks) end - describe "ability to add reservations to the Reservation Manager" do - it "able to add reservations to Reservation Manager" do - expect(@sample_two.reservations.length).must_equal 2 - expect(@sample_two.reservation_blocks.length).must_equal 1 + describe "ability to add reservation_blocks to the Reservation Manager" do + it "able to add reservation_blocks to Reservation Manager" do + expect(@sample_two.reservation_blocks.length).must_equal 3 end it "room available after other block end date - block" do @@ -82,7 +80,7 @@ @res_three = HotelManager::ReservationBlock.new(@res_three_data) @sample_two.add_reservation(@res_three, @sample_two.reservation_blocks) - expect(@sample_two.reservations.length).must_equal 2 + expect(@sample_two.reservation_blocks.length).must_equal 4 end it "room available after other individual end date - individual" do @@ -92,13 +90,13 @@ start_date: Date.new(2020,3,24), end_date: Date.new(2020,3,28), room_cost: 200, - room_id: 1 + room_ids: [1] } - @res_four = HotelManager::Reservation.new(@res_four_data) - @sample_two.add_reservation(@res_four, @sample_two.reservations) + @res_four = HotelManager::ReservationBlock.new(@res_four_data) + @sample_two.add_reservation(@res_four, @sample_two.reservation_blocks) - expect(@sample_two.reservations.length).must_equal 3 + expect(@sample_two.reservation_blocks.length).must_equal 4 end it "room available after individual end date - block" do @@ -115,7 +113,7 @@ @res_six = HotelManager::ReservationBlock.new(@res_six_data) @sample_two.add_reservation(@res_six, @sample_two.reservation_blocks) - expect(@sample_two.reservations.length).must_equal 2 + expect(@sample_two.reservation_blocks.length).must_equal 4 end it "room available after block end date - individual" do @@ -125,13 +123,13 @@ start_date: Date.new(2020,3,18), end_date: Date.new(2020,3,25), room_cost: 200, - room_id: 7 + room_ids: [7] } - @res_seven = HotelManager::Reservation.new(@res_seven_data) - @sample_two.add_reservation(@res_seven,@sample_two.reservations) + @res_seven = HotelManager::ReservationBlock.new(@res_seven_data) + @sample_two.add_reservation(@res_seven,@sample_two.reservation_blocks) - expect(@sample_two.reservations.length).must_equal 3 + expect(@sample_two.reservation_blocks.length).must_equal 4 end it "raise argument error if individual overbooking attempted on individual" do @@ -141,11 +139,11 @@ start_date: Date.new(2020,3,23), end_date: Date.new(2020,3,28), room_cost: 200, - room_id: 1 + room_ids: [1] } - @res_five = HotelManager::Reservation.new(@res_five_data) - expect{@sample_two.add_reservation(@res_five,@sample_two.reservations)}.must_raise ArgumentError + @res_five = HotelManager::ReservationBlock.new(@res_five_data) + expect{@sample_two.add_reservation(@res_five,@sample_two.reservation_blocks)}.must_raise ArgumentError end it "raise argument error if individual overbooking attempted on block" do @@ -155,17 +153,17 @@ start_date: Date.new(2020,3,4), end_date: Date.new(2020,3,10), room_cost: 200, - room_id: 7 + room_ids: [7] } - @res_five = HotelManager::Reservation.new(@res_five_data) + @res_five = HotelManager::ReservationBlock.new(@res_five_data) - expect{@sample_two.add_reservation(@res_five,@sample_two.reservations)}.must_raise ArgumentError + expect{@sample_two.add_reservation(@res_five,@sample_two.reservation_blocks)}.must_raise ArgumentError end it "raise argument error if block overbooking attempted on block" do @res_five = HotelManager::ReservationBlock.new(@res_block_data) - expect{@sample_two.add_reservation(@res_five,@sample_two.reservations)}.must_raise ArgumentError + expect{@sample_two.add_reservation(@res_five,@sample_two.reservation_blocks)}.must_raise ArgumentError end it "raise argument error if block overbooking attempted on individual" do @@ -229,7 +227,7 @@ expect(@search_result[1].id).must_equal 3 end - it "raise argument error if no reservations on that date" do + it "raise argument error if no reservation_blocks on that date" do @search_result = @sample_two.search_by_date(Date.new(2020,4,1)) expect(@search_result).must_include "No reservations available in date range." end @@ -271,31 +269,31 @@ end it "create and save reservation providing only dates" do - expect(@sample.reservations.length).must_equal 2 + expect(@sample.reservation_blocks.length).must_equal 4 end it "verify that next available rooms were used - individual" do @sample.save_reservation(Date.new(2020,3,4),Date.new(2020,3,7)) @sample.save_reservation(Date.new(2020,3,10),Date.new(2020,3,17)) - - expect(@sample.reservations[0].room_id).must_equal 1 - expect(@sample.reservations[1].room_id).must_equal 2 - expect(@sample.reservations[2].room_id).must_equal 12 - expect(@sample.reservations[3].room_id).must_equal 1 + + expect(@sample.reservation_blocks[0].room_ids).must_equal [1] + expect(@sample.reservation_blocks[1].room_ids).must_equal [2] + expect(@sample.reservation_blocks[4].room_ids).must_equal [12] + expect(@sample.reservation_blocks[5].room_ids).must_equal [1] end it "create and save reservation blocks providing only dates" do - expect(@sample.reservation_blocks.length).must_equal 2 + expect(@sample.reservation_blocks.length).must_equal 4 end it "verify that next available rooms were used - block" do @sample.save_reservation(Date.new(2020,3,4),Date.new(2020,3,7),num_of_rooms:4) @sample.save_reservation(Date.new(2020,3,10),Date.new(2020,3,15),num_of_rooms:5) - expect(@sample.reservation_blocks[0].room_ids).must_equal [3,4,5,6,7] - expect(@sample.reservation_blocks[1].room_ids).must_equal [8,9,10,11] - expect(@sample.reservation_blocks[2].room_ids).must_equal [12,13,14,15] - expect(@sample.reservation_blocks[3].room_ids).must_equal [1,2,3,4,5] + expect(@sample.reservation_blocks[2].room_ids).must_equal [3,4,5,6,7] + expect(@sample.reservation_blocks[3].room_ids).must_equal [8,9,10,11] + expect(@sample.reservation_blocks[4].room_ids).must_equal [12,13,14,15] + expect(@sample.reservation_blocks[5].room_ids).must_equal [1,2,3,4,5] end it "raises arugment error if no rooms are available - individual" do @@ -316,8 +314,8 @@ # List of available rooms will not include the rooms in block reservation # Correctly list out whether a given block has any rooms available? -# Able to reserve a specific room from a hotel block, but only for the full duration of the block (individual reservations still work?) -# List of reservation by date will list out both individual and block reservations +# Able to reserve a specific room from a hotel block, but only for the full duration of the block (individual reservation_blocks still work?) +# List of reservation by date will list out both individual and block reservation_blocks # Check to make sure that when a block is reserved, the indivudal room reservation dates will also be there # Confirm that all availabilty checking logic from Wave 2 also respects room blocks From 1e009231abfe2068e400bd3215ffa10c16657ee7 Mon Sep 17 00:00:00 2001 From: Jocelyn Wang Date: Thu, 5 Mar 2020 21:17:05 -0800 Subject: [PATCH 32/43] migrate reservation methods over to reservation block --- lib/reservation.rb | 54 ---------------------------------------- lib/reservation_block.rb | 25 +++++++++++++++++-- test/reservation_test.rb | 2 +- 3 files changed, 24 insertions(+), 57 deletions(-) diff --git a/lib/reservation.rb b/lib/reservation.rb index 3c7efe30a..1272169d4 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -4,59 +4,5 @@ module HotelManager class Reservation - attr_reader :id, :customer_id, :start_date, :end_date, :room_cost, :room, :room_id - - # Room default cost of $200 - def initialize id:, - customer_id:, - start_date:, - end_date:, - room_cost: 200, - room: nil, - room_id: nil - - @id = id - @customer_id = customer_id - @start_date = start_date - @end_date = end_date - @room_cost = room_cost - @room = room - @room_id = room_id - - # Raise exception for missing or invalid inputs - validate_date - validate_room - end - - # Check input validation - def validate_date - if !@start_date.is_a?(Date) || !@end_date.is_a?(Date) - raise ArgumentError, "Expected #{@start_date} and #{@end_date} to be date" - elsif @start_date >= @end_date - raise ArgumentError, "#{@start_date} must be before #{@end_date}" - end - end - - def validate_room - raise ArgumentError, "Room or room_id is required" if room.nil? && room_id.nil? - end - - # Calculate cost of reservation, exclusive of last date - def total_cost - @room_cost * (@end_date - @start_date - 1) - end - - # Checks if reservation exists on a specific date - def check_date(search_date) - (search_date >= @start_date) && (search_date <= @end_date) - end - - # Checks whether reservation is within a given date range - def check_reservation_range(date_one, date_two) - first_date = date_one < date_two ? date_one : date_two - second_date = date_one < date_two ? date_two : date_one - first_date < @end_date && second_date > @start_date - end - end end \ No newline at end of file diff --git a/lib/reservation_block.rb b/lib/reservation_block.rb index bee6d2052..3c7acfc13 100644 --- a/lib/reservation_block.rb +++ b/lib/reservation_block.rb @@ -38,9 +38,30 @@ def validate_room(attribute) end end - # Calculate cost of reservation (exclusive of last date) + # Check input validation + def validate_date + if !@start_date.is_a?(Date) || !@end_date.is_a?(Date) + raise ArgumentError, "Expected #{@start_date} and #{@end_date} to be date" + elsif @start_date >= @end_date + raise ArgumentError, "#{@start_date} must be before #{@end_date}" + end + end + + # Calculate cost of reservation, exclusive of last date def total_cost - super * @rooms.length + @room_cost * (@end_date - @start_date - 1) * @rooms.length + end + + # Checks if reservation exists on a specific date + def check_date(search_date) + (search_date >= @start_date) && (search_date <= @end_date) + end + + # Checks whether reservation is within a given date range + def check_reservation_range(date_one, date_two) + first_date = date_one < date_two ? date_one : date_two + second_date = date_one < date_two ? date_two : date_one + first_date < @end_date && second_date > @start_date end end diff --git a/test/reservation_test.rb b/test/reservation_test.rb index 0e91c8d4d..361c8f626 100644 --- a/test/reservation_test.rb +++ b/test/reservation_test.rb @@ -1,6 +1,6 @@ require_relative 'test_helper' -describe "Reservation Class" do +xdescribe "Reservation Class" do before do @reservation_data = { id: 1, From 00dfceab696f040a344f45ab59bf231a7b783100 Mon Sep 17 00:00:00 2001 From: Jocelyn Wang Date: Thu, 5 Mar 2020 21:36:37 -0800 Subject: [PATCH 33/43] deleted reservation class and related code --- lib/reservation.rb | 8 ----- lib/reservation_block.rb | 3 +- lib/reservation_manager.rb | 11 ++----- test/reservation_manager_test.rb | 4 +-- test/reservation_test.rb | 50 -------------------------------- test/test_helper.rb | 1 - 6 files changed, 5 insertions(+), 72 deletions(-) delete mode 100644 lib/reservation.rb delete mode 100644 test/reservation_test.rb diff --git a/lib/reservation.rb b/lib/reservation.rb deleted file mode 100644 index 1272169d4..000000000 --- a/lib/reservation.rb +++ /dev/null @@ -1,8 +0,0 @@ -require 'date' -require 'pry' - -module HotelManager - class Reservation - - end -end \ No newline at end of file diff --git a/lib/reservation_block.rb b/lib/reservation_block.rb index 3c7acfc13..f0cf2ba97 100644 --- a/lib/reservation_block.rb +++ b/lib/reservation_block.rb @@ -1,11 +1,10 @@ require 'date' require 'pry' -require_relative 'reservation' require_relative 'room' module HotelManager - class ReservationBlock < Reservation + class ReservationBlock attr_reader :id, :customer_id, :start_date, :end_date, :room_cost, :rooms, :room_ids diff --git a/lib/reservation_manager.rb b/lib/reservation_manager.rb index 3c55e3eb5..71011e95e 100644 --- a/lib/reservation_manager.rb +++ b/lib/reservation_manager.rb @@ -1,7 +1,6 @@ require 'pry' require 'date' -require_relative 'reservation' require_relative 'reservation_block' require_relative 'room' @@ -66,13 +65,9 @@ def save_reservation(first_date, second_date, num_of_rooms: 1, customer_id: nil, def add_reservation(reservation, class_storage) potential_rooms = self.list_room_by_range(reservation.start_date, reservation.end_date) - if reservation.class == HotelManager::Reservation - found_room = potential_rooms.include? find_room(reservation.room_id) - elsif reservation.class == HotelManager::ReservationBlock - found_room = reservation.room_ids.map do |room_id| - potential_rooms.include? find_room(room_id) - end.all? (true) - end + found_room = reservation.room_ids.map do |room_id| + potential_rooms.include? find_room(room_id) + end.all? (true) if found_room class_storage << reservation diff --git a/test/reservation_manager_test.rb b/test/reservation_manager_test.rb index d51175d90..237d77400 100644 --- a/test/reservation_manager_test.rb +++ b/test/reservation_manager_test.rb @@ -200,7 +200,6 @@ @search_result = @sample_two.search_by_room_date(1, Date.new(2020,3,1), Date.new(2020,3,10)) expect(@search_result).must_be_kind_of Array - expect(@search_result[0]).must_be_kind_of HotelManager::Reservation expect(@search_result[1]).must_be_kind_of HotelManager::ReservationBlock expect(@search_result[0].id).must_equal 1 expect(@search_result[1].id).must_equal 4 @@ -221,8 +220,7 @@ it "able to list reservation by specific date" do @search_result = @sample_two.search_by_date(Date.new(2020,3,17)) expect(@search_result).must_be_kind_of Array - expect(@search_result[0]).must_be_kind_of HotelManager::Reservation - expect(@search_result[1]).must_be_kind_of HotelManager::ReservationBlock + expect(@search_result[0]).must_be_kind_of HotelManager::ReservationBlock expect(@search_result[0].id).must_equal 2 expect(@search_result[1].id).must_equal 3 end diff --git a/test/reservation_test.rb b/test/reservation_test.rb deleted file mode 100644 index 361c8f626..000000000 --- a/test/reservation_test.rb +++ /dev/null @@ -1,50 +0,0 @@ -require_relative 'test_helper' - -xdescribe "Reservation Class" do - before do - @reservation_data = { - id: 1, - customer_id: 1, - start_date: Date.new(2020,3,2), - end_date: Date.new(2020,3,5), - room: HotelManager::Room.new(id: 1) - } - @reservation = HotelManager::Reservation.new(@reservation_data) - end - - it "is an instance of Reservation" do - expect(@reservation).must_be_kind_of HotelManager::Reservation - end - - it "raises argument error if no room or room id provided" do - @reservation_data.delete(:room) - expect{HotelManager::Reservation.new(@reservation_data)}.must_raise ArgumentError - end - - it "validates that start/end date is a date class" do - expect(@reservation.end_date).must_be_kind_of Date - expect(@reservation.start_date).must_be_kind_of Date - end - - it "raises argument error if start is not date class" do - @reservation_data[:start_date] = "Janurary 5th, 2020" - expect{HotelManager::Reservation.new(@reservation_data)}.must_raise ArgumentError - end - - it "raises argument error if end before start date" do - @reservation_data[:start_date] = Date.new(2020,3,5) - @reservation_data[:end_date] = Date.new(2020,3,2) - expect{HotelManager::Reservation.new(@reservation_data)}.must_raise ArgumentError - end - - it "raises argument error if end and start date are the same" do - @reservation_data[:start_date] = Date.new(2020,3,5) - @reservation_data[:end_date] = Date.new(2020,3,5) - expect{HotelManager::Reservation.new(@reservation_data)}.must_raise ArgumentError - end - - it "calculates total cost correctly" do - reservation_cost = @reservation.total_cost() - expect(reservation_cost).must_equal 400 - end -end \ No newline at end of file diff --git a/test/test_helper.rb b/test/test_helper.rb index 4c7eac24c..e5d15cc5c 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -14,7 +14,6 @@ # Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new # require_relative your lib files here! -require_relative '../lib/reservation.rb' require_relative '../lib/room.rb' require_relative '../lib/reservation_manager.rb' require_relative '../lib/reservation_block.rb' From e731461fdab38e5673edd7b0de0d7c360b52fe57 Mon Sep 17 00:00:00 2001 From: Jocelyn Wang Date: Thu, 5 Mar 2020 21:38:52 -0800 Subject: [PATCH 34/43] removed reservations holder and related code --- lib/reservation_manager.rb | 48 ++++++++------------------------------ 1 file changed, 10 insertions(+), 38 deletions(-) diff --git a/lib/reservation_manager.rb b/lib/reservation_manager.rb index 71011e95e..116ccb1ce 100644 --- a/lib/reservation_manager.rb +++ b/lib/reservation_manager.rb @@ -7,12 +7,11 @@ module HotelManager class ReservationManager - attr_reader :rooms, :reservations, :reservation_blocks + attr_reader :rooms, :reservation_blocks # Populate hotel with 20 rooms upon initialization def initialize @rooms = [] - @reservations = [] @reservation_blocks = [] 20.times do |index| @@ -36,27 +35,15 @@ def save_reservation(first_date, second_date, num_of_rooms: 1, customer_id: nil, chosen_rooms.map!{|room| room.id} - if num_of_rooms == 1 - class_storage = @reservation_blocks - new_reservation = HotelManager::ReservationBlock.new( - id: @reservation_blocks.length + 1, - customer_id: customer_id, - start_date: first_date, - end_date: second_date, - room_cost: room_cost, - room_ids: chosen_rooms - ) - else - class_storage = @reservation_blocks - new_reservation = HotelManager::ReservationBlock.new( - id: @reservation_blocks.length + 1, - customer_id: customer_id, - start_date: first_date, - end_date: second_date, - room_cost: room_cost, - room_ids: chosen_rooms - ) - end + class_storage = @reservation_blocks + new_reservation = HotelManager::ReservationBlock.new( + id: @reservation_blocks.length + 1, + customer_id: customer_id, + start_date: first_date, + end_date: second_date, + room_cost: room_cost, + room_ids: chosen_rooms + ) add_reservation(new_reservation,class_storage) end @@ -91,12 +78,6 @@ def search_by_room_date(room, first_date, second_date) raise ArgumentError, "Room #{room} does not exist" if @rooms.last.id < room reservation_room_date = [] - @reservations.each do |reservation| - if reservation.check_reservation_range(first_date, second_date) && reservation.room_id == room - reservation_room_date << reservation - end - end - @reservation_blocks.each do |reservation_block| found_room = reservation_block.room_ids.include? room if reservation_block.check_reservation_range(first_date,second_date) && found_room @@ -110,9 +91,6 @@ def search_by_room_date(room, first_date, second_date) # List out reservations by specific date def search_by_date(date) reservation_by_date = [] - @reservations.each do |reservation| - reservation_by_date << reservation if reservation.check_date(date) - end @reservation_blocks.each do |reservation_block| reservation_by_date << reservation_block if reservation_block.check_date(date) @@ -126,12 +104,6 @@ def list_room_by_range(first_date, second_date) available_rooms = @rooms.dup - @reservations.each do |reservation| - if reservation.check_reservation_range(first_date, second_date) - available_rooms -= [find_room(reservation.room_id)] - end - end - @reservation_blocks.each do |reservation_block| if reservation_block.check_reservation_range(first_date, second_date) reservation_block.room_ids.each do |room_id| From 683adf1456b0cfa912774bafe66114878c1517d1 Mon Sep 17 00:00:00 2001 From: Jocelyn Wang Date: Thu, 5 Mar 2020 21:44:40 -0800 Subject: [PATCH 35/43] updated add reservation to only work with reservation blocks --- lib/reservation_manager.rb | 11 +++++------ test/reservation_manager_test.rb | 25 ++++++++++++------------- 2 files changed, 17 insertions(+), 19 deletions(-) diff --git a/lib/reservation_manager.rb b/lib/reservation_manager.rb index 116ccb1ce..f6f0b630a 100644 --- a/lib/reservation_manager.rb +++ b/lib/reservation_manager.rb @@ -34,8 +34,7 @@ def save_reservation(first_date, second_date, num_of_rooms: 1, customer_id: nil, end chosen_rooms.map!{|room| room.id} - - class_storage = @reservation_blocks + new_reservation = HotelManager::ReservationBlock.new( id: @reservation_blocks.length + 1, customer_id: customer_id, @@ -45,11 +44,11 @@ def save_reservation(first_date, second_date, num_of_rooms: 1, customer_id: nil, room_ids: chosen_rooms ) - add_reservation(new_reservation,class_storage) + add_reservation(new_reservation) end # Add reservation to instance variables - def add_reservation(reservation, class_storage) + def add_reservation(reservation) potential_rooms = self.list_room_by_range(reservation.start_date, reservation.end_date) found_room = reservation.room_ids.map do |room_id| @@ -57,7 +56,7 @@ def add_reservation(reservation, class_storage) end.all? (true) if found_room - class_storage << reservation + @reservation_blocks << reservation else raise ArgumentError, "#{reservation.class} double booked. Not saved to reservation manager." end @@ -103,7 +102,7 @@ def list_room_by_range(first_date, second_date) search_date_validation(first_date,second_date) available_rooms = @rooms.dup - + @reservation_blocks.each do |reservation_block| if reservation_block.check_reservation_range(first_date, second_date) reservation_block.room_ids.each do |room_id| diff --git a/test/reservation_manager_test.rb b/test/reservation_manager_test.rb index 237d77400..e82aca52b 100644 --- a/test/reservation_manager_test.rb +++ b/test/reservation_manager_test.rb @@ -56,10 +56,9 @@ @res_block = HotelManager::ReservationBlock.new(@res_block_data) @sample_two = HotelManager::ReservationManager.new() - @sample_two.add_reservation(@res_one, @sample_two.reservation_blocks) - - @sample_two.add_reservation(@res_two, @sample_two.reservation_blocks) - @sample_two.add_reservation(@res_block, @sample_two.reservation_blocks) + @sample_two.add_reservation(@res_one) + @sample_two.add_reservation(@res_two) + @sample_two.add_reservation(@res_block) end describe "ability to add reservation_blocks to the Reservation Manager" do @@ -78,7 +77,7 @@ } @res_three = HotelManager::ReservationBlock.new(@res_three_data) - @sample_two.add_reservation(@res_three, @sample_two.reservation_blocks) + @sample_two.add_reservation(@res_three) expect(@sample_two.reservation_blocks.length).must_equal 4 end @@ -94,7 +93,7 @@ } @res_four = HotelManager::ReservationBlock.new(@res_four_data) - @sample_two.add_reservation(@res_four, @sample_two.reservation_blocks) + @sample_two.add_reservation(@res_four) expect(@sample_two.reservation_blocks.length).must_equal 4 end @@ -111,7 +110,7 @@ } @res_six = HotelManager::ReservationBlock.new(@res_six_data) - @sample_two.add_reservation(@res_six, @sample_two.reservation_blocks) + @sample_two.add_reservation(@res_six) expect(@sample_two.reservation_blocks.length).must_equal 4 end @@ -127,7 +126,7 @@ } @res_seven = HotelManager::ReservationBlock.new(@res_seven_data) - @sample_two.add_reservation(@res_seven,@sample_two.reservation_blocks) + @sample_two.add_reservation(@res_seven) expect(@sample_two.reservation_blocks.length).must_equal 4 end @@ -143,7 +142,7 @@ } @res_five = HotelManager::ReservationBlock.new(@res_five_data) - expect{@sample_two.add_reservation(@res_five,@sample_two.reservation_blocks)}.must_raise ArgumentError + expect{@sample_two.add_reservation(@res_five)}.must_raise ArgumentError end it "raise argument error if individual overbooking attempted on block" do @@ -158,12 +157,12 @@ @res_five = HotelManager::ReservationBlock.new(@res_five_data) - expect{@sample_two.add_reservation(@res_five,@sample_two.reservation_blocks)}.must_raise ArgumentError + expect{@sample_two.add_reservation(@res_five)}.must_raise ArgumentError end it "raise argument error if block overbooking attempted on block" do @res_five = HotelManager::ReservationBlock.new(@res_block_data) - expect{@sample_two.add_reservation(@res_five,@sample_two.reservation_blocks)}.must_raise ArgumentError + expect{@sample_two.add_reservation(@res_five)}.must_raise ArgumentError end it "raise argument error if block overbooking attempted on individual" do @@ -179,7 +178,7 @@ @res_six = HotelManager::ReservationBlock.new(@res_six_data) - expect{@sample_two.add_reservation(@res_six, @sample_two.reservation_blocks)}.must_raise ArgumentError + expect{@sample_two.add_reservation(@res_six)}.must_raise ArgumentError end end @@ -196,7 +195,7 @@ } @res_block = HotelManager::ReservationBlock.new(@res_block_data) - @sample_two.add_reservation(@res_block,@sample_two.reservation_blocks) + @sample_two.add_reservation(@res_block) @search_result = @sample_two.search_by_room_date(1, Date.new(2020,3,1), Date.new(2020,3,10)) expect(@search_result).must_be_kind_of Array From 407eb0ad9a3ebab466917cd3cffcc06b8718b76e Mon Sep 17 00:00:00 2001 From: Jocelyn Wang Date: Thu, 5 Mar 2020 22:28:48 -0800 Subject: [PATCH 36/43] modified main tests for adding reservations --- lib/reservation_manager.rb | 33 ++--- test/reservation_manager_test.rb | 207 ++++++++++++------------------- 2 files changed, 93 insertions(+), 147 deletions(-) diff --git a/lib/reservation_manager.rb b/lib/reservation_manager.rb index f6f0b630a..65405f0fd 100644 --- a/lib/reservation_manager.rb +++ b/lib/reservation_manager.rb @@ -24,15 +24,10 @@ def save_reservation(first_date, second_date, num_of_rooms: 1, customer_id: nil, # change list_room_by_range to return argument rather than string available_rooms = list_room_by_range(first_date,second_date) - - if available_rooms.is_a? Array # Change to check !.empty? - # add in error handling if available rooms < num_of_rooms - # get rid of if check after implmenting the above - chosen_rooms = available_rooms.take(num_of_rooms) - else - raise ArgumentError, "Something's not right here..." - end - + chosen_rooms = available_rooms.take(num_of_rooms) + + raise ArgumentError, "No rooms available" if chosen_rooms.empty? + chosen_rooms.map!{|room| room.id} new_reservation = HotelManager::ReservationBlock.new( @@ -44,22 +39,13 @@ def save_reservation(first_date, second_date, num_of_rooms: 1, customer_id: nil, room_ids: chosen_rooms ) + # add_reservation(new_reservation) add_reservation(new_reservation) end # Add reservation to instance variables def add_reservation(reservation) - potential_rooms = self.list_room_by_range(reservation.start_date, reservation.end_date) - - found_room = reservation.room_ids.map do |room_id| - potential_rooms.include? find_room(room_id) - end.all? (true) - - if found_room - @reservation_blocks << reservation - else - raise ArgumentError, "#{reservation.class} double booked. Not saved to reservation manager." - end + @reservation_blocks << reservation end def find_room(id) @@ -77,6 +63,7 @@ def search_by_room_date(room, first_date, second_date) raise ArgumentError, "Room #{room} does not exist" if @rooms.last.id < room reservation_room_date = [] + @reservation_blocks.each do |reservation_block| found_room = reservation_block.room_ids.include? room if reservation_block.check_reservation_range(first_date,second_date) && found_room @@ -116,7 +103,11 @@ def list_room_by_range(first_date, second_date) # GET RID OF -- return available rooms as [] def reservation_found? tracker, type - tracker.empty? ? "No #{type} available in date range." : tracker + if tracker.empty? + raise ArgumentError, "No #{type} available in date range." + end + + return tracker end # potentially move to own class to be inherited? diff --git a/test/reservation_manager_test.rb b/test/reservation_manager_test.rb index e82aca52b..6695c14b1 100644 --- a/test/reservation_manager_test.rb +++ b/test/reservation_manager_test.rb @@ -61,127 +61,6 @@ @sample_two.add_reservation(@res_block) end - describe "ability to add reservation_blocks to the Reservation Manager" do - it "able to add reservation_blocks to Reservation Manager" do - expect(@sample_two.reservation_blocks.length).must_equal 3 - end - - it "room available after other block end date - block" do - @res_three_data = { - id: 3, - customer_id: 3, - start_date: Date.new(2020,3,24), - end_date: Date.new(2020,3,30), - room_cost: 100, - room_ids: @rooms - } - - @res_three = HotelManager::ReservationBlock.new(@res_three_data) - @sample_two.add_reservation(@res_three) - - expect(@sample_two.reservation_blocks.length).must_equal 4 - end - - it "room available after other individual end date - individual" do - @res_four_data = { - id: 4, - customer_id: 4, - start_date: Date.new(2020,3,24), - end_date: Date.new(2020,3,28), - room_cost: 200, - room_ids: [1] - } - - @res_four = HotelManager::ReservationBlock.new(@res_four_data) - @sample_two.add_reservation(@res_four) - - expect(@sample_two.reservation_blocks.length).must_equal 4 - end - - it "room available after individual end date - block" do - @rooms = [1,2] - @res_six_data = { - id: 3, - customer_id: 3, - start_date: Date.new(2020,3,5), - end_date: Date.new(2020,3,14), - room_cost: 100, - room_ids: @rooms - } - - @res_six = HotelManager::ReservationBlock.new(@res_six_data) - @sample_two.add_reservation(@res_six) - - expect(@sample_two.reservation_blocks.length).must_equal 4 - end - - it "room available after block end date - individual" do - @res_seven_data = { - id: 7, - customer_id: 7, - start_date: Date.new(2020,3,18), - end_date: Date.new(2020,3,25), - room_cost: 200, - room_ids: [7] - } - - @res_seven = HotelManager::ReservationBlock.new(@res_seven_data) - @sample_two.add_reservation(@res_seven) - - expect(@sample_two.reservation_blocks.length).must_equal 4 - end - - it "raise argument error if individual overbooking attempted on individual" do - @res_five_data = { - id: 5, - customer_id: 5, - start_date: Date.new(2020,3,23), - end_date: Date.new(2020,3,28), - room_cost: 200, - room_ids: [1] - } - - @res_five = HotelManager::ReservationBlock.new(@res_five_data) - expect{@sample_two.add_reservation(@res_five)}.must_raise ArgumentError - end - - it "raise argument error if individual overbooking attempted on block" do - @res_five_data = { - id: 5, - customer_id: 5, - start_date: Date.new(2020,3,4), - end_date: Date.new(2020,3,10), - room_cost: 200, - room_ids: [7] - } - - @res_five = HotelManager::ReservationBlock.new(@res_five_data) - - expect{@sample_two.add_reservation(@res_five)}.must_raise ArgumentError - end - - it "raise argument error if block overbooking attempted on block" do - @res_five = HotelManager::ReservationBlock.new(@res_block_data) - expect{@sample_two.add_reservation(@res_five)}.must_raise ArgumentError - end - - it "raise argument error if block overbooking attempted on individual" do - @rooms = [1,2] - @res_six_data = { - id: 3, - customer_id: 3, - start_date: Date.new(2020,3,4), - end_date: Date.new(2020,3,14), - room_cost: 100, - room_ids: @rooms - } - - @res_six = HotelManager::ReservationBlock.new(@res_six_data) - - expect{@sample_two.add_reservation(@res_six)}.must_raise ArgumentError - end - end - describe "test search by room AND date range" do it "able to list reservation by room/date range" do @rooms = [1, 2, 3] @@ -205,8 +84,7 @@ end it "notifies user if no room by room/date range" do - @search_result = @sample_two.search_by_room_date(1, Date.new(2020,4,1), Date.new(2020,4,10)) - expect(@search_result).must_include "No reservations available in date range." + expect{@sample_two.search_by_room_date(1, Date.new(2020,4,1), Date.new(2020,4,10))}.must_raise ArgumentError end it "raise argument error if invalid room or date range" do @@ -225,8 +103,7 @@ end it "raise argument error if no reservation_blocks on that date" do - @search_result = @sample_two.search_by_date(Date.new(2020,4,1)) - expect(@search_result).must_include "No reservations available in date range." + expect{@sample_two.search_by_date(Date.new(2020,4,1))}.must_raise ArgumentError end it "raise argument error if invalid date entered" do @@ -265,7 +142,7 @@ @sample.save_reservation(Date.new(2020,3,1),Date.new(2020,3,5),num_of_rooms:4) end - it "create and save reservation providing only dates" do + it "able to add reservation_blocks to Reservation Manager" do expect(@sample.reservation_blocks.length).must_equal 4 end @@ -308,6 +185,84 @@ end end +describe "Reservation Manager Class - Double Booking" do + before do + @sample = HotelManager::ReservationManager.new() + @sample.save_reservation(Date.new(2020,3,1),Date.new(2020,3,15),num_of_rooms:5) + @sample.save_reservation(Date.new(2020,3,1),Date.new(2020,3,15),num_of_rooms:5) + @sample.save_reservation(Date.new(2020,3,1),Date.new(2020,3,15),num_of_rooms:5) + @sample.save_reservation(Date.new(2020,3,1),Date.new(2020,3,15),num_of_rooms:5) + end + + it "raise error - individual overbooking same range" do + expect{@sample.save_reservation(Date.new(2020,3,1),Date.new(2020,3,15))}.must_raise ArgumentError + end + + it "raise error - overbooking within range" do + expect{@sample.save_reservation(Date.new(2020,3,2),Date.new(2020,3,14),num_of_rooms: 5)}.must_raise ArgumentError + end + + it "raise error - overbooking outside range" do + expect{@sample.save_reservation(Date.new(2020,2,25),Date.new(2020,4,1),num_of_rooms: 5)}.must_raise ArgumentError + end + + it "raise error - overbooking before range" do + expect{@sample.save_reservation(Date.new(2020,2,25),Date.new(2020,3,5),num_of_rooms: 5)}.must_raise ArgumentError + end + + it "raise error - overbooking after range" do + expect{@sample.save_reservation(Date.new(2020,3,8),Date.new(2020,3,20),num_of_rooms: 5)}.must_raise ArgumentError + end + + it "allow booking overlapping last day" do + @sample.save_reservation(Date.new(2020,3,15),Date.new(2020,3,20),num_of_rooms: 5) + expect(@sample.reservation_blocks[4]).must_be_kind_of HotelManager::ReservationBlock + expect(@sample.reservation_blocks.length).must_equal 5 + end +end + +describe "Reservation Manager Class - Allow Booking" do + before do + @sample = HotelManager::ReservationManager.new() + @sample.save_reservation(Date.new(2020,3,1),Date.new(2020,3,15),num_of_rooms:5) + end + + it "allow booking overlapping last day" do + @sample.save_reservation(Date.new(2020,3,15),Date.new(2020,3,20),num_of_rooms: 5) + expect(@sample.reservation_blocks[1]).must_be_kind_of HotelManager::ReservationBlock + expect(@sample.reservation_blocks.length).must_equal 2 + end + + it "allow individual booking on same range" do + @sample.save_reservation(Date.new(2020,3,1),Date.new(2020,3,15)) + expect(@sample.reservation_blocks[1]).must_be_kind_of HotelManager::ReservationBlock + expect(@sample.reservation_blocks.length).must_equal 2 + end + + it "allow booking within range" do + @sample.save_reservation(Date.new(2020,3,2),Date.new(2020,3,14)) + expect(@sample.reservation_blocks[1]).must_be_kind_of HotelManager::ReservationBlock + expect(@sample.reservation_blocks.length).must_equal 2 + end + + it "allow booking outside of range" do + @sample.save_reservation(Date.new(2020,2,25),Date.new(2020,3,18)) + expect(@sample.reservation_blocks[1]).must_be_kind_of HotelManager::ReservationBlock + expect(@sample.reservation_blocks.length).must_equal 2 + end + + it "allow booking before range" do + @sample.save_reservation(Date.new(2020,2,25),Date.new(2020,3,5)) + expect(@sample.reservation_blocks[1]).must_be_kind_of HotelManager::ReservationBlock + expect(@sample.reservation_blocks.length).must_equal 2 + end + + it "allow booking after range" do + @sample.save_reservation(Date.new(2020,3,8),Date.new(2020,3,20)) + expect(@sample.reservation_blocks[1]).must_be_kind_of HotelManager::ReservationBlock + expect(@sample.reservation_blocks.length).must_equal 2 + end +end # List of available rooms will not include the rooms in block reservation # Correctly list out whether a given block has any rooms available? From 5337847c3ad8b70428e800d4a589b259a25d59bc Mon Sep 17 00:00:00 2001 From: Jocelyn Wang Date: Thu, 5 Mar 2020 22:40:13 -0800 Subject: [PATCH 37/43] added in test for if no rooms are available through list --- test/reservation_manager_test.rb | 81 +++++++++----------------------- 1 file changed, 22 insertions(+), 59 deletions(-) diff --git a/test/reservation_manager_test.rb b/test/reservation_manager_test.rb index 6695c14b1..e774f3561 100644 --- a/test/reservation_manager_test.rb +++ b/test/reservation_manager_test.rb @@ -22,61 +22,19 @@ end end -describe "Reservation Manager Class - Manipulation" do +describe "Reservation Manager Class - Search Functions" do before do - @res_one_data = { - id: 1, - customer_id: 1, - start_date: Date.new(2020,3,2), - end_date: Date.new(2020,3,5), - room_cost: 200, - room_ids: [1] - } - @res_two_data = { - id: 2, - customer_id: 2, - start_date: Date.new(2020,3,15), - end_date: Date.new(2020,3,24), - room_cost: 200, - room_ids: [1] - } - @rooms = [7, 8, 9] - @res_block_data = { - id: 3, - customer_id: 3, - start_date: Date.new(2020,3,2), - end_date: Date.new(2020,3,18), - room_cost: 100, - room_ids: @rooms - } - - @res_one = HotelManager::ReservationBlock.new(@res_one_data) - @res_two = HotelManager::ReservationBlock.new(@res_two_data) - - @res_block = HotelManager::ReservationBlock.new(@res_block_data) - @sample_two = HotelManager::ReservationManager.new() - - @sample_two.add_reservation(@res_one) - @sample_two.add_reservation(@res_two) - @sample_two.add_reservation(@res_block) + @sample = HotelManager::ReservationManager.new() + @sample.save_reservation(Date.new(2020,3,2),Date.new(2020,3,5)) + @sample.save_reservation(Date.new(2020,3,15),Date.new(2020,3,24)) + @sample.save_reservation(Date.new(2020,3,2),Date.new(2020,3,18),num_of_rooms:3) end describe "test search by room AND date range" do it "able to list reservation by room/date range" do - @rooms = [1, 2, 3] - @res_block_data = { - id: 4, - customer_id: 4, - start_date: Date.new(2020,3,6), - end_date: Date.new(2020,3,12), - room_cost: 100, - room_ids: @rooms - } - - @res_block = HotelManager::ReservationBlock.new(@res_block_data) - @sample_two.add_reservation(@res_block) - - @search_result = @sample_two.search_by_room_date(1, Date.new(2020,3,1), Date.new(2020,3,10)) + @sample.save_reservation(Date.new(2020,3,6),Date.new(2020,3,12),num_of_rooms:3,room_cost:100) + + @search_result = @sample.search_by_room_date(1, Date.new(2020,3,1), Date.new(2020,3,10)) expect(@search_result).must_be_kind_of Array expect(@search_result[1]).must_be_kind_of HotelManager::ReservationBlock expect(@search_result[0].id).must_equal 1 @@ -84,18 +42,18 @@ end it "notifies user if no room by room/date range" do - expect{@sample_two.search_by_room_date(1, Date.new(2020,4,1), Date.new(2020,4,10))}.must_raise ArgumentError + expect{@sample.search_by_room_date(1, Date.new(2020,4,1), Date.new(2020,4,10))}.must_raise ArgumentError end it "raise argument error if invalid room or date range" do - expect{@sample_two.search_by_room_date(30, Date.new(2020,3,1), Date.new(2020,3,10))}.must_raise ArgumentError - expect{@sample_two.search_by_room_date(1, "Date.new(2020,3,1)", Date.new(2020,3,10))}.must_raise ArgumentError + expect{@sample.search_by_room_date(30, Date.new(2020,3,1), Date.new(2020,3,10))}.must_raise ArgumentError + expect{@sample.search_by_room_date(1, "Date.new(2020,3,1)", Date.new(2020,3,10))}.must_raise ArgumentError end end describe "test search by date" do it "able to list reservation by specific date" do - @search_result = @sample_two.search_by_date(Date.new(2020,3,17)) + @search_result = @sample.search_by_date(Date.new(2020,3,17)) expect(@search_result).must_be_kind_of Array expect(@search_result[0]).must_be_kind_of HotelManager::ReservationBlock expect(@search_result[0].id).must_equal 2 @@ -103,31 +61,36 @@ end it "raise argument error if no reservation_blocks on that date" do - expect{@sample_two.search_by_date(Date.new(2020,4,1))}.must_raise ArgumentError + expect{@sample.search_by_date(Date.new(2020,4,1))}.must_raise ArgumentError end it "raise argument error if invalid date entered" do - expect{@sample_two.search_by_date("Date.new(2020,3,1)")}.must_raise ArgumentError + expect{@sample.search_by_date("Date.new(2020,3,1)")}.must_raise ArgumentError end end # see list of rooms available for a given date range describe "list rooms available by date range" do it "able to list out all available rooms by date range" do - @search_result = @sample_two.list_room_by_range(Date.new(2020,4,1),Date.new(2020,4,7)) + @search_result = @sample.list_room_by_range(Date.new(2020,4,1),Date.new(2020,4,7)) expect(@search_result).must_be_kind_of Array expect(@search_result.length).must_equal 20 end it "able to list out all available rooms by date range" do - @search_result = @sample_two.list_room_by_range(Date.new(2020,3,1),Date.new(2020,3,5)) + @search_result = @sample.list_room_by_range(Date.new(2020,3,1),Date.new(2020,3,5)) expect(@search_result).must_be_kind_of Array expect(@search_result[0]).must_be_kind_of HotelManager::Room expect(@search_result.length).must_equal 16 end it "notify user if no rooms are available" do - #complete later, need to modify hotel room # to by dynamic (user input) + @sample.save_reservation(Date.new(2020,3,2),Date.new(2020,3,18),num_of_rooms:5) + @sample.save_reservation(Date.new(2020,3,2),Date.new(2020,3,18),num_of_rooms:5) + @sample.save_reservation(Date.new(2020,3,2),Date.new(2020,3,18),num_of_rooms:5) + @sample.save_reservation(Date.new(2020,3,2),Date.new(2020,3,18)) + + expect{@sample.list_room_by_range(Date.new(2020,3,1),Date.new(2020,3,5))}.must_raise ArgumentError end end end From 805f64468493c6b854fb9babf6495a1f078ffc8f Mon Sep 17 00:00:00 2001 From: Jocelyn Wang Date: Thu, 5 Mar 2020 22:41:34 -0800 Subject: [PATCH 38/43] moved order of test sections around --- test/reservation_manager_test.rb | 156 ++++++++++++++++--------------- 1 file changed, 79 insertions(+), 77 deletions(-) diff --git a/test/reservation_manager_test.rb b/test/reservation_manager_test.rb index e774f3561..d51a548d9 100644 --- a/test/reservation_manager_test.rb +++ b/test/reservation_manager_test.rb @@ -22,6 +22,85 @@ end end +describe "Reservation Manager Class - Allow Booking" do + before do + @sample = HotelManager::ReservationManager.new() + @sample.save_reservation(Date.new(2020,3,1),Date.new(2020,3,15),num_of_rooms:5) + end + + it "allow booking overlapping last day" do + @sample.save_reservation(Date.new(2020,3,15),Date.new(2020,3,20),num_of_rooms: 5) + expect(@sample.reservation_blocks[1]).must_be_kind_of HotelManager::ReservationBlock + expect(@sample.reservation_blocks.length).must_equal 2 + end + + it "allow individual booking on same range" do + @sample.save_reservation(Date.new(2020,3,1),Date.new(2020,3,15)) + expect(@sample.reservation_blocks[1]).must_be_kind_of HotelManager::ReservationBlock + expect(@sample.reservation_blocks.length).must_equal 2 + end + + it "allow booking within range" do + @sample.save_reservation(Date.new(2020,3,2),Date.new(2020,3,14)) + expect(@sample.reservation_blocks[1]).must_be_kind_of HotelManager::ReservationBlock + expect(@sample.reservation_blocks.length).must_equal 2 + end + + it "allow booking outside of range" do + @sample.save_reservation(Date.new(2020,2,25),Date.new(2020,3,18)) + expect(@sample.reservation_blocks[1]).must_be_kind_of HotelManager::ReservationBlock + expect(@sample.reservation_blocks.length).must_equal 2 + end + + it "allow booking before range" do + @sample.save_reservation(Date.new(2020,2,25),Date.new(2020,3,5)) + expect(@sample.reservation_blocks[1]).must_be_kind_of HotelManager::ReservationBlock + expect(@sample.reservation_blocks.length).must_equal 2 + end + + it "allow booking after range" do + @sample.save_reservation(Date.new(2020,3,8),Date.new(2020,3,20)) + expect(@sample.reservation_blocks[1]).must_be_kind_of HotelManager::ReservationBlock + expect(@sample.reservation_blocks.length).must_equal 2 + end +end + +describe "Reservation Manager Class - Double Booking" do + before do + @sample = HotelManager::ReservationManager.new() + @sample.save_reservation(Date.new(2020,3,1),Date.new(2020,3,15),num_of_rooms:5) + @sample.save_reservation(Date.new(2020,3,1),Date.new(2020,3,15),num_of_rooms:5) + @sample.save_reservation(Date.new(2020,3,1),Date.new(2020,3,15),num_of_rooms:5) + @sample.save_reservation(Date.new(2020,3,1),Date.new(2020,3,15),num_of_rooms:5) + end + + it "raise error - individual overbooking same range" do + expect{@sample.save_reservation(Date.new(2020,3,1),Date.new(2020,3,15))}.must_raise ArgumentError + end + + it "raise error - overbooking within range" do + expect{@sample.save_reservation(Date.new(2020,3,2),Date.new(2020,3,14),num_of_rooms: 5)}.must_raise ArgumentError + end + + it "raise error - overbooking outside range" do + expect{@sample.save_reservation(Date.new(2020,2,25),Date.new(2020,4,1),num_of_rooms: 5)}.must_raise ArgumentError + end + + it "raise error - overbooking before range" do + expect{@sample.save_reservation(Date.new(2020,2,25),Date.new(2020,3,5),num_of_rooms: 5)}.must_raise ArgumentError + end + + it "raise error - overbooking after range" do + expect{@sample.save_reservation(Date.new(2020,3,8),Date.new(2020,3,20),num_of_rooms: 5)}.must_raise ArgumentError + end + + it "allow booking overlapping last day" do + @sample.save_reservation(Date.new(2020,3,15),Date.new(2020,3,20),num_of_rooms: 5) + expect(@sample.reservation_blocks[4]).must_be_kind_of HotelManager::ReservationBlock + expect(@sample.reservation_blocks.length).must_equal 5 + end +end + describe "Reservation Manager Class - Search Functions" do before do @sample = HotelManager::ReservationManager.new() @@ -148,84 +227,7 @@ end end -describe "Reservation Manager Class - Double Booking" do - before do - @sample = HotelManager::ReservationManager.new() - @sample.save_reservation(Date.new(2020,3,1),Date.new(2020,3,15),num_of_rooms:5) - @sample.save_reservation(Date.new(2020,3,1),Date.new(2020,3,15),num_of_rooms:5) - @sample.save_reservation(Date.new(2020,3,1),Date.new(2020,3,15),num_of_rooms:5) - @sample.save_reservation(Date.new(2020,3,1),Date.new(2020,3,15),num_of_rooms:5) - end - - it "raise error - individual overbooking same range" do - expect{@sample.save_reservation(Date.new(2020,3,1),Date.new(2020,3,15))}.must_raise ArgumentError - end - - it "raise error - overbooking within range" do - expect{@sample.save_reservation(Date.new(2020,3,2),Date.new(2020,3,14),num_of_rooms: 5)}.must_raise ArgumentError - end - - it "raise error - overbooking outside range" do - expect{@sample.save_reservation(Date.new(2020,2,25),Date.new(2020,4,1),num_of_rooms: 5)}.must_raise ArgumentError - end - - it "raise error - overbooking before range" do - expect{@sample.save_reservation(Date.new(2020,2,25),Date.new(2020,3,5),num_of_rooms: 5)}.must_raise ArgumentError - end - - it "raise error - overbooking after range" do - expect{@sample.save_reservation(Date.new(2020,3,8),Date.new(2020,3,20),num_of_rooms: 5)}.must_raise ArgumentError - end - - it "allow booking overlapping last day" do - @sample.save_reservation(Date.new(2020,3,15),Date.new(2020,3,20),num_of_rooms: 5) - expect(@sample.reservation_blocks[4]).must_be_kind_of HotelManager::ReservationBlock - expect(@sample.reservation_blocks.length).must_equal 5 - end -end - -describe "Reservation Manager Class - Allow Booking" do - before do - @sample = HotelManager::ReservationManager.new() - @sample.save_reservation(Date.new(2020,3,1),Date.new(2020,3,15),num_of_rooms:5) - end - - it "allow booking overlapping last day" do - @sample.save_reservation(Date.new(2020,3,15),Date.new(2020,3,20),num_of_rooms: 5) - expect(@sample.reservation_blocks[1]).must_be_kind_of HotelManager::ReservationBlock - expect(@sample.reservation_blocks.length).must_equal 2 - end - - it "allow individual booking on same range" do - @sample.save_reservation(Date.new(2020,3,1),Date.new(2020,3,15)) - expect(@sample.reservation_blocks[1]).must_be_kind_of HotelManager::ReservationBlock - expect(@sample.reservation_blocks.length).must_equal 2 - end - it "allow booking within range" do - @sample.save_reservation(Date.new(2020,3,2),Date.new(2020,3,14)) - expect(@sample.reservation_blocks[1]).must_be_kind_of HotelManager::ReservationBlock - expect(@sample.reservation_blocks.length).must_equal 2 - end - - it "allow booking outside of range" do - @sample.save_reservation(Date.new(2020,2,25),Date.new(2020,3,18)) - expect(@sample.reservation_blocks[1]).must_be_kind_of HotelManager::ReservationBlock - expect(@sample.reservation_blocks.length).must_equal 2 - end - - it "allow booking before range" do - @sample.save_reservation(Date.new(2020,2,25),Date.new(2020,3,5)) - expect(@sample.reservation_blocks[1]).must_be_kind_of HotelManager::ReservationBlock - expect(@sample.reservation_blocks.length).must_equal 2 - end - - it "allow booking after range" do - @sample.save_reservation(Date.new(2020,3,8),Date.new(2020,3,20)) - expect(@sample.reservation_blocks[1]).must_be_kind_of HotelManager::ReservationBlock - expect(@sample.reservation_blocks.length).must_equal 2 - end -end # List of available rooms will not include the rooms in block reservation # Correctly list out whether a given block has any rooms available? From 1f54f6a8133ed6cb8f798c3b8b43236e1d459635 Mon Sep 17 00:00:00 2001 From: Jocelyn Wang Date: Thu, 5 Mar 2020 22:44:34 -0800 Subject: [PATCH 39/43] changed name of reservation block to reservation --- lib/{reservation_block.rb => reservation.rb} | 2 +- lib/reservation_manager.rb | 4 ++-- test/reservation_manager_test.rb | 18 +++++++++--------- ...ation_block_test.rb => reservation_test.rb} | 14 +++++++------- test/test_helper.rb | 2 +- 5 files changed, 20 insertions(+), 20 deletions(-) rename lib/{reservation_block.rb => reservation.rb} (98%) rename test/{reservation_block_test.rb => reservation_test.rb} (79%) diff --git a/lib/reservation_block.rb b/lib/reservation.rb similarity index 98% rename from lib/reservation_block.rb rename to lib/reservation.rb index f0cf2ba97..de705d70b 100644 --- a/lib/reservation_block.rb +++ b/lib/reservation.rb @@ -4,7 +4,7 @@ require_relative 'room' module HotelManager - class ReservationBlock + class Reservation attr_reader :id, :customer_id, :start_date, :end_date, :room_cost, :rooms, :room_ids diff --git a/lib/reservation_manager.rb b/lib/reservation_manager.rb index 65405f0fd..a0d269f96 100644 --- a/lib/reservation_manager.rb +++ b/lib/reservation_manager.rb @@ -1,7 +1,7 @@ require 'pry' require 'date' -require_relative 'reservation_block' +require_relative 'reservation' require_relative 'room' module HotelManager @@ -30,7 +30,7 @@ def save_reservation(first_date, second_date, num_of_rooms: 1, customer_id: nil, chosen_rooms.map!{|room| room.id} - new_reservation = HotelManager::ReservationBlock.new( + new_reservation = HotelManager::Reservation.new( id: @reservation_blocks.length + 1, customer_id: customer_id, start_date: first_date, diff --git a/test/reservation_manager_test.rb b/test/reservation_manager_test.rb index d51a548d9..2a8d91e13 100644 --- a/test/reservation_manager_test.rb +++ b/test/reservation_manager_test.rb @@ -30,37 +30,37 @@ it "allow booking overlapping last day" do @sample.save_reservation(Date.new(2020,3,15),Date.new(2020,3,20),num_of_rooms: 5) - expect(@sample.reservation_blocks[1]).must_be_kind_of HotelManager::ReservationBlock + expect(@sample.reservation_blocks[1]).must_be_kind_of HotelManager::Reservation expect(@sample.reservation_blocks.length).must_equal 2 end it "allow individual booking on same range" do @sample.save_reservation(Date.new(2020,3,1),Date.new(2020,3,15)) - expect(@sample.reservation_blocks[1]).must_be_kind_of HotelManager::ReservationBlock + expect(@sample.reservation_blocks[1]).must_be_kind_of HotelManager::Reservation expect(@sample.reservation_blocks.length).must_equal 2 end it "allow booking within range" do @sample.save_reservation(Date.new(2020,3,2),Date.new(2020,3,14)) - expect(@sample.reservation_blocks[1]).must_be_kind_of HotelManager::ReservationBlock + expect(@sample.reservation_blocks[1]).must_be_kind_of HotelManager::Reservation expect(@sample.reservation_blocks.length).must_equal 2 end it "allow booking outside of range" do @sample.save_reservation(Date.new(2020,2,25),Date.new(2020,3,18)) - expect(@sample.reservation_blocks[1]).must_be_kind_of HotelManager::ReservationBlock + expect(@sample.reservation_blocks[1]).must_be_kind_of HotelManager::Reservation expect(@sample.reservation_blocks.length).must_equal 2 end it "allow booking before range" do @sample.save_reservation(Date.new(2020,2,25),Date.new(2020,3,5)) - expect(@sample.reservation_blocks[1]).must_be_kind_of HotelManager::ReservationBlock + expect(@sample.reservation_blocks[1]).must_be_kind_of HotelManager::Reservation expect(@sample.reservation_blocks.length).must_equal 2 end it "allow booking after range" do @sample.save_reservation(Date.new(2020,3,8),Date.new(2020,3,20)) - expect(@sample.reservation_blocks[1]).must_be_kind_of HotelManager::ReservationBlock + expect(@sample.reservation_blocks[1]).must_be_kind_of HotelManager::Reservation expect(@sample.reservation_blocks.length).must_equal 2 end end @@ -96,7 +96,7 @@ it "allow booking overlapping last day" do @sample.save_reservation(Date.new(2020,3,15),Date.new(2020,3,20),num_of_rooms: 5) - expect(@sample.reservation_blocks[4]).must_be_kind_of HotelManager::ReservationBlock + expect(@sample.reservation_blocks[4]).must_be_kind_of HotelManager::Reservation expect(@sample.reservation_blocks.length).must_equal 5 end end @@ -115,7 +115,7 @@ @search_result = @sample.search_by_room_date(1, Date.new(2020,3,1), Date.new(2020,3,10)) expect(@search_result).must_be_kind_of Array - expect(@search_result[1]).must_be_kind_of HotelManager::ReservationBlock + expect(@search_result[1]).must_be_kind_of HotelManager::Reservation expect(@search_result[0].id).must_equal 1 expect(@search_result[1].id).must_equal 4 end @@ -134,7 +134,7 @@ it "able to list reservation by specific date" do @search_result = @sample.search_by_date(Date.new(2020,3,17)) expect(@search_result).must_be_kind_of Array - expect(@search_result[0]).must_be_kind_of HotelManager::ReservationBlock + expect(@search_result[0]).must_be_kind_of HotelManager::Reservation expect(@search_result[0].id).must_equal 2 expect(@search_result[1].id).must_equal 3 end diff --git a/test/reservation_block_test.rb b/test/reservation_test.rb similarity index 79% rename from test/reservation_block_test.rb rename to test/reservation_test.rb index 3c00f45fa..cf9c9cde5 100644 --- a/test/reservation_block_test.rb +++ b/test/reservation_test.rb @@ -18,26 +18,26 @@ rooms: @rooms } - @reservation = HotelManager::ReservationBlock.new(@reservation_data) + @reservation = HotelManager::Reservation.new(@reservation_data) end describe "Reservation Block Class - Attributes" do it "is an instance of Reservation Block" do - expect(@reservation).must_be_kind_of HotelManager::ReservationBlock + expect(@reservation).must_be_kind_of HotelManager::Reservation expect(@reservation.rooms).must_be_kind_of Array end it "raises argument if block only has one room" do @rooms = nil @reservation_data[:rooms] = @rooms - expect{HotelManager::ReservationBlock.new(@reservation_data)}.must_raise ArgumentError + expect{HotelManager::Reservation.new(@reservation_data)}.must_raise ArgumentError end it "raises argument if block has more than 5 rooms" do @rooms += [HotelManager::Room.new(id: 6)] @reservation_data[:rooms] = @rooms - expect{HotelManager::ReservationBlock.new(@reservation_data)}.must_raise ArgumentError + expect{HotelManager::Reservation.new(@reservation_data)}.must_raise ArgumentError end it "calculates total cost correctly" do @@ -55,19 +55,19 @@ it "raises argument error if start is not date class" do @reservation_data[:start_date] = "Janurary 5th, 2020" - expect{HotelManager::ReservationBlock.new(@reservation_data)}.must_raise ArgumentError + expect{HotelManager::Reservation.new(@reservation_data)}.must_raise ArgumentError end it "raises argument error if end before start date" do @reservation_data[:start_date] = Date.new(2020,3,5) @reservation_data[:end_date] = Date.new(2020,3,2) - expect{HotelManager::ReservationBlock.new(@reservation_data)}.must_raise ArgumentError + expect{HotelManager::Reservation.new(@reservation_data)}.must_raise ArgumentError end it "raises argument error if start and end date are the same" do @reservation_data[:start_date] = Date.new(2020,3,5) @reservation_data[:end_date] = Date.new(2020,3,5) - expect{HotelManager::ReservationBlock.new(@reservation_data)}.must_raise ArgumentError + expect{HotelManager::Reservation.new(@reservation_data)}.must_raise ArgumentError end end end diff --git a/test/test_helper.rb b/test/test_helper.rb index e5d15cc5c..c4c9aa989 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -16,5 +16,5 @@ # require_relative your lib files here! require_relative '../lib/room.rb' require_relative '../lib/reservation_manager.rb' -require_relative '../lib/reservation_block.rb' +require_relative '../lib/reservation.rb' require_relative '../lib/csv_record.rb' \ No newline at end of file From d8cf487d7231ac887bfd8716413c0374240242a8 Mon Sep 17 00:00:00 2001 From: Jocelyn Wang Date: Thu, 5 Mar 2020 22:56:51 -0800 Subject: [PATCH 40/43] removed data validation code from reservation manager --- lib/reservation_manager.rb | 25 ++++--------------------- test/reservation_test.rb | 3 --- 2 files changed, 4 insertions(+), 24 deletions(-) diff --git a/lib/reservation_manager.rb b/lib/reservation_manager.rb index a0d269f96..09ab83fd6 100644 --- a/lib/reservation_manager.rb +++ b/lib/reservation_manager.rb @@ -39,13 +39,7 @@ def save_reservation(first_date, second_date, num_of_rooms: 1, customer_id: nil, room_ids: chosen_rooms ) - # add_reservation(new_reservation) - add_reservation(new_reservation) - end - - # Add reservation to instance variables - def add_reservation(reservation) - @reservation_blocks << reservation + @reservation_blocks << new_reservation end def find_room(id) @@ -59,7 +53,6 @@ def rooms_list # List out all reservations by room and date range def search_by_room_date(room, first_date, second_date) - search_date_validation(first_date,second_date) raise ArgumentError, "Room #{room} does not exist" if @rooms.last.id < room reservation_room_date = [] @@ -86,7 +79,6 @@ def search_by_date(date) end def list_room_by_range(first_date, second_date) - search_date_validation(first_date,second_date) available_rooms = @rooms.dup @@ -101,20 +93,11 @@ def list_room_by_range(first_date, second_date) return reservation_found?(available_rooms, "rooms") end - # GET RID OF -- return available rooms as [] + # Raise argument error if no items found def reservation_found? tracker, type - if tracker.empty? - raise ArgumentError, "No #{type} available in date range." - end - - return tracker - end + raise ArgumentError, "No #{type} available in date range." if tracker.empty? - # potentially move to own class to be inherited? - def search_date_validation(first_date, second_date) - if first_date.class != Date || second_date.class != Date - raise ArgumentError, "One date (#{first_date} or #{second_date}) is not valid" - end + return tracker end end diff --git a/test/reservation_test.rb b/test/reservation_test.rb index cf9c9cde5..a7df35b45 100644 --- a/test/reservation_test.rb +++ b/test/reservation_test.rb @@ -73,9 +73,6 @@ end # Edge case - date entered is > 31 days per month or leap year -# raise argument error if start and end date are the same - -# Able to reserve a hotel block (group of rooms for a specific group of customers for a set period of time) From c2e1e7f39a2f4d06a94557d735080b19e2b30af8 Mon Sep 17 00:00:00 2001 From: Jocelyn Wang Date: Thu, 5 Mar 2020 23:01:28 -0800 Subject: [PATCH 41/43] cleaned out some extra comments --- lib/reservation_manager.rb | 1 - test/reservation_manager_test.rb | 13 +------------ test/reservation_test.rb | 1 + 3 files changed, 2 insertions(+), 13 deletions(-) diff --git a/lib/reservation_manager.rb b/lib/reservation_manager.rb index 09ab83fd6..c32135f29 100644 --- a/lib/reservation_manager.rb +++ b/lib/reservation_manager.rb @@ -79,7 +79,6 @@ def search_by_date(date) end def list_room_by_range(first_date, second_date) - available_rooms = @rooms.dup @reservation_blocks.each do |reservation_block| diff --git a/test/reservation_manager_test.rb b/test/reservation_manager_test.rb index 2a8d91e13..cabd5d336 100644 --- a/test/reservation_manager_test.rb +++ b/test/reservation_manager_test.rb @@ -225,15 +225,4 @@ expect{@sample.save_reservation(Date.new(2020,3,1),Date.new(2020,3,5),num_of_rooms:2)}.must_raise ArgumentError end -end - - - -# List of available rooms will not include the rooms in block reservation -# Correctly list out whether a given block has any rooms available? -# Able to reserve a specific room from a hotel block, but only for the full duration of the block (individual reservation_blocks still work?) -# List of reservation by date will list out both individual and block reservation_blocks -# Check to make sure that when a block is reserved, the indivudal room reservation dates will also be there -# Confirm that all availabilty checking logic from Wave 2 also respects room blocks - -# 2 or more days overlapping (of all dates in the range) \ No newline at end of file +end \ No newline at end of file diff --git a/test/reservation_test.rb b/test/reservation_test.rb index a7df35b45..e46d36b8b 100644 --- a/test/reservation_test.rb +++ b/test/reservation_test.rb @@ -73,6 +73,7 @@ end # Edge case - date entered is > 31 days per month or leap year +# Add in test for if a a block is reserved, where only part of the rooms are available From 0fd096bf792e779c999259ae5349c11163f77ff0 Mon Sep 17 00:00:00 2001 From: Jocelyn Wang Date: Fri, 6 Mar 2020 09:31:26 -0800 Subject: [PATCH 42/43] fixed build_path test --- lib/csv_record.rb | 11 +++++------ test/csv_record_test.rb | 9 +++------ 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/lib/csv_record.rb b/lib/csv_record.rb index bd192267d..f11ffb2b4 100644 --- a/lib/csv_record.rb +++ b/lib/csv_record.rb @@ -13,8 +13,9 @@ def initialize(id) # Takes either full_path or directory and optional file_name # Default file name matches class name def self.load_all(full_path: nil, directory: nil, file_name: nil) - full_path ||= build_path(directory, file_name) + full_path ||= build_path(directory, file_name) + return CSV.read( full_path, headers: true, @@ -29,18 +30,16 @@ def self.validate_id(id) end end - private + # private def self.from_csv(record) raise NotImplementedError, 'Implement me in a child class!' end def self.build_path(directory, file_name) - unless directory - raise ArgumentError, "Either full_path or directory is required" - end + raise ArgumentError, "Either full_path or directory is required" if directory.nil? - unless file_name + if file_name.nil? class_name = self.to_s.split('::').last file_name = "#{class_name.downcase}s.csv" end diff --git a/test/csv_record_test.rb b/test/csv_record_test.rb index cd95a9de4..1f6ad440b 100644 --- a/test/csv_record_test.rb +++ b/test/csv_record_test.rb @@ -13,11 +13,9 @@ expect {HotelManager::CsvRecord.new("One")}.must_raise ArgumentError end - xit 'validates that id accepts natural numbers' do + it 'validates that id accepts natural numbers' do [1, 10, 9999].each {|id| HotelManager::CsvRecord.validate_id(id)} end - - end describe 'CSV Record Class - Load All' do @@ -25,9 +23,8 @@ expect {HotelManager::CsvRecord.load_all}.must_raise ArgumentError end - xit "raises an error if invoked directly (without subclassing)" do - record = HotelManager::CsvRecord.new(1) - record.build_path(TEST_DATA_DIR,"") + it "raises an error if missing directory or file path" do + expect {HotelManager::CsvRecord.build_path(nil,"")}.must_raise ArgumentError end it "raises an error if invoked directly (without subclassing)" do From 17c3016262ba739e90d392d65ec7ce9bb8ee4ba7 Mon Sep 17 00:00:00 2001 From: Jocelyn Wang Date: Fri, 6 Mar 2020 10:35:18 -0800 Subject: [PATCH 43/43] finished csv record testing by creating a test class --- lib/csv_record.rb | 6 +- test/csv_record_test.rb | 122 +++++++----------- test/test_data/reservations.csv | 22 ++-- ...reservation_blocks.csv => testrecords.csv} | 2 +- 4 files changed, 62 insertions(+), 90 deletions(-) rename test/test_data/{reservation_blocks.csv => testrecords.csv} (90%) diff --git a/lib/csv_record.rb b/lib/csv_record.rb index f11ffb2b4..6a5335a43 100644 --- a/lib/csv_record.rb +++ b/lib/csv_record.rb @@ -1,3 +1,7 @@ +################################ +## ADAPTED FROM OO-RIDE-SHARE ## +################################ + require 'csv' require 'pry' @@ -29,8 +33,6 @@ def self.validate_id(id) raise ArgumentError, 'ID cannot be blank or less than one.' end end - - # private def self.from_csv(record) raise NotImplementedError, 'Implement me in a child class!' diff --git a/test/csv_record_test.rb b/test/csv_record_test.rb index 1f6ad440b..006bd5ef9 100644 --- a/test/csv_record_test.rb +++ b/test/csv_record_test.rb @@ -1,3 +1,7 @@ +################################ +## ADAPTED FROM OO-RIDE-SHARE ## +################################ + require_relative 'test_helper' TEST_DATA_DIR = 'test/test_data' @@ -16,6 +20,14 @@ it 'validates that id accepts natural numbers' do [1, 10, 9999].each {|id| HotelManager::CsvRecord.validate_id(id)} end + + it 'raises for negative numbers and 0' do + [0, -1, -10, -9999].each {|id| expect { HotelManager::CsvRecord.validate_id(id) }.must_raise ArgumentError} + end + + it 'raises for nil' do + expect {HotelManager::CsvRecord.validate_id(nil)}.must_raise ArgumentError + end end describe 'CSV Record Class - Load All' do @@ -28,100 +40,58 @@ end it "raises an error if invoked directly (without subclassing)" do - full_path = "#{TEST_DATA_DIR}/reservations.csv" + full_path = "#{TEST_DATA_DIR}/testrecords.csv" expect {HotelManager::CsvRecord.load_all(full_path: full_path)}.must_raise NotImplementedError end end +describe 'CSV Record Class - Test Class' do + class TestRecord < HotelManager::CsvRecord + attr_reader :call_count -describe "blah" do - - - xdescribe 'validate_id' do - it 'accepts natural numbers' do - # Should not raise - [1, 10, 9999].each do |id| - HotelManager::CsvRecord.validate_id(id) - end + def initialize(id:, customer_id:, start_date:, end_date: , num_of_rooms:) + super(id) end - it 'raises for negative numbers and 0' do - [0, -1, -10, -9999].each do |id| - expect { - HotelManager::CsvRecord.validate_id(id) - }.must_raise ArgumentError - end + def self.load_all(*args, **kwargs) + @call_count = 0 + super end - it 'raises for nil' do - expect { - HotelManager::CsvRecord.validate_id(nil) - }.must_raise ArgumentError + def self.from_csv(record) + new(**record) + @call_count += 1 end - end - xdescribe 'extension' do - # It's a class that's designed to be extended. - # How do you test that? Extend it! - class TestRecord < HotelManager::CsvRecord - attr_reader :name - def initialize(id:, name:) - super(id) - @name = name - end - - def self.load_all(*args, **kwargs) - @call_count = 0 - super - end - - def self.from_csv(record) - new(**record) - @call_count ||= 0 - @call_count += 1 - end - - class << self - attr_reader :call_count - end + def self.call_count + @call_count end + end - xdescribe 'load_all' do - let(:record_count) { - CSV.read("#{TEST_DATA_DIR}/testrecords.csv", headers: true).length - } - - it 'finds data given just a directory' do - records = TestRecord.load_all(directory: TEST_DATA_DIR) + describe 'load_all' do + file_name = 'testrecords.csv' + record_count = CSV.read("#{TEST_DATA_DIR}/#{file_name}", headers: true).length - expect(records.length).must_equal record_count - end + it 'finds data given just a directory' do + records = TestRecord.load_all(directory: TEST_DATA_DIR) + expect(records.length).must_equal record_count + end - it 'finds data given a directory and filename' do - file_name = 'custom_filename_test.csv' - records = TestRecord.load_all(directory: TEST_DATA_DIR, file_name: file_name) + it 'finds data given a directory and filename' do + records = TestRecord.load_all(directory: TEST_DATA_DIR, file_name: file_name) - expect(records.length).must_equal record_count - end + expect(records.length).must_equal record_count + end - it 'finds data given a full path' do - path = "#{TEST_DATA_DIR}/custom_filename_test.csv" - records = TestRecord.load_all(full_path: path) + it 'finds data given a full path' do + records = TestRecord.load_all(full_path: "#{TEST_DATA_DIR}/#{file_name}") - expect(records.length).must_equal record_count - end + expect(records.length).must_equal record_count + end - it 'calls `from_csv` for each record in the file' do - TestRecord.load_all(directory: TEST_DATA_DIR) - expect(TestRecord.call_count).must_equal record_count - end + it 'calls `from_csv` for each record in the file' do + TestRecord.load_all(directory: TEST_DATA_DIR) + expect(TestRecord.call_count).must_equal record_count end end - - - -end - -# only accept data entries with ids -# loads in information from files -# argument error if no full_path or directory is provided +end \ No newline at end of file diff --git a/test/test_data/reservations.csv b/test/test_data/reservations.csv index bbc7016be..27ad6f329 100644 --- a/test/test_data/reservations.csv +++ b/test/test_data/reservations.csv @@ -1,11 +1,11 @@ -id,customer_id,start_date,end_date -1,1,2020-05-01 15:00:00 -0800,2020-05-10 11:00:00 -0800 -2,2,2020-05-01 15:00:00 -0800,2020-05-04 11:00:00 -0800 -3,3,2020-05-02 15:00:00 -0800,2020-05-15 11:00:00 -0800 -4,4,2020-05-15 15:00:00 -0800,2020-05-30 11:00:00 -0800 -5,2,2020-05-28 15:00:00 -0800,2020-05-30 11:00:00 -0800 -6,3,2020-05-25 15:00:00 -0800,2020-05-29 11:00:00 -0800 -7,4,2020-05-28 15:00:00 -0800,2020-05-29 11:00:00 -0800 -8,6,2020-05-25 15:00:00 -0800,2020-05-27 11:00:00 -0800 -9,7,2020-05-01 15:00:00 -0800,2020-05-16 11:00:00 -0800 -10,8,2020-05-28 15:00:00 -0800,2020-06-01 11:00:00 -0800 +id,customer_id,start_date,end_date,num_of_rooms +1,1,2020-05-01 15:00:00 -0800,2020-05-10 11:00:00 -0800,1 +2,2,2020-05-01 15:00:00 -0800,2020-05-04 11:00:00 -0800,2 +3,3,2020-05-02 15:00:00 -0800,2020-05-15 11:00:00 -0800,3 +4,4,2020-05-15 15:00:00 -0800,2020-05-30 11:00:00 -0800,4 +5,2,2020-05-28 15:00:00 -0800,2020-05-30 11:00:00 -0800,5 +6,3,2020-05-25 15:00:00 -0800,2020-05-29 11:00:00 -0800,5 +7,4,2020-05-28 15:00:00 -0800,2020-05-29 11:00:00 -0800,4 +8,6,2020-05-25 15:00:00 -0800,2020-05-27 11:00:00 -0800,3 +9,7,2020-05-01 15:00:00 -0800,2020-05-16 11:00:00 -0800,2 +10,8,2020-05-28 15:00:00 -0800,2020-06-01 11:00:00 -0800,1 diff --git a/test/test_data/reservation_blocks.csv b/test/test_data/testrecords.csv similarity index 90% rename from test/test_data/reservation_blocks.csv rename to test/test_data/testrecords.csv index 4edffe5a8..480eca51d 100644 --- a/test/test_data/reservation_blocks.csv +++ b/test/test_data/testrecords.csv @@ -1,5 +1,5 @@ id,customer_id,start_date,end_date,num_of_rooms -1,1,2020-05-01 15:00:00 -0800,2020-07-10 11:00:00 -0800,2 +1,1,2020-05-01 15:00:00 -0800,2020-07-10 11:00:00 -0800,2 2,2,2020-05-08 15:00:00 -0800,2020-05-12 11:00:00 -0800,3 3,3,2020-05-05 15:00:00 -0800,2020-05-15 11:00:00 -0800,4 4,4,2020-05-14 15:00:00 -0800,2020-05-28 11:00:00 -0800,5