From e1faeb925ed89cdb285eaa65f000684408257f5f Mon Sep 17 00:00:00 2001 From: Kiera Date: Mon, 5 Mar 2018 12:33:35 -0800 Subject: [PATCH 01/15] initial commit with skeleton of classes --- .gitignore | 2 ++ Rakefile | 9 +++++++++ lib/hotel.rb | 17 +++++++++++++++++ lib/reservation.rb | 29 +++++++++++++++++++++++++++++ specs/hotel_spec.rb | 1 + specs/reservation_spec.rb | 1 + specs/spec_helper.rb | 11 +++++++++++ 7 files changed, 70 insertions(+) create mode 100644 Rakefile create mode 100644 lib/hotel.rb create mode 100644 lib/reservation.rb create mode 100644 specs/hotel_spec.rb create mode 100644 specs/reservation_spec.rb create mode 100644 specs/spec_helper.rb diff --git a/.gitignore b/.gitignore index 5e1422c9c..1ded4cf46 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ +coverage + *.gem *.rbc /.config diff --git a/Rakefile b/Rakefile new file mode 100644 index 000000000..deb52f2cd --- /dev/null +++ b/Rakefile @@ -0,0 +1,9 @@ +require 'rake/testtask' + +Rake::TestTask.new do |t| + t.libs = ["lib"] + t.warning = true + t.test_files = FileList['specs/*_spec.rb'] +end + +task default: :test diff --git a/lib/hotel.rb b/lib/hotel.rb new file mode 100644 index 000000000..9a13b62be --- /dev/null +++ b/lib/hotel.rb @@ -0,0 +1,17 @@ + +class hotel + + def initialize + @rooms = [] + @reservations = [] + + RATE = 200 + end + + + + # list of all rooms (available and unavailable) + + + # list of reservations +end diff --git a/lib/reservation.rb b/lib/reservation.rb new file mode 100644 index 000000000..dc36f94f0 --- /dev/null +++ b/lib/reservation.rb @@ -0,0 +1,29 @@ + +class Reservations + + def initialize + @id = + @start_date = + @end_date = + @cost = + @days_booked = + + + + end + + +# ask for list reservations + +# find an available room + +# change room status + +# calculate days booked + +# calculate total cost of reservation + + + + +end diff --git a/specs/hotel_spec.rb b/specs/hotel_spec.rb new file mode 100644 index 000000000..ae9c220ea --- /dev/null +++ b/specs/hotel_spec.rb @@ -0,0 +1 @@ +require_relative 'spec_helper' diff --git a/specs/reservation_spec.rb b/specs/reservation_spec.rb new file mode 100644 index 000000000..ae9c220ea --- /dev/null +++ b/specs/reservation_spec.rb @@ -0,0 +1 @@ +require_relative 'spec_helper' diff --git a/specs/spec_helper.rb b/specs/spec_helper.rb new file mode 100644 index 000000000..af47715d3 --- /dev/null +++ b/specs/spec_helper.rb @@ -0,0 +1,11 @@ +require 'simplecov' +SimpleCov.start + +require 'minitest/autorun' +require 'minitest/reporters' +require_relative '../lib/card' +require_relative '../lib/deck' + + + +Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new From 398e59873a779fb6769d035e443b616eabadf071 Mon Sep 17 00:00:00 2001 From: Kiera Date: Mon, 5 Mar 2018 14:36:04 -0800 Subject: [PATCH 02/15] initialize methods and tests created and passed for both reservation and hotel claslasses --- lib/hotel.rb | 20 +++++++++++++------- lib/reservation.rb | 29 ++++++++++++++++------------- specs/hotel_spec.rb | 8 ++++++++ specs/reservation_spec.rb | 9 +++++++++ specs/spec_helper.rb | 4 ++-- 5 files changed, 48 insertions(+), 22 deletions(-) diff --git a/lib/hotel.rb b/lib/hotel.rb index 9a13b62be..ca2bd2a52 100644 --- a/lib/hotel.rb +++ b/lib/hotel.rb @@ -1,17 +1,23 @@ +# class hotel should store and manage the room and reservation lists +# class hotel should store the fixed hotel RATE -class hotel - +class Hotel +attr_accessor :rooms, :reservations +# the ability to create a hotel instance def initialize - @rooms = [] + @rooms = [] # 1-20 @reservations = [] - RATE = 200 end + # access a list of all rooms (available and unavailable) + + + # access a list of all reservations + + # find an available room for the new reservation - # list of all rooms (available and unavailable) - - # list of reservations + # change room status to unavailable after creating a new reservation end diff --git a/lib/reservation.rb b/lib/reservation.rb index dc36f94f0..111b1fd75 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -1,27 +1,30 @@ -class Reservations +class Reservation +# RATE 200 +# the ability to create a reservation +# raise an error for the incorrect date range def initialize - @id = - @start_date = - @end_date = - @cost = - @days_booked = + @id = :id + @start_date = :start_date + @end_date = :end_date + @cost = :cost + @days_booked = 0 + end # initialize +# access a list of all reservations - end +# get an available room from hotel for the new reservation -# ask for list reservations -# find an available room +# calculate days booked to help determine the cost of the reservation +# days_booked should not include the last day of the reservation +# checkout day -# change room status -# calculate days booked - -# calculate total cost of reservation +# calculate total cost of reservation using the days booked and RATE diff --git a/specs/hotel_spec.rb b/specs/hotel_spec.rb index ae9c220ea..5b25e86b6 100644 --- a/specs/hotel_spec.rb +++ b/specs/hotel_spec.rb @@ -1 +1,9 @@ require_relative 'spec_helper' + +describe 'initialize' do + it "can create an instance of hotel" do + new_building = Hotel.new + + new_building.must_be_instance_of Hotel + end +end diff --git a/specs/reservation_spec.rb b/specs/reservation_spec.rb index ae9c220ea..b718c0f64 100644 --- a/specs/reservation_spec.rb +++ b/specs/reservation_spec.rb @@ -1 +1,10 @@ require_relative 'spec_helper' + +describe 'initialize' do + it "can create an instance of reservation" do + + new_reservation = Reservation.new + + new_reservation.must_be_instance_of Reservation + end +end diff --git a/specs/spec_helper.rb b/specs/spec_helper.rb index af47715d3..70753a2be 100644 --- a/specs/spec_helper.rb +++ b/specs/spec_helper.rb @@ -3,8 +3,8 @@ require 'minitest/autorun' require 'minitest/reporters' -require_relative '../lib/card' -require_relative '../lib/deck' +require_relative '../lib/hotel' +require_relative '../lib/reservation' From 96980c86d71014dd50f04e51fa36696ed8a8d23f Mon Sep 17 00:00:00 2001 From: Kiera Date: Tue, 6 Mar 2018 12:25:05 -0800 Subject: [PATCH 03/15] made edits to everything reservation list not quite working --- lib/hotel.rb | 33 ++++++++++++++------ lib/reservation.rb | 63 ++++++++++++++++++++++++++------------- specs/hotel_spec.rb | 2 ++ specs/reservation_spec.rb | 34 ++++++++++++++++++--- specs/spec_helper.rb | 3 ++ 5 files changed, 101 insertions(+), 34 deletions(-) diff --git a/lib/hotel.rb b/lib/hotel.rb index ca2bd2a52..609464110 100644 --- a/lib/hotel.rb +++ b/lib/hotel.rb @@ -1,23 +1,38 @@ +require_relative 'reservation' +require 'pry' + # class hotel should store and manage the room and reservation lists -# class hotel should store the fixed hotel RATE class Hotel -attr_accessor :rooms, :reservations -# the ability to create a hotel instance + attr_accessor :rooms, :reservations + # the ability to create a hotel instance def initialize - @rooms = [] # 1-20 + @rooms = %w[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20]# 1-20 @reservations = [] - end # access a list of all rooms (available and unavailable) - # access a list of all reservations + # returns a list of all reservations + # def reservations + # hotel.reservations + # end # find an available room for the new reservation - - - # change room status to unavailable after creating a new reservation + # go through the rooms array to pick a room number + # if the room number is not included in the reservations array + # assign the room number to the new reservation + def find_available_room + if reservations.length == 0 + rooms.sample + else + rooms.fetch { |room| reservations.room_number.include? == false} + end + return rooms + end end + +# new_hot = Hotel.new +# new_hot.find_available_room diff --git a/lib/reservation.rb b/lib/reservation.rb index 111b1fd75..8a7d7ecbf 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -1,31 +1,52 @@ +require_relative 'hotel' +require 'date' +require 'pry' class Reservation -# RATE 200 -# the ability to create a reservation -# raise an error for the incorrect date range - def initialize - @id = :id - @start_date = :start_date - @end_date = :end_date - @cost = :cost - @days_booked = 0 - end # initialize - - -# access a list of all reservations - + RATE = 200 -# get an available room from hotel for the new reservation + # the ability to create a reservation + # raise an error for the incorrect date range + def initialize(start_date, end_date) + @id = rand(001..999) + @room_number = :room_number + @start_date = start_date + @end_date = end_date + @cost = days_booked * RATE + @reservations = [] + # raise ArgumentError.new('Invalid date') if start_date.valid_date? == false || end_date.valid_date == false -# calculate days booked to help determine the cost of the reservation -# days_booked should not include the last day of the reservation -# checkout day - - -# calculate total cost of reservation using the days booked and RATE + # @reservations << Reservation.new + end # initialize + # load a list of all rooms with reservations and the reservation details + # reservations come from the hotel + # return reservations + # def load_reservations + # reserved_rooms = Hotel.reservations + # return reserved_rooms + # end + + # get an available room from hotel for the new reservation + # def add_room + # find_room = Hotel.find_available_room + # return find_room + # end + + # calculate days booked to help determine the cost of the reservation + # days_booked should not include the last day of the reservation + # checkout day days_booked is the (end_date - start_date) + def days_booked + @end_date - @start_date + end + + # calculate total cost of reservation using the days booked and RATE + def total_cost + total_cost = days_booked * RATE + return total_cost + end diff --git a/specs/hotel_spec.rb b/specs/hotel_spec.rb index 5b25e86b6..ff5f9d5c0 100644 --- a/specs/hotel_spec.rb +++ b/specs/hotel_spec.rb @@ -1,4 +1,6 @@ require_relative 'spec_helper' +gem 'minitest', '>= 5.0.0' +require 'minitest/pride' describe 'initialize' do it "can create an instance of hotel" do diff --git a/specs/reservation_spec.rb b/specs/reservation_spec.rb index b718c0f64..9f4091a03 100644 --- a/specs/reservation_spec.rb +++ b/specs/reservation_spec.rb @@ -1,10 +1,36 @@ require_relative 'spec_helper' +gem 'minitest', '>= 5.0.0' +require 'minitest/pride' +require 'pry' -describe 'initialize' do - it "can create an instance of reservation" do +describe 'reservations class' do - new_reservation = Reservation.new + describe 'initialize' do + it "can create an instance of reservation" do - new_reservation.must_be_instance_of Reservation + new_reservation = Reservation.new(Date.new(2018,2,5), Date.new(2018,2,6)) + new_reservation.must_be_instance_of Reservation + end + + describe 'load_reservations' do + + it "can list all reservations made" do + + new_hotel = Hotel.new + + reservations = [ + reservation_001 = Reservation.new(start_date = Date.new(2018,3,12),end_date = Date.new(2018,3,16)), + reservation_002 = Reservation.new(start_date = Date.new(2018,3,14), end_date = Date.new(2018,3,20)), + reservation_003 = Reservation.new(start_date = Date.new(2018,4,12), end_date = Date.new(2018,3,16)) + ] + + + new_hotel.reservations + @reservations.must_be_kind_of Array + @reservations.must_equal reservations + + + end + end end end diff --git a/specs/spec_helper.rb b/specs/spec_helper.rb index 70753a2be..f21648442 100644 --- a/specs/spec_helper.rb +++ b/specs/spec_helper.rb @@ -1,8 +1,11 @@ require 'simplecov' SimpleCov.start +gem 'minitest', '>= 5.0.0' +require 'minitest/pride' require 'minitest/autorun' require 'minitest/reporters' + require_relative '../lib/hotel' require_relative '../lib/reservation' From 99ee92ba7302ff85420dc85fad38e692331ade08 Mon Sep 17 00:00:00 2001 From: Kiera Date: Tue, 6 Mar 2018 16:24:36 -0800 Subject: [PATCH 04/15] moved/removed and tests from reservation class to hotel class. Added find_available_room and create_reservation. --- lib/hotel.rb | 26 +++++++++++++++----------- lib/reservation.rb | 26 +++----------------------- specs/hotel_spec.rb | 12 +++++++++++- specs/reservation_spec.rb | 20 -------------------- 4 files changed, 29 insertions(+), 55 deletions(-) diff --git a/lib/hotel.rb b/lib/hotel.rb index 609464110..c9132c362 100644 --- a/lib/hotel.rb +++ b/lib/hotel.rb @@ -7,17 +7,19 @@ class Hotel attr_accessor :rooms, :reservations # the ability to create a hotel instance def initialize - @rooms = %w[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20]# 1-20 + @rooms = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]# 1-20 @reservations = [] end - # access a list of all rooms (available and unavailable) - + # access a list of all rooms + # def list_rooms + # print rooms + # end # returns a list of all reservations # def reservations - # hotel.reservations + # @reservations # end # find an available room for the new reservation @@ -26,13 +28,15 @@ def initialize # assign the room number to the new reservation def find_available_room if reservations.length == 0 - rooms.sample - else - rooms.fetch { |room| reservations.room_number.include? == false} + @rooms.sample + return rooms end - return rooms end -end -# new_hot = Hotel.new -# new_hot.find_available_room + def create_reservation(start_date, end_date) + find_available_room + new_res = reservation.new(room, start_date, end_date) + # shovel reservation into hotel reservations + @reservations << new_res + end +end diff --git a/lib/reservation.rb b/lib/reservation.rb index 8a7d7ecbf..aac5a3356 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -8,32 +8,14 @@ class Reservation # the ability to create a reservation # raise an error for the incorrect date range - def initialize(start_date, end_date) - @id = rand(001..999) - @room_number = :room_number + def initialize(start_date, end_date, room_number) + @room_number = room_number @start_date = start_date @end_date = end_date - @cost = days_booked * RATE - @reservations = [] # raise ArgumentError.new('Invalid date') if start_date.valid_date? == false || end_date.valid_date == false - - # @reservations << Reservation.new end # initialize - # load a list of all rooms with reservations and the reservation details - # reservations come from the hotel - # return reservations - # def load_reservations - # reserved_rooms = Hotel.reservations - # return reserved_rooms - # end - - # get an available room from hotel for the new reservation - # def add_room - # find_room = Hotel.find_available_room - # return find_room - # end # calculate days booked to help determine the cost of the reservation # days_booked should not include the last day of the reservation @@ -48,6 +30,4 @@ def total_cost return total_cost end - - -end +end #class diff --git a/specs/hotel_spec.rb b/specs/hotel_spec.rb index ff5f9d5c0..f4260f24d 100644 --- a/specs/hotel_spec.rb +++ b/specs/hotel_spec.rb @@ -5,7 +5,17 @@ describe 'initialize' do it "can create an instance of hotel" do new_building = Hotel.new - new_building.must_be_instance_of Hotel end + + it "has a list of 20 rooms" do + new_building = Hotel.new + new_building.rooms.length.must_equal 20 + end + + it "has a empty list of reservations" do + new_building = Hotel.new + new_building.reservations.length.must_equal 0 + end + end diff --git a/specs/reservation_spec.rb b/specs/reservation_spec.rb index 9f4091a03..bb6c44e6e 100644 --- a/specs/reservation_spec.rb +++ b/specs/reservation_spec.rb @@ -12,25 +12,5 @@ new_reservation.must_be_instance_of Reservation end - describe 'load_reservations' do - - it "can list all reservations made" do - - new_hotel = Hotel.new - - reservations = [ - reservation_001 = Reservation.new(start_date = Date.new(2018,3,12),end_date = Date.new(2018,3,16)), - reservation_002 = Reservation.new(start_date = Date.new(2018,3,14), end_date = Date.new(2018,3,20)), - reservation_003 = Reservation.new(start_date = Date.new(2018,4,12), end_date = Date.new(2018,3,16)) - ] - - - new_hotel.reservations - @reservations.must_be_kind_of Array - @reservations.must_equal reservations - - - end - end end end From 0a8f4cc55b0f1c4969e8499d00528f7bf3d3231f Mon Sep 17 00:00:00 2001 From: Kiera Date: Tue, 6 Mar 2018 18:04:18 -0800 Subject: [PATCH 05/15] added test for find_available_room. making progress on code for verifying if the room is available --- lib/hotel.rb | 22 ++++-------- specs/hotel_spec.rb | 70 +++++++++++++++++++++++++++++++++------ specs/reservation_spec.rb | 13 +++++++- 3 files changed, 78 insertions(+), 27 deletions(-) diff --git a/lib/hotel.rb b/lib/hotel.rb index c9132c362..c9a2b40ad 100644 --- a/lib/hotel.rb +++ b/lib/hotel.rb @@ -12,15 +12,6 @@ def initialize end - # access a list of all rooms - # def list_rooms - # print rooms - # end - - # returns a list of all reservations - # def reservations - # @reservations - # end # find an available room for the new reservation # go through the rooms array to pick a room number @@ -28,15 +19,16 @@ def initialize # assign the room number to the new reservation def find_available_room if reservations.length == 0 - @rooms.sample - return rooms + room_number = @rooms.sample + else + room_number = @rooms.sample + # @rooms.collect { |room_number| } + return room_number end end def create_reservation(start_date, end_date) - find_available_room - new_res = reservation.new(room, start_date, end_date) - # shovel reservation into hotel reservations - @reservations << new_res + new_res = Reservation.new(start_date, end_date,find_available_room) + reservations << new_res end end diff --git a/specs/hotel_spec.rb b/specs/hotel_spec.rb index f4260f24d..a8f4572e8 100644 --- a/specs/hotel_spec.rb +++ b/specs/hotel_spec.rb @@ -1,21 +1,69 @@ require_relative 'spec_helper' gem 'minitest', '>= 5.0.0' require 'minitest/pride' +require 'awesome_print' +require 'pry' -describe 'initialize' do - it "can create an instance of hotel" do - new_building = Hotel.new - new_building.must_be_instance_of Hotel - end +describe 'hotel class' do + + describe '#initialize' do + it "can create an instance of hotel" do + new_building = Hotel.new + new_building.must_be_instance_of Hotel + end + + it "has a list of 20 rooms" do + new_building = Hotel.new + new_building.rooms.length.must_equal 20 + end - it "has a list of 20 rooms" do - new_building = Hotel.new - new_building.rooms.length.must_equal 20 + it "has a empty list of reservations" do + new_building = Hotel.new + new_building.reservations.length.must_equal 0 + end end - it "has a empty list of reservations" do - new_building = Hotel.new - new_building.reservations.length.must_equal 0 + describe 'find_available_room' do + it "can find a random room within the correct room numbers" do + new_building = Hotel.new + room_number = new_building.find_available_room + + room_number.must_be :>,0 + room_number.must_be :<,20 + end + + it "can find a room when there is one or more reservations" do + new_building = Hotel.new + room_number = new_building.find_available_room + + new_reservation = new_building.create_reservation(Date.new(2018,3,6),Date.new(2018,3,9)) + new_reservation = new_building.create_reservation(Date.new(2018,3,7),Date.new(2018,3,10)) + new_reservation = new_building.create_reservation(Date.new(2018,3,8),Date.new(2018,3,11)) + + ap new_building.reservations + # binding.pry + # new_building.reservations[room_number].must_be :>,0 + # new_building.reservations[room_number].must_be :<,20 + end end + describe 'create_reservation' do + it "can create a reservation" do + new_building = Hotel.new + new_reservation = new_building.create_reservation(Date.new(2018,3,6),Date.new(2018,3,9)) + + new_building.reservations.length.must_equal 1 + new_building.reservations.length.must_be :>, 0 + end + + it "can create multiple reservations" do + new_building = Hotel.new + new_reservation = new_building.create_reservation(Date.new(2018,3,6),Date.new(2018,3,9)) + new_reservation = new_building.create_reservation(Date.new(2018,3,7),Date.new(2018,3,10)) + new_reservation = new_building.create_reservation(Date.new(2018,3,8),Date.new(2018,3,11)) + + new_reservation.length.must_equal 3 + new_building.reservations.length.must_be :>, 2 + end + end end diff --git a/specs/reservation_spec.rb b/specs/reservation_spec.rb index bb6c44e6e..4120f53e1 100644 --- a/specs/reservation_spec.rb +++ b/specs/reservation_spec.rb @@ -8,9 +8,20 @@ describe 'initialize' do it "can create an instance of reservation" do - new_reservation = Reservation.new(Date.new(2018,2,5), Date.new(2018,2,6)) + new_reservation = Reservation.new(Date.new(2018,2,5), Date.new(2018,2,6),3) new_reservation.must_be_instance_of Reservation end + end + + describe 'days_booked' do + it "calculates days booked correctly" do + end + end + + describe 'total_cost' do + it "calculates total_cost correctly" do + + end end end From 68569f0c16230fc91f3b2fba1b979dfdbb348f80 Mon Sep 17 00:00:00 2001 From: Kiera Date: Wed, 7 Mar 2018 14:50:47 -0800 Subject: [PATCH 06/15] progress on room available psuedocode, completed and passed total cost and days booked tests --- lib/hotel.rb | 4 +++- lib/reservation.rb | 4 ++++ specs/hotel_spec.rb | 2 +- specs/reservation_spec.rb | 4 ++++ 4 files changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/hotel.rb b/lib/hotel.rb index c9a2b40ad..4a6f230b8 100644 --- a/lib/hotel.rb +++ b/lib/hotel.rb @@ -4,7 +4,7 @@ # class hotel should store and manage the room and reservation lists class Hotel - attr_accessor :rooms, :reservations + attr_reader :rooms, :reservations # the ability to create a hotel instance def initialize @rooms = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]# 1-20 @@ -21,6 +21,8 @@ def find_available_room if reservations.length == 0 room_number = @rooms.sample else + # look to overlap? to determine whether a reservation overlaps with another and if not i,e false room number is then available. + room_number = @rooms.sample # @rooms.collect { |room_number| } return room_number diff --git a/lib/reservation.rb b/lib/reservation.rb index aac5a3356..2c22cc573 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -3,6 +3,7 @@ require 'pry' class Reservation +attr_reader :start_date, :end_date RATE = 200 @@ -16,6 +17,9 @@ def initialize(start_date, end_date, room_number) # raise ArgumentError.new('Invalid date') if start_date.valid_date? == false || end_date.valid_date == false end # initialize +# overlap? method reservation needs to take in date range and determine if it overlaps with self any other reservations ie boolean output + +# # calculate days booked to help determine the cost of the reservation # days_booked should not include the last day of the reservation diff --git a/specs/hotel_spec.rb b/specs/hotel_spec.rb index a8f4572e8..506fdbe55 100644 --- a/specs/hotel_spec.rb +++ b/specs/hotel_spec.rb @@ -40,7 +40,7 @@ new_reservation = new_building.create_reservation(Date.new(2018,3,7),Date.new(2018,3,10)) new_reservation = new_building.create_reservation(Date.new(2018,3,8),Date.new(2018,3,11)) - ap new_building.reservations + # ap new_building.reservations # binding.pry # new_building.reservations[room_number].must_be :>,0 # new_building.reservations[room_number].must_be :<,20 diff --git a/specs/reservation_spec.rb b/specs/reservation_spec.rb index 4120f53e1..c3dd07aab 100644 --- a/specs/reservation_spec.rb +++ b/specs/reservation_spec.rb @@ -15,13 +15,17 @@ describe 'days_booked' do it "calculates days booked correctly" do + new_reservation = Reservation.new(Date.new(2018,3,10), Date.new(2018,3,15),3) + new_reservation.days_booked.must_equal 5 end end describe 'total_cost' do it "calculates total_cost correctly" do + new_reservation = Reservation.new(Date.new(2018,5,10), Date.new(2018,5,20),9) + new_reservation.total_cost.must_equal 2000 end end end From 063aa7751ce524943a2394799f185bab0a4e38de Mon Sep 17 00:00:00 2001 From: Kiera Date: Wed, 7 Mar 2018 16:56:33 -0800 Subject: [PATCH 07/15] still making progress on testing reservation overlap dates and finding an available le room --- lib/hotel.rb | 25 +++++++++++++++++++------ lib/reservation.rb | 7 ++----- specs/hotel_spec.rb | 11 +++++------ specs/reservation_spec.rb | 2 +- 4 files changed, 27 insertions(+), 18 deletions(-) diff --git a/lib/hotel.rb b/lib/hotel.rb index 4a6f230b8..84716d50e 100644 --- a/lib/hotel.rb +++ b/lib/hotel.rb @@ -12,7 +12,6 @@ def initialize end - # find an available room for the new reservation # go through the rooms array to pick a room number # if the room number is not included in the reservations array @@ -21,16 +20,30 @@ def find_available_room if reservations.length == 0 room_number = @rooms.sample else - # look to overlap? to determine whether a reservation overlaps with another and if not i,e false room number is then available. - - room_number = @rooms.sample - # @rooms.collect { |room_number| } - return room_number + # go through @rooms array and sample a room_number + # with that room_number call reserevation_overlap? + # determine whether the new reservations date range overlaps + # with reservations in the reservation array + # until false return room number is then available. + @rooms.each do |room| + room_number = rooms.sample + unless false + room_number.reservation_overlap?(Reservation.new(start_date,end_date)) + end + end + return room_number end end + # take in a room_number + def create_reservation(start_date, end_date) new_res = Reservation.new(start_date, end_date,find_available_room) reservations << new_res end + + # overlap? method reservation needs to take in date range and determine if it overlaps with self any other reservations ie boolean output + def reservation_overlap?(start_date, end_date) + return false if start_date >= end_date || end_date <= start_date + end end diff --git a/lib/reservation.rb b/lib/reservation.rb index 2c22cc573..1c8324bd0 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -17,20 +17,17 @@ def initialize(start_date, end_date, room_number) # raise ArgumentError.new('Invalid date') if start_date.valid_date? == false || end_date.valid_date == false end # initialize -# overlap? method reservation needs to take in date range and determine if it overlaps with self any other reservations ie boolean output - -# # calculate days booked to help determine the cost of the reservation # days_booked should not include the last day of the reservation # checkout day days_booked is the (end_date - start_date) def days_booked - @end_date - @start_date + end_date - start_date end # calculate total cost of reservation using the days booked and RATE def total_cost - total_cost = days_booked * RATE + total_cost = (days_booked - 1)* RATE return total_cost end diff --git a/specs/hotel_spec.rb b/specs/hotel_spec.rb index 506fdbe55..c2f751cdf 100644 --- a/specs/hotel_spec.rb +++ b/specs/hotel_spec.rb @@ -32,18 +32,17 @@ room_number.must_be :<,20 end - it "can find a room when there is one or more reservations" do + it "can find an available room when there is one or more reservations" do new_building = Hotel.new room_number = new_building.find_available_room new_reservation = new_building.create_reservation(Date.new(2018,3,6),Date.new(2018,3,9)) - new_reservation = new_building.create_reservation(Date.new(2018,3,7),Date.new(2018,3,10)) - new_reservation = new_building.create_reservation(Date.new(2018,3,8),Date.new(2018,3,11)) + new_reservation = new_building.create_reservation(Date.new(2018,3,7),Date.new(2018,3,8)) - # ap new_building.reservations + ap new_building.reservations # binding.pry - # new_building.reservations[room_number].must_be :>,0 - # new_building.reservations[room_number].must_be :<,20 + new_building.reservations.room_number.must_be :>,0 + new_building.reservations.room_number.must_be :<,20 end end diff --git a/specs/reservation_spec.rb b/specs/reservation_spec.rb index c3dd07aab..1b9998000 100644 --- a/specs/reservation_spec.rb +++ b/specs/reservation_spec.rb @@ -25,7 +25,7 @@ it "calculates total_cost correctly" do new_reservation = Reservation.new(Date.new(2018,5,10), Date.new(2018,5,20),9) - new_reservation.total_cost.must_equal 2000 + new_reservation.total_cost.must_equal 1800 end end end From 08ee7dbd3712b6e9434ebcaa4ba73b12bb9774b2 Mon Sep 17 00:00:00 2001 From: Kiera Date: Thu, 8 Mar 2018 19:50:27 -0800 Subject: [PATCH 08/15] made changes to create reservation method and find a room method also updated tests tto reflect changes in methods --- lib/hotel.rb | 24 ++++++++++++++++++------ lib/reservation.rb | 2 +- specs/hotel_spec.rb | 39 ++++++++++++++++++++++++++------------- specs/reservation_spec.rb | 8 ++++++++ 4 files changed, 53 insertions(+), 20 deletions(-) diff --git a/lib/hotel.rb b/lib/hotel.rb index 84716d50e..3028bdb99 100644 --- a/lib/hotel.rb +++ b/lib/hotel.rb @@ -27,17 +27,29 @@ def find_available_room # until false return room number is then available. @rooms.each do |room| room_number = rooms.sample - unless false - room_number.reservation_overlap?(Reservation.new(start_date,end_date)) - end + # until false + reservation_overlap?(Reservation.new(start_date,end_date)) + # room_number. + # end end - return room_number end + return room_number end - # take in a room_number + # take in a date and list the reservations during that date + def list_reservations(date) + # pass in a date + # go through the reservations array and list all reservations + # during that date range + reservations.each do |reservation| + date.between?(start_date, end_date) + return reservations + end + end - def create_reservation(start_date, end_date) + def create_reservation(reservation) + start_date = reservation.start_date + end_date = reservation.end_date new_res = Reservation.new(start_date, end_date,find_available_room) reservations << new_res end diff --git a/lib/reservation.rb b/lib/reservation.rb index 1c8324bd0..396be12fb 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -14,7 +14,7 @@ def initialize(start_date, end_date, room_number) @start_date = start_date @end_date = end_date - # raise ArgumentError.new('Invalid date') if start_date.valid_date? == false || end_date.valid_date == false + # raise ArgumentError.new('Invalid date') if Date.valid_date?(start_date) == false || Date.valid_date?(end_date) == false end # initialize diff --git a/specs/hotel_spec.rb b/specs/hotel_spec.rb index c2f751cdf..3b5e94b1f 100644 --- a/specs/hotel_spec.rb +++ b/specs/hotel_spec.rb @@ -32,24 +32,26 @@ room_number.must_be :<,20 end - it "can find an available room when there is one or more reservations" do - new_building = Hotel.new - room_number = new_building.find_available_room - - new_reservation = new_building.create_reservation(Date.new(2018,3,6),Date.new(2018,3,9)) - new_reservation = new_building.create_reservation(Date.new(2018,3,7),Date.new(2018,3,8)) - - ap new_building.reservations - # binding.pry - new_building.reservations.room_number.must_be :>,0 - new_building.reservations.room_number.must_be :<,20 - end + # it "can find an available room when there is one or more reservations" do + # new_building = Hotel.new + # # room_number = new_building.find_available_room + # + # new_reservation = new_building.create_reservation(Date.new(2018,3,6),Date.new(2018,3,9)) + # new_reservation_2 = new_building.create_reservation(Date.new(2018,3,7),Date.new(2018,3,8)) + # + # # ap new_building.reservations + # # binding.pry + # # new_building.reservations.room_number.must_be :>,0 + # # new_building.reservations.room_number.must_be :<,20 + # new_building.new_reservation.room_number.wont_be_same_as new_building.new_reservation_2.room_number + # end end describe 'create_reservation' do it "can create a reservation" do new_building = Hotel.new - new_reservation = new_building.create_reservation(Date.new(2018,3,6),Date.new(2018,3,9)) + Reservation.new(Date.new(2018,3,6),Date.new(2018,3,9)) + new_building.create_reservation(data) new_building.reservations.length.must_equal 1 new_building.reservations.length.must_be :>, 0 @@ -65,4 +67,15 @@ new_building.reservations.length.must_be :>, 2 end end + + describe 'list_reservations' do + it "will list all reservations during the date passed in" do + new_building = Hotel.new + new_reservation = new_building.create_reservation(Date.new(2018,6,1),Date.new(2018,6,6)) + + res = new_building.list_reservations(Date.new(2018,6,2)) + res.must_be_kind_of Array + new_building.reservations.length.must_equal 1 + end + end end diff --git a/specs/reservation_spec.rb b/specs/reservation_spec.rb index 1b9998000..b15b9ff5d 100644 --- a/specs/reservation_spec.rb +++ b/specs/reservation_spec.rb @@ -11,6 +11,14 @@ new_reservation = Reservation.new(Date.new(2018,2,5), Date.new(2018,2,6),3) new_reservation.must_be_instance_of Reservation end + + # it "raises and ArgumentError if start_date or end_date are invalid" do + # new_res = Reservation.new(Date.new(2018,2,5), Date.new(2018,2,30),3) + # + # proc{ + # new_res + # }.must_raise ArgumentError + # end end describe 'days_booked' do From 0783009968791c4a89e24e4b9f494d62e5044a68 Mon Sep 17 00:00:00 2001 From: Kiera Date: Fri, 9 Mar 2018 14:03:51 -0800 Subject: [PATCH 09/15] updated test to reflect method parameter changes. updated and tested list_reservations method to take in a date and output only reservations during that time --- lib/hotel.rb | 26 ++++++++++++++------------ specs/hotel_spec.rb | 32 +++++++++++++++++++++++--------- 2 files changed, 37 insertions(+), 21 deletions(-) diff --git a/lib/hotel.rb b/lib/hotel.rb index 3028bdb99..fa03fe09f 100644 --- a/lib/hotel.rb +++ b/lib/hotel.rb @@ -19,19 +19,19 @@ def initialize def find_available_room if reservations.length == 0 room_number = @rooms.sample - else + # else # go through @rooms array and sample a room_number # with that room_number call reserevation_overlap? # determine whether the new reservations date range overlaps # with reservations in the reservation array # until false return room number is then available. - @rooms.each do |room| - room_number = rooms.sample - # until false - reservation_overlap?(Reservation.new(start_date,end_date)) - # room_number. - # end - end + # @rooms.each do |room| + # rooms.sample + # room_number = rooms.sample + # until false + # reservation_overlap?(reservation.start_date,reservation.end_date) + # end + # end end return room_number end @@ -39,11 +39,13 @@ def find_available_room # take in a date and list the reservations during that date def list_reservations(date) # pass in a date - # go through the reservations array and list all reservations - # during that date range + # go through the reservations array and list all reservations during that date range + list = [] reservations.each do |reservation| - date.between?(start_date, end_date) - return reservations + if date.between?(reservation.start_date,reservation.end_date) + list << reservation + end + return list end end diff --git a/specs/hotel_spec.rb b/specs/hotel_spec.rb index 3b5e94b1f..f88d1bbdf 100644 --- a/specs/hotel_spec.rb +++ b/specs/hotel_spec.rb @@ -32,7 +32,7 @@ room_number.must_be :<,20 end - # it "can find an available room when there is one or more reservations" do + # it "can find an available room when there is one or more reservation" do # new_building = Hotel.new # # room_number = new_building.find_available_room # @@ -50,8 +50,8 @@ describe 'create_reservation' do it "can create a reservation" do new_building = Hotel.new - Reservation.new(Date.new(2018,3,6),Date.new(2018,3,9)) - new_building.create_reservation(data) + new_res = Reservation.new(Date.new(2018,3,6),Date.new(2018,3,9),3) + new_building.create_reservation(new_res) new_building.reservations.length.must_equal 1 new_building.reservations.length.must_be :>, 0 @@ -59,23 +59,37 @@ it "can create multiple reservations" do new_building = Hotel.new - new_reservation = new_building.create_reservation(Date.new(2018,3,6),Date.new(2018,3,9)) - new_reservation = new_building.create_reservation(Date.new(2018,3,7),Date.new(2018,3,10)) - new_reservation = new_building.create_reservation(Date.new(2018,3,8),Date.new(2018,3,11)) - new_reservation.length.must_equal 3 + new_res_1 = Reservation.new(Date.new(2018,3,6),Date.new(2018,3,9),2) + new_res_2 = Reservation.new(Date.new(2018,3,7),Date.new(2018,3,10),7) + new_res_3 = Reservation.new(Date.new(2018,3,8),Date.new(2018,3,11),20) + + new_building.create_reservation(new_res_1) + new_building.create_reservation(new_res_2) + new_building.create_reservation(new_res_3) + + new_building.reservations.length.must_equal 3 new_building.reservations.length.must_be :>, 2 + # ap new_building.reservations end end describe 'list_reservations' do it "will list all reservations during the date passed in" do new_building = Hotel.new - new_reservation = new_building.create_reservation(Date.new(2018,6,1),Date.new(2018,6,6)) + + new_reservation1 = Reservation.new(Date.new(2018,6,1),Date.new(2018,6,6),1) + new_reservation = Reservation.new(Date.new(2018,9,1),Date.new(2018,9,6),1) + + new_building.create_reservation(new_reservation1) + new_building.create_reservation(new_reservation) + res = new_building.list_reservations(Date.new(2018,6,2)) res.must_be_kind_of Array - new_building.reservations.length.must_equal 1 + + res.length.must_equal 1 + end end end From c641961ac6bdc62f1943a2352e360a204b42ab36 Mon Sep 17 00:00:00 2001 From: Kiera Date: Fri, 9 Mar 2018 14:58:32 -0800 Subject: [PATCH 10/15] completed and passed can find available room when there is one of more reservation ato mmake sure the room numbers are not the same. --- lib/hotel.rb | 6 ++++-- lib/reservation.rb | 2 +- specs/hotel_spec.rb | 28 +++++++++++++++------------- 3 files changed, 20 insertions(+), 16 deletions(-) diff --git a/lib/hotel.rb b/lib/hotel.rb index fa03fe09f..5f31057c1 100644 --- a/lib/hotel.rb +++ b/lib/hotel.rb @@ -19,7 +19,7 @@ def initialize def find_available_room if reservations.length == 0 room_number = @rooms.sample - # else + else # go through @rooms array and sample a room_number # with that room_number call reserevation_overlap? # determine whether the new reservations date range overlaps @@ -52,8 +52,10 @@ def list_reservations(date) def create_reservation(reservation) start_date = reservation.start_date end_date = reservation.end_date - new_res = Reservation.new(start_date, end_date,find_available_room) + room_number = find_available_room + new_res = Reservation.new(start_date, end_date,room_number) reservations << new_res + return new_res end # overlap? method reservation needs to take in date range and determine if it overlaps with self any other reservations ie boolean output diff --git a/lib/reservation.rb b/lib/reservation.rb index 396be12fb..0fe7c20cd 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -3,7 +3,7 @@ require 'pry' class Reservation -attr_reader :start_date, :end_date +attr_reader :start_date, :end_date, :room_number RATE = 200 diff --git a/specs/hotel_spec.rb b/specs/hotel_spec.rb index f88d1bbdf..d0d59bbf3 100644 --- a/specs/hotel_spec.rb +++ b/specs/hotel_spec.rb @@ -32,19 +32,21 @@ room_number.must_be :<,20 end - # it "can find an available room when there is one or more reservation" do - # new_building = Hotel.new - # # room_number = new_building.find_available_room - # - # new_reservation = new_building.create_reservation(Date.new(2018,3,6),Date.new(2018,3,9)) - # new_reservation_2 = new_building.create_reservation(Date.new(2018,3,7),Date.new(2018,3,8)) - # - # # ap new_building.reservations - # # binding.pry - # # new_building.reservations.room_number.must_be :>,0 - # # new_building.reservations.room_number.must_be :<,20 - # new_building.new_reservation.room_number.wont_be_same_as new_building.new_reservation_2.room_number - # end + it "can find an available room when there is one or more reservation" do + new_building = Hotel.new + room_number = new_building.find_available_room + + new_reservation = Reservation.new(Date.new(2018,3,6),Date.new(2018,3,9),1) + new_reservation_2 = Reservation.new(Date.new(2018,3,7),Date.new(2018,3,8),1) + + new_res = new_building.create_reservation(new_reservation_2) + new_res_1 = new_building.create_reservation(new_reservation) + + # ap new_building.reservations + # binding.pry + + new_res.room_number.wont_be_same_as new_res_1.room_number + end end describe 'create_reservation' do From 34b16411da60e8eed12081ecfdb0dd429efe9ccf Mon Sep 17 00:00:00 2001 From: Kiera Date: Fri, 9 Mar 2018 19:18:24 -0800 Subject: [PATCH 11/15] completed logic for find_available room method if reservation had existing reservations and the dates overlaped. update reservation overlap to take in one parameter. completed and passed tests to check that find_available_method is working as intended. --- lib/hotel.rb | 26 ++++++++++++++------------ specs/hotel_spec.rb | 18 +++++++++++++++--- 2 files changed, 29 insertions(+), 15 deletions(-) diff --git a/lib/hotel.rb b/lib/hotel.rb index 5f31057c1..426c87710 100644 --- a/lib/hotel.rb +++ b/lib/hotel.rb @@ -13,8 +13,6 @@ def initialize # find an available room for the new reservation - # go through the rooms array to pick a room number - # if the room number is not included in the reservations array # assign the room number to the new reservation def find_available_room if reservations.length == 0 @@ -25,17 +23,19 @@ def find_available_room # determine whether the new reservations date range overlaps # with reservations in the reservation array # until false return room number is then available. - # @rooms.each do |room| - # rooms.sample - # room_number = rooms.sample - # until false - # reservation_overlap?(reservation.start_date,reservation.end_date) - # end - # end + @rooms.each do |room| + room_number = rooms.sample + reservations.each do |reservation| + if reservation_overlap?(reservation) == true + end + end + end + return room_number end - return room_number + end + # take in a date and list the reservations during that date def list_reservations(date) # pass in a date @@ -58,8 +58,10 @@ def create_reservation(reservation) return new_res end - # overlap? method reservation needs to take in date range and determine if it overlaps with self any other reservations ie boolean output - def reservation_overlap?(start_date, end_date) + # overlap? reservation needs to take in a date range and determine if it overlaps with self any other reservations ie boolean output + def reservation_overlap?(reservation) + start_date = reservation.start_date + end_date = reservation.end_date return false if start_date >= end_date || end_date <= start_date end end diff --git a/specs/hotel_spec.rb b/specs/hotel_spec.rb index d0d59bbf3..aa3e9bac1 100644 --- a/specs/hotel_spec.rb +++ b/specs/hotel_spec.rb @@ -32,6 +32,21 @@ room_number.must_be :<,20 end + it "can find an available room when dates overlap with existing reservation" do + + new_building = Hotel.new + new_reservation = Reservation.new(Date.new(2018,3,6),Date.new(2018,3,9),1) + res_0 = new_building.create_reservation(new_reservation) + + new_reservation_2 = Reservation.new(Date.new(2018,3,6),Date.new(2018,3,9),1) + + res_1 = new_building.create_reservation(new_reservation_2) + # ap new_building.reservations + + res_1.room_number.wont_be_same_as res_0.room_number + + end + it "can find an available room when there is one or more reservation" do new_building = Hotel.new room_number = new_building.find_available_room @@ -42,9 +57,6 @@ new_res = new_building.create_reservation(new_reservation_2) new_res_1 = new_building.create_reservation(new_reservation) - # ap new_building.reservations - # binding.pry - new_res.room_number.wont_be_same_as new_res_1.room_number end end From 54b33145b9b9446bc207ee6c1f8b9045359230bb Mon Sep 17 00:00:00 2001 From: Kiera Date: Sat, 10 Mar 2018 19:34:45 -0800 Subject: [PATCH 12/15] updated reservation initialize to handle incorrect date ranges --- lib/reservation.rb | 11 ++++++++++- specs/reservation_spec.rb | 19 ++++++++++++------- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/lib/reservation.rb b/lib/reservation.rb index 0fe7c20cd..26b5328ae 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -14,9 +14,18 @@ def initialize(start_date, end_date, room_number) @start_date = start_date @end_date = end_date - # raise ArgumentError.new('Invalid date') if Date.valid_date?(start_date) == false || Date.valid_date?(end_date) == false + valid_date end # initialize + #validate the dates for checkin and checkout + def valid_date + unless @end_date == nil || @start_date == nil + if @end_date < @start_date + raise ArgumentError.new('Invalid date range') + end + end + end + # calculate days booked to help determine the cost of the reservation # days_booked should not include the last day of the reservation diff --git a/specs/reservation_spec.rb b/specs/reservation_spec.rb index b15b9ff5d..83c41145c 100644 --- a/specs/reservation_spec.rb +++ b/specs/reservation_spec.rb @@ -12,13 +12,18 @@ new_reservation.must_be_instance_of Reservation end - # it "raises and ArgumentError if start_date or end_date are invalid" do - # new_res = Reservation.new(Date.new(2018,2,5), Date.new(2018,2,30),3) - # - # proc{ - # new_res - # }.must_raise ArgumentError - # end + it "raises and ArgumentError if end_date is before start_date" do + + start_date = Date.new(2018,2,20), + end_date = Date.new(2018,2,2), + room_number = 4 + + + proc { + new_res = Reservation.new(start_date, end_date, room_number) + }.must_raise ArgumentError + + end end describe 'days_booked' do From 8dc191c96358b498f9d216228bfe240ad94da977 Mon Sep 17 00:00:00 2001 From: Kiera Date: Sat, 10 Mar 2018 19:41:41 -0800 Subject: [PATCH 13/15] updated the way an error was being raised for incorrect date range --- lib/reservation.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/reservation.rb b/lib/reservation.rb index 26b5328ae..5ec8e1be9 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -20,9 +20,7 @@ def initialize(start_date, end_date, room_number) #validate the dates for checkin and checkout def valid_date unless @end_date == nil || @start_date == nil - if @end_date < @start_date - raise ArgumentError.new('Invalid date range') - end + raise ArgumentError.new('Invalid date range') if @end_date < @start_date end end From a6b882245e9560b51bf4221147f026810f936ee6 Mon Sep 17 00:00:00 2001 From: Kiera Date: Tue, 27 Mar 2018 12:49:54 -0700 Subject: [PATCH 14/15] added design activity to files and initally completed the questions --- design_activity.md | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 design_activity.md diff --git a/design_activity.md b/design_activity.md new file mode 100644 index 000000000..0fffc62b3 --- /dev/null +++ b/design_activity.md @@ -0,0 +1,36 @@ +What classes does each implementation include? Are the lists the same? + Each implementation has the same classes CartEntry, ShoppingCart, and Order. + +Write down a sentence to describe each class. + CartEntry: the CartEntry is responsible for keeping track of the unit price and quantity of each cart item. + ShoppingCart:The shopping car is responsible for storing all the entries in an array. + Order: The order is responsible for storing the sales tax, creating a new shopping cart, and calculating the total price of each cart. + +How do the classes relate to each other? It might be helpful to draw a diagram on a whiteboard or piece of paper. + Each class relies on another to either fill in its data or to use in a method to calculate data. + +What data does each class store? How (if at all) does this differ between the two implementations? + ShoppingCart is the only class that stores data it stores all the entries. this is the same among implementations + +What methods does each class have? How (if at all) does this differ between the two +implementations? + In implementation A Order is the only class that contains a method outside of initialize and it has a total_price method. + + In implementation B all of the classes have a method. CartEntry and ShoppingCart have a price method, which differ from each other and order class has a total_price method. + +Consider the Order#total_price method. In each implementation: + +Is logic to compute the price delegated to "lower level" classes like ShoppingCart and CartEntry, or is it retained in Order? + It is delegated to lower level classes + +Does total_price directly manipulate the instance variables of other classes? + yes. + +If we decide items are cheaper if bought in bulk, how would this change the code? Which implementation is easier to modify? + The CartEntry class would change to include constants with bulk pricing. this could possibly be done with a method and if bulk pricing the equation of total price would differ. I think implementation A would be easier to modify because it refers to unit_price and quantity only once outside of the CartEntry class. + +Which implementation better adheres to the single responsibility principle? + Implementation A + +Bonus question once you've read Metz ch. 3: Which implementation is more loosely coupled? + Implementation A From 609b4e2d9028af8cd8a3e08d7e45a38803d569e2 Mon Sep 17 00:00:00 2001 From: Kiera Date: Thu, 29 Mar 2018 15:17:49 -0700 Subject: [PATCH 15/15] completely revamped hotel class to meet the requirements for Wave 2, also modified tests --- design_activity.md | 5 ++ lib/hotel.rb | 75 +++++++++------------ lib/reservation.rb | 13 +++- lib/rooms.rb | 0 specs/hotel_spec.rb | 137 +++++++++++++++++++++++++------------- specs/reservation_spec.rb | 1 + 6 files changed, 136 insertions(+), 95 deletions(-) create mode 100644 lib/rooms.rb diff --git a/design_activity.md b/design_activity.md index 0fffc62b3..53f50d8be 100644 --- a/design_activity.md +++ b/design_activity.md @@ -34,3 +34,8 @@ Which implementation better adheres to the single responsibility principle? Bonus question once you've read Metz ch. 3: Which implementation is more loosely coupled? Implementation A + + + Identify one place in your Hotel project where a class takes on multiple roles, or directly modifies the attributes of another class. + Describe in design-activity.md what changes you would need to make to improve this design, and how the resulting design would be an improvement. + The hotel class takes on multiple roles, it does not modify attributes from the reservation class but, along with storing the reservations and listing the reservations and rooms available it also determines whether a room is available which could be something that a room class could do. To improve this design I would add a room class which would take on storing the rooms and managing whether they were available or not. Then the hotel could call the method in the rooms class as it does in the reservations class to determine whether the room is available and add it to a reservation and/or list available rooms depending on what admin needed to do. diff --git a/lib/hotel.rb b/lib/hotel.rb index 426c87710..7ce42c04a 100644 --- a/lib/hotel.rb +++ b/lib/hotel.rb @@ -1,4 +1,5 @@ require_relative 'reservation' +require_relative 'rooms' require 'pry' # class hotel should store and manage the room and reservation lists @@ -12,56 +13,42 @@ def initialize end - # find an available room for the new reservation - # assign the room number to the new reservation - def find_available_room - if reservations.length == 0 - room_number = @rooms.sample - else - # go through @rooms array and sample a room_number - # with that room_number call reserevation_overlap? - # determine whether the new reservations date range overlaps - # with reservations in the reservation array - # until false return room number is then available. - @rooms.each do |room| - room_number = rooms.sample - reservations.each do |reservation| - if reservation_overlap?(reservation) == true - end - end - end - return room_number + # As an administrator, I can reserve a room for a given date range + # As an administrator, I can reserve an available room for a given date range + def create_reservation(start_date, end_date,room) + # create instance of reservation + # store the reservation + unless room_available?(start_date, end_date, room) + raise ArgumentError.new("Room number #{room} is not available") end + new_reservation = Reservation.new(start_date, end_date, room) + @reservations << new_reservation end - # take in a date and list the reservations during that date - def list_reservations(date) - # pass in a date - # go through the reservations array and list all reservations during that date range - list = [] - reservations.each do |reservation| - if date.between?(reservation.start_date,reservation.end_date) - list << reservation - end - return list - end - end - def create_reservation(reservation) - start_date = reservation.start_date - end_date = reservation.end_date - room_number = find_available_room - new_res = Reservation.new(start_date, end_date,room_number) - reservations << new_res - return new_res + # As an administrator, I can access the list of reservations for a specific date + def list_reservations_for_specific_date(date) + # find reservations that contain the date + @reservations.select { |reservation| reservation.contains?(date) } end - # overlap? reservation needs to take in a date range and determine if it overlaps with self any other reservations ie boolean output - def reservation_overlap?(reservation) - start_date = reservation.start_date - end_date = reservation.end_date - return false if start_date >= end_date || end_date <= start_date + # As an administrator, I can view a list of rooms that are not reserved for a given date range + def list_rooms_available(start_date,end_date) + rooms.select {|room| room_available?(start_date, end_date, room) } end -end + + def room_available?(start_date, end_date,room) + # find all reservations for room + # check reservations for date + select_rooms = reservations.select { |reservation| reservation.room_number == room } + + select_rooms.each do |res| + if res.contains?(start_date) || res.contains?(end_date) || (res.start_date >= start_date && res.end_date <= end_date) + return false + end + end + return true + end #room_available +end #class diff --git a/lib/reservation.rb b/lib/reservation.rb index 5ec8e1be9..4bc4af489 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -1,4 +1,4 @@ -require_relative 'hotel' + require 'date' require 'pry' @@ -14,16 +14,23 @@ def initialize(start_date, end_date, room_number) @start_date = start_date @end_date = end_date - valid_date + + date_valid? end # initialize #validate the dates for checkin and checkout - def valid_date + def date_valid? unless @end_date == nil || @start_date == nil raise ArgumentError.new('Invalid date range') if @end_date < @start_date end end + def contains?(date) + # reservation dates are included or not included in params + # room is reserved or available + date >= start_date && date <= end_date + end + # calculate days booked to help determine the cost of the reservation # days_booked should not include the last day of the reservation diff --git a/lib/rooms.rb b/lib/rooms.rb new file mode 100644 index 000000000..e69de29bb diff --git a/specs/hotel_spec.rb b/specs/hotel_spec.rb index aa3e9bac1..8173de203 100644 --- a/specs/hotel_spec.rb +++ b/specs/hotel_spec.rb @@ -23,87 +23,128 @@ end end - describe 'find_available_room' do - it "can find a random room within the correct room numbers" do + describe 'create_reservation' do + it "can create a reservation" do new_building = Hotel.new - room_number = new_building.find_available_room - room_number.must_be :>,0 - room_number.must_be :<,20 - end + new_res = new_building.create_reservation(Date.new(2018,3,6),Date.new(2018,3,9),3) - it "can find an available room when dates overlap with existing reservation" do + new_building.reservations.length.must_equal 1 + new_building.reservations.length.must_be :>, 0 + end + it "can create multiple reservations" do new_building = Hotel.new - new_reservation = Reservation.new(Date.new(2018,3,6),Date.new(2018,3,9),1) - res_0 = new_building.create_reservation(new_reservation) - - new_reservation_2 = Reservation.new(Date.new(2018,3,6),Date.new(2018,3,9),1) - res_1 = new_building.create_reservation(new_reservation_2) - # ap new_building.reservations + new_building.create_reservation(Date.new(2018,3,6),Date.new(2018,3,9),2) + new_building.create_reservation(Date.new(2018,3,7),Date.new(2018,3,10),7) + new_building.create_reservation(Date.new(2018,3,8),Date.new(2018,3,11),20) - res_1.room_number.wont_be_same_as res_0.room_number + new_building.reservations.length.must_equal 3 + new_building.reservations.length.must_be :>, 2 end - it "can find an available room when there is one or more reservation" do + it "raises an ArgumentError when a room is not avaialable" do new_building = Hotel.new - room_number = new_building.find_available_room - new_reservation = Reservation.new(Date.new(2018,3,6),Date.new(2018,3,9),1) - new_reservation_2 = Reservation.new(Date.new(2018,3,7),Date.new(2018,3,8),1) + proc { new_building.create_reservation(Date.new(2018,3,6),Date.new(2018,3,9),1) + new_building.create_reservation(Date.new(2018,3,7),Date.new(2018,3,10),1) + }.must_raise ArgumentError + end - new_res = new_building.create_reservation(new_reservation_2) - new_res_1 = new_building.create_reservation(new_reservation) + it "raises an ArgumentError when all the rooms are books" do + new_building = Hotel.new - new_res.room_number.wont_be_same_as new_res_1.room_number + new_building.create_reservation(Date.new(2018,3,6),Date.new(2018,3,9),1) + new_building.create_reservation(Date.new(2018,3,7),Date.new(2018,3,10),2) + new_building.create_reservation(Date.new(2018,3,8),Date.new(2018,3,11),3) + new_building.create_reservation(Date.new(2018,3,6),Date.new(2018,3,9),4) + new_building.create_reservation(Date.new(2018,3,7),Date.new(2018,3,10),5) + new_building.create_reservation(Date.new(2018,3,8),Date.new(2018,3,11),6) + new_building.create_reservation(Date.new(2018,3,6),Date.new(2018,3,9),7) + new_building.create_reservation(Date.new(2018,3,7),Date.new(2018,3,10),8) + new_building.create_reservation(Date.new(2018,3,8),Date.new(2018,3,11),9) + new_building.create_reservation(Date.new(2018,3,6),Date.new(2018,3,9),10) + new_building.create_reservation(Date.new(2018,3,7),Date.new(2018,3,10),11) + new_building.create_reservation(Date.new(2018,3,8),Date.new(2018,3,11),12) + new_building.create_reservation(Date.new(2018,3,6),Date.new(2018,3,9),13) + new_building.create_reservation(Date.new(2018,3,7),Date.new(2018,3,10),14) + new_building.create_reservation(Date.new(2018,3,8),Date.new(2018,3,11),15) + new_building.create_reservation(Date.new(2018,3,6),Date.new(2018,3,9),16) + new_building.create_reservation(Date.new(2018,3,7),Date.new(2018,3,10),17) + new_building.create_reservation(Date.new(2018,3,8),Date.new(2018,3,11),18) + new_building.create_reservation(Date.new(2018,3,6),Date.new(2018,3,9),19) + new_building.create_reservation(Date.new(2018,3,7),Date.new(2018,3,10),20) + + proc { new_building.create_reservation(Date.new(2018,3,8),Date.new(2018,3,11),3) + }.must_raise ArgumentError end + end - describe 'create_reservation' do - it "can create a reservation" do + describe 'list_reservations_for_specific_date' do + it "will list all reservations during the date passed in" do new_building = Hotel.new - new_res = Reservation.new(Date.new(2018,3,6),Date.new(2018,3,9),3) - new_building.create_reservation(new_res) - new_building.reservations.length.must_equal 1 - new_building.reservations.length.must_be :>, 0 - end - it "can create multiple reservations" do - new_building = Hotel.new + new_building.create_reservation(Date.new(2018,4,2),Date.new(2018,4,7),1) + new_building.create_reservation(Date.new(2018,9,1),Date.new(2018,9,6),1) - new_res_1 = Reservation.new(Date.new(2018,3,6),Date.new(2018,3,9),2) - new_res_2 = Reservation.new(Date.new(2018,3,7),Date.new(2018,3,10),7) - new_res_3 = Reservation.new(Date.new(2018,3,8),Date.new(2018,3,11),20) - new_building.create_reservation(new_res_1) - new_building.create_reservation(new_res_2) - new_building.create_reservation(new_res_3) + res = new_building.list_reservations_for_specific_date(Date.new(2018,4,4)) + res.must_be_kind_of Array + + res.length.must_equal 1 + + res_2 = new_building.list_reservations_for_specific_date(Date.new(2017,4,4)) + res_2.length.must_equal 0 + - new_building.reservations.length.must_equal 3 - new_building.reservations.length.must_be :>, 2 - # ap new_building.reservations end + #two reservations that overlap and make sure it displays 2 end - describe 'list_reservations' do - it "will list all reservations during the date passed in" do + + describe 'list_rooms_available' do + it "can list all available rooms" do + new_building = Hotel.new + + res_1 = new_building.create_reservation(Date.new(2018,4,2),Date.new(2018,4,7),2) + res_2 = new_building.create_reservation(Date.new(2018,4,1),Date.new(2018,4,6),1) + res_3 = new_building.create_reservation(Date.new(2018,4,3),Date.new(2018,4,5),3) + # new_building.create_reservation(Date.new(2018,5,7),Date.new(2018,5,14),1) + + list = new_building.list_rooms_available(Date.new(2018,4,3),Date.new(2018,4,6)) + + list.must_be_kind_of Array + list.wont_include 2 + list.wont_include 1 + list.wont_include 3 + end + end + + describe 'room_available?' do + it "returns true when room is available" do new_building = Hotel.new - new_reservation1 = Reservation.new(Date.new(2018,6,1),Date.new(2018,6,6),1) - new_reservation = Reservation.new(Date.new(2018,9,1),Date.new(2018,9,6),1) + res_0 = new_building.create_reservation(Date.new(2018,3,6),Date.new(2018,3,9),1) + res_1 = new_building.create_reservation(Date.new(2018,4,2),Date.new(2018,4,7),2) - new_building.create_reservation(new_reservation1) - new_building.create_reservation(new_reservation) + res_2 = new_building.room_available?(Date.new(2018,3,6),Date.new(2018,3,9),2) + res_2.must_equal true + end - res = new_building.list_reservations(Date.new(2018,6,2)) - res.must_be_kind_of Array + it "returns false when a room is not available" do + new_building = Hotel.new - res.length.must_equal 1 + res_0 = new_building.create_reservation(Date.new(2018,3,6),Date.new(2018,3,9),1) + res_1 = new_building.create_reservation(Date.new(2018,4,2),Date.new(2018,4,7),2) + + res_2 = new_building.room_available?(Date.new(2018,3,6),Date.new(2018,3,9),1) + res_2.must_equal false end end end diff --git a/specs/reservation_spec.rb b/specs/reservation_spec.rb index 83c41145c..be4af501e 100644 --- a/specs/reservation_spec.rb +++ b/specs/reservation_spec.rb @@ -41,4 +41,5 @@ new_reservation.total_cost.must_equal 1800 end end + end