From 97643690d12a9191a35da7abebc38fc2d743b870 Mon Sep 17 00:00:00 2001 From: Karis Kim Date: Tue, 4 Sep 2018 11:30:27 -0700 Subject: [PATCH 01/70] created test file for initial set up to test guard --- lib/.keep | 1 + lib/test.rb | 12 ++++++++++++ spec/spec_helper.rb | 4 ++++ 3 files changed, 17 insertions(+) create mode 100644 lib/test.rb diff --git a/lib/.keep b/lib/.keep index e69de29bb..8b1378917 100644 --- a/lib/.keep +++ b/lib/.keep @@ -0,0 +1 @@ + diff --git a/lib/test.rb b/lib/test.rb new file mode 100644 index 000000000..8e6bcf5a7 --- /dev/null +++ b/lib/test.rb @@ -0,0 +1,12 @@ +class Sum + attr_reader :num1, :num2 + + def initialize(num1, num2) + @num1 = num1 + @num2 = num2 + end + + def sum + return num1 + num2 + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 4d1e3fdc8..d254c8b80 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,3 +1,6 @@ +require 'simplecov' +SimpleCov.start + require 'minitest' require 'minitest/autorun' require 'minitest/reporters' @@ -6,3 +9,4 @@ Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new # Require_relative your lib files here! +require_relative '../lib/test' From f84bf419800b7ed48a18f346715c962f2721142a Mon Sep 17 00:00:00 2001 From: Karis Kim Date: Tue, 4 Sep 2018 11:31:41 -0700 Subject: [PATCH 02/70] created test_spec file to test guard --- spec/test_spec.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 spec/test_spec.rb diff --git a/spec/test_spec.rb b/spec/test_spec.rb new file mode 100644 index 000000000..dd68b7643 --- /dev/null +++ b/spec/test_spec.rb @@ -0,0 +1,12 @@ +require_relative 'spec_helper' + +describe "new class" do + it "sum method" do + num1 = 1 + num2 = 1 + + sum = Sum.new(num1, num2).sum + + expect(sum).must_equal 2 + end +end From 511ead2890639a2b7de34a7cd4df588c27db163e Mon Sep 17 00:00:00 2001 From: Karis Kim Date: Tue, 4 Sep 2018 14:07:01 -0700 Subject: [PATCH 03/70] added awesome print, minitest/skip_dsl --- spec/spec_helper.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index d254c8b80..c118e898e 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -4,7 +4,8 @@ require 'minitest' require 'minitest/autorun' require 'minitest/reporters' -# Add simplecov +require 'minitest/skip_dsl' +require 'awesome_print' Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new From bb9a903124a5ec127cea975defcfd1d3f290018b Mon Sep 17 00:00:00 2001 From: Karis Kim Date: Tue, 4 Sep 2018 14:39:16 -0700 Subject: [PATCH 04/70] added change Dan requested --- Guardfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Guardfile b/Guardfile index 6760f9177..fa59fc3ef 100644 --- a/Guardfile +++ b/Guardfile @@ -1,4 +1,4 @@ -guard :minitest, bundler: false, rubygems: false do +guard :minitest, bundler: false, autorun: false, rubygems: false do # with Minitest::Spec watch(%r{^spec/(.*)_spec\.rb$}) watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" } From 8609b2a8e68aa17102892e17c80cbb1362461881 Mon Sep 17 00:00:00 2001 From: Karis Kim Date: Tue, 4 Sep 2018 14:47:53 -0700 Subject: [PATCH 05/70] no change, just finalizing that guardfile is good now --- Guardfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Guardfile b/Guardfile index fa59fc3ef..3f057633d 100644 --- a/Guardfile +++ b/Guardfile @@ -1,6 +1,6 @@ guard :minitest, bundler: false, autorun: false, rubygems: false do # with Minitest::Spec watch(%r{^spec/(.*)_spec\.rb$}) - watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" } + watch(%r{^lib/(.+)\.rb$}) { |m| "speguc/#{m[1]}_spec.rb" } watch(%r{^spec/spec_helper\.rb$}) { 'spec' } end From 05a329a6ac496fa3738f7409f55d19adc8c40c3c Mon Sep 17 00:00:00 2001 From: Karis Kim Date: Tue, 4 Sep 2018 14:48:25 -0700 Subject: [PATCH 06/70] deleted lib/test.rb since that was just for initial set up before real project begins --- lib/test.rb | 12 ------------ 1 file changed, 12 deletions(-) delete mode 100644 lib/test.rb diff --git a/lib/test.rb b/lib/test.rb deleted file mode 100644 index 8e6bcf5a7..000000000 --- a/lib/test.rb +++ /dev/null @@ -1,12 +0,0 @@ -class Sum - attr_reader :num1, :num2 - - def initialize(num1, num2) - @num1 = num1 - @num2 = num2 - end - - def sum - return num1 + num2 - end -end From 7cfd9933c4dcfb15a5b2bc9bc175ca7cae17c790 Mon Sep 17 00:00:00 2001 From: Karis Kim Date: Tue, 4 Sep 2018 14:49:24 -0700 Subject: [PATCH 07/70] created rooms in hotel method where it will load all rooms in hotel as array --- lib/reservation_creator.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 lib/reservation_creator.rb diff --git a/lib/reservation_creator.rb b/lib/reservation_creator.rb new file mode 100644 index 000000000..4357a1175 --- /dev/null +++ b/lib/reservation_creator.rb @@ -0,0 +1,10 @@ +module Hotel + class Reservation + + def rooms_in_hotel + rooms = [*"Room 1".."Room 20"] + end + + + end +end From 0a433dcf535678c9986ac4ac04b85beec1fd2a80 Mon Sep 17 00:00:00 2001 From: Karis Kim Date: Tue, 4 Sep 2018 14:50:44 -0700 Subject: [PATCH 08/70] created test for rooms in hotel and it passed. checked if array is returned --- spec/reservation_creator_spec.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 spec/reservation_creator_spec.rb diff --git a/spec/reservation_creator_spec.rb b/spec/reservation_creator_spec.rb new file mode 100644 index 000000000..c818e0021 --- /dev/null +++ b/spec/reservation_creator_spec.rb @@ -0,0 +1,12 @@ +require_relative 'spec_helper' + +describe "BookingSystem class" do + before do + list = Hotel::Reservation.new + end + + it "rooms in hotel method" do + expect(list.rooms_in_hotel).must_be_kind_of Array + expect(list.rooms_in_hotel).must_equal [*"Room 1".."Room 20"] + end +end From 7dbba012b03057c6590ff651be2ba8147a725f50 Mon Sep 17 00:00:00 2001 From: Karis Kim Date: Tue, 4 Sep 2018 16:25:07 -0700 Subject: [PATCH 09/70] created initialize method with room number, checkin date, checkout date --- lib/reservation_creator.rb | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/lib/reservation_creator.rb b/lib/reservation_creator.rb index 4357a1175..cf1ec1756 100644 --- a/lib/reservation_creator.rb +++ b/lib/reservation_creator.rb @@ -1,10 +1,27 @@ +require 'date' + module Hotel class Reservation + attr_reader :room_number, :check_in_date, :check_out_date - def rooms_in_hotel - rooms = [*"Room 1".."Room 20"] + def initialize(room_number = 1, check_in_date, check_out_date) + @room_number = room_number.to_i + raise ArgumentError, "Invalid room number, only 1 to 20 allowed" if room_number > 20 + @check_in_date = Date.parse("#{check_in_date}") + @check_out_date = Date.parse("#{check_out_date}") + raise ArgumentError, "Checkout date cannot be before checkin date" unless check_in_date < check_out_date end - + # def rooms_in_hotel + # rooms = [*"Room 1".."Room 20"] + # end + # + # def show_all_rooms + # rooms_in_hotel.each_with_index { |room, number| return "#{number + 1}. #{room}"} + # end + # + # def choose_room_by_date(chosen_room) + # rooms.select {|room| return room if chosen_room == room } + # end end end From 2b292b800ebde3040a4a5417a344dd1f1e18c6cd Mon Sep 17 00:00:00 2001 From: Karis Kim Date: Tue, 4 Sep 2018 16:25:42 -0700 Subject: [PATCH 10/70] added test to see if initialize method raises argument errors and if 3 instance variables are created --- spec/reservation_creator_spec.rb | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/spec/reservation_creator_spec.rb b/spec/reservation_creator_spec.rb index c818e0021..26de98b89 100644 --- a/spec/reservation_creator_spec.rb +++ b/spec/reservation_creator_spec.rb @@ -1,12 +1,22 @@ require_relative 'spec_helper' describe "BookingSystem class" do - before do - list = Hotel::Reservation.new - end + # before do + # list = Hotel::Reservation.new + # end it "rooms in hotel method" do - expect(list.rooms_in_hotel).must_be_kind_of Array - expect(list.rooms_in_hotel).must_equal [*"Room 1".."Room 20"] + list = Hotel::Reservation.new(2, 180904, 180905) + + expect(list).must_be_kind_of Hotel::Reservation + expect(list.room_number).must_be_kind_of Integer + expect(list.room_number).must_equal 2 + expect(list.check_in_date).must_equal Date.parse("2018-09-04") + expect(list.check_out_date).must_equal Date.parse("2018-09-05") + end + + it "raises ArgumentError if check out date is before check in date" do + + expect{(list = Hotel::Reservation.new(2, 180904, 180903))}.must_raise ArgumentError end end From b15f56606f3982484e726f3c1ad861eb1ce5fe1c Mon Sep 17 00:00:00 2001 From: Karis Kim Date: Wed, 5 Sep 2018 16:05:24 -0700 Subject: [PATCH 11/70] created count_number_of_nights method to calculate number of nights user wants to book --- lib/reservation_creator.rb | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/lib/reservation_creator.rb b/lib/reservation_creator.rb index cf1ec1756..06bb647c9 100644 --- a/lib/reservation_creator.rb +++ b/lib/reservation_creator.rb @@ -1,27 +1,19 @@ require 'date' +require_relative 'rooms' module Hotel class Reservation - attr_reader :room_number, :check_in_date, :check_out_date + attr_reader :check_in_date, :check_out_date - def initialize(room_number = 1, check_in_date, check_out_date) - @room_number = room_number.to_i - raise ArgumentError, "Invalid room number, only 1 to 20 allowed" if room_number > 20 + def initialize(check_in_date, check_out_date) @check_in_date = Date.parse("#{check_in_date}") @check_out_date = Date.parse("#{check_out_date}") - raise ArgumentError, "Checkout date cannot be before checkin date" unless check_in_date < check_out_date + raise StandardError, "Checkout date cannot be before checkin date" unless check_in_date < check_out_date end - # def rooms_in_hotel - # rooms = [*"Room 1".."Room 20"] - # end - # - # def show_all_rooms - # rooms_in_hotel.each_with_index { |room, number| return "#{number + 1}. #{room}"} - # end - # - # def choose_room_by_date(chosen_room) - # rooms.select {|room| return room if chosen_room == room } - # end + def count_number_of_nights + num_of_nights = check_out_date.mjd - check_in_date.mjd + return num_of_nights + end end end From 00ef5ed806cfe458bbd67f071bada6c2dc0de902 Mon Sep 17 00:00:00 2001 From: Karis Kim Date: Wed, 5 Sep 2018 16:05:48 -0700 Subject: [PATCH 12/70] created test for count_number_of_nights method and it passed --- spec/reservation_creator_spec.rb | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/spec/reservation_creator_spec.rb b/spec/reservation_creator_spec.rb index 26de98b89..5885a5149 100644 --- a/spec/reservation_creator_spec.rb +++ b/spec/reservation_creator_spec.rb @@ -6,17 +6,24 @@ # end it "rooms in hotel method" do - list = Hotel::Reservation.new(2, 180904, 180905) + list = Hotel::Reservation.new(180904, 180905) expect(list).must_be_kind_of Hotel::Reservation - expect(list.room_number).must_be_kind_of Integer - expect(list.room_number).must_equal 2 + # expect(list.room_number).must_be_kind_of Integer + # expect(list.room_number).must_equal 2 expect(list.check_in_date).must_equal Date.parse("2018-09-04") expect(list.check_out_date).must_equal Date.parse("2018-09-05") end it "raises ArgumentError if check out date is before check in date" do + expect{(list = Hotel::Reservation.new(180904, 180903))}.must_raise StandardError + end - expect{(list = Hotel::Reservation.new(2, 180904, 180903))}.must_raise ArgumentError + it "calculates the number of nights the user wants to book" do + list = Hotel::Reservation.new(180904, 180905) + expect(list.count_number_of_nights).must_equal 1 end + + + end From 87edfda0df48308aa2be5c434b8bab01c3227bab Mon Sep 17 00:00:00 2001 From: Karis Kim Date: Thu, 6 Sep 2018 14:10:53 -0700 Subject: [PATCH 13/70] created booking system class with initialize method --- lib/booking_system.rb | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 lib/booking_system.rb diff --git a/lib/booking_system.rb b/lib/booking_system.rb new file mode 100644 index 000000000..d08fa591f --- /dev/null +++ b/lib/booking_system.rb @@ -0,0 +1,40 @@ +require_relative 'reservation_creator' + +module Hotel + class BookingSystem + attr_reader :reservation_date + + def initialize(reservation_date) + @reservation_date = Date.parse("#{reservation_date}") + end + # + # def available_rooms? + # return available_rooms + # + # end + # + # def load_pending_reservation(check_in_date, check_out_date) + # pending = Reservation.new(check_in_date, check_out_date) + # # need to figure out date range before doing this + # # need to check if date is already there + # + # unless booked_dates.empty?(pending.check_in_date) + # booked_dates << pending + # end + # end + # + # def assign_room + # end + # + # def rooms_in_hotel + # end + end +end + + +# , :booked_rooms, :booked_dates, :available_rooms, :available_dates +# , booked_rooms, booked_dates, available_rooms, available_dates +# @booked_rooms = [] +# @booked_dates = [] +# @available_rooms = [*1..20] +# @available_dates = [] From a52808e7f96db570c896c898c3c56b74801392de Mon Sep 17 00:00:00 2001 From: Karis Kim Date: Thu, 6 Sep 2018 14:11:13 -0700 Subject: [PATCH 14/70] created tests for booking system initialize method --- spec/booking_system_spec.rb | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 spec/booking_system_spec.rb diff --git a/spec/booking_system_spec.rb b/spec/booking_system_spec.rb new file mode 100644 index 000000000..55b472c26 --- /dev/null +++ b/spec/booking_system_spec.rb @@ -0,0 +1,9 @@ +require_relative 'spec_helper' + +describe "BookingSystem class" do + it "initialize method" do + booking = Hotel::BookingSystem.new(180904) + + expect(booking).must_be_kind_of Hotel::BookingSystem + end +end From aab529c6fbbe387b481ec5a6f0634eb3c7d3a46d Mon Sep 17 00:00:00 2001 From: Karis Kim Date: Thu, 6 Sep 2018 14:11:39 -0700 Subject: [PATCH 15/70] created rooms class but will determine if need to delete later --- lib/rooms.rb | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 lib/rooms.rb diff --git a/lib/rooms.rb b/lib/rooms.rb new file mode 100644 index 000000000..8bf9b604b --- /dev/null +++ b/lib/rooms.rb @@ -0,0 +1,17 @@ +module Hotel + class Rooms + def total_rooms_in_hotel + return total_num = 20 + end + + def all_room_numbers + return room_numbers = [*1..total_rooms_in_hotel] + end + + def list_rooms + rooms = [*"Room #{all_room_numbers.min}".."Room #{all_room_numbers.max}"] + + return rooms + end + end +end From 95de6be0521063d7d2905924c51968171441292b Mon Sep 17 00:00:00 2001 From: Karis Kim Date: Thu, 6 Sep 2018 14:11:54 -0700 Subject: [PATCH 16/70] created file to test rooms class --- spec/rooms_spec.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 spec/rooms_spec.rb diff --git a/spec/rooms_spec.rb b/spec/rooms_spec.rb new file mode 100644 index 000000000..bac31b9bd --- /dev/null +++ b/spec/rooms_spec.rb @@ -0,0 +1,11 @@ +# require_relative 'spec_helper' +# +# describe "Rooms class" do +# it "list rooms method" do +# expect(Hotel::Rooms.total_rooms_in_hotel).must_equal 20 +# end + + # it "raises ArgumentError if check out date is before check in date" do + # expect{(list = Hotel::Reservation.new(180904, 180903))}.must_raise StandardError + # end +# end From a032ab7450fdf98b2d6c43262464dab4e9fa3f4c Mon Sep 17 00:00:00 2001 From: Karis Kim Date: Thu, 6 Sep 2018 14:12:18 -0700 Subject: [PATCH 17/70] require relatived both booking system and rooms file --- spec/spec_helper.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index c118e898e..393f3aa85 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -10,4 +10,6 @@ Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new # Require_relative your lib files here! -require_relative '../lib/test' +require_relative '../lib/reservation_creator' +require_relative '../lib/rooms' +require_relative '../lib/booking_system' From 96323941765bef1d732b66b29a772423b430b384 Mon Sep 17 00:00:00 2001 From: Karis Kim Date: Thu, 6 Sep 2018 14:26:17 -0700 Subject: [PATCH 18/70] created test for rooms in hotel method --- spec/booking_system_spec.rb | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/spec/booking_system_spec.rb b/spec/booking_system_spec.rb index 55b472c26..10bed3850 100644 --- a/spec/booking_system_spec.rb +++ b/spec/booking_system_spec.rb @@ -1,9 +1,16 @@ require_relative 'spec_helper' describe "BookingSystem class" do + before do + @booking = Hotel::BookingSystem.new(180904) + end + it "initialize method" do - booking = Hotel::BookingSystem.new(180904) + expect(@booking).must_be_kind_of Hotel::BookingSystem + end - expect(booking).must_be_kind_of Hotel::BookingSystem + it "returns array of all room numbers in hotel" do + expect(@booking.rooms_in_hotel).must_be_kind_of Array + expect(@booking.rooms_in_hotel).must_equal [*1..20] end end From e8c00f0cd98d7281dcc3eb1870f710a56e67eb5f Mon Sep 17 00:00:00 2001 From: Karis Kim Date: Thu, 6 Sep 2018 14:26:38 -0700 Subject: [PATCH 19/70] created rooms in hotel method that lists all the rooms in hotel --- lib/booking_system.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/booking_system.rb b/lib/booking_system.rb index d08fa591f..15473efa0 100644 --- a/lib/booking_system.rb +++ b/lib/booking_system.rb @@ -26,8 +26,10 @@ def initialize(reservation_date) # def assign_room # end # - # def rooms_in_hotel - # end + def rooms_in_hotel + all_rooms = [*1..20] + return all_rooms + end end end From ee5555d50f89ca5912ddf0d609da0cdd417872cb Mon Sep 17 00:00:00 2001 From: Karis Kim Date: Thu, 6 Sep 2018 14:44:56 -0700 Subject: [PATCH 20/70] created reservation_date_range method to create array of all dates that will be reserved --- lib/reservation_creator.rb | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/reservation_creator.rb b/lib/reservation_creator.rb index 06bb647c9..c7fa85753 100644 --- a/lib/reservation_creator.rb +++ b/lib/reservation_creator.rb @@ -1,5 +1,4 @@ require 'date' -require_relative 'rooms' module Hotel class Reservation @@ -11,9 +10,9 @@ def initialize(check_in_date, check_out_date) raise StandardError, "Checkout date cannot be before checkin date" unless check_in_date < check_out_date end - def count_number_of_nights - num_of_nights = check_out_date.mjd - check_in_date.mjd - return num_of_nights + def reservation_date_range + date_range = [*check_in_date..check_in_date] + return date_range end end end From 9e47d43076977ccf8e1ace8f628b4cd94ea754f9 Mon Sep 17 00:00:00 2001 From: Karis Kim Date: Thu, 6 Sep 2018 14:45:31 -0700 Subject: [PATCH 21/70] created 2 tests for reservation_date_range method and passed both --- spec/reservation_creator_spec.rb | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/spec/reservation_creator_spec.rb b/spec/reservation_creator_spec.rb index 5885a5149..b0c04764e 100644 --- a/spec/reservation_creator_spec.rb +++ b/spec/reservation_creator_spec.rb @@ -1,29 +1,27 @@ require_relative 'spec_helper' -describe "BookingSystem class" do - # before do - # list = Hotel::Reservation.new - # end - - it "rooms in hotel method" do - list = Hotel::Reservation.new(180904, 180905) +describe "Reservation class" do + before do + @reservation = Hotel::Reservation.new(180904, 180905) + end - expect(list).must_be_kind_of Hotel::Reservation + it "initialize method" do + expect(@reservation).must_be_kind_of Hotel::Reservation # expect(list.room_number).must_be_kind_of Integer # expect(list.room_number).must_equal 2 - expect(list.check_in_date).must_equal Date.parse("2018-09-04") - expect(list.check_out_date).must_equal Date.parse("2018-09-05") + expect(@reservation.check_in_date).must_equal Date.parse("2018-09-04") + expect(@reservation.check_out_date).must_equal Date.parse("2018-09-05") end it "raises ArgumentError if check out date is before check in date" do expect{(list = Hotel::Reservation.new(180904, 180903))}.must_raise StandardError end - it "calculates the number of nights the user wants to book" do - list = Hotel::Reservation.new(180904, 180905) - expect(list.count_number_of_nights).must_equal 1 + it "returns array of all dates user wants to book" do + expect(@reservation.reservation_date_range).must_be_kind_of Array end - - + it "returns the array of dates except the check out date" do + expect(@reservation.reservation_date_range).must_equal [Date.parse("180904")] + end end From 4fa4d3b310e2fcdeb439692208e3f26aae60739e Mon Sep 17 00:00:00 2001 From: Karis Kim Date: Thu, 6 Sep 2018 15:56:21 -0700 Subject: [PATCH 22/70] changed instance variables as hash key/values --- lib/reservation_creator.rb | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/lib/reservation_creator.rb b/lib/reservation_creator.rb index c7fa85753..0255470f9 100644 --- a/lib/reservation_creator.rb +++ b/lib/reservation_creator.rb @@ -4,14 +4,23 @@ module Hotel class Reservation attr_reader :check_in_date, :check_out_date - def initialize(check_in_date, check_out_date) - @check_in_date = Date.parse("#{check_in_date}") - @check_out_date = Date.parse("#{check_out_date}") + def initialize(input) + @check_in_date = input[:check_in_date] + @check_out_date = input[:check_out_date] raise StandardError, "Checkout date cannot be before checkin date" unless check_in_date < check_out_date end + def checkin_num_to_date + puts :check_date + return Date.parse("#{check_in_date}") + end + + def checkout_num_to_date + return Date.parse("#{check_out_date}") + end + def reservation_date_range - date_range = [*check_in_date..check_in_date] + date_range = [*checkin_num_to_date..checkout_num_to_date] return date_range end end From 6afd0c18a23a69f9670452461916625d2912509e Mon Sep 17 00:00:00 2001 From: Karis Kim Date: Thu, 6 Sep 2018 15:56:56 -0700 Subject: [PATCH 23/70] changed tests to accommodate the hash as input --- spec/reservation_creator_spec.rb | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/spec/reservation_creator_spec.rb b/spec/reservation_creator_spec.rb index b0c04764e..e038d6c20 100644 --- a/spec/reservation_creator_spec.rb +++ b/spec/reservation_creator_spec.rb @@ -2,15 +2,13 @@ describe "Reservation class" do before do - @reservation = Hotel::Reservation.new(180904, 180905) + @reservation = Hotel::Reservation.new({check_in_date: 180904, check_out_date: 180905}) end it "initialize method" do expect(@reservation).must_be_kind_of Hotel::Reservation - # expect(list.room_number).must_be_kind_of Integer - # expect(list.room_number).must_equal 2 - expect(@reservation.check_in_date).must_equal Date.parse("2018-09-04") - expect(@reservation.check_out_date).must_equal Date.parse("2018-09-05") + expect(@reservation.check_in_date).must_be_kind_of Integer + expect(@reservation.check_out_date).must_be_kind_of Integer end it "raises ArgumentError if check out date is before check in date" do @@ -19,9 +17,10 @@ it "returns array of all dates user wants to book" do expect(@reservation.reservation_date_range).must_be_kind_of Array + end it "returns the array of dates except the check out date" do - expect(@reservation.reservation_date_range).must_equal [Date.parse("180904")] - end + expect(@reservation.reservation_date_range).must_equal [Date.parse("180904")] +end end From 89ff764c98a4e9aebe8e8c6f6be49e95b0edc197 Mon Sep 17 00:00:00 2001 From: Karis Kim Date: Fri, 7 Sep 2018 10:54:36 -0700 Subject: [PATCH 24/70] created assign_room_to_booking method to assign room to the date range --- lib/booking_system.rb | 52 +++++++++++++++++-------------------------- 1 file changed, 21 insertions(+), 31 deletions(-) diff --git a/lib/booking_system.rb b/lib/booking_system.rb index 15473efa0..5060e498e 100644 --- a/lib/booking_system.rb +++ b/lib/booking_system.rb @@ -2,41 +2,31 @@ module Hotel class BookingSystem - attr_reader :reservation_date + # attr_reader :reservation_date - def initialize(reservation_date) - @reservation_date = Date.parse("#{reservation_date}") + def initialize + @booked_dates = [] end - # - # def available_rooms? - # return available_rooms - # - # end - # - # def load_pending_reservation(check_in_date, check_out_date) - # pending = Reservation.new(check_in_date, check_out_date) - # # need to figure out date range before doing this - # # need to check if date is already there - # - # unless booked_dates.empty?(pending.check_in_date) - # booked_dates << pending - # end - # end - # - # def assign_room - # end - # - def rooms_in_hotel + + def new_reservation(reservation) + @booked_dates << reservation.booking_date_range + return @booked_dates.flatten! + end + + def assign_room_to_booking(reservation) + assigned_room = [] + + reservation.booking_date_range.each do |date| + room_selected = [date, list_all_rooms_in_hotel.first] + assigned_room << room_selected + end + + return assigned_room + end + + def list_all_rooms_in_hotel all_rooms = [*1..20] return all_rooms end end end - - -# , :booked_rooms, :booked_dates, :available_rooms, :available_dates -# , booked_rooms, booked_dates, available_rooms, available_dates -# @booked_rooms = [] -# @booked_dates = [] -# @available_rooms = [*1..20] -# @available_dates = [] From 42bbd4133c1ddd7530ea12bf076ab8e3f6ac0465 Mon Sep 17 00:00:00 2001 From: Karis Kim Date: Fri, 7 Sep 2018 10:55:03 -0700 Subject: [PATCH 25/70] added tests to pass assigned room to booking method --- spec/booking_system_spec.rb | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/spec/booking_system_spec.rb b/spec/booking_system_spec.rb index 10bed3850..90b854008 100644 --- a/spec/booking_system_spec.rb +++ b/spec/booking_system_spec.rb @@ -2,7 +2,9 @@ describe "BookingSystem class" do before do - @booking = Hotel::BookingSystem.new(180904) + @reservation = Hotel::Reservation.new({check_in_date: 180904, check_out_date: 180907}) + + @booking = Hotel::BookingSystem.new end it "initialize method" do @@ -10,7 +12,21 @@ end it "returns array of all room numbers in hotel" do - expect(@booking.rooms_in_hotel).must_be_kind_of Array - expect(@booking.rooms_in_hotel).must_equal [*1..20] + expect(@booking.list_all_rooms_in_hotel).must_be_kind_of Array + expect(@booking.list_all_rooms_in_hotel).must_equal [*1..20] + end + + it "returns all dates that have been booked" do + expect(@booking.new_reservation(@reservation)).must_equal [Date.parse("180904"), Date.parse("180905"), Date.parse("180906")] + end + + it "returns array of dates with the assigned room" do + expect(@booking.assign_room_to_booking(@reservation)).must_equal [[Date.parse("180904"), 1], [Date.parse("180905"), 1], [Date.parse("180906"), 1]] + end + + xit "" do + end + + xit "" do end end From 1b1212c14fb5b1bd92e4363e70447a9058b47e8d Mon Sep 17 00:00:00 2001 From: Karis Kim Date: Fri, 7 Sep 2018 11:03:37 -0700 Subject: [PATCH 26/70] created calculate_booking_cost method to calculate cost of booking by date range --- lib/booking_system.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/booking_system.rb b/lib/booking_system.rb index 5060e498e..ba8387a5a 100644 --- a/lib/booking_system.rb +++ b/lib/booking_system.rb @@ -28,5 +28,10 @@ def list_all_rooms_in_hotel all_rooms = [*1..20] return all_rooms end + + def calculate_booking_cost(reservation) + cost = reservation.booking_date_range.length * 200 + return cost + end end end From 4bd629b1ef65ed7eb11a45c063bb763ac5562c75 Mon Sep 17 00:00:00 2001 From: Karis Kim Date: Fri, 7 Sep 2018 11:03:55 -0700 Subject: [PATCH 27/70] created test for calculate_booking_cost method and passed all tests --- spec/booking_system_spec.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/spec/booking_system_spec.rb b/spec/booking_system_spec.rb index 90b854008..37a682ece 100644 --- a/spec/booking_system_spec.rb +++ b/spec/booking_system_spec.rb @@ -17,14 +17,18 @@ end it "returns all dates that have been booked" do + expect(@booking.list_all_rooms_in_hotel).must_be_kind_of Array expect(@booking.new_reservation(@reservation)).must_equal [Date.parse("180904"), Date.parse("180905"), Date.parse("180906")] end it "returns array of dates with the assigned room" do + expect(@booking.list_all_rooms_in_hotel).must_be_kind_of Array expect(@booking.assign_room_to_booking(@reservation)).must_equal [[Date.parse("180904"), 1], [Date.parse("180905"), 1], [Date.parse("180906"), 1]] end - xit "" do + it "returns the total cost of the booking" do + expect(@booking.calculate_booking_cost(@reservation)).must_be_kind_of Integer + expect(@booking.calculate_booking_cost(@reservation)).must_equal 600 end xit "" do From c8d4d2d12acb727a82618f27914301c6ab3d54ab Mon Sep 17 00:00:00 2001 From: Karis Kim Date: Fri, 7 Sep 2018 11:13:40 -0700 Subject: [PATCH 28/70] made booked_dates as attr_reader to read it --- lib/booking_system.rb | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/lib/booking_system.rb b/lib/booking_system.rb index ba8387a5a..54fa215af 100644 --- a/lib/booking_system.rb +++ b/lib/booking_system.rb @@ -2,26 +2,18 @@ module Hotel class BookingSystem - # attr_reader :reservation_date + attr_reader :booked_dates def initialize @booked_dates = [] end - def new_reservation(reservation) - @booked_dates << reservation.booking_date_range - return @booked_dates.flatten! - end - def assign_room_to_booking(reservation) - assigned_room = [] - - reservation.booking_date_range.each do |date| + @booked_dates = reservation.booking_date_range.map do |date| room_selected = [date, list_all_rooms_in_hotel.first] - assigned_room << room_selected end - return assigned_room + return @booked_dates end def list_all_rooms_in_hotel @@ -33,5 +25,9 @@ def calculate_booking_cost(reservation) cost = reservation.booking_date_range.length * 200 return cost end + + def list_bookings_by_date(date) + + end end end From 1e04809d26b61dbab7538d151d033fce055953cc Mon Sep 17 00:00:00 2001 From: Karis Kim Date: Fri, 7 Sep 2018 11:14:59 -0700 Subject: [PATCH 29/70] created test for checking existence of booked_dates and for creating the booking system class --- spec/booking_system_spec.rb | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/spec/booking_system_spec.rb b/spec/booking_system_spec.rb index 37a682ece..2bee69baf 100644 --- a/spec/booking_system_spec.rb +++ b/spec/booking_system_spec.rb @@ -7,22 +7,21 @@ @booking = Hotel::BookingSystem.new end - it "initialize method" do + it "create BookingSystem class" do expect(@booking).must_be_kind_of Hotel::BookingSystem end - it "returns array of all room numbers in hotel" do - expect(@booking.list_all_rooms_in_hotel).must_be_kind_of Array - expect(@booking.list_all_rooms_in_hotel).must_equal [*1..20] + it "initialize method" do + expect(@booking.booked_dates).must_be_kind_of Array end - it "returns all dates that have been booked" do + it "returns array of all room numbers in hotel" do expect(@booking.list_all_rooms_in_hotel).must_be_kind_of Array - expect(@booking.new_reservation(@reservation)).must_equal [Date.parse("180904"), Date.parse("180905"), Date.parse("180906")] + expect(@booking.list_all_rooms_in_hotel).must_equal [*1..20] end it "returns array of dates with the assigned room" do - expect(@booking.list_all_rooms_in_hotel).must_be_kind_of Array + expect(@booking.assign_room_to_booking(@reservation)).must_be_kind_of Array expect(@booking.assign_room_to_booking(@reservation)).must_equal [[Date.parse("180904"), 1], [Date.parse("180905"), 1], [Date.parse("180906"), 1]] end From d721fa5b8b20a83c0b76f8152152bc78f9460b37 Mon Sep 17 00:00:00 2001 From: Karis Kim Date: Fri, 7 Sep 2018 14:48:16 -0700 Subject: [PATCH 30/70] deleted unnecessary file --- spec/test_spec.rb | 12 ------------ 1 file changed, 12 deletions(-) delete mode 100644 spec/test_spec.rb diff --git a/spec/test_spec.rb b/spec/test_spec.rb deleted file mode 100644 index dd68b7643..000000000 --- a/spec/test_spec.rb +++ /dev/null @@ -1,12 +0,0 @@ -require_relative 'spec_helper' - -describe "new class" do - it "sum method" do - num1 = 1 - num2 = 1 - - sum = Sum.new(num1, num2).sum - - expect(sum).must_equal 2 - end -end From 50914347f139fd77f7a4e5e245f0fa760facbb74 Mon Sep 17 00:00:00 2001 From: Karis Kim Date: Fri, 7 Sep 2018 14:49:05 -0700 Subject: [PATCH 31/70] changed file name and made booking system create a new instance of reservation --- spec/reservation_creator_spec.rb | 26 -------------------------- 1 file changed, 26 deletions(-) delete mode 100644 spec/reservation_creator_spec.rb diff --git a/spec/reservation_creator_spec.rb b/spec/reservation_creator_spec.rb deleted file mode 100644 index e038d6c20..000000000 --- a/spec/reservation_creator_spec.rb +++ /dev/null @@ -1,26 +0,0 @@ -require_relative 'spec_helper' - -describe "Reservation class" do - before do - @reservation = Hotel::Reservation.new({check_in_date: 180904, check_out_date: 180905}) - end - - it "initialize method" do - expect(@reservation).must_be_kind_of Hotel::Reservation - expect(@reservation.check_in_date).must_be_kind_of Integer - expect(@reservation.check_out_date).must_be_kind_of Integer - end - - it "raises ArgumentError if check out date is before check in date" do - expect{(list = Hotel::Reservation.new(180904, 180903))}.must_raise StandardError - end - - it "returns array of all dates user wants to book" do - expect(@reservation.reservation_date_range).must_be_kind_of Array - - end - - it "returns the array of dates except the check out date" do - expect(@reservation.reservation_date_range).must_equal [Date.parse("180904")] -end -end From adc40e1a8ce76cf5e56dfd80941e03281b142957 Mon Sep 17 00:00:00 2001 From: Karis Kim Date: Fri, 7 Sep 2018 14:49:34 -0700 Subject: [PATCH 32/70] created make_reservation that makes new instance of reservation within booking system --- lib/booking_system.rb | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/lib/booking_system.rb b/lib/booking_system.rb index 54fa215af..2aabd90b6 100644 --- a/lib/booking_system.rb +++ b/lib/booking_system.rb @@ -1,19 +1,26 @@ -require_relative 'reservation_creator' +require_relative 'reservation' module Hotel class BookingSystem - attr_reader :booked_dates + attr_reader :rooms, :reservations def initialize - @booked_dates = [] + @reservations = [] + @rooms = [*1..20] end - def assign_room_to_booking(reservation) - @booked_dates = reservation.booking_date_range.map do |date| - room_selected = [date, list_all_rooms_in_hotel.first] - end + def make_reservation(check_in_date:, check_out_date:) + # TODO find a room that's available + room_number = get_available_room(check_in_date: check_in_date, check_out_date: check_out_date) + + reservation = Reservation.new(check_in_date: check_in_date, check_out_date: check_out_date, room_number: room_number) + + @reservations << reservation + return reservation + end - return @booked_dates + def get_available_room(check_in_date:, check_out_date:) + return @rooms.first end def list_all_rooms_in_hotel @@ -26,8 +33,16 @@ def calculate_booking_cost(reservation) return cost end - def list_bookings_by_date(date) - + def list_bookings_by_date(specific_date) + @booked_dates.select do |date| + if date[0] == specific_date + return date + else + ################ + #rescue later on + return nil + end + end end end end From 24178643dd9e32edb86f269d5b9f2d4c472789f9 Mon Sep 17 00:00:00 2001 From: Karis Kim Date: Fri, 7 Sep 2018 14:49:50 -0700 Subject: [PATCH 33/70] changed file names --- spec/spec_helper.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 393f3aa85..a2e2a537b 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -10,6 +10,6 @@ Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new # Require_relative your lib files here! -require_relative '../lib/reservation_creator' +require_relative '../lib/reservation' require_relative '../lib/rooms' require_relative '../lib/booking_system' From 677185b31921f8a701dff7e2e9beecd39c4679b1 Mon Sep 17 00:00:00 2001 From: Karis Kim Date: Fri, 7 Sep 2018 14:50:17 -0700 Subject: [PATCH 34/70] deleted unnecessary methods --- lib/reservation_creator.rb | 27 --------------------------- 1 file changed, 27 deletions(-) delete mode 100644 lib/reservation_creator.rb diff --git a/lib/reservation_creator.rb b/lib/reservation_creator.rb deleted file mode 100644 index 0255470f9..000000000 --- a/lib/reservation_creator.rb +++ /dev/null @@ -1,27 +0,0 @@ -require 'date' - -module Hotel - class Reservation - attr_reader :check_in_date, :check_out_date - - def initialize(input) - @check_in_date = input[:check_in_date] - @check_out_date = input[:check_out_date] - raise StandardError, "Checkout date cannot be before checkin date" unless check_in_date < check_out_date - end - - def checkin_num_to_date - puts :check_date - return Date.parse("#{check_in_date}") - end - - def checkout_num_to_date - return Date.parse("#{check_out_date}") - end - - def reservation_date_range - date_range = [*checkin_num_to_date..checkout_num_to_date] - return date_range - end - end -end From c0c9721715ca741f94dca00d660e83b4f8365cca Mon Sep 17 00:00:00 2001 From: Karis Kim Date: Fri, 7 Sep 2018 14:50:35 -0700 Subject: [PATCH 35/70] deleted unnecessary methods --- spec/booking_system_spec.rb | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/spec/booking_system_spec.rb b/spec/booking_system_spec.rb index 2bee69baf..c9ceeb3d4 100644 --- a/spec/booking_system_spec.rb +++ b/spec/booking_system_spec.rb @@ -2,9 +2,8 @@ describe "BookingSystem class" do before do - @reservation = Hotel::Reservation.new({check_in_date: 180904, check_out_date: 180907}) - @booking = Hotel::BookingSystem.new + @reservation = @booking.make_reservation(check_in_date: 180904, check_out_date: 180907) end it "create BookingSystem class" do @@ -30,6 +29,25 @@ expect(@booking.calculate_booking_cost(@reservation)).must_equal 600 end - xit "" do + it "list all bookings of that date" do + specific_date = Date.parse("180904") + @booking.assign_room_to_booking(@reservation) + + expect(@booking.list_bookings_by_date(specific_date)).must_equal [Date.parse("180904"), 1] + end + + it "returns nil if no booking was made for that date" do + specific_date = Date.parse("180907") + @booking.assign_room_to_booking(@reservation) + + expect(@booking.list_bookings_by_date(specific_date)).must_equal nil + end + + it "adds a second booking to the booked_dates" do + @booking.assign_room_to_booking(@reservation) + @reservation = Hotel::Reservation.new({check_in_date: 180904, check_out_date: 180905}) + @booking.assign_room_to_booking(@reservation) + + expect(@booking.assign_room_to_booking(@reservation)).must_equal [[Date.parse("180904"), 1], [Date.parse("180905"), 1], [Date.parse("180906"), 1], [Date.parse("180904"), 1]] end end From f1b9bf8e75bdd3b69750d1cd38e3f1c3a2ca223d Mon Sep 17 00:00:00 2001 From: Karis Kim Date: Fri, 7 Sep 2018 14:50:49 -0700 Subject: [PATCH 36/70] changed file name for readability --- lib/reservation.rb | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 lib/reservation.rb diff --git a/lib/reservation.rb b/lib/reservation.rb new file mode 100644 index 000000000..7605ecc9e --- /dev/null +++ b/lib/reservation.rb @@ -0,0 +1,30 @@ +require 'date' + +module Hotel + class Reservation + attr_reader :check_in_date, :check_out_date + + def initialize(input) + @check_in_date = input[:check_in_date] + @check_out_date = input[:check_out_date] + raise StandardError, "Checkout date cannot be before checkin date" unless check_in_date < check_out_date + # @room_number = [*1..20].first + # input[:room_number] + + @room_number = input[:room_number] + end + + def checkin_num_to_date + return Date.parse("#{check_in_date}") + end + + def checkout_num_to_date + return Date.parse("#{check_out_date}") + end + + def booking_date_range + date_range = [*checkin_num_to_date...checkout_num_to_date] + return date_range + end + end +end From 53b73d6b58b8c61bbb3ae31651445c8d6933e255 Mon Sep 17 00:00:00 2001 From: Karis Kim Date: Fri, 7 Sep 2018 14:51:07 -0700 Subject: [PATCH 37/70] changed file name for readability --- spec/reservation_spec.rb | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 spec/reservation_spec.rb diff --git a/spec/reservation_spec.rb b/spec/reservation_spec.rb new file mode 100644 index 000000000..47d264561 --- /dev/null +++ b/spec/reservation_spec.rb @@ -0,0 +1,26 @@ +require_relative 'spec_helper' + +describe "Reservation class" do + before do + @reservation = Hotel::Reservation.new({check_in_date: 180904, check_out_date: 180905}) + end + + it "initialize method" do + expect(@reservation).must_be_kind_of Hotel::Reservation + expect(@reservation.check_in_date).must_be_kind_of Integer + expect(@reservation.check_out_date).must_be_kind_of Integer + end + + it "raises ArgumentError if check out date is before check in date" do + expect{(list = Hotel::Reservation.new(180904, 180903))}.must_raise StandardError + end + + it "returns array of all dates user wants to book" do + expect(@reservation.booking_date_range).must_be_kind_of Array + + end + + it "returns the array of dates except the check out date" do + expect(@reservation.booking_date_range).must_equal [Date.parse("180904")] +end +end From 6a77f5b77d114bdab499fe9eefc7687c110e7dfd Mon Sep 17 00:00:00 2001 From: Karis Kim Date: Fri, 7 Sep 2018 16:33:02 -0700 Subject: [PATCH 38/70] created list_existing_reservations --- lib/booking_system.rb | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/lib/booking_system.rb b/lib/booking_system.rb index 2aabd90b6..db9ab5163 100644 --- a/lib/booking_system.rb +++ b/lib/booking_system.rb @@ -23,26 +23,23 @@ def get_available_room(check_in_date:, check_out_date:) return @rooms.first end - def list_all_rooms_in_hotel - all_rooms = [*1..20] - return all_rooms + def list_all_rooms + return @rooms end - def calculate_booking_cost(reservation) - cost = reservation.booking_date_range.length * 200 - return cost - end + def list_existing_reservations(date) + bookings_by_date = [] - def list_bookings_by_date(specific_date) - @booked_dates.select do |date| - if date[0] == specific_date - return date - else - ################ - #rescue later on - return nil + specific_date = Date.parse("#{date}") + @reservations.each do |booking| + booking.date_range.each do |date| + if specific_date == date + bookings_by_date << booking + end end end + + return bookings_by_date end end end From 929977f9fc4d1250ba13411f76d45bebe431807f Mon Sep 17 00:00:00 2001 From: Karis Kim Date: Fri, 7 Sep 2018 16:33:21 -0700 Subject: [PATCH 39/70] created calculate_booking_cost method --- lib/reservation.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/reservation.rb b/lib/reservation.rb index 7605ecc9e..fdd1606b6 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -22,9 +22,14 @@ def checkout_num_to_date return Date.parse("#{check_out_date}") end - def booking_date_range + def date_range date_range = [*checkin_num_to_date...checkout_num_to_date] return date_range end + + def calculate_booking_cost + cost = date_range.length * 200 + return cost + end end end From e51b6f838f25164741c66091f73514e345e4a19e Mon Sep 17 00:00:00 2001 From: Karis Kim Date: Fri, 7 Sep 2018 16:33:59 -0700 Subject: [PATCH 40/70] created test for list_existing_reservations --- spec/booking_system_spec.rb | 35 +++++++++-------------------------- 1 file changed, 9 insertions(+), 26 deletions(-) diff --git a/spec/booking_system_spec.rb b/spec/booking_system_spec.rb index c9ceeb3d4..3dc46ca3d 100644 --- a/spec/booking_system_spec.rb +++ b/spec/booking_system_spec.rb @@ -11,43 +11,26 @@ end it "initialize method" do - expect(@booking.booked_dates).must_be_kind_of Array + expect(@booking.reservations).must_be_kind_of Array end it "returns array of all room numbers in hotel" do - expect(@booking.list_all_rooms_in_hotel).must_be_kind_of Array - expect(@booking.list_all_rooms_in_hotel).must_equal [*1..20] + expect(@booking.list_all_rooms).must_be_kind_of Array + expect(@booking.list_all_rooms).must_equal [*1..20] end - it "returns array of dates with the assigned room" do - expect(@booking.assign_room_to_booking(@reservation)).must_be_kind_of Array - expect(@booking.assign_room_to_booking(@reservation)).must_equal [[Date.parse("180904"), 1], [Date.parse("180905"), 1], [Date.parse("180906"), 1]] + it "returns array of dates" do + expect(@reservation).must_be_kind_of Hotel::Reservation + expect(@reservation.date_range).must_equal [Date.parse("180904"), Date.parse("180905"), Date.parse("180906")] end it "returns the total cost of the booking" do - expect(@booking.calculate_booking_cost(@reservation)).must_be_kind_of Integer - expect(@booking.calculate_booking_cost(@reservation)).must_equal 600 + expect(@reservation.calculate_booking_cost).must_equal 600 end it "list all bookings of that date" do - specific_date = Date.parse("180904") - @booking.assign_room_to_booking(@reservation) + @reservation = @booking.make_reservation(check_in_date: 180909, check_out_date: 1809010) - expect(@booking.list_bookings_by_date(specific_date)).must_equal [Date.parse("180904"), 1] - end - - it "returns nil if no booking was made for that date" do - specific_date = Date.parse("180907") - @booking.assign_room_to_booking(@reservation) - - expect(@booking.list_bookings_by_date(specific_date)).must_equal nil - end - - it "adds a second booking to the booked_dates" do - @booking.assign_room_to_booking(@reservation) - @reservation = Hotel::Reservation.new({check_in_date: 180904, check_out_date: 180905}) - @booking.assign_room_to_booking(@reservation) - - expect(@booking.assign_room_to_booking(@reservation)).must_equal [[Date.parse("180904"), 1], [Date.parse("180905"), 1], [Date.parse("180906"), 1], [Date.parse("180904"), 1]] + expect(@booking.list_existing_reservations(180904)).must_equal [@reservation] end end From f47355e64dbffea5477fa657d808209197ddae1c Mon Sep 17 00:00:00 2001 From: Karis Kim Date: Fri, 7 Sep 2018 16:34:28 -0700 Subject: [PATCH 41/70] created test for list_existing_reservations method --- spec/reservation_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/reservation_spec.rb b/spec/reservation_spec.rb index 47d264561..26ab7594c 100644 --- a/spec/reservation_spec.rb +++ b/spec/reservation_spec.rb @@ -16,11 +16,11 @@ end it "returns array of all dates user wants to book" do - expect(@reservation.booking_date_range).must_be_kind_of Array + expect(@reservation.date_range).must_be_kind_of Array end it "returns the array of dates except the check out date" do - expect(@reservation.booking_date_range).must_equal [Date.parse("180904")] + expect(@reservation.date_range).must_equal [Date.parse("180904")] end end From 47864f1661dc8f6458bc4e7bdd3cde8ff804742e Mon Sep 17 00:00:00 2001 From: Karis Kim Date: Sat, 8 Sep 2018 14:20:09 -0700 Subject: [PATCH 42/70] added code in make reservation method for edge case of overlapping dates --- lib/booking_system.rb | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/lib/booking_system.rb b/lib/booking_system.rb index db9ab5163..9d4bfe794 100644 --- a/lib/booking_system.rb +++ b/lib/booking_system.rb @@ -1,4 +1,5 @@ require_relative 'reservation' +require 'pry' module Hotel class BookingSystem @@ -11,12 +12,34 @@ def initialize def make_reservation(check_in_date:, check_out_date:) # TODO find a room that's available + + + room_number = get_available_room(check_in_date: check_in_date, check_out_date: check_out_date) reservation = Reservation.new(check_in_date: check_in_date, check_out_date: check_out_date, room_number: room_number) - @reservations << reservation - return reservation + if @reservations.empty? + @reservations << reservation + # binding.pry + return reservation + elsif @reservations.length >= 1 + requested_dates = reservation.date_range + + @reservations.each do |booking| + reserved_dates = booking.date_range + + reserved_dates.zip(requested_dates).each do |date1, date2| + unless date1 != date2 + raise ArgumentError, "unavailable date" + end + end + end + + @reservations << reservation + # binding.pry + return @reservations + end end def get_available_room(check_in_date:, check_out_date:) @@ -27,7 +50,7 @@ def list_all_rooms return @rooms end - def list_existing_reservations(date) + def list_reservations_by_date(date) bookings_by_date = [] specific_date = Date.parse("#{date}") @@ -41,5 +64,11 @@ def list_existing_reservations(date) return bookings_by_date end + + def available_rooms_for_date_range(check_in_date:, check_out_date:) + + + @reservations + end end end From 6a4136bae5ab62eb1e12825d19d4d4371b48c4c3 Mon Sep 17 00:00:00 2001 From: Karis Kim Date: Sat, 8 Sep 2018 14:21:09 -0700 Subject: [PATCH 43/70] no change, removed the added room number again --- lib/reservation.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/reservation.rb b/lib/reservation.rb index fdd1606b6..840359e1b 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -6,11 +6,9 @@ class Reservation def initialize(input) @check_in_date = input[:check_in_date] + @check_out_date = input[:check_out_date] raise StandardError, "Checkout date cannot be before checkin date" unless check_in_date < check_out_date - # @room_number = [*1..20].first - # input[:room_number] - @room_number = input[:room_number] end From d1ea68c8c73aebad373827bb81421d5bb41084b5 Mon Sep 17 00:00:00 2001 From: Karis Kim Date: Sat, 8 Sep 2018 14:21:25 -0700 Subject: [PATCH 44/70] added test for checking for edge case of overlapping dates --- spec/booking_system_spec.rb | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/spec/booking_system_spec.rb b/spec/booking_system_spec.rb index 3dc46ca3d..b83ad0a9f 100644 --- a/spec/booking_system_spec.rb +++ b/spec/booking_system_spec.rb @@ -29,8 +29,20 @@ end it "list all bookings of that date" do - @reservation = @booking.make_reservation(check_in_date: 180909, check_out_date: 1809010) + bad_reservation = @booking.make_reservation(check_in_date: 180909, check_out_date: 1809010) - expect(@booking.list_existing_reservations(180904)).must_equal [@reservation] + expect(@booking.list_reservations_by_date(180904)).must_include @reservation + expect(@booking.list_reservations_by_date(180904)).wont_include bad_reservation end + + it "returns msg if more than one reservation created" do + bad_reservation = @booking.make_reservation(check_in_date: 180909, check_out_date: 1809010) + expect(@booking.reservations.length).must_equal 2 + end + + # it "does not create reservation if dates are booked" do + # bad_reservation = @booking.make_reservation(check_in_date: 180904, check_out_date: 180905) + # + # expect{(@booking)}.must_raise 0 + # end end From ebafef335550f2b5fbd3029970e4c3a87a405110 Mon Sep 17 00:00:00 2001 From: Karis Kim Date: Sat, 8 Sep 2018 16:59:30 -0700 Subject: [PATCH 45/70] raised error when unavailable date is asked to be booked --- lib/booking_system.rb | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/lib/booking_system.rb b/lib/booking_system.rb index 9d4bfe794..a5496478e 100644 --- a/lib/booking_system.rb +++ b/lib/booking_system.rb @@ -13,32 +13,33 @@ def initialize def make_reservation(check_in_date:, check_out_date:) # TODO find a room that's available - - room_number = get_available_room(check_in_date: check_in_date, check_out_date: check_out_date) reservation = Reservation.new(check_in_date: check_in_date, check_out_date: check_out_date, room_number: room_number) + + if @reservations.empty? @reservations << reservation - # binding.pry return reservation elsif @reservations.length >= 1 requested_dates = reservation.date_range + count = requested_dates.length - 1 @reservations.each do |booking| - reserved_dates = booking.date_range - - reserved_dates.zip(requested_dates).each do |date1, date2| - unless date1 != date2 + booked_dates = booking.date_range + while count >= 0 + if booked_dates.include?(requested_dates[count]) raise ArgumentError, "unavailable date" - end end + + count -= 1 end - @reservations << reservation - # binding.pry - return @reservations + end + + @reservations << reservation + return @reservations end end From 1fb771486ace88119d7a00e4103571ab31849cf1 Mon Sep 17 00:00:00 2001 From: Karis Kim Date: Sat, 8 Sep 2018 18:19:17 -0700 Subject: [PATCH 46/70] changed code to make list_reservations_by_date method to show all bookings that includes that specific date in their date range --- lib/booking_system.rb | 118 +++++++++++++++++++++++++++++++----------- 1 file changed, 87 insertions(+), 31 deletions(-) diff --git a/lib/booking_system.rb b/lib/booking_system.rb index a5496478e..037f7a244 100644 --- a/lib/booking_system.rb +++ b/lib/booking_system.rb @@ -19,28 +19,33 @@ def make_reservation(check_in_date:, check_out_date:) - if @reservations.empty? + # if @reservations.empty? + # @reservations << reservation + # return reservation + # elsif @reservations.length >= 1 + # requested_dates = reservation.date_range + # count = requested_dates.length - 1 + # + # @reservations.each do |booking| + # booked_dates = booking.date_range + # while count >= 0 + # if booked_dates.include?(requested_dates[count]) + # if booking.room_number == reservation.room_number + # + # unless reservation.room_number == 20 + # reservation.room_number += 1 + # end + # end + # end + # + # count -= 1 + # end + # + # end + # @reservations << reservation return reservation - elsif @reservations.length >= 1 - requested_dates = reservation.date_range - count = requested_dates.length - 1 - - @reservations.each do |booking| - booked_dates = booking.date_range - while count >= 0 - if booked_dates.include?(requested_dates[count]) - raise ArgumentError, "unavailable date" - end - - count -= 1 - end - - end - - @reservations << reservation - return @reservations - end + # end end def get_available_room(check_in_date:, check_out_date:) @@ -52,24 +57,75 @@ def list_all_rooms end def list_reservations_by_date(date) - bookings_by_date = [] - specific_date = Date.parse("#{date}") + + bookings_by_date = [] @reservations.each do |booking| - booking.date_range.each do |date| - if specific_date == date - bookings_by_date << booking - end + dates = booking.date_range + if dates.include?(specific_date) + bookings_by_date << booking end end return bookings_by_date end - def available_rooms_for_date_range(check_in_date:, check_out_date:) - - - @reservations - end + # def available_rooms_for_date_range(check_in_date:, check_out_date:) + # + # + # @reservations + # end end end + +# +# if @reservations.empty? +# @reservations << reservation +# return reservation +# elsif @reservations.length >= 1 +# requested_dates = reservation.date_range +# count = requested_dates.length - 1 +# +# @reservations.each do |booking| +# booked_dates = booking.date_range +# while count >= 0 +# if booked_dates.include?(requested_dates[count]) +# if booking.room_number == reservation.room_number +# +# unless reservation.room_number == 20 +# reservation.room_number += 1 +# end +# end +# end +# +# count -= 1 +# end +# +# end +# +# @reservations << reservation +# return reservation +# end +# end + + + + + + + + +# def list_reservations_by_date(date) +# specific_date = Date.parse("#{date}") +# +# bookings_by_date = [] +# @reservations.each do |booking| +# dates = booking.date_range +# if dates.include?(specific_date) +# bookings_by_date << booking +# end +# # binding.pry +# end +# +# return bookings_by_date +# end From 9fb37dd825b047b31a3c5eea6fe30d6c18b240ae Mon Sep 17 00:00:00 2001 From: Karis Kim Date: Sat, 8 Sep 2018 18:19:42 -0700 Subject: [PATCH 47/70] added more test for list_reservations_by_date method and passed all --- spec/booking_system_spec.rb | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/spec/booking_system_spec.rb b/spec/booking_system_spec.rb index b83ad0a9f..d6e01e531 100644 --- a/spec/booking_system_spec.rb +++ b/spec/booking_system_spec.rb @@ -28,21 +28,26 @@ expect(@reservation.calculate_booking_cost).must_equal 600 end - it "list all bookings of that date" do - bad_reservation = @booking.make_reservation(check_in_date: 180909, check_out_date: 1809010) + it "list all bookings of that includes that date" do + bad_reservation = @booking.make_reservation(check_in_date: 180909, check_out_date: 180910) + new_reservation = @booking.make_reservation(check_in_date: 180904, check_out_date: 180906) + fourth_reservation = @booking.make_reservation(check_in_date: 180903, check_out_date: 180906) + expect(@booking.reservations.length).must_equal 4 expect(@booking.list_reservations_by_date(180904)).must_include @reservation expect(@booking.list_reservations_by_date(180904)).wont_include bad_reservation + expect(@booking.list_reservations_by_date(180904).length).must_equal 3 end - it "returns msg if more than one reservation created" do - bad_reservation = @booking.make_reservation(check_in_date: 180909, check_out_date: 1809010) - expect(@booking.reservations.length).must_equal 2 + it "accepts reservations that aren't overlapping in dates with other bookings" do + new_reservation = @booking.make_reservation(check_in_date: 180909, check_out_date: 1809010) + another_booking = @booking.make_reservation(check_in_date: 180909, check_out_date: 180910) + + expect(@booking.reservations.length).must_equal 3 end # it "does not create reservation if dates are booked" do - # bad_reservation = @booking.make_reservation(check_in_date: 180904, check_out_date: 180905) # - # expect{(@booking)}.must_raise 0 + # expect{ @booking.make_reservation(check_in_date: 180904, check_out_date: 180906) }.must_raise ArgumentError # end end From cef78da157dad02f16a1f1b853a18e9003d92836 Mon Sep 17 00:00:00 2001 From: Karis Kim Date: Sat, 8 Sep 2018 18:29:42 -0700 Subject: [PATCH 48/70] added test to check if booking is created when the checkin date is the same as another bookings checkout date --- spec/booking_system_spec.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/spec/booking_system_spec.rb b/spec/booking_system_spec.rb index d6e01e531..92d888657 100644 --- a/spec/booking_system_spec.rb +++ b/spec/booking_system_spec.rb @@ -28,6 +28,11 @@ expect(@reservation.calculate_booking_cost).must_equal 600 end + it "books a reservation that starts at the checkout date of another reservation" do + new_reservation = @booking.make_reservation(check_in_date: 180907, check_out_date: 180908) + expect(@booking.reservations.length).must_equal 2 + end + it "list all bookings of that includes that date" do bad_reservation = @booking.make_reservation(check_in_date: 180909, check_out_date: 180910) new_reservation = @booking.make_reservation(check_in_date: 180904, check_out_date: 180906) From 7ce95727c4a3eae4208d4136bfeadf0950fbd04f Mon Sep 17 00:00:00 2001 From: Karis Kim Date: Mon, 10 Sep 2018 04:13:03 -0700 Subject: [PATCH 49/70] not using rooms.rb so deleted it --- lib/booking_system.rb | 208 +++++++++++++++++++++--------------------- lib/reservation.rb | 33 ------- lib/rooms.rb | 17 ---- 3 files changed, 105 insertions(+), 153 deletions(-) delete mode 100644 lib/reservation.rb delete mode 100644 lib/rooms.rb diff --git a/lib/booking_system.rb b/lib/booking_system.rb index 037f7a244..a4dcc18c2 100644 --- a/lib/booking_system.rb +++ b/lib/booking_system.rb @@ -1,66 +1,122 @@ -require_relative 'reservation' +require_relative 'reservation_creator' require 'pry' module Hotel - class BookingSystem - attr_reader :rooms, :reservations + class BookingSystem < ReservationCreator + attr_reader :num_of_rooms, :reservations, :list_of_reservations def initialize - @reservations = [] - @rooms = [*1..20] + @reservations = {} + @num_of_rooms = 20 + @list_of_reservations = [] end - def make_reservation(check_in_date:, check_out_date:) - # TODO find a room that's available - - room_number = get_available_room(check_in_date: check_in_date, check_out_date: check_out_date) - - reservation = Reservation.new(check_in_date: check_in_date, check_out_date: check_out_date, room_number: room_number) - - - - # if @reservations.empty? - # @reservations << reservation - # return reservation - # elsif @reservations.length >= 1 - # requested_dates = reservation.date_range - # count = requested_dates.length - 1 - # - # @reservations.each do |booking| - # booked_dates = booking.date_range - # while count >= 0 - # if booked_dates.include?(requested_dates[count]) - # if booking.room_number == reservation.room_number - # - # unless reservation.room_number == 20 - # reservation.room_number += 1 - # end - # end - # end - # - # count -= 1 - # end - # - # end - # - @reservations << reservation - return reservation - # end + def any_rooms(date) + if @reservations[date] == nil + @reservations[date] = {} + return true + elsif @reservations[date].keys.length < @num_of_rooms + return true + else + return false + end end - def get_available_room(check_in_date:, check_out_date:) - return @rooms.first + def reserve_room(date, room_number) + if @reservations[date] == nil + @reservations[date] = {} + end + if @reservations[date][room_number] == true + return false + end + + @reservations[date][room_number] = true + # ouput = {180904=>{0=>true} + + return true end - def list_all_rooms - return @rooms + def reserve_room_for_date_range(check_in_date:, check_out_date:) + space = can_make_reservation(check_in_date: check_in_date, check_out_date: check_out_date) + if space == true + + available_room = show_available_rooms_in_range(check_in_date: check_in_date, check_out_date: check_out_date).first + date = check_in_date + + while date != check_out_date + reserved = reserve_room(date, available_room) + date += 1 + end + else + raise ArgumentError, "there are no more rooms" + end + + reservation = ReservationCreator.new(check_in_date: check_in_date, check_out_date: check_out_date, room_number: available_room) + @list_of_reservations << reservation + end + + def automatically_reserve_and_return_room_number(date) + room_number = 1 + room_reserved = reserve_room(date, room_number) + if room_reserved == true + return room_number + end + + while room_reserved == false + room_number += 1 + if room_number > @num_of_rooms + raise ArgumentError, "unavailable room" + end + + room_reserved = reserve_room(date, room_number) + end + + return room_number + end + + def can_make_reservation(check_in_date:, check_out_date:) + + date = check_in_date + + while date != check_out_date + if any_rooms(date) == false + return false + else + date += 1 + end + end + + + return true + end + + def show_reserved_rooms(date) + if @reservations[date] == nil + return [] + end + + return @reservations[date].keys + end + + def show_available_rooms_in_range(check_in_date:, check_out_date:) + rooms = [*1..@num_of_rooms] + # rooms = rooms + group2 + date = check_in_date + + while date != check_out_date + rooms += show_reserved_rooms(date) + date += 1 + # binding.pry + end + + return rooms.find_all {|room| rooms.count(room) == 1} end def list_reservations_by_date(date) specific_date = Date.parse("#{date}") bookings_by_date = [] - @reservations.each do |booking| + @list_of_reservations.each do |booking| dates = booking.date_range if dates.include?(specific_date) bookings_by_date << booking @@ -70,62 +126,8 @@ def list_reservations_by_date(date) return bookings_by_date end - # def available_rooms_for_date_range(check_in_date:, check_out_date:) - # - # - # @reservations - # end + def list_all_rooms + return [*1..@num_of_rooms] + end end end - -# -# if @reservations.empty? -# @reservations << reservation -# return reservation -# elsif @reservations.length >= 1 -# requested_dates = reservation.date_range -# count = requested_dates.length - 1 -# -# @reservations.each do |booking| -# booked_dates = booking.date_range -# while count >= 0 -# if booked_dates.include?(requested_dates[count]) -# if booking.room_number == reservation.room_number -# -# unless reservation.room_number == 20 -# reservation.room_number += 1 -# end -# end -# end -# -# count -= 1 -# end -# -# end -# -# @reservations << reservation -# return reservation -# end -# end - - - - - - - - -# def list_reservations_by_date(date) -# specific_date = Date.parse("#{date}") -# -# bookings_by_date = [] -# @reservations.each do |booking| -# dates = booking.date_range -# if dates.include?(specific_date) -# bookings_by_date << booking -# end -# # binding.pry -# end -# -# return bookings_by_date -# end diff --git a/lib/reservation.rb b/lib/reservation.rb deleted file mode 100644 index 840359e1b..000000000 --- a/lib/reservation.rb +++ /dev/null @@ -1,33 +0,0 @@ -require 'date' - -module Hotel - class Reservation - attr_reader :check_in_date, :check_out_date - - def initialize(input) - @check_in_date = input[:check_in_date] - - @check_out_date = input[:check_out_date] - raise StandardError, "Checkout date cannot be before checkin date" unless check_in_date < check_out_date - @room_number = input[:room_number] - end - - def checkin_num_to_date - return Date.parse("#{check_in_date}") - end - - def checkout_num_to_date - return Date.parse("#{check_out_date}") - end - - def date_range - date_range = [*checkin_num_to_date...checkout_num_to_date] - return date_range - end - - def calculate_booking_cost - cost = date_range.length * 200 - return cost - end - end -end diff --git a/lib/rooms.rb b/lib/rooms.rb deleted file mode 100644 index 8bf9b604b..000000000 --- a/lib/rooms.rb +++ /dev/null @@ -1,17 +0,0 @@ -module Hotel - class Rooms - def total_rooms_in_hotel - return total_num = 20 - end - - def all_room_numbers - return room_numbers = [*1..total_rooms_in_hotel] - end - - def list_rooms - rooms = [*"Room #{all_room_numbers.min}".."Room #{all_room_numbers.max}"] - - return rooms - end - end -end From 42d4c5384a14f2c2e1e9052d1b0418f74f884463 Mon Sep 17 00:00:00 2001 From: Karis Kim Date: Mon, 10 Sep 2018 04:13:25 -0700 Subject: [PATCH 50/70] added tests to reflect changes in booking_systems.rb --- spec/booking_system_spec.rb | 138 ++++++++++++++++++++++++++++-------- 1 file changed, 109 insertions(+), 29 deletions(-) diff --git a/spec/booking_system_spec.rb b/spec/booking_system_spec.rb index 92d888657..ea48e04d3 100644 --- a/spec/booking_system_spec.rb +++ b/spec/booking_system_spec.rb @@ -3,7 +3,6 @@ describe "BookingSystem class" do before do @booking = Hotel::BookingSystem.new - @reservation = @booking.make_reservation(check_in_date: 180904, check_out_date: 180907) end it "create BookingSystem class" do @@ -11,48 +10,129 @@ end it "initialize method" do - expect(@booking.reservations).must_be_kind_of Array + expect(@booking.reservations).must_be_kind_of Hash end - it "returns array of all room numbers in hotel" do - expect(@booking.list_all_rooms).must_be_kind_of Array - expect(@booking.list_all_rooms).must_equal [*1..20] + it "any rooms should return true when no reservations" do + expect(@booking.any_rooms(180904)).must_equal true end - it "returns array of dates" do - expect(@reservation).must_be_kind_of Hotel::Reservation - expect(@reservation.date_range).must_equal [Date.parse("180904"), Date.parse("180905"), Date.parse("180906")] + it "any rooms should return true when only 1 reservation" do + @booking.reserve_room(180904, 1) + expect(@booking.any_rooms(180904)).must_equal true end - it "returns the total cost of the booking" do - expect(@reservation.calculate_booking_cost).must_equal 600 + it "returns false if room is taken for that date" do + numbers = [*1..20] + + numbers.each do |num| + @booking.reserve_room(180904, num) + num += 1 + end + + expect(@booking.any_rooms(180904)).must_equal false end - it "books a reservation that starts at the checkout date of another reservation" do - new_reservation = @booking.make_reservation(check_in_date: 180907, check_out_date: 180908) - expect(@booking.reservations.length).must_equal 2 + it "returns true when no reservation" do + + check_in_date = 180904 + check_out_date = 180905 + result = @booking.can_make_reservation(check_in_date: check_in_date, check_out_date: check_out_date) + expect(result).must_equal true end - it "list all bookings of that includes that date" do - bad_reservation = @booking.make_reservation(check_in_date: 180909, check_out_date: 180910) - new_reservation = @booking.make_reservation(check_in_date: 180904, check_out_date: 180906) - fourth_reservation = @booking.make_reservation(check_in_date: 180903, check_out_date: 180906) + it "returns first room number when no other reservations" do + result = @booking.automatically_reserve_and_return_room_number(180904) - expect(@booking.reservations.length).must_equal 4 - expect(@booking.list_reservations_by_date(180904)).must_include @reservation - expect(@booking.list_reservations_by_date(180904)).wont_include bad_reservation - expect(@booking.list_reservations_by_date(180904).length).must_equal 3 + expect(result).must_equal 1 end - it "accepts reservations that aren't overlapping in dates with other bookings" do - new_reservation = @booking.make_reservation(check_in_date: 180909, check_out_date: 1809010) - another_booking = @booking.make_reservation(check_in_date: 180909, check_out_date: 180910) + it "returns next available room number" do + @booking.reserve_room(180904, 1) + result = @booking.automatically_reserve_and_return_room_number(180904) + + expect(result).must_equal 2 + end + + it "raises argument error when all rooms are booked" do + numbers = [*1..20] + + numbers.each do |num| + @booking.reserve_room(180904, num) + num += 1 + end - expect(@booking.reservations.length).must_equal 3 + expect{(@booking.automatically_reserve_and_return_room_number(180904))}.must_raise ArgumentError end - # it "does not create reservation if dates are booked" do - # - # expect{ @booking.make_reservation(check_in_date: 180904, check_out_date: 180906) }.must_raise ArgumentError - # end + it "return empty array when no rooms are reserved" do + expect(@booking.show_reserved_rooms(180904)).must_equal [] + end + + it "return an array with one room when one room is reserved" do + @booking.reserve_room(180904, 1) + + expect(@booking.show_reserved_rooms(180904).length).must_equal 1 + expect(@booking.show_reserved_rooms(180904)).must_equal [1] + end + + it "return an array of rooms that are available on all days" do + @booking.reserve_room(180904, 1) + # @booking.reserve_room(180905, 1) + + result = @booking.show_available_rooms_in_range(check_in_date: 180904, check_out_date: 180905) + expect(result).must_equal [*2..20] + end + + it "return an empty array if no rooms available on all days" do + room_number = [*1..20].each do |num| + @booking.reserve_room(180904, num) + num += 1 + end + + + result = @booking.show_available_rooms_in_range(check_in_date: 180904, check_out_date: 180905) + expect(result).must_equal [] + end + + it "return array of available rooms when we reserve it in the date range" do + @booking.reserve_room_for_date_range(check_in_date: 180904, check_out_date: 180905) + + result = @booking.show_available_rooms_in_range(check_in_date: 180904, check_out_date: 180905) + expect(result.length).must_equal 19 + expect(result).must_equal [*2..20] + end + + it "return empty array when we reserve it in the date range" do + 20.times do + @booking.reserve_room_for_date_range(check_in_date: 180904, check_out_date: 180905) + end + + result = @booking.show_available_rooms_in_range(check_in_date: 180904, check_out_date: 180905) + expect(result).must_equal [] + end + + it "raises argument error when try to overbook" do + 20.times do + @booking.reserve_room_for_date_range(check_in_date: 180904, check_out_date: 180905) + end + + + expect{(@booking.reserve_room_for_date_range(check_in_date: 180904, check_out_date: 180905))}.must_raise ArgumentError + end + + it "returns array of all room numbers in hotel" do + expect(@booking.list_all_rooms).must_be_kind_of Array + expect(@booking.list_all_rooms).must_equal [*1..20] + end + + it "returns booked rooms of that specific date" do + @booking.reserve_room_for_date_range(check_in_date: 180904, check_out_date: 180906) + @booking.reserve_room_for_date_range(check_in_date: 180904, check_out_date: 180906) + @booking.reserve_room_for_date_range(check_in_date: 180904, check_out_date: 180906) + + expect(@booking.list_reservations_by_date(180904)).must_be_kind_of Array + expect(@booking.list_reservations_by_date(180904)[2]).must_be_kind_of Hotel::ReservationCreator + expect(@booking.list_reservations_by_date(180904).length).must_equal 3 + end end From 6d99938a2be0f8b8335e78c99e5ee4c75102231f Mon Sep 17 00:00:00 2001 From: Karis Kim Date: Mon, 10 Sep 2018 04:13:50 -0700 Subject: [PATCH 51/70] added tests to reflect changes in reservation.rb --- spec/reservation_spec.rb | 26 -------------------------- 1 file changed, 26 deletions(-) delete mode 100644 spec/reservation_spec.rb diff --git a/spec/reservation_spec.rb b/spec/reservation_spec.rb deleted file mode 100644 index 26ab7594c..000000000 --- a/spec/reservation_spec.rb +++ /dev/null @@ -1,26 +0,0 @@ -require_relative 'spec_helper' - -describe "Reservation class" do - before do - @reservation = Hotel::Reservation.new({check_in_date: 180904, check_out_date: 180905}) - end - - it "initialize method" do - expect(@reservation).must_be_kind_of Hotel::Reservation - expect(@reservation.check_in_date).must_be_kind_of Integer - expect(@reservation.check_out_date).must_be_kind_of Integer - end - - it "raises ArgumentError if check out date is before check in date" do - expect{(list = Hotel::Reservation.new(180904, 180903))}.must_raise StandardError - end - - it "returns array of all dates user wants to book" do - expect(@reservation.date_range).must_be_kind_of Array - - end - - it "returns the array of dates except the check out date" do - expect(@reservation.date_range).must_equal [Date.parse("180904")] -end -end From de76799b0415873d07ee5ee3a651105d535b6dd9 Mon Sep 17 00:00:00 2001 From: Karis Kim Date: Mon, 10 Sep 2018 04:14:10 -0700 Subject: [PATCH 52/70] deleted because no longer needed --- spec/rooms_spec.rb | 11 ----------- 1 file changed, 11 deletions(-) delete mode 100644 spec/rooms_spec.rb diff --git a/spec/rooms_spec.rb b/spec/rooms_spec.rb deleted file mode 100644 index bac31b9bd..000000000 --- a/spec/rooms_spec.rb +++ /dev/null @@ -1,11 +0,0 @@ -# require_relative 'spec_helper' -# -# describe "Rooms class" do -# it "list rooms method" do -# expect(Hotel::Rooms.total_rooms_in_hotel).must_equal 20 -# end - - # it "raises ArgumentError if check out date is before check in date" do - # expect{(list = Hotel::Reservation.new(180904, 180903))}.must_raise StandardError - # end -# end From 6f851e331653831545a24eb4502e3da78a364fd4 Mon Sep 17 00:00:00 2001 From: Karis Kim Date: Mon, 10 Sep 2018 04:14:24 -0700 Subject: [PATCH 53/70] changed file names --- spec/spec_helper.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index a2e2a537b..393f3aa85 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -10,6 +10,6 @@ Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new # Require_relative your lib files here! -require_relative '../lib/reservation' +require_relative '../lib/reservation_creator' require_relative '../lib/rooms' require_relative '../lib/booking_system' From 14c15b1cfc616e5c69eceb41b44295f0eaa183e1 Mon Sep 17 00:00:00 2001 From: Karis Kim Date: Mon, 10 Sep 2018 04:15:41 -0700 Subject: [PATCH 54/70] changed file name, worked with Ada tutor on both booking_system and reservation due to difficulties with this project --- lib/reservation_creator.rb | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 lib/reservation_creator.rb diff --git a/lib/reservation_creator.rb b/lib/reservation_creator.rb new file mode 100644 index 000000000..f954a11af --- /dev/null +++ b/lib/reservation_creator.rb @@ -0,0 +1,26 @@ +require 'date' + +module Hotel + class ReservationCreator + attr_reader :check_in_date, :check_out_date + attr_accessor :room_number + + def initialize(input) + @check_in_date = Date.parse("#{input[:check_in_date]}") + + @check_out_date = Date.parse("#{input[:check_out_date]}") + raise StandardError, "Checkout date cannot be before checkin date" unless input[:check_in_date] < input[:check_out_date] + @room_number = input[:room_number].nil? ? [] : input[:room_number] + end + + def date_range + date_range = [*check_in_date...check_out_date] + return date_range + end + + def calculate_booking_cost + cost = date_range.length * 200 + return cost + end + end +end From 273eddc462eeade07d57e71b88afdd755d0ebb5f Mon Sep 17 00:00:00 2001 From: Karis Kim Date: Mon, 10 Sep 2018 04:16:05 -0700 Subject: [PATCH 55/70] added tests to reflect changes in reservation_creator.rb --- spec/reservation_creator_spec.rb | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 spec/reservation_creator_spec.rb diff --git a/spec/reservation_creator_spec.rb b/spec/reservation_creator_spec.rb new file mode 100644 index 000000000..b66fee004 --- /dev/null +++ b/spec/reservation_creator_spec.rb @@ -0,0 +1,28 @@ +require_relative 'spec_helper' + +describe "ReservationCreator class" do + before do + @reservation = Hotel::ReservationCreator.new({check_in_date: 180904, check_out_date: 180905}) + end + + it "initialize method" do + expect(@reservation).must_be_kind_of Hotel::ReservationCreator + expect(@reservation.check_in_date).must_be_kind_of Date + expect(@reservation.check_out_date).must_be_kind_of Date + end + + it "raises ArgumentError if check out date is before check in date" do + expect{(list = Hotel::ReservationCreator.new(180904, 180903))}.must_raise StandardError + end + + it "returns all dates in date range" do + expect(@reservation.date_range).must_be_kind_of Array + expect(@reservation.date_range.length).must_equal 1 + expect(@reservation.date_range).must_equal [Date.parse("180904")] + end + + it "returns cost of that date range" do + expect(@reservation.calculate_booking_cost).must_be_kind_of Integer + expect(@reservation.calculate_booking_cost).must_equal 200 + end +end From a79583be88e098a9406751ab09fb8106b9ca8619 Mon Sep 17 00:00:00 2001 From: Karis Kim Date: Wed, 12 Sep 2018 23:04:32 -0700 Subject: [PATCH 56/70] submitting original --- lib/booking_system.rb | 136 +++++++++++++----------------------------- 1 file changed, 40 insertions(+), 96 deletions(-) diff --git a/lib/booking_system.rb b/lib/booking_system.rb index a4dcc18c2..38910a669 100644 --- a/lib/booking_system.rb +++ b/lib/booking_system.rb @@ -2,121 +2,69 @@ require 'pry' module Hotel - class BookingSystem < ReservationCreator - attr_reader :num_of_rooms, :reservations, :list_of_reservations + class BookingSystem + attr_reader :rooms, :reservations, :dates def initialize - @reservations = {} - @num_of_rooms = 20 - @list_of_reservations = [] + @reservations = [] + @rooms = [*1..20] + @dates = [] end - def any_rooms(date) - if @reservations[date] == nil - @reservations[date] = {} - return true - elsif @reservations[date].keys.length < @num_of_rooms - return true - else - return false - end - end + def make_reservation(check_in_date:, check_out_date:) + # TODO find a room that's available - def reserve_room(date, room_number) - if @reservations[date] == nil - @reservations[date] = {} - end - if @reservations[date][room_number] == true - return false - end + room_number = get_available_room(check_in_date: check_in_date, check_out_date: check_out_date) - @reservations[date][room_number] = true - # ouput = {180904=>{0=>true} + reservation = ReservationCreator.new(check_in_date: check_in_date, check_out_date: check_out_date, room_number: room_number) - return true - end - def reserve_room_for_date_range(check_in_date:, check_out_date:) - space = can_make_reservation(check_in_date: check_in_date, check_out_date: check_out_date) - if space == true - available_room = show_available_rooms_in_range(check_in_date: check_in_date, check_out_date: check_out_date).first - date = check_in_date + # @dates.each do |date_with_room| + # date + # if reservation.date_range.include?(date) && reservation.room_number == room + # + # reservation.room_number += 1 + # end + # binding.pry + # end + # end - while date != check_out_date - reserved = reserve_room(date, available_room) - date += 1 - end - else - raise ArgumentError, "there are no more rooms" - end - reservation = ReservationCreator.new(check_in_date: check_in_date, check_out_date: check_out_date, room_number: available_room) - @list_of_reservations << reservation - end + @reservations << reservation - def automatically_reserve_and_return_room_number(date) - room_number = 1 - room_reserved = reserve_room(date, room_number) - if room_reserved == true - return room_number - end - - while room_reserved == false - room_number += 1 - if room_number > @num_of_rooms - raise ArgumentError, "unavailable room" - end - - room_reserved = reserve_room(date, room_number) - end - - return room_number + return reservation end - def can_make_reservation(check_in_date:, check_out_date:) - - date = check_in_date - - while date != check_out_date - if any_rooms(date) == false - return false - else - date += 1 - end - end - - - return true + def get_available_room(check_in_date:, check_out_date:) + return @rooms.first end - def show_reserved_rooms(date) - if @reservations[date] == nil - return [] - end - - return @reservations[date].keys + def list_all_rooms + return @rooms end - def show_available_rooms_in_range(check_in_date:, check_out_date:) - rooms = [*1..@num_of_rooms] - # rooms = rooms + group2 - date = check_in_date - - while date != check_out_date - rooms += show_reserved_rooms(date) - date += 1 - # binding.pry - end - - return rooms.find_all {|room| rooms.count(room) == 1} - end + # def add_dates_with_rooms + # + # @reservations.each do |booking| + # + # booking.date_range.each do |date| + # date_with_room = {} + # # binding.pry + # + # date_with_room[date] = booking.room_number + # @dates << date_with_room + # end + # end + # + # return @dates + # end def list_reservations_by_date(date) specific_date = Date.parse("#{date}") bookings_by_date = [] - @list_of_reservations.each do |booking| + @reservations.each do |booking| dates = booking.date_range if dates.include?(specific_date) bookings_by_date << booking @@ -125,9 +73,5 @@ def list_reservations_by_date(date) return bookings_by_date end - - def list_all_rooms - return [*1..@num_of_rooms] - end end end From c2f08f78f3673b640c420e3777f904dd5b977f6e Mon Sep 17 00:00:00 2001 From: Karis Kim Date: Wed, 12 Sep 2018 23:05:15 -0700 Subject: [PATCH 57/70] submitting original --- spec/booking_system_spec.rb | 134 ++++++------------------------------ 1 file changed, 20 insertions(+), 114 deletions(-) diff --git a/spec/booking_system_spec.rb b/spec/booking_system_spec.rb index ea48e04d3..0edcc8306 100644 --- a/spec/booking_system_spec.rb +++ b/spec/booking_system_spec.rb @@ -3,6 +3,7 @@ describe "BookingSystem class" do before do @booking = Hotel::BookingSystem.new + @reservation = @booking.make_reservation(check_in_date: 180904, check_out_date: 180907) end it "create BookingSystem class" do @@ -10,129 +11,34 @@ end it "initialize method" do - expect(@booking.reservations).must_be_kind_of Hash + expect(@booking.reservations).must_be_kind_of Array end - it "any rooms should return true when no reservations" do - expect(@booking.any_rooms(180904)).must_equal true - end - - it "any rooms should return true when only 1 reservation" do - @booking.reserve_room(180904, 1) - expect(@booking.any_rooms(180904)).must_equal true - end - - it "returns false if room is taken for that date" do - numbers = [*1..20] - - numbers.each do |num| - @booking.reserve_room(180904, num) - num += 1 - end - - expect(@booking.any_rooms(180904)).must_equal false - end - - it "returns true when no reservation" do - - check_in_date = 180904 - check_out_date = 180905 - result = @booking.can_make_reservation(check_in_date: check_in_date, check_out_date: check_out_date) - expect(result).must_equal true - end - - it "returns first room number when no other reservations" do - result = @booking.automatically_reserve_and_return_room_number(180904) - - expect(result).must_equal 1 - end - - it "returns next available room number" do - @booking.reserve_room(180904, 1) - result = @booking.automatically_reserve_and_return_room_number(180904) - - expect(result).must_equal 2 - end - - it "raises argument error when all rooms are booked" do - numbers = [*1..20] - - numbers.each do |num| - @booking.reserve_room(180904, num) - num += 1 - end - - expect{(@booking.automatically_reserve_and_return_room_number(180904))}.must_raise ArgumentError - end - - it "return empty array when no rooms are reserved" do - expect(@booking.show_reserved_rooms(180904)).must_equal [] - end - - it "return an array with one room when one room is reserved" do - @booking.reserve_room(180904, 1) - - expect(@booking.show_reserved_rooms(180904).length).must_equal 1 - expect(@booking.show_reserved_rooms(180904)).must_equal [1] - end - - it "return an array of rooms that are available on all days" do - @booking.reserve_room(180904, 1) - # @booking.reserve_room(180905, 1) - - result = @booking.show_available_rooms_in_range(check_in_date: 180904, check_out_date: 180905) - expect(result).must_equal [*2..20] - end - - it "return an empty array if no rooms available on all days" do - room_number = [*1..20].each do |num| - @booking.reserve_room(180904, num) - num += 1 - end - - - result = @booking.show_available_rooms_in_range(check_in_date: 180904, check_out_date: 180905) - expect(result).must_equal [] + it "returns array of all room numbers in hotel" do + expect(@booking.list_all_rooms).must_be_kind_of Array + expect(@booking.list_all_rooms).must_equal [*1..20] end - it "return array of available rooms when we reserve it in the date range" do - @booking.reserve_room_for_date_range(check_in_date: 180904, check_out_date: 180905) - - result = @booking.show_available_rooms_in_range(check_in_date: 180904, check_out_date: 180905) - expect(result.length).must_equal 19 - expect(result).must_equal [*2..20] + it "returns array of dates" do + expect(@reservation).must_be_kind_of Hotel::ReservationCreator + expect(@reservation.date_range).must_equal [Date.parse("180904"), Date.parse("180905"), Date.parse("180906")] end - it "return empty array when we reserve it in the date range" do - 20.times do - @booking.reserve_room_for_date_range(check_in_date: 180904, check_out_date: 180905) - end - - result = @booking.show_available_rooms_in_range(check_in_date: 180904, check_out_date: 180905) - expect(result).must_equal [] + it "returns the total cost of the booking" do + expect(@reservation.calculate_booking_cost).must_equal 600 end - it "raises argument error when try to overbook" do - 20.times do - @booking.reserve_room_for_date_range(check_in_date: 180904, check_out_date: 180905) - end + it "list all bookings of that date" do + bad_reservation = @booking.make_reservation(check_in_date: 180909, check_out_date: 1809010) - - expect{(@booking.reserve_room_for_date_range(check_in_date: 180904, check_out_date: 180905))}.must_raise ArgumentError + expect(@booking.list_reservations_by_date(180904)).must_include @reservation + expect(@booking.list_reservations_by_date(180904)).wont_include bad_reservation end - it "returns array of all room numbers in hotel" do - expect(@booking.list_all_rooms).must_be_kind_of Array - expect(@booking.list_all_rooms).must_equal [*1..20] - end - - it "returns booked rooms of that specific date" do - @booking.reserve_room_for_date_range(check_in_date: 180904, check_out_date: 180906) - @booking.reserve_room_for_date_range(check_in_date: 180904, check_out_date: 180906) - @booking.reserve_room_for_date_range(check_in_date: 180904, check_out_date: 180906) - - expect(@booking.list_reservations_by_date(180904)).must_be_kind_of Array - expect(@booking.list_reservations_by_date(180904)[2]).must_be_kind_of Hotel::ReservationCreator - expect(@booking.list_reservations_by_date(180904).length).must_equal 3 - end + # it "does not assign room when room is booked for that date" do + # bad_reservation = @booking.make_reservation(check_in_date: 180904, check_out_date: 180905) + # + # expect() + # + # end end From bb156acaaab9fbe2bcff47f5d6f5c61a8bed8279 Mon Sep 17 00:00:00 2001 From: Karis Kim Date: Wed, 12 Sep 2018 23:05:38 -0700 Subject: [PATCH 58/70] removed rooms --- spec/spec_helper.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 393f3aa85..d839275a4 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -11,5 +11,4 @@ # Require_relative your lib files here! require_relative '../lib/reservation_creator' -require_relative '../lib/rooms' require_relative '../lib/booking_system' From af79d2a26d24b9c8623bd6ff46781237355e6596 Mon Sep 17 00:00:00 2001 From: Karis Kim Date: Sat, 29 Sep 2018 18:24:51 -0700 Subject: [PATCH 59/70] able to assign new room number if room already booked for that date range, test coverage 100% --- lib/booking_system.rb | 59 ++++++++++++++++--------------------- spec/booking_system_spec.rb | 37 +++++++++++++++++++---- 2 files changed, 56 insertions(+), 40 deletions(-) diff --git a/lib/booking_system.rb b/lib/booking_system.rb index 38910a669..916e01994 100644 --- a/lib/booking_system.rb +++ b/lib/booking_system.rb @@ -3,12 +3,11 @@ module Hotel class BookingSystem - attr_reader :rooms, :reservations, :dates + attr_reader :rooms, :reservations def initialize @reservations = [] @rooms = [*1..20] - @dates = [] end def make_reservation(check_in_date:, check_out_date:) @@ -18,22 +17,30 @@ def make_reservation(check_in_date:, check_out_date:) reservation = ReservationCreator.new(check_in_date: check_in_date, check_out_date: check_out_date, room_number: room_number) - - - # @dates.each do |date_with_room| - # date - # if reservation.date_range.include?(date) && reservation.room_number == room - # - # reservation.room_number += 1 - # end - # binding.pry - # end - # end - - - @reservations << reservation - - return reservation + # @reservations << reservation + + # return reservation + if @reservations.length == 0 + @reservations << reservation + return reservation + elsif @reservations.length >= 1 + dates = reservation.date_range + dates.each do |date| + overlapping_bookings = list_reservations_by_date(date) + if overlapping_bookings.length == 0 + @reservations << reservation + return reservation + elsif overlapping_bookings.length >= 1 + reservation.room_number += overlapping_bookings.length + if reservation.room_number > 20 + raise ArgumentError, "unable to reserve, rooms all booked" + end + + @reservations << reservation + return reservation + end + end + end end def get_available_room(check_in_date:, check_out_date:) @@ -44,22 +51,6 @@ def list_all_rooms return @rooms end - # def add_dates_with_rooms - # - # @reservations.each do |booking| - # - # booking.date_range.each do |date| - # date_with_room = {} - # # binding.pry - # - # date_with_room[date] = booking.room_number - # @dates << date_with_room - # end - # end - # - # return @dates - # end - def list_reservations_by_date(date) specific_date = Date.parse("#{date}") diff --git a/spec/booking_system_spec.rb b/spec/booking_system_spec.rb index 0edcc8306..eabb5f223 100644 --- a/spec/booking_system_spec.rb +++ b/spec/booking_system_spec.rb @@ -35,10 +35,35 @@ expect(@booking.list_reservations_by_date(180904)).wont_include bad_reservation end - # it "does not assign room when room is booked for that date" do - # bad_reservation = @booking.make_reservation(check_in_date: 180904, check_out_date: 180905) - # - # expect() - # - # end + it "assigns different room if room already taken for that date" do + sec_reservation = @booking.make_reservation(check_in_date: 180904, check_out_date: 180905) + third_reservation = @booking.make_reservation(check_in_date: 180904, check_out_date: 180905) + fourth_reservation = @booking.make_reservation(check_in_date: 180904, check_out_date: 180905) + + expect(fourth_reservation.room_number).must_equal 4 + expect(@booking.list_reservations_by_date(180904)).must_include fourth_reservation + end + + it "raises argument error when try to overbook" do + 19.times do + @booking.make_reservation(check_in_date: 180904, check_out_date: 180905) + end + + expect{(@booking.make_reservation(check_in_date: 180904, check_out_date: 180905))}.must_raise ArgumentError + end + + it "does not raise error when booking for 20th time on same day" do + 19.times do + @booking.make_reservation(check_in_date: 180904, check_out_date: 180905) + end + + expect(@booking.list_reservations_by_date(180904).length).must_equal 20 + end + + it "automatically assigns room number 1 if it\'s the first booking for that date range" do + new_reservation = @booking.make_reservation(check_in_date: 180910, check_out_date: 180911) + + expect(new_reservation.room_number).must_equal 1 + expect(@booking.reservations.length).must_equal 2 + end end From f748e4e9032da27703129cda2dacd09a9bc85bb4 Mon Sep 17 00:00:00 2001 From: Karis Kim Date: Sat, 29 Sep 2018 20:00:58 -0700 Subject: [PATCH 60/70] added tests to cover all edge cases Dan wrote in slack --- lib/booking_system.rb | 10 +++---- spec/booking_system_spec.rb | 53 +++++++++++++++++++++++++++++++++---- 2 files changed, 53 insertions(+), 10 deletions(-) diff --git a/lib/booking_system.rb b/lib/booking_system.rb index 916e01994..4fa079dd1 100644 --- a/lib/booking_system.rb +++ b/lib/booking_system.rb @@ -27,19 +27,19 @@ def make_reservation(check_in_date:, check_out_date:) dates = reservation.date_range dates.each do |date| overlapping_bookings = list_reservations_by_date(date) - if overlapping_bookings.length == 0 - @reservations << reservation - return reservation - elsif overlapping_bookings.length >= 1 + if overlapping_bookings.length >= 1 reservation.room_number += overlapping_bookings.length if reservation.room_number > 20 raise ArgumentError, "unable to reserve, rooms all booked" end - + @reservations << reservation return reservation end end + + @reservations << reservation + return reservation end end diff --git a/spec/booking_system_spec.rb b/spec/booking_system_spec.rb index eabb5f223..bd79c8bdb 100644 --- a/spec/booking_system_spec.rb +++ b/spec/booking_system_spec.rb @@ -6,12 +6,13 @@ @reservation = @booking.make_reservation(check_in_date: 180904, check_out_date: 180907) end - it "create BookingSystem class" do + it "creates a BookingSystem class" do expect(@booking).must_be_kind_of Hotel::BookingSystem end - it "initialize method" do + it "returns an array of all instances of reservations created" do expect(@booking.reservations).must_be_kind_of Array + expect(@booking.reservations[0]).must_be_kind_of Hotel::ReservationCreator end it "returns array of all room numbers in hotel" do @@ -28,14 +29,14 @@ expect(@reservation.calculate_booking_cost).must_equal 600 end - it "list all bookings of that date" do + it "lists all bookings of specific date" do bad_reservation = @booking.make_reservation(check_in_date: 180909, check_out_date: 1809010) expect(@booking.list_reservations_by_date(180904)).must_include @reservation expect(@booking.list_reservations_by_date(180904)).wont_include bad_reservation end - it "assigns different room if room already taken for that date" do + it "assigns different room number if room number is already taken for existing reservation" do sec_reservation = @booking.make_reservation(check_in_date: 180904, check_out_date: 180905) third_reservation = @booking.make_reservation(check_in_date: 180904, check_out_date: 180905) fourth_reservation = @booking.make_reservation(check_in_date: 180904, check_out_date: 180905) @@ -44,7 +45,7 @@ expect(@booking.list_reservations_by_date(180904)).must_include fourth_reservation end - it "raises argument error when try to overbook" do + it "raises argument error when tries to overbook" do 19.times do @booking.make_reservation(check_in_date: 180904, check_out_date: 180905) end @@ -66,4 +67,46 @@ expect(new_reservation.room_number).must_equal 1 expect(@booking.reservations.length).must_equal 2 end + + it "assigns different room number if dates overlap at the front" do + new_reservation = @booking.make_reservation(check_in_date: 180903, check_out_date: 180905) + + expect(new_reservation.room_number).must_equal 2 + expect(@booking.reservations.length).must_equal 2 + end + + it "assigns different room number if dates overlap at the end" do + new_reservation = @booking.make_reservation(check_in_date: 180906, check_out_date: 180908) + + expect(new_reservation.room_number).must_equal 2 + expect(@booking.reservations.length).must_equal 2 + end + + it "assigns different room number if dates are contained in existing reservation date_range" do + new_reservation = @booking.make_reservation(check_in_date: 180905, check_out_date: 180906) + + expect(new_reservation.room_number).must_equal 2 + expect(@booking.reservations.length).must_equal 2 + end + + it "assigns different room number if dates contain the existing reservation date_range" do + new_reservation = @booking.make_reservation(check_in_date: 180903, check_out_date: 180908) + + expect(new_reservation.room_number).must_equal 2 + expect(@booking.reservations.length).must_equal 2 + end + + it "creates reservation for checkout date of existing reservation" do + new_reservation = @booking.make_reservation(check_in_date: 180907, check_out_date: 180908) + + expect(new_reservation.room_number).must_equal 1 + expect(@booking.reservations.length).must_equal 2 + end + + it "creates new reservation with the same checkout date for the checkin date of existing reservation" do + new_reservation = @booking.make_reservation(check_in_date: 180903, check_out_date: 180904) + + expect(new_reservation.room_number).must_equal 1 + expect(@booking.reservations.length).must_equal 2 + end end From fc4e0659228986cc138ac66c0d1512e127968d4b Mon Sep 17 00:00:00 2001 From: Karis Kim Date: Sat, 29 Sep 2018 20:21:55 -0700 Subject: [PATCH 61/70] added method to show rooms available on specific date --- lib/booking_system.rb | 5 +++++ spec/booking_system_spec.rb | 7 +++++++ 2 files changed, 12 insertions(+) diff --git a/lib/booking_system.rb b/lib/booking_system.rb index 4fa079dd1..4fb016105 100644 --- a/lib/booking_system.rb +++ b/lib/booking_system.rb @@ -64,5 +64,10 @@ def list_reservations_by_date(date) return bookings_by_date end + + def show_available_rooms(date) + show = list_reservations_by_date(date).map {|reservation| reservation.room_number } + return list_all_rooms - show + end end end diff --git a/spec/booking_system_spec.rb b/spec/booking_system_spec.rb index bd79c8bdb..1b9cffb8b 100644 --- a/spec/booking_system_spec.rb +++ b/spec/booking_system_spec.rb @@ -109,4 +109,11 @@ expect(new_reservation.room_number).must_equal 1 expect(@booking.reservations.length).must_equal 2 end + + it "shows all room_numbers that were reserved" do + new_reservation = @booking.make_reservation(check_in_date: 180903, check_out_date: 180906) + + expect(@booking.show_available_rooms(180904)).must_be_kind_of Array + expect(@booking.show_available_rooms(180904)).must_equal [*3..20] + end end From ae0e4aee6120ab2de0069dec480f4e914f1244bf Mon Sep 17 00:00:00 2001 From: Karis Kim Date: Sat, 29 Sep 2018 20:27:40 -0700 Subject: [PATCH 62/70] fixed all warning signs of unused assigned variables in tests --- lib/booking_system.rb | 5 +---- spec/booking_system_spec.rb | 6 +++--- spec/reservation_creator_spec.rb | 2 +- 3 files changed, 5 insertions(+), 8 deletions(-) diff --git a/lib/booking_system.rb b/lib/booking_system.rb index 4fb016105..9e212738b 100644 --- a/lib/booking_system.rb +++ b/lib/booking_system.rb @@ -11,15 +11,12 @@ def initialize end def make_reservation(check_in_date:, check_out_date:) - # TODO find a room that's available + # TODO: find a room that's available room_number = get_available_room(check_in_date: check_in_date, check_out_date: check_out_date) reservation = ReservationCreator.new(check_in_date: check_in_date, check_out_date: check_out_date, room_number: room_number) - # @reservations << reservation - - # return reservation if @reservations.length == 0 @reservations << reservation return reservation diff --git a/spec/booking_system_spec.rb b/spec/booking_system_spec.rb index 1b9cffb8b..61b048e4d 100644 --- a/spec/booking_system_spec.rb +++ b/spec/booking_system_spec.rb @@ -37,8 +37,8 @@ end it "assigns different room number if room number is already taken for existing reservation" do - sec_reservation = @booking.make_reservation(check_in_date: 180904, check_out_date: 180905) - third_reservation = @booking.make_reservation(check_in_date: 180904, check_out_date: 180905) + @booking.make_reservation(check_in_date: 180904, check_out_date: 180905) + @booking.make_reservation(check_in_date: 180904, check_out_date: 180905) fourth_reservation = @booking.make_reservation(check_in_date: 180904, check_out_date: 180905) expect(fourth_reservation.room_number).must_equal 4 @@ -111,7 +111,7 @@ end it "shows all room_numbers that were reserved" do - new_reservation = @booking.make_reservation(check_in_date: 180903, check_out_date: 180906) + @booking.make_reservation(check_in_date: 180903, check_out_date: 180906) expect(@booking.show_available_rooms(180904)).must_be_kind_of Array expect(@booking.show_available_rooms(180904)).must_equal [*3..20] diff --git a/spec/reservation_creator_spec.rb b/spec/reservation_creator_spec.rb index b66fee004..4f5c1d1f9 100644 --- a/spec/reservation_creator_spec.rb +++ b/spec/reservation_creator_spec.rb @@ -12,7 +12,7 @@ end it "raises ArgumentError if check out date is before check in date" do - expect{(list = Hotel::ReservationCreator.new(180904, 180903))}.must_raise StandardError + expect{(Hotel::ReservationCreator.new(180904, 180903))}.must_raise StandardError end it "returns all dates in date range" do From 9bfcbcb73724a52eff420e12ce06fc51158485f6 Mon Sep 17 00:00:00 2001 From: Karis Kim Date: Sat, 29 Sep 2018 22:41:06 -0700 Subject: [PATCH 63/70] final submission for wave 2 before refactoring to DRY code --- lib/booking_system.rb | 2 +- lib/reservation_creator.rb | 4 ++-- spec/booking_system_spec.rb | 4 ++-- spec/reservation_creator_spec.rb | 8 ++++---- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/booking_system.rb b/lib/booking_system.rb index 9e212738b..6b9e1c9ca 100644 --- a/lib/booking_system.rb +++ b/lib/booking_system.rb @@ -15,7 +15,7 @@ def make_reservation(check_in_date:, check_out_date:) room_number = get_available_room(check_in_date: check_in_date, check_out_date: check_out_date) - reservation = ReservationCreator.new(check_in_date: check_in_date, check_out_date: check_out_date, room_number: room_number) + reservation = Reservation.new(check_in_date: check_in_date, check_out_date: check_out_date, room_number: room_number) if @reservations.length == 0 @reservations << reservation diff --git a/lib/reservation_creator.rb b/lib/reservation_creator.rb index f954a11af..e705cdaeb 100644 --- a/lib/reservation_creator.rb +++ b/lib/reservation_creator.rb @@ -1,7 +1,7 @@ require 'date' module Hotel - class ReservationCreator + class Reservation attr_reader :check_in_date, :check_out_date attr_accessor :room_number @@ -9,7 +9,7 @@ def initialize(input) @check_in_date = Date.parse("#{input[:check_in_date]}") @check_out_date = Date.parse("#{input[:check_out_date]}") - raise StandardError, "Checkout date cannot be before checkin date" unless input[:check_in_date] < input[:check_out_date] + raise StandardError, "Checkout date cannot be before checkin date" unless input[:check_in_date] < input[:check_out_date] @room_number = input[:room_number].nil? ? [] : input[:room_number] end diff --git a/spec/booking_system_spec.rb b/spec/booking_system_spec.rb index 61b048e4d..75bcdb2a6 100644 --- a/spec/booking_system_spec.rb +++ b/spec/booking_system_spec.rb @@ -12,7 +12,7 @@ it "returns an array of all instances of reservations created" do expect(@booking.reservations).must_be_kind_of Array - expect(@booking.reservations[0]).must_be_kind_of Hotel::ReservationCreator + expect(@booking.reservations[0]).must_be_kind_of Hotel::Reservation end it "returns array of all room numbers in hotel" do @@ -21,7 +21,7 @@ end it "returns array of dates" do - expect(@reservation).must_be_kind_of Hotel::ReservationCreator + expect(@reservation).must_be_kind_of Hotel::Reservation expect(@reservation.date_range).must_equal [Date.parse("180904"), Date.parse("180905"), Date.parse("180906")] end diff --git a/spec/reservation_creator_spec.rb b/spec/reservation_creator_spec.rb index 4f5c1d1f9..fb5701304 100644 --- a/spec/reservation_creator_spec.rb +++ b/spec/reservation_creator_spec.rb @@ -1,18 +1,18 @@ require_relative 'spec_helper' -describe "ReservationCreator class" do +describe "Reservation class" do before do - @reservation = Hotel::ReservationCreator.new({check_in_date: 180904, check_out_date: 180905}) + @reservation = Hotel::Reservation.new({check_in_date: 180904, check_out_date: 180905}) end it "initialize method" do - expect(@reservation).must_be_kind_of Hotel::ReservationCreator + expect(@reservation).must_be_kind_of Hotel::Reservation expect(@reservation.check_in_date).must_be_kind_of Date expect(@reservation.check_out_date).must_be_kind_of Date end it "raises ArgumentError if check out date is before check in date" do - expect{(Hotel::ReservationCreator.new(180904, 180903))}.must_raise StandardError + expect{(Hotel::Reservation.new(180904, 180903))}.must_raise StandardError end it "returns all dates in date range" do From 6aeea07fea55e4d8786a422a7706f6f19dbe6a7e Mon Sep 17 00:00:00 2001 From: Karis Kim Date: Sat, 29 Sep 2018 22:46:23 -0700 Subject: [PATCH 64/70] renamed reservation creator to reservation.rb --- lib/{reservation_creator.rb => reservation.rb} | 0 spec/{reservation_creator_spec.rb => reservation_spec.rb} | 0 spec/spec_helper.rb | 2 +- 3 files changed, 1 insertion(+), 1 deletion(-) rename lib/{reservation_creator.rb => reservation.rb} (100%) rename spec/{reservation_creator_spec.rb => reservation_spec.rb} (100%) diff --git a/lib/reservation_creator.rb b/lib/reservation.rb similarity index 100% rename from lib/reservation_creator.rb rename to lib/reservation.rb diff --git a/spec/reservation_creator_spec.rb b/spec/reservation_spec.rb similarity index 100% rename from spec/reservation_creator_spec.rb rename to spec/reservation_spec.rb diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index d839275a4..1242bfc9f 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -10,5 +10,5 @@ Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new # Require_relative your lib files here! -require_relative '../lib/reservation_creator' +require_relative '../lib/reservation' require_relative '../lib/booking_system' From 53d07afe0b3f616c8024119b5ccc07c0a08fae42 Mon Sep 17 00:00:00 2001 From: Karis Kim Date: Sat, 29 Sep 2018 22:48:34 -0700 Subject: [PATCH 65/70] changed reservation_creator to reservation.rb --- lib/booking_system.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/booking_system.rb b/lib/booking_system.rb index 6b9e1c9ca..a63ec17aa 100644 --- a/lib/booking_system.rb +++ b/lib/booking_system.rb @@ -1,4 +1,4 @@ -require_relative 'reservation_creator' +require_relative 'reservation' require 'pry' module Hotel From e8f1196d53afa0b6ca02ef9e3c727a20940d8075 Mon Sep 17 00:00:00 2001 From: Karis Kim Date: Sun, 30 Sep 2018 10:40:36 -0700 Subject: [PATCH 66/70] completed all questions to design-activity.md --- design-activity.md | 46 +++++++++++++++++++++++++++++++++++++ lib/reservation.rb | 2 +- spec/booking_system_spec.rb | 2 +- 3 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 design-activity.md diff --git a/design-activity.md b/design-activity.md new file mode 100644 index 000000000..4da5378aa --- /dev/null +++ b/design-activity.md @@ -0,0 +1,46 @@ +What classes does each implementation include? Are the lists the same? + Both implementation include classes named CartEntry, ShoppingCart, Order. If you mean the class name list by "list" then yes, the list is the same for both implementations. + +Write down a sentence to describe each class. + For implementation A, class CartEntry stores unit price and quantity. Class ShoppingCart stores all entries in an array. Class Order stores all entries from ShoppingCart then calculates the cost of all entries including tax. + + For implementation B, class CartEntry stores unit price and quantity while also being able to calculate cost of the merchandise with its cost and quantity. Class ShoppingCart stores all entires and can calculate the total cost for all entires. Class Order stores all entries, gets the total price from ShoppingCart then adds tax to calculate total sum. + +How do the classes relate to each other? It might be helpful to draw a diagram on a whiteboard or piece of paper. + + For implementation A, ShoppingCart is directly instantiated in Order with .new but CartEntry's instance variables are also called in Order. Order is the big class that brings all classes together. + + For implementation B, CartEntry's instance method price, price, is called in ShoppingCart inside it's instance method, price. ShoppingCart's price method is called in Order's instance method, total_price. + +What data does each class store? How (if at all) does this differ between the two implementations? + For A, CartEntry holds unit_price and quantity, ShoppingCart holds all entires, Order holds all entries and total sum. + + For B, CartEntry holds unit_price, quantity, price of one merchandise w/o tax, ShoppingCart holds all entires with price of all merchandise w/o tax, Order holds all entries and total sum with tax. + + Difference is that B holds more data and has more behavior for each of its classes compared to A. A has 2 classes that just holds state. + +What methods does each class have? How (if at all) does this differ between the two implementations? + A: Only order has an instance method but all classes do have the initialize method. Order#total_price method. + + B: CartEntry#price method, ShoppingCart#price method, Order#total_price method. + + Difference: B has more instance methods in each class so more behavior. + +Consider the Order#total_price method. In each implementation: +Is logic to compute the price delegated to "lower level" classes like ShoppingCart and CartEntry, or is it retained in Order? + A: retained in order + + B: delegated to lower level classes + +Does total_price directly manipulate the instance variables of other classes? + A: yes, because total_price directly iterates over @cart.entries which is manipulating the instance variable of ShoppingCart. + + B: no, because total_price simply calls the ShoppingCart#price method then assigns it to a local variable then adds the tax to that local variable. + +If we decide items are cheaper if bought in bulk, how would this change the code? Which implementation is easier to modify? + It would change the unit price to drop if one buys 10 folders in quantity for one order instead of 10 orders with 1 folder. So it could be an if statement of if quantity is multiples of 10 then minus price by 2 dollars. + + B is easier to modify because the if statement can go inside #price method of CartEntry class. For A, I would either need to create an if statement inside the initialize method or create it inside the iteration of the total_price which would make it have too much responsibility in one method. + +Which implementation better adheres to the single responsibility principle? + B. Because each class has both state and behavior while having one single responsibility. diff --git a/lib/reservation.rb b/lib/reservation.rb index e705cdaeb..a9dab0790 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -10,7 +10,7 @@ def initialize(input) @check_out_date = Date.parse("#{input[:check_out_date]}") raise StandardError, "Checkout date cannot be before checkin date" unless input[:check_in_date] < input[:check_out_date] - @room_number = input[:room_number].nil? ? [] : input[:room_number] + @room_number = input[:room_number].nil? ? input[:room_number] : 1 end def date_range diff --git a/spec/booking_system_spec.rb b/spec/booking_system_spec.rb index 75bcdb2a6..cbf6a70ac 100644 --- a/spec/booking_system_spec.rb +++ b/spec/booking_system_spec.rb @@ -63,7 +63,7 @@ it "automatically assigns room number 1 if it\'s the first booking for that date range" do new_reservation = @booking.make_reservation(check_in_date: 180910, check_out_date: 180911) - +# binding.pry expect(new_reservation.room_number).must_equal 1 expect(@booking.reservations.length).must_equal 2 end From 660979af9799e9fffc30d713d46470d774ac8813 Mon Sep 17 00:00:00 2001 From: Karis Kim Date: Sun, 30 Sep 2018 11:41:58 -0700 Subject: [PATCH 67/70] created and wrote things to refactor in refactors.txt --- refactors.txt | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 refactors.txt diff --git a/refactors.txt b/refactors.txt new file mode 100644 index 000000000..1526cfbdf --- /dev/null +++ b/refactors.txt @@ -0,0 +1,5 @@ +Refactoring ideas: + + BookingSystem might have too many responsibilities so might need to divide it into several classes. + #make_reservation method has too many responsibilities inside and the if statement is too clumped. Break down the method to several instance methods. + Use .map instead of .each in #list_reservations_by_date. From 59bf0960fdb5cc047872be67a146b1872e3b2923 Mon Sep 17 00:00:00 2001 From: Karis Kim Date: Sun, 30 Sep 2018 22:46:33 -0700 Subject: [PATCH 68/70] reduced DRY in #make_reservation method and used show available rooms to assign next room number --- design-activity.md | 2 ++ hotel-revisited.txt | 29 +++++++++++++++++++++++++++++ lib/booking_system.rb | 30 +++++++++--------------------- lib/reservation.rb | 4 +++- refactors.txt | 4 ++++ 5 files changed, 47 insertions(+), 22 deletions(-) create mode 100644 hotel-revisited.txt diff --git a/design-activity.md b/design-activity.md index 4da5378aa..1e67755f9 100644 --- a/design-activity.md +++ b/design-activity.md @@ -44,3 +44,5 @@ If we decide items are cheaper if bought in bulk, how would this change the code Which implementation better adheres to the single responsibility principle? B. Because each class has both state and behavior while having one single responsibility. + +HOTEL REVISITED: diff --git a/hotel-revisited.txt b/hotel-revisited.txt new file mode 100644 index 000000000..eaaf3c2b4 --- /dev/null +++ b/hotel-revisited.txt @@ -0,0 +1,29 @@ +What is this class's responsibility? You should be able to describe it in a single sentence. + Reservation: stores check in, check out date, and room number while also calculating the cost for those dates. + + BookingSystem: keeps track of all reservations, assigns room number, checks for overlaps in dates for new booking, list reservations by date and show available rooms. + +Is this class responsible for exactly one thing? + Reservation: Almost. It's perhaps it will be better if it just focuses on dates rather than also keeping track of the room_number as well as calculating the cost. + + BookingSystem: No. It's checking to see if the dates overlap while trying to confirm the client is able to make a reservation for that date range. + +Does this class take on any responsibility that should be delegated to "lower level" classes? + Reservation: Not sure if calculating the cost should go to a lower level class but it this class does need to focus on one responsibility. + + BookingSystem: Yes. Overlaps could go to a date range class. + +Is there code in other classes that directly manipulates this class's instance variables? + Reservation: + + BookingSystem: + +How easy is it to follow your own instructions? + It was easy for me to follow the instructions because I wrote them today but if I looked at it a week from now then it will be difficult to follow because it's not specific. + +Do these refactors improve the clarity of your code? + Yes, if implemented it will reduce the number of responsibilities the booking_system class has right now. + + +Do you still agree with your previous assesment, or could your refactor be further improved? + I agree with the previous assessment but there are always ways to refactor for one's code to be improved. diff --git a/lib/booking_system.rb b/lib/booking_system.rb index a63ec17aa..e6c64f587 100644 --- a/lib/booking_system.rb +++ b/lib/booking_system.rb @@ -1,5 +1,4 @@ require_relative 'reservation' -require 'pry' module Hotel class BookingSystem @@ -11,33 +10,24 @@ def initialize end def make_reservation(check_in_date:, check_out_date:) - # TODO: find a room that's available room_number = get_available_room(check_in_date: check_in_date, check_out_date: check_out_date) - reservation = Reservation.new(check_in_date: check_in_date, check_out_date: check_out_date, room_number: room_number) - if @reservations.length == 0 - @reservations << reservation - return reservation - elsif @reservations.length >= 1 - dates = reservation.date_range - dates.each do |date| - overlapping_bookings = list_reservations_by_date(date) - if overlapping_bookings.length >= 1 - reservation.room_number += overlapping_bookings.length - if reservation.room_number > 20 - raise ArgumentError, "unable to reserve, rooms all booked" - end + if @reservations.length >= 1 + reservation.date_range.each do |date| + if list_reservations_by_date(date).length >= 1 + reservation.room_number = show_available_rooms(date).first + raise ArgumentError, "unable to reserve/rooms all booked" unless reservation.room_number != nil @reservations << reservation return reservation end end - - @reservations << reservation - return reservation end + + @reservations << reservation + return reservation end def get_available_room(check_in_date:, check_out_date:) @@ -54,9 +44,7 @@ def list_reservations_by_date(date) bookings_by_date = [] @reservations.each do |booking| dates = booking.date_range - if dates.include?(specific_date) - bookings_by_date << booking - end + bookings_by_date << booking if dates.include?(specific_date) end return bookings_by_date diff --git a/lib/reservation.rb b/lib/reservation.rb index a9dab0790..126c3697b 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -5,6 +5,8 @@ class Reservation attr_reader :check_in_date, :check_out_date attr_accessor :room_number + COST = 200 + def initialize(input) @check_in_date = Date.parse("#{input[:check_in_date]}") @@ -19,7 +21,7 @@ def date_range end def calculate_booking_cost - cost = date_range.length * 200 + cost = date_range.length * COST return cost end end diff --git a/refactors.txt b/refactors.txt index 1526cfbdf..732c51eb6 100644 --- a/refactors.txt +++ b/refactors.txt @@ -1,5 +1,9 @@ Refactoring ideas: BookingSystem might have too many responsibilities so might need to divide it into several classes. + #make_reservation method has too many responsibilities inside and the if statement is too clumped. Break down the method to several instance methods. + Use .map instead of .each in #list_reservations_by_date. + + Clean up the tests and organize them so that it's easier to read. From 1f4a43fdbbc8a002beaa9c0cdfee2037fc9357b6 Mon Sep 17 00:00:00 2001 From: Karis Kim Date: Sun, 30 Sep 2018 23:39:17 -0700 Subject: [PATCH 69/70] wrote what could be improved with the design in design-activity.md --- design-activity.md | 9 +++++++++ lib/booking_system.rb | 18 +++++++++--------- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/design-activity.md b/design-activity.md index 1e67755f9..9b853ed93 100644 --- a/design-activity.md +++ b/design-activity.md @@ -46,3 +46,12 @@ Which implementation better adheres to the single responsibility principle? B. Because each class has both state and behavior while having one single responsibility. HOTEL REVISITED: + +what changes you would need to make to improve this design, and how the resulting design would be an improvement. + BookingSystem takes on the role of checking for overlapping dates which is more than what it should do. Reservation class could instead become a date range class and it could address the overlapping date ranges there. This will allow BookingSystem to have less responsibilities. + + Then I could still have a reservation class where it takes in the confirmed date ranges that can be used to make a reservation and assign a room number to that date range in that class. + + I did not finish wave 2 before when we submitted this project so I focused on completing that part. Then I focused on breaking down the methods with less responsibility for the #make_reservation method. + + The way the methods are set up makes sense for them to be in BookingSystem so I had difficulty with fleshing the responsibilities out in BookingSystem. I am still working on breaking down the booking_system class so I have not committed a repo with a newly improved design but it does have better methods with less responsibility. diff --git a/lib/booking_system.rb b/lib/booking_system.rb index e6c64f587..f75fa30e7 100644 --- a/lib/booking_system.rb +++ b/lib/booking_system.rb @@ -14,20 +14,20 @@ def make_reservation(check_in_date:, check_out_date:) room_number = get_available_room(check_in_date: check_in_date, check_out_date: check_out_date) reservation = Reservation.new(check_in_date: check_in_date, check_out_date: check_out_date, room_number: room_number) + assign_diff_room_for_overlapping_dates(reservation) + @reservations << reservation + return reservation + end + + def assign_diff_room_for_overlapping_dates(booking) if @reservations.length >= 1 - reservation.date_range.each do |date| + booking.date_range.each do |date| if list_reservations_by_date(date).length >= 1 - reservation.room_number = show_available_rooms(date).first - raise ArgumentError, "unable to reserve/rooms all booked" unless reservation.room_number != nil - - @reservations << reservation - return reservation + booking.room_number = show_available_rooms(date).first + raise ArgumentError, "unable to reserve/rooms all booked" unless booking.room_number != nil end end end - - @reservations << reservation - return reservation end def get_available_room(check_in_date:, check_out_date:) From a1a99e0b979bae2da0edc4c7dcc3d02293af883d Mon Sep 17 00:00:00 2001 From: Karis Kim Date: Mon, 1 Oct 2018 00:22:47 -0700 Subject: [PATCH 70/70] created custom error msg and tested it yay --- lib/booking_system.rb | 4 +++- spec/booking_system_spec.rb | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/booking_system.rb b/lib/booking_system.rb index f75fa30e7..a1b1ba410 100644 --- a/lib/booking_system.rb +++ b/lib/booking_system.rb @@ -2,6 +2,8 @@ module Hotel class BookingSystem + class AllBookedError < StandardError ; end + attr_reader :rooms, :reservations def initialize @@ -24,7 +26,7 @@ def assign_diff_room_for_overlapping_dates(booking) booking.date_range.each do |date| if list_reservations_by_date(date).length >= 1 booking.room_number = show_available_rooms(date).first - raise ArgumentError, "unable to reserve/rooms all booked" unless booking.room_number != nil + raise AllBookedError.new("unable to reserve/rooms all booked") unless !booking.room_number.nil? end end end diff --git a/spec/booking_system_spec.rb b/spec/booking_system_spec.rb index cbf6a70ac..92437e506 100644 --- a/spec/booking_system_spec.rb +++ b/spec/booking_system_spec.rb @@ -50,7 +50,7 @@ @booking.make_reservation(check_in_date: 180904, check_out_date: 180905) end - expect{(@booking.make_reservation(check_in_date: 180904, check_out_date: 180905))}.must_raise ArgumentError + expect{(@booking.make_reservation(check_in_date: 180904, check_out_date: 180905))}.must_raise Hotel::BookingSystem::AllBookedError end it "does not raise error when booking for 20th time on same day" do