From 1558e9920105fd0da974fd23f95cb1a04b22b3b9 Mon Sep 17 00:00:00 2001 From: nicoletabrandolini Date: Mon, 5 Mar 2018 16:19:54 -0800 Subject: [PATCH 01/13] Created files and initialize methods and test --- Rakefile | 9 +++++++++ lib/frontdesk.rb | 28 ++++++++++++++++++++++++++++ lib/reservation.rb | 30 ++++++++++++++++++++++++++++++ lib/room.rb | 5 +++++ specs/frontdesk_spec.rb | 1 + specs/reservation_spec.rb | 14 ++++++++++++++ specs/room_spec.rb | 1 + specs/spec_helper.rb | 15 +++++++++++++++ 8 files changed, 103 insertions(+) create mode 100644 Rakefile create mode 100644 lib/frontdesk.rb create mode 100644 lib/reservation.rb create mode 100644 lib/room.rb create mode 100644 specs/frontdesk_spec.rb create mode 100644 specs/reservation_spec.rb create mode 100644 specs/room_spec.rb create mode 100644 specs/spec_helper.rb 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/frontdesk.rb b/lib/frontdesk.rb new file mode 100644 index 000000000..c30db5c40 --- /dev/null +++ b/lib/frontdesk.rb @@ -0,0 +1,28 @@ +require 'date' +module Hotel + class FrontDesk + + ROOM_PRICE = 200 + attr_accessor :rooms, :reservations + + def initialize + @rooms = get_rooms + @reservations = get_reservations + end + + def access_rooms + end + + def reserve_room + end + + def access_reservations + end + + def total_cost + end + + all_rooms = (1..20) + + end +end diff --git a/lib/reservation.rb b/lib/reservation.rb new file mode 100644 index 000000000..a60b79125 --- /dev/null +++ b/lib/reservation.rb @@ -0,0 +1,30 @@ +require 'date' + +module Hotel + class Reservation + + attr_accessor :rooms, :check_in, :check_out, :cost + + def initialize + @rooms = rooms + @check_in = check_in + @check_out = check_out + @cost = cost + + end #initialize + + + def duration + if check_in == nil || check_out == nil + raise ArgumentError.new ("Invalid date") + else + return duration = (@check_out - @check_in) + end + end # duration + + def request_reservation + end + + + end +end diff --git a/lib/room.rb b/lib/room.rb new file mode 100644 index 000000000..9b5c94f34 --- /dev/null +++ b/lib/room.rb @@ -0,0 +1,5 @@ +require 'date' +module Hotel + class Room + end +end diff --git a/specs/frontdesk_spec.rb b/specs/frontdesk_spec.rb new file mode 100644 index 000000000..ae9c220ea --- /dev/null +++ b/specs/frontdesk_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..4801f441a --- /dev/null +++ b/specs/reservation_spec.rb @@ -0,0 +1,14 @@ +require_relative 'spec_helper' + +describe "Reservation class" do + describe "Initializer" do + it "is an instance of Reservation" do + reservation = Hotel::Reservation.new + reservation.must_be_kind_of Hotel::Reservation + end + + it "establishes the base data structures when instantiated" do + reservation = Hotel::Reservation.new + end + end +end diff --git a/specs/room_spec.rb b/specs/room_spec.rb new file mode 100644 index 000000000..ae9c220ea --- /dev/null +++ b/specs/room_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..cddc9c952 --- /dev/null +++ b/specs/spec_helper.rb @@ -0,0 +1,15 @@ +require 'simplecov' +SimpleCov.start + +require 'date' +require 'minitest' +require 'minitest/autorun' + +require 'minitest/reporters' +require 'minitest/skip_dsl' +Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new + +# Require_relative your lib files here! +require_relative '../lib/room' +require_relative '../lib/frontdesk' +require_relative '../lib/reservation' From 0c0b38694f402894051ea5730fe05bafb6d07379 Mon Sep 17 00:00:00 2001 From: nicoletabrandolini Date: Tue, 6 Mar 2018 09:35:58 -0800 Subject: [PATCH 02/13] Create tests for room and reservation class and code for it --- lib/frontdesk.rb | 8 +++---- lib/reservation.rb | 47 ++++++++++++++++++++++++++++----------- lib/room.rb | 20 ++++++++++++++++- specs/frontdesk_spec.rb | 12 ++++++++++ specs/reservation_spec.rb | 29 +++++++++++++++++------- specs/room_spec.rb | 18 +++++++++++++++ 6 files changed, 107 insertions(+), 27 deletions(-) diff --git a/lib/frontdesk.rb b/lib/frontdesk.rb index c30db5c40..a1f4db8d3 100644 --- a/lib/frontdesk.rb +++ b/lib/frontdesk.rb @@ -6,14 +6,12 @@ class FrontDesk attr_accessor :rooms, :reservations def initialize - @rooms = get_rooms - @reservations = get_reservations - end + @rooms = + @reservations = [] - def access_rooms end - def reserve_room + def add_reservation end def access_reservations diff --git a/lib/reservation.rb b/lib/reservation.rb index a60b79125..e55dc19f9 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -3,28 +3,49 @@ module Hotel class Reservation - attr_accessor :rooms, :check_in, :check_out, :cost + attr_reader :guest, :check_in, :check_out, :room, :total_nights + + def initialize(guest, check_in, check_out, room) + @room = room + @check_in = check_in_date(check_in) + @check_out = check_out_date(check_out) + @guest = guest + @total_nights = calculate_length - def initialize - @rooms = rooms - @check_in = check_in - @check_out = check_out - @cost = cost end #initialize - def duration - if check_in == nil || check_out == nil - raise ArgumentError.new ("Invalid date") + private + + def check_in_date(check_in) + if check_in.is_a? (Date) + @check_in = check_in else - return duration = (@check_out - @check_in) + raise ArgumentError.new "Invalid check in date" end - end # duration + end - def request_reservation + def check_out_date(check_out) + if check_in.is_a? (Date) + @check_out = check_out + else + raise ArgumentError.new "Invalid check out date" + end end - + def calculate_length + if @check_in >= @check_out || @check_in < Date.today + raise ArgumentError.new "invalid dates" + else + length = @check_out - @check_in + return length.to_i + end + end + + # def request_reservation + # end + + end end diff --git a/lib/room.rb b/lib/room.rb index 9b5c94f34..c8229a296 100644 --- a/lib/room.rb +++ b/lib/room.rb @@ -1,5 +1,23 @@ require 'date' module Hotel class Room + attr_reader :number + attr_accessor :rate + + def initialize (number) + @number = check_room_number(number) + @rate = 200 end -end + + private # Raise arument error if room number is not an integer or is already taken + + def check_room_number(number) + if number.is_a?(Integer) == false + raise ArgumentError.new "Room number must be an integer" + else + @number = number + end + end + +end # class +end # module diff --git a/specs/frontdesk_spec.rb b/specs/frontdesk_spec.rb index ae9c220ea..077fc6bfb 100644 --- a/specs/frontdesk_spec.rb +++ b/specs/frontdesk_spec.rb @@ -1 +1,13 @@ require_relative 'spec_helper' + +describe "Hotel::FrontDesk" do + describe "Initializer" do + it "is an instance of FrontDesk" do + reservation = Hotel::FrontDesk.new.must_be_kind_of Hotel::FrontDesk + end + + it "establishes the base data structures when instantiated" do + reservation = Hotel::FrontDesk.new + end + end +end diff --git a/specs/reservation_spec.rb b/specs/reservation_spec.rb index 4801f441a..4b2acf829 100644 --- a/specs/reservation_spec.rb +++ b/specs/reservation_spec.rb @@ -1,14 +1,27 @@ require_relative 'spec_helper' -describe "Reservation class" do - describe "Initializer" do - it "is an instance of Reservation" do - reservation = Hotel::Reservation.new - reservation.must_be_kind_of Hotel::Reservation +describe Hotel:: Reservation do + before do + @guest = "Tom Hank" + end + describe "can create a reservation instance" do + it "can be created" do + check_in = Date.new(2018, 03, 16) + check_out = Date.new(2018, 03, 20) + reservation = Hotel::Reservation.new(@guest, check_in, check_out, Hotel::Room.new(1)) + reservation.must_be_instance_of Hotel::Reservation end - it "establishes the base data structures when instantiated" do - reservation = Hotel::Reservation.new - end + it "accurately calculates the length of a stay of 1 night" do + check_in = Date.new(2018, 03, 16) + check_out = Date.new(2018, 03, 19) + reservation = Hotel::Reservation.new(@guest, check_in, check_out, Hotel::Room.new(3)) + reservation.total_nights.must_equal 3 + end + + + # it "establishes the base data structures when instantiated" do + # reservation = Hotel::Reservation.new(@guest, check_in, check_out,Hotel::Room.new) + # end end end diff --git a/specs/room_spec.rb b/specs/room_spec.rb index ae9c220ea..2265c9217 100644 --- a/specs/room_spec.rb +++ b/specs/room_spec.rb @@ -1 +1,19 @@ require_relative 'spec_helper' + +describe Hotel::Room do + describe "Can create a room instance" do + it "can be created" do + room = Hotel::Room.new(1) + room.must_be_instance_of Hotel::Room + end + + it "raises an ArgumentError for invalid parameters" do + proc{Hotel::Room.new("A76")}.must_raise ArgumentError + end + + it "initializes with a cost of $200 per night" do + room = Hotel::Room.new (7) + room.rate.must_equal 200 + end + end +end From 8473a361cdc94b12f57e4a80020a163693aac636 Mon Sep 17 00:00:00 2001 From: nicoletabrandolini Date: Tue, 6 Mar 2018 15:36:33 -0800 Subject: [PATCH 03/13] Update reservation class and pass tests --- lib/reservation.rb | 20 +++++++------------- specs/reservation_spec.rb | 12 ++++++------ 2 files changed, 13 insertions(+), 19 deletions(-) diff --git a/lib/reservation.rb b/lib/reservation.rb index e55dc19f9..a1857a2d7 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -7,8 +7,8 @@ class Reservation def initialize(guest, check_in, check_out, room) @room = room - @check_in = check_in_date(check_in) - @check_out = check_out_date(check_out) + @check_in = validate_date(check_in) + @check_out = validate_date(check_out) @guest = guest @total_nights = calculate_length @@ -18,22 +18,14 @@ def initialize(guest, check_in, check_out, room) private - def check_in_date(check_in) - if check_in.is_a? (Date) - @check_in = check_in + def validate_date(date) + if date.is_a? (Date) + return date else raise ArgumentError.new "Invalid check in date" end end - def check_out_date(check_out) - if check_in.is_a? (Date) - @check_out = check_out - else - raise ArgumentError.new "Invalid check out date" - end - end - def calculate_length if @check_in >= @check_out || @check_in < Date.today raise ArgumentError.new "invalid dates" @@ -43,6 +35,8 @@ def calculate_length end end + def total_cost + end # def request_reservation # end diff --git a/specs/reservation_spec.rb b/specs/reservation_spec.rb index 4b2acf829..4be99e0e3 100644 --- a/specs/reservation_spec.rb +++ b/specs/reservation_spec.rb @@ -10,14 +10,14 @@ check_out = Date.new(2018, 03, 20) reservation = Hotel::Reservation.new(@guest, check_in, check_out, Hotel::Room.new(1)) reservation.must_be_instance_of Hotel::Reservation - end + end it "accurately calculates the length of a stay of 1 night" do - check_in = Date.new(2018, 03, 16) - check_out = Date.new(2018, 03, 19) - reservation = Hotel::Reservation.new(@guest, check_in, check_out, Hotel::Room.new(3)) - reservation.total_nights.must_equal 3 - end + check_in = Date.new(2018, 03, 16) + check_out = Date.new(2018, 03, 19) + reservation = Hotel::Reservation.new(@guest, check_in, check_out, Hotel::Room.new(3)) + reservation.total_nights.must_equal 3 + end # it "establishes the base data structures when instantiated" do From 8f739909ca2b4d7034cd0d971539f7d28efc3002 Mon Sep 17 00:00:00 2001 From: nicoletabrandolini Date: Wed, 7 Mar 2018 09:26:27 -0800 Subject: [PATCH 04/13] Update reservation class and tests --- lib/frontdesk.rb | 50 ++++++++++++++++++++++++++++---- lib/reservation.rb | 6 +++- lib/room.rb | 2 +- specs/frontdesk_spec.rb | 15 ++++++++++ specs/reservation_spec.rb | 60 ++++++++++++++++++++++++++------------- specs/room_spec.rb | 2 +- 6 files changed, 108 insertions(+), 27 deletions(-) diff --git a/lib/frontdesk.rb b/lib/frontdesk.rb index a1f4db8d3..930dd6f82 100644 --- a/lib/frontdesk.rb +++ b/lib/frontdesk.rb @@ -1,4 +1,5 @@ require 'date' +require 'pry' module Hotel class FrontDesk @@ -6,21 +7,60 @@ class FrontDesk attr_accessor :rooms, :reservations def initialize - @rooms = + @rooms = create_rooms @reservations = [] + end + + # TODO arg raise Argument Error for no available rooms + def make_reservation(guest, check_in, check_out) + room = assign_room(check_in, check_out) + @reservations << Reservation.new(guest, check_in, check_out, room) + end + + def validate_date(date) + if date.is_a? (Date) + return date + else + raise ArgumentError.new "Invalid check in date" + end end - def add_reservation + def create_rooms + rooms = [] + (1..20).each do |num| + rooms << Room.new(num) + end + return rooms end - def access_reservations + def get_available_rooms(date_begin, date_end) + available_rooms = @rooms.clone + + @reservations.each do |res| + overlap = (res.check_in...res.check_out).to_a & (date_begin...date_end).to_a + + if overlap[0] != nil + available_rooms.delete(res.room) + end + end + return available_rooms end - def total_cost + + def get_reservation_by_date(date) + reservation_by_date = [] + @reservations.each do |res| + if res.include_date?(date) + reservation_by_date << res + end + end + return reservation_by_date end - all_rooms = (1..20) + # TODO + def assign_room(check_in, check_out) + end end end diff --git a/lib/reservation.rb b/lib/reservation.rb index a1857a2d7..4842bbc0b 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -15,6 +15,9 @@ def initialize(guest, check_in, check_out, room) end #initialize + def include_date?(date) + return date >= check_in && date < check_out + end private @@ -35,9 +38,10 @@ def calculate_length end end + def total_cost end - # def request_reservation + # end diff --git a/lib/room.rb b/lib/room.rb index c8229a296..bb150070d 100644 --- a/lib/room.rb +++ b/lib/room.rb @@ -19,5 +19,5 @@ def check_room_number(number) end end -end # class + end # class end # module diff --git a/specs/frontdesk_spec.rb b/specs/frontdesk_spec.rb index 077fc6bfb..5bb4ef98b 100644 --- a/specs/frontdesk_spec.rb +++ b/specs/frontdesk_spec.rb @@ -1,6 +1,9 @@ require_relative 'spec_helper' describe "Hotel::FrontDesk" do + before do + @guest = "Tom Hank" + end describe "Initializer" do it "is an instance of FrontDesk" do reservation = Hotel::FrontDesk.new.must_be_kind_of Hotel::FrontDesk @@ -10,4 +13,16 @@ reservation = Hotel::FrontDesk.new end end + + describe "get_available_rooms" do + it "gets a list of available rooms" do + fd = Hotel::FrontDesk.new + date_begin = Date.new(2018, 03, 16) + date_end = Date.new(2018, 03, 19) + fd.make_reservation(@guest, date_begin, date_end) + rooms = fd.get_available_rooms(date_begin - 3, date_end- 1) + rooms.must_be_kind_of Array + end + end + end diff --git a/specs/reservation_spec.rb b/specs/reservation_spec.rb index 4be99e0e3..cdeb6aec6 100644 --- a/specs/reservation_spec.rb +++ b/specs/reservation_spec.rb @@ -4,24 +4,46 @@ before do @guest = "Tom Hank" end - describe "can create a reservation instance" do - it "can be created" do - check_in = Date.new(2018, 03, 16) - check_out = Date.new(2018, 03, 20) - reservation = Hotel::Reservation.new(@guest, check_in, check_out, Hotel::Room.new(1)) - reservation.must_be_instance_of Hotel::Reservation - end - - it "accurately calculates the length of a stay of 1 night" do - check_in = Date.new(2018, 03, 16) - check_out = Date.new(2018, 03, 19) - reservation = Hotel::Reservation.new(@guest, check_in, check_out, Hotel::Room.new(3)) - reservation.total_nights.must_equal 3 - end - - - # it "establishes the base data structures when instantiated" do - # reservation = Hotel::Reservation.new(@guest, check_in, check_out,Hotel::Room.new) - # end + describe "can create a reservation instance" do + it "can be created" do + check_in = Date.new(2018, 03, 16) + check_out = Date.new(2018, 03, 20) + reservation = Hotel::Reservation.new(@guest, check_in, check_out, Hotel::Room.new(1)) + reservation.must_be_instance_of Hotel::Reservation + end + + it "accurately calculates the length of a stay of 1 night" do + check_in = Date.new(2018, 03, 16) + check_out = Date.new(2018, 03, 19) + reservation = Hotel::Reservation.new(@guest, check_in, check_out, Hotel::Room.new(3)) + reservation.total_nights.must_equal 3 + end end + + describe "include_date?" do + it "returns true if the date is between check_in and check_out" do + check_in = Date.new(2018, 03, 16) + check_out = Date.new(2018, 03, 19) + reservation = Hotel::Reservation.new(@guest, check_in, check_out, Hotel::Room.new(3)) + date = Date.new(2018, 03, 17) + reservation.include_date?(date).must_equal true + end + it "returns false if the date is not between check_in and check_out" do + check_in = Date.new(2018, 03, 16) + check_out = Date.new(2018, 03, 19) + reservation = Hotel::Reservation.new(@guest, check_in, check_out, Hotel::Room.new(3)) + date = Date.new(2018, 03, 20) + reservation.include_date?(date).must_equal false + + end + end + end + + + + + +# it "establishes the base data structures when instantiated" do +# reservation = Hotel::Reservation.new(@guest, check_in, check_out,Hotel::Room.new) +# end diff --git a/specs/room_spec.rb b/specs/room_spec.rb index 2265c9217..eb2ce53d1 100644 --- a/specs/room_spec.rb +++ b/specs/room_spec.rb @@ -1,6 +1,6 @@ require_relative 'spec_helper' -describe Hotel::Room do +xdescribe Hotel::Room do describe "Can create a room instance" do it "can be created" do room = Hotel::Room.new(1) From af62344b1f0a0b41f3a84ec90486195ac7d6ffc2 Mon Sep 17 00:00:00 2001 From: nicoletabrandolini Date: Wed, 7 Mar 2018 17:47:54 -0800 Subject: [PATCH 05/13] Implemented #total_cost method and past tests --- lib/frontdesk.rb | 19 ++++++++++++++++--- lib/reservation.rb | 17 +++++++++-------- specs/reservation_spec.rb | 17 +++++++++++++++++ 3 files changed, 42 insertions(+), 11 deletions(-) diff --git a/lib/frontdesk.rb b/lib/frontdesk.rb index 930dd6f82..f4e971294 100644 --- a/lib/frontdesk.rb +++ b/lib/frontdesk.rb @@ -12,11 +12,13 @@ def initialize end # TODO arg raise Argument Error for no available rooms + def make_reservation(guest, check_in, check_out) - room = assign_room(check_in, check_out) + # room = assign_room(check_in, check_out) @reservations << Reservation.new(guest, check_in, check_out, room) end + def validate_date(date) if date.is_a? (Date) return date @@ -59,8 +61,19 @@ def get_reservation_by_date(date) end # TODO - def assign_room(check_in, check_out) - end + + # def assign_room(check_in, check_out, room) + # available_rooms = get_available_rooms(check_in, check_out) + # raise StandardError.new "No more rooms available for that day", if available_rooms.empty? + # + # available_rooms.each do |empty_room| + # if empty_room.number == room + # return empty_room + # end + # end + # + # end + end end diff --git a/lib/reservation.rb b/lib/reservation.rb index 4842bbc0b..a04761da6 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -15,8 +15,16 @@ def initialize(guest, check_in, check_out, room) end #initialize + # def include_date?(date) + # return date >= check_in && date < check_out + # end + def include_date?(date) - return date >= check_in && date < check_out + date.between?(@check_in, @check_out - 1) + end + + def total_cost + @total_nights * 200 end private @@ -38,12 +46,5 @@ def calculate_length end end - - def total_cost - end - - # end - - end end diff --git a/specs/reservation_spec.rb b/specs/reservation_spec.rb index cdeb6aec6..6eede470f 100644 --- a/specs/reservation_spec.rb +++ b/specs/reservation_spec.rb @@ -38,12 +38,29 @@ end end + describe "total_cost" do + it "calculates the total_cost of one night" do + check_in = Date.new(2018, 03, 16) + check_out = Date.new(2018, 03, 17) + reservation = Hotel::Reservation.new(@guest, check_in, check_out, Hotel::Room.new(3)) + reservation.total_cost.must_equal 200 + end + + it "calculates the total cost of multiple nights" do + check_in = Date.new(2018, 03, 16) + check_out = Date.new(2018, 03, 19) + reservation = Hotel::Reservation.new(@guest, check_in, check_out, Hotel::Room.new(3)) + reservation.total_cost.must_equal 600 + end + end end + + # it "establishes the base data structures when instantiated" do # reservation = Hotel::Reservation.new(@guest, check_in, check_out,Hotel::Room.new) # end From 531723fb39b4a652d8cad58ceb5b96b3a111c565 Mon Sep 17 00:00:00 2001 From: nicoletabrandolini Date: Thu, 8 Mar 2018 00:07:28 -0800 Subject: [PATCH 06/13] Implemented test for make reservation still need more tests --- lib/frontdesk.rb | 53 +++++++++++++++++++++------------------ specs/frontdesk_spec.rb | 21 ++++++++++++++++ specs/reservation_spec.rb | 2 +- 3 files changed, 50 insertions(+), 26 deletions(-) diff --git a/lib/frontdesk.rb b/lib/frontdesk.rb index f4e971294..733288c54 100644 --- a/lib/frontdesk.rb +++ b/lib/frontdesk.rb @@ -14,11 +14,15 @@ def initialize # TODO arg raise Argument Error for no available rooms def make_reservation(guest, check_in, check_out) - # room = assign_room(check_in, check_out) - @reservations << Reservation.new(guest, check_in, check_out, room) + avail_rooms = get_available_rooms(check_in, check_out) + if avail_rooms.empty? + raise StandardError.new("No room is available for this date range") + else + reservation = Hotel::Reservation.new(guest,check_in, check_out, avail_rooms.sample) + @reservations << reservation + end end - def validate_date(date) if date.is_a? (Date) return date @@ -47,33 +51,32 @@ def get_available_rooms(date_begin, date_end) end end return available_rooms - end - - def get_reservation_by_date(date) - reservation_by_date = [] - @reservations.each do |res| - if res.include_date?(date) - reservation_by_date << res + def get_reservation_by_date(date) + reservation_by_date = [] + @reservations.each do |res| + if res.include_date?(date) + reservation_by_date << res + end end + return reservation_by_date end - return reservation_by_date - end - # TODO + TODO - # def assign_room(check_in, check_out, room) - # available_rooms = get_available_rooms(check_in, check_out) - # raise StandardError.new "No more rooms available for that day", if available_rooms.empty? - # - # available_rooms.each do |empty_room| - # if empty_room.number == room - # return empty_room - # end - # end - # - # end - + # def assign_room(check_in, check_out, room) + # available_rooms = get_available_rooms(check_in, check_out) + # raise StandardError.new "No more rooms available for that day", if available_rooms.empty? + # + # available_rooms.each do |empty_room| + # if empty_room.number == room + # return empty_room + # end + # end + # + # end + + end end end diff --git a/specs/frontdesk_spec.rb b/specs/frontdesk_spec.rb index 5bb4ef98b..e21eb6c47 100644 --- a/specs/frontdesk_spec.rb +++ b/specs/frontdesk_spec.rb @@ -14,6 +14,27 @@ end end + describe "make_reservation" do + it "creates a reservation" do + frontdesk = Hotel::FrontDesk.new + check_in = Date.new(2018, 03, 16) + check_out = Date.new(2018, 03, 19) + frontdesk.make_reservation(@guest, check_in, check_out) + end + it "raises a StandardError when they are no available rooms left" do + frontdesk = Hotel::FrontDesk.new + check_in = Date.new(2018, 03, 16) + check_out = Date.new(2018, 03, 19) + + 20.times do + frontdesk.make_reservation(@guest, check_in, check_out) + end + proc{frontdesk.make_reservation(@guest, check_in, check_out)}.must_raise StandardError + end + end + + + describe "get_available_rooms" do it "gets a list of available rooms" do fd = Hotel::FrontDesk.new diff --git a/specs/reservation_spec.rb b/specs/reservation_spec.rb index 6eede470f..59cd558df 100644 --- a/specs/reservation_spec.rb +++ b/specs/reservation_spec.rb @@ -39,7 +39,7 @@ end describe "total_cost" do - it "calculates the total_cost of one night" do + it "calculates the total cost of one night" do check_in = Date.new(2018, 03, 16) check_out = Date.new(2018, 03, 17) reservation = Hotel::Reservation.new(@guest, check_in, check_out, Hotel::Room.new(3)) From 4f848f2c9f9ac1a5feb8c75259abb9df08437cf1 Mon Sep 17 00:00:00 2001 From: nicoletabrandolini Date: Sun, 11 Mar 2018 14:54:35 -0700 Subject: [PATCH 07/13] More tests --- lib/frontdesk.rb | 35 +++++++++++++++---------- specs/frontdesk_spec.rb | 57 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 77 insertions(+), 15 deletions(-) diff --git a/lib/frontdesk.rb b/lib/frontdesk.rb index 733288c54..09917eb40 100644 --- a/lib/frontdesk.rb +++ b/lib/frontdesk.rb @@ -9,9 +9,8 @@ class FrontDesk def initialize @rooms = create_rooms @reservations = [] - end + end - # TODO arg raise Argument Error for no available rooms def make_reservation(guest, check_in, check_out) avail_rooms = get_available_rooms(check_in, check_out) @@ -23,14 +22,6 @@ def make_reservation(guest, check_in, check_out) end end - def validate_date(date) - if date.is_a? (Date) - return date - else - raise ArgumentError.new "Invalid check in date" - end - - end def create_rooms rooms = [] @@ -53,16 +44,32 @@ def get_available_rooms(date_begin, date_end) return available_rooms def get_reservation_by_date(date) - reservation_by_date = [] + reservations_by_date = [] @reservations.each do |res| if res.include_date?(date) - reservation_by_date << res end + reservations_by_date << res + end + return reservations_by_date + end + + def include_date?(date) + date.between?(@check_in, @check_out - 1) + end + + private + + def validate_date(date) + if date.is_a? (Date) + return date + else + raise ArgumentError.new "Argument must be a date." end - return reservation_by_date + end - TODO + + # def assign_room(check_in, check_out, room) # available_rooms = get_available_rooms(check_in, check_out) diff --git a/specs/frontdesk_spec.rb b/specs/frontdesk_spec.rb index e21eb6c47..29a65f47a 100644 --- a/specs/frontdesk_spec.rb +++ b/specs/frontdesk_spec.rb @@ -4,7 +4,7 @@ before do @guest = "Tom Hank" end - describe "Initializer" do + describe "initialize" do it "is an instance of FrontDesk" do reservation = Hotel::FrontDesk.new.must_be_kind_of Hotel::FrontDesk end @@ -12,15 +12,52 @@ it "establishes the base data structures when instantiated" do reservation = Hotel::FrontDesk.new end + + it "creates instance of rooms" do + rooms_in_hotel = Hotel::FrontDesk.new.rooms + rooms_in_hotel.must_be_instance_of Array + rooms_in_hotel.each do |room| + room.must_be_instance_of Hotel::Room + end + end + it "creates 20 rooms" do + rooms_in_hotel = Hotel::FrontDesk.new.rooms + rooms_in_hotel.length.must_equal 20 + end + + it "creates an empty array of reservations" do + reservations_in_hotel = Hotel::FrontDesk.new.reservations + reservations_in_hotel.must_be_kind_of Array + reservations_in_hotel.length.must_equal 0 + end end describe "make_reservation" do it "creates a reservation" do + frontdesk = Hotel::FrontDesk.new + check_in = Date.new(2018, 03, 16) + check_out = Date.new(2018, 03, 19) + new_res = frontdesk.make_reservation(@guest, check_in, check_out) + new_res.must_be_instance_of Hotel::Reservation + end + + it "adds a new reservation to the reservations array" do frontdesk = Hotel::FrontDesk.new check_in = Date.new(2018, 03, 16) check_out = Date.new(2018, 03, 19) frontdesk.make_reservation(@guest, check_in, check_out) + frontdesk.reservations.length.must_equal 1 + frontdesk.reservations.last.must_be_instance_of Hotel::Reservation + end + + it "uses check_in and check_out dates for reservation" do + # the dates on the reservation are the same ones that we passed into a method" end + + it "assigns a room" do + #checks that the reservation instance has a room + end + it "raises a StandardError when they are no available rooms left" do frontdesk = Hotel::FrontDesk.new check_in = Date.new(2018, 03, 16) @@ -33,7 +70,11 @@ end end + describe "validate_date" do + it "raises an ArgumentError when date is not in a valid format" do + end + end describe "get_available_rooms" do it "gets a list of available rooms" do @@ -46,4 +87,18 @@ end end + describe "get_reservation_by_date" do + it "gets a list of reservations for a particular date" do + fd = Hotel::FrontDesk.new + date_begin = Date.new(2018, 03, 16) + date_end = Date.new(2018, 03, 19) + fd.make_reservation(@guest, date_begin, date_end) + fd = Hotel::FrontDesk.new + date_begin = Date.new(2018, 03, 14) + date_end = Date.new(2018, 03, 17) + fd.make_reservation(@guest, date_begin, date_end) + res = fd.get_reservation_by_date(2018, 03, 16) + res.length.must_equal 2 + end + end end From 6fa2657b51507c5f8cef82645f3a9bdb48a6d963 Mon Sep 17 00:00:00 2001 From: nicoletabrandolini Date: Sun, 11 Mar 2018 15:49:54 -0700 Subject: [PATCH 08/13] Wave 2 tests done --- lib/frontdesk.rb | 73 ++++++++++++++++++++--------------------- specs/frontdesk_spec.rb | 15 +++++++-- specs/room_spec.rb | 2 +- 3 files changed, 49 insertions(+), 41 deletions(-) diff --git a/lib/frontdesk.rb b/lib/frontdesk.rb index 09917eb40..eff755262 100644 --- a/lib/frontdesk.rb +++ b/lib/frontdesk.rb @@ -9,7 +9,7 @@ class FrontDesk def initialize @rooms = create_rooms @reservations = [] - end + end def make_reservation(guest, check_in, check_out) @@ -20,16 +20,10 @@ def make_reservation(guest, check_in, check_out) reservation = Hotel::Reservation.new(guest,check_in, check_out, avail_rooms.sample) @reservations << reservation end + return reservation end - def create_rooms - rooms = [] - (1..20).each do |num| - rooms << Room.new(num) - end - return rooms - end def get_available_rooms(date_begin, date_end) available_rooms = @rooms.clone @@ -42,48 +36,51 @@ def get_available_rooms(date_begin, date_end) end end return available_rooms + end - def get_reservation_by_date(date) - reservations_by_date = [] - @reservations.each do |res| - if res.include_date?(date) - end - reservations_by_date << res + def get_reservation_by_date(date) + reservations_by_date = [] + @reservations.each do |res| + if res.include_date?(date) + reservations_by_date << res end - return reservations_by_date - end - - def include_date?(date) - date.between?(@check_in, @check_out - 1) end + return reservations_by_date + end private - def validate_date(date) - if date.is_a? (Date) - return date - else - raise ArgumentError.new "Argument must be a date." - end - + def validate_date(date) + if date.is_a? (Date) + return date + else + raise ArgumentError.new "Argument must be a date." end + end + def create_rooms + rooms = [] + (1..20).each do |num| + rooms << Room.new(num) + end + return rooms + end - # def assign_room(check_in, check_out, room) - # available_rooms = get_available_rooms(check_in, check_out) - # raise StandardError.new "No more rooms available for that day", if available_rooms.empty? - # - # available_rooms.each do |empty_room| - # if empty_room.number == room - # return empty_room - # end - # end - # - # end + + # def assign_room(check_in, check_out, room) + # available_rooms = get_available_rooms(check_in, check_out) + # raise StandardError.new "No more rooms available for that day", if available_rooms.empty? + # + # available_rooms.each do |empty_room| + # if empty_room.number == room + # return empty_room + # end + # end + # + # end - end end end diff --git a/specs/frontdesk_spec.rb b/specs/frontdesk_spec.rb index 29a65f47a..9d29e731f 100644 --- a/specs/frontdesk_spec.rb +++ b/specs/frontdesk_spec.rb @@ -93,12 +93,23 @@ date_begin = Date.new(2018, 03, 16) date_end = Date.new(2018, 03, 19) fd.make_reservation(@guest, date_begin, date_end) - fd = Hotel::FrontDesk.new date_begin = Date.new(2018, 03, 14) date_end = Date.new(2018, 03, 17) fd.make_reservation(@guest, date_begin, date_end) - res = fd.get_reservation_by_date(2018, 03, 16) + res = fd.get_reservation_by_date(Date.new(2018, 03, 16)) res.length.must_equal 2 end + + it "returns an empty array when there are no overlaping reservation" do + fd = Hotel::FrontDesk.new + date_begin = Date.new(2018, 03, 16) + date_end = Date.new(2018, 03, 19) + fd.make_reservation(@guest, date_begin, date_end) + date_begin = Date.new(2018, 03, 14) + date_end = Date.new(2018, 03, 17) + fd.make_reservation(@guest, date_begin, date_end) + res = fd.get_reservation_by_date(Date.new(2018, 03, 19)) + res.length.must_equal 0 + end end end diff --git a/specs/room_spec.rb b/specs/room_spec.rb index eb2ce53d1..2265c9217 100644 --- a/specs/room_spec.rb +++ b/specs/room_spec.rb @@ -1,6 +1,6 @@ require_relative 'spec_helper' -xdescribe Hotel::Room do +describe Hotel::Room do describe "Can create a room instance" do it "can be created" do room = Hotel::Room.new(1) From 07ddd05384eb39bdc81d931401017cd866027e32 Mon Sep 17 00:00:00 2001 From: nicoletabrandolini Date: Mon, 12 Mar 2018 07:25:23 -0700 Subject: [PATCH 09/13] Refactor but still more to do --- lib/frontdesk.rb | 18 ------------------ lib/reservation.rb | 8 +------- lib/room.rb | 6 +++--- specs/reservation_spec.rb | 10 ---------- 4 files changed, 4 insertions(+), 38 deletions(-) diff --git a/lib/frontdesk.rb b/lib/frontdesk.rb index eff755262..f753df638 100644 --- a/lib/frontdesk.rb +++ b/lib/frontdesk.rb @@ -11,7 +11,6 @@ def initialize @reservations = [] end - def make_reservation(guest, check_in, check_out) avail_rooms = get_available_rooms(check_in, check_out) if avail_rooms.empty? @@ -23,8 +22,6 @@ def make_reservation(guest, check_in, check_out) return reservation end - - def get_available_rooms(date_begin, date_end) available_rooms = @rooms.clone @@ -67,20 +64,5 @@ def create_rooms return rooms end - - - # def assign_room(check_in, check_out, room) - # available_rooms = get_available_rooms(check_in, check_out) - # raise StandardError.new "No more rooms available for that day", if available_rooms.empty? - # - # available_rooms.each do |empty_room| - # if empty_room.number == room - # return empty_room - # end - # end - # - # end - - end end diff --git a/lib/reservation.rb b/lib/reservation.rb index a04761da6..64bbffd58 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -11,13 +11,7 @@ def initialize(guest, check_in, check_out, room) @check_out = validate_date(check_out) @guest = guest @total_nights = calculate_length - - - end #initialize - - # def include_date?(date) - # return date >= check_in && date < check_out - # end + end def include_date?(date) date.between?(@check_in, @check_out - 1) diff --git a/lib/room.rb b/lib/room.rb index bb150070d..8bd3956aa 100644 --- a/lib/room.rb +++ b/lib/room.rb @@ -9,7 +9,7 @@ def initialize (number) @rate = 200 end - private # Raise arument error if room number is not an integer or is already taken + private def check_room_number(number) if number.is_a?(Integer) == false @@ -19,5 +19,5 @@ def check_room_number(number) end end - end # class -end # module + end +end diff --git a/specs/reservation_spec.rb b/specs/reservation_spec.rb index 59cd558df..2f6eac909 100644 --- a/specs/reservation_spec.rb +++ b/specs/reservation_spec.rb @@ -54,13 +54,3 @@ end end end - - - - - - - -# it "establishes the base data structures when instantiated" do -# reservation = Hotel::Reservation.new(@guest, check_in, check_out,Hotel::Room.new) -# end From 99b4b01a63458f1632057a72d3fd5b5d1c594d0a Mon Sep 17 00:00:00 2001 From: nicoletabrandolini Date: Tue, 13 Mar 2018 19:40:44 -0700 Subject: [PATCH 10/13] Fixed bugs and create more tests --- lib/frontdesk.rb | 9 --------- lib/reservation.rb | 4 ++-- specs/reservation_spec.rb | 16 ++++++++++++++++ specs/spec_helper.rb | 2 +- 4 files changed, 19 insertions(+), 12 deletions(-) diff --git a/lib/frontdesk.rb b/lib/frontdesk.rb index f753df638..673681737 100644 --- a/lib/frontdesk.rb +++ b/lib/frontdesk.rb @@ -47,15 +47,6 @@ def get_reservation_by_date(date) private - def validate_date(date) - if date.is_a? (Date) - return date - else - raise ArgumentError.new "Argument must be a date." - end - - end - def create_rooms rooms = [] (1..20).each do |num| diff --git a/lib/reservation.rb b/lib/reservation.rb index 64bbffd58..bd33b4f79 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -27,8 +27,8 @@ def validate_date(date) if date.is_a? (Date) return date else - raise ArgumentError.new "Invalid check in date" - end + raise ArgumentError.new "Invalid date" + end end def calculate_length diff --git a/specs/reservation_spec.rb b/specs/reservation_spec.rb index 2f6eac909..b6a5afa74 100644 --- a/specs/reservation_spec.rb +++ b/specs/reservation_spec.rb @@ -12,6 +12,22 @@ reservation.must_be_instance_of Hotel::Reservation end + it "raises an Argument Error for nil check_in" do + check_in = nil + check_out = Date.new(2018, 03, 20) + + error = proc{Hotel::Reservation.new(@guest, check_in, check_out, Hotel::Room.new(1))}.must_raise ArgumentError + error.message.must_equal "Invalid date" + end + + it "raises an Argument Error for nil check_out" do + check_in = Date.new(2018, 03, 16) + check_out = nil + + error = proc{Hotel::Reservation.new(@guest, check_in, check_out, Hotel::Room.new(1))}.must_raise ArgumentError + error.message.must_equal "Invalid date" + end + it "accurately calculates the length of a stay of 1 night" do check_in = Date.new(2018, 03, 16) check_out = Date.new(2018, 03, 19) diff --git a/specs/spec_helper.rb b/specs/spec_helper.rb index cddc9c952..d375a0f3b 100644 --- a/specs/spec_helper.rb +++ b/specs/spec_helper.rb @@ -1,6 +1,6 @@ require 'simplecov' SimpleCov.start - +require 'pry' require 'date' require 'minitest' require 'minitest/autorun' From 1f1f99bfd855eb4309e2953033ab4444981b017a Mon Sep 17 00:00:00 2001 From: nicoletabrandolini Date: Sat, 17 Mar 2018 15:50:26 -0700 Subject: [PATCH 11/13] Reservations with check-in dates earlier than today are valid now --- lib/reservation.rb | 4 ++-- specs/reservation_spec.rb | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/reservation.rb b/lib/reservation.rb index bd33b4f79..2211ea141 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -28,11 +28,11 @@ def validate_date(date) return date else raise ArgumentError.new "Invalid date" - end + end end def calculate_length - if @check_in >= @check_out || @check_in < Date.today + if @check_in >= @check_out raise ArgumentError.new "invalid dates" else length = @check_out - @check_in diff --git a/specs/reservation_spec.rb b/specs/reservation_spec.rb index b6a5afa74..19ffd27d3 100644 --- a/specs/reservation_spec.rb +++ b/specs/reservation_spec.rb @@ -18,6 +18,7 @@ error = proc{Hotel::Reservation.new(@guest, check_in, check_out, Hotel::Room.new(1))}.must_raise ArgumentError error.message.must_equal "Invalid date" + end it "raises an Argument Error for nil check_out" do From 0e2e0ae34dc084ef885c09b1a33d15aaf4169d33 Mon Sep 17 00:00:00 2001 From: nicoletabrandolini Date: Sat, 31 Mar 2018 11:44:00 -0700 Subject: [PATCH 12/13] Created file and add questions --- design-activity.md | 0 hotel | 30 ++++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 design-activity.md create mode 100644 hotel diff --git a/design-activity.md b/design-activity.md new file mode 100644 index 000000000..e69de29bb diff --git a/hotel b/hotel new file mode 100644 index 000000000..8034c82d1 --- /dev/null +++ b/hotel @@ -0,0 +1,30 @@ + +1. What classes does each implementation include? Are the lists the same? + + + +2. Write down a sentence to describe each class. + + +3. How do the classes relate to each other? It might be helpful to draw a diagram on a whiteboard or piece of paper. + +4. What data does each class store? How (if at all) does this differ between the two implementations? +5. What methods does each class have? How (if at all) does this differ between the two implementations? + +6. 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? + Does total_price directly manipulate the instance variables of other classes? + +7. If we decide items are cheaper if bought in bulk, how would this change the code? Which implementation is easier to modify? + +8. Which implementation better adheres to the single responsibility principle? + +9. Bonus question once you've read Metz ch. 3: Which implementation is more loosely coupled? + +>>>>>Revisiting Hotel>>>>>> + +1. What is this class's responsibility? + You should be able to describe it in a single sentence. +2. Is this class responsible for exactly one thing? +3. Does this class take on any responsibility that should be delegated to "lower level" classes? +4. Is there code in other classes that directly manipulates this class's instance variables? From 3649f5310a638ff48a59ff5d6665b7b08f428aff Mon Sep 17 00:00:00 2001 From: nicoletabrandolini Date: Sun, 1 Apr 2018 19:54:05 -0700 Subject: [PATCH 13/13] Completed questions on design-activity --- design-activity.md | 73 ++++++++++++++++++++++++++++++++++++++++++++++ hotel | 30 ------------------- 2 files changed, 73 insertions(+), 30 deletions(-) diff --git a/design-activity.md b/design-activity.md index e69de29bb..9a8e84475 100644 --- a/design-activity.md +++ b/design-activity.md @@ -0,0 +1,73 @@ + +1. What classes does each implementation include? Are the lists the same? + + +Yes both implementations have the same classes: CartEntry, ShoppingCart and Order. + +2. Write down a sentence to describe each class. + +For Implementation A: +CartEntry assigns the variables unit_price and quantity and gives access to them from outside the class. +ShoppingCart is in charge of the entries in the cart and gives access to it from outside. +Order instantiate a new instance of ShoppingCart and it calculates all the entries in it making use of the constant SALES_TAX. + +For Implementation B: +CartEntry assigns the variables unit_price and quantity, but also calculates the price based on unit_price and quantity. +ShoppingCart is in charge of the entries but also calculates the price for all entries. +Order creates a new instance of Shopping Cart and calculates the total_price by using SALES_TAX and the method price from the ShoppingCart. + +3. How do the classes relate to each other? It might be helpful to draw a diagram on a whiteboard or piece of paper. + +In Implementation A: +ShoppingCart stores instances of entries and then Order has an instance of shopping cart and calculates the price of each entry. + +In Implementation B: +ShoppingCart also gets the price of every entry and adds them to get the sum of the shopping cart. Then the Order gets an instance of ShoppingCart and calculates the total_price with tax. + +4. What data does each class store? How (if at all) does this differ between the two implementations? + + In implementation A only the class Order has the responsibility of calculating total_cost. Order does all the work in this implementation. + + In implementation B CartEntry calculates price, ShoppingCart returns sum and Order returns total_price.So each class does some work + + +5. What methods does each class have? How (if at all) does this differ between the two implementations? +All classes have initialize methods. In implementation A only the class Order has a total_price. +However in implementation B the CartEntry and the ShoppingCart have price methods and the Order has a total_price method. + +In Implementation B each class has their own responsibilities, while in Implementation A class Order does all the work + + + +6. Consider the Order#total_price method. In each implementation: + Is logic to compute the price delegated to "lower level" classes like ShoppingCart and CartEntry, or is it retained in Order? + + In Implementation A the price is retained in Order. + In Implementation B it is delegated to "lower level" classes. + + Does total_price directly manipulate the instance variables of other classes? + Yes for A and No for B. + + +7. If we decide items are cheaper if bought in bulk, how would this change the code? Which implementation is easier to modify? + +I believe B will be easier to change, since we could add it to the price method of the class CartEntry. + +8. Which implementation better adheres to the single responsibility principle? + +I think in implementation B by giving more responsibility to the lower classes. + +9. Bonus question once you've read Metz ch. 3: Which implementation is more loosely coupled? + +In implementation B the classes are more loosely coupled, so class B has less dependency with the other classes. + +>>>>>Revisiting Hotel>>>>>> + +1. What is this class's responsibility? + You should be able to describe it in a single sentence. +2. Is this class responsible for exactly one thing? +3. Does this class take on any responsibility that should be delegated to "lower level" classes? +4. Is there code in other classes that directly manipulates this class's instance variables? + + +I have not done Wave 3 of Hotel, but I only have 3 classes and I believe they all have their own responsibilities, and I should not have to delegate to other "lower level" classes. My frontdesk and reservation classes do all the work, but i feel like they both handle their own responsibilities. diff --git a/hotel b/hotel index 8034c82d1..e69de29bb 100644 --- a/hotel +++ b/hotel @@ -1,30 +0,0 @@ - -1. What classes does each implementation include? Are the lists the same? - - - -2. Write down a sentence to describe each class. - - -3. How do the classes relate to each other? It might be helpful to draw a diagram on a whiteboard or piece of paper. - -4. What data does each class store? How (if at all) does this differ between the two implementations? -5. What methods does each class have? How (if at all) does this differ between the two implementations? - -6. 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? - Does total_price directly manipulate the instance variables of other classes? - -7. If we decide items are cheaper if bought in bulk, how would this change the code? Which implementation is easier to modify? - -8. Which implementation better adheres to the single responsibility principle? - -9. Bonus question once you've read Metz ch. 3: Which implementation is more loosely coupled? - ->>>>>Revisiting Hotel>>>>>> - -1. What is this class's responsibility? - You should be able to describe it in a single sentence. -2. Is this class responsible for exactly one thing? -3. Does this class take on any responsibility that should be delegated to "lower level" classes? -4. Is there code in other classes that directly manipulates this class's instance variables?