From e4ddc918a9c4107a4bea214aac8d587c927961d2 Mon Sep 17 00:00:00 2001 From: Dominique Date: Wed, 4 Sep 2019 15:22:13 -0700 Subject: [PATCH 01/14] Date_Range instantiation --- lib/date_range.rb | 10 ++++++++++ lib/hotel_system.rb | 0 lib/reservation.rb | 0 lib/room.rb | 0 test/date_range_test.rb | 16 ++++++++++++++++ test/hotel_system_test.rb | 1 + test/reservation_test.rb | 1 + test/room_test.rb | 1 + test/test_helper.rb | 9 +++++++++ 9 files changed, 38 insertions(+) create mode 100644 lib/date_range.rb create mode 100644 lib/hotel_system.rb create mode 100644 lib/reservation.rb create mode 100644 lib/room.rb create mode 100644 test/date_range_test.rb create mode 100644 test/hotel_system_test.rb create mode 100644 test/reservation_test.rb create mode 100644 test/room_test.rb diff --git a/lib/date_range.rb b/lib/date_range.rb new file mode 100644 index 000000000..bd8127121 --- /dev/null +++ b/lib/date_range.rb @@ -0,0 +1,10 @@ +module Hotel + class Date_Range + attr_reader :start_date, :end_date + + def initialize(start_date, end_date) + @start_date = start_date + @end_date = end_date + end + end +end \ No newline at end of file diff --git a/lib/hotel_system.rb b/lib/hotel_system.rb new file mode 100644 index 000000000..e69de29bb diff --git a/lib/reservation.rb b/lib/reservation.rb new file mode 100644 index 000000000..e69de29bb diff --git a/lib/room.rb b/lib/room.rb new file mode 100644 index 000000000..e69de29bb diff --git a/test/date_range_test.rb b/test/date_range_test.rb new file mode 100644 index 000000000..203d72b41 --- /dev/null +++ b/test/date_range_test.rb @@ -0,0 +1,16 @@ +require_relative "test_helper" +require "date" + +describe "Date_Range" do + describe "initialize" do + it "Create an instance of Date_Range" do + #Arrange + start_date = Date.parse("2019-02-03") + end_date = Date.parse("2019-02-01") + #Act + date_range = Hotel::Date_Range.new(start_date, end_date) + #Assert + expect(date_range).must_be_kind_of Hotel::Date_Range + end + end +end \ No newline at end of file diff --git a/test/hotel_system_test.rb b/test/hotel_system_test.rb new file mode 100644 index 000000000..a46e68164 --- /dev/null +++ b/test/hotel_system_test.rb @@ -0,0 +1 @@ +require_relative "test_helper" \ No newline at end of file diff --git a/test/reservation_test.rb b/test/reservation_test.rb new file mode 100644 index 000000000..a46e68164 --- /dev/null +++ b/test/reservation_test.rb @@ -0,0 +1 @@ +require_relative "test_helper" \ No newline at end of file diff --git a/test/room_test.rb b/test/room_test.rb new file mode 100644 index 000000000..a46e68164 --- /dev/null +++ b/test/room_test.rb @@ -0,0 +1 @@ +require_relative "test_helper" \ No newline at end of file diff --git a/test/test_helper.rb b/test/test_helper.rb index c3a7695cf..265253972 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,8 +1,17 @@ # Add simplecov +require "simplecov" +SimpleCov.start + 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/hotel_system' +require_relative '../lib/reservation' +require_relative '../lib/room' +require_relative '../lib/date_range' + From 3d0277de04ee1e37b06e20f32b0c2eed021151c1 Mon Sep 17 00:00:00 2001 From: Dominique Date: Wed, 4 Sep 2019 17:04:54 -0700 Subject: [PATCH 02/14] throws an argument error if start date is after end date date_range_test.rb --- lib/date_range.rb | 14 ++++++++++++-- test/date_range_test.rb | 28 +++++++++++++++++++++++----- 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/lib/date_range.rb b/lib/date_range.rb index bd8127121..479f3cc9d 100644 --- a/lib/date_range.rb +++ b/lib/date_range.rb @@ -2,9 +2,19 @@ module Hotel class Date_Range attr_reader :start_date, :end_date - def initialize(start_date, end_date) + def initialize(start_date: , end_date:) @start_date = start_date @end_date = end_date + + raise ArgumentError, "start date cannot be after end date" if start_date > end_date end end -end \ No newline at end of file +end + + +# expect {Card.new(14, :clovers)}.must_raise ArgumentError + +# if (value > 13 || value < 1) || (suit_array.include?(suit) == false) +# raise ArgumentError, "You must provide a value 1..13 or suit +# (:hearts, :spades, :clubs, :diamonds)" +# end \ No newline at end of file diff --git a/test/date_range_test.rb b/test/date_range_test.rb index 203d72b41..baf678483 100644 --- a/test/date_range_test.rb +++ b/test/date_range_test.rb @@ -1,16 +1,34 @@ require_relative "test_helper" require "date" +# date is year-month-day describe "Date_Range" do - describe "initialize" do - it "Create an instance of Date_Range" do + describe "Date_Range instantiation" do + before do #Arrange start_date = Date.parse("2019-02-03") - end_date = Date.parse("2019-02-01") + end_date = Date.parse("2019-02-05") + #Act - date_range = Hotel::Date_Range.new(start_date, end_date) + @date_range = Hotel::Date_Range.new( + start_date: start_date, + end_date: end_date + ) + end + + it "is an instance of Date_Range" do #Assert - expect(date_range).must_be_kind_of Hotel::Date_Range + expect(@date_range).must_be_kind_of Hotel::Date_Range end + + it "throws an argument error if start date is after end date" do + #Arrange + start_date = Date.parse("2019-02-05") + end_date = Date.parse("2019-02-04") + + #Assert + expect{Hotel::Date_Range.new(start_date: start_date, end_date: end_date)}.must_raise ArgumentError + # when you initialize, it throws the argument error. This is why we instantiate Date_Range in the expect block. + end end end \ No newline at end of file From 3bc017475193dc60bb97c0dc69dfddd4b395631e Mon Sep 17 00:00:00 2001 From: Dominique Date: Wed, 4 Sep 2019 17:22:47 -0700 Subject: [PATCH 03/14] room instantiation room_test.rb --- lib/room.rb | 10 ++++++++++ test/room_test.rb | 23 ++++++++++++++++++++++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/lib/room.rb b/lib/room.rb index e69de29bb..251a83d18 100644 --- a/lib/room.rb +++ b/lib/room.rb @@ -0,0 +1,10 @@ +module Hotel + class Room + attr_reader :room_number, :room_cost + + def initialize(room_number: , room_cost:) + @room_number = room_number + @room_cost = room_cost + end + end +end \ No newline at end of file diff --git a/test/room_test.rb b/test/room_test.rb index a46e68164..ae0a44fc1 100644 --- a/test/room_test.rb +++ b/test/room_test.rb @@ -1 +1,22 @@ -require_relative "test_helper" \ No newline at end of file +require_relative "test_helper" + +describe "Room" do + describe "Room instantiation" do + before do + #Arrange + room_number = 2 + room_cost = 200 + + #Act + @room = Hotel::Room.new( + room_number: room_number, + room_cost: room_cost + ) + end + + it "is an instance of Room" do + #Assert + expect(@room).must_be_kind_of Hotel::Room + end + end +end From 4d3d56a66e4f7b5831b19733d409bae350927b53 Mon Sep 17 00:00:00 2001 From: Dominique Date: Wed, 4 Sep 2019 18:39:25 -0700 Subject: [PATCH 04/14] load_all method test --- lib/room.rb | 8 ++++++++ test/room_test.rb | 22 ++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/lib/room.rb b/lib/room.rb index 251a83d18..d1d4bcba1 100644 --- a/lib/room.rb +++ b/lib/room.rb @@ -6,5 +6,13 @@ def initialize(room_number: , room_cost:) @room_number = room_number @room_cost = room_cost end + + def self.load_all + + room_instances_array = (1..20).map do |i| + self.new(room_number:[i], + room_cost: 200) + end + end end end \ No newline at end of file diff --git a/test/room_test.rb b/test/room_test.rb index ae0a44fc1..2d7093ad4 100644 --- a/test/room_test.rb +++ b/test/room_test.rb @@ -18,5 +18,27 @@ #Assert expect(@room).must_be_kind_of Hotel::Room end + end + + describe "load_all method" do + it "returns array of 20 elements" do + #Act + result = Hotel::Room.load_all + #don't have to arrange anything because it is a self method and nothing is being passed in + + #Assert + expect(result).must_be_kind_of Array + expect(result.length).must_equal 20 + end + + it "each element in returned array is a room object" do + #Act + result = Hotel::Room.load_all + + #Assert + result.each do |object| + expect(object).must_be_kind_of Hotel::Room + end + end end end From 190aac8bcc7aebb6d3874900bd745fdfe4949547 Mon Sep 17 00:00:00 2001 From: Dominique Date: Fri, 6 Sep 2019 13:45:37 -0700 Subject: [PATCH 05/14] hotel_system instantiation tests --- lib/date_range.rb | 7 ------- lib/hotel_system.rb | 10 +++++++++ lib/reservation.rb | 24 ++++++++++++++++++++++ lib/room.rb | 1 - test/hotel_system_test.rb | 37 ++++++++++++++++++++++++++++++++- test/reservation_test.rb | 43 ++++++++++++++++++++++++++++++++++++++- test/room_test.rb | 22 ++++++++++---------- 7 files changed, 123 insertions(+), 21 deletions(-) diff --git a/lib/date_range.rb b/lib/date_range.rb index 479f3cc9d..48f915822 100644 --- a/lib/date_range.rb +++ b/lib/date_range.rb @@ -11,10 +11,3 @@ def initialize(start_date: , end_date:) end end - -# expect {Card.new(14, :clovers)}.must_raise ArgumentError - -# if (value > 13 || value < 1) || (suit_array.include?(suit) == false) -# raise ArgumentError, "You must provide a value 1..13 or suit -# (:hearts, :spades, :clubs, :diamonds)" -# end \ No newline at end of file diff --git a/lib/hotel_system.rb b/lib/hotel_system.rb index e69de29bb..0dba241e4 100644 --- a/lib/hotel_system.rb +++ b/lib/hotel_system.rb @@ -0,0 +1,10 @@ +module Hotel + class Hotel_System + attr_reader :rooms, :reservations + + def initialize(rooms: , reservations:) + @rooms = rooms + @reservations = reservations || [] + end + end +end diff --git a/lib/reservation.rb b/lib/reservation.rb index e69de29bb..00e685c20 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -0,0 +1,24 @@ +require "pry" + +module Hotel + class Reservation + # attr_reader is making the instance variables readable + attr_reader :id, :room, :date_range, :total_cost + + # parameter is just taking in input from user + def initialize(id: , room: , date_range: ) + @id = id + @room = room + @date_range = date_range + @total_cost = calculate_reservation_cost + #you wouldn't set "calculate_reservation_cost" to the total_cost parameter + # because you don't want to give the user the option of changing it. + + end + + def calculate_reservation_cost + duration_of_stay = date_range.end_date - date_range.start_date + duration_of_stay.to_i * 200 + end + end +end diff --git a/lib/room.rb b/lib/room.rb index d1d4bcba1..97c72e44b 100644 --- a/lib/room.rb +++ b/lib/room.rb @@ -8,7 +8,6 @@ def initialize(room_number: , room_cost:) end def self.load_all - room_instances_array = (1..20).map do |i| self.new(room_number:[i], room_cost: 200) diff --git a/test/hotel_system_test.rb b/test/hotel_system_test.rb index a46e68164..ffbfd1541 100644 --- a/test/hotel_system_test.rb +++ b/test/hotel_system_test.rb @@ -1 +1,36 @@ -require_relative "test_helper" \ No newline at end of file +require_relative "test_helper" +require "Pry" + +describe "Hotel_System" do + before do + #Arrange + rooms = [Hotel::Room.new(room_number: 2, room_cost: 200)] + reservations = + [ + Hotel::Reservation.new(id: 1, + room: Hotel::Room.new(room_number: 3, room_cost: 200), + date_range: Hotel::Date_Range.new(start_date: Date.parse("2019-02-03"), + end_date: Date.parse("2019-02-05"))) + ] + + #Act + @hotel_system = Hotel::Hotel_System.new(rooms: rooms, + reservations: reservations) + # binding.pry + end + + describe "Hotel_System instantiation" do + it "is an instance of Hotel" do + #Assert + expect(@hotel_system).must_be_kind_of Hotel::Hotel_System + end + + it "stores an instance of room" do + expect(@hotel_system.rooms[0]).must_be_kind_of Hotel::Room + end + + it "stores an instance of reservation" do + expect(@hotel_system.reservations[0]).must_be_kind_of Hotel::Reservation + end + end +end diff --git a/test/reservation_test.rb b/test/reservation_test.rb index a46e68164..a9914bc35 100644 --- a/test/reservation_test.rb +++ b/test/reservation_test.rb @@ -1 +1,42 @@ -require_relative "test_helper" \ No newline at end of file +require_relative "test_helper" + +describe "Reservation" do + before do + #Arrange + id = 1 + room = Hotel::Room.new(room_number: 1, room_cost: 200) + date_range = Hotel::Date_Range.new(start_date: Date.parse("2019-02-03"), end_date: Date.parse("2019-02-05")) + + #Act + @reservation = Hotel::Reservation.new(id: id, + room: room, + date_range: date_range + ) + end + + describe "Reservation instantiation" do + it "is instance of Reservation" do + #Assert + expect(@reservation).must_be_kind_of Hotel::Reservation + end + + it "stores an instance of room" do + expect(@reservation.room).must_be_kind_of Hotel::Room + end + + it "stores an instance of date_range" do + expect(@reservation.date_range).must_be_kind_of Hotel::Date_Range + end + end + + describe "calculate_reservation_cost method" do + it "calculates the cost of a reservation" do + #Act + result = @reservation.calculate_reservation_cost + + #Assert + expect(result).must_be_kind_of Integer + expect(result).must_be :>, 0 + end + end +end \ No newline at end of file diff --git a/test/room_test.rb b/test/room_test.rb index 2d7093ad4..c4f9c3395 100644 --- a/test/room_test.rb +++ b/test/room_test.rb @@ -1,19 +1,19 @@ require_relative "test_helper" describe "Room" do - describe "Room instantiation" do - before do - #Arrange - room_number = 2 - room_cost = 200 + before do + #Arrange + room_number = 2 + room_cost = 200 - #Act - @room = Hotel::Room.new( - room_number: room_number, - room_cost: room_cost - ) - end + #Act + @room = Hotel::Room.new( + room_number: room_number, + room_cost: room_cost + ) + end + describe "Room instantiation" do it "is an instance of Room" do #Assert expect(@room).must_be_kind_of Hotel::Room From 4a8713e8c81b4293ffe4d79804b71567b8f42928 Mon Sep 17 00:00:00 2001 From: Dominique Date: Fri, 6 Sep 2019 14:42:25 -0700 Subject: [PATCH 06/14] list_rooms method added --- lib/hotel_system.rb | 13 +++++++++++-- lib/room.rb | 6 +++--- test/hotel_system_test.rb | 25 +++++++++++++++++++++++-- 3 files changed, 37 insertions(+), 7 deletions(-) diff --git a/lib/hotel_system.rb b/lib/hotel_system.rb index 0dba241e4..f439650ea 100644 --- a/lib/hotel_system.rb +++ b/lib/hotel_system.rb @@ -2,9 +2,18 @@ module Hotel class Hotel_System attr_reader :rooms, :reservations - def initialize(rooms: , reservations:) + def initialize(rooms: Hotel::Room.load_all, reservations: []) @rooms = rooms - @reservations = reservations || [] + @reservations = reservations end + + def list_rooms + rooms + end + + # def make_reservation(start_date, end_date) + # end + + end end diff --git a/lib/room.rb b/lib/room.rb index 97c72e44b..2869a355a 100644 --- a/lib/room.rb +++ b/lib/room.rb @@ -8,9 +8,9 @@ def initialize(room_number: , room_cost:) end def self.load_all - room_instances_array = (1..20).map do |i| - self.new(room_number:[i], - room_cost: 200) + (1..20).map do |i| + self.new(room_number:[i], + room_cost: 200) end end end diff --git a/test/hotel_system_test.rb b/test/hotel_system_test.rb index ffbfd1541..e4c0f5a46 100644 --- a/test/hotel_system_test.rb +++ b/test/hotel_system_test.rb @@ -4,7 +4,7 @@ describe "Hotel_System" do before do #Arrange - rooms = [Hotel::Room.new(room_number: 2, room_cost: 200)] + rooms = Hotel::Room.load_all reservations = [ Hotel::Reservation.new(id: 1, @@ -16,7 +16,6 @@ #Act @hotel_system = Hotel::Hotel_System.new(rooms: rooms, reservations: reservations) - # binding.pry end describe "Hotel_System instantiation" do @@ -33,4 +32,26 @@ expect(@hotel_system.reservations[0]).must_be_kind_of Hotel::Reservation end end + + describe "list_rooms" do + it "returns a list of all the rooms in the hotel" do + + expect(@hotel_system.list_rooms).must_be_kind_of Array + + expect(@hotel_system.list_rooms.length).must_equal 20 + expect(@hotel_system.list_rooms[0]).must_be_kind_of Hotel::Room + end + end + + # describe "make_reservation" do + # it "makes a reservation when given a date range" do + # #Act + # result = make_reservation(Date.parse("2019-03-05"), Date.parse("2019-03-07")) + # #Assert + # expect(@hotel_system.make_reservation).must_be_kind_of Hotel::Reservation + # end + + # it "pushes new reservation to Hotel_System's array of reservations" do + # # end + # end end From e7c6c60ed31d2baa4f4014fe642c7e67153c2369 Mon Sep 17 00:00:00 2001 From: Dominique Date: Sat, 7 Sep 2019 11:53:13 -0700 Subject: [PATCH 07/14] Wave 1 complete --- lib/hotel_system.rb | 22 +++++++++++--- test/hotel_system_test.rb | 64 ++++++++++++++++++++++++++++++++------- 2 files changed, 71 insertions(+), 15 deletions(-) diff --git a/lib/hotel_system.rb b/lib/hotel_system.rb index f439650ea..1334630ed 100644 --- a/lib/hotel_system.rb +++ b/lib/hotel_system.rb @@ -1,6 +1,7 @@ module Hotel class Hotel_System - attr_reader :rooms, :reservations + attr_reader :rooms + attr_accessor :reservations def initialize(rooms: Hotel::Room.load_all, reservations: []) @rooms = rooms @@ -11,9 +12,22 @@ def list_rooms rooms end - # def make_reservation(start_date, end_date) - # end - + def make_reservation(start_date, end_date) + reservations << Hotel::Reservation.new(id: 2, + room: Hotel::Room.new(room_number: 3, room_cost: 200), + date_range: Hotel::Date_Range.new(start_date: start_date, + end_date: end_date)) + # binding.pry + end + def find_reservation(start_date, end_date) + found_reservations = [] + reservations.each do |reservation| + if reservation.date_range.start_date == start_date && reservation.date_range.end_date == end_date + found_reservations.push(reservation) + end + end + found_reservations + end end end diff --git a/test/hotel_system_test.rb b/test/hotel_system_test.rb index e4c0f5a46..2037f8de4 100644 --- a/test/hotel_system_test.rb +++ b/test/hotel_system_test.rb @@ -1,5 +1,6 @@ require_relative "test_helper" require "Pry" +require "minitest/skip_dsl" describe "Hotel_System" do before do @@ -43,15 +44,56 @@ end end - # describe "make_reservation" do - # it "makes a reservation when given a date range" do - # #Act - # result = make_reservation(Date.parse("2019-03-05"), Date.parse("2019-03-07")) - # #Assert - # expect(@hotel_system.make_reservation).must_be_kind_of Hotel::Reservation - # end - - # it "pushes new reservation to Hotel_System's array of reservations" do - # # end - # end + describe "make_reservation" do + it "makes a reservation when given a date range" do + #Arrange + # start_date = @hotel_system.reservations[0].date_range.start_date + # end_date = @hotel_system.reservations[0].date_range.end_date + start_date = Date.parse("2019-02-08") + end_date = Date.parse("2019-02-12") + + #Act + result = @hotel_system.make_reservation(start_date, end_date) + + #Assert + expect(result).must_be_kind_of Array + expect(result[1]).must_be_kind_of Hotel::Reservation + expect(result[1].date_range.start_date).must_equal start_date + expect(result[1].date_range.end_date).must_equal end_date + end + + it "pushes a new reservation to Hotel_System's reservation instance variable" do + #Arrange + # start_date = @hotel_system.reservations[0].date_range.start_date + # end_date = @hotel_system.reservations[0].date_range.end_date + start_date = Date.parse("2019-02-08") + end_date = Date.parse("2019-02-12") + + #Act + result = @hotel_system.make_reservation(start_date, end_date) + + #Assert + expect(@hotel_system.reservations.length).must_equal 2 + end + end + + describe "find_reservation" do + it "finds reservations booked for a specific date" do + #Arrange + start_date = Date.parse("2019-12-11") + end_date = Date.parse("2019-12-17") + + #Act + @hotel_system.make_reservation(start_date, end_date) + result = @hotel_system.find_reservation(start_date, end_date) + + #Assert + expect(result).must_be_kind_of Array + expect(result[0]).must_be_kind_of Hotel::Reservation + expect(result.length).must_equal 1 + end + end end + +# I can access the list of reservations for a specific date, so that I can track reservations +# by date From 5d9afc40b578d04bf31ceb1db36e695f8699d188 Mon Sep 17 00:00:00 2001 From: Dominique Date: Sat, 7 Sep 2019 15:00:44 -0700 Subject: [PATCH 08/14] Wave 2 list_available_rooms test passed --- lib/hotel_system.rb | 49 +++++++++++++++++++++++++++++++++++++++ lib/room.rb | 2 +- test/hotel_system_test.rb | 19 +++++++++++++-- 3 files changed, 67 insertions(+), 3 deletions(-) diff --git a/lib/hotel_system.rb b/lib/hotel_system.rb index 1334630ed..374b37891 100644 --- a/lib/hotel_system.rb +++ b/lib/hotel_system.rb @@ -29,5 +29,54 @@ def find_reservation(start_date, end_date) end found_reservations end + + #I can view a list of rooms that are not reserved for a given date range, so that I can see all + #available rooms for that day + + # I can access the list of reservations for a specific date, so that I can track reservations + # by date + + # def list_available_rooms(start_date, end_date) + # reserved_room_numbers = [] + # reservations.each do |reservation| + # if reservation.date_range.start_date == start_date && reservation.date_range.end_date == end_date + # reserved_room_numbers.push(reservation.room.room_number) + # end + # end + + # available_rooms = [] + # rooms.each do |room| + # if reserved_room_numbers.include?(room.room_number) == false + # available_rooms.push(room) + # end + # end + # available_rooms + # end + + def list_available_rooms (start_date, end_date) + booked_rooms = list_reserved_room_numbers(start_date, end_date) + available_rooms = [] + rooms.each do |room| + if booked_rooms.include?(room.room_number) == false + available_rooms.push(room) + end + end + available_rooms + end + + private + # This won't need a test since it is private + def list_reserved_room_numbers(start_date, end_date) + reserved_room_numbers = [] + reservations.each do |reservation| + if reservation.date_range.start_date == start_date && reservation.date_range.end_date == end_date + reserved_room_numbers.push(reservation.room.room_number) + end + end + reserved_room_numbers + end end end + +#All the rooms are booked? +# No rooms are booked? diff --git a/lib/room.rb b/lib/room.rb index 2869a355a..22c843172 100644 --- a/lib/room.rb +++ b/lib/room.rb @@ -9,7 +9,7 @@ def initialize(room_number: , room_cost:) def self.load_all (1..20).map do |i| - self.new(room_number:[i], + self.new(room_number: i, room_cost: 200) end end diff --git a/test/hotel_system_test.rb b/test/hotel_system_test.rb index 2037f8de4..24706d64d 100644 --- a/test/hotel_system_test.rb +++ b/test/hotel_system_test.rb @@ -17,6 +17,7 @@ #Act @hotel_system = Hotel::Hotel_System.new(rooms: rooms, reservations: reservations) + end describe "Hotel_System instantiation" do @@ -93,7 +94,21 @@ expect(result.length).must_equal 1 end end + #Wave 2 + describe "list_available_rooms" do + it "lists available rooms for a given date" do + #Arrange + start_date = Date.parse("2019-02-03") + end_date = Date.parse("2019-02-05") + + #Act + result = @hotel_system.list_available_rooms(start_date, end_date) + + #Assert + expect(result).must_be_kind_of Array + result.each {|room| expect(room).must_be_kind_of Hotel::Room} + expect(result.length).must_equal 19 + end + end end -# I can access the list of reservations for a specific date, so that I can track reservations -# by date From c099b5c9aec9fb17f8beaeb677161375fe71378c Mon Sep 17 00:00:00 2001 From: Dominique Date: Sat, 7 Sep 2019 23:31:21 -0700 Subject: [PATCH 09/14] Wave 2 complete --- lib/hotel_system.rb | 68 ++++++++++++++++++++++----------------- lib/reservation.rb | 5 ++- test/hotel_system_test.rb | 19 +++++++++-- test/reservation_test.rb | 3 +- 4 files changed, 59 insertions(+), 36 deletions(-) diff --git a/lib/hotel_system.rb b/lib/hotel_system.rb index 374b37891..c8ba85048 100644 --- a/lib/hotel_system.rb +++ b/lib/hotel_system.rb @@ -1,3 +1,4 @@ +require "Pry" module Hotel class Hotel_System attr_reader :rooms @@ -12,47 +13,50 @@ def list_rooms rooms end + def no_rooms_available + raise ArgumentError, "There are no rooms available for that given date range." + end + + def list_available_room_numbers(start_date, end_date) + available_rooms = list_available_rooms(start_date, end_date) + available_room_numbers = available_rooms.map do |room| + available_rooms[0].room_number + end + available_room_numbers.sample + end + def make_reservation(start_date, end_date) - reservations << Hotel::Reservation.new(id: 2, - room: Hotel::Room.new(room_number: 3, room_cost: 200), + + if list_available_room_numbers(start_date, end_date) == nil + no_rooms_available + else + reservations << Hotel::Reservation.new( + room: Hotel::Room.new(room_number: list_available_room_numbers(start_date, end_date), room_cost: 200), date_range: Hotel::Date_Range.new(start_date: start_date, end_date: end_date)) - # binding.pry + end end + # I can access the list of reservations for a specific date, so that I can track reservations by date def find_reservation(start_date, end_date) found_reservations = [] reservations.each do |reservation| - if reservation.date_range.start_date == start_date && reservation.date_range.end_date == end_date + # if reservation.date_range.start_date == start_date && reservation.date_range.end_date == end_date + # found_reservations.push(reservation) + # end + if (reservation.date_range.start_date >= start_date && reservation.date_range.start_date < end_date) || + (reservation.date_range.end_date >= start_date && reservation.date_range.end_date <= end_date) found_reservations.push(reservation) - end + end end found_reservations end - #I can view a list of rooms that are not reserved for a given date range, so that I can see all - #available rooms for that day - - # I can access the list of reservations for a specific date, so that I can track reservations - # by date - - # def list_available_rooms(start_date, end_date) - # reserved_room_numbers = [] - # reservations.each do |reservation| - # if reservation.date_range.start_date == start_date && reservation.date_range.end_date == end_date - # reserved_room_numbers.push(reservation.room.room_number) - # end - # end - - # available_rooms = [] - # rooms.each do |room| - # if reserved_room_numbers.include?(room.room_number) == false - # available_rooms.push(room) - # end - # end - # available_rooms - # end + # I can get/make a reservation of a room for a given date range, and that room + # will not be part of any other reservation overlapping that date range + # A reservation is allowed start on the same day that another reservation for the same room ends + #returns a list of available rooms for this given date range def list_available_rooms (start_date, end_date) booked_rooms = list_reserved_room_numbers(start_date, end_date) available_rooms = [] @@ -69,14 +73,20 @@ def list_available_rooms (start_date, end_date) def list_reserved_room_numbers(start_date, end_date) reserved_room_numbers = [] reservations.each do |reservation| - if reservation.date_range.start_date == start_date && reservation.date_range.end_date == end_date + # if reservation.date_range.start_date == start_date && reservation.date_range.end_date == end_date + # reserved_room_numbers.push(reservation.room.room_number) + # end + if (reservation.date_range.start_date >= start_date && reservation.date_range.start_date < end_date) || + (reservation.date_range.end_date >= start_date && reservation.date_range.end_date <= end_date) reserved_room_numbers.push(reservation.room.room_number) - end + end end reserved_room_numbers + end end end +#Edge cases #All the rooms are booked? # No rooms are booked? diff --git a/lib/reservation.rb b/lib/reservation.rb index 00e685c20..8d1edbb40 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -3,11 +3,10 @@ module Hotel class Reservation # attr_reader is making the instance variables readable - attr_reader :id, :room, :date_range, :total_cost + attr_reader :room, :date_range, :total_cost # parameter is just taking in input from user - def initialize(id: , room: , date_range: ) - @id = id + def initialize(room: , date_range: ) @room = room @date_range = date_range @total_cost = calculate_reservation_cost diff --git a/test/hotel_system_test.rb b/test/hotel_system_test.rb index 24706d64d..661c741ca 100644 --- a/test/hotel_system_test.rb +++ b/test/hotel_system_test.rb @@ -8,7 +8,7 @@ rooms = Hotel::Room.load_all reservations = [ - Hotel::Reservation.new(id: 1, + Hotel::Reservation.new( room: Hotel::Room.new(room_number: 3, room_cost: 200), date_range: Hotel::Date_Range.new(start_date: Date.parse("2019-02-03"), end_date: Date.parse("2019-02-05"))) @@ -17,7 +17,6 @@ #Act @hotel_system = Hotel::Hotel_System.new(rooms: rooms, reservations: reservations) - end describe "Hotel_System instantiation" do @@ -76,6 +75,21 @@ #Assert expect(@hotel_system.reservations.length).must_equal 2 end + + it "Raises an argument error if there are no rooms available for the given date range" do + #Arrange + start_date = Date.parse("2019-02-03") + end_date = Date.parse("2019-02-05") + + #Act + 19.times do + @hotel_system.make_reservation(start_date, end_date) + end + # @hotel_system.reservations[0].room.room_number + + #Assert + expect{@hotel_system.make_reservation(start_date, end_date)}.must_raise ArgumentError + end end describe "find_reservation" do @@ -94,6 +108,7 @@ expect(result.length).must_equal 1 end end + #Wave 2 describe "list_available_rooms" do it "lists available rooms for a given date" do diff --git a/test/reservation_test.rb b/test/reservation_test.rb index a9914bc35..2ed346f8e 100644 --- a/test/reservation_test.rb +++ b/test/reservation_test.rb @@ -3,12 +3,11 @@ describe "Reservation" do before do #Arrange - id = 1 room = Hotel::Room.new(room_number: 1, room_cost: 200) date_range = Hotel::Date_Range.new(start_date: Date.parse("2019-02-03"), end_date: Date.parse("2019-02-05")) #Act - @reservation = Hotel::Reservation.new(id: id, + @reservation = Hotel::Reservation.new( room: room, date_range: date_range ) From 45840af48b94dc058bba4bb79ec713f33d4315fc Mon Sep 17 00:00:00 2001 From: Dominique Date: Mon, 9 Sep 2019 07:49:59 -0700 Subject: [PATCH 10/14] created refactors.txt file --- refactors.txt | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 refactors.txt diff --git a/refactors.txt b/refactors.txt new file mode 100644 index 000000000..e69de29bb From 9e28570c2ac0703b969942232a19e3772889df63 Mon Sep 17 00:00:00 2001 From: Dominique Date: Mon, 9 Sep 2019 07:51:25 -0700 Subject: [PATCH 11/14] final submission --- lib/hotel_system.rb | 20 +++----------------- lib/reservation.rb | 5 ----- refactors.txt | 0 test/date_range_test.rb | 8 +------- test/hotel_system_test.rb | 24 ------------------------ test/reservation_test.rb | 5 ----- test/room_test.rb | 8 +------- 7 files changed, 5 insertions(+), 65 deletions(-) delete mode 100644 refactors.txt diff --git a/lib/hotel_system.rb b/lib/hotel_system.rb index c8ba85048..8749734c2 100644 --- a/lib/hotel_system.rb +++ b/lib/hotel_system.rb @@ -37,13 +37,10 @@ def make_reservation(start_date, end_date) end end - # I can access the list of reservations for a specific date, so that I can track reservations by date def find_reservation(start_date, end_date) found_reservations = [] reservations.each do |reservation| - # if reservation.date_range.start_date == start_date && reservation.date_range.end_date == end_date - # found_reservations.push(reservation) - # end + if (reservation.date_range.start_date >= start_date && reservation.date_range.start_date < end_date) || (reservation.date_range.end_date >= start_date && reservation.date_range.end_date <= end_date) found_reservations.push(reservation) @@ -52,11 +49,6 @@ def find_reservation(start_date, end_date) found_reservations end - # I can get/make a reservation of a room for a given date range, and that room - # will not be part of any other reservation overlapping that date range - # A reservation is allowed start on the same day that another reservation for the same room ends - - #returns a list of available rooms for this given date range def list_available_rooms (start_date, end_date) booked_rooms = list_reserved_room_numbers(start_date, end_date) available_rooms = [] @@ -68,14 +60,10 @@ def list_available_rooms (start_date, end_date) available_rooms end - private - # This won't need a test since it is private def list_reserved_room_numbers(start_date, end_date) reserved_room_numbers = [] reservations.each do |reservation| - # if reservation.date_range.start_date == start_date && reservation.date_range.end_date == end_date - # reserved_room_numbers.push(reservation.room.room_number) - # end + if (reservation.date_range.start_date >= start_date && reservation.date_range.start_date < end_date) || (reservation.date_range.end_date >= start_date && reservation.date_range.end_date <= end_date) reserved_room_numbers.push(reservation.room.room_number) @@ -87,6 +75,4 @@ def list_reserved_room_numbers(start_date, end_date) end end -#Edge cases -#All the rooms are booked? -# No rooms are booked? + diff --git a/lib/reservation.rb b/lib/reservation.rb index 8d1edbb40..7eea5d515 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -2,17 +2,12 @@ module Hotel class Reservation - # attr_reader is making the instance variables readable attr_reader :room, :date_range, :total_cost - # parameter is just taking in input from user def initialize(room: , date_range: ) @room = room @date_range = date_range @total_cost = calculate_reservation_cost - #you wouldn't set "calculate_reservation_cost" to the total_cost parameter - # because you don't want to give the user the option of changing it. - end def calculate_reservation_cost diff --git a/refactors.txt b/refactors.txt deleted file mode 100644 index e69de29bb..000000000 diff --git a/test/date_range_test.rb b/test/date_range_test.rb index baf678483..08111f9ac 100644 --- a/test/date_range_test.rb +++ b/test/date_range_test.rb @@ -1,15 +1,13 @@ require_relative "test_helper" require "date" -# date is year-month-day describe "Date_Range" do describe "Date_Range instantiation" do before do - #Arrange + start_date = Date.parse("2019-02-03") end_date = Date.parse("2019-02-05") - #Act @date_range = Hotel::Date_Range.new( start_date: start_date, end_date: end_date @@ -17,18 +15,14 @@ end it "is an instance of Date_Range" do - #Assert expect(@date_range).must_be_kind_of Hotel::Date_Range end it "throws an argument error if start date is after end date" do - #Arrange start_date = Date.parse("2019-02-05") end_date = Date.parse("2019-02-04") - #Assert expect{Hotel::Date_Range.new(start_date: start_date, end_date: end_date)}.must_raise ArgumentError - # when you initialize, it throws the argument error. This is why we instantiate Date_Range in the expect block. end end end \ No newline at end of file diff --git a/test/hotel_system_test.rb b/test/hotel_system_test.rb index 661c741ca..7c44f23df 100644 --- a/test/hotel_system_test.rb +++ b/test/hotel_system_test.rb @@ -4,7 +4,6 @@ describe "Hotel_System" do before do - #Arrange rooms = Hotel::Room.load_all reservations = [ @@ -14,14 +13,12 @@ end_date: Date.parse("2019-02-05"))) ] - #Act @hotel_system = Hotel::Hotel_System.new(rooms: rooms, reservations: reservations) end describe "Hotel_System instantiation" do it "is an instance of Hotel" do - #Assert expect(@hotel_system).must_be_kind_of Hotel::Hotel_System end @@ -46,16 +43,11 @@ describe "make_reservation" do it "makes a reservation when given a date range" do - #Arrange - # start_date = @hotel_system.reservations[0].date_range.start_date - # end_date = @hotel_system.reservations[0].date_range.end_date start_date = Date.parse("2019-02-08") end_date = Date.parse("2019-02-12") - #Act result = @hotel_system.make_reservation(start_date, end_date) - #Assert expect(result).must_be_kind_of Array expect(result[1]).must_be_kind_of Hotel::Reservation expect(result[1].date_range.start_date).must_equal start_date @@ -63,63 +55,47 @@ end it "pushes a new reservation to Hotel_System's reservation instance variable" do - #Arrange - # start_date = @hotel_system.reservations[0].date_range.start_date - # end_date = @hotel_system.reservations[0].date_range.end_date start_date = Date.parse("2019-02-08") end_date = Date.parse("2019-02-12") - #Act result = @hotel_system.make_reservation(start_date, end_date) - #Assert expect(@hotel_system.reservations.length).must_equal 2 end it "Raises an argument error if there are no rooms available for the given date range" do - #Arrange start_date = Date.parse("2019-02-03") end_date = Date.parse("2019-02-05") - #Act 19.times do @hotel_system.make_reservation(start_date, end_date) end - # @hotel_system.reservations[0].room.room_number - #Assert expect{@hotel_system.make_reservation(start_date, end_date)}.must_raise ArgumentError end end describe "find_reservation" do it "finds reservations booked for a specific date" do - #Arrange start_date = Date.parse("2019-12-11") end_date = Date.parse("2019-12-17") - #Act @hotel_system.make_reservation(start_date, end_date) result = @hotel_system.find_reservation(start_date, end_date) - #Assert expect(result).must_be_kind_of Array expect(result[0]).must_be_kind_of Hotel::Reservation expect(result.length).must_equal 1 end end - #Wave 2 describe "list_available_rooms" do it "lists available rooms for a given date" do - #Arrange start_date = Date.parse("2019-02-03") end_date = Date.parse("2019-02-05") - #Act result = @hotel_system.list_available_rooms(start_date, end_date) - #Assert expect(result).must_be_kind_of Array result.each {|room| expect(room).must_be_kind_of Hotel::Room} expect(result.length).must_equal 19 diff --git a/test/reservation_test.rb b/test/reservation_test.rb index 2ed346f8e..42523a0e9 100644 --- a/test/reservation_test.rb +++ b/test/reservation_test.rb @@ -2,11 +2,9 @@ describe "Reservation" do before do - #Arrange room = Hotel::Room.new(room_number: 1, room_cost: 200) date_range = Hotel::Date_Range.new(start_date: Date.parse("2019-02-03"), end_date: Date.parse("2019-02-05")) - #Act @reservation = Hotel::Reservation.new( room: room, date_range: date_range @@ -15,7 +13,6 @@ describe "Reservation instantiation" do it "is instance of Reservation" do - #Assert expect(@reservation).must_be_kind_of Hotel::Reservation end @@ -30,10 +27,8 @@ describe "calculate_reservation_cost method" do it "calculates the cost of a reservation" do - #Act result = @reservation.calculate_reservation_cost - #Assert expect(result).must_be_kind_of Integer expect(result).must_be :>, 0 end diff --git a/test/room_test.rb b/test/room_test.rb index c4f9c3395..0ac5c95e0 100644 --- a/test/room_test.rb +++ b/test/room_test.rb @@ -15,27 +15,21 @@ describe "Room instantiation" do it "is an instance of Room" do - #Assert expect(@room).must_be_kind_of Hotel::Room end end describe "load_all method" do it "returns array of 20 elements" do - #Act result = Hotel::Room.load_all - #don't have to arrange anything because it is a self method and nothing is being passed in - - #Assert + expect(result).must_be_kind_of Array expect(result.length).must_equal 20 end it "each element in returned array is a room object" do - #Act result = Hotel::Room.load_all - #Assert result.each do |object| expect(object).must_be_kind_of Hotel::Room end From 33bd604522aaab6b76d6d4b8f967221853713aff Mon Sep 17 00:00:00 2001 From: Dominique Date: Mon, 9 Sep 2019 08:03:09 -0700 Subject: [PATCH 12/14] created refactors.txt --- lib/refactors.txt | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 lib/refactors.txt diff --git a/lib/refactors.txt b/lib/refactors.txt new file mode 100644 index 000000000..73aecbfaf --- /dev/null +++ b/lib/refactors.txt @@ -0,0 +1,11 @@ +Room Class + + I definitely did not need a room class. My current room class made things has no useful methods. Also, since the rooms instance variable + in hotel system is an array of room objects, it made it harder to access the room numbers. I ended up making unnecessary methods in hotel system + like "list_available_room_numbers" because I had to extract the room numbers from the room objects. If I had no room class and + simply just made the rooms instance variable in hotel system an array of integers 1 -20, I wouldn't need the extra methods. + Also, there was no reason to list the room cost. Every room is $200 and total cost is calculated in the reservations class. + +Use of Let + + Try to use let when you refactor hotel. Before is useful, but I feel like let could dry up your code. \ No newline at end of file From be2b253dc5d752251a1fb3dde27a36cb9fa77e98 Mon Sep 17 00:00:00 2001 From: Dominique Date: Thu, 26 Sep 2019 14:56:27 -0700 Subject: [PATCH 13/14] design-activity.md --- lib/design-activity.md | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 lib/design-activity.md diff --git a/lib/design-activity.md b/lib/design-activity.md new file mode 100644 index 000000000..608a4c3b0 --- /dev/null +++ b/lib/design-activity.md @@ -0,0 +1,30 @@ +Question 1: What classes does each implementation include? Are the lists the same? +Answer: Implementation A and Implementation B both include CartEntry, ShoppingCart and Order classes. + +Question 2: Write down a sentence to describe each class. +Answer: Implementation A - The CartEntry class creates a single CartEntry object and defines its unit_price and quantity. The ShoppingCart class keeps track of all the CartEntry objects. The Order class calculates the total price of all the entries in the ShoppingCart by iterating through ShoppingCart's array, pulling out the unit_price and quantity attributes from each CartEntry object and then multiplying the sales_tax to the entries' subtotal. +Answer: Implementation B - The CartEntry class now stores a state AND a behavior. It is instantiated with unit_price and quantity, and holds a method that calculates and returns the item/s subtotal. The ShoppingCart class keeps track of the CartEntry objects, and now holds a method that calls on CartEntry to return the sum price of all its entry objects. The Order class holds a method that returns the total price of all the entries. Now, instead of calculating the subtotal itself, the class simply asks the ShoppingCart class for the sum price of all the entry objects and then multiplies the sales_tax to that subtotal to return the total_price. + +Question 3: How do the classes relate to each other? It might be helpful to draw a diagram on a whiteboard or piece of paper. +Answer: In both implementations, the Order class has one ShoppingCart object, and the ShoppingCart class has many cart entry objects. + + +Question 4: What data does each class store? How (if at all) does this differ between the two implementations? +Answer: Each class in both implementations store the same data. The CartEntry class stores unit_price and quantity. The ShoppingCart class stores an array of CartEntry objects. The Order class stores a ShoppingCart object. + +Question 5: What methods does each class have? How (if at all) does this differ between the two implementations? +Answer: Implementation A - The CartEntry and ShoppingCart classes do not hold methods. The Order class holds one "total_price" method. +Answer: Implementation B - The CartEntry and Shopping classes now each hold one method called "price." The Order class still holds one method called "total_price." + +Question 6: Consider the Order#total_price method. +Answer: Implementation A - Logic to compute the price is retained in Order. The Order class knows too much about the ShoppingCart and CartEntry classes and also manipulates both of those classes' states (instance variables) to calculate the total_price. +Answer: Implementation B - Unlike the former implementation, this version does not manipulate states of other classes. Instead, it allows the other classes to handle the logic, and then simply calls on those classes to provide an answer. + +Question 7: If we decide items are cheaper if bought in bulk, how would this change the code? Which implementation is easier to modify? +Answer: In both implementations, we'd have to alter the initialize method of the CartEntry class to determine the "unit_price" if the quantity given was over a certain amount. However, since the Order class directly manipulates the CartEntry attributes in Implementation A, we would also have to make changes to Order's "total_price" method. Therefore, implementation B is easier to modify since CartEntry is solely responsible for manipulating its own state and logic. + +Question 8: Which implementation better adheres to the single responsibility principle? +Answer: Implementation B. Unlike Implementation A, Implementation B does not store all of the logic in the Order class. Instead it assigns logic to a class depending on which instance variables are modified. + +Question 9: +Answer: Implementation B is more loosely coupled. Implementation A's Order class knows too much about the ShoppingCart and CartEntry classes. It has to dig through class objects to get the data it needs. If the "unit_price" and "entry" names change in CartEntry, it will break the code in Order. From fe025896173813e9e10c249d09f5792647ec3a8b Mon Sep 17 00:00:00 2001 From: Dominique Date: Fri, 27 Sep 2019 22:52:18 -0700 Subject: [PATCH 14/14] Hotel redesign --- lib/date_range.rb | 8 ++- lib/design-activity.md | 19 ++++++- lib/hotel_system.rb | 59 ++++++-------------- lib/reservation.rb | 47 ++++++++++++---- lib/room.rb | 17 ------ test/date_range_test.rb | 28 ++++++---- test/hotel_system_test.rb | 110 +++++++++++++++++--------------------- test/reservation_test.rb | 42 +++++++++------ test/room_test.rb | 38 ------------- test/test_helper.rb | 1 - 10 files changed, 174 insertions(+), 195 deletions(-) delete mode 100644 test/room_test.rb diff --git a/lib/date_range.rb b/lib/date_range.rb index 48f915822..73b3d631c 100644 --- a/lib/date_range.rb +++ b/lib/date_range.rb @@ -2,12 +2,16 @@ module Hotel class Date_Range attr_reader :start_date, :end_date - def initialize(start_date: , end_date:) + def initialize(start_date: , end_date: ) @start_date = start_date @end_date = end_date raise ArgumentError, "start date cannot be after end date" if start_date > end_date end + + def calculate_cost_of_stay + duration_of_stay = end_date - start_date + duration_of_stay.to_i * 200 + end end end - diff --git a/lib/design-activity.md b/lib/design-activity.md index 608a4c3b0..08219adc0 100644 --- a/lib/design-activity.md +++ b/lib/design-activity.md @@ -6,7 +6,7 @@ Answer: Implementation A - The CartEntry class creates a single CartEntry object Answer: Implementation B - The CartEntry class now stores a state AND a behavior. It is instantiated with unit_price and quantity, and holds a method that calculates and returns the item/s subtotal. The ShoppingCart class keeps track of the CartEntry objects, and now holds a method that calls on CartEntry to return the sum price of all its entry objects. The Order class holds a method that returns the total price of all the entries. Now, instead of calculating the subtotal itself, the class simply asks the ShoppingCart class for the sum price of all the entry objects and then multiplies the sales_tax to that subtotal to return the total_price. Question 3: How do the classes relate to each other? It might be helpful to draw a diagram on a whiteboard or piece of paper. -Answer: In both implementations, the Order class has one ShoppingCart object, and the ShoppingCart class has many cart entry objects. +Answer: In both implementations, the Order class has one ShoppingCart object, and the ShoppingCart class has many cart entry objects. Question 4: What data does each class store? How (if at all) does this differ between the two implementations? @@ -28,3 +28,20 @@ Answer: Implementation B. Unlike Implementation A, Implementation B does not sto Question 9: Answer: Implementation B is more loosely coupled. Implementation A's Order class knows too much about the ShoppingCart and CartEntry classes. It has to dig through class objects to get the data it needs. If the "unit_price" and "entry" names change in CartEntry, it will break the code in Order. + + +Hotel Re-design + +The Hotel_System class takes on entirely too much responsibility. I need to delegate more responsibility to the Reservation class. Instead of having Hotel_System so much logic, I will put the majority of it in +Reservation. + +First, I plan to remove the Rooms class and make all references to room just be an integer. This will make it easier to access just room numbers, instead of digging through a room object. + +I plan to let Reservation do the logic to make a reservation, make a list of reserved rooms for a given +date range and also specifically find the room numbers for a given reservation. + +I plan to let Hotel_System asks Reservation for WHAT it needs, not HOW it does it. So in each of my +methods in Hotel_Sytem, the majority of them will call a Reservation method on an instance of Reservation. This way, the classes aren't so tightly coupled. + +I'll also let the Date_Range class calculate the cost of stay, and raise an argument error if a given +start date comes after a given end date. \ No newline at end of file diff --git a/lib/hotel_system.rb b/lib/hotel_system.rb index 8749734c2..2e98b4bb0 100644 --- a/lib/hotel_system.rb +++ b/lib/hotel_system.rb @@ -1,12 +1,13 @@ -require "Pry" + +require "pry" module Hotel class Hotel_System attr_reader :rooms - attr_accessor :reservations + attr_accessor :reservation - def initialize(rooms: Hotel::Room.load_all, reservations: []) - @rooms = rooms - @reservations = reservations + def initialize(rooms: [], reservation: nil ) + @rooms = rooms + @reservation = reservation end def list_rooms @@ -17,60 +18,34 @@ def no_rooms_available raise ArgumentError, "There are no rooms available for that given date range." end - def list_available_room_numbers(start_date, end_date) - available_rooms = list_available_rooms(start_date, end_date) - available_room_numbers = available_rooms.map do |room| - available_rooms[0].room_number - end - available_room_numbers.sample - end - - def make_reservation(start_date, end_date) - - if list_available_room_numbers(start_date, end_date) == nil + def reservation?(room, start_date, end_date) + available_room = list_available_rooms(start_date, end_date) + + if available_room.sample == nil no_rooms_available else - reservations << Hotel::Reservation.new( - room: Hotel::Room.new(room_number: list_available_room_numbers(start_date, end_date), room_cost: 200), - date_range: Hotel::Date_Range.new(start_date: start_date, - end_date: end_date)) + reservation.make_reservation(room, start_date, end_date) end end def find_reservation(start_date, end_date) - found_reservations = [] - reservations.each do |reservation| - - if (reservation.date_range.start_date >= start_date && reservation.date_range.start_date < end_date) || - (reservation.date_range.end_date >= start_date && reservation.date_range.end_date <= end_date) - found_reservations.push(reservation) - end - end - found_reservations + reservation.find_reservation(start_date, end_date) end - def list_available_rooms (start_date, end_date) + def list_available_rooms(start_date, end_date) booked_rooms = list_reserved_room_numbers(start_date, end_date) + available_rooms = [] rooms.each do |room| - if booked_rooms.include?(room.room_number) == false + if booked_rooms.include?(room) == false available_rooms.push(room) end end available_rooms end - def list_reserved_room_numbers(start_date, end_date) - reserved_room_numbers = [] - reservations.each do |reservation| - - if (reservation.date_range.start_date >= start_date && reservation.date_range.start_date < end_date) || - (reservation.date_range.end_date >= start_date && reservation.date_range.end_date <= end_date) - reserved_room_numbers.push(reservation.room.room_number) - end - end - reserved_room_numbers - + def list_reserved_room_numbers(requested_start_date, requested_end_date) + reservation.list_reserved_room_numbers(requested_start_date, requested_end_date) end end end diff --git a/lib/reservation.rb b/lib/reservation.rb index 7eea5d515..aab7de47f 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -1,18 +1,47 @@ -require "pry" + module Hotel class Reservation - attr_reader :room, :date_range, :total_cost + attr_reader :room, :start_date, :end_date + attr_accessor :total_reservations - def initialize(room: , date_range: ) + def initialize(room: , start_date: , end_date: , total_reservations: []) @room = room - @date_range = date_range - @total_cost = calculate_reservation_cost + @start_date = start_date + @end_date = end_date + @total_reservations = total_reservations end - def calculate_reservation_cost - duration_of_stay = date_range.end_date - date_range.start_date - duration_of_stay.to_i * 200 + def make_reservation(room, requested_start_date, requested_end_date) + total_reservations << Reservation.new( + room: room, + start_date: start_date, + end_date: end_date) + end + + def list_reserved_room_numbers(requested_start_date, requested_end_date) + reserved_rooms = [] + + total_reservations.each do |reservation| + if (start_date >= requested_start_date && end_date < requested_end_date) || + (end_date >= requested_start_date && end_date <= requested_end_date) + reserved_rooms.push(room) + end + end + reserved_rooms end - end + + def find_reservation(requested_start_date, requested_end_date) + found_reservations = [] + + total_reservations.each do |reservation| + if (start_date >= requested_start_date && start_date < requested_end_date) || + (end_date >= requested_start_date && end_date <= requested_end_date) + found_reservations.push(reservation) + end + end + found_reservations + end + end end + diff --git a/lib/room.rb b/lib/room.rb index 22c843172..e69de29bb 100644 --- a/lib/room.rb +++ b/lib/room.rb @@ -1,17 +0,0 @@ -module Hotel - class Room - attr_reader :room_number, :room_cost - - def initialize(room_number: , room_cost:) - @room_number = room_number - @room_cost = room_cost - end - - def self.load_all - (1..20).map do |i| - self.new(room_number: i, - room_cost: 200) - end - end - end -end \ No newline at end of file diff --git a/test/date_range_test.rb b/test/date_range_test.rb index 08111f9ac..feabcce00 100644 --- a/test/date_range_test.rb +++ b/test/date_range_test.rb @@ -2,18 +2,18 @@ require "date" describe "Date_Range" do - describe "Date_Range instantiation" do - before do + before do - start_date = Date.parse("2019-02-03") - end_date = Date.parse("2019-02-05") + start_date = Date.parse("2019-02-03") + end_date = Date.parse("2019-02-05") - @date_range = Hotel::Date_Range.new( - start_date: start_date, - end_date: end_date - ) - end - + @date_range = Hotel::Date_Range.new( + start_date: start_date, + end_date: end_date + ) + end + + describe "Date_Range instantiation" do it "is an instance of Date_Range" do expect(@date_range).must_be_kind_of Hotel::Date_Range end @@ -25,4 +25,12 @@ expect{Hotel::Date_Range.new(start_date: start_date, end_date: end_date)}.must_raise ArgumentError end end + + describe "calculate_cost_of_stay" do + it "returns the cost of the duration of stay" do + result = @date_range.calculate_cost_of_stay + + expect(result).must_equal 400 + end + end end \ No newline at end of file diff --git a/test/hotel_system_test.rb b/test/hotel_system_test.rb index 7c44f23df..0c386631c 100644 --- a/test/hotel_system_test.rb +++ b/test/hotel_system_test.rb @@ -1,20 +1,17 @@ require_relative "test_helper" -require "Pry" require "minitest/skip_dsl" describe "Hotel_System" do before do - rooms = Hotel::Room.load_all - reservations = - [ + rooms = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] + reservation = Hotel::Reservation.new( - room: Hotel::Room.new(room_number: 3, room_cost: 200), - date_range: Hotel::Date_Range.new(start_date: Date.parse("2019-02-03"), - end_date: Date.parse("2019-02-05"))) - ] - + room: 3, + start_date: Date.parse("2019-02-03"), + end_date: Date.parse("2019-02-05")) + @hotel_system = Hotel::Hotel_System.new(rooms: rooms, - reservations: reservations) + reservation: reservation) end describe "Hotel_System instantiation" do @@ -22,12 +19,12 @@ expect(@hotel_system).must_be_kind_of Hotel::Hotel_System end - it "stores an instance of room" do - expect(@hotel_system.rooms[0]).must_be_kind_of Hotel::Room + it "stores an array of room numbers" do + expect(@hotel_system.rooms[0]).must_be_kind_of Integer end it "stores an instance of reservation" do - expect(@hotel_system.reservations[0]).must_be_kind_of Hotel::Reservation + expect(@hotel_system.reservation).must_be_kind_of Hotel::Reservation end end @@ -35,71 +32,64 @@ it "returns a list of all the rooms in the hotel" do expect(@hotel_system.list_rooms).must_be_kind_of Array - expect(@hotel_system.list_rooms.length).must_equal 20 - expect(@hotel_system.list_rooms[0]).must_be_kind_of Hotel::Room + expect(@hotel_system.list_rooms[0]).must_be_kind_of Integer end end - describe "make_reservation" do - it "makes a reservation when given a date range" do - start_date = Date.parse("2019-02-08") - end_date = Date.parse("2019-02-12") - - result = @hotel_system.make_reservation(start_date, end_date) - - expect(result).must_be_kind_of Array - expect(result[1]).must_be_kind_of Hotel::Reservation - expect(result[1].date_range.start_date).must_equal start_date - expect(result[1].date_range.end_date).must_equal end_date - end - - it "pushes a new reservation to Hotel_System's reservation instance variable" do - start_date = Date.parse("2019-02-08") - end_date = Date.parse("2019-02-12") + describe "reservation" do + it "calls the Reservation class to make a reservation when given a date range" do - result = @hotel_system.make_reservation(start_date, end_date) - - expect(@hotel_system.reservations.length).must_equal 2 - end - - it "Raises an argument error if there are no rooms available for the given date range" do + room = 3 start_date = Date.parse("2019-02-03") end_date = Date.parse("2019-02-05") - 19.times do - @hotel_system.make_reservation(start_date, end_date) - end - - expect{@hotel_system.make_reservation(start_date, end_date)}.must_raise ArgumentError - end - end - - describe "find_reservation" do - it "finds reservations booked for a specific date" do - start_date = Date.parse("2019-12-11") - end_date = Date.parse("2019-12-17") - - @hotel_system.make_reservation(start_date, end_date) - result = @hotel_system.find_reservation(start_date, end_date) + @reservation = Hotel::Reservation.new( + room: room, + start_date: start_date, + end_date: end_date, + ) - expect(result).must_be_kind_of Array - expect(result[0]).must_be_kind_of Hotel::Reservation - expect(result.length).must_equal 1 - end + @hotel_system.reservation?(3, Date.parse("2019-02-03"), Date.parse("2019-02-05")) + expect(@hotel_system.reservation.total_reservations.length).must_equal 1 + end end describe "list_available_rooms" do it "lists available rooms for a given date" do + room = 3 start_date = Date.parse("2019-02-03") end_date = Date.parse("2019-02-05") - result = @hotel_system.list_available_rooms(start_date, end_date) - - expect(result).must_be_kind_of Array - result.each {|room| expect(room).must_be_kind_of Hotel::Room} + @reservation = Hotel::Reservation.new( + room: room, + start_date: start_date, + end_date: end_date, + ) + + @hotel_system.reservation?(3, Date.parse("2019-02-03"), Date.parse("2019-02-05")) + @hotel_system.list_reserved_room_numbers(Date.parse("2019-02-03"), Date.parse("2019-02-05")) + result = @hotel_system.list_available_rooms(Date.parse("2019-02-03"), Date.parse("2019-02-05")) expect(result.length).must_equal 19 end end + + describe "list_reserved_room_numbers" do + it "returns reserved room numbers" do + room = 3 + start_date = Date.parse("2019-02-03") + end_date = Date.parse("2019-02-05") + + @reservation = Hotel::Reservation.new( + room: room, + start_date: start_date, + end_date: end_date, + ) + + @hotel_system.reservation?(3, Date.parse("2019-02-03"), Date.parse("2019-02-05")) + result = @hotel_system.list_reserved_room_numbers(Date.parse("2019-02-03"), Date.parse("2019-02-05")) + expect(result[0]).must_equal 3 + end + end end diff --git a/test/reservation_test.rb b/test/reservation_test.rb index 42523a0e9..52f8346ee 100644 --- a/test/reservation_test.rb +++ b/test/reservation_test.rb @@ -2,12 +2,15 @@ describe "Reservation" do before do - room = Hotel::Room.new(room_number: 1, room_cost: 200) - date_range = Hotel::Date_Range.new(start_date: Date.parse("2019-02-03"), end_date: Date.parse("2019-02-05")) + + room = 3 + start_date = Date.parse("2019-02-03") + end_date = Date.parse("2019-02-05") @reservation = Hotel::Reservation.new( room: room, - date_range: date_range + start_date: start_date, + end_date: end_date, ) end @@ -15,22 +18,31 @@ it "is instance of Reservation" do expect(@reservation).must_be_kind_of Hotel::Reservation end + end - it "stores an instance of room" do - expect(@reservation.room).must_be_kind_of Hotel::Room - end - - it "stores an instance of date_range" do - expect(@reservation.date_range).must_be_kind_of Hotel::Date_Range + describe "make_reservation" do + it "makes and pushes reservations into the total_reservations array" do + @reservation.make_reservation(3, Date.parse("2019-02-03"), Date.parse("2019-02-05")) + expect(@reservation.total_reservations.length).must_equal 1 end end - - describe "calculate_reservation_cost method" do - it "calculates the cost of a reservation" do - result = @reservation.calculate_reservation_cost - expect(result).must_be_kind_of Integer - expect(result).must_be :>, 0 + describe "list_reserved_room_numbers" do + it "returns an array of reserved room numbers for a given date range" do + @reservation.make_reservation(3, Date.parse("2019-02-03"), Date.parse("2019-02-05")) + + result = @reservation.list_reserved_room_numbers(Date.parse("2019-02-03"), Date.parse("2019-02-05")) + expect(result[0]).must_equal 3 end + end + + describe "find_reservation" do + it "returns an array of all the reservation instances" do + @reservation.make_reservation(4, Date.parse("2019-02-03"), Date.parse("2019-02-05")) + @reservation.make_reservation(5, Date.parse("2019-02-03"), Date.parse("2019-02-05")) + + result = @reservation.find_reservation(Date.parse("2019-02-03"), Date.parse("2019-02-05")) + expect(result.length).must_equal 2 + end end end \ No newline at end of file diff --git a/test/room_test.rb b/test/room_test.rb deleted file mode 100644 index 0ac5c95e0..000000000 --- a/test/room_test.rb +++ /dev/null @@ -1,38 +0,0 @@ -require_relative "test_helper" - -describe "Room" do - before do - #Arrange - room_number = 2 - room_cost = 200 - - #Act - @room = Hotel::Room.new( - room_number: room_number, - room_cost: room_cost - ) - end - - describe "Room instantiation" do - it "is an instance of Room" do - expect(@room).must_be_kind_of Hotel::Room - end - end - - describe "load_all method" do - it "returns array of 20 elements" do - result = Hotel::Room.load_all - - expect(result).must_be_kind_of Array - expect(result.length).must_equal 20 - end - - it "each element in returned array is a room object" do - result = Hotel::Room.load_all - - result.each do |object| - expect(object).must_be_kind_of Hotel::Room - end - end - end -end diff --git a/test/test_helper.rb b/test/test_helper.rb index 265253972..cfbc4e08b 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -12,6 +12,5 @@ # require_relative your lib files here! require_relative '../lib/hotel_system' require_relative '../lib/reservation' -require_relative '../lib/room' require_relative '../lib/date_range'