From 553e81446dcf36f81e27a240c4b282ed8ebbd1e2 Mon Sep 17 00:00:00 2001 From: Lindsay Terchin Date: Tue, 4 Sep 2018 14:54:44 -0700 Subject: [PATCH 01/42] created room.rb and room_spec.rb with corresponding initialization tests --- lib/room.rb | 20 ++++++++++++++++++++ spec/room_spec.rb | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 lib/room.rb create mode 100644 spec/room_spec.rb diff --git a/lib/room.rb b/lib/room.rb new file mode 100644 index 000000000..0f4541e37 --- /dev/null +++ b/lib/room.rb @@ -0,0 +1,20 @@ +module Hotel + class Room + attr_reader :status + + def initialize(status = :AVAILABLE) + @status = status + validate_status + end + + + def validate_status + status_options = [:AVAILABLE, :UNAVAILABLE] + + raise ArgumentError.new("You must enter a valid status.") unless status_options.include?(status) + end + + + + end +end diff --git a/spec/room_spec.rb b/spec/room_spec.rb new file mode 100644 index 000000000..a4971e544 --- /dev/null +++ b/spec/room_spec.rb @@ -0,0 +1,45 @@ +require_relative 'spec_helper' + +describe 'Room class' do + + describe "initialize" do + + it "is an instance of Room" do + @room = Hotel::Room.new() + expect(@room).must_be_instance_of Hotel::Room + end + + + it "contains a valid status" do + valid_statuses = [:AVAILABLE, :UNAVAILABLE] + valid_statuses.each do |status| + @room = Hotel::Room.new(status) + expect(@room.status).must_equal status + end + end + + + it "sets the status to :AVAILABLE if no status is given" do + @room = Hotel::Room.new() + expect(@room.status).must_equal :AVAILABLE + end + + + it "must throw an argument error if an invalid status is given" do + expect {Hotel::Room.new(:HUH)}.must_raise ArgumentError + end + + + end + + +#must have a valid status +#must throw argument error without valid status? + + + + + + + +end From bfeac50d59e23c64f2ab0605a95a9d23f85f5cb6 Mon Sep 17 00:00:00 2001 From: Lindsay Terchin Date: Tue, 4 Sep 2018 15:18:59 -0700 Subject: [PATCH 02/42] created a new parameter for room (id) and corresponding tests --- lib/room.rb | 14 ++++++++++++-- spec/room_spec.rb | 30 ++++++++++++++++++++++-------- 2 files changed, 34 insertions(+), 10 deletions(-) diff --git a/lib/room.rb b/lib/room.rb index 0f4541e37..178ca7f4d 100644 --- a/lib/room.rb +++ b/lib/room.rb @@ -1,10 +1,14 @@ module Hotel class Room - attr_reader :status + attr_reader :status, :id - def initialize(status = :AVAILABLE) + def initialize(id, status = :AVAILABLE) + @id = id @status = status + + validate_id validate_status + end @@ -15,6 +19,12 @@ def validate_status end + def validate_id + id_options = [1..20] + + raise ArgumentError.new("you must enter a valid id.") unless id_options.include?(id) + end + end end diff --git a/spec/room_spec.rb b/spec/room_spec.rb index a4971e544..6f8e61cd4 100644 --- a/spec/room_spec.rb +++ b/spec/room_spec.rb @@ -5,7 +5,7 @@ describe "initialize" do it "is an instance of Room" do - @room = Hotel::Room.new() + @room = Hotel::Room.new(2) expect(@room).must_be_instance_of Hotel::Room end @@ -13,33 +13,47 @@ it "contains a valid status" do valid_statuses = [:AVAILABLE, :UNAVAILABLE] valid_statuses.each do |status| - @room = Hotel::Room.new(status) + @room = Hotel::Room.new(2, status) expect(@room.status).must_equal status end end it "sets the status to :AVAILABLE if no status is given" do - @room = Hotel::Room.new() + @room = Hotel::Room.new(2) expect(@room.status).must_equal :AVAILABLE end - it "must throw an argument error if an invalid status is given" do - expect {Hotel::Room.new(:HUH)}.must_raise ArgumentError + it "must throw an argument error if an invalid status is given" do + expect {Hotel::Room.new(2, :HUH)}.must_raise ArgumentError end - end + it "accepts all valid ids" do + valid_ids = [1..20] + + valid_ids.each do |id| + @room = Hotel::Room.new(id) + expect(@room.id).must_equal id + end + end + it "must throw an argument error if an invalid id is given" do + bogus_ids = [53, 0, 'cool', -5, nil, :cool] + bogus_ids.each do |id| + expect {Hotel::Room.new(id) + }.must_raise ArgumentError + end + end -#must have a valid status -#must throw argument error without valid status? + end + end From de1cc7b905aa5b85eabf147e40937428f2609bca Mon Sep 17 00:00:00 2001 From: Lindsay Terchin Date: Tue, 4 Sep 2018 15:53:37 -0700 Subject: [PATCH 03/42] fixed argument error for invalid ID & corresponding test --- lib/room.rb | 6 +++--- spec/room_spec.rb | 12 ++++++++---- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/lib/room.rb b/lib/room.rb index 178ca7f4d..3ee02ce92 100644 --- a/lib/room.rb +++ b/lib/room.rb @@ -5,7 +5,7 @@ class Room def initialize(id, status = :AVAILABLE) @id = id @status = status - + validate_id validate_status @@ -20,9 +20,9 @@ def validate_status def validate_id - id_options = [1..20] + id_options = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] - raise ArgumentError.new("you must enter a valid id.") unless id_options.include?(id) + raise ArgumentError.new("You must enter a valid id.") unless id_options.include?(id) end diff --git a/spec/room_spec.rb b/spec/room_spec.rb index 6f8e61cd4..5114fdbc3 100644 --- a/spec/room_spec.rb +++ b/spec/room_spec.rb @@ -1,4 +1,5 @@ require_relative 'spec_helper' +require 'pry' describe 'Room class' do @@ -6,7 +7,9 @@ it "is an instance of Room" do @room = Hotel::Room.new(2) - expect(@room).must_be_instance_of Hotel::Room + # binding.pry + + expect(@room).must_be_kind_of Hotel::Room end @@ -31,13 +34,14 @@ it "accepts all valid ids" do - valid_ids = [1..20] + valid_ids = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] valid_ids.each do |id| - @room = Hotel::Room.new(id) - expect(@room.id).must_equal id + room = Hotel::Room.new(id) + expect(room.id).must_equal id end end + it "must throw an argument error if an invalid id is given" do bogus_ids = [53, 0, 'cool', -5, nil, :cool] From 979b2ec1d2ceed2311dd066e6ca6f9c612289449 Mon Sep 17 00:00:00 2001 From: Lindsay Terchin Date: Tue, 4 Sep 2018 16:21:36 -0700 Subject: [PATCH 04/42] updated id variable to room_id variable --- lib/room.rb | 14 +++++++------- spec/room_spec.rb | 14 ++++++-------- 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/lib/room.rb b/lib/room.rb index 3ee02ce92..fb258c9e8 100644 --- a/lib/room.rb +++ b/lib/room.rb @@ -1,12 +1,12 @@ module Hotel class Room - attr_reader :status, :id + attr_reader :status, :room_id - def initialize(id, status = :AVAILABLE) - @id = id + def initialize(room_id, status = :AVAILABLE) + @room_id = room_id @status = status - validate_id + validate_room_id validate_status end @@ -19,10 +19,10 @@ def validate_status end - def validate_id - id_options = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] + def validate_room_id + room_id_options = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] - raise ArgumentError.new("You must enter a valid id.") unless id_options.include?(id) + raise ArgumentError.new("You must enter a valid room id.") unless room_id_options.include?(room_id) end diff --git a/spec/room_spec.rb b/spec/room_spec.rb index 5114fdbc3..c25661bda 100644 --- a/spec/room_spec.rb +++ b/spec/room_spec.rb @@ -7,8 +7,6 @@ it "is an instance of Room" do @room = Hotel::Room.new(2) - # binding.pry - expect(@room).must_be_kind_of Hotel::Room end @@ -34,18 +32,18 @@ it "accepts all valid ids" do - valid_ids = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] + valid_room_ids = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] - valid_ids.each do |id| + valid_room_ids.each do |id| room = Hotel::Room.new(id) - expect(room.id).must_equal id + expect(room.room_id).must_equal id end end - + it "must throw an argument error if an invalid id is given" do - bogus_ids = [53, 0, 'cool', -5, nil, :cool] - bogus_ids.each do |id| + bogus_room_ids = [53, 0, 'cool', -5, nil, :cool] + bogus_room_ids.each do |id| expect {Hotel::Room.new(id) }.must_raise ArgumentError end From 3b4ce06f6e62488433f7b9127e26e45b051f17da Mon Sep 17 00:00:00 2001 From: Lindsay Terchin Date: Wed, 5 Sep 2018 11:38:27 -0700 Subject: [PATCH 05/42] created a class for reservation --- lib/reservation.rb | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 lib/reservation.rb diff --git a/lib/reservation.rb b/lib/reservation.rb new file mode 100644 index 000000000..c356bbb4e --- /dev/null +++ b/lib/reservation.rb @@ -0,0 +1,29 @@ +require 'date' + +module Hotel + class Reservation + + @@total_reservations = 0 + + attr_reader :start_date, :end_date + + attr_accessor :reservation_id + + def initialize(start_date, end_date) + + @reservation_id = create_id + @start_date = Date.new(start_date) + @end_date = Date.new(end_date) + + end + + def create_id + new_id = @@total_reservations + 1 + end + + + end +end + + +#tests: From 2cfc494a5ca8f46b6cc233bce0e97ea0dda01209 Mon Sep 17 00:00:00 2001 From: Lindsay Terchin Date: Wed, 5 Sep 2018 12:02:04 -0700 Subject: [PATCH 06/42] created method to catch invalid start/end dates --- lib/reservation.rb | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/lib/reservation.rb b/lib/reservation.rb index c356bbb4e..1012a69de 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -2,12 +2,13 @@ module Hotel class Reservation + attr_reader :start_date, :end_date + attr_accessor :reservation_id + @@total_reservations = 0 + ROOMS = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] - attr_reader :start_date, :end_date - - attr_accessor :reservation_id def initialize(start_date, end_date) @@ -15,15 +16,33 @@ def initialize(start_date, end_date) @start_date = Date.new(start_date) @end_date = Date.new(end_date) + validate_dates + end + + + #create a new reservation id by adding 1 to total reservations def create_id new_id = @@total_reservations + 1 end + + #make sure there are valid start/end dates + def validate_dates + raise ArgumentError.new("The end date must be after the start date") if @end_date >= @start_date + + raise ArgumentError.new("The start date can't be nil") if @start_date == nil + + raise ArgumentError.new("The end date can't be nil") if @end_date == nil + end + + + + end end -#tests: +#tests: make sure params for start/end date are in the right format (else argument error) From b0848aafbc72771943158514c30aebe291dc7928 Mon Sep 17 00:00:00 2001 From: Lindsay Terchin Date: Wed, 5 Sep 2018 12:07:34 -0700 Subject: [PATCH 07/42] Add coverage directory to .gitignore file --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 5e1422c9c..c0ac3dc53 100644 --- a/.gitignore +++ b/.gitignore @@ -48,3 +48,4 @@ build-iPhoneSimulator/ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this: .rvmrc +coverage From e9b803559617a74721a1c7afb485eb2a510ce8cf Mon Sep 17 00:00:00 2001 From: Lindsay Terchin Date: Wed, 5 Sep 2018 12:15:42 -0700 Subject: [PATCH 08/42] created a new class ReservationHub to manage all reservations --- lib/reservation_hub.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 lib/reservation_hub.rb diff --git a/lib/reservation_hub.rb b/lib/reservation_hub.rb new file mode 100644 index 000000000..20e9930fc --- /dev/null +++ b/lib/reservation_hub.rb @@ -0,0 +1,11 @@ +require 'date' + +module Hotel + + class ReservationHub + + + + end + +end From 93c9f0b5b017451259340b1246997dbcbc0672f2 Mon Sep 17 00:00:00 2001 From: Lindsay Terchin Date: Wed, 5 Sep 2018 12:29:32 -0700 Subject: [PATCH 09/42] created some placeholder methods - add reservation, create id, return all reservations --- lib/reservation_hub.rb | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/lib/reservation_hub.rb b/lib/reservation_hub.rb index 20e9930fc..c6963393f 100644 --- a/lib/reservation_hub.rb +++ b/lib/reservation_hub.rb @@ -1,9 +1,41 @@ require 'date' module Hotel - class ReservationHub + attr_reader :start_date, :end_date + attr_accessor :reservation_id + + + + ROOMS = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] + + + def initialize(all_reservations) + + end + + + + def add_reservation(reservation_id, start_date, end_date) + reservation_id = create_id + + reservation = Reservation.new(reservation_id, start_date, end_date) + + @reservations << reservation + end + + + + #create a new reservation id by adding 1 to total reservations + def create_id + new_id = @reservations.length + 1 + end + + + def return_all_reservations + @reservations + end end From bf5738f5761b6acd51eb53d7f5c20dfd69efc019 Mon Sep 17 00:00:00 2001 From: Lindsay Terchin Date: Wed, 5 Sep 2018 14:13:34 -0700 Subject: [PATCH 10/42] created helper method to generate start/end dates --- lib/reservation.rb | 27 +++++++++------------------ 1 file changed, 9 insertions(+), 18 deletions(-) diff --git a/lib/reservation.rb b/lib/reservation.rb index 1012a69de..9e6a1ae12 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -1,37 +1,24 @@ require 'date' +require 'pry' module Hotel class Reservation attr_reader :start_date, :end_date - attr_accessor :reservation_id - - @@total_reservations = 0 - ROOMS = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] + def initialize(start_year, start_month, start_day, end_year, end_month, end_day) - def initialize(start_date, end_date) - - @reservation_id = create_id - @start_date = Date.new(start_date) - @end_date = Date.new(end_date) + @start_date = generate_date(start_year, start_month, start_day) + @end_date = generate_date(end_year, end_month, end_day) validate_dates end - - #create a new reservation id by adding 1 to total reservations - def create_id - new_id = @@total_reservations + 1 - end - - - #make sure there are valid start/end dates def validate_dates - raise ArgumentError.new("The end date must be after the start date") if @end_date >= @start_date + raise ArgumentError.new("The end date must be after the start date") if @end_date <= @start_date raise ArgumentError.new("The start date can't be nil") if @start_date == nil @@ -40,6 +27,10 @@ def validate_dates + def generate_date(year, month, day) + return Date.new(year, month, day) + end + end end From 84432574788fbdf5bfbdabe10841ce1cbe8ff5e8 Mon Sep 17 00:00:00 2001 From: Lindsay Terchin Date: Wed, 5 Sep 2018 14:14:12 -0700 Subject: [PATCH 11/42] created test to check reservation initialization --- spec/reservation_spec.rb | 16 ++++++++++++++++ 1 file changed, 16 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..af8016563 --- /dev/null +++ b/spec/reservation_spec.rb @@ -0,0 +1,16 @@ +require_relative 'spec_helper' + +describe "Reservation class" do + + describe "Reservation initialization" do + + it "is an instance of Reservation" do + @reservation = Hotel::Reservation.new(2015, 03, 01, 2015, 03, 05) + expect(@reservation).must_be_kind_of Hotel::Reservation + end + + + end + + +end From 834953a3a18bf912835dac80edca0f413d1bc633 Mon Sep 17 00:00:00 2001 From: Lindsay Terchin Date: Wed, 5 Sep 2018 14:29:07 -0700 Subject: [PATCH 12/42] created tests to validate reservation initialization & to make sure that the dates are the correct class --- spec/reservation_spec.rb | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/spec/reservation_spec.rb b/spec/reservation_spec.rb index af8016563..03adff5b9 100644 --- a/spec/reservation_spec.rb +++ b/spec/reservation_spec.rb @@ -9,8 +9,17 @@ expect(@reservation).must_be_kind_of Hotel::Reservation end - + it "must throw an argument error if the end date is before the start date" do + expect {Hotel::Reservation.new(2015, 03, 05, 2015, 03, 01)}.must_raise ArgumentError + end end + describe "Generate date" do + it "is a kind of date" do + + date = Date.new(2018,1,3) + expect(date).must_be_kind_of Date + end + end end From b68d74611b65b44a040073fa8c61bd361038ec32 Mon Sep 17 00:00:00 2001 From: Lindsay Terchin Date: Wed, 5 Sep 2018 21:51:46 -0700 Subject: [PATCH 13/42] created initialization tests for Reservation Hub --- spec/reservation_spec.rb | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/spec/reservation_spec.rb b/spec/reservation_spec.rb index 03adff5b9..74bbbb744 100644 --- a/spec/reservation_spec.rb +++ b/spec/reservation_spec.rb @@ -1,4 +1,5 @@ require_relative 'spec_helper' +require 'pry' describe "Reservation class" do @@ -16,10 +17,17 @@ describe "Generate date" do it "is a kind of date" do - date = Date.new(2018,1,3) expect(date).must_be_kind_of Date end end end + +#As an administrator, I can access the list of all of the rooms in the hotel +#As an administrator, I can reserve a room for a given date range +#As an administrator, I can access the list of reservations for a specific date +#def - find reservations (find all?) (date) +#return array of reservations + +#As an administrator, I can get the total cost for a given reservation From 1d6cc99f59fb9bc1dbc7402a312a8f3d189e6fc8 Mon Sep 17 00:00:00 2001 From: Lindsay Terchin Date: Wed, 5 Sep 2018 22:41:06 -0700 Subject: [PATCH 14/42] created test to see if new reservations are added to an array --- spec/reservation_hub_spec.rb | 105 +++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 spec/reservation_hub_spec.rb diff --git a/spec/reservation_hub_spec.rb b/spec/reservation_hub_spec.rb new file mode 100644 index 000000000..08b01ba12 --- /dev/null +++ b/spec/reservation_hub_spec.rb @@ -0,0 +1,105 @@ +require_relative 'spec_helper' +require 'pry' + +describe "Reservation Hub class" do + + before do + @reservation_hub = Hotel::ReservationHub.new + + @reservation1 = Hotel::Reservation.new(2018,01,03,2018,01,07) + @reservation2 = Hotel::Reservation.new(2018,04,02,2018,04,06) + @reservation3 = Hotel::Reservation.new(2018,02,04,2018,02,12) + + end + + + describe "Reservation Hub initialization" do + + it "is an instance of Reservation Hub" do + expect(@reservation_hub).must_be_kind_of Hotel::ReservationHub + end + + it "can access all available rooms" do + expect(@reservation_hub.rooms.length).must_equal 20 + end + + it "must initialize with an empty array of reservations" do + expect(@reservation_hub.reservations).must_be_kind_of Array + + expect(@reservation_hub.reservations.length).must_equal 0 + end + end + + describe "add reservation" do + + before do + @reservation_hub = Hotel::ReservationHub.new + end + + it "adds a new reservation to array of all reservations" do + + @reservation4 = @reservation_hub.add_reservation(2018,06,12,2018,07,01) + + @reservation4 = @reservation_hub.add_reservation(2017,01,02,2017,01,06) + + expect(@reservation_hub.reservations.length).must_equal 2 + + # binding.pry + end + + + + + + end + + +end + + + + + + #As an administrator, I can access the list of all of the rooms in the hotel + #As an administrator, I can reserve a room for a given date range + #As an administrator, I can access the list of reservations for a specific date + #def - find reservations (find all?) (date) + #return array of reservations + + #As an administrator, I can get the total cost for a given reservation + + + + + # it "will add the new reservation to the array" do + # # binding.pry + # # @reservations << @reservation1 + # expect(@reservations).must_include @reservation1 + # end + + # it 'will return all reservations' do + # expect(@reservations.length).must_equal 2 + # end + + # it 'will return an array of all reservations' do + # reservations = Hotel::Reservation.all + # expect(reservations).must_be_kind_of Array + # end + + # it "each item in array is a Reservation instance" do + # @reservations + # end + + + # it "Returns an array of all customers" do + # customers = Customer.all + # + # expect(customers.length).must_equal 35 + # customers.each do |c| + # expect(c).must_be_kind_of Customer + # end + # end + + + +# end From d040dddd8d8cf9e34f1aa3394358ff7c3ff8b7a3 Mon Sep 17 00:00:00 2001 From: Lindsay Terchin Date: Wed, 5 Sep 2018 22:58:50 -0700 Subject: [PATCH 15/42] created method for calculating reservation cost --- lib/reservation.rb | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/lib/reservation.rb b/lib/reservation.rb index 9e6a1ae12..1b9d5a7a9 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -5,12 +5,14 @@ module Hotel class Reservation attr_reader :start_date, :end_date - def initialize(start_year, start_month, start_day, end_year, end_month, end_day) @start_date = generate_date(start_year, start_month, start_day) + @end_date = generate_date(end_year, end_month, end_day) + @total_cost = calculate_reservation_cost(@start_date, @end_date) + validate_dates end @@ -32,8 +34,12 @@ def generate_date(year, month, day) end - end -end + def calculate_reservation_cost(start_date, end_date) + nightly_cost = 200 + total_days = (end_date - start_date).to_i + total_cost = total_days * nightly_cost + end -#tests: make sure params for start/end date are in the right format (else argument error) + end +end From e4ea61be574ec0dff74d419e22bc67f7d2d7a7f1 Mon Sep 17 00:00:00 2001 From: Lindsay Terchin Date: Thu, 6 Sep 2018 07:07:51 -0700 Subject: [PATCH 16/42] created test to find the total cost of a reservation --- spec/reservation_spec.rb | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/spec/reservation_spec.rb b/spec/reservation_spec.rb index 74bbbb744..a4f8534a0 100644 --- a/spec/reservation_spec.rb +++ b/spec/reservation_spec.rb @@ -22,8 +22,34 @@ end end + describe "reservation cost" do + it "returns the total amount of the new reservation" do + + @reservation = Hotel::Reservation.new(2015, 03, 01, 2015, 03, 05) + + start_date = @reservation.start_date + end_date = @reservation.end_date + + expect(@reservation.reservation_cost(start_date, end_date)).must_equal 800 + + + + end + + + end + end +# it "Returns the total from the collection of products" do +# products = { "banana" => 1.99, "cracker" => 3.00 } +# order = Order.new(1337, products, customer) +# +# expected_total = 5.36 +# +# expect(order.total).must_equal expected_total +# end + #As an administrator, I can access the list of all of the rooms in the hotel #As an administrator, I can reserve a room for a given date range #As an administrator, I can access the list of reservations for a specific date From 6365948080c2cedc977b4a5e123ceb6187941ff0 Mon Sep 17 00:00:00 2001 From: Lindsay Terchin Date: Thu, 6 Sep 2018 07:17:02 -0700 Subject: [PATCH 17/42] created tests to find a reservation by start date --- spec/reservation_hub_spec.rb | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/spec/reservation_hub_spec.rb b/spec/reservation_hub_spec.rb index 08b01ba12..78082142c 100644 --- a/spec/reservation_hub_spec.rb +++ b/spec/reservation_hub_spec.rb @@ -19,7 +19,7 @@ expect(@reservation_hub).must_be_kind_of Hotel::ReservationHub end - it "can access all available rooms" do + it "can access all rooms in the hotel" do expect(@reservation_hub.rooms.length).must_equal 20 end @@ -44,10 +44,32 @@ expect(@reservation_hub.reservations.length).must_equal 2 - # binding.pry end + describe "find reservation" do + + before do + @reservation_hub = Hotel::ReservationHub.new + + @reservation1 = Hotel::Reservation.new(2018,01,03,2018,01,07) + @reservation2 = Hotel::Reservation.new(2018,04,02,2018,04,06) + @reservation3 = Hotel::Reservation.new(2018,01,03,2018,02,12) + end + + it "returns a list of reservations based on a start date" do + + expect(@reservation_hub.find_reservations(2018-01-03)).must_be_kind_of Array + + expect(@reservation_hub.find_reservations(2018-01-03).lenght).must_equall 2 + + #each must be kind of (instance of?) reservation + + + end + + + end From bfb88945b7ff890b13567c8c95b45d23d6c506d4 Mon Sep 17 00:00:00 2001 From: Lindsay Terchin Date: Thu, 6 Sep 2018 09:13:49 -0700 Subject: [PATCH 18/42] created method to return all reservations --- lib/reservation_hub.rb | 44 ++++++++++++++++++++++++++---------------- 1 file changed, 27 insertions(+), 17 deletions(-) diff --git a/lib/reservation_hub.rb b/lib/reservation_hub.rb index c6963393f..3351d7d90 100644 --- a/lib/reservation_hub.rb +++ b/lib/reservation_hub.rb @@ -1,43 +1,53 @@ require 'date' +require 'pry' + +require_relative 'reservation' module Hotel class ReservationHub - attr_reader :start_date, :end_date - attr_accessor :reservation_id - - + attr_reader :start_date, :end_date, :rooms, :reservations + # attr_accessor :reservation_id, :reservations ROOMS = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] - def initialize(all_reservations) - + def initialize + @reservations = [] + @rooms = ROOMS end + def add_reservation(start_year, start_month, start_day, end_year, end_month, end_day) - def add_reservation(reservation_id, start_date, end_date) - reservation_id = create_id - - reservation = Reservation.new(reservation_id, start_date, end_date) + reservation = Reservation.new(start_year, start_month, start_day, end_year, end_month, end_day) - @reservations << reservation + all_reservations(reservation) end + def find_reservations(start_date) + reservations_by_date = [] + ReservationHub.add_reservation.each do |reservation| + if reservation.start_date = start_date + reservations_by_date << reservation + end + + end - #create a new reservation id by adding 1 to total reservations - def create_id - new_id = @reservations.length + 1 end - def return_all_reservations - @reservations + def all_reservations(reservation) + @reservations << reservation + return @reservations end + # + # + # def find_reservations(start_date) + # + # end end - end From cfd2ced4153b8fe6b73041d2b34e5beefcf09772 Mon Sep 17 00:00:00 2001 From: Lindsay Terchin Date: Thu, 6 Sep 2018 13:51:27 -0700 Subject: [PATCH 19/42] moving date creation from reservation to reservation hub --- lib/reservation.rb | 10 +++++----- lib/reservation_hub.rb | 36 +++++++++++++++++++++++++++--------- 2 files changed, 32 insertions(+), 14 deletions(-) diff --git a/lib/reservation.rb b/lib/reservation.rb index 1b9d5a7a9..492c9a43b 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -11,7 +11,7 @@ def initialize(start_year, start_month, start_day, end_year, end_month, end_day) @end_date = generate_date(end_year, end_month, end_day) - @total_cost = calculate_reservation_cost(@start_date, @end_date) + @total_cost = reservation_cost(@start_date, @end_date) validate_dates @@ -29,12 +29,12 @@ def validate_dates - def generate_date(year, month, day) - return Date.new(year, month, day) - end + # def generate_date(year, month, day) + # return Date.new(year, month, day) + # end - def calculate_reservation_cost(start_date, end_date) + def reservation_cost(start_date, end_date) nightly_cost = 200 total_days = (end_date - start_date).to_i total_cost = total_days * nightly_cost diff --git a/lib/reservation_hub.rb b/lib/reservation_hub.rb index 3351d7d90..46813d2e5 100644 --- a/lib/reservation_hub.rb +++ b/lib/reservation_hub.rb @@ -21,27 +21,45 @@ def add_reservation(start_year, start_month, start_day, end_year, end_month, end reservation = Reservation.new(start_year, start_month, start_day, end_year, end_month, end_day) - all_reservations(reservation) + @reservations << reservation + # return @reservations + end + + + def generate_date(year, month, day) + return Date.new(year, month, day) end + + def find_reservations(year, month, day) + # binding.pry - def find_reservations(start_date) + start_date = Reservation.generate_date(year, month, day) reservations_by_date = [] - ReservationHub.add_reservation.each do |reservation| - if reservation.start_date = start_date - reservations_by_date << reservation + index = 0 + all_reservations.each do + # binding.pry + if all_reservations[index].start_date == start_date + + reservations_by_date << all_reservations[index] + index +=1 end + return reservations_by_date end + end + + def all_reservations + @reservations end - def all_reservations(reservation) - @reservations << reservation - return @reservations - end + + # def all_reservations(reservation) + # + # end # # # def find_reservations(start_date) From 6dbe8c95aec874d400bdd833c69505408861ae5e Mon Sep 17 00:00:00 2001 From: Lindsay Terchin Date: Fri, 7 Sep 2018 06:58:00 -0700 Subject: [PATCH 20/42] finished moving the generate date method & corresponding tests from the reservation class to the reservation spec class --- lib/reservation.rb | 6 +++--- lib/reservation_hub.rb | 18 +++++++++++------ spec/reservation_hub_spec.rb | 39 ++++++++++++++++++++++++++++-------- spec/reservation_spec.rb | 23 +++++++++------------ 4 files changed, 56 insertions(+), 30 deletions(-) diff --git a/lib/reservation.rb b/lib/reservation.rb index 492c9a43b..7d490118c 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -5,11 +5,11 @@ module Hotel class Reservation attr_reader :start_date, :end_date - def initialize(start_year, start_month, start_day, end_year, end_month, end_day) + def initialize(start_date, end_date) - @start_date = generate_date(start_year, start_month, start_day) + @start_date = start_date - @end_date = generate_date(end_year, end_month, end_day) + @end_date = end_date @total_cost = reservation_cost(@start_date, @end_date) diff --git a/lib/reservation_hub.rb b/lib/reservation_hub.rb index 46813d2e5..551234894 100644 --- a/lib/reservation_hub.rb +++ b/lib/reservation_hub.rb @@ -19,7 +19,11 @@ def initialize def add_reservation(start_year, start_month, start_day, end_year, end_month, end_day) - reservation = Reservation.new(start_year, start_month, start_day, end_year, end_month, end_day) + start_date = generate_date(start_year, start_month, start_day) + + end_date = generate_date(end_year, end_month, end_day) + + reservation = Reservation.new(start_date, end_date) @reservations << reservation # return @reservations @@ -29,12 +33,12 @@ def add_reservation(start_year, start_month, start_day, end_year, end_month, end def generate_date(year, month, day) return Date.new(year, month, day) end - + def find_reservations(year, month, day) - # binding.pry + + start_date = generate_date(year, month, day) - start_date = Reservation.generate_date(year, month, day) reservations_by_date = [] index = 0 all_reservations.each do @@ -42,11 +46,13 @@ def find_reservations(year, month, day) if all_reservations[index].start_date == start_date reservations_by_date << all_reservations[index] - index +=1 + # binding.pry end - return reservations_by_date + index +=1 end + return reservations_by_date + end diff --git a/spec/reservation_hub_spec.rb b/spec/reservation_hub_spec.rb index 78082142c..a022e9e8f 100644 --- a/spec/reservation_hub_spec.rb +++ b/spec/reservation_hub_spec.rb @@ -6,9 +6,17 @@ before do @reservation_hub = Hotel::ReservationHub.new - @reservation1 = Hotel::Reservation.new(2018,01,03,2018,01,07) - @reservation2 = Hotel::Reservation.new(2018,04,02,2018,04,06) - @reservation3 = Hotel::Reservation.new(2018,02,04,2018,02,12) +start_date1 = Date.new(2018, 01, 03) +start_date2 = Date.new(2018, 04, 03) +start_date3 = Date.new(2018, 02, 04) + +end_date1 = Date.new(2018, 01, 07) +end_date2 = Date.new(2018, 04, 06) +end_date3 = Date.new(2018, 02, 12) + + @reservation1 = Hotel::Reservation.new(start_date1, end_date1) + @reservation2 = Hotel::Reservation.new(start_date2, end_date2) + @reservation3 = Hotel::Reservation.new(start_date3, end_date3) end @@ -43,26 +51,41 @@ @reservation4 = @reservation_hub.add_reservation(2017,01,02,2017,01,06) expect(@reservation_hub.reservations.length).must_equal 2 + # binding.pry + end + describe "Generate date" do + it "is a kind of date" do + date = Date.new(2018,1,3) + expect(date).must_be_kind_of Date + end + end describe "find reservation" do before do @reservation_hub = Hotel::ReservationHub.new - @reservation1 = Hotel::Reservation.new(2018,01,03,2018,01,07) - @reservation2 = Hotel::Reservation.new(2018,04,02,2018,04,06) - @reservation3 = Hotel::Reservation.new(2018,01,03,2018,02,12) + @reservation_hub.add_reservation(2018, 01, 03, 2018, 01, 07) + @reservation_hub.add_reservation(2018, 04, 03, 2018, 04, 06) + @reservation_hub.add_reservation(2018, 01, 03, 2018, 02, 12) + + # @reservation1 = @reservation_hub.add_reservation(2018,01,03,2018,01,07) + # @reservation2 = @reservation_hub.add_reservation(2018,04,02,2018,04,06) + # @reservation3 = @reservation_hub.add_reservation(2018,01,03,2018,02,12) + end it "returns a list of reservations based on a start date" do - expect(@reservation_hub.find_reservations(2018-01-03)).must_be_kind_of Array + reservations = @reservation_hub.find_reservations(2018, 04, 03) + # binding.pry - expect(@reservation_hub.find_reservations(2018-01-03).lenght).must_equall 2 + expect(reservations).must_be_kind_of Array + expect(reservations.length).must_equal 1 #each must be kind of (instance of?) reservation diff --git a/spec/reservation_spec.rb b/spec/reservation_spec.rb index a4f8534a0..c8f6c8aa4 100644 --- a/spec/reservation_spec.rb +++ b/spec/reservation_spec.rb @@ -6,34 +6,31 @@ describe "Reservation initialization" do it "is an instance of Reservation" do - @reservation = Hotel::Reservation.new(2015, 03, 01, 2015, 03, 05) + start_date = Date.new(2015, 03, 01) + end_date = Date.new(2015, 03, 05) + @reservation = Hotel::Reservation.new(start_date, end_date) expect(@reservation).must_be_kind_of Hotel::Reservation end it "must throw an argument error if the end date is before the start date" do - expect {Hotel::Reservation.new(2015, 03, 05, 2015, 03, 01)}.must_raise ArgumentError + start_date = Date.new(2015, 03, 05) + end_date = Date.new(2015, 03, 01) + expect {Hotel::Reservation.new(start_date, end_date)}.must_raise ArgumentError end end - describe "Generate date" do - it "is a kind of date" do - date = Date.new(2018,1,3) - expect(date).must_be_kind_of Date - end - end + describe "reservation cost" do it "returns the total amount of the new reservation" do - - @reservation = Hotel::Reservation.new(2015, 03, 01, 2015, 03, 05) + start_date = Date.new(2015, 03, 01) + end_date = Date.new(2015, 03, 05) + @reservation = Hotel::Reservation.new(start_date, end_date) start_date = @reservation.start_date end_date = @reservation.end_date expect(@reservation.reservation_cost(start_date, end_date)).must_equal 800 - - - end From 4a61fd4fa86f54c487413ce593eaf6171de6df1c Mon Sep 17 00:00:00 2001 From: Lindsay Terchin Date: Fri, 7 Sep 2018 10:32:49 -0700 Subject: [PATCH 21/42] created additional tests for returning all reservations --- spec/reservation_hub_spec.rb | 84 ++++++++++++++++++++++-------------- 1 file changed, 51 insertions(+), 33 deletions(-) diff --git a/spec/reservation_hub_spec.rb b/spec/reservation_hub_spec.rb index a022e9e8f..af8a90624 100644 --- a/spec/reservation_hub_spec.rb +++ b/spec/reservation_hub_spec.rb @@ -6,13 +6,13 @@ before do @reservation_hub = Hotel::ReservationHub.new -start_date1 = Date.new(2018, 01, 03) -start_date2 = Date.new(2018, 04, 03) -start_date3 = Date.new(2018, 02, 04) + start_date1 = Date.new(2018, 01, 03) + start_date2 = Date.new(2018, 04, 03) + start_date3 = Date.new(2018, 02, 04) -end_date1 = Date.new(2018, 01, 07) -end_date2 = Date.new(2018, 04, 06) -end_date3 = Date.new(2018, 02, 12) + end_date1 = Date.new(2018, 01, 07) + end_date2 = Date.new(2018, 04, 06) + end_date3 = Date.new(2018, 02, 12) @reservation1 = Hotel::Reservation.new(start_date1, end_date1) @reservation2 = Hotel::Reservation.new(start_date2, end_date2) @@ -40,9 +40,9 @@ describe "add reservation" do - before do - @reservation_hub = Hotel::ReservationHub.new - end + # before do + # @reservation_hub = Hotel::ReservationHub.new + # end it "adds a new reservation to array of all reservations" do @@ -51,54 +51,72 @@ @reservation4 = @reservation_hub.add_reservation(2017,01,02,2017,01,06) expect(@reservation_hub.reservations.length).must_equal 2 - # binding.pry + end - end + # it "accepts a Date argument for start and end dates" do + # expect(start_date).must_be_kind_of Date + # expect(end_date).must_be_kind_of Date + # + # end + end - describe "Generate date" do - it "is a kind of date" do - date = Date.new(2018,1,3) - expect(date).must_be_kind_of Date - end + describe "Generate date" do + it "is a kind of date" do + date = Date.new(2018,1,3) + expect(date).must_be_kind_of Date end + end - describe "find reservation" do + describe "find reservation" do - before do - @reservation_hub = Hotel::ReservationHub.new + before do + @reservation_hub = Hotel::ReservationHub.new - @reservation_hub.add_reservation(2018, 01, 03, 2018, 01, 07) - @reservation_hub.add_reservation(2018, 04, 03, 2018, 04, 06) - @reservation_hub.add_reservation(2018, 01, 03, 2018, 02, 12) + @reservation_hub.add_reservation(2018, 01, 03, 2018, 01, 07) + @reservation_hub.add_reservation(2018, 04, 03, 2018, 04, 06) + @reservation_hub.add_reservation(2018, 01, 03, 2018, 02, 12) - # @reservation1 = @reservation_hub.add_reservation(2018,01,03,2018,01,07) - # @reservation2 = @reservation_hub.add_reservation(2018,04,02,2018,04,06) - # @reservation3 = @reservation_hub.add_reservation(2018,01,03,2018,02,12) + end - end + it "returns a list of reservations based on a start date" do - it "returns a list of reservations based on a start date" do + reservations = @reservation_hub.find_reservations(2018, 04, 03) - reservations = @reservation_hub.find_reservations(2018, 04, 03) - # binding.pry + expect(reservations).must_be_kind_of Array - expect(reservations).must_be_kind_of Array + expect(reservations.length).must_equal 1 - expect(reservations.length).must_equal 1 - #each must be kind of (instance of?) reservation + end + end + describe "all reservations" do - end + before do + @reservation_hub = Hotel::ReservationHub.new + @reservation_hub.add_reservation(2018, 01, 03, 2018, 01, 07) + @reservation_hub.add_reservation(2018, 04, 03, 2018, 04, 06) + @reservation_hub.add_reservation(2018, 01, 03, 2018, 02, 12) end + it "returns an array of all reservations" do + reservations = @reservation_hub.all_reservations + expect(reservations.length).must_equal 3 + + reservations.each do |res| + expect(res).must_be_kind_of Hotel::Reservation + end + + end end + + end From 89e4c38dabbc44a4bd3add6c3b1be35bb18fb91f Mon Sep 17 00:00:00 2001 From: Lindsay Terchin Date: Fri, 7 Sep 2018 10:35:15 -0700 Subject: [PATCH 22/42] overall formatting cleanup before waves 2/3 --- Guardfile | 2 +- lib/reservation.rb | 9 ++- lib/reservation_hub.rb | 21 +----- lib/room.rb | 60 ++++++++--------- spec/reservation_hub_spec.rb | 27 ++------ spec/reservation_spec.rb | 11 +--- spec/room_spec.rb | 122 +++++++++++++++++------------------ spec/spec_helper.rb | 9 ++- 8 files changed, 114 insertions(+), 147 deletions(-) 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" } diff --git a/lib/reservation.rb b/lib/reservation.rb index 7d490118c..c14613f15 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -13,6 +13,8 @@ def initialize(start_date, end_date) @total_cost = reservation_cost(@start_date, @end_date) + # @room_number = find_room #Room.find(@start_date, @end_date? - return id of ones that are available) + validate_dates end @@ -33,7 +35,6 @@ def validate_dates # return Date.new(year, month, day) # end - def reservation_cost(start_date, end_date) nightly_cost = 200 total_days = (end_date - start_date).to_i @@ -41,5 +42,11 @@ def reservation_cost(start_date, end_date) end + def find_room(start_date, end_date) + + + end + + end end diff --git a/lib/reservation_hub.rb b/lib/reservation_hub.rb index 551234894..127486dc7 100644 --- a/lib/reservation_hub.rb +++ b/lib/reservation_hub.rb @@ -6,7 +6,6 @@ module Hotel class ReservationHub attr_reader :start_date, :end_date, :rooms, :reservations - # attr_accessor :reservation_id, :reservations ROOMS = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] @@ -26,7 +25,6 @@ def add_reservation(start_year, start_month, start_day, end_year, end_month, end reservation = Reservation.new(start_date, end_date) @reservations << reservation - # return @reservations end @@ -36,23 +34,20 @@ def generate_date(year, month, day) def find_reservations(year, month, day) - - start_date = generate_date(year, month, day) + start_date = generate_date(year, month, day) reservations_by_date = [] index = 0 + all_reservations.each do - # binding.pry if all_reservations[index].start_date == start_date reservations_by_date << all_reservations[index] - # binding.pry end index +=1 end return reservations_by_date - end @@ -61,17 +56,5 @@ def all_reservations end - - - # def all_reservations(reservation) - # - # end - # - # - # def find_reservations(start_date) - # - # end - - end end diff --git a/lib/room.rb b/lib/room.rb index fb258c9e8..5733a0915 100644 --- a/lib/room.rb +++ b/lib/room.rb @@ -1,30 +1,30 @@ -module Hotel - class Room - attr_reader :status, :room_id - - def initialize(room_id, status = :AVAILABLE) - @room_id = room_id - @status = status - - validate_room_id - validate_status - - end - - - def validate_status - status_options = [:AVAILABLE, :UNAVAILABLE] - - raise ArgumentError.new("You must enter a valid status.") unless status_options.include?(status) - end - - - def validate_room_id - room_id_options = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] - - raise ArgumentError.new("You must enter a valid room id.") unless room_id_options.include?(room_id) - end - - - end -end +# module Hotel +# class Room +# attr_reader :status, :room_id +# +# def initialize(room_id, status = :AVAILABLE) +# @room_id = room_id +# @status = status +# +# validate_room_id +# validate_status +# +# end +# +# +# def validate_status +# status_options = [:AVAILABLE, :UNAVAILABLE] +# +# raise ArgumentError.new("You must enter a valid status.") unless status_options.include?(status) +# end +# +# +# def validate_room_id +# room_id_options = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] +# +# raise ArgumentError.new("You must enter a valid room id.") unless room_id_options.include?(room_id) +# end +# +# +# end +# end diff --git a/spec/reservation_hub_spec.rb b/spec/reservation_hub_spec.rb index af8a90624..8b922de21 100644 --- a/spec/reservation_hub_spec.rb +++ b/spec/reservation_hub_spec.rb @@ -38,6 +38,7 @@ end end + describe "add reservation" do # before do @@ -52,15 +53,9 @@ expect(@reservation_hub.reservations.length).must_equal 2 end - - - # it "accepts a Date argument for start and end dates" do - # expect(start_date).must_be_kind_of Date - # expect(end_date).must_be_kind_of Date - # - # end end + describe "Generate date" do it "is a kind of date" do date = Date.new(2018,1,3) @@ -68,6 +63,7 @@ end end + describe "find reservation" do before do @@ -76,9 +72,9 @@ @reservation_hub.add_reservation(2018, 01, 03, 2018, 01, 07) @reservation_hub.add_reservation(2018, 04, 03, 2018, 04, 06) @reservation_hub.add_reservation(2018, 01, 03, 2018, 02, 12) - end + it "returns a list of reservations based on a start date" do reservations = @reservation_hub.find_reservations(2018, 04, 03) @@ -86,7 +82,6 @@ expect(reservations).must_be_kind_of Array expect(reservations.length).must_equal 1 - end end @@ -98,7 +93,6 @@ @reservation_hub.add_reservation(2018, 01, 03, 2018, 01, 07) @reservation_hub.add_reservation(2018, 04, 03, 2018, 04, 06) @reservation_hub.add_reservation(2018, 01, 03, 2018, 02, 12) - end it "returns an array of all reservations" do @@ -109,28 +103,15 @@ reservations.each do |res| expect(res).must_be_kind_of Hotel::Reservation end - end - end - - - end - #As an administrator, I can access the list of all of the rooms in the hotel - #As an administrator, I can reserve a room for a given date range - #As an administrator, I can access the list of reservations for a specific date - #def - find reservations (find all?) (date) - #return array of reservations - - #As an administrator, I can get the total cost for a given reservation - diff --git a/spec/reservation_spec.rb b/spec/reservation_spec.rb index c8f6c8aa4..13048d9d5 100644 --- a/spec/reservation_spec.rb +++ b/spec/reservation_spec.rb @@ -32,20 +32,11 @@ expect(@reservation.reservation_cost(start_date, end_date)).must_equal 800 end - - end end -# it "Returns the total from the collection of products" do -# products = { "banana" => 1.99, "cracker" => 3.00 } -# order = Order.new(1337, products, customer) -# -# expected_total = 5.36 -# -# expect(order.total).must_equal expected_total -# end + #As an administrator, I can access the list of all of the rooms in the hotel #As an administrator, I can reserve a room for a given date range diff --git a/spec/room_spec.rb b/spec/room_spec.rb index c25661bda..331081618 100644 --- a/spec/room_spec.rb +++ b/spec/room_spec.rb @@ -1,61 +1,61 @@ -require_relative 'spec_helper' -require 'pry' - -describe 'Room class' do - - describe "initialize" do - - it "is an instance of Room" do - @room = Hotel::Room.new(2) - expect(@room).must_be_kind_of Hotel::Room - end - - - it "contains a valid status" do - valid_statuses = [:AVAILABLE, :UNAVAILABLE] - valid_statuses.each do |status| - @room = Hotel::Room.new(2, status) - expect(@room.status).must_equal status - end - end - - - it "sets the status to :AVAILABLE if no status is given" do - @room = Hotel::Room.new(2) - expect(@room.status).must_equal :AVAILABLE - end - - - it "must throw an argument error if an invalid status is given" do - expect {Hotel::Room.new(2, :HUH)}.must_raise ArgumentError - end - - - it "accepts all valid ids" do - valid_room_ids = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] - - valid_room_ids.each do |id| - room = Hotel::Room.new(id) - expect(room.room_id).must_equal id - end - end - - - it "must throw an argument error if an invalid id is given" do - bogus_room_ids = [53, 0, 'cool', -5, nil, :cool] - bogus_room_ids.each do |id| - expect {Hotel::Room.new(id) - }.must_raise ArgumentError - end - end - - - - - - - end - - - -end +# require_relative 'spec_helper' +# require 'pry' +# +# describe 'Room class' do +# +# describe "initialize" do +# +# it "is an instance of Room" do +# @room = Hotel::Room.new(2) +# expect(@room).must_be_kind_of Hotel::Room +# end +# +# +# it "contains a valid status" do +# valid_statuses = [:AVAILABLE, :UNAVAILABLE] +# valid_statuses.each do |status| +# @room = Hotel::Room.new(2, status) +# expect(@room.status).must_equal status +# end +# end +# +# +# it "sets the status to :AVAILABLE if no status is given" do +# @room = Hotel::Room.new(2) +# expect(@room.status).must_equal :AVAILABLE +# end +# +# +# it "must throw an argument error if an invalid status is given" do +# expect {Hotel::Room.new(2, :HUH)}.must_raise ArgumentError +# end +# +# +# it "accepts all valid ids" do +# valid_room_ids = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] +# +# valid_room_ids.each do |id| +# room = Hotel::Room.new(id) +# expect(room.room_id).must_equal id +# end +# end +# +# +# it "must throw an argument error if an invalid id is given" do +# bogus_room_ids = [53, 0, 'cool', -5, nil, :cool] +# bogus_room_ids.each do |id| +# expect {Hotel::Room.new(id) +# }.must_raise ArgumentError +# end +# end +# +# +# +# +# +# +# end +# +# +# +# end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 4d1e3fdc8..8c7888664 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,8 +1,13 @@ +require 'simplecov' +SimpleCov.start + require 'minitest' require 'minitest/autorun' require 'minitest/reporters' -# Add simplecov +require 'date' Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new -# Require_relative your lib files here! +require_relative '../lib/room' +require_relative '../lib/reservation' +require_relative '../lib/reservation_hub' From 43eda8a3d70af96f46f3f6873c22be1ab7dfb709 Mon Sep 17 00:00:00 2001 From: Lindsay Terchin Date: Fri, 7 Sep 2018 11:05:08 -0700 Subject: [PATCH 23/42] created new class for room_booking & create some skeleton methods --- lib/room_booking.rb | 65 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 lib/room_booking.rb diff --git a/lib/room_booking.rb b/lib/room_booking.rb new file mode 100644 index 000000000..653a2128d --- /dev/null +++ b/lib/room_booking.rb @@ -0,0 +1,65 @@ +require 'date' +require 'pry' + +require_relative 'reservation' + +module Hotel + class RoomBooking + + ROOMS = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] + + def initialize(start_date, end_date) + + @start_date = start_date + @end_date = end_date + @room_id = find_room_id(@start_date, @end_date) + @booked_dates = {} + + end + + + def create_date_array(start_date, end_date) + number_of_nights = (end_date - start_date).to_i + date_array = [] + number_of_nights.times do + date_array << start_date + start_date +=1 + end + return date_array + end + + + def find_room_id + @booked_dates.each do |room| + room.each do |dates| + + end + end + end + + + def add_booked_dates(room_id) + #check to see if room id already exists in hash + #if not, create new k/v pair + date_array_by_room(x, x) + + + end + + + + + + + + def add_booked_dates(id, start_date, end_date) +#returns hash of booked dates by room + end + + + + + + + end +end From 528cb450ed630aff90110470f293b0859ed3beed Mon Sep 17 00:00:00 2001 From: Lindsay Terchin Date: Fri, 7 Sep 2018 13:18:46 -0700 Subject: [PATCH 24/42] moved methods from room_booking back to reservation_hub --- lib/reservation.rb | 20 ++++- lib/reservation_hub.rb | 41 ++++++++++- lib/room.rb | 51 ++++++++----- lib/room_booking.rb | 151 ++++++++++++++++++++++---------------- spec/room_booking_spec.rb | 2 + 5 files changed, 178 insertions(+), 87 deletions(-) create mode 100644 spec/room_booking_spec.rb diff --git a/lib/reservation.rb b/lib/reservation.rb index c14613f15..cbd31f315 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -1,9 +1,11 @@ require 'date' require 'pry' +require_relative 'room_booking' + module Hotel class Reservation - attr_reader :start_date, :end_date + attr_reader :start_date, :end_date, :room_id def initialize(start_date, end_date) @@ -13,7 +15,9 @@ def initialize(start_date, end_date) @total_cost = reservation_cost(@start_date, @end_date) - # @room_number = find_room #Room.find(@start_date, @end_date? - return id of ones that are available) + @room_booking = Hotel::RoomBooking.new(start_date, end_date) + + # @room_id = Hotel::RoomBooking.find_room_id(start_date, end_date) validate_dates @@ -30,6 +34,18 @@ def validate_dates end + #create array for all nights in the reservation + def create_date_array(start_date, end_date) + number_of_nights = (end_date - start_date).to_i + date_array = [] + number_of_nights.times do + date_array << start_date + start_date +=1 + end + return date_array + end + + # def generate_date(year, month, day) # return Date.new(year, month, day) diff --git a/lib/reservation_hub.rb b/lib/reservation_hub.rb index 127486dc7..fb2b2e156 100644 --- a/lib/reservation_hub.rb +++ b/lib/reservation_hub.rb @@ -7,12 +7,12 @@ module Hotel class ReservationHub attr_reader :start_date, :end_date, :rooms, :reservations - ROOMS = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] + # ROOMS = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] def initialize @reservations = [] - @rooms = ROOMS + @room_bookings = {1 => [], 2 => [], 3 => [], 4 => [], 5 => [], 6 => [], 7 => [], 8 => [], 9 => [], 10 => [], 11 => [], 12 => [], 13 => [], 14 => [], 15 => [], 16 => [], 17 => [], 18 => [], 19 => [], 20 => []} end @@ -33,6 +33,43 @@ def generate_date(year, month, day) end + def create_date_array(start_date, end_date) + number_of_nights = (end_date - start_date).to_i + date_array = [] + number_of_nights.times do + date_array << start_date + start_date +=1 + end + return date_array + end + + + def check_available_rooms(start_date, end_date) + reservation_dates = create_date_array(start_date, end_date) + + @room_bookings.each do |room| + + reservation_dates.each do |date| + if @room_bookings[room].include?(date) + break + end + end + + room_id = assign_room(room, reservation_dates) + return room_id + end + + return "I'm sorry but there are no rooms available for your dates." + end + + + def assign_room(room_id, reservation_dates) + @room_bookings[room_id] << reservation_dates + return room_id + end + + + def find_reservations(year, month, day) start_date = generate_date(year, month, day) diff --git a/lib/room.rb b/lib/room.rb index 5733a0915..fa14e46e2 100644 --- a/lib/room.rb +++ b/lib/room.rb @@ -1,18 +1,33 @@ -# module Hotel -# class Room -# attr_reader :status, :room_id -# -# def initialize(room_id, status = :AVAILABLE) -# @room_id = room_id -# @status = status -# -# validate_room_id -# validate_status -# -# end -# -# -# def validate_status +module Hotel + class Room + + require_relative 'reservation' + + module Hotel + class RoomBooking + + ROOMS = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] + + def initialize(start_date, end_date) + + @start_date = start_date + @end_date = end_date + @booked_dates = [] + + end + + + # def initialize(room_id, status = :AVAILABLE) + # @room_id = room_id + # @status = status + # + # validate_room_id + # validate_status + # + # end + + + # def validate_status # status_options = [:AVAILABLE, :UNAVAILABLE] # # raise ArgumentError.new("You must enter a valid status.") unless status_options.include?(status) @@ -25,6 +40,6 @@ # raise ArgumentError.new("You must enter a valid room id.") unless room_id_options.include?(room_id) # end # -# -# end -# end + + end +end diff --git a/lib/room_booking.rb b/lib/room_booking.rb index 653a2128d..373438593 100644 --- a/lib/room_booking.rb +++ b/lib/room_booking.rb @@ -1,65 +1,86 @@ -require 'date' -require 'pry' - -require_relative 'reservation' - -module Hotel - class RoomBooking - - ROOMS = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] - - def initialize(start_date, end_date) - - @start_date = start_date - @end_date = end_date - @room_id = find_room_id(@start_date, @end_date) - @booked_dates = {} - - end - - - def create_date_array(start_date, end_date) - number_of_nights = (end_date - start_date).to_i - date_array = [] - number_of_nights.times do - date_array << start_date - start_date +=1 - end - return date_array - end - - - def find_room_id - @booked_dates.each do |room| - room.each do |dates| - - end - end - end - - - def add_booked_dates(room_id) - #check to see if room id already exists in hash - #if not, create new k/v pair - date_array_by_room(x, x) - - - end - - - - - - - - def add_booked_dates(id, start_date, end_date) -#returns hash of booked dates by room - end - - - - - - - end -end +# require 'date' +# require 'pry' +# +# require_relative 'reservation' +# +# module Hotel +# class RoomBooking +# +# ROOMS = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] +# +# def initialize(start_date, end_date) +# +# @start_date = start_date +# @end_date = end_date +# @room_id = find_room_id(@start_date, @end_date) +# @booked_dates = {} +# +# end +# +# +# def create_date_array(start_date, end_date) +# number_of_nights = (end_date - start_date).to_i +# date_array = [] +# number_of_nights.times do +# date_array << start_date +# start_date +=1 +# end +# return date_array +# end +# +# +# def find_room_id(start_date, end_date) +# +# reservation_dates = create_date_array(start_date, end_date) +# +# ROOMS.each do |room| +# +# if @booked_dates.key?(room) == false +# add_booked_dates(room, reservation_dates) +# return room +# end +# +# reservation_dates.each do |date| +# if booked_dates.include?(date) +# break +# else +# add_booked_dates(room, reservation_dates) +# return room +# end +# end +# end +# return "I'm sorry but there are no rooms available for your dates." +# end +# +# +# def add_booked_dates(room_id, reservation_dates) +# @booked_dates[:room_id] = reservation_dates +# return room_id +# end +# +# +# +# +# +# +# +# +# +# #this method checks to see if there is a key/value pair for a given room number (so no booked dates) +# # def never_been_booked?(room, reservation_dates) +# # if @booked_dates.has_key?(room) == false +# # return true +# # end +# # end +# +# #this method is assuming a room already has some dates assigned to it +# # def check_each_date(reservation_dates, booked) +# # reservation_dates.each do |date| +# # if booked_dates.include?(date) +# # break +# # end +# # end +# +# +# end +# end diff --git a/spec/room_booking_spec.rb b/spec/room_booking_spec.rb new file mode 100644 index 000000000..1a8982905 --- /dev/null +++ b/spec/room_booking_spec.rb @@ -0,0 +1,2 @@ +require_relative 'spec_helper' +require 'pry' From a3a4a42c7a2cc54ea5068f65aac569043833afde Mon Sep 17 00:00:00 2001 From: Lindsay Terchin Date: Fri, 7 Sep 2018 14:24:40 -0700 Subject: [PATCH 25/42] added room number to reservation initialization --- lib/reservation.rb | 23 ++++++++++---------- lib/reservation_hub.rb | 14 ++++++------ lib/room.rb | 42 ++++++++++++++++++------------------ spec/reservation_hub_spec.rb | 9 ++++---- spec/reservation_spec.rb | 4 ++-- 5 files changed, 48 insertions(+), 44 deletions(-) diff --git a/lib/reservation.rb b/lib/reservation.rb index cbd31f315..9ad351ed2 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -7,15 +7,16 @@ module Hotel class Reservation attr_reader :start_date, :end_date, :room_id - def initialize(start_date, end_date) + def initialize(start_date, end_date, room_id) @start_date = start_date @end_date = end_date + @room_id = room_id + @total_cost = reservation_cost(@start_date, @end_date) - @room_booking = Hotel::RoomBooking.new(start_date, end_date) # @room_id = Hotel::RoomBooking.find_room_id(start_date, end_date) @@ -35,15 +36,15 @@ def validate_dates #create array for all nights in the reservation - def create_date_array(start_date, end_date) - number_of_nights = (end_date - start_date).to_i - date_array = [] - number_of_nights.times do - date_array << start_date - start_date +=1 - end - return date_array - end + # def create_date_array(start_date, end_date) + # number_of_nights = (end_date - start_date).to_i + # date_array = [] + # number_of_nights.times do + # date_array << start_date + # start_date +=1 + # end + # return date_array + # end diff --git a/lib/reservation_hub.rb b/lib/reservation_hub.rb index fb2b2e156..87cc8434b 100644 --- a/lib/reservation_hub.rb +++ b/lib/reservation_hub.rb @@ -5,24 +5,26 @@ module Hotel class ReservationHub - attr_reader :start_date, :end_date, :rooms, :reservations + attr_reader :start_date, :end_date, :rooms, :reservations, :room_bookings # ROOMS = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] - def initialize @reservations = [] @room_bookings = {1 => [], 2 => [], 3 => [], 4 => [], 5 => [], 6 => [], 7 => [], 8 => [], 9 => [], 10 => [], 11 => [], 12 => [], 13 => [], 14 => [], 15 => [], 16 => [], 17 => [], 18 => [], 19 => [], 20 => []} end + def add_reservation(start_year, start_month, start_day, end_year, end_month, end_day) start_date = generate_date(start_year, start_month, start_day) end_date = generate_date(end_year, end_month, end_day) - reservation = Reservation.new(start_date, end_date) + room_id = check_available_rooms(start_date, end_date) + + reservation = Reservation.new(start_date, end_date, room_id) @reservations << reservation end @@ -48,9 +50,10 @@ def check_available_rooms(start_date, end_date) reservation_dates = create_date_array(start_date, end_date) @room_bookings.each do |room| - reservation_dates.each do |date| - if @room_bookings[room].include?(date) + # binding.pry + + if room.include?(date) break end end @@ -69,7 +72,6 @@ def assign_room(room_id, reservation_dates) end - def find_reservations(year, month, day) start_date = generate_date(year, month, day) diff --git a/lib/room.rb b/lib/room.rb index fa14e46e2..30de57806 100644 --- a/lib/room.rb +++ b/lib/room.rb @@ -1,21 +1,21 @@ -module Hotel - class Room - - require_relative 'reservation' - - module Hotel - class RoomBooking - - ROOMS = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] - - def initialize(start_date, end_date) - - @start_date = start_date - @end_date = end_date - @booked_dates = [] - - end - +# module Hotel +# class Room +# +# require_relative 'reservation' +# +# module Hotel +# class RoomBooking +# +# ROOMS = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] +# +# def initialize(start_date, end_date) +# +# @start_date = start_date +# @end_date = end_date +# @booked_dates = [] +# +# end +# # def initialize(room_id, status = :AVAILABLE) # @room_id = room_id @@ -40,6 +40,6 @@ def initialize(start_date, end_date) # raise ArgumentError.new("You must enter a valid room id.") unless room_id_options.include?(room_id) # end # - - end -end +# +# end +# end diff --git a/spec/reservation_hub_spec.rb b/spec/reservation_hub_spec.rb index 8b922de21..f97c02203 100644 --- a/spec/reservation_hub_spec.rb +++ b/spec/reservation_hub_spec.rb @@ -14,9 +14,10 @@ end_date2 = Date.new(2018, 04, 06) end_date3 = Date.new(2018, 02, 12) - @reservation1 = Hotel::Reservation.new(start_date1, end_date1) - @reservation2 = Hotel::Reservation.new(start_date2, end_date2) - @reservation3 = Hotel::Reservation.new(start_date3, end_date3) + @reservation1 = Hotel::Reservation.new(start_date1, end_date1, 3) + @reservation2 = Hotel::Reservation.new(start_date2, end_date2, 4) + @reservation3 = Hotel::Reservation.new(start_date3, end_date3, 5) + # binding.pry end @@ -28,7 +29,7 @@ end it "can access all rooms in the hotel" do - expect(@reservation_hub.rooms.length).must_equal 20 + expect(@room_bookings.keys.length).must_equal 20 end it "must initialize with an empty array of reservations" do diff --git a/spec/reservation_spec.rb b/spec/reservation_spec.rb index 13048d9d5..43bb25952 100644 --- a/spec/reservation_spec.rb +++ b/spec/reservation_spec.rb @@ -8,7 +8,7 @@ it "is an instance of Reservation" do start_date = Date.new(2015, 03, 01) end_date = Date.new(2015, 03, 05) - @reservation = Hotel::Reservation.new(start_date, end_date) + @reservation = Hotel::Reservation.new(start_date, end_date, 4) expect(@reservation).must_be_kind_of Hotel::Reservation end @@ -25,7 +25,7 @@ it "returns the total amount of the new reservation" do start_date = Date.new(2015, 03, 01) end_date = Date.new(2015, 03, 05) - @reservation = Hotel::Reservation.new(start_date, end_date) + @reservation = Hotel::Reservation.new(start_date, end_date, 3) start_date = @reservation.start_date end_date = @reservation.end_date From 8792c9886f900c153cc6ffac66026a67f95a24cc Mon Sep 17 00:00:00 2001 From: Lindsay Terchin Date: Fri, 7 Sep 2018 15:02:10 -0700 Subject: [PATCH 26/42] created tests to check the create_date_array method --- spec/reservation_hub_spec.rb | 47 +++++++++++++++++++++++++++--------- 1 file changed, 36 insertions(+), 11 deletions(-) diff --git a/spec/reservation_hub_spec.rb b/spec/reservation_hub_spec.rb index f97c02203..9c7131cfb 100644 --- a/spec/reservation_hub_spec.rb +++ b/spec/reservation_hub_spec.rb @@ -14,9 +14,12 @@ end_date2 = Date.new(2018, 04, 06) end_date3 = Date.new(2018, 02, 12) - @reservation1 = Hotel::Reservation.new(start_date1, end_date1, 3) - @reservation2 = Hotel::Reservation.new(start_date2, end_date2, 4) - @reservation3 = Hotel::Reservation.new(start_date3, end_date3, 5) + + + # + # @reservation1 = Hotel::Reservation.new(start_date1, end_date1, 3) + # @reservation2 = Hotel::Reservation.new(start_date2, end_date2, 4) + # @reservation3 = Hotel::Reservation.new(start_date3, end_date3, 5) # binding.pry end @@ -29,7 +32,7 @@ end it "can access all rooms in the hotel" do - expect(@room_bookings.keys.length).must_equal 20 + expect(@reservation_hub.room_bookings.keys.length).must_equal 20 end it "must initialize with an empty array of reservations" do @@ -41,18 +44,25 @@ describe "add reservation" do + before do + @reservation1 = @reservation_hub.add_reservation(2018,06,12,2018,07,01) - # before do - # @reservation_hub = Hotel::ReservationHub.new - # end + @reservation2 = @reservation_hub.add_reservation(2017,01,02,2017,01,06) - it "adds a new reservation to array of all reservations" do + @reservation3 = @reservation_hub.add_reservation(2018,06,12,2018,06,15) @reservation4 = @reservation_hub.add_reservation(2018,06,12,2018,07,01) + end + + + it "returns an array of reservations" do + expect(@reservation4).must_be_kind_of Array + end + + it "adds a new reservation to array of all reservations" do - @reservation4 = @reservation_hub.add_reservation(2017,01,02,2017,01,06) + expect(@reservation_hub.reservations.length).must_equal 4 - expect(@reservation_hub.reservations.length).must_equal 2 end end @@ -62,6 +72,8 @@ date = Date.new(2018,1,3) expect(date).must_be_kind_of Date end + + end @@ -107,8 +119,21 @@ end end -end + describe "create date array" do + + it "returns an array of all dates within a reservation, excluding the start date" do + + start_date = Date.new(2018,01,06) + end_date = Date.new(2018,01,18) + date_array = @reservation_hub.create_date_array(start_date, end_date) + expect(date_array).must_be_kind_of Array + expect(date_array.length).must_equal 12 + + end + + end +end From 276c23e9ee456a3ee562c1178e9a33ecf96f5420 Mon Sep 17 00:00:00 2001 From: Lindsay Terchin Date: Fri, 7 Sep 2018 15:45:38 -0700 Subject: [PATCH 27/42] created method to assign an available room to a reservation --- lib/reservation_hub.rb | 46 +++++++++++++++++++++++++++++++++--------- 1 file changed, 37 insertions(+), 9 deletions(-) diff --git a/lib/reservation_hub.rb b/lib/reservation_hub.rb index 87cc8434b..c6dd39e38 100644 --- a/lib/reservation_hub.rb +++ b/lib/reservation_hub.rb @@ -22,7 +22,7 @@ def add_reservation(start_year, start_month, start_day, end_year, end_month, end end_date = generate_date(end_year, end_month, end_day) - room_id = check_available_rooms(start_date, end_date) + room_id = assign_room(start_date, end_date) reservation = Reservation.new(start_date, end_date, room_id) @@ -49,29 +49,57 @@ def create_date_array(start_date, end_date) def check_available_rooms(start_date, end_date) reservation_dates = create_date_array(start_date, end_date) - @room_bookings.each do |room| + available_rooms = [] + + @room_bookings.each do |room, dates| reservation_dates.each do |date| # binding.pry - if room.include?(date) + if @room_bookings[room].include?(date) break end end + # binding.pry - room_id = assign_room(room, reservation_dates) - return room_id + # room_id = assign_room(room, reservation_dates) + # return room_id + available_rooms << room end - - return "I'm sorry but there are no rooms available for your dates." + # + # return "I'm sorry but there are no rooms available for your dates." + return available_rooms end - def assign_room(room_id, reservation_dates) - @room_bookings[room_id] << reservation_dates + def assign_room(start_date, end_date) + + available_rooms = check_available_rooms(start_date, end_date) + + return "I'm sorry but there are no rooms available for your dates" if available_rooms.length == 0 + + reservation_dates = create_date_array(start_date, end_date) + + room_id = available_rooms[0] + + reservation_dates.each{|date| @room_bookings[room_id] << date} + + # @room_bookings[room_id] << reservation_dates + # binding.pry return room_id end + +# hash1 = {1 => ["hi", "bye"]} +# +# array = ["hello", "goodbye"] +# +# array.each{|x| hash1[1] << x} +# hash1 => {1=>["hi", "bye", "hello", "goodbye"]} + + + + def find_reservations(year, month, day) start_date = generate_date(year, month, day) From a9ecf7bfcd9ba05933d6f48b7ecae4d0702d8ca3 Mon Sep 17 00:00:00 2001 From: Lindsay Terchin Date: Sat, 8 Sep 2018 15:51:42 -0700 Subject: [PATCH 28/42] fixed assign_room method --- lib/reservation_hub.rb | 51 ++++++++++++++++++++++++------------------ 1 file changed, 29 insertions(+), 22 deletions(-) diff --git a/lib/reservation_hub.rb b/lib/reservation_hub.rb index c6dd39e38..7a9320295 100644 --- a/lib/reservation_hub.rb +++ b/lib/reservation_hub.rb @@ -5,6 +5,8 @@ module Hotel class ReservationHub + class NoRoomsAvailableError < StandardError; end + attr_reader :start_date, :end_date, :rooms, :reservations, :room_bookings # ROOMS = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] @@ -24,6 +26,10 @@ def add_reservation(start_year, start_month, start_day, end_year, end_month, end room_id = assign_room(start_date, end_date) + if room_id.nil? + raise NoRoomsAvailableError, "No rooms!" + end + reservation = Reservation.new(start_date, end_date, room_id) @reservations << reservation @@ -36,6 +42,7 @@ def generate_date(year, month, day) def create_date_array(start_date, end_date) + # binding.pry number_of_nights = (end_date - start_date).to_i date_array = [] number_of_nights.times do @@ -46,37 +53,46 @@ def create_date_array(start_date, end_date) end + # def check_available_rooms(start_date, end_date) + # reservation_dates = create_date_array(start_date, end_date) + # + # available_rooms = [] + # + # @room_bookings.each do |room, dates| + # reservation_dates.each do |date| + # + # if @room_bookings[room].include?(date) == false + # available_rooms << room + # end + # end + # end + + def check_available_rooms(start_date, end_date) reservation_dates = create_date_array(start_date, end_date) available_rooms = [] @room_bookings.each do |room, dates| - reservation_dates.each do |date| - # binding.pry + overlap = reservation_dates & dates - if @room_bookings[room].include?(date) - break - end + if overlap.length == 0 + available_rooms << room end - # binding.pry - # room_id = assign_room(room, reservation_dates) - # return room_id - available_rooms << room end - # - # return "I'm sorry but there are no rooms available for your dates." return available_rooms end + # return available_rooms + # end + + def assign_room(start_date, end_date) available_rooms = check_available_rooms(start_date, end_date) - return "I'm sorry but there are no rooms available for your dates" if available_rooms.length == 0 - reservation_dates = create_date_array(start_date, end_date) room_id = available_rooms[0] @@ -90,15 +106,6 @@ def assign_room(start_date, end_date) -# hash1 = {1 => ["hi", "bye"]} -# -# array = ["hello", "goodbye"] -# -# array.each{|x| hash1[1] << x} -# hash1 => {1=>["hi", "bye", "hello", "goodbye"]} - - - def find_reservations(year, month, day) From 18c4d1539bfd7047861b598f9b46dcd23af7b763 Mon Sep 17 00:00:00 2001 From: Lindsay Terchin Date: Sat, 8 Sep 2018 16:24:57 -0700 Subject: [PATCH 29/42] overall cleanup before wave 3 --- lib/reservation.rb | 27 ------------ lib/reservation_hub.rb | 36 +++------------ spec/reservation_hub_spec.rb | 85 ++++++++++++++++++++---------------- spec/reservation_spec.rb | 12 +---- 4 files changed, 53 insertions(+), 107 deletions(-) diff --git a/lib/reservation.rb b/lib/reservation.rb index 9ad351ed2..d98224f97 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -17,9 +17,6 @@ def initialize(start_date, end_date, room_id) @total_cost = reservation_cost(@start_date, @end_date) - - # @room_id = Hotel::RoomBooking.find_room_id(start_date, end_date) - validate_dates end @@ -35,35 +32,11 @@ def validate_dates end - #create array for all nights in the reservation - # def create_date_array(start_date, end_date) - # number_of_nights = (end_date - start_date).to_i - # date_array = [] - # number_of_nights.times do - # date_array << start_date - # start_date +=1 - # end - # return date_array - # end - - - - # def generate_date(year, month, day) - # return Date.new(year, month, day) - # end - def reservation_cost(start_date, end_date) nightly_cost = 200 total_days = (end_date - start_date).to_i total_cost = total_days * nightly_cost end - - def find_room(start_date, end_date) - - - end - - end end diff --git a/lib/reservation_hub.rb b/lib/reservation_hub.rb index 7a9320295..3c5e0b527 100644 --- a/lib/reservation_hub.rb +++ b/lib/reservation_hub.rb @@ -7,9 +7,8 @@ module Hotel class ReservationHub class NoRoomsAvailableError < StandardError; end - attr_reader :start_date, :end_date, :rooms, :reservations, :room_bookings + attr_reader :reservations, :room_bookings - # ROOMS = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] def initialize @reservations = [] @@ -17,7 +16,6 @@ def initialize end - def add_reservation(start_year, start_month, start_day, end_year, end_month, end_day) start_date = generate_date(start_year, start_month, start_day) @@ -26,10 +24,6 @@ def add_reservation(start_year, start_month, start_day, end_year, end_month, end room_id = assign_room(start_date, end_date) - if room_id.nil? - raise NoRoomsAvailableError, "No rooms!" - end - reservation = Reservation.new(start_date, end_date, room_id) @reservations << reservation @@ -42,7 +36,6 @@ def generate_date(year, month, day) def create_date_array(start_date, end_date) - # binding.pry number_of_nights = (end_date - start_date).to_i date_array = [] number_of_nights.times do @@ -53,21 +46,6 @@ def create_date_array(start_date, end_date) end - # def check_available_rooms(start_date, end_date) - # reservation_dates = create_date_array(start_date, end_date) - # - # available_rooms = [] - # - # @room_bookings.each do |room, dates| - # reservation_dates.each do |date| - # - # if @room_bookings[room].include?(date) == false - # available_rooms << room - # end - # end - # end - - def check_available_rooms(start_date, end_date) reservation_dates = create_date_array(start_date, end_date) @@ -85,28 +63,24 @@ def check_available_rooms(start_date, end_date) end - # return available_rooms - # end - - def assign_room(start_date, end_date) available_rooms = check_available_rooms(start_date, end_date) reservation_dates = create_date_array(start_date, end_date) + if available_rooms == nil + raise NoRoomsAvailableError, "No rooms are available." + end + room_id = available_rooms[0] reservation_dates.each{|date| @room_bookings[room_id] << date} - # @room_bookings[room_id] << reservation_dates - # binding.pry return room_id end - - def find_reservations(year, month, day) start_date = generate_date(year, month, day) diff --git a/spec/reservation_hub_spec.rb b/spec/reservation_hub_spec.rb index 9c7131cfb..c44d15a5d 100644 --- a/spec/reservation_hub_spec.rb +++ b/spec/reservation_hub_spec.rb @@ -13,15 +13,6 @@ end_date1 = Date.new(2018, 01, 07) end_date2 = Date.new(2018, 04, 06) end_date3 = Date.new(2018, 02, 12) - - - - # - # @reservation1 = Hotel::Reservation.new(start_date1, end_date1, 3) - # @reservation2 = Hotel::Reservation.new(start_date2, end_date2, 4) - # @reservation3 = Hotel::Reservation.new(start_date3, end_date3, 5) - # binding.pry - end @@ -40,6 +31,14 @@ expect(@reservation_hub.reservations.length).must_equal 0 end + + it "must initialize with a hash and cooresponding empty arrays for each room" do + expect(@reservation_hub.room_bookings).must_be_kind_of Hash + + expect(@reservation_hub.room_bookings.length).must_equal 20 + + expect(@reservation_hub.room_bookings[0]).must_equal nil + end end @@ -60,9 +59,7 @@ end it "adds a new reservation to array of all reservations" do - expect(@reservation_hub.reservations.length).must_equal 4 - end end @@ -72,8 +69,6 @@ date = Date.new(2018,1,3) expect(date).must_be_kind_of Date end - - end @@ -131,45 +126,59 @@ expect(date_array.length).must_equal 12 end + end + + + describe "check available rooms method" do + it "returns an array of all available rooms for a given date range" do + + @start_date = Date.new(2018,01,06) + @end_date = Date.new(2018,01,18) + + available_rooms = @reservation_hub.check_available_rooms(@start_date, @end_date) + + expect(available_rooms.length).must_equal 20 + expect(available_rooms).must_be_kind_of Array + end end -end + describe "assign room" do + before do + @start_date = Date.new(2018,01,06) + @end_date = Date.new(2018,01,18) - # it "will add the new reservation to the array" do - # # binding.pry - # # @reservations << @reservation1 - # expect(@reservations).must_include @reservation1 - # end + available_rooms = @reservation_hub.check_available_rooms(@start_date, @end_date) + end - # it 'will return all reservations' do - # expect(@reservations.length).must_equal 2 - # end + it "will assign the first value in the array to the reservation" do + room_id = @reservation_hub.assign_room(@start_date, @end_date) - # it 'will return an array of all reservations' do - # reservations = Hotel::Reservation.all - # expect(reservations).must_be_kind_of Array - # end + expect(room_id).must_equal 1 + end - # it "each item in array is a Reservation instance" do - # @reservations - # end + it "won't duplicate room ids if two reservations share the same dates" do + room_id = @reservation_hub.assign_room(@start_date, @end_date) + room_id2 = @reservation_hub.assign_room(@start_date, @end_date) + + expect(room_id2.must_equal 2) + end - # it "Returns an array of all customers" do - # customers = Customer.all - # - # expect(customers.length).must_equal 35 - # customers.each do |c| - # expect(c).must_be_kind_of Customer - # end - # end + it "raises an error if the hotel is fully booked" do + 20.times do + @reservation_hub.add_reservation(2018,01,06,2018,01,18) + end + expect{@reservation_hub.add_reservation(2018,01,06,2018,01,18)}.must_raise StandardError + end -# end + end + +end diff --git a/spec/reservation_spec.rb b/spec/reservation_spec.rb index 43bb25952..4b4d27c57 100644 --- a/spec/reservation_spec.rb +++ b/spec/reservation_spec.rb @@ -12,6 +12,7 @@ expect(@reservation).must_be_kind_of Hotel::Reservation end + it "must throw an argument error if the end date is before the start date" do start_date = Date.new(2015, 03, 05) end_date = Date.new(2015, 03, 01) @@ -20,7 +21,6 @@ end - describe "reservation cost" do it "returns the total amount of the new reservation" do start_date = Date.new(2015, 03, 01) @@ -35,13 +35,3 @@ end end - - - -#As an administrator, I can access the list of all of the rooms in the hotel -#As an administrator, I can reserve a room for a given date range -#As an administrator, I can access the list of reservations for a specific date -#def - find reservations (find all?) (date) -#return array of reservations - -#As an administrator, I can get the total cost for a given reservation From 8af6d6aee839200c573bf2b0bf0965f4dce25e75 Mon Sep 17 00:00:00 2001 From: Lindsay Terchin Date: Sun, 9 Sep 2018 23:31:34 -0700 Subject: [PATCH 30/42] overall cleanup --- lib/reservation.rb | 3 - lib/room_block.rb | 25 ++++++++ spec/reservation_hub_spec.rb | 116 +++++++++++++++++++---------------- spec/room_block_spec.rb | 26 ++++++++ spec/room_booking_spec.rb | 2 - 5 files changed, 114 insertions(+), 58 deletions(-) create mode 100644 lib/room_block.rb create mode 100644 spec/room_block_spec.rb delete mode 100644 spec/room_booking_spec.rb diff --git a/lib/reservation.rb b/lib/reservation.rb index d98224f97..55c88dd03 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -10,11 +10,8 @@ class Reservation def initialize(start_date, end_date, room_id) @start_date = start_date - @end_date = end_date - @room_id = room_id - @total_cost = reservation_cost(@start_date, @end_date) validate_dates diff --git a/lib/room_block.rb b/lib/room_block.rb new file mode 100644 index 000000000..39e4c3235 --- /dev/null +++ b/lib/room_block.rb @@ -0,0 +1,25 @@ +# require 'date' +# require 'pry' +# +# module Hotel +# class RoomBlock +# +# attr_reader :room_ids +# +# def initialize(block_id, start_date, end_date, room_ids) +# +# @block_id = block_id +# @start_date = start_date +# @end_date = end_date +# @room_ids = room_ids +# +# end +# +# +# def validate_dates +# +# end +# +# +# end +# end diff --git a/spec/reservation_hub_spec.rb b/spec/reservation_hub_spec.rb index c44d15a5d..7d49808c5 100644 --- a/spec/reservation_hub_spec.rb +++ b/spec/reservation_hub_spec.rb @@ -5,17 +5,8 @@ before do @reservation_hub = Hotel::ReservationHub.new - - start_date1 = Date.new(2018, 01, 03) - start_date2 = Date.new(2018, 04, 03) - start_date3 = Date.new(2018, 02, 04) - - end_date1 = Date.new(2018, 01, 07) - end_date2 = Date.new(2018, 04, 06) - end_date3 = Date.new(2018, 02, 12) end - describe "Reservation Hub initialization" do it "is an instance of Reservation Hub" do @@ -32,7 +23,7 @@ expect(@reservation_hub.reservations.length).must_equal 0 end - it "must initialize with a hash and cooresponding empty arrays for each room" do + it "must initialize with a hash and corresponding empty arrays for each room" do expect(@reservation_hub.room_bookings).must_be_kind_of Hash expect(@reservation_hub.room_bookings.length).must_equal 20 @@ -72,48 +63,6 @@ end - describe "find reservation" do - - before do - @reservation_hub = Hotel::ReservationHub.new - - @reservation_hub.add_reservation(2018, 01, 03, 2018, 01, 07) - @reservation_hub.add_reservation(2018, 04, 03, 2018, 04, 06) - @reservation_hub.add_reservation(2018, 01, 03, 2018, 02, 12) - end - - - it "returns a list of reservations based on a start date" do - - reservations = @reservation_hub.find_reservations(2018, 04, 03) - - expect(reservations).must_be_kind_of Array - - expect(reservations.length).must_equal 1 - end - end - - describe "all reservations" do - - before do - @reservation_hub = Hotel::ReservationHub.new - - @reservation_hub.add_reservation(2018, 01, 03, 2018, 01, 07) - @reservation_hub.add_reservation(2018, 04, 03, 2018, 04, 06) - @reservation_hub.add_reservation(2018, 01, 03, 2018, 02, 12) - end - - it "returns an array of all reservations" do - reservations = @reservation_hub.all_reservations - - expect(reservations.length).must_equal 3 - - reservations.each do |res| - expect(res).must_be_kind_of Hotel::Reservation - end - end - end - describe "create date array" do it "returns an array of all dates within a reservation, excluding the start date" do @@ -141,9 +90,17 @@ expect(available_rooms.length).must_equal 20 expect(available_rooms).must_be_kind_of Array end - end + it "doesn't include rooms that are not available" do + @start_date = Date.new(2018,01,06) + @end_date = Date.new(2018,01,18) + + @reservation1 = @reservation_hub.add_reservation(@start_date.year, @start_date.month, @start_date.day, @end_date.year, @end_date.month, @end_date.day) + available_rooms = @reservation_hub.check_available_rooms(@start_date, @end_date) + expect(available_rooms.length).must_equal 19 + end + end describe "assign room" do @@ -178,7 +135,60 @@ expect{@reservation_hub.add_reservation(2018,01,06,2018,01,18)}.must_raise StandardError end + end + + describe "find reservation" do + + before do + @reservation_hub = Hotel::ReservationHub.new + + @reservation_hub.add_reservation(2018, 01, 03, 2018, 01, 07) + @reservation_hub.add_reservation(2018, 04, 03, 2018, 04, 06) + @reservation_hub.add_reservation(2018, 01, 03, 2018, 02, 12) + end + + + it "returns a list of reservations based on a start date" do + + reservations = @reservation_hub.find_reservations(2018, 04, 03) + + expect(reservations).must_be_kind_of Array + + expect(reservations.length).must_equal 1 + end + end + + + describe "all reservations" do + + before do + @reservation_hub = Hotel::ReservationHub.new + + @reservation_hub.add_reservation(2018, 01, 03, 2018, 01, 07) + @reservation_hub.add_reservation(2018, 04, 03, 2018, 04, 06) + @reservation_hub.add_reservation(2018, 01, 03, 2018, 02, 12) + end + + it "returns an array of all reservations" do + reservations = @reservation_hub.all_reservations + + expect(reservations.length).must_equal 3 + + reservations.each do |res| + expect(res).must_be_kind_of Hotel::Reservation + end + end end + + + + + + + + + + end diff --git a/spec/room_block_spec.rb b/spec/room_block_spec.rb new file mode 100644 index 000000000..5a7cae0c6 --- /dev/null +++ b/spec/room_block_spec.rb @@ -0,0 +1,26 @@ +# require_relative 'spec_helper' +# require 'pry' +# +# describe "Room Block class" do +# +# describe "Room Block initialization" do +# +# it "is an instance of a Room Block" do +# +# block_id = 1 +# start_date = Date.new(2015, 03, 01) +# end_date = Date.new(2015, 03, 05) +# room_ids = [1,2,3,4] +# +# @room_block = Hotel::RoomBlock.new(4, start_date, end_date, room_ids) +# expect(@room_block).must_be_kind_of Hotel::RoomBlock +# end +# +# it "must throw an argument error if the end date is before the start date" do +# start_date = Date.new(2015, 03, 05) +# end_date = Date.new(2015, 03, 01) +# expect {Hotel::RoomBlock.new(start_date, end_date)}.must_raise ArgumentError +# end +# end +# +# end diff --git a/spec/room_booking_spec.rb b/spec/room_booking_spec.rb deleted file mode 100644 index 1a8982905..000000000 --- a/spec/room_booking_spec.rb +++ /dev/null @@ -1,2 +0,0 @@ -require_relative 'spec_helper' -require 'pry' From d29022f4d69ef664bdc329ba39c04158e13b4069 Mon Sep 17 00:00:00 2001 From: Lindsay Terchin Date: Sun, 9 Sep 2018 23:35:04 -0700 Subject: [PATCH 31/42] more cleanup --- lib/reservation.rb | 1 - lib/room.rb | 45 ------------------- lib/room_block.rb | 25 ----------- lib/room_booking.rb | 86 ------------------------------------ spec/reservation_hub_spec.rb | 12 ----- spec/room_block_spec.rb | 26 ----------- spec/room_spec.rb | 61 ------------------------- spec/spec_helper.rb | 1 - 8 files changed, 257 deletions(-) delete mode 100644 lib/room.rb delete mode 100644 lib/room_block.rb delete mode 100644 lib/room_booking.rb delete mode 100644 spec/room_block_spec.rb delete mode 100644 spec/room_spec.rb diff --git a/lib/reservation.rb b/lib/reservation.rb index 55c88dd03..f5340983f 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -19,7 +19,6 @@ def initialize(start_date, end_date, room_id) end - #make sure there are valid start/end dates def validate_dates raise ArgumentError.new("The end date must be after the start date") if @end_date <= @start_date diff --git a/lib/room.rb b/lib/room.rb deleted file mode 100644 index 30de57806..000000000 --- a/lib/room.rb +++ /dev/null @@ -1,45 +0,0 @@ -# module Hotel -# class Room -# -# require_relative 'reservation' -# -# module Hotel -# class RoomBooking -# -# ROOMS = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] -# -# def initialize(start_date, end_date) -# -# @start_date = start_date -# @end_date = end_date -# @booked_dates = [] -# -# end -# - - # def initialize(room_id, status = :AVAILABLE) - # @room_id = room_id - # @status = status - # - # validate_room_id - # validate_status - # - # end - - - # def validate_status -# status_options = [:AVAILABLE, :UNAVAILABLE] -# -# raise ArgumentError.new("You must enter a valid status.") unless status_options.include?(status) -# end -# -# -# def validate_room_id -# room_id_options = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] -# -# raise ArgumentError.new("You must enter a valid room id.") unless room_id_options.include?(room_id) -# end -# -# -# end -# end diff --git a/lib/room_block.rb b/lib/room_block.rb deleted file mode 100644 index 39e4c3235..000000000 --- a/lib/room_block.rb +++ /dev/null @@ -1,25 +0,0 @@ -# require 'date' -# require 'pry' -# -# module Hotel -# class RoomBlock -# -# attr_reader :room_ids -# -# def initialize(block_id, start_date, end_date, room_ids) -# -# @block_id = block_id -# @start_date = start_date -# @end_date = end_date -# @room_ids = room_ids -# -# end -# -# -# def validate_dates -# -# end -# -# -# end -# end diff --git a/lib/room_booking.rb b/lib/room_booking.rb deleted file mode 100644 index 373438593..000000000 --- a/lib/room_booking.rb +++ /dev/null @@ -1,86 +0,0 @@ -# require 'date' -# require 'pry' -# -# require_relative 'reservation' -# -# module Hotel -# class RoomBooking -# -# ROOMS = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] -# -# def initialize(start_date, end_date) -# -# @start_date = start_date -# @end_date = end_date -# @room_id = find_room_id(@start_date, @end_date) -# @booked_dates = {} -# -# end -# -# -# def create_date_array(start_date, end_date) -# number_of_nights = (end_date - start_date).to_i -# date_array = [] -# number_of_nights.times do -# date_array << start_date -# start_date +=1 -# end -# return date_array -# end -# -# -# def find_room_id(start_date, end_date) -# -# reservation_dates = create_date_array(start_date, end_date) -# -# ROOMS.each do |room| -# -# if @booked_dates.key?(room) == false -# add_booked_dates(room, reservation_dates) -# return room -# end -# -# reservation_dates.each do |date| -# if booked_dates.include?(date) -# break -# else -# add_booked_dates(room, reservation_dates) -# return room -# end -# end -# end -# return "I'm sorry but there are no rooms available for your dates." -# end -# -# -# def add_booked_dates(room_id, reservation_dates) -# @booked_dates[:room_id] = reservation_dates -# return room_id -# end -# -# -# -# -# -# -# -# -# -# #this method checks to see if there is a key/value pair for a given room number (so no booked dates) -# # def never_been_booked?(room, reservation_dates) -# # if @booked_dates.has_key?(room) == false -# # return true -# # end -# # end -# -# #this method is assuming a room already has some dates assigned to it -# # def check_each_date(reservation_dates, booked) -# # reservation_dates.each do |date| -# # if booked_dates.include?(date) -# # break -# # end -# # end -# -# -# end -# end diff --git a/spec/reservation_hub_spec.rb b/spec/reservation_hub_spec.rb index 7d49808c5..52c5c6f12 100644 --- a/spec/reservation_hub_spec.rb +++ b/spec/reservation_hub_spec.rb @@ -44,7 +44,6 @@ @reservation4 = @reservation_hub.add_reservation(2018,06,12,2018,07,01) end - it "returns an array of reservations" do expect(@reservation4).must_be_kind_of Array end @@ -148,7 +147,6 @@ @reservation_hub.add_reservation(2018, 01, 03, 2018, 02, 12) end - it "returns a list of reservations based on a start date" do reservations = @reservation_hub.find_reservations(2018, 04, 03) @@ -181,14 +179,4 @@ end end - - - - - - - - - - end diff --git a/spec/room_block_spec.rb b/spec/room_block_spec.rb deleted file mode 100644 index 5a7cae0c6..000000000 --- a/spec/room_block_spec.rb +++ /dev/null @@ -1,26 +0,0 @@ -# require_relative 'spec_helper' -# require 'pry' -# -# describe "Room Block class" do -# -# describe "Room Block initialization" do -# -# it "is an instance of a Room Block" do -# -# block_id = 1 -# start_date = Date.new(2015, 03, 01) -# end_date = Date.new(2015, 03, 05) -# room_ids = [1,2,3,4] -# -# @room_block = Hotel::RoomBlock.new(4, start_date, end_date, room_ids) -# expect(@room_block).must_be_kind_of Hotel::RoomBlock -# end -# -# it "must throw an argument error if the end date is before the start date" do -# start_date = Date.new(2015, 03, 05) -# end_date = Date.new(2015, 03, 01) -# expect {Hotel::RoomBlock.new(start_date, end_date)}.must_raise ArgumentError -# end -# end -# -# end diff --git a/spec/room_spec.rb b/spec/room_spec.rb deleted file mode 100644 index 331081618..000000000 --- a/spec/room_spec.rb +++ /dev/null @@ -1,61 +0,0 @@ -# require_relative 'spec_helper' -# require 'pry' -# -# describe 'Room class' do -# -# describe "initialize" do -# -# it "is an instance of Room" do -# @room = Hotel::Room.new(2) -# expect(@room).must_be_kind_of Hotel::Room -# end -# -# -# it "contains a valid status" do -# valid_statuses = [:AVAILABLE, :UNAVAILABLE] -# valid_statuses.each do |status| -# @room = Hotel::Room.new(2, status) -# expect(@room.status).must_equal status -# end -# end -# -# -# it "sets the status to :AVAILABLE if no status is given" do -# @room = Hotel::Room.new(2) -# expect(@room.status).must_equal :AVAILABLE -# end -# -# -# it "must throw an argument error if an invalid status is given" do -# expect {Hotel::Room.new(2, :HUH)}.must_raise ArgumentError -# end -# -# -# it "accepts all valid ids" do -# valid_room_ids = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] -# -# valid_room_ids.each do |id| -# room = Hotel::Room.new(id) -# expect(room.room_id).must_equal id -# end -# end -# -# -# it "must throw an argument error if an invalid id is given" do -# bogus_room_ids = [53, 0, 'cool', -5, nil, :cool] -# bogus_room_ids.each do |id| -# expect {Hotel::Room.new(id) -# }.must_raise ArgumentError -# end -# end -# -# -# -# -# -# -# end -# -# -# -# end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 8c7888664..ba258ac28 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -8,6 +8,5 @@ Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new -require_relative '../lib/room' require_relative '../lib/reservation' require_relative '../lib/reservation_hub' From 592640defe1d7cd7758a5084a54652a081d5b60e Mon Sep 17 00:00:00 2001 From: Lindsay Terchin Date: Mon, 10 Sep 2018 11:12:15 -0700 Subject: [PATCH 32/42] more cleanup: --- lib/reservation.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/reservation.rb b/lib/reservation.rb index f5340983f..455d0df47 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -1,7 +1,6 @@ require 'date' require 'pry' -require_relative 'room_booking' module Hotel class Reservation From df9a296d6c750791a896a75ea4628ba34f3624d5 Mon Sep 17 00:00:00 2001 From: Lindsay Terchin Date: Wed, 12 Sep 2018 10:15:07 -0700 Subject: [PATCH 33/42] simplified date formats --- lib/reservation_hub.rb | 22 ++++----- spec/reservation_hub_spec.rb | 89 ++++++++++++++++++++++++------------ 2 files changed, 70 insertions(+), 41 deletions(-) diff --git a/lib/reservation_hub.rb b/lib/reservation_hub.rb index 3c5e0b527..542f7cff4 100644 --- a/lib/reservation_hub.rb +++ b/lib/reservation_hub.rb @@ -16,24 +16,22 @@ def initialize end - def add_reservation(start_year, start_month, start_day, end_year, end_month, end_day) - - start_date = generate_date(start_year, start_month, start_day) - - end_date = generate_date(end_year, end_month, end_day) + def add_reservation(start_date, end_date) room_id = assign_room(start_date, end_date) reservation = Reservation.new(start_date, end_date, room_id) @reservations << reservation - end - - def generate_date(year, month, day) - return Date.new(year, month, day) + return reservation end + # + # def generate_date(year, month, day) + # return Date.new(year, month, day) + # end + def create_date_array(start_date, end_date) number_of_nights = (end_date - start_date).to_i @@ -81,14 +79,14 @@ def assign_room(start_date, end_date) end - def find_reservations(year, month, day) + def find_reservations(date) - start_date = generate_date(year, month, day) reservations_by_date = [] index = 0 all_reservations.each do - if all_reservations[index].start_date == start_date + # if all_reservation[index].dates.include?(date) + if all_reservations[index].start_date == date reservations_by_date << all_reservations[index] end diff --git a/spec/reservation_hub_spec.rb b/spec/reservation_hub_spec.rb index 52c5c6f12..34c40ec88 100644 --- a/spec/reservation_hub_spec.rb +++ b/spec/reservation_hub_spec.rb @@ -35,13 +35,22 @@ describe "add reservation" do before do - @reservation1 = @reservation_hub.add_reservation(2018,06,12,2018,07,01) + start_date1 = Date.new(2018,01,03) + end_date1 = Date.new(2018,01,06) + start_date2 = Date.new(2018,02,05) + end_date2 = Date.new(2018,02,15) + start_date3 = Date.new(2018,01,05) + end_date3 = Date.new(2018,01,21) + start_date4 = Date.new(2018,03,05) + end_date4 = Date.new(2018,03,11) + @reservation1 = @reservation_hub.add_reservation(start_date1, end_date1) - @reservation2 = @reservation_hub.add_reservation(2017,01,02,2017,01,06) + @reservation2 = @reservation_hub.add_reservation(start_date2, end_date2) - @reservation3 = @reservation_hub.add_reservation(2018,06,12,2018,06,15) + @reservation3 = @reservation_hub.add_reservation(start_date3, end_date3) - @reservation4 = @reservation_hub.add_reservation(2018,06,12,2018,07,01) + @reservation4 = @reservation_hub.add_reservation(start_date4, end_date4) + binding.pry end it "returns an array of reservations" do @@ -53,13 +62,13 @@ end end - - describe "Generate date" do - it "is a kind of date" do - date = Date.new(2018,1,3) - expect(date).must_be_kind_of Date - end - end + # + # describe "Generate date" do + # it "is a kind of date" do + # date = Date.new(2018,1,3) + # expect(date).must_be_kind_of Date + # end + # end describe "create date array" do @@ -81,22 +90,22 @@ it "returns an array of all available rooms for a given date range" do - @start_date = Date.new(2018,01,06) - @end_date = Date.new(2018,01,18) + start_date = Date.new(2018,01,06) + end_date = Date.new(2018,01,18) - available_rooms = @reservation_hub.check_available_rooms(@start_date, @end_date) + available_rooms = @reservation_hub.check_available_rooms(start_date, end_date) expect(available_rooms.length).must_equal 20 expect(available_rooms).must_be_kind_of Array end it "doesn't include rooms that are not available" do - @start_date = Date.new(2018,01,06) - @end_date = Date.new(2018,01,18) + start_date = Date.new(2018,01,06) + end_date = Date.new(2018,01,18) - @reservation1 = @reservation_hub.add_reservation(@start_date.year, @start_date.month, @start_date.day, @end_date.year, @end_date.month, @end_date.day) + @reservation1 = @reservation_hub.add_reservation(start_date, end_date) - available_rooms = @reservation_hub.check_available_rooms(@start_date, @end_date) + available_rooms = @reservation_hub.check_available_rooms(start_date, end_date) expect(available_rooms.length).must_equal 19 end end @@ -128,11 +137,15 @@ it "raises an error if the hotel is fully booked" do + start_date = Date.new(2018,01,03) + end_date = Date.new(2018,01,05) + + 20.times do - @reservation_hub.add_reservation(2018,01,06,2018,01,18) + @reservation_hub.add_reservation(start_date, end_date) end - expect{@reservation_hub.add_reservation(2018,01,06,2018,01,18)}.must_raise StandardError + expect{@reservation_hub.add_reservation(start_date, end_date)}.must_raise StandardError end end @@ -140,20 +153,31 @@ describe "find reservation" do before do - @reservation_hub = Hotel::ReservationHub.new + start_date1 = Date.new(2018,01,03) + end_date1 = Date.new(2018,01,06) + start_date2 = Date.new(2018,02,05) + end_date2 = Date.new(2018,02,15) + start_date3 = Date.new(2018,01,11) + end_date3 = Date.new(2018,01,21) + start_date4 = Date.new(2018,01,03) + end_date4 = Date.new(2018,01,05) - @reservation_hub.add_reservation(2018, 01, 03, 2018, 01, 07) - @reservation_hub.add_reservation(2018, 04, 03, 2018, 04, 06) - @reservation_hub.add_reservation(2018, 01, 03, 2018, 02, 12) + @reservation_hub = Hotel::ReservationHub.new + @reservation_hub.add_reservation(start_date1, end_date1) + @reservation_hub.add_reservation(start_date2, end_date2) + @reservation_hub.add_reservation(start_date3, end_date3) + @reservation_hub.add_reservation(start_date4, end_date4) end it "returns a list of reservations based on a start date" do - reservations = @reservation_hub.find_reservations(2018, 04, 03) + start_date = Date.new(2018,01,03) + + reservations = @reservation_hub.find_reservations(start_date) expect(reservations).must_be_kind_of Array - expect(reservations.length).must_equal 1 + expect(reservations.length).must_equal 2 end end @@ -163,9 +187,16 @@ before do @reservation_hub = Hotel::ReservationHub.new - @reservation_hub.add_reservation(2018, 01, 03, 2018, 01, 07) - @reservation_hub.add_reservation(2018, 04, 03, 2018, 04, 06) - @reservation_hub.add_reservation(2018, 01, 03, 2018, 02, 12) + start_date1 = Date.new(2018,01,03) + end_date1 = Date.new(2018,01,06) + start_date2 = Date.new(2018,02,05) + end_date2 = Date.new(2018,02,15) + start_date3 = Date.new(2018,01,11) + end_date3 = Date.new(2018,01,21) + + @reservation_hub.add_reservation(start_date1, end_date1) + @reservation_hub.add_reservation(start_date2, end_date2) + @reservation_hub.add_reservation(start_date3, end_date3) end it "returns an array of all reservations" do From c93c962e2b560a2fe7b7b5445fd7fd0f6d896675 Mon Sep 17 00:00:00 2001 From: Lindsay Terchin Date: Wed, 12 Sep 2018 21:24:55 -0700 Subject: [PATCH 34/42] cleaned up reservation class & tests --- lib/reservation.rb | 35 ++++++++++++++++------------------- spec/reservation_spec.rb | 29 ++++++++++++++++++----------- 2 files changed, 34 insertions(+), 30 deletions(-) diff --git a/lib/reservation.rb b/lib/reservation.rb index 455d0df47..78def02e7 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -4,34 +4,31 @@ module Hotel class Reservation - attr_reader :start_date, :end_date, :room_id + attr_reader :start_date, :end_date, :room_id, :date_range, :total_cost - def initialize(start_date, end_date, room_id) + def initialize(date_range, room_id) - @start_date = start_date - @end_date = end_date + @date_range = date_range @room_id = room_id - @total_cost = reservation_cost(@start_date, @end_date) + @total_cost = reservation_cost(date_range) - validate_dates - - end - - - def validate_dates - raise ArgumentError.new("The end date must be after the start date") if @end_date <= @start_date - - raise ArgumentError.new("The start date can't be nil") if @start_date == nil - - raise ArgumentError.new("The end date can't be nil") if @end_date == nil end - - def reservation_cost(start_date, end_date) + def reservation_cost(date_range) nightly_cost = 200 - total_days = (end_date - start_date).to_i + total_days = (date_range.length).to_i total_cost = total_days * nightly_cost end end end + + + +# def validate_dates +# raise ArgumentError.new("The end date must be after the start date") if @end_date <= @start_date +# +# raise ArgumentError.new("The start date can't be nil") if @start_date == nil +# +# raise ArgumentError.new("The end date can't be nil") if @end_date == nil +# end diff --git a/spec/reservation_spec.rb b/spec/reservation_spec.rb index 4b4d27c57..74b303ebb 100644 --- a/spec/reservation_spec.rb +++ b/spec/reservation_spec.rb @@ -5,32 +5,39 @@ describe "Reservation initialization" do + before do + date_range = [1,2,3] + @reservation = Hotel::Reservation.new(date_range, 4) + end + it "is an instance of Reservation" do - start_date = Date.new(2015, 03, 01) - end_date = Date.new(2015, 03, 05) - @reservation = Hotel::Reservation.new(start_date, end_date, 4) expect(@reservation).must_be_kind_of Hotel::Reservation end + it "is initialized with a date range, room id and total cost" do + expect(@reservation).must_respond_to :date_range + expect(@reservation.date_range).must_be_kind_of Array + + expect(@reservation).must_respond_to :room_id + expect(@reservation.room_id).must_be_kind_of Integer + + expect(@reservation).must_respond_to :total_cost + expect(@reservation.total_cost).must_be_kind_of Integer - it "must throw an argument error if the end date is before the start date" do - start_date = Date.new(2015, 03, 05) - end_date = Date.new(2015, 03, 01) - expect {Hotel::Reservation.new(start_date, end_date)}.must_raise ArgumentError end end describe "reservation cost" do it "returns the total amount of the new reservation" do - start_date = Date.new(2015, 03, 01) - end_date = Date.new(2015, 03, 05) - @reservation = Hotel::Reservation.new(start_date, end_date, 3) + + date_range = [1,2,3,4] + @reservation = Hotel::Reservation.new(date_range, 3) start_date = @reservation.start_date end_date = @reservation.end_date - expect(@reservation.reservation_cost(start_date, end_date)).must_equal 800 + expect(@reservation.reservation_cost(date_range)).must_equal 800 end end From e6ac0b51e393a257d8059404a15ed107edff3a9d Mon Sep 17 00:00:00 2001 From: Lindsay Terchin Date: Thu, 13 Sep 2018 21:52:00 -0700 Subject: [PATCH 35/42] fixed tests for checking for duplicate reservations --- spec/reservation_hub_spec.rb | 246 ++++++++++++++++++++--------------- 1 file changed, 138 insertions(+), 108 deletions(-) diff --git a/spec/reservation_hub_spec.rb b/spec/reservation_hub_spec.rb index 34c40ec88..e29706d29 100644 --- a/spec/reservation_hub_spec.rb +++ b/spec/reservation_hub_spec.rb @@ -10,25 +10,45 @@ describe "Reservation Hub initialization" do it "is an instance of Reservation Hub" do + expect(@reservation_hub).must_be_kind_of Hotel::ReservationHub + end it "can access all rooms in the hotel" do + expect(@reservation_hub.room_bookings.keys.length).must_equal 20 + end it "must initialize with an empty array of reservations" do - expect(@reservation_hub.reservations).must_be_kind_of Array + expect(@reservation_hub.reservations).must_be_kind_of Array expect(@reservation_hub.reservations.length).must_equal 0 + end it "must initialize with a hash and corresponding empty arrays for each room" do - expect(@reservation_hub.room_bookings).must_be_kind_of Hash + expect(@reservation_hub.room_bookings).must_be_kind_of Hash expect(@reservation_hub.room_bookings.length).must_equal 20 + expect(@reservation_hub.room_bookings[1]).must_equal [] + + end + + it "must be able to access all reservations and room bookings" do + + expect(@reservation_hub).must_respond_to :reservations + expect(@reservation_hub).must_respond_to :room_bookings + + end + + it "raises an error if the start date is after the end date" do + + start_date = Date.new(2018,01,05) + end_date = Date.new(2018,01,02) + expect{@reservation_hub.add_reservation(start_date, end_date)}.must_raise ArgumentError - expect(@reservation_hub.room_bookings[0]).must_equal nil end end @@ -37,50 +57,55 @@ before do start_date1 = Date.new(2018,01,03) end_date1 = Date.new(2018,01,06) - start_date2 = Date.new(2018,02,05) - end_date2 = Date.new(2018,02,15) - start_date3 = Date.new(2018,01,05) - end_date3 = Date.new(2018,01,21) - start_date4 = Date.new(2018,03,05) - end_date4 = Date.new(2018,03,11) + + start_date2 = Date.new(2018,04,06) + end_date2 = Date.new(2018,04,11) + @reservation1 = @reservation_hub.add_reservation(start_date1, end_date1) @reservation2 = @reservation_hub.add_reservation(start_date2, end_date2) - @reservation3 = @reservation_hub.add_reservation(start_date3, end_date3) - - @reservation4 = @reservation_hub.add_reservation(start_date4, end_date4) - binding.pry end - it "returns an array of reservations" do - expect(@reservation4).must_be_kind_of Array + it "returns a new reservation" do + + expect(@reservation1).must_be_kind_of Hotel::Reservation + expect(@reservation2).must_be_kind_of Hotel::Reservation + end it "adds a new reservation to array of all reservations" do - expect(@reservation_hub.reservations.length).must_equal 4 + + expect(@reservation_hub.reservations.length).must_equal 2 + end end - # - # describe "Generate date" do - # it "is a kind of date" do - # date = Date.new(2018,1,3) - # expect(date).must_be_kind_of Date - # end - # end - describe "create date array" do - it "returns an array of all dates within a reservation, excluding the start date" do - + before do start_date = Date.new(2018,01,06) - end_date = Date.new(2018,01,18) - date_array = @reservation_hub.create_date_array(start_date, end_date) + @end_date = Date.new(2018,01,10) + + @date_array = @reservation_hub.create_date_array(start_date, @end_date) + + @test_dates = [] + until start_date == @end_date + @test_dates << start_date + start_date +=1 + end + + end + + it "returns an array of all dates" do + expect(@date_array).must_be_kind_of Array + expect(@date_array.length).must_equal @test_dates.length + end - expect(date_array).must_be_kind_of Array - expect(date_array.length).must_equal 12 + it "includes all dates in the reservation, not including the end date" do + expect(@date_array).wont_include @end_date + expect(@date_array).must_equal @test_dates end end @@ -88,25 +113,32 @@ describe "check available rooms method" do - it "returns an array of all available rooms for a given date range" do + it "returns an array of 20 rooms upon initialization" do - start_date = Date.new(2018,01,06) - end_date = Date.new(2018,01,18) + reservation_dates = [1,2] - available_rooms = @reservation_hub.check_available_rooms(start_date, end_date) + available_rooms = @reservation_hub.check_available_rooms(reservation_dates) expect(available_rooms.length).must_equal 20 expect(available_rooms).must_be_kind_of Array end - it "doesn't include rooms that are not available" do - start_date = Date.new(2018,01,06) - end_date = Date.new(2018,01,18) + it "doesn't include rooms that have already been booked" do + + start_date = 1 + end_date = 3 @reservation1 = @reservation_hub.add_reservation(start_date, end_date) - available_rooms = @reservation_hub.check_available_rooms(start_date, end_date) - expect(available_rooms.length).must_equal 19 + @reservation2 = @reservation_hub.add_reservation(start_date, end_date) + + @reservation_dates = [1,2] + + + available_rooms = @reservation_hub.check_available_rooms(@reservation_dates) + + expect(available_rooms.length).must_equal 18 + end end @@ -114,32 +146,30 @@ describe "assign room" do before do - - @start_date = Date.new(2018,01,06) - @end_date = Date.new(2018,01,18) - - available_rooms = @reservation_hub.check_available_rooms(@start_date, @end_date) + @reservation_dates = [1,2,3] end it "will assign the first value in the array to the reservation" do - room_id = @reservation_hub.assign_room(@start_date, @end_date) + room_id = @reservation_hub.assign_room(@reservation_dates) expect(room_id).must_equal 1 + end it "won't duplicate room ids if two reservations share the same dates" do - room_id = @reservation_hub.assign_room(@start_date, @end_date) - room_id2 = @reservation_hub.assign_room(@start_date, @end_date) + reservation_dates = [1,2,3] + room_id = @reservation_hub.assign_room(reservation_dates) + room_id2 = @reservation_hub.assign_room(reservation_dates) + expect(room_id).must_equal 1 + expect(room_id2).must_equal 2 - expect(room_id2.must_equal 2) end - it "raises an error if the hotel is fully booked" do - start_date = Date.new(2018,01,03) - end_date = Date.new(2018,01,05) + start_date = Date.new(2018,01,05) + end_date = Date.new(2018,01,06) 20.times do @reservation_hub.add_reservation(start_date, end_date) @@ -150,64 +180,64 @@ end - describe "find reservation" do - - before do - start_date1 = Date.new(2018,01,03) - end_date1 = Date.new(2018,01,06) - start_date2 = Date.new(2018,02,05) - end_date2 = Date.new(2018,02,15) - start_date3 = Date.new(2018,01,11) - end_date3 = Date.new(2018,01,21) - start_date4 = Date.new(2018,01,03) - end_date4 = Date.new(2018,01,05) - - @reservation_hub = Hotel::ReservationHub.new - @reservation_hub.add_reservation(start_date1, end_date1) - @reservation_hub.add_reservation(start_date2, end_date2) - @reservation_hub.add_reservation(start_date3, end_date3) - @reservation_hub.add_reservation(start_date4, end_date4) - end - - it "returns a list of reservations based on a start date" do - - start_date = Date.new(2018,01,03) - - reservations = @reservation_hub.find_reservations(start_date) - - expect(reservations).must_be_kind_of Array - - expect(reservations.length).must_equal 2 - end - end - - - describe "all reservations" do - - before do - @reservation_hub = Hotel::ReservationHub.new - - start_date1 = Date.new(2018,01,03) - end_date1 = Date.new(2018,01,06) - start_date2 = Date.new(2018,02,05) - end_date2 = Date.new(2018,02,15) - start_date3 = Date.new(2018,01,11) - end_date3 = Date.new(2018,01,21) - - @reservation_hub.add_reservation(start_date1, end_date1) - @reservation_hub.add_reservation(start_date2, end_date2) - @reservation_hub.add_reservation(start_date3, end_date3) - end - - it "returns an array of all reservations" do - reservations = @reservation_hub.all_reservations + # describe "find reservation" do + # + # before do + # start_date1 = Date.new(2018,01,03) + # end_date1 = Date.new(2018,01,06) + # start_date2 = Date.new(2018,02,05) + # end_date2 = Date.new(2018,02,15) + # start_date3 = Date.new(2018,01,11) + # end_date3 = Date.new(2018,01,21) + # start_date4 = Date.new(2018,01,03) + # end_date4 = Date.new(2018,01,05) + # + # @reservation_hub = Hotel::ReservationHub.new + # @reservation_hub.add_reservation(start_date1, end_date1) + # @reservation_hub.add_reservation(start_date2, end_date2) + # @reservation_hub.add_reservation(start_date3, end_date3) + # @reservation_hub.add_reservation(start_date4, end_date4) + # end + # + # it "returns a list of reservations based on a start date" do + # + # start_date = Date.new(2018,01,03) + # + # reservations = @reservation_hub.find_reservations(start_date) + # + # expect(reservations).must_be_kind_of Array + # + # expect(reservations.length).must_equal 2 + # end + # end - expect(reservations.length).must_equal 3 - reservations.each do |res| - expect(res).must_be_kind_of Hotel::Reservation - end - end - end + # describe "all reservations" do + # + # before do + # @reservation_hub = Hotel::ReservationHub.new + # + # start_date1 = Date.new(2018,01,03) + # end_date1 = Date.new(2018,01,06) + # start_date2 = Date.new(2018,02,05) + # end_date2 = Date.new(2018,02,15) + # start_date3 = Date.new(2018,01,11) + # end_date3 = Date.new(2018,01,21) + # + # @reservation_hub.add_reservation(start_date1, end_date1) + # @reservation_hub.add_reservation(start_date2, end_date2) + # @reservation_hub.add_reservation(start_date3, end_date3) + # end + # + # it "returns an array of all reservations" do + # reservations = @reservation_hub.all_reservations + # + # expect(reservations.length).must_equal 3 + # + # reservations.each do |res| + # expect(res).must_be_kind_of Hotel::Reservation + # end + # end + # end end From b40bea9c85b2bfba6386dfd9250e2c35d81ba895 Mon Sep 17 00:00:00 2001 From: Lindsay Terchin Date: Thu, 13 Sep 2018 22:30:50 -0700 Subject: [PATCH 36/42] finished updating tests for reservation hub --- spec/reservation_hub_spec.rb | 133 +++++++++++++++++++---------------- 1 file changed, 74 insertions(+), 59 deletions(-) diff --git a/spec/reservation_hub_spec.rb b/spec/reservation_hub_spec.rb index e29706d29..47d9be6dc 100644 --- a/spec/reservation_hub_spec.rb +++ b/spec/reservation_hub_spec.rb @@ -7,6 +7,7 @@ @reservation_hub = Hotel::ReservationHub.new end + describe "Reservation Hub initialization" do it "is an instance of Reservation Hub" do @@ -180,64 +181,78 @@ end - # describe "find reservation" do - # - # before do - # start_date1 = Date.new(2018,01,03) - # end_date1 = Date.new(2018,01,06) - # start_date2 = Date.new(2018,02,05) - # end_date2 = Date.new(2018,02,15) - # start_date3 = Date.new(2018,01,11) - # end_date3 = Date.new(2018,01,21) - # start_date4 = Date.new(2018,01,03) - # end_date4 = Date.new(2018,01,05) - # - # @reservation_hub = Hotel::ReservationHub.new - # @reservation_hub.add_reservation(start_date1, end_date1) - # @reservation_hub.add_reservation(start_date2, end_date2) - # @reservation_hub.add_reservation(start_date3, end_date3) - # @reservation_hub.add_reservation(start_date4, end_date4) - # end - # - # it "returns a list of reservations based on a start date" do - # - # start_date = Date.new(2018,01,03) - # - # reservations = @reservation_hub.find_reservations(start_date) - # - # expect(reservations).must_be_kind_of Array - # - # expect(reservations.length).must_equal 2 - # end - # end - - - # describe "all reservations" do - # - # before do - # @reservation_hub = Hotel::ReservationHub.new - # - # start_date1 = Date.new(2018,01,03) - # end_date1 = Date.new(2018,01,06) - # start_date2 = Date.new(2018,02,05) - # end_date2 = Date.new(2018,02,15) - # start_date3 = Date.new(2018,01,11) - # end_date3 = Date.new(2018,01,21) - # - # @reservation_hub.add_reservation(start_date1, end_date1) - # @reservation_hub.add_reservation(start_date2, end_date2) - # @reservation_hub.add_reservation(start_date3, end_date3) - # end - # - # it "returns an array of all reservations" do - # reservations = @reservation_hub.all_reservations - # - # expect(reservations.length).must_equal 3 - # - # reservations.each do |res| - # expect(res).must_be_kind_of Hotel::Reservation - # end - # end - # end + describe "find reservation" do + + before do + @start_date = Date.new(2018,01,03) + @end_date = Date.new(2018,01,06) + + @reservation_hub = Hotel::ReservationHub.new + reservation1 = @reservation_hub.add_reservation(@start_date, @end_date) + end + + it "returns an array of reservations" do + + date = Date.new(2017,01,01) + reservations = @reservation_hub.find_reservations(date) + + expect(reservations).must_be_kind_of Array + + expect(@reservation_hub.reservations[0]).must_be_kind_of Hotel::Reservation + + end + + it "returns a reservation if its start date is the parameter" do + + reservations = @reservation_hub.find_reservations(@start_date) + + expect(reservations.length).must_equal 1 + end + + it "returns a reservation if one of the middle dates is a parameter" do + date = @start_date +1 + + reservations = @reservation_hub.find_reservations(date) + + expect(reservations.length).must_equal 1 + + end + + it "does not include a reservation if its end date is a parameter" do + + reservation = @reservation_hub.find_reservations(@end_date) + + expect(reservation.length).must_equal 0 + end + end + + + describe "all reservations" do + + before do + @reservation_hub = Hotel::ReservationHub.new + + start_date1 = Date.new(2018,01,03) + end_date1 = Date.new(2018,01,06) + start_date2 = Date.new(2018,02,05) + end_date2 = Date.new(2018,02,15) + start_date3 = Date.new(2018,01,11) + end_date3 = Date.new(2018,01,21) + + @reservation_hub.add_reservation(start_date1, end_date1) + @reservation_hub.add_reservation(start_date2, end_date2) + @reservation_hub.add_reservation(start_date3, end_date3) + end + + it "returns an array of all reservations" do + reservations = @reservation_hub.all_reservations + + expect(reservations.length).must_equal 3 + + reservations.each do |res| + expect(res).must_be_kind_of Hotel::Reservation + end + end + end end From 97d7df67d485059108e374dc57b2ae3e311cceb0 Mon Sep 17 00:00:00 2001 From: Lindsay Terchin Date: Fri, 14 Sep 2018 21:43:04 -0700 Subject: [PATCH 37/42] more testing cleanup --- lib/reservation.rb | 13 ++++++------ lib/reservation_hub.rb | 46 +++++++++++++++++++++++++++------------- spec/reservation_spec.rb | 4 ++-- 3 files changed, 39 insertions(+), 24 deletions(-) diff --git a/lib/reservation.rb b/lib/reservation.rb index 78def02e7..8562edf71 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -1,22 +1,21 @@ require 'date' require 'pry' - module Hotel class Reservation - attr_reader :start_date, :end_date, :room_id, :date_range, :total_cost + attr_reader :start_date, :end_date, :room_id, :reservation_dates, :total_cost - def initialize(date_range, room_id) + def initialize(reservation_dates, room_id) - @date_range = date_range + @reservation_dates = reservation_dates @room_id = room_id - @total_cost = reservation_cost(date_range) + @total_cost = reservation_cost(reservation_dates) end - def reservation_cost(date_range) + def reservation_cost(reservation_dates) nightly_cost = 200 - total_days = (date_range.length).to_i + total_days = (reservation_dates.length).to_i total_cost = total_days * nightly_cost end diff --git a/lib/reservation_hub.rb b/lib/reservation_hub.rb index 542f7cff4..fd3524152 100644 --- a/lib/reservation_hub.rb +++ b/lib/reservation_hub.rb @@ -3,11 +3,12 @@ require_relative 'reservation' + module Hotel class ReservationHub class NoRoomsAvailableError < StandardError; end - attr_reader :reservations, :room_bookings + attr_reader :reservations, :room_bookings, :reservation_dates def initialize @@ -18,19 +19,38 @@ def initialize def add_reservation(start_date, end_date) - room_id = assign_room(start_date, end_date) + @start_date = start_date + @end_date = end_date + validate_dates + + reservation_dates = create_date_array(start_date, end_date) + + room_id = assign_room(reservation_dates) - reservation = Reservation.new(start_date, end_date, room_id) + reservation = Reservation.new(reservation_dates, room_id) @reservations << reservation return reservation end - # - # def generate_date(year, month, day) - # return Date.new(year, month, day) - # end + + def add_block_reservation(block_id) + #find room block + #room_block = something.find + + #start date and end date are returned + #start_date = room_block.start_date + #end_date = room_block.end_date + + #add_reservation(start_date, end_date) + #remove room id from room_ids array + end + + + def validate_dates + raise ArgumentError.new("The end date must be after the start date") if @end_date <= @start_date + end def create_date_array(start_date, end_date) @@ -44,8 +64,7 @@ def create_date_array(start_date, end_date) end - def check_available_rooms(start_date, end_date) - reservation_dates = create_date_array(start_date, end_date) + def check_available_rooms(reservation_dates) available_rooms = [] @@ -61,11 +80,9 @@ def check_available_rooms(start_date, end_date) end - def assign_room(start_date, end_date) - - available_rooms = check_available_rooms(start_date, end_date) + def assign_room(reservation_dates) - reservation_dates = create_date_array(start_date, end_date) + available_rooms = check_available_rooms(reservation_dates) if available_rooms == nil raise NoRoomsAvailableError, "No rooms are available." @@ -85,9 +102,8 @@ def find_reservations(date) index = 0 all_reservations.each do - # if all_reservation[index].dates.include?(date) - if all_reservations[index].start_date == date + if all_reservations[index].reservation_dates.include?(date) reservations_by_date << all_reservations[index] end index +=1 diff --git a/spec/reservation_spec.rb b/spec/reservation_spec.rb index 74b303ebb..18bae9462 100644 --- a/spec/reservation_spec.rb +++ b/spec/reservation_spec.rb @@ -15,8 +15,8 @@ end it "is initialized with a date range, room id and total cost" do - expect(@reservation).must_respond_to :date_range - expect(@reservation.date_range).must_be_kind_of Array + expect(@reservation).must_respond_to :reservation_dates + expect(@reservation.reservation_dates).must_be_kind_of Array expect(@reservation).must_respond_to :room_id expect(@reservation.room_id).must_be_kind_of Integer From ef9e46931668ce219aaaa6fd8588d32a5464c304 Mon Sep 17 00:00:00 2001 From: Lindsay Terchin Date: Sat, 15 Sep 2018 09:02:45 -0700 Subject: [PATCH 38/42] created class for room block and a method to create a new one --- lib/reservation.rb | 13 +------------ lib/reservation_hub.rb | 32 +++++++++++++++++++++++--------- lib/room_block.rb | 16 ++++++++++++++++ 3 files changed, 40 insertions(+), 21 deletions(-) create mode 100644 lib/room_block.rb diff --git a/lib/reservation.rb b/lib/reservation.rb index 8562edf71..9c6a657e3 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -1,5 +1,4 @@ -require 'date' -require 'pry' + module Hotel class Reservation @@ -21,13 +20,3 @@ def reservation_cost(reservation_dates) end end - - - -# def validate_dates -# raise ArgumentError.new("The end date must be after the start date") if @end_date <= @start_date -# -# raise ArgumentError.new("The start date can't be nil") if @start_date == nil -# -# raise ArgumentError.new("The end date can't be nil") if @end_date == nil -# end diff --git a/lib/reservation_hub.rb b/lib/reservation_hub.rb index fd3524152..9f0a12ec6 100644 --- a/lib/reservation_hub.rb +++ b/lib/reservation_hub.rb @@ -2,18 +2,20 @@ require 'pry' require_relative 'reservation' +require_relative 'room_block' module Hotel class ReservationHub class NoRoomsAvailableError < StandardError; end - attr_reader :reservations, :room_bookings, :reservation_dates + attr_reader :reservations, :room_bookings, :reservation_dates, :room_blocks def initialize @reservations = [] @room_bookings = {1 => [], 2 => [], 3 => [], 4 => [], 5 => [], 6 => [], 7 => [], 8 => [], 9 => [], 10 => [], 11 => [], 12 => [], 13 => [], 14 => [], 15 => [], 16 => [], 17 => [], 18 => [], 19 => [], 20 => []} + @room_blocks = [] end @@ -35,16 +37,28 @@ def add_reservation(start_date, end_date) end - def add_block_reservation(block_id) - #find room block - #room_block = something.find + def add_room_block(start_date, end_date, total_rooms) - #start date and end date are returned - #start_date = room_block.start_date - #end_date = room_block.end_date + @start_date = start_date + @end_date = end_date + @total_rooms = total_rooms + validate dates + + reservation_dates = create_date_array(start_date, end_date) + + room_ids = [] + + @total_rooms.each do + room_id = assign_room(reservation_dates) + room_ids << room_id + end + + block_id = @room_blocks.length += 1 + + room_block = RoomBlock.new(reservation_dates, room_ids, block_id) + + @room_blocks << room_block - #add_reservation(start_date, end_date) - #remove room id from room_ids array end diff --git a/lib/room_block.rb b/lib/room_block.rb new file mode 100644 index 000000000..b93b421d2 --- /dev/null +++ b/lib/room_block.rb @@ -0,0 +1,16 @@ + + +module Hotel + class RoomBlock + attr_reader :reservation_dates, :room_ids + + def initialize(reservation_dates, room_ids, block_id) + + @reservation_dates = reservation_dates + @room_ids = room_ids + @block_id = block_id + + end + + end +end From 7263bb06c895424b7e55987293babd8899f728b9 Mon Sep 17 00:00:00 2001 From: Lindsay Terchin Date: Sat, 15 Sep 2018 09:43:02 -0700 Subject: [PATCH 39/42] final commit with refactors.txt --- lib/reservation_hub.rb | 24 +++++++++------ lib/room_block.rb | 2 +- refactors.txt | 5 +++ spec/reservation_hub_spec.rb | 59 ++++++++++++++++++------------------ spec/room_block_spec.rb | 37 ++++++++++++++++++++++ 5 files changed, 88 insertions(+), 39 deletions(-) create mode 100644 refactors.txt create mode 100644 spec/room_block_spec.rb diff --git a/lib/reservation_hub.rb b/lib/reservation_hub.rb index 9f0a12ec6..d3b779056 100644 --- a/lib/reservation_hub.rb +++ b/lib/reservation_hub.rb @@ -42,23 +42,24 @@ def add_room_block(start_date, end_date, total_rooms) @start_date = start_date @end_date = end_date @total_rooms = total_rooms - validate dates + validate_dates reservation_dates = create_date_array(start_date, end_date) room_ids = [] - @total_rooms.each do + @total_rooms.times do room_id = assign_room(reservation_dates) room_ids << room_id end - block_id = @room_blocks.length += 1 + block_id = @room_blocks.length + 1 room_block = RoomBlock.new(reservation_dates, room_ids, block_id) @room_blocks << room_block + return room_block end @@ -115,10 +116,10 @@ def find_reservations(date) reservations_by_date = [] index = 0 - all_reservations.each do + @reservations.each do - if all_reservations[index].reservation_dates.include?(date) - reservations_by_date << all_reservations[index] + if @reservations[index].reservation_dates.include?(date) + reservations_by_date << @reservations[index] end index +=1 @@ -127,10 +128,15 @@ def find_reservations(date) end - def all_reservations - @reservations - end + def find_room_block(id) + + @room_blocks.each do |room_block| + if room_block.block_id == id + return room_block + end + end + end end end diff --git a/lib/room_block.rb b/lib/room_block.rb index b93b421d2..e3c684a99 100644 --- a/lib/room_block.rb +++ b/lib/room_block.rb @@ -2,7 +2,7 @@ module Hotel class RoomBlock - attr_reader :reservation_dates, :room_ids + attr_reader :reservation_dates, :room_ids, :block_id def initialize(reservation_dates, room_ids, block_id) diff --git a/refactors.txt b/refactors.txt new file mode 100644 index 000000000..90ae534b0 --- /dev/null +++ b/refactors.txt @@ -0,0 +1,5 @@ +**the name "room_bookings" seems a bit misleading. I used it to include both, reservations and room blocks for each room. For each room, I probably would have been better off creating a nested hash. One key/value pair would be confirmed_reservations => [] and one would be room_blocks => []. + +**I would have changed the method for adding reservations so it could also be used to add a block reservation as well. The current method assigns rooms, which is a step that wouldn't be necessary if a user entered in a block id (it already would have been handled). + +**I feel like some of my methods in reservation hub are currently doing a lot. I think the overall code would benefit by breaking some of those methods up into smaller steps. diff --git a/spec/reservation_hub_spec.rb b/spec/reservation_hub_spec.rb index 47d9be6dc..5cc7d1121 100644 --- a/spec/reservation_hub_spec.rb +++ b/spec/reservation_hub_spec.rb @@ -83,6 +83,36 @@ end + describe "add room block" do + + before do + start_date = Date.new(2018,01,03) + end_date = Date.new(2018,01,06) + total_rooms = 3 + @room_block = @reservation_hub.add_room_block(start_date, end_date, total_rooms) + end + + it "returns a new room block" do + expect(@room_block).must_be_kind_of Hotel::RoomBlock + end + + it "adds a new room block to an array of all room blocks" do + expect(@reservation_hub.room_blocks.length).must_equal 1 + end + + it "increments the block id by 1 for every new room block" do + + start_date = Date.new(2018,02,04) + end_date = Date.new(2018,02,15) + total_rooms = 2 + + @room_block2 = @reservation_hub.add_room_block(start_date, end_date, total_rooms) + + expect(@room_block2.block_id).must_equal 2 + end + end + + describe "create date array" do before do @@ -226,33 +256,4 @@ end end - - describe "all reservations" do - - before do - @reservation_hub = Hotel::ReservationHub.new - - start_date1 = Date.new(2018,01,03) - end_date1 = Date.new(2018,01,06) - start_date2 = Date.new(2018,02,05) - end_date2 = Date.new(2018,02,15) - start_date3 = Date.new(2018,01,11) - end_date3 = Date.new(2018,01,21) - - @reservation_hub.add_reservation(start_date1, end_date1) - @reservation_hub.add_reservation(start_date2, end_date2) - @reservation_hub.add_reservation(start_date3, end_date3) - end - - it "returns an array of all reservations" do - reservations = @reservation_hub.all_reservations - - expect(reservations.length).must_equal 3 - - reservations.each do |res| - expect(res).must_be_kind_of Hotel::Reservation - end - end - end - end diff --git a/spec/room_block_spec.rb b/spec/room_block_spec.rb new file mode 100644 index 000000000..e402c8a9d --- /dev/null +++ b/spec/room_block_spec.rb @@ -0,0 +1,37 @@ +require_relative 'spec_helper' +require 'pry' + +describe "Room Block class" do + + describe "room block initialization" do + + before do + reservation_dates = [1,2,3] + room_ids = [4,5,6] + block_id = 1 + + @room_block = Hotel::RoomBlock.new(reservation_dates, room_ids, block_id) + end + + it "is an instance of a Room Block" do + expect(@room_block).must_be_kind_of Hotel::RoomBlock + end + + it "is initialized with a date range, room ids and block id" do + + expect(@room_block).must_respond_to :reservation_dates + expect(@room_block.reservation_dates).must_be_kind_of Array + + expect(@room_block).must_respond_to :room_ids + expect(@room_block.room_ids).must_be_kind_of Array + + expect(@room_block).must_respond_to :block_id + expect(@room_block.block_id).must_be_kind_of Integer + + end + + + + + end +end From 6e340c4f19be20043ccf6c951088abd69e8d6061 Mon Sep 17 00:00:00 2001 From: Lindsay Terchin Date: Thu, 27 Sep 2018 14:20:24 -0700 Subject: [PATCH 40/42] answered prompts related to the shopping cart implementations: --- design-activity.md | 63 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 design-activity.md diff --git a/design-activity.md b/design-activity.md new file mode 100644 index 000000000..3f5960c93 --- /dev/null +++ b/design-activity.md @@ -0,0 +1,63 @@ +## Hotel Design Activity + +**What classes does each implementation include? Are the lists the same?** + +Both implementations have 3 classes by the same names: CartEntry, ShoppingCart, Order. + + +**Write down a sentence to describe each class.** + +CartEntry represents the addition of a given product to a cart, including its price and quantity. + +ShoppingCart represents a collection of everything that has been added to a cart. + +Order represents the total cost of everything in a given cart, including sales tax. + + +**How do the classes relate to each other? It might be helpful to draw a diagram on a whiteboard or piece of paper.** + +ShoppingCart contains each CartEntry. Order calculates the total of the ShoppingCart after sales tax. + + +**What data does each class store? How (if at all) does this differ between the two implementations?** + +CartEntry: both implementations store the prices and quantities of a given product. In implementation A, those values can be accessed outside of the class due to the attr_accessor whereas in Implementation B, they have to be used locally. + +ShoppingCart: both implementations have an array that stores each CartEntry object. In Implementation A, those entries can be accessed outside of the class. Implementation B also has a value for the sum of all entries in the cart. + +Order: both implementations store the sum/subtotal, or total price of a complete order (before sales tax). This is calculated differently in each implementation. Implementation A iterates through each price and quantity in @entries. Implementation B calls the #price method from ShoppingCart. + + +**What methods does each class have? How (if at all) does this differ between the two implementations?** + +CartEntry: both implementations have an initialize method. Implementation B also has a price method that returns the subtotal of one particular item. + +ShoppingCart: both have an initialize method. + +Order: both have an initialize method that creates a new instance of a ShoppingCart. Both also have a total_price method that returns the total of all cart entries plus sales tax. The difference is that the method in Implementation A handles all of the logic for "sum" whereas Implementation B calls the "price" method in the ShoppingCart class. + + +**Consider the Order#total_price method. In each implementation:** +**Is logic to compute the price delegated to "lower level" classes like ShoppingCart and CartEntry, or is it retained in Order?** + +In Implementation A, the logic is retained in Order. In Implementation B, the logic relies on ShoppingCart, which is a lower level class. + + +**Does total_price directly manipulate the instance variables of other classes?** + +In Implementation A, total_price reads @entries from ShoppingCart and @unit_price and @quantity from CartEntry. In Implementation B, total_price does not read instance variables of any other class. + + +**If we decide items are cheaper if bought in bulk, how would this change the code? Which implementation is easier to modify?** + +You would need to add some conditional logic that lowers the :unit_price based on :quantity. Implementation A is harder to modify because that logic would need to be called into account in the CartEntry class and Order class. Implementation B is easier to modify because you would only need to modify the CartEntry class. + + +**Which implementation better adheres to the single responsibility principle?** + +Implementation B. + + +**Bonus question once you've read Metz ch. 3: Which implementation is more loosely coupled?** + +Implementation B. From 988f233777854c57e8582b91f694099e87b9fcc7 Mon Sep 17 00:00:00 2001 From: Lindsay Terchin Date: Thu, 27 Sep 2018 14:35:55 -0700 Subject: [PATCH 41/42] added answer for how to refactor hotel --- design-activity.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/design-activity.md b/design-activity.md index 3f5960c93..4dda7273e 100644 --- a/design-activity.md +++ b/design-activity.md @@ -61,3 +61,7 @@ Implementation B. **Bonus question once you've read Metz ch. 3: Which implementation is more loosely coupled?** Implementation B. + + +**changes to hotel** +I had a lot of logic regarding the date within reservation_hub. I can create a separate DateRange class to validate dates and create a date array. It would make my code better because I'd be able to call the DateRange class when booking both, room blocks and reservations. It would not only dry up my code, but also help me better adhere to single responsibility. From 2f92016e3344713b65e542362c93ac1ce3912a69 Mon Sep 17 00:00:00 2001 From: Lindsay Terchin Date: Thu, 27 Sep 2018 15:22:19 -0700 Subject: [PATCH 42/42] added date class and refactored affected methods and tests --- lib/date_range.rb | 34 +++++++++++++++++++++++ lib/reservation.rb | 2 +- lib/reservation_hub.rb | 37 +++++-------------------- spec/date_range_spec.rb | 52 ++++++++++++++++++++++++++++++++++++ spec/reservation_hub_spec.rb | 31 +-------------------- spec/reservation_spec.rb | 7 ++--- spec/spec_helper.rb | 2 ++ 7 files changed, 99 insertions(+), 66 deletions(-) create mode 100644 lib/date_range.rb create mode 100644 spec/date_range_spec.rb diff --git a/lib/date_range.rb b/lib/date_range.rb new file mode 100644 index 000000000..ccaa02084 --- /dev/null +++ b/lib/date_range.rb @@ -0,0 +1,34 @@ +require 'date' + + +module Hotel + class DateRange + + def initialize(start_date, end_date) + + @start_date = start_date + @end_date = end_date + + validate_dates + + @date_range = create_date_array(start_date, end_date) + end + + + def validate_dates + raise ArgumentError.new("The end date must be after the start date") if @end_date <= @start_date + end + + + def create_date_array(start_date, end_date) + number_of_nights = (end_date - start_date).to_i + date_array = [] + number_of_nights.times do + date_array << start_date + start_date +=1 + end + return date_array + end + + end +end diff --git a/lib/reservation.rb b/lib/reservation.rb index 9c6a657e3..a9f820972 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -2,7 +2,7 @@ module Hotel class Reservation - attr_reader :start_date, :end_date, :room_id, :reservation_dates, :total_cost + attr_reader :room_id, :reservation_dates, :total_cost def initialize(reservation_dates, room_id) diff --git a/lib/reservation_hub.rb b/lib/reservation_hub.rb index d3b779056..5ab1fcdce 100644 --- a/lib/reservation_hub.rb +++ b/lib/reservation_hub.rb @@ -3,13 +3,14 @@ require_relative 'reservation' require_relative 'room_block' +require_relative 'date_range' module Hotel class ReservationHub class NoRoomsAvailableError < StandardError; end - attr_reader :reservations, :room_bookings, :reservation_dates, :room_blocks + attr_reader :reservations, :room_bookings, :room_blocks def initialize @@ -21,11 +22,8 @@ def initialize def add_reservation(start_date, end_date) - @start_date = start_date - @end_date = end_date - validate_dates - - reservation_dates = create_date_array(start_date, end_date) + dates = DateRange.new(start_date, end_date) + reservation_dates = dates.create_date_array(start_date, end_date) room_id = assign_room(reservation_dates) @@ -39,16 +37,12 @@ def add_reservation(start_date, end_date) def add_room_block(start_date, end_date, total_rooms) - @start_date = start_date - @end_date = end_date - @total_rooms = total_rooms - validate_dates - - reservation_dates = create_date_array(start_date, end_date) + dates = DateRange.new(start_date, end_date) + reservation_dates = dates.create_date_array(start_date, end_date) room_ids = [] - @total_rooms.times do + total_rooms.times do room_id = assign_room(reservation_dates) room_ids << room_id end @@ -63,22 +57,6 @@ def add_room_block(start_date, end_date, total_rooms) end - def validate_dates - raise ArgumentError.new("The end date must be after the start date") if @end_date <= @start_date - end - - - def create_date_array(start_date, end_date) - number_of_nights = (end_date - start_date).to_i - date_array = [] - number_of_nights.times do - date_array << start_date - start_date +=1 - end - return date_array - end - - def check_available_rooms(reservation_dates) available_rooms = [] @@ -135,7 +113,6 @@ def find_room_block(id) return room_block end end - end end diff --git a/spec/date_range_spec.rb b/spec/date_range_spec.rb new file mode 100644 index 000000000..c01e86d5d --- /dev/null +++ b/spec/date_range_spec.rb @@ -0,0 +1,52 @@ +require_relative 'spec_helper' +require 'pry' + +describe "DateRange class" do + + describe "validate" do + + it "must throw an argument error if the end date is before the start date" do + + start_date = Date.new(2015, 03, 05) + end_date = Date.new(2015, 03, 01) + + room_id = 4 + + expect {Hotel::DateRange.new(start_date, end_date)}.must_raise ArgumentError + + end + end + + + describe "create date array" do + + before do + start_date = Date.new(2018,01,06) + end_date = Date.new(2018,01,10) + + dates = Hotel::DateRange.new(start_date, end_date) + + @date_array = dates.create_date_array(start_date, end_date) + + @test_dates = [] + + until start_date == end_date + @test_dates << start_date + start_date +=1 + end + + end + + it "returns an array of all dates" do + expect(@date_array).must_be_kind_of Array + expect(@date_array.length).must_equal @test_dates.length + end + + it "includes all dates in the reservation, not including the end date" do + expect(@date_array).wont_include @end_date + expect(@date_array).must_equal @test_dates + + end + end + +end diff --git a/spec/reservation_hub_spec.rb b/spec/reservation_hub_spec.rb index 5cc7d1121..c8ac88807 100644 --- a/spec/reservation_hub_spec.rb +++ b/spec/reservation_hub_spec.rb @@ -111,36 +111,7 @@ expect(@room_block2.block_id).must_equal 2 end end - - - describe "create date array" do - - before do - start_date = Date.new(2018,01,06) - @end_date = Date.new(2018,01,10) - - @date_array = @reservation_hub.create_date_array(start_date, @end_date) - - @test_dates = [] - until start_date == @end_date - @test_dates << start_date - start_date +=1 - end - - end - - it "returns an array of all dates" do - expect(@date_array).must_be_kind_of Array - expect(@date_array.length).must_equal @test_dates.length - end - - it "includes all dates in the reservation, not including the end date" do - expect(@date_array).wont_include @end_date - expect(@date_array).must_equal @test_dates - - end - end - + describe "check available rooms method" do diff --git a/spec/reservation_spec.rb b/spec/reservation_spec.rb index 18bae9462..b58011082 100644 --- a/spec/reservation_spec.rb +++ b/spec/reservation_spec.rb @@ -32,12 +32,9 @@ it "returns the total amount of the new reservation" do date_range = [1,2,3,4] - @reservation = Hotel::Reservation.new(date_range, 3) + reservation = Hotel::Reservation.new(date_range, 3) - start_date = @reservation.start_date - end_date = @reservation.end_date - - expect(@reservation.reservation_cost(date_range)).must_equal 800 + expect(reservation.reservation_cost(date_range)).must_equal 800 end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index ba258ac28..909ea9b6b 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -10,3 +10,5 @@ require_relative '../lib/reservation' require_relative '../lib/reservation_hub' +require_relative '../lib/room_block' +require_relative '../lib/date_range'