From 4200d5b10054bd27a7f915084f6848850b7164c2 Mon Sep 17 00:00:00 2001 From: Melissa O'Hearn Date: Tue, 4 Sep 2018 15:54:29 -0700 Subject: [PATCH 01/20] created spec files, reservation class, and Booking System class --- Guardfile | 2 +- lib/booking_system.rb | 23 +++++++++++++++++++++++ lib/reservations.rb | 20 ++++++++++++++++++++ spec/booking_system_spec.rb | 3 +++ spec/reservation_spec.rb | 1 + spec/spec_helper.rb | 5 +++++ 6 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 lib/booking_system.rb create mode 100644 lib/reservations.rb create mode 100644 spec/booking_system_spec.rb create mode 100644 spec/reservation_spec.rb 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/booking_system.rb b/lib/booking_system.rb new file mode 100644 index 000000000..788053c49 --- /dev/null +++ b/lib/booking_system.rb @@ -0,0 +1,23 @@ +require_relative 'reservations' + +module Hotel + class BookingSystem + attr_reader :reservations + + room_list = [ + "Room 1", "Room 2", "Room 3", "Room 4", "Room 5", "Room 6", "Room 7", "Room 8", "Room 9", "Room 10", "Room 11", "Room 12", "Room 13", "Room 14" , "Room 15", "Room 16", "Room 17", "Room 18", "Room 19", "Room 20" + ] + + def initalize + @reservations = [] + @availibility = # boolean + @list_of_avail_rooms + @list_of_booked_rooms + end + + # methods + # - Check availibility of room + # - generate a list of availible rooms? + # - generate a list of booked rooms? + end +end diff --git a/lib/reservations.rb b/lib/reservations.rb new file mode 100644 index 000000000..4333b3cc7 --- /dev/null +++ b/lib/reservations.rb @@ -0,0 +1,20 @@ +module Hotel + class Reservation + + attr_reader :availibility, :check_in, :check_out, :cost, :reservation_id, + + def initalize + + @check_in = # date + @check_out = # date + @cost = # $200/night + @reservation_id = #random generated id + end + + # methods + # - Find duration of stay + # - Find total cost + # - Generate an reservation ID + + end +end diff --git a/spec/booking_system_spec.rb b/spec/booking_system_spec.rb new file mode 100644 index 000000000..554fd5f5d --- /dev/null +++ b/spec/booking_system_spec.rb @@ -0,0 +1,3 @@ +require_relative 'spec_helper' + +# raise an error (StandardError) when an invalid date range is provided diff --git a/spec/reservation_spec.rb b/spec/reservation_spec.rb new file mode 100644 index 000000000..ae9c220ea --- /dev/null +++ b/spec/reservation_spec.rb @@ -0,0 +1 @@ +require_relative 'spec_helper' diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 4d1e3fdc8..e04867bc4 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,3 +1,6 @@ +require 'simplecov' +SimpleCov.start + require 'minitest' require 'minitest/autorun' require 'minitest/reporters' @@ -5,4 +8,6 @@ Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new +require_relative '../lib/booking_system.rb' +require_relative '../lib/reservation.rb' # Require_relative your lib files here! From 0d29cf20ac97d03c35e28760dc1047e8f22cba8e Mon Sep 17 00:00:00 2001 From: Melissa O'Hearn Date: Tue, 4 Sep 2018 21:00:25 -0700 Subject: [PATCH 02/20] renamed reservations.rb to reservation.rb. added a StandardError if check-out time is before check-in --- lib/booking_system.rb | 28 ++++++++++---------- lib/reservation.rb | 27 +++++++++++++++++++ lib/reservations.rb | 20 -------------- spec/booking_system_spec.rb | 11 ++++++++ spec/reservation_spec.rb | 53 +++++++++++++++++++++++++++++++++++++ spec/spec_helper.rb | 3 +-- 6 files changed, 106 insertions(+), 36 deletions(-) create mode 100644 lib/reservation.rb delete mode 100644 lib/reservations.rb diff --git a/lib/booking_system.rb b/lib/booking_system.rb index 788053c49..9b6ce0b2d 100644 --- a/lib/booking_system.rb +++ b/lib/booking_system.rb @@ -1,23 +1,23 @@ -require_relative 'reservations' +require 'date' module Hotel class BookingSystem - attr_reader :reservations + attr_reader :rooms, :reservations, :availibility - room_list = [ - "Room 1", "Room 2", "Room 3", "Room 4", "Room 5", "Room 6", "Room 7", "Room 8", "Room 9", "Room 10", "Room 11", "Room 12", "Room 13", "Room 14" , "Room 15", "Room 16", "Room 17", "Room 18", "Room 19", "Room 20" - ] + def initialize + @rooms = + [ + {room_number: 1}, {room_number: 2}, {room_number: 3}, {room_number: 4}, {room_number: 5}, {room_number: 6}, {room_number: 7}, {room_number: 8}, {room_number: 9}, {room_number: 10}, {room_number: 11}, {room_number: 12}, {room_number: 13}, {room_number: 14}, {room_number: 15}, {room_number: 16}, {room_number: 17}, {room_number: 18}, {room_number: 19}, {room_number: 20} + ] - def initalize @reservations = [] - @availibility = # boolean - @list_of_avail_rooms - @list_of_booked_rooms - end - # methods - # - Check availibility of room - # - generate a list of availible rooms? - # - generate a list of booked rooms? + + + # methods + # - Check availibility of room + # - generate a list of availible rooms? + # - generate a list of booked rooms? + end end end diff --git a/lib/reservation.rb b/lib/reservation.rb new file mode 100644 index 000000000..e759403fa --- /dev/null +++ b/lib/reservation.rb @@ -0,0 +1,27 @@ +require 'date' + +module Hotel + class Reservation + + attr_reader :check_in, :check_out, :cost, :reservation_id + + def initialize(check_in, check_out, cost, reservation_id) + + + @check_in = Date.parse(check_in) + @check_out = Date.parse(check_out) + @cost = cost + @reservation_id = reservation_id + + unless @check_out > @check_in + raise StandardError.new("Check-out can not be before check-in. Chech-out is: #{@check_out} check-in is #{@check_in}") + end + end + + # methods + # - Find duration of stay + # - Find total cost + # - Generate an reservation ID + + end +end diff --git a/lib/reservations.rb b/lib/reservations.rb deleted file mode 100644 index 4333b3cc7..000000000 --- a/lib/reservations.rb +++ /dev/null @@ -1,20 +0,0 @@ -module Hotel - class Reservation - - attr_reader :availibility, :check_in, :check_out, :cost, :reservation_id, - - def initalize - - @check_in = # date - @check_out = # date - @cost = # $200/night - @reservation_id = #random generated id - end - - # methods - # - Find duration of stay - # - Find total cost - # - Generate an reservation ID - - end -end diff --git a/spec/booking_system_spec.rb b/spec/booking_system_spec.rb index 554fd5f5d..2c7fc6b5c 100644 --- a/spec/booking_system_spec.rb +++ b/spec/booking_system_spec.rb @@ -1,3 +1,14 @@ require_relative 'spec_helper' +describe "BookingSystem class" do + + describe "Booking System instantiation" do + describe "Initalizer" do + booking_system = Hotel::BookingSystem.new() + + end + end +end + + # raise an error (StandardError) when an invalid date range is provided diff --git a/spec/reservation_spec.rb b/spec/reservation_spec.rb index ae9c220ea..87818d3bc 100644 --- a/spec/reservation_spec.rb +++ b/spec/reservation_spec.rb @@ -1 +1,54 @@ +require 'minitest' +require 'minitest/autorun' +require 'minitest/reporters' + require_relative 'spec_helper' +require_relative '../lib/booking_system.rb' +require_relative '../lib/reservation.rb' + + +describe "initialize" do + it "Takes check_in, check_out, cost, and reservation_id" do + + check_in = "2018-02-03" + check_out = "2018-02-06" + cost = 600 + reservation_id = 1337 + + + reservation = Hotel::Reservation.new(check_in, check_out, cost, reservation_id) + + expect(reservation).must_respond_to :check_in + expect(reservation.check_in.to_s).must_equal check_in + + expect(reservation).must_respond_to :check_out + expect(reservation.check_out.to_s).must_equal check_out + + expect(reservation).must_respond_to :cost + expect(reservation.cost).must_equal cost + + expect(reservation).must_respond_to :reservation_id + expect(reservation.reservation_id).must_equal reservation_id + end + + it "it raises a StandardError if check-out is before check-in " do + # Arrange + # make a start time that is after an end time + # and put those in @trip_data + + # Act + # Make a new trip with the bad times + + + check_in = "2018-02-03" + check_out = "2018-01-30" + cost = 600 + reservation_id = 1337 + + + expect {Hotel::reservation.new(check_in, check_out, cost, reservation_id)}.must_raise StandardError + + end + + +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index e04867bc4..88788d2a4 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -4,10 +4,9 @@ require 'minitest' require 'minitest/autorun' require 'minitest/reporters' -# Add simplecov + Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new require_relative '../lib/booking_system.rb' require_relative '../lib/reservation.rb' -# Require_relative your lib files here! From b1cd92f4c779b410dd6152611c4bcb439132f34d Mon Sep 17 00:00:00 2001 From: Melissa O'Hearn Date: Wed, 5 Sep 2018 12:08:05 -0700 Subject: [PATCH 03/20] added a test in reservation_spec.rb to set it up for specific attributes and data types --- lib/reservation.rb | 8 ++++-- spec/booking_system_spec.rb | 22 ++++++++++------ spec/reservation_spec.rb | 50 ++++++++++++++++++++++++++----------- 3 files changed, 56 insertions(+), 24 deletions(-) diff --git a/lib/reservation.rb b/lib/reservation.rb index e759403fa..8efbbfcca 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -10,14 +10,18 @@ def initialize(check_in, check_out, cost, reservation_id) @check_in = Date.parse(check_in) @check_out = Date.parse(check_out) - @cost = cost - @reservation_id = reservation_id + @cost = cost.to_f + @reservation_id = reservation_id.to_s unless @check_out > @check_in raise StandardError.new("Check-out can not be before check-in. Chech-out is: #{@check_out} check-in is #{@check_in}") end end + def generate_id + return reservation_id + end + # methods # - Find duration of stay # - Find total cost diff --git a/spec/booking_system_spec.rb b/spec/booking_system_spec.rb index 2c7fc6b5c..87928479a 100644 --- a/spec/booking_system_spec.rb +++ b/spec/booking_system_spec.rb @@ -2,13 +2,21 @@ describe "BookingSystem class" do - describe "Booking System instantiation" do - describe "Initalizer" do - booking_system = Hotel::BookingSystem.new() - + describe "Booking System instantiation" do + describe "Initalizer" do + booking_system = Hotel::BookingSystem.new() + end - end -end + # it "Returns an array all of the rooms in the hotel" do + # + # rooms = rooms.all + # expect(rooms.length).must_equal 20 + # rooms.each do |room| + # expect(room).must_be_kind_of rooms + # end + # end -# raise an error (StandardError) when an invalid date range is provided + + end +end diff --git a/spec/reservation_spec.rb b/spec/reservation_spec.rb index 87818d3bc..0ecc352b3 100644 --- a/spec/reservation_spec.rb +++ b/spec/reservation_spec.rb @@ -8,12 +8,15 @@ describe "initialize" do + before do + @new_res = Hotel::Reservation.new(check_in = "2018-02-03" , check_out = "2018-02-06", cost = 600, reservation_id = "603-XOX") + end it "Takes check_in, check_out, cost, and reservation_id" do check_in = "2018-02-03" check_out = "2018-02-06" cost = 600 - reservation_id = 1337 + reservation_id = "603-XOX" reservation = Hotel::Reservation.new(check_in, check_out, cost, reservation_id) @@ -31,24 +34,41 @@ expect(reservation.reservation_id).must_equal reservation_id end - it "it raises a StandardError if check-out is before check-in " do - # Arrange - # make a start time that is after an end time - # and put those in @trip_data + # raise an error (StandardError) when an invalid date range is provided + it "raises a StandardError if check-out is before check-in " do - # Act - # Make a new trip with the bad times + check_in = "2018-02-03" + check_out = "2018-01-30" + cost = 600 + reservation_id = "603-XOX" - check_in = "2018-02-03" - check_out = "2018-01-30" - cost = 600 - reservation_id = 1337 + expect {Hotel::reservation.new(check_in, check_out, cost, reservation_id)}.must_raise StandardError + end + it "is set up for specific attributes and data types" do + [:check_in, :check_out, :cost, :reservation_id].each do |prop| + expect(@new_res).must_respond_to prop + end - expect {Hotel::reservation.new(check_in, check_out, cost, reservation_id)}.must_raise StandardError - - end + expect(@new_res.reservation_id).must_be_kind_of String + expect(@new_res.check_in).must_be_kind_of Date + expect(@new_res.check_out).must_be_kind_of Date + expect(@new_res.cost).must_be_kind_of Float + end +end +# describe "make a reservation" do +# it "generates a reservation ID" do +# +# check_in = "2018-02-03" +# check_out = "2018-01-30" +# cost = 600 +# reservation_id = "603-XOX" +# +# expect(@user.name).must_be_kind_of String +# end -end + + +# end From 66869e99a9d471cfca28fd4f78c78fcb0924dec9 Mon Sep 17 00:00:00 2001 From: Melissa O'Hearn Date: Wed, 5 Sep 2018 12:17:48 -0700 Subject: [PATCH 04/20] refactored my tests to include a before do to use that instance for tests. added a test to make sure it's an instance of a Reservation Class --- spec/reservation_spec.rb | 34 ++++++++++------------------------ 1 file changed, 10 insertions(+), 24 deletions(-) diff --git a/spec/reservation_spec.rb b/spec/reservation_spec.rb index 0ecc352b3..aaee91402 100644 --- a/spec/reservation_spec.rb +++ b/spec/reservation_spec.rb @@ -9,40 +9,26 @@ describe "initialize" do before do - @new_res = Hotel::Reservation.new(check_in = "2018-02-03" , check_out = "2018-02-06", cost = 600, reservation_id = "603-XOX") + @new_res = Hotel::Reservation.new(check_in = "2018-02-03" , check_out = "2018-02-06", cost = 600.00, reservation_id = "603-XOX") end - it "Takes check_in, check_out, cost, and reservation_id" do - - check_in = "2018-02-03" - check_out = "2018-02-06" - cost = 600 - reservation_id = "603-XOX" - - reservation = Hotel::Reservation.new(check_in, check_out, cost, reservation_id) - - expect(reservation).must_respond_to :check_in - expect(reservation.check_in.to_s).must_equal check_in + it "is an instance of User" do + expect(@new_res).must_be_kind_of Hotel::Reservation + end - expect(reservation).must_respond_to :check_out - expect(reservation.check_out.to_s).must_equal check_out + it "Takes check_in, check_out, cost, and reservation_id" do - expect(reservation).must_respond_to :cost - expect(reservation.cost).must_equal cost + expect(@new_res).must_respond_to :check_in + expect(@new_res).must_respond_to :check_out + expect(@new_res).must_respond_to :cost + expect(@new_res).must_respond_to :reservation_id - expect(reservation).must_respond_to :reservation_id - expect(reservation.reservation_id).must_equal reservation_id end + # raise an error (StandardError) when an invalid date range is provided it "raises a StandardError if check-out is before check-in " do - check_in = "2018-02-03" - check_out = "2018-01-30" - cost = 600 - reservation_id = "603-XOX" - - expect {Hotel::reservation.new(check_in, check_out, cost, reservation_id)}.must_raise StandardError end From 7038e066d9a2621f5ae3c588dea0d4bb66f241d8 Mon Sep 17 00:00:00 2001 From: Melissa O'Hearn Date: Wed, 5 Sep 2018 15:53:37 -0700 Subject: [PATCH 05/20] added methods to reservation.rb to find duration of stay and total cost of stay, with corresponding tests in spec.rb --- lib/booking_system.rb | 3 +++ lib/reservation.rb | 35 +++++++++++++++++++++++++++++++---- spec/reservation_spec.rb | 32 +++++++++++++++++--------------- 3 files changed, 51 insertions(+), 19 deletions(-) diff --git a/lib/booking_system.rb b/lib/booking_system.rb index 9b6ce0b2d..8372f14a6 100644 --- a/lib/booking_system.rb +++ b/lib/booking_system.rb @@ -13,6 +13,9 @@ def initialize @reservations = [] +# method to make_reservation +# return if successful return res_id, or "sucess", or return true +# keep track of reservations - make sure # of reservations have increased by 1 # methods # - Check availibility of room diff --git a/lib/reservation.rb b/lib/reservation.rb index 8efbbfcca..73c5dee95 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -1,4 +1,5 @@ require 'date' +require 'pry' module Hotel class Reservation @@ -17,15 +18,41 @@ def initialize(check_in, check_out, cost, reservation_id) raise StandardError.new("Check-out can not be before check-in. Chech-out is: #{@check_out} check-in is #{@check_in}") end end - + # - Generate an reservation ID def generate_id - return reservation_id + rand_array = [] + + 3.times do + rand_num = rand(1..9).to_s + rand_array << rand_num + end + + 3.times do + rand_letter = ('A'..'Z').to_a.sample.to_s + rand_array << rand_letter + end + + rand_array.insert(3, "-") + @reservation_id = rand_array * "".to_s + return @reservation_id + end - # methods # - Find duration of stay + def duration_of_stay + duration_of_stay = @check_out - @check_in + return duration_of_stay + end # - Find total cost - # - Generate an reservation ID + def total_cost + total_cost = @cost * duration_of_stay + return total_cost + end + + + + + # {res_id: msmsm, cost: $200, nights:2 } end end diff --git a/spec/reservation_spec.rb b/spec/reservation_spec.rb index aaee91402..506d9435f 100644 --- a/spec/reservation_spec.rb +++ b/spec/reservation_spec.rb @@ -12,7 +12,7 @@ @new_res = Hotel::Reservation.new(check_in = "2018-02-03" , check_out = "2018-02-06", cost = 600.00, reservation_id = "603-XOX") end - it "is an instance of User" do + it "is an instance of Reservation" do expect(@new_res).must_be_kind_of Hotel::Reservation end @@ -33,8 +33,8 @@ end it "is set up for specific attributes and data types" do - [:check_in, :check_out, :cost, :reservation_id].each do |prop| - expect(@new_res).must_respond_to prop + [:check_in, :check_out, :cost, :reservation_id].each do |initial| + expect(@new_res).must_respond_to initial end expect(@new_res.reservation_id).must_be_kind_of String @@ -44,17 +44,19 @@ end end -# describe "make a reservation" do -# it "generates a reservation ID" do -# -# check_in = "2018-02-03" -# check_out = "2018-01-30" -# cost = 600 -# reservation_id = "603-XOX" -# -# expect(@user.name).must_be_kind_of String -# end - +describe "make a reservation" do + before do + @new_res = Hotel::Reservation.new(check_in = "2018-02-03" , check_out = "2018-02-06", cost = 200.00, reservation_id = "") + end + it "generates a reservation ID" do + expect(@new_res.generate_id.length).must_equal 7 + end + it "finds the duration of stay" do + expect(@new_res.duration_of_stay).must_equal 3 + end -# end + it "finds the total cost of reservation" do + expect(@new_res.total_cost).must_equal 600.00 + end +end From f0e9993b770029b34e949f25f7774e0ec88c70de Mon Sep 17 00:00:00 2001 From: Melissa O'Hearn Date: Wed, 5 Sep 2018 19:21:48 -0700 Subject: [PATCH 06/20] took reservation_id out of initialize paramater. Wrote initialize tests for booking_system --- lib/booking_system.rb | 25 ++++++++++++++++++---- lib/reservation.rb | 14 ++++++------- spec/booking_system_spec.rb | 42 +++++++++++++++++++++++++++---------- spec/reservation_spec.rb | 6 +++--- 4 files changed, 62 insertions(+), 25 deletions(-) diff --git a/lib/booking_system.rb b/lib/booking_system.rb index 8372f14a6..b9d2568bf 100644 --- a/lib/booking_system.rb +++ b/lib/booking_system.rb @@ -4,18 +4,35 @@ module Hotel class BookingSystem attr_reader :rooms, :reservations, :availibility - def initialize + def initialize (rooms, reservations, availibility) @rooms = [ {room_number: 1}, {room_number: 2}, {room_number: 3}, {room_number: 4}, {room_number: 5}, {room_number: 6}, {room_number: 7}, {room_number: 8}, {room_number: 9}, {room_number: 10}, {room_number: 11}, {room_number: 12}, {room_number: 13}, {room_number: 14}, {room_number: 15}, {room_number: 16}, {room_number: 17}, {room_number: 18}, {room_number: 19}, {room_number: 20} ] @reservations = [] + @availibility = availibility + # keep track of reservations - make sure # of reservations have increased by 1 + + # method to make_reservation + # return if successful return res_id, or "sucess", or return true + + #method to find an availible room: + # input:start, end. output:availible room + + # use a helper method to check_overlapping dates: + # input: reservation attempt: (start date and end date) compare to an existing reservation + # Output: boolean + #edge case: begining of res or end of res can overlap(but check_out and check_in can be same date) + + #edge case - what if there are no available rooms? + #edge case - + # + # + + -# method to make_reservation -# return if successful return res_id, or "sucess", or return true -# keep track of reservations - make sure # of reservations have increased by 1 # methods # - Check availibility of room diff --git a/lib/reservation.rb b/lib/reservation.rb index 73c5dee95..01c06bdcc 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -6,20 +6,20 @@ class Reservation attr_reader :check_in, :check_out, :cost, :reservation_id - def initialize(check_in, check_out, cost, reservation_id) + def initialize(check_in, check_out, cost) @check_in = Date.parse(check_in) @check_out = Date.parse(check_out) @cost = cost.to_f - @reservation_id = reservation_id.to_s + @reservation_id = Reservation.generate_id unless @check_out > @check_in raise StandardError.new("Check-out can not be before check-in. Chech-out is: #{@check_out} check-in is #{@check_in}") end end - # - Generate an reservation ID - def generate_id + # - Generate an reservation ID + def self.generate_id rand_array = [] 3.times do @@ -33,8 +33,8 @@ def generate_id end rand_array.insert(3, "-") - @reservation_id = rand_array * "".to_s - return @reservation_id + return rand_array * "".to_s + end @@ -50,7 +50,7 @@ def total_cost end - + # {res_id: msmsm, cost: $200, nights:2 } diff --git a/spec/booking_system_spec.rb b/spec/booking_system_spec.rb index 87928479a..51bd42c60 100644 --- a/spec/booking_system_spec.rb +++ b/spec/booking_system_spec.rb @@ -2,21 +2,41 @@ describe "BookingSystem class" do - describe "Booking System instantiation" do - describe "Initalizer" do - booking_system = Hotel::BookingSystem.new() + describe "initialize" do + before do + @booking = Hotel::BookingSystem.new(rooms = [], reservations = [], availibility = availibility) + end + it "is an instance of Reservation" do + expect(@booking).must_be_kind_of Hotel::BookingSystem end - # it "Returns an array all of the rooms in the hotel" do - # - # rooms = rooms.all - # expect(rooms.length).must_equal 20 - # rooms.each do |room| - # expect(room).must_be_kind_of rooms - # end - # end + it "Takes check_in, check_out, cost, and reservation_id" do + + expect(@booking).must_respond_to :rooms + expect(@booking).must_respond_to :reservations + expect(@booking).must_respond_to :availibility + end + it "is set up for specific attributes and data types" do + [:rooms, :reservations, :availibility].each do |initial| + expect(@booking).must_respond_to initial + end + + expect(@booking.rooms).must_be_kind_of Array + expect(@booking.reservations).must_be_kind_of Array + # expect(@booking_system.availibility).must_be_kind_of String + end + end + describe "rooms" do + before do + @booking = Hotel::BookingSystem.new(rooms = [], reservations = [], availibility = availibility) + end + it "Returns an array all of the rooms in the hotel" do + expect(@booking.rooms.length).must_equal 20 + end end + + end diff --git a/spec/reservation_spec.rb b/spec/reservation_spec.rb index 506d9435f..d206e6175 100644 --- a/spec/reservation_spec.rb +++ b/spec/reservation_spec.rb @@ -9,7 +9,7 @@ describe "initialize" do before do - @new_res = Hotel::Reservation.new(check_in = "2018-02-03" , check_out = "2018-02-06", cost = 600.00, reservation_id = "603-XOX") + @new_res = Hotel::Reservation.new(check_in = "2018-02-03" , check_out = "2018-02-06", cost = 600.00) end it "is an instance of Reservation" do @@ -46,10 +46,10 @@ describe "make a reservation" do before do - @new_res = Hotel::Reservation.new(check_in = "2018-02-03" , check_out = "2018-02-06", cost = 200.00, reservation_id = "") + @new_res = Hotel::Reservation.new(check_in = "2018-02-03" , check_out = "2018-02-06", cost = 200.00) end it "generates a reservation ID" do - expect(@new_res.generate_id.length).must_equal 7 + expect(@new_res.reservation_id.length).must_equal 7 end it "finds the duration of stay" do From 51d91ea94d9ee693dd874bbfff2dade2544e166b Mon Sep 17 00:00:00 2001 From: Melissa O'Hearn Date: Thu, 6 Sep 2018 11:14:28 -0700 Subject: [PATCH 07/20] email update? --- lib/booking_system.rb | 2 ++ lib/reservation.rb | 2 -- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/booking_system.rb b/lib/booking_system.rb index b9d2568bf..1afc7bcca 100644 --- a/lib/booking_system.rb +++ b/lib/booking_system.rb @@ -12,6 +12,8 @@ def initialize (rooms, reservations, availibility) @reservations = [] @availibility = availibility + + # keep track of reservations - make sure # of reservations have increased by 1 # method to make_reservation diff --git a/lib/reservation.rb b/lib/reservation.rb index 01c06bdcc..3902e28e9 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -34,8 +34,6 @@ def self.generate_id rand_array.insert(3, "-") return rand_array * "".to_s - - end # - Find duration of stay From 76947f039403c92d742bdd9c427bda976603a091 Mon Sep 17 00:00:00 2001 From: Melissa O'Hearn Date: Thu, 6 Sep 2018 13:50:12 -0700 Subject: [PATCH 08/20] added DateRange class and spec --- lib/date_range.rb | 1 + spec/date_range_spec.rb | 7 +++++++ 2 files changed, 8 insertions(+) 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..a99751eca --- /dev/null +++ b/lib/date_range.rb @@ -0,0 +1 @@ +require 'date' diff --git a/spec/date_range_spec.rb b/spec/date_range_spec.rb new file mode 100644 index 000000000..6cc8481b4 --- /dev/null +++ b/spec/date_range_spec.rb @@ -0,0 +1,7 @@ +require 'minitest' +require 'minitest/autorun' +require 'minitest/reporters' + +require_relative 'spec_helper' +require_relative '../lib/booking_system.rb' +require_relative '../lib/reservation.rb' From 323aca3a2ccabed880c71b412f242cff5aa9ad1a Mon Sep 17 00:00:00 2001 From: Melissa O'Hearn Date: Thu, 6 Sep 2018 14:49:11 -0700 Subject: [PATCH 09/20] added tests to date_range and removed them from reservation --- lib/booking_system.rb | 5 +++- lib/date_range.rb | 25 +++++++++++++++++++ lib/reservation.rb | 23 ++++------------- spec/booking_system_spec.rb | 2 -- spec/date_range_spec.rb | 50 +++++++++++++++++++++++++++++++++++-- spec/reservation_spec.rb | 28 ++++++++++----------- spec/spec_helper.rb | 1 + 7 files changed, 96 insertions(+), 38 deletions(-) diff --git a/lib/booking_system.rb b/lib/booking_system.rb index 1afc7bcca..aba48a002 100644 --- a/lib/booking_system.rb +++ b/lib/booking_system.rb @@ -1,4 +1,7 @@ require 'date' +require_relative "reservation" +require_relative "date_range" + module Hotel class BookingSystem @@ -13,7 +16,7 @@ def initialize (rooms, reservations, availibility) @reservations = [] @availibility = availibility - + # keep track of reservations - make sure # of reservations have increased by 1 # method to make_reservation diff --git a/lib/date_range.rb b/lib/date_range.rb index a99751eca..e353dd5a6 100644 --- a/lib/date_range.rb +++ b/lib/date_range.rb @@ -1 +1,26 @@ require 'date' +require_relative '../lib/reservation' + +module Hotel + class DateRange + + attr_reader :check_in, :check_out + + def initialize(check_in, check_out) + + @check_in = Date.parse(check_in) + @check_out = Date.parse(check_out) + + unless @check_out > @check_in + raise StandardError.new("Check-out can not be before check-in. Chech-out is: #{@check_out} check-in is #{@check_in}") + end + + end + # - Find duration of stay + def duration_of_stay + duration_of_stay = @check_out - @check_in + return duration_of_stay + end + + end +end diff --git a/lib/reservation.rb b/lib/reservation.rb index 3902e28e9..36dfc1c2d 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -1,22 +1,15 @@ -require 'date' +require_relative '../lib/date_range' require 'pry' -module Hotel - class Reservation - - attr_reader :check_in, :check_out, :cost, :reservation_id - def initialize(check_in, check_out, cost) +module Hotel + class Reservation < DateRange + attr_reader :cost, :reservation_id - @check_in = Date.parse(check_in) - @check_out = Date.parse(check_out) + def initialize(cost) @cost = cost.to_f @reservation_id = Reservation.generate_id - - unless @check_out > @check_in - raise StandardError.new("Check-out can not be before check-in. Chech-out is: #{@check_out} check-in is #{@check_in}") - end end # - Generate an reservation ID def self.generate_id @@ -35,12 +28,6 @@ def self.generate_id rand_array.insert(3, "-") return rand_array * "".to_s end - - # - Find duration of stay - def duration_of_stay - duration_of_stay = @check_out - @check_in - return duration_of_stay - end # - Find total cost def total_cost total_cost = @cost * duration_of_stay diff --git a/spec/booking_system_spec.rb b/spec/booking_system_spec.rb index 51bd42c60..47616263b 100644 --- a/spec/booking_system_spec.rb +++ b/spec/booking_system_spec.rb @@ -12,7 +12,6 @@ end it "Takes check_in, check_out, cost, and reservation_id" do - expect(@booking).must_respond_to :rooms expect(@booking).must_respond_to :reservations expect(@booking).must_respond_to :availibility @@ -22,7 +21,6 @@ [:rooms, :reservations, :availibility].each do |initial| expect(@booking).must_respond_to initial end - expect(@booking.rooms).must_be_kind_of Array expect(@booking.reservations).must_be_kind_of Array # expect(@booking_system.availibility).must_be_kind_of String diff --git a/spec/date_range_spec.rb b/spec/date_range_spec.rb index 6cc8481b4..4531725b8 100644 --- a/spec/date_range_spec.rb +++ b/spec/date_range_spec.rb @@ -3,5 +3,51 @@ require 'minitest/reporters' require_relative 'spec_helper' -require_relative '../lib/booking_system.rb' -require_relative '../lib/reservation.rb' +require_relative '../lib/booking_system' +require_relative '../lib/reservation' + + + +describe "initialize" do + before do + @dates = Hotel::DateRange.new(check_in = "2018-02-03", check_out = "2018-02-06") + end + + it "is an instance of Reservation" do + expect(@dates).must_be_kind_of Hotel::DateRange + end + + it "Takes check_in, check_out" do + expect(@dates).must_respond_to :check_in + expect(@dates).must_respond_to :check_out + end + + it "raises a StandardError if check-out is before check-in " do + + expect {Hotel::reservation.new(check_in, check_out)}.must_raise StandardError + end + + it "is set up for specific attributes and data types" do + [:check_in, :check_out].each do |initial| + expect(@dates).must_respond_to initial + end + expect(@dates.check_in).must_be_kind_of Date + expect(@dates.check_out).must_be_kind_of Date + + end + + describe "find_duration_of_stay" do + before do + @dates = Hotel::DateRange.new(check_in = "2018-02-03", check_out = "2018-02-06") + end + + it "finds the duration of stay" do + expect(@dates.duration_of_stay).must_equal 3 + end + + # it "finds the total cost of reservation" do + # expect(@new_res.total_cost).must_equal 600.00 + # end + end + +end diff --git a/spec/reservation_spec.rb b/spec/reservation_spec.rb index d206e6175..b66b85cbc 100644 --- a/spec/reservation_spec.rb +++ b/spec/reservation_spec.rb @@ -9,17 +9,15 @@ describe "initialize" do before do - @new_res = Hotel::Reservation.new(check_in = "2018-02-03" , check_out = "2018-02-06", cost = 600.00) + @new_res = Hotel::Reservation.new(cost = 600.00) end it "is an instance of Reservation" do expect(@new_res).must_be_kind_of Hotel::Reservation end - it "Takes check_in, check_out, cost, and reservation_id" do + it "Takes cost, and reservation_id" do - expect(@new_res).must_respond_to :check_in - expect(@new_res).must_respond_to :check_out expect(@new_res).must_respond_to :cost expect(@new_res).must_respond_to :reservation_id @@ -27,34 +25,34 @@ # raise an error (StandardError) when an invalid date range is provided - it "raises a StandardError if check-out is before check-in " do - - expect {Hotel::reservation.new(check_in, check_out, cost, reservation_id)}.must_raise StandardError - end + # it "raises a StandardError if check-out is before check-in " do + # + # expect {Hotel::reservation.new(check_in, check_out, cost, reservation_id)}.must_raise StandardError + # end it "is set up for specific attributes and data types" do - [:check_in, :check_out, :cost, :reservation_id].each do |initial| + [:cost, :reservation_id].each do |initial| expect(@new_res).must_respond_to initial end expect(@new_res.reservation_id).must_be_kind_of String - expect(@new_res.check_in).must_be_kind_of Date - expect(@new_res.check_out).must_be_kind_of Date + # expect(@new_res.check_in).must_be_kind_of Date + # expect(@new_res.check_out).must_be_kind_of Date expect(@new_res.cost).must_be_kind_of Float end end describe "make a reservation" do before do - @new_res = Hotel::Reservation.new(check_in = "2018-02-03" , check_out = "2018-02-06", cost = 200.00) + @new_res = Hotel::Reservation.new(cost = 200.00) end it "generates a reservation ID" do expect(@new_res.reservation_id.length).must_equal 7 end - it "finds the duration of stay" do - expect(@new_res.duration_of_stay).must_equal 3 - end + # it "finds the duration of stay" do + # expect(@new_res.duration_of_stay).must_equal 3 + # end it "finds the total cost of reservation" do expect(@new_res.total_cost).must_equal 600.00 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 88788d2a4..487218b67 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -10,3 +10,4 @@ require_relative '../lib/booking_system.rb' require_relative '../lib/reservation.rb' +require_relative '../lib/date_range.rb' From 94cee4f3e1dd1849f069fbf17dcc4433d30b0df0 Mon Sep 17 00:00:00 2001 From: Melissa O'Hearn Date: Fri, 7 Sep 2018 16:00:05 -0700 Subject: [PATCH 10/20] added reservations by date method and tests to list reservations by date --- lib/booking_system.rb | 41 ++++++++++++++++++++++++------------- lib/date_range.rb | 10 +++++++-- lib/reservation.rb | 16 +++++++-------- spec/booking_system_spec.rb | 36 ++++++++++++++++++++++++++++++-- spec/date_range_spec.rb | 19 ++++++++++++----- spec/reservation_spec.rb | 16 ++------------- 6 files changed, 92 insertions(+), 46 deletions(-) diff --git a/lib/booking_system.rb b/lib/booking_system.rb index aba48a002..9d1a9823d 100644 --- a/lib/booking_system.rb +++ b/lib/booking_system.rb @@ -7,42 +7,55 @@ module Hotel class BookingSystem attr_reader :rooms, :reservations, :availibility - def initialize (rooms, reservations, availibility) + def initialize @rooms = [ {room_number: 1}, {room_number: 2}, {room_number: 3}, {room_number: 4}, {room_number: 5}, {room_number: 6}, {room_number: 7}, {room_number: 8}, {room_number: 9}, {room_number: 10}, {room_number: 11}, {room_number: 12}, {room_number: 13}, {room_number: 14}, {room_number: 15}, {room_number: 16}, {room_number: 17}, {room_number: 18}, {room_number: 19}, {room_number: 20} ] - @reservations = [] - @availibility = availibility + @reservations = [ + ] + # @availibility = availibility - # keep track of reservations - make sure # of reservations have increased by 1 + #get reservations by date - # method to make_reservation - # return if successful return res_id, or "sucess", or return true + + # keep track of reservations - make sure # of reservations have increased by 1 #method to find an availible room: # input:start, end. output:availible room # use a helper method to check_overlapping dates: # input: reservation attempt: (start date and end date) compare to an existing reservation - # Output: boolean + #edge case: begining of res or end of res can overlap(but check_out and check_in can be same date) #edge case - what if there are no available rooms? - #edge case - - # - # - - - - # methods # - Check availibility of room # - generate a list of availible rooms? # - generate a list of booked rooms? end + + # method to make_reservation + # return if successful return res_id, or "sucess", or return true + def make_reservation(check_in, check_out) + room_number = @rooms.sample[:room_number] + reservation = Hotel::Reservation.new(room_number, 200, check_in, check_out) + @reservations << reservation + return reservation + end + + # list reservations for a specific date + + def reservations_by_date(date) + date = Date.parse(date) + res_by_date = @reservations.select do |res| + res.date_range.included_in_date_range(date) + end + return res_by_date + end end end diff --git a/lib/date_range.rb b/lib/date_range.rb index e353dd5a6..d0dec04db 100644 --- a/lib/date_range.rb +++ b/lib/date_range.rb @@ -16,11 +16,17 @@ def initialize(check_in, check_out) end end - # - Find duration of stay + # -Find duration of stay def duration_of_stay - duration_of_stay = @check_out - @check_in + duration_of_stay = (@check_out - @check_in).to_i return duration_of_stay end + # overlap? method + # include? method + def included_in_date_range(date) + return date.between?(check_in, check_out) + end + end end diff --git a/lib/reservation.rb b/lib/reservation.rb index 36dfc1c2d..e1b0e92c8 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -3,13 +3,16 @@ module Hotel - class Reservation < DateRange + class Reservation - attr_reader :cost, :reservation_id + attr_reader :room_number, :cost, :date_range, :reservation_id - def initialize(cost) + def initialize(room_number, cost, check_in, check_out) + @room_number = room_number @cost = cost.to_f + @date_range = Hotel::DateRange.new(check_in, check_out) @reservation_id = Reservation.generate_id + end # - Generate an reservation ID def self.generate_id @@ -30,14 +33,9 @@ def self.generate_id end # - Find total cost def total_cost - total_cost = @cost * duration_of_stay + total_cost = @cost * @date_range.duration_of_stay return total_cost end - - - - # {res_id: msmsm, cost: $200, nights:2 } - end end diff --git a/spec/booking_system_spec.rb b/spec/booking_system_spec.rb index 47616263b..bb054d353 100644 --- a/spec/booking_system_spec.rb +++ b/spec/booking_system_spec.rb @@ -4,7 +4,7 @@ describe "initialize" do before do - @booking = Hotel::BookingSystem.new(rooms = [], reservations = [], availibility = availibility) + @booking = Hotel::BookingSystem.new() end it "is an instance of Reservation" do @@ -29,12 +29,44 @@ describe "rooms" do before do - @booking = Hotel::BookingSystem.new(rooms = [], reservations = [], availibility = availibility) + @booking = Hotel::BookingSystem.new() end it "Returns an array all of the rooms in the hotel" do expect(@booking.rooms.length).must_equal 20 + # puts @booking.rooms end + + + + describe "make a reservation" do + it "takes check-in and check-out, matches to an availible room, ID and pushes it into reservations array" do + + @booking.make_reservation("2018-02-03", "2018-02-06") + # puts @booking.reservations.first.reservation_id + # puts @booking.reservations.first.total_cost + # puts @booking.reservations + expect(@booking.reservations.length).must_equal 1 + + end + end + describe "list reservations" do + it "lists reservations for a specific date" do + res1 = @booking.make_reservation("2018-02-03", "2018-02-06") + res2 = @booking.make_reservation("2018-04-03", "2018-04-06") + # puts @booking.reservations + + check_day = @booking.reservations_by_date("2018-02-05") + + expect(check_day.length).must_equal 1 + # binding.pry + expect(check_day.first).must_equal res1 + end + end + end + + + end diff --git a/spec/date_range_spec.rb b/spec/date_range_spec.rb index 4531725b8..251f64d2e 100644 --- a/spec/date_range_spec.rb +++ b/spec/date_range_spec.rb @@ -10,7 +10,7 @@ describe "initialize" do before do - @dates = Hotel::DateRange.new(check_in = "2018-02-03", check_out = "2018-02-06") + @dates = Hotel::DateRange.new("2018-02-03", "2018-02-06") end it "is an instance of Reservation" do @@ -38,16 +38,25 @@ describe "find_duration_of_stay" do before do - @dates = Hotel::DateRange.new(check_in = "2018-02-03", check_out = "2018-02-06") + @dates = Hotel::DateRange.new("2018-02-03", "2018-02-06") + # @dates2 = Hotel::DateRange.new("2018-04-03", "2018-04-06") + # @dates3 = Hotel::DateRange.new("2018-06-03", "2018-06-06") end it "finds the duration of stay" do expect(@dates.duration_of_stay).must_equal 3 end - # it "finds the total cost of reservation" do - # expect(@new_res.total_cost).must_equal 600.00 - # end + describe "find if dates are included" do + before do + @dates = Hotel::DateRange.new("2018-02-03", "2018-02-06") + end + it "finds if there are overlapping dates" do + date_to_find = Date.parse("2018-02-04") + reservations_by_date = @dates.included_in_date_range(date_to_find) + expect(reservations_by_date).must_equal true + end + end end end diff --git a/spec/reservation_spec.rb b/spec/reservation_spec.rb index b66b85cbc..fae6969af 100644 --- a/spec/reservation_spec.rb +++ b/spec/reservation_spec.rb @@ -9,7 +9,7 @@ describe "initialize" do before do - @new_res = Hotel::Reservation.new(cost = 600.00) + @new_res = Hotel::Reservation.new(1, 600.00, "2018-02-03", "2018-02-06") end it "is an instance of Reservation" do @@ -23,36 +23,24 @@ end - - # raise an error (StandardError) when an invalid date range is provided - # it "raises a StandardError if check-out is before check-in " do - # - # expect {Hotel::reservation.new(check_in, check_out, cost, reservation_id)}.must_raise StandardError - # end - it "is set up for specific attributes and data types" do [:cost, :reservation_id].each do |initial| expect(@new_res).must_respond_to initial end expect(@new_res.reservation_id).must_be_kind_of String - # expect(@new_res.check_in).must_be_kind_of Date - # expect(@new_res.check_out).must_be_kind_of Date expect(@new_res.cost).must_be_kind_of Float end end describe "make a reservation" do before do - @new_res = Hotel::Reservation.new(cost = 200.00) + @new_res = Hotel::Reservation.new(1, 200.00, "2018-02-03", "2018-02-06") end it "generates a reservation ID" do expect(@new_res.reservation_id.length).must_equal 7 end - # it "finds the duration of stay" do - # expect(@new_res.duration_of_stay).must_equal 3 - # end it "finds the total cost of reservation" do expect(@new_res.total_cost).must_equal 600.00 From 23fe9f2a89380861965a2e32f92442fc9f2dbb39 Mon Sep 17 00:00:00 2001 From: Melissa O'Hearn Date: Sat, 8 Sep 2018 11:15:03 -0700 Subject: [PATCH 11/20] added overlaping method to date range and corresponding test in date_range_spec --- lib/booking_system.rb | 10 +++++--- lib/date_range.rb | 21 +++++++++++++++- lib/reservation.rb | 10 ++++---- spec/booking_system_spec.rb | 48 ++++++++++++++++++------------------- spec/date_range_spec.rb | 22 ++++++++++------- spec/reservation_spec.rb | 8 +++---- 6 files changed, 73 insertions(+), 46 deletions(-) diff --git a/lib/booking_system.rb b/lib/booking_system.rb index 9d1a9823d..da3848021 100644 --- a/lib/booking_system.rb +++ b/lib/booking_system.rb @@ -41,15 +41,14 @@ def initialize # method to make_reservation # return if successful return res_id, or "sucess", or return true - def make_reservation(check_in, check_out) + def make_reservation(cost_per_night, check_in, check_out) room_number = @rooms.sample[:room_number] - reservation = Hotel::Reservation.new(room_number, 200, check_in, check_out) + reservation = Hotel::Reservation.new(room_number, cost_per_night, check_in, check_out) @reservations << reservation return reservation end # list reservations for a specific date - def reservations_by_date(date) date = Date.parse(date) res_by_date = @reservations.select do |res| @@ -57,5 +56,10 @@ def reservations_by_date(date) end return res_by_date end + + # As an administrator, I can view a list of rooms that are available for a given date range + def available_rooms() + + end end end diff --git a/lib/date_range.rb b/lib/date_range.rb index d0dec04db..9a240541a 100644 --- a/lib/date_range.rb +++ b/lib/date_range.rb @@ -22,11 +22,30 @@ def duration_of_stay return duration_of_stay end - # overlap? method # include? method def included_in_date_range(date) return date.between?(check_in, check_out) end + # overlap? method + + # Two date ranges *do* overlap if range A compared to range B: + # - Same dates + # - Overlaps in the front + # - Overlaps in the back + # - Completely contained + # - Completely containing + # + # Two date ranges are *not* overlapping if range A compared to range B: + # - Completely before + # - Completely after + # - Ends on the checkin date + # - Starts on the checkout date + + def overlaps?(date_range) + overlap = @check_in <= date_range.check_out && date_range.check_in <= @check_out + return overlap + end + end end diff --git a/lib/reservation.rb b/lib/reservation.rb index e1b0e92c8..7dbcfc129 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -5,15 +5,15 @@ module Hotel class Reservation - attr_reader :room_number, :cost, :date_range, :reservation_id + attr_reader :room_number, :cost_per_night, :date_range, :reservation_id - def initialize(room_number, cost, check_in, check_out) + def initialize(room_number, cost_per_night, check_in, check_out) @room_number = room_number - @cost = cost.to_f + @cost_per_night = cost_per_night.to_f @date_range = Hotel::DateRange.new(check_in, check_out) @reservation_id = Reservation.generate_id - end + # - Generate an reservation ID def self.generate_id rand_array = [] @@ -33,7 +33,7 @@ def self.generate_id end # - Find total cost def total_cost - total_cost = @cost * @date_range.duration_of_stay + total_cost = @cost_per_night * @date_range.duration_of_stay return total_cost end diff --git a/spec/booking_system_spec.rb b/spec/booking_system_spec.rb index bb054d353..3c10779ff 100644 --- a/spec/booking_system_spec.rb +++ b/spec/booking_system_spec.rb @@ -31,42 +31,42 @@ before do @booking = Hotel::BookingSystem.new() end - it "Returns an array all of the rooms in the hotel" do + it "returns an array all of the rooms in the hotel" do expect(@booking.rooms.length).must_equal 20 # puts @booking.rooms end + it "returns a list of available rooms for a given date range"do - - describe "make a reservation" do - it "takes check-in and check-out, matches to an availible room, ID and pushes it into reservations array" do - - @booking.make_reservation("2018-02-03", "2018-02-06") - # puts @booking.reservations.first.reservation_id - # puts @booking.reservations.first.total_cost - # puts @booking.reservations - expect(@booking.reservations.length).must_equal 1 - - end + expect(@booking.rooms.length).must_equal end - describe "list reservations" do - it "lists reservations for a specific date" do - res1 = @booking.make_reservation("2018-02-03", "2018-02-06") - res2 = @booking.make_reservation("2018-04-03", "2018-04-06") - # puts @booking.reservations - check_day = @booking.reservations_by_date("2018-02-05") + end - expect(check_day.length).must_equal 1 - # binding.pry - expect(check_day.first).must_equal res1 - end + describe "reservations" do + before do + @booking = Hotel::BookingSystem.new() end + it "takes check-in and check-out, matches to an availible room, ID and pushes it into reservations array" do - end - + @booking.make_reservation(200, "2018-02-03", "2018-02-06") + # puts @booking.reservations.first.reservation_id + puts @booking.reservations.first.total_cost + puts @booking.reservations + expect(@booking.reservations.length).must_equal 1 + end + it "lists reservations for a specific date" do + res1 = @booking.make_reservation(200, "2018-02-03", "2018-02-06") + res2 = @booking.make_reservation(200, "2018-04-03", "2018-04-06") + puts res1.total_cost + check_day = @booking.reservations_by_date("2018-02-05") + expect(check_day.length).must_equal 1 + # binding.pry + expect(check_day.first).must_equal res1 + end + end end diff --git a/spec/date_range_spec.rb b/spec/date_range_spec.rb index 251f64d2e..41afd6820 100644 --- a/spec/date_range_spec.rb +++ b/spec/date_range_spec.rb @@ -36,23 +36,27 @@ end - describe "find_duration_of_stay" do - before do - @dates = Hotel::DateRange.new("2018-02-03", "2018-02-06") - # @dates2 = Hotel::DateRange.new("2018-04-03", "2018-04-06") - # @dates3 = Hotel::DateRange.new("2018-06-03", "2018-06-06") - end + describe "date range" do it "finds the duration of stay" do - expect(@dates.duration_of_stay).must_equal 3 + dates = Hotel::DateRange.new("2018-02-03", "2018-02-06") + expect(dates.duration_of_stay).must_equal 3 + end + + it "finds if date ranges overlap" do + dates = Hotel::DateRange.new("2018-02-03", "2018-02-06") + dates2 = Hotel::DateRange.new("2018-04-03", "2018-04-06") + overlaps = dates2.overlaps?(dates) + expect(overlaps).must_equal false + end describe "find if dates are included" do before do @dates = Hotel::DateRange.new("2018-02-03", "2018-02-06") end - it "finds if there are overlapping dates" do - date_to_find = Date.parse("2018-02-04") + it "finds if date is included in date range" do + date_to_find = Date.parse("2018-02-06") reservations_by_date = @dates.included_in_date_range(date_to_find) expect(reservations_by_date).must_equal true end diff --git a/spec/reservation_spec.rb b/spec/reservation_spec.rb index fae6969af..8322c0b4b 100644 --- a/spec/reservation_spec.rb +++ b/spec/reservation_spec.rb @@ -9,7 +9,7 @@ describe "initialize" do before do - @new_res = Hotel::Reservation.new(1, 600.00, "2018-02-03", "2018-02-06") + @new_res = Hotel::Reservation.new(1, 200.00, "2018-02-03", "2018-02-06") end it "is an instance of Reservation" do @@ -18,18 +18,18 @@ it "Takes cost, and reservation_id" do - expect(@new_res).must_respond_to :cost + expect(@new_res).must_respond_to :cost_per_night expect(@new_res).must_respond_to :reservation_id end it "is set up for specific attributes and data types" do - [:cost, :reservation_id].each do |initial| + [:cost_per_night, :reservation_id].each do |initial| expect(@new_res).must_respond_to initial end expect(@new_res.reservation_id).must_be_kind_of String - expect(@new_res.cost).must_be_kind_of Float + expect(@new_res.cost_per_night).must_be_kind_of Float end end From 61e5f8d48ec280af35fbe9e6d30a414748083338 Mon Sep 17 00:00:00 2001 From: Melissa O'Hearn Date: Sat, 8 Sep 2018 11:31:36 -0700 Subject: [PATCH 12/20] added date range overlappig tests in date_range_spec --- spec/date_range_spec.rb | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/spec/date_range_spec.rb b/spec/date_range_spec.rb index 41afd6820..d1af244ff 100644 --- a/spec/date_range_spec.rb +++ b/spec/date_range_spec.rb @@ -48,7 +48,36 @@ dates2 = Hotel::DateRange.new("2018-04-03", "2018-04-06") overlaps = dates2.overlaps?(dates) expect(overlaps).must_equal false - + end + it "finds if ranges overlap if same dates" do + dates = Hotel::DateRange.new("2018-02-03", "2018-02-06") + dates2 = Hotel::DateRange.new("2018-02-03", "2018-02-06") + overlaps = dates2.overlaps?(dates) + expect(overlaps).must_equal true + end + it "finds if ranges overlap in the front" do + dates = Hotel::DateRange.new("2018-02-03", "2018-02-06") + dates2 = Hotel::DateRange.new("2018-02-03", "2018-02-04") + overlaps = dates2.overlaps?(dates) + expect(overlaps).must_equal true + end + it "finds if ranges overlap in the back" do + dates = Hotel::DateRange.new("2018-02-03", "2018-02-06") + dates2 = Hotel::DateRange.new("2018-02-05", "2018-02-07") + overlaps = dates2.overlaps?(dates) + expect(overlaps).must_equal true + end + it "finds if ranges are completely contain" do + dates = Hotel::DateRange.new("2018-02-03", "2018-02-06") + dates2 = Hotel::DateRange.new("2018-02-04", "2018-02-05") + overlaps = dates2.overlaps?(dates) + expect(overlaps).must_equal true + end + it "finds if ranges are completely containing" do + dates = Hotel::DateRange.new("2018-02-05", "2018-02-06") + dates2 = Hotel::DateRange.new("2018-02-04", "2018-02-07") + overlaps = dates2.overlaps?(dates) + expect(overlaps).must_equal true end describe "find if dates are included" do From 6ff80858c3bb28287120118c1c2ed53653e4f1ff Mon Sep 17 00:00:00 2001 From: Melissa O'Hearn Date: Sat, 8 Sep 2018 12:07:32 -0700 Subject: [PATCH 13/20] added not overlapping tests to date_rage_spec --- lib/booking_system.rb | 2 +- lib/date_range.rb | 25 ++++++++++++------------- spec/booking_system_spec.rb | 7 ++++--- spec/date_range_spec.rb | 25 +++++++++++++++++++++++++ 4 files changed, 42 insertions(+), 17 deletions(-) diff --git a/lib/booking_system.rb b/lib/booking_system.rb index da3848021..d1e689aee 100644 --- a/lib/booking_system.rb +++ b/lib/booking_system.rb @@ -57,7 +57,7 @@ def reservations_by_date(date) return res_by_date end - # As an administrator, I can view a list of rooms that are available for a given date range + # As an administrator, I can view a list of rooms that are not available for a given date range def available_rooms() end diff --git a/lib/date_range.rb b/lib/date_range.rb index 9a240541a..3984ec112 100644 --- a/lib/date_range.rb +++ b/lib/date_range.rb @@ -28,22 +28,21 @@ def included_in_date_range(date) end # overlap? method - # Two date ranges *do* overlap if range A compared to range B: - # - Same dates - # - Overlaps in the front - # - Overlaps in the back - # - Completely contained - # - Completely containing - # - # Two date ranges are *not* overlapping if range A compared to range B: - # - Completely before - # - Completely after - # - Ends on the checkin date - # - Starts on the checkout date + # - Same dates + # - Overlaps in the front + # - Overlaps in the back + # - Completely contained + # - Completely containing + # + # Two date ranges are *not* overlapping if range A compared to range B: + # - Completely before + # - Completely after + # - Ends on the checkin date + # - Starts on the checkout date def overlaps?(date_range) - overlap = @check_in <= date_range.check_out && date_range.check_in <= @check_out + overlap = @check_in < date_range.check_out && date_range.check_in < @check_out return overlap end diff --git a/spec/booking_system_spec.rb b/spec/booking_system_spec.rb index 3c10779ff..e6d09da67 100644 --- a/spec/booking_system_spec.rb +++ b/spec/booking_system_spec.rb @@ -38,7 +38,7 @@ it "returns a list of available rooms for a given date range"do - expect(@booking.rooms.length).must_equal + expect(@booking.rooms.length).must_equal 20 end end @@ -61,12 +61,13 @@ res1 = @booking.make_reservation(200, "2018-02-03", "2018-02-06") res2 = @booking.make_reservation(200, "2018-04-03", "2018-04-06") puts res1.total_cost - check_day = @booking.reservations_by_date("2018-02-05") - expect(check_day.length).must_equal 1 # binding.pry expect(check_day.first).must_equal res1 end + + it "lists rooms that are not available for a date range" do + end end end diff --git a/spec/date_range_spec.rb b/spec/date_range_spec.rb index d1af244ff..cde73e473 100644 --- a/spec/date_range_spec.rb +++ b/spec/date_range_spec.rb @@ -79,6 +79,31 @@ overlaps = dates2.overlaps?(dates) expect(overlaps).must_equal true end + it "finds if ranges are not overlpping if completely after eachother" do + dates = Hotel::DateRange.new("2018-02-03", "2018-02-06") + dates2 = Hotel::DateRange.new("2018-02-07", "2018-02-11") + overlaps = dates2.overlaps?(dates) + expect(overlaps).must_equal false + end + it "finds if ranges are not overlapping if completely after eachother" do + dates = Hotel::DateRange.new("2018-02-03", "2018-02-06") + dates2 = Hotel::DateRange.new("2018-02-01", "2018-02-02") + overlaps = dates2.overlaps?(dates) + expect(overlaps).must_equal false + end + it "finds if ranges are not overlapping if one range ends on checkin date " do + dates = Hotel::DateRange.new("2018-02-03", "2018-02-06") + dates2 = Hotel::DateRange.new("2018-02-06", "2018-02-08") + overlaps = dates2.overlaps?(dates) + expect(overlaps).must_equal false + end + it "finds if ranges are not overlapping if one range starts on checkout date " do + dates = Hotel::DateRange.new("2018-02-08", "2018-02-10") + dates2 = Hotel::DateRange.new("2018-02-06", "2018-02-08") + overlaps = dates2.overlaps?(dates) + expect(overlaps).must_equal false + end + describe "find if dates are included" do before do From f8ec590920cc9bbc17ec7dc886fea7581c9d313d Mon Sep 17 00:00:00 2001 From: Melissa O'Hearn Date: Sat, 8 Sep 2018 14:55:31 -0700 Subject: [PATCH 14/20] added list available rooms method and tests --- lib/booking_system.rb | 36 +++++++++++++++++++++++++----------- spec/booking_system_spec.rb | 18 +++++++++++++++--- 2 files changed, 40 insertions(+), 14 deletions(-) diff --git a/lib/booking_system.rb b/lib/booking_system.rb index d1e689aee..155261682 100644 --- a/lib/booking_system.rb +++ b/lib/booking_system.rb @@ -1,11 +1,12 @@ require 'date' require_relative "reservation" require_relative "date_range" +require 'pry' module Hotel class BookingSystem - attr_reader :rooms, :reservations, :availibility + attr_reader :rooms, :reservations def initialize @rooms = @@ -16,21 +17,15 @@ def initialize @reservations = [ ] + # @availibility = availibility #get reservations by date - # keep track of reservations - make sure # of reservations have increased by 1 #method to find an availible room: # input:start, end. output:availible room - - # use a helper method to check_overlapping dates: - # input: reservation attempt: (start date and end date) compare to an existing reservation - - #edge case: begining of res or end of res can overlap(but check_out and check_in can be same date) - #edge case - what if there are no available rooms? # methods @@ -40,7 +35,7 @@ def initialize end # method to make_reservation - # return if successful return res_id, or "sucess", or return true + # return if successful return res_id def make_reservation(cost_per_night, check_in, check_out) room_number = @rooms.sample[:room_number] reservation = Hotel::Reservation.new(room_number, cost_per_night, check_in, check_out) @@ -57,9 +52,28 @@ def reservations_by_date(date) return res_by_date end - # As an administrator, I can view a list of rooms that are not available for a given date range - def available_rooms() + # list reservations for a specific date range + def reservations_by_date_range(date_range) + res_by_date_range = @reservations.select do |res| + res.date_range.overlaps?(date_range) + end + return res_by_date_range + end + + # As an administrator, I can view a list of rooms that are not reserved for a given date range + # reservation date range overlaps with + def list_available_rooms(date_range) + conflicting_reservations = reservations_by_date_range(date_range) + available_rooms = @rooms.reject do |room| + conflicting_reservations.find do |res| + res.room_number == room[:room_number] + end + end + #puts "#{available_rooms}" + return available_rooms end + + end end diff --git a/spec/booking_system_spec.rb b/spec/booking_system_spec.rb index e6d09da67..9867937a3 100644 --- a/spec/booking_system_spec.rb +++ b/spec/booking_system_spec.rb @@ -14,11 +14,11 @@ it "Takes check_in, check_out, cost, and reservation_id" do expect(@booking).must_respond_to :rooms expect(@booking).must_respond_to :reservations - expect(@booking).must_respond_to :availibility + # expect(@booking).must_respond_to :availibility end it "is set up for specific attributes and data types" do - [:rooms, :reservations, :availibility].each do |initial| + [:rooms, :reservations].each do |initial| expect(@booking).must_respond_to initial end expect(@booking.rooms).must_be_kind_of Array @@ -66,8 +66,20 @@ # binding.pry expect(check_day.first).must_equal res1 end + it "lists reservations for a specific date range" do + res1 = @booking.make_reservation(200, "2018-02-03", "2018-02-06") + res2 = @booking.make_reservation(200, "2018-02-06", "2018-04-10") + # puts res1.total_cost + check_day = @booking.reservations_by_date_range(Hotel::DateRange.new("2018-02-05","2018-02-10")) + expect(check_day.length).must_equal 2 + # binding.pry + expect(check_day.first).must_equal res1 + end + + it "lists rooms that are available for a date range" do + res1 = @booking.make_reservation(200, "2018-04-03", "2018-04-06") - it "lists rooms that are not available for a date range" do + expect(@booking.list_available_rooms(Hotel::DateRange.new("2018-04-03", "2018-04-06")).length).must_equal 19 end end end From 4334e896979c1f6ff1f7e2bc6801336850bf28d8 Mon Sep 17 00:00:00 2001 From: Melissa O'Hearn Date: Sat, 8 Sep 2018 15:16:11 -0700 Subject: [PATCH 15/20] raise a standard error if all rooms are booked for a date range --- lib/booking_system.rb | 6 +++++- spec/booking_system_spec.rb | 7 +++++++ spec/date_range_spec.rb | 2 +- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/booking_system.rb b/lib/booking_system.rb index 155261682..2d9b182d8 100644 --- a/lib/booking_system.rb +++ b/lib/booking_system.rb @@ -37,7 +37,11 @@ def initialize # method to make_reservation # return if successful return res_id def make_reservation(cost_per_night, check_in, check_out) - room_number = @rooms.sample[:room_number] + available_rooms = list_available_rooms(Hotel::DateRange.new(check_in, check_out)) + if available_rooms.empty? + raise StandardError, "There's no room in the inn" + end + room_number = available_rooms.sample[:room_number] reservation = Hotel::Reservation.new(room_number, cost_per_night, check_in, check_out) @reservations << reservation return reservation diff --git a/spec/booking_system_spec.rb b/spec/booking_system_spec.rb index 9867937a3..c917b18e5 100644 --- a/spec/booking_system_spec.rb +++ b/spec/booking_system_spec.rb @@ -54,9 +54,16 @@ puts @booking.reservations.first.total_cost puts @booking.reservations expect(@booking.reservations.length).must_equal 1 + end + it "raises an error if no rooms are available" do + 20.times do + @booking.make_reservation(200, "2018-02-03", "2018-02-06") + end + expect { @booking.make_reservation(200, "2018-02-03", "2018-02-06") }.must_raise StandardError end + it "lists reservations for a specific date" do res1 = @booking.make_reservation(200, "2018-02-03", "2018-02-06") res2 = @booking.make_reservation(200, "2018-04-03", "2018-04-06") diff --git a/spec/date_range_spec.rb b/spec/date_range_spec.rb index cde73e473..bfb786e8b 100644 --- a/spec/date_range_spec.rb +++ b/spec/date_range_spec.rb @@ -24,7 +24,7 @@ it "raises a StandardError if check-out is before check-in " do - expect {Hotel::reservation.new(check_in, check_out)}.must_raise StandardError + expect { Hotel::DateRange.new("2018-02-08", "2018-02-06") }.must_raise StandardError end it "is set up for specific attributes and data types" do From 1045d1c2e0ac8a840335bb9a0e71f3ca844d6f79 Mon Sep 17 00:00:00 2001 From: Melissa O'Hearn Date: Sat, 8 Sep 2018 15:33:09 -0700 Subject: [PATCH 16/20] add blocks class and spec file --- lib/blocks.rb | 4 ++++ lib/booking_system.rb | 1 + spec/blocks_spec.rb | 8 ++++++++ 3 files changed, 13 insertions(+) create mode 100644 lib/blocks.rb create mode 100644 spec/blocks_spec.rb diff --git a/lib/blocks.rb b/lib/blocks.rb new file mode 100644 index 000000000..dcb448d3b --- /dev/null +++ b/lib/blocks.rb @@ -0,0 +1,4 @@ +require 'date' +require_relative "reservation" +require_relative "date_range" +require 'pry' diff --git a/lib/booking_system.rb b/lib/booking_system.rb index 2d9b182d8..3e423069c 100644 --- a/lib/booking_system.rb +++ b/lib/booking_system.rb @@ -1,6 +1,7 @@ require 'date' require_relative "reservation" require_relative "date_range" +require_relative "blocks" require 'pry' diff --git a/spec/blocks_spec.rb b/spec/blocks_spec.rb new file mode 100644 index 000000000..c572f3665 --- /dev/null +++ b/spec/blocks_spec.rb @@ -0,0 +1,8 @@ +require 'minitest' +require 'minitest/autorun' +require 'minitest/reporters' + +require_relative 'spec_helper' +require_relative '../lib/booking_system.rb' +require_relative '../lib/reservation.rb' +require_relative '../lib/date_range.rb' From e8b1e841097735ba656b5b9ea1ec3084c799945d Mon Sep 17 00:00:00 2001 From: Melissa O'Hearn Date: Mon, 10 Sep 2018 08:19:45 -0700 Subject: [PATCH 17/20] add blocks.rb initialize and tests --- lib/blocks.rb | 18 ++++++++++++++++++ lib/booking_system.rb | 15 --------------- lib/date_range.rb | 12 ------------ refactors.txt | 0 spec/blocks_spec.rb | 30 ++++++++++++++++++++++++++++++ spec/booking_system_spec.rb | 2 +- spec/reservation_spec.rb | 4 ++++ 7 files changed, 53 insertions(+), 28 deletions(-) create mode 100644 refactors.txt diff --git a/lib/blocks.rb b/lib/blocks.rb index dcb448d3b..d0bf7336f 100644 --- a/lib/blocks.rb +++ b/lib/blocks.rb @@ -2,3 +2,21 @@ require_relative "reservation" require_relative "date_range" require 'pry' + +module Hotel + class Blocks + + # As an administrator, I can create a block of rooms + # To create a block you need a date range, collection of rooms and a discounted room rate + # The collection of rooms should only include rooms that are available for the given date range + # If a room is set aside in a block, it is not available for reservation by the general public, nor can it be included in another block + + attr_reader :collection_of_rooms, :date_range, :cost_per_night + + def initialize(collection_of_rooms, cost_per_night, check_in, check_out) + @collection_of_rooms = collection_of_rooms + @cost_per_night = cost_per_night + @date_range = Hotel::DateRange.new(check_in, check_out) + end + end +end diff --git a/lib/booking_system.rb b/lib/booking_system.rb index 3e423069c..a0816c28a 100644 --- a/lib/booking_system.rb +++ b/lib/booking_system.rb @@ -19,20 +19,6 @@ def initialize ] - # @availibility = availibility - - #get reservations by date - - # keep track of reservations - make sure # of reservations have increased by 1 - - #method to find an availible room: - # input:start, end. output:availible room - #edge case - what if there are no available rooms? - - # methods - # - Check availibility of room - # - generate a list of availible rooms? - # - generate a list of booked rooms? end # method to make_reservation @@ -75,7 +61,6 @@ def list_available_rooms(date_range) res.room_number == room[:room_number] end end - #puts "#{available_rooms}" return available_rooms end diff --git a/lib/date_range.rb b/lib/date_range.rb index 3984ec112..1151f820c 100644 --- a/lib/date_range.rb +++ b/lib/date_range.rb @@ -28,18 +28,6 @@ def included_in_date_range(date) end # overlap? method - # Two date ranges *do* overlap if range A compared to range B: - # - Same dates - # - Overlaps in the front - # - Overlaps in the back - # - Completely contained - # - Completely containing - # - # Two date ranges are *not* overlapping if range A compared to range B: - # - Completely before - # - Completely after - # - Ends on the checkin date - # - Starts on the checkout date def overlaps?(date_range) overlap = @check_in < date_range.check_out && date_range.check_in < @check_out diff --git a/refactors.txt b/refactors.txt new file mode 100644 index 000000000..e69de29bb diff --git a/spec/blocks_spec.rb b/spec/blocks_spec.rb index c572f3665..4fc8ae7a8 100644 --- a/spec/blocks_spec.rb +++ b/spec/blocks_spec.rb @@ -6,3 +6,33 @@ require_relative '../lib/booking_system.rb' require_relative '../lib/reservation.rb' require_relative '../lib/date_range.rb' + +describe "initialize" do + before do + @new_block_res = Hotel::Blocks.new([{room_number: 1}, {room_number: 2}, {room_number: 3}, {room_number: 4}], 200.00, "2018-02-03", "2018-02-06") + end + + it "is an instance of Blocks" do + expect(@new_block_res).must_be_kind_of Hotel::Blocks + end + + it "Takes date_range, collection_of_rooms and cost per night" do + + expect(@new_block_res).must_respond_to :date_range + expect(@new_block_res).must_respond_to :collection_of_rooms + expect(@new_block_res).must_respond_to :cost_per_night + + end + + it "is set up for specific attributes and data types" do + [:date_range, :collection_of_rooms, :cost_per_night].each do |initial| + expect(@new_block_res).must_respond_to initial + end + + expect(@new_block_res.date_range).must_be_kind_of Hotel::DateRange + expect(@new_block_res.collection_of_rooms).must_be_kind_of Array + expect(@new_block_res.cost_per_night).must_be_kind_of Float + + + end +end diff --git a/spec/booking_system_spec.rb b/spec/booking_system_spec.rb index c917b18e5..2001459a2 100644 --- a/spec/booking_system_spec.rb +++ b/spec/booking_system_spec.rb @@ -76,7 +76,7 @@ it "lists reservations for a specific date range" do res1 = @booking.make_reservation(200, "2018-02-03", "2018-02-06") res2 = @booking.make_reservation(200, "2018-02-06", "2018-04-10") - # puts res1.total_cost + puts res1.total_cost check_day = @booking.reservations_by_date_range(Hotel::DateRange.new("2018-02-05","2018-02-10")) expect(check_day.length).must_equal 2 # binding.pry diff --git a/spec/reservation_spec.rb b/spec/reservation_spec.rb index 8322c0b4b..d74eee365 100644 --- a/spec/reservation_spec.rb +++ b/spec/reservation_spec.rb @@ -45,4 +45,8 @@ it "finds the total cost of reservation" do expect(@new_res.total_cost).must_equal 600.00 end + + it "finds the total cost of each room in a block" do + expect(@new_res.discounted_rate).must_equal 480 + end end From 8a819610e979909f8951a614484f640f5db9b87a Mon Sep 17 00:00:00 2001 From: Melissa O'Hearn Date: Mon, 10 Sep 2018 08:28:19 -0700 Subject: [PATCH 18/20] add a refactors.txt --- refactors.txt | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/refactors.txt b/refactors.txt index e69de29bb..7f69360d1 100644 --- a/refactors.txt +++ b/refactors.txt @@ -0,0 +1,12 @@ +- Finish the blocks class. +-- The collection of rooms should only include rooms that are available for the given date range +-- If a room is set aside in a block, it is not available for reservation by the general public, nor can it be included in another block + +- Some names could be changed/condensed: +-- list_available_rooms +-- reservations_by_date_range +-- blocks.rb change to room_blocks? + + +- Tests could be better organized: +-- in date_range, most could fit under "date range" From e1c7963f3bfedc58b68cd84a2621ebc078ed2cb5 Mon Sep 17 00:00:00 2001 From: Melissa O'Hearn Date: Mon, 10 Sep 2018 08:46:46 -0700 Subject: [PATCH 19/20] add refactor notes --- refactors.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/refactors.txt b/refactors.txt index 7f69360d1..fb8da5258 100644 --- a/refactors.txt +++ b/refactors.txt @@ -5,8 +5,9 @@ - Some names could be changed/condensed: -- list_available_rooms -- reservations_by_date_range --- blocks.rb change to room_blocks? +-- blocks.rb change to room_block? - Tests could be better organized: -- in date_range, most could fit under "date range" +-- Tests could have more edge chases - If no date is passed in, From abaacfc285eb3fd8ba9d88fa8745995faa4ca7a9 Mon Sep 17 00:00:00 2001 From: Melissa O'Hearn Date: Mon, 1 Oct 2018 09:10:11 -0700 Subject: [PATCH 20/20] add design-activity --- design-activity.md | 130 +++++++++++++++++++++++++ lib/{blocks.rb => block.rb} | 3 +- lib/booking_system.rb | 23 ++++- lib/reservation.rb | 2 +- spec/{blocks_spec.rb => block_spec.rb} | 4 +- spec/booking_system_spec.rb | 3 + spec/reservation_spec.rb | 6 +- 7 files changed, 160 insertions(+), 11 deletions(-) create mode 100644 design-activity.md rename lib/{blocks.rb => block.rb} (98%) rename spec/{blocks_spec.rb => block_spec.rb} (82%) diff --git a/design-activity.md b/design-activity.md new file mode 100644 index 000000000..a1f16832c --- /dev/null +++ b/design-activity.md @@ -0,0 +1,130 @@ +Implementation A +class CartEntry + attr_accessor :unit_price, :quantity + def initialize(unit_price, quantity) + @unit_price = unit_price + @quantity = quantity + end +end + +class ShoppingCart + attr_accessor :entries + def initialize + @entries = [] + end +end + +class Order + SALES_TAX = 0.07 + def initialize + @cart = ShoppingCart.new + end + + def total_price + sum = 0 + @cart.entries.each do |entry| + sum += entry.unit_price * entry.quantity + end + return sum + sum * SALES_TAX + end +end +Implementation B +class CartEntry + def initialize(unit_price, quantity) + @unit_price = unit_price + @quantity = quantity + end + + def price + return @unit_price * @quantity + end +end + +class ShoppingCart + def initialize + @entries = [] + end + + def price + sum = 0 + @entries.each do |entry| + sum += entry.price + end + return sum + end +end + +class Order + SALES_TAX = 0.07 + def initialize + @cart = ShoppingCart.new + end + + def total_price + subtotal = @cart.price + return subtotal + subtotal * SALES_TAX + end +end + + +What classes does each implementation include? Are the lists the same? +Implementation A classes: +1 - CartEntry +2 - ShoppingCart +3 - Order + +Implementation B classes: +1 - CartEntry +2 - ShoppingCart +3 - Order + +The lists are the same + +Write down a sentence to describe each class. +Implementation A classes: +1 - CartEntry: Initializes a new instance of an item's price and quantity +2 - ShoppingCart: Stores all the entries in an array +3 - Order: Creates a new instance of a ShoppingCart, and calculates the total price of items in the cart + +Implementation B classes: +1 - CartEntry: Same as A +2 - ShoppingCart: Same as A +3 - Order: Same as A + +How do the classes relate to each other? It might be helpful to draw a diagram on a whiteboard or piece of paper. +In A, the CartEntry class instantiates 2 variables, @unit_price and @quantity. These are used in the the Order class to calculate sum. IN the class ShoppingCart, @entries is instantiated. this instance variable is also used in the Order class to also calculate the sum + +In B, CartEntry class instantiates @unit_price and @quantity which are used in a method within the class. This method uses these variables to return a price. In ShoppingCart class, @entries in instantiated and another price method is created, this method uses the price from the CartEntry class to calculate the sum of items in the cart. This price is then used in the Order class to calculate the total price of all items + + +What data does each class store? How (if at all) does this differ between the two implementations? +In A & B, CartEntry stores unit_price and quantity. + ShoppingCart stores entries + Order stores SALES_TAX, cart + + +What methods does each class have? How (if at all) does this differ between the two implementations? +IN A, the only class that has methods is Order. This calculates total_price. + +In B, CartEntry stores price method. + ShoppingCart stores another price method. + Order stores a total_price method like A + +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 A, it is retained in Order +In B, it is delegated to lower level classes + +Does total_price directly manipulate the instance variables of other classes? +In A, total_price does manipulate the instance variables of other classes +In B, total_price does not manipulate them + +If we decide items are cheaper if bought in bulk, how would this change the code? Which implementation is easier to modify? +In B, we could create another class that would have bulk prices that could return a bulk price that could be added to the order class. Or just add a bulk_price to CartEntry that would be added to the price method. +In A, a bulk_price could be added to CartEntry but then Order would also have to be changed to add it there too. + +Which implementation better adheres to the single responsibility principle? +B + +Bonus question once you've read Metz ch. 3: Which implementation is more loosely coupled? +B diff --git a/lib/blocks.rb b/lib/block.rb similarity index 98% rename from lib/blocks.rb rename to lib/block.rb index d0bf7336f..6448d0af0 100644 --- a/lib/blocks.rb +++ b/lib/block.rb @@ -4,7 +4,7 @@ require 'pry' module Hotel - class Blocks + class Block # As an administrator, I can create a block of rooms # To create a block you need a date range, collection of rooms and a discounted room rate @@ -14,6 +14,7 @@ class Blocks attr_reader :collection_of_rooms, :date_range, :cost_per_night def initialize(collection_of_rooms, cost_per_night, check_in, check_out) + @collection_of_rooms = collection_of_rooms @cost_per_night = cost_per_night @date_range = Hotel::DateRange.new(check_in, check_out) diff --git a/lib/booking_system.rb b/lib/booking_system.rb index a0816c28a..ae3bf1e2b 100644 --- a/lib/booking_system.rb +++ b/lib/booking_system.rb @@ -1,7 +1,7 @@ require 'date' require_relative "reservation" require_relative "date_range" -require_relative "blocks" +require_relative "block" require 'pry' @@ -15,9 +15,7 @@ def initialize {room_number: 1}, {room_number: 2}, {room_number: 3}, {room_number: 4}, {room_number: 5}, {room_number: 6}, {room_number: 7}, {room_number: 8}, {room_number: 9}, {room_number: 10}, {room_number: 11}, {room_number: 12}, {room_number: 13}, {room_number: 14}, {room_number: 15}, {room_number: 16}, {room_number: 17}, {room_number: 18}, {room_number: 19}, {room_number: 20} ] - @reservations = [ - - ] + @reservations = [ ] end @@ -34,6 +32,23 @@ def make_reservation(cost_per_night, check_in, check_out) return reservation end + #make block reservation + # error if not between 3-5 rooms + def make_block_reservation(number_of_rooms, cost_per_night, check_in, check_out) + available_rooms = list_available_rooms(Hotel::DateRange.new(check_in, check_out)) + if available_rooms.length < number_of_rooms + raise StandardError, "There's no room in the inn" + end + block_reservation = [] + number_of_rooms.times do + room_number = available_rooms.sample[:room_number] + reservation = Hotel::Reservation.new(room_number, cost_per_night, check_in, check_out) + @reservations << reservation + block_reservation << res + end + return block_reservation + end + # list reservations for a specific date def reservations_by_date(date) date = Date.parse(date) diff --git a/lib/reservation.rb b/lib/reservation.rb index 7dbcfc129..350f3b02e 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -1,4 +1,4 @@ -require_relative '../lib/date_range' +# require_relative '../lib/date_range' require 'pry' diff --git a/spec/blocks_spec.rb b/spec/block_spec.rb similarity index 82% rename from spec/blocks_spec.rb rename to spec/block_spec.rb index 4fc8ae7a8..ea08dcb6e 100644 --- a/spec/blocks_spec.rb +++ b/spec/block_spec.rb @@ -9,11 +9,11 @@ describe "initialize" do before do - @new_block_res = Hotel::Blocks.new([{room_number: 1}, {room_number: 2}, {room_number: 3}, {room_number: 4}], 200.00, "2018-02-03", "2018-02-06") + @new_block_res = Hotel::Block.new([{room_number: 1}, {room_number: 2}, {room_number: 3}, {room_number: 4}], 200.00, "2018-02-03", "2018-02-06") end it "is an instance of Blocks" do - expect(@new_block_res).must_be_kind_of Hotel::Blocks + expect(@new_block_res).must_be_kind_of Hotel::Block end it "Takes date_range, collection_of_rooms and cost per night" do diff --git a/spec/booking_system_spec.rb b/spec/booking_system_spec.rb index 2001459a2..7ec63041a 100644 --- a/spec/booking_system_spec.rb +++ b/spec/booking_system_spec.rb @@ -55,6 +55,9 @@ puts @booking.reservations expect(@booking.reservations.length).must_equal 1 end + # it "Makes a block reservation - takes number of rooms, cost_per_night, check-in, check_out " + + it "raises an error if no rooms are available" do 20.times do @booking.make_reservation(200, "2018-02-03", "2018-02-06") diff --git a/spec/reservation_spec.rb b/spec/reservation_spec.rb index d74eee365..ebf6e4e47 100644 --- a/spec/reservation_spec.rb +++ b/spec/reservation_spec.rb @@ -46,7 +46,7 @@ expect(@new_res.total_cost).must_equal 600.00 end - it "finds the total cost of each room in a block" do - expect(@new_res.discounted_rate).must_equal 480 - end + # it "finds the total cost of each room in a block" do + # expect(@new_res.discounted_rate).must_equal 480 + # end end