From aa7f32232cde365165fbcb8077ea4edaab51b71d Mon Sep 17 00:00:00 2001 From: SuelyBarreto Date: Sat, 29 Feb 2020 19:15:31 -0800 Subject: [PATCH 01/37] setting up the enviroment --- lib/hotel.rb | 5 +++++ test/hotel_test.rb | 1 + test/test_helper.rb | 1 + 3 files changed, 7 insertions(+) create mode 100644 lib/hotel.rb create mode 100644 test/hotel_test.rb diff --git a/lib/hotel.rb b/lib/hotel.rb new file mode 100644 index 000000000..91a0e01ab --- /dev/null +++ b/lib/hotel.rb @@ -0,0 +1,5 @@ + +class Hotel + + +end \ No newline at end of file diff --git a/test/hotel_test.rb b/test/hotel_test.rb new file mode 100644 index 000000000..b5cae113d --- /dev/null +++ b/test/hotel_test.rb @@ -0,0 +1 @@ +require_relative "test_helper" diff --git a/test/test_helper.rb b/test/test_helper.rb index c3a7695cf..aa8b56169 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -6,3 +6,4 @@ Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new # require_relative your lib files here! +require_relative "hotel" \ No newline at end of file From 44f1e61e9ef8aa074dc988a2413a217f51cbad26 Mon Sep 17 00:00:00 2001 From: SuelyBarreto Date: Sat, 29 Feb 2020 19:54:41 -0800 Subject: [PATCH 02/37] desing scaffolding --- design-scaffolding-notes.md | 49 ++++++++++++++++++++++ lib/date_range.rb | 20 +++++++++ lib/hotel.rb | 5 --- lib/hotel_controller.rb | 24 +++++++++++ lib/reservation.rb | 11 +++++ test/date_range_test.rb | 78 +++++++++++++++++++++++++++++++++++ test/hotel_controller_test.rb | 50 ++++++++++++++++++++++ test/hotel_test.rb | 1 - test/reservation_test.rb | 12 ++++++ test/test_helper.rb | 9 +++- 10 files changed, 251 insertions(+), 8 deletions(-) create mode 100644 design-scaffolding-notes.md create mode 100644 lib/date_range.rb delete mode 100644 lib/hotel.rb create mode 100644 lib/hotel_controller.rb create mode 100644 lib/reservation.rb create mode 100644 test/date_range_test.rb create mode 100644 test/hotel_controller_test.rb delete mode 100644 test/hotel_test.rb create mode 100644 test/reservation_test.rb diff --git a/design-scaffolding-notes.md b/design-scaffolding-notes.md new file mode 100644 index 000000000..8efb3d487 --- /dev/null +++ b/design-scaffolding-notes.md @@ -0,0 +1,49 @@ +# Hotel Design Scaffolding + +## How to Read This Scaffolding to Create a Second Draft + +This scaffolding is one solution that answers some of the initial questions about how project files can be laid out. + +Use [this view of our branch on GitHub.com](https://github.com/AdaGold/hotel/tree/design-scaffolding) to explore the files that exist on this repo. + - What classes exist? + - Why? What are they named, what do they represent, and what state and behavior do they have? + - What tests exist? + - What parts of this design inspires you, and you want to steal? + - What parts of this design are you unsure about, and need to consider again later? + - What parts of this design do you think you can do without? + +Spend **no more than 1 hour** answering those questions and adjusting your project's first draft design. After one hour, get started; don't forget that a useful skill for the programmer is the ability to get started, and adjust in small ways often. + +### What it includes + +- Three class stubs, `HotelController`, `Reservation` and `DateRange` +- Stubs for public methods of each class from waves 1 and 2, as described in the user stories +- "Interface" tests for each class method that invoke it with the right parameters and verify the return type +- Full test stubs for the `DateRange` class + +### What it does not include + +- Opinions about how classes should interact or data should be stored +- Opinions about whether there should be a `Room` class, or whether it should know about `Reservation`s +- Private helper methods to keep code organized + +Students should feel free to modify any code as they see fit, including changing method signatures, adding new classes and methods, reordering things, not looking at the `DateRange` tests because they want to give it a shot on their own, etc. + +## How to use this code + +Design scaffolding code lives on the `design-scaffolding` branch. + +You can use this code either as inspiration, or as a starting point. If using it as an inspiration, it follows our standard project layout, with product code under `lib/` and tests under `test/`. + +If you choose to use the code on this branch as a starting point, you can either: + +1. Copy and paste each file from this project into your existing `hotel` folder +2. Or start your project anew with the following steps: + + ``` + $ git clone + $ cd hotel + $ git merge origin/design-scaffolding + ``` + + - Note: You can try to merge in the design scaffolding after you've started, but you'll probably end up with merge conflicts. See an instructor if you're not able to resolve them yourself. diff --git a/lib/date_range.rb b/lib/date_range.rb new file mode 100644 index 000000000..da2197f22 --- /dev/null +++ b/lib/date_range.rb @@ -0,0 +1,20 @@ +module Hotel + class DateRange + attr_accessor :start_date, :end_date + + def initialize(start_date, end_date) + end + + def overlap?(other) + return false + end + + def include?(date) + return false + end + + def nights + return 3 + end + end +end diff --git a/lib/hotel.rb b/lib/hotel.rb deleted file mode 100644 index 91a0e01ab..000000000 --- a/lib/hotel.rb +++ /dev/null @@ -1,5 +0,0 @@ - -class Hotel - - -end \ No newline at end of file diff --git a/lib/hotel_controller.rb b/lib/hotel_controller.rb new file mode 100644 index 000000000..accca6de4 --- /dev/null +++ b/lib/hotel_controller.rb @@ -0,0 +1,24 @@ +module Hotel + class HotelController + # Wave 1 + def rooms + # You might want to replace this method with an attr_reader + return [] + end + + def reserve_room(start_date, end_date) + # start_date and end_date should be instances of class Date + return Reservation.new(start_date, end_date, nil) + end + + def reservations(date) + return [] + end + + # Wave 2 + def available_rooms(start_date, end_date) + # start_date and end_date should be instances of class Date + return [] + end + end +end diff --git a/lib/reservation.rb b/lib/reservation.rb new file mode 100644 index 000000000..7587837e7 --- /dev/null +++ b/lib/reservation.rb @@ -0,0 +1,11 @@ +module Hotel + class Reservation + # Feel free to change this method signature as needed. Make sure to update the tests! + def initialize(start_date, end_date, room) + end + + def cost + return 3 + end + end +end diff --git a/test/date_range_test.rb b/test/date_range_test.rb new file mode 100644 index 000000000..70bd65115 --- /dev/null +++ b/test/date_range_test.rb @@ -0,0 +1,78 @@ +require_relative "test_helper" + +describe Hotel::DateRange do + describe "consructor" do + it "Can be initialized with two dates" do + start_date = Date.new(2017, 01, 01) + end_date = start_date + 3 + + range = Hotel::DateRange.new(start_date, end_date) + + expect(range.start_date).must_equal start_date + expect(range.end_date).must_equal end_date + end + + xit "is an an error for negative-lenght ranges" do + end + + xit "is an error to create a 0-length range" do + end + end + + describe "overlap?" do + before do + start_date = Date.new(2017, 01, 01) + end_date = start_date + 3 + + @range = Hotel::DateRange.new(start_date, end_date) + end + + it "returns true for the same range" do + start_date = @range.start_date + end_date = @range.end_date + test_range = Hotel::DateRange.new(start_date, end_date) + + expect(@range.overlap?(test_range)).must_equal true + end + + xit "returns true for a contained range" do + end + + xit "returns true for a range that overlaps in front" do + end + + xit "returns true for a range that overlaps in the back" do + end + + xit "returns true for a containing range" do + end + + xit "returns false for a range starting on the end_date date" do + end + + xit "returns false for a range ending on the start_date date" do + end + + xit "returns false for a range completely before" do + end + + xit "returns false for a date completely after" do + end + end + + xdescribe "include?" do + it "reutrns false if the date is clearly out" do + end + + it "returns true for dates in the range" do + end + + it "returns false for the end_date date" do + end + end + + xdescribe "nights" do + it "returns the correct number of nights" do + end + end +end diff --git a/test/hotel_controller_test.rb b/test/hotel_controller_test.rb new file mode 100644 index 000000000..bd50dece0 --- /dev/null +++ b/test/hotel_controller_test.rb @@ -0,0 +1,50 @@ +require_relative "test_helper" + +describe Hotel::HotelController do + before do + @hotel_controller = Hotel::HotelController.new + @date = Date.parse("2020-08-04") + end + describe "wave 1" do + describe "rooms" do + it "returns a list" do + rooms = @hotel_controller.rooms + expect(rooms).must_be_kind_of Array + end + end + describe "reserve_room" do + it "takes two Date objects and returns a Reservation" do + start_date = @date + end_date = start_date + 3 + + reservation = @hotel_controller.reserve_room(start_date, end_date) + + expect(reservation).must_be_kind_of Hotel::Reservation + end + end + + describe "reservations" do + it "takes a Date and returns a list of Reservations" do + reservation_list = @hotel_controller.reservations(@date) + + expect(reservation_list).must_be_kind_of Array + reservation_list.each do |res| + res.must_be_kind_of Reservation + end + end + end + end + + describe "wave 2" do + describe "available_rooms" do + it "takes two dates and returns a list" do + start_date = @date + end_date = start_date + 3 + + room_list = @hotel_controller.available_rooms(start_date, end_date) + + expect(room_list).must_be_kind_of Array + end + end + end +end diff --git a/test/hotel_test.rb b/test/hotel_test.rb deleted file mode 100644 index b5cae113d..000000000 --- a/test/hotel_test.rb +++ /dev/null @@ -1 +0,0 @@ -require_relative "test_helper" diff --git a/test/reservation_test.rb b/test/reservation_test.rb new file mode 100644 index 000000000..ec4204774 --- /dev/null +++ b/test/reservation_test.rb @@ -0,0 +1,12 @@ +require_relative "test_helper" + +describe Hotel::Reservation do + describe "cost" do + it "returns a number" do + start_date = Date.new(2017, 01, 01) + end_date = start_date + 3 + reservation = Hotel::Reservation.new(start_date, end_date, nil) + expect(reservation.cost).must_be_kind_of Numeric + end + end +end diff --git a/test/test_helper.rb b/test/test_helper.rb index aa8b56169..cbf8c253e 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -2,8 +2,13 @@ require "minitest" require "minitest/autorun" require "minitest/reporters" +require "minitest/skip_dsl" + +require "date" Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new -# require_relative your lib files here! -require_relative "hotel" \ No newline at end of file +# Require_relative your lib files here! +require_relative "../lib/hotel_controller.rb" +require_relative "../lib/reservation.rb" +require_relative "../lib/date_range.rb" From 49edc0590ba2ddcd503771b78444648f9b1ba0f4 Mon Sep 17 00:00:00 2001 From: SuelyBarreto Date: Sun, 1 Mar 2020 07:36:25 -0800 Subject: [PATCH 03/37] guard and minicov installed --- test/test_helper.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/test_helper.rb b/test/test_helper.rb index cbf8c253e..054b30230 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,4 +1,5 @@ -# Add simplecov +require "simplecov" + require "minitest" require "minitest/autorun" require "minitest/reporters" @@ -6,6 +7,7 @@ require "date" +SimpleCov.start Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new # Require_relative your lib files here! From ed52713e851e44a0fed8e3022080d8c9f7318cda Mon Sep 17 00:00:00 2001 From: SuelyBarreto Date: Sun, 1 Mar 2020 12:43:27 -0800 Subject: [PATCH 04/37] minor fixes after first look at tests and classes --- lib/reservation.rb | 1 + test/date_range_test.rb | 17 ++++++++++++----- test/hotel_controller_test.rb | 13 ++++++------- test/reservation_test.rb | 4 ++-- 4 files changed, 21 insertions(+), 14 deletions(-) diff --git a/lib/reservation.rb b/lib/reservation.rb index 7587837e7..16526a4d7 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -2,6 +2,7 @@ module Hotel class Reservation # Feel free to change this method signature as needed. Make sure to update the tests! def initialize(start_date, end_date, room) + end def cost diff --git a/test/date_range_test.rb b/test/date_range_test.rb index 70bd65115..0f639983b 100644 --- a/test/date_range_test.rb +++ b/test/date_range_test.rb @@ -1,6 +1,6 @@ require_relative "test_helper" -describe Hotel::DateRange do +describe "Hotel::DateRange" do describe "consructor" do it "Can be initialized with two dates" do start_date = Date.new(2017, 01, 01) @@ -12,7 +12,7 @@ expect(range.end_date).must_equal end_date end - xit "is an an error for negative-lenght ranges" do + xit "is an error for negative-length ranges" do end xit "is an error to create a 0-length range" do @@ -22,14 +22,14 @@ describe "overlap?" do before do start_date = Date.new(2017, 01, 01) - end_date = start_date + 3 + end_date = start_date + 3 @range = Hotel::DateRange.new(start_date, end_date) end it "returns true for the same range" do start_date = @range.start_date - end_date = @range.end_date + end_date = @range.end_date test_range = Hotel::DateRange.new(start_date, end_date) expect(@range.overlap?(test_range)).must_equal true @@ -61,14 +61,21 @@ end xdescribe "include?" do - it "reutrns false if the date is clearly out" do + it "returns false if the date is clearly before" do + end + + it "returns false if the date is clearly after" do end it "returns true for dates in the range" do end + it "returns true for the start_date date" do + end + it "returns false for the end_date date" do end + end xdescribe "nights" do diff --git a/test/hotel_controller_test.rb b/test/hotel_controller_test.rb index bd50dece0..0601a46be 100644 --- a/test/hotel_controller_test.rb +++ b/test/hotel_controller_test.rb @@ -1,6 +1,6 @@ require_relative "test_helper" -describe Hotel::HotelController do +describe "Hotel::HotelController" do before do @hotel_controller = Hotel::HotelController.new @date = Date.parse("2020-08-04") @@ -8,14 +8,13 @@ describe "wave 1" do describe "rooms" do it "returns a list" do - rooms = @hotel_controller.rooms - expect(rooms).must_be_kind_of Array + expect(@hotel_controller.rooms).must_be_kind_of Array end end describe "reserve_room" do it "takes two Date objects and returns a Reservation" do start_date = @date - end_date = start_date + 3 + end_date = start_date + 3 reservation = @hotel_controller.reserve_room(start_date, end_date) @@ -29,7 +28,7 @@ expect(reservation_list).must_be_kind_of Array reservation_list.each do |res| - res.must_be_kind_of Reservation + res.must_be_kind_of Hotel::Reservation # included module name end end end @@ -39,9 +38,9 @@ describe "available_rooms" do it "takes two dates and returns a list" do start_date = @date - end_date = start_date + 3 + end_date = start_date + 3 - room_list = @hotel_controller.available_rooms(start_date, end_date) + room_list = @hotel_controller.available_rooms(start_date, end_date) # DateRange? expect(room_list).must_be_kind_of Array end diff --git a/test/reservation_test.rb b/test/reservation_test.rb index ec4204774..e2f7d6713 100644 --- a/test/reservation_test.rb +++ b/test/reservation_test.rb @@ -1,10 +1,10 @@ require_relative "test_helper" -describe Hotel::Reservation do +describe "Hotel::Reservation" do describe "cost" do it "returns a number" do start_date = Date.new(2017, 01, 01) - end_date = start_date + 3 + end_date = start_date + 3 reservation = Hotel::Reservation.new(start_date, end_date, nil) expect(reservation.cost).must_be_kind_of Numeric end From 15f25f7b10a22feb3bf76093649863f62e3cf3d2 Mon Sep 17 00:00:00 2001 From: SuelyBarreto Date: Sun, 1 Mar 2020 15:41:40 -0800 Subject: [PATCH 05/37] completed test and class for date range --- lib/date_range.rb | 24 +++++++- test/date_range_test.rb | 120 ++++++++++++++++++++++++++++++++-------- 2 files changed, 118 insertions(+), 26 deletions(-) diff --git a/lib/date_range.rb b/lib/date_range.rb index da2197f22..d1c4183cd 100644 --- a/lib/date_range.rb +++ b/lib/date_range.rb @@ -3,18 +3,36 @@ class DateRange attr_accessor :start_date, :end_date def initialize(start_date, end_date) + unless start_date.class == Date && end_date.class == Date + raise ArgumentError, "Parameters must be of class Date" + end + if start_date >= end_date + raise ArgumentError, "Start date must be before end date" + end + @start_date = start_date + @end_date = end_date end def overlap?(other) - return false + unless other.class == Hotel::DateRange + raise ArgumentError, "Parameters must be of class Hotel::DateRange" + end + + return other.start_date < @end_date && other.end_date > @start_date + + # return !(other.start_date >= @end_date || other.end_date <= @start_date) end def include?(date) - return false + unless date.class == Date + raise ArgumentError, "Parameters must be of class Date" + end + + return date >= @start_date && date < @end_date end def nights - return 3 + return (@end_date - @start_date).to_i end end end diff --git a/test/date_range_test.rb b/test/date_range_test.rb index 0f639983b..71b95dccf 100644 --- a/test/date_range_test.rb +++ b/test/date_range_test.rb @@ -1,85 +1,159 @@ require_relative "test_helper" describe "Hotel::DateRange" do - describe "consructor" do + + describe "constructor" do + it "Can be initialized with two dates" do start_date = Date.new(2017, 01, 01) - end_date = start_date + 3 - + end_date = start_date + 3 range = Hotel::DateRange.new(start_date, end_date) expect(range.start_date).must_equal start_date expect(range.end_date).must_equal end_date end - xit "is an error for negative-length ranges" do + it "is an error if parameters are not dates" do + start_date = "2017/01/01" + end_date = "2017/01/04" + + expect{ range = Hotel::DateRange.new(start_date, end_date) }.must_raise ArgumentError end - xit "is an error to create a 0-length range" do + it "is an error for negative-length ranges" do + start_date = Date.new(2017, 01, 01) + end_date = start_date - 3 + + expect{ range = Hotel::DateRange.new(start_date, end_date) }.must_raise ArgumentError + end + + it "is an error to create a 0-length range" do + start_date = Date.new(2017, 01, 01) + end_date = start_date + + expect{ range = Hotel::DateRange.new(start_date, end_date) }.must_raise ArgumentError end + end describe "overlap?" do + before do - start_date = Date.new(2017, 01, 01) - end_date = start_date + 3 - @range = Hotel::DateRange.new(start_date, end_date) + def simple_range(start_day, end_day) + return Hotel::DateRange.new(Date.new(2017, 01, start_day), Date.new(2017, 01, end_day)) + end + end - it "returns true for the same range" do - start_date = @range.start_date - end_date = @range.end_date - test_range = Hotel::DateRange.new(start_date, end_date) + it "raises an ArgumentError if parameter is not a range" do + range1 = simple_range(01, 05) + range2 = Date.new(2017, 01, 01) + expect{ range1.overlap?(range2) }.must_raise ArgumentError + end - expect(@range.overlap?(test_range)).must_equal true + it "returns true for the same range" do + range1 = simple_range(01, 05) + range2 = simple_range(01, 05) + expect(range1.overlap?(range2)).must_equal true end - xit "returns true for a contained range" do + it "returns true for a contained range" do + range1 = simple_range(05, 10) + range2 = simple_range(01, 15) + expect(range1.overlap?(range2)).must_equal true end - xit "returns true for a range that overlaps in front" do + it "returns true for a range that overlaps in front" do + range1 = simple_range(05, 15) + range2 = simple_range(01, 10) + expect(range1.overlap?(range2)).must_equal true end - xit "returns true for a range that overlaps in the back" do + it "returns true for a range that overlaps in the back" do + range1 = simple_range(01, 10) + range2 = simple_range(05, 15) + expect(range1.overlap?(range2)).must_equal true end - xit "returns true for a containing range" do + it "returns true for a containing range" do + range1 = simple_range(01, 15) + range2 = simple_range(05, 10) + expect(range1.overlap?(range2)).must_equal true end - xit "returns false for a range starting on the end_date date" do + it "returns false for a range starting on the end_date date" do + range1 = simple_range(01, 05) + range2 = simple_range(05, 10) + expect(range1.overlap?(range2)).must_equal false end - xit "returns false for a range ending on the start_date date" do + it "returns false for a range ending on the start_date date" do + range1 = simple_range(05, 10) + range2 = simple_range(01, 05) + expect(range1.overlap?(range2)).must_equal false end - xit "returns false for a range completely before" do + it "returns false for a range completely before" do + range1 = simple_range(01, 05) + range2 = simple_range(10, 15) + expect(range1.overlap?(range2)).must_equal false end - xit "returns false for a date completely after" do + it "returns false for a date completely after" do + range1 = simple_range(10, 15) + range2 = simple_range(01, 05) + expect(range1.overlap?(range2)).must_equal false end + end - xdescribe "include?" do + describe "include?" do + + before do + start_date = Date.new(2018, 01, 01) + end_date = Date.new(2018, 01, 10) + + @range = Hotel::DateRange.new(start_date, end_date) + end + + it "raises an ArgumentError if parameter is not a date" do + expect{ @range.include?("2018/01/01") }.must_raise ArgumentError + end + it "returns false if the date is clearly before" do + expect(@range.include?(Date.new(2017, 01, 01))).must_equal false end it "returns false if the date is clearly after" do + expect(@range.include?(Date.new(2019, 01, 01))).must_equal false end it "returns true for dates in the range" do + expect(@range.include?(Date.new(2018, 01, 05))).must_equal true end it "returns true for the start_date date" do + expect(@range.include?(Date.new(2018, 01, 01))).must_equal true end it "returns false for the end_date date" do + expect(@range.include?(Date.new(2018, 01, 10))).must_equal false end end - xdescribe "nights" do + describe "nights" do + it "returns the correct number of nights" do + start_date = Date.new(2017, 01, 01) + end_date = start_date + 5 + range = Hotel::DateRange.new(start_date, end_date) + + expect(range.nights).must_be_kind_of Integer + expect(range.nights).must_equal 5 end + end + end From 235cb9a8ead5094ba3416e35e7a1e766cb950c3c Mon Sep 17 00:00:00 2001 From: SuelyBarreto Date: Sun, 1 Mar 2020 18:14:27 -0800 Subject: [PATCH 06/37] Add coverage directory to .gitignore file --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 5e1422c9c..c0ac3dc53 100644 --- a/.gitignore +++ b/.gitignore @@ -48,3 +48,4 @@ build-iPhoneSimulator/ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this: .rvmrc +coverage From 94f6957140672bde299082266ade48a6ff6292e0 Mon Sep 17 00:00:00 2001 From: SuelyBarreto Date: Mon, 2 Mar 2020 15:07:49 -0800 Subject: [PATCH 07/37] added range must be kind of HotelDateRange to the describe constructor --- test/date_range_test.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/date_range_test.rb b/test/date_range_test.rb index 71b95dccf..e53ac8c44 100644 --- a/test/date_range_test.rb +++ b/test/date_range_test.rb @@ -3,14 +3,14 @@ describe "Hotel::DateRange" do describe "constructor" do - - it "Can be initialized with two dates" do + it "can be initialized with two dates" do start_date = Date.new(2017, 01, 01) end_date = start_date + 3 - range = Hotel::DateRange.new(start_date, end_date) + range = Hotel::DateRange.new(start_date, end_date) expect(range.start_date).must_equal start_date expect(range.end_date).must_equal end_date + expect(range).must_be_kind_of Hotel::DateRange end it "is an error if parameters are not dates" do From c0f50ca4adcd1262e5c35bf413af944f40492eed Mon Sep 17 00:00:00 2001 From: SuelyBarreto Date: Mon, 2 Mar 2020 17:56:41 -0800 Subject: [PATCH 08/37] date_range instance_of? --- lib/date_range.rb | 9 ++++----- test/date_range_test.rb | 6 ------ test/hotel_controller_test.rb | 2 ++ 3 files changed, 6 insertions(+), 11 deletions(-) diff --git a/lib/date_range.rb b/lib/date_range.rb index d1c4183cd..fc21298d4 100644 --- a/lib/date_range.rb +++ b/lib/date_range.rb @@ -3,7 +3,7 @@ class DateRange attr_accessor :start_date, :end_date def initialize(start_date, end_date) - unless start_date.class == Date && end_date.class == Date + unless start_date.instance_of?(Date) && end_date.instance_of?(Date) raise ArgumentError, "Parameters must be of class Date" end if start_date >= end_date @@ -14,17 +14,15 @@ def initialize(start_date, end_date) end def overlap?(other) - unless other.class == Hotel::DateRange + unless other.instance_of?(Hotel::DateRange) raise ArgumentError, "Parameters must be of class Hotel::DateRange" end return other.start_date < @end_date && other.end_date > @start_date - - # return !(other.start_date >= @end_date || other.end_date <= @start_date) end def include?(date) - unless date.class == Date + unless date.instance_of?(Date) raise ArgumentError, "Parameters must be of class Date" end @@ -34,5 +32,6 @@ def include?(date) def nights return (@end_date - @start_date).to_i end + end end diff --git a/test/date_range_test.rb b/test/date_range_test.rb index e53ac8c44..fd4776cc0 100644 --- a/test/date_range_test.rb +++ b/test/date_range_test.rb @@ -37,7 +37,6 @@ end describe "overlap?" do - before do def simple_range(start_day, end_day) @@ -105,11 +104,9 @@ def simple_range(start_day, end_day) range2 = simple_range(01, 05) expect(range1.overlap?(range2)).must_equal false end - end describe "include?" do - before do start_date = Date.new(2018, 01, 01) end_date = Date.new(2018, 01, 10) @@ -140,11 +137,9 @@ def simple_range(start_day, end_day) it "returns false for the end_date date" do expect(@range.include?(Date.new(2018, 01, 10))).must_equal false end - end describe "nights" do - it "returns the correct number of nights" do start_date = Date.new(2017, 01, 01) end_date = start_date + 5 @@ -153,7 +148,6 @@ def simple_range(start_day, end_day) expect(range.nights).must_be_kind_of Integer expect(range.nights).must_equal 5 end - end end diff --git a/test/hotel_controller_test.rb b/test/hotel_controller_test.rb index 0601a46be..ab36c06b9 100644 --- a/test/hotel_controller_test.rb +++ b/test/hotel_controller_test.rb @@ -5,12 +5,14 @@ @hotel_controller = Hotel::HotelController.new @date = Date.parse("2020-08-04") end + describe "wave 1" do describe "rooms" do it "returns a list" do expect(@hotel_controller.rooms).must_be_kind_of Array end end + describe "reserve_room" do it "takes two Date objects and returns a Reservation" do start_date = @date From d6a757ef81293d9f0108909685fd44f1e7a9830e Mon Sep 17 00:00:00 2001 From: SuelyBarreto Date: Mon, 2 Mar 2020 19:05:21 -0800 Subject: [PATCH 09/37] reservation_test and reservation for wave 1 --- lib/reservation.rb | 10 +++++--- test/hotel_controller_test.rb | 8 +++++++ test/reservation_test.rb | 44 ++++++++++++++++++++++++++++++++--- 3 files changed, 56 insertions(+), 6 deletions(-) diff --git a/lib/reservation.rb b/lib/reservation.rb index 16526a4d7..b034300ab 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -1,12 +1,16 @@ module Hotel class Reservation - # Feel free to change this method signature as needed. Make sure to update the tests! + attr_reader :range, :room + ROOM_COST = 200 + def initialize(start_date, end_date, room) - + @range = Hotel::DateRange.new(start_date, end_date) + @room = room end + # Reservation - Every room is identical and a room always costs $200/night def cost - return 3 + return ROOM_COST * @range.nights end end end diff --git a/test/hotel_controller_test.rb b/test/hotel_controller_test.rb index ab36c06b9..9bbf0b66d 100644 --- a/test/hotel_controller_test.rb +++ b/test/hotel_controller_test.rb @@ -49,3 +49,11 @@ end end end + + +# HotelController - access the list of all of the rooms in the hotel +# HotelController - access the list of reservations for a specified room and a given date range +# HotelController - access the list of reservations for a specific date, so that I can track reservations by date +# HotelController - The hotel has 20 rooms, and they are numbered 1 through 20 +# HotelController - When reserving a room, the user provides only the start and end dates - the library should determine which room to use for the reservation +# HotelController - Check whether reservations conflict with each other (this will come in wave 2!) diff --git a/test/reservation_test.rb b/test/reservation_test.rb index e2f7d6713..6f1115d5c 100644 --- a/test/reservation_test.rb +++ b/test/reservation_test.rb @@ -1,12 +1,50 @@ require_relative "test_helper" describe "Hotel::Reservation" do + describe "constructor" do + it "can be initialized with two dates and a room" do + start_date = Date.new(2017, 01, 01) + end_date = start_date + 3 + room = 10 + reservation = Hotel::Reservation.new(start_date, end_date, room) + + expect(reservation).must_be_kind_of Hotel::Reservation + expect(reservation.range.start_date).must_equal start_date + expect(reservation.range.end_date).must_equal end_date + expect(reservation.room).must_equal room + end + + it "raises an ArgumentError if invalid dates provided" do + start_date = Date.new(2017, 01, 01) + end_date1 = start_date - 1 + end_date2 = "2017/01/04" + room = 10 + + # start date can not be after end date + expect{ reservation = Hotel::Reservation.new(start_date, end_date1, room) }.must_raise ArgumentError + # date can not be a string + expect{ reservation = Hotel::Reservation.new(start_date, end_date2, room) }.must_raise ArgumentError + # start date can not be the same as end date + expect{ reservation = Hotel::Reservation.new(start_date, start_date, room) }.must_raise ArgumentError + end + end + describe "cost" do - it "returns a number" do + before do start_date = Date.new(2017, 01, 01) end_date = start_date + 3 - reservation = Hotel::Reservation.new(start_date, end_date, nil) - expect(reservation.cost).must_be_kind_of Numeric + room = 10 + @reservation = Hotel::Reservation.new(start_date, end_date, room) + end + + it "returns a number" do + expect(@reservation.cost).must_be_kind_of Numeric + end + + it "calculates the cost" do + # Reservation - Every room is identical and a room always costs $200/night + expect(@reservation.cost).must_equal 600 end + end end From 76cd3acf73ff25c191911d31db7df84d483ce9ba Mon Sep 17 00:00:00 2001 From: SuelyBarreto Date: Mon, 2 Mar 2020 20:32:47 -0800 Subject: [PATCH 10/37] starting hotel_controller in wave 1 --- lib/hotel_controller.rb | 21 ++++++++++++++------- test/hotel_controller_test.rb | 27 ++++++++++++++++----------- 2 files changed, 30 insertions(+), 18 deletions(-) diff --git a/lib/hotel_controller.rb b/lib/hotel_controller.rb index accca6de4..d86b1f554 100644 --- a/lib/hotel_controller.rb +++ b/lib/hotel_controller.rb @@ -1,18 +1,25 @@ module Hotel class HotelController - # Wave 1 - def rooms - # You might want to replace this method with an attr_reader - return [] + attr_reader :rooms, :reservation_list + + def initialize + @rooms = (1..20).to_a + @reservation_list = [] end def reserve_room(start_date, end_date) - # start_date and end_date should be instances of class Date - return Reservation.new(start_date, end_date, nil) + room = available_rooms(start_date, end_date).first + reservation = Hotel::Reservation.new(start_date, end_date, room) + @reservation_list << reservation + return reservation end + # loop thru reservation list, for each reservation, + # use include? to check if the range include the date in question def reservations(date) - return [] + return @reservation_list.select do |reservation| + reservation.range.include?(date) + end end # Wave 2 diff --git a/test/hotel_controller_test.rb b/test/hotel_controller_test.rb index 9bbf0b66d..75e06093d 100644 --- a/test/hotel_controller_test.rb +++ b/test/hotel_controller_test.rb @@ -7,35 +7,46 @@ end describe "wave 1" do + describe "rooms" do it "returns a list" do expect(@hotel_controller.rooms).must_be_kind_of Array end + + it "return 20 rooms" do + expect(@hotel_controller.rooms.size).must_equal 20 + end + + it "return 20 rooms numbered 1 through 20" do + (1..20).each do |room| + expect(@hotel_controller.rooms.include?(room)).must_equal true + end + end end - + describe "reserve_room" do it "takes two Date objects and returns a Reservation" do start_date = @date end_date = start_date + 3 - reservation = @hotel_controller.reserve_room(start_date, end_date) expect(reservation).must_be_kind_of Hotel::Reservation end end + # access the list of reservations for a specific date, so that I can track reservations by date describe "reservations" do it "takes a Date and returns a list of Reservations" do reservation_list = @hotel_controller.reservations(@date) expect(reservation_list).must_be_kind_of Array reservation_list.each do |res| - res.must_be_kind_of Hotel::Reservation # included module name + res.must_be_kind_of Hotel::Reservation end end end end - + # check whether reservations conflict with each other (this will come in wave 2!) describe "wave 2" do describe "available_rooms" do it "takes two dates and returns a list" do @@ -50,10 +61,4 @@ end end - -# HotelController - access the list of all of the rooms in the hotel -# HotelController - access the list of reservations for a specified room and a given date range -# HotelController - access the list of reservations for a specific date, so that I can track reservations by date -# HotelController - The hotel has 20 rooms, and they are numbered 1 through 20 -# HotelController - When reserving a room, the user provides only the start and end dates - the library should determine which room to use for the reservation -# HotelController - Check whether reservations conflict with each other (this will come in wave 2!) + # access the list of reservations for a specified room and a given date range From bc4ae6dbc9596e431215581d3413dbaf655952d9 Mon Sep 17 00:00:00 2001 From: SuelyBarreto Date: Tue, 3 Mar 2020 06:06:38 -0800 Subject: [PATCH 11/37] added comments and user stories for guidance --- lib/hotel_controller.rb | 3 +-- lib/reservation.rb | 2 +- test/date_range_test.rb | 17 +++++++++++++++++ test/hotel_controller_test.rb | 36 ++++++++++++++++++++++++++++++----- test/reservation_test.rb | 17 +++++++++++++++++ 5 files changed, 67 insertions(+), 8 deletions(-) diff --git a/lib/hotel_controller.rb b/lib/hotel_controller.rb index d86b1f554..ce1e853a7 100644 --- a/lib/hotel_controller.rb +++ b/lib/hotel_controller.rb @@ -14,8 +14,7 @@ def reserve_room(start_date, end_date) return reservation end - # loop thru reservation list, for each reservation, - # use include? to check if the range include the date in question + # on reservation list, for each reservation, check if the range include the date in question def reservations(date) return @reservation_list.select do |reservation| reservation.range.include?(date) diff --git a/lib/reservation.rb b/lib/reservation.rb index b034300ab..6ba5d8ee3 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -8,7 +8,7 @@ def initialize(start_date, end_date, room) @room = room end - # Reservation - Every room is identical and a room always costs $200/night + # Every room is identical and costs $200/night def cost return ROOM_COST * @range.nights end diff --git a/test/date_range_test.rb b/test/date_range_test.rb index fd4776cc0..e40a9df64 100644 --- a/test/date_range_test.rb +++ b/test/date_range_test.rb @@ -151,3 +151,20 @@ def simple_range(start_day, end_day) end end + +# User Stories - As a user of the hotel system...I can/want to...so that I can/can't + +# HotelController - access the list of all of the rooms in the hotel "Who has the list of all the rooms in the hotel?" +# HotelController - access the list of reservations for a specified room and a given date range "Who has access to that?" +# HotelController - access the list of reservations for a specific date, so that I can track reservations by date +# Reservation - get the total cost for a given reservation +# DateRange - want exception raised when an invalid date range is provided, so I can't make a reservation for an invalid date range + +# Details + +# HotelController - the hotel has 20 rooms, numbered 1 through 20 (array from 1 to 20) +# Reservation - every room is identical and costs $200/night (constant cost $200) +# DateRange - last day is the checkout day, guest shouldn't be charged for that day/night (end_day doesn't count) +# HotelController - given only start and end dates, determine which room to use for the reservation + +# For this wave, you don't need to check whether reservations conflict with each other (this will come in wave 2!) \ No newline at end of file diff --git a/test/hotel_controller_test.rb b/test/hotel_controller_test.rb index 75e06093d..bde2ae979 100644 --- a/test/hotel_controller_test.rb +++ b/test/hotel_controller_test.rb @@ -37,15 +37,23 @@ # access the list of reservations for a specific date, so that I can track reservations by date describe "reservations" do it "takes a Date and returns a list of Reservations" do - reservation_list = @hotel_controller.reservations(@date) + start_date = @date + end_date = start_date + 3 + reservation1 = @hotel_controller.reserve_room(start_date, end_date) + reservation2 = @hotel_controller.reserve_room(start_date, end_date) + + reservations_ondate = @hotel_controller.reservations(@date) - expect(reservation_list).must_be_kind_of Array - reservation_list.each do |res| - res.must_be_kind_of Hotel::Reservation + expect(reservations_ondate).must_be_kind_of Array + expect(reservations_ondate.size).must_equal 2 + + reservations_ondate.each do |reservation| + expect(reservation).must_be_kind_of Hotel::Reservation end end end end + # check whether reservations conflict with each other (this will come in wave 2!) describe "wave 2" do describe "available_rooms" do @@ -60,5 +68,23 @@ end end end +# access the list of reservations for a specified room and a given date range + + +# User Stories - As a user of the hotel system...I can/want to...so that I can/can't + +# HotelController - access the list of all of the rooms in the hotel "Who has the list of all the rooms in the hotel?" +# HotelController - access the list of reservations for a specified room and a given date range "Who has access to that?" +# HotelController - access the list of reservations for a specific date, so that I can track reservations by date +# Reservation - get the total cost for a given reservation +# DateRange - want exception raised when an invalid date range is provided, so I can't make a reservation for an invalid date range + +# Details + +# HotelController - the hotel has 20 rooms, numbered 1 through 20 (array from 1 to 20) +# Reservation - every room is identical and costs $200/night (constant cost $200) +# DateRange - last day is the checkout day, guest shouldn't be charged for that day/night (end_day doesn't count) +# HotelController - given only start and end dates, determine which room to use for the reservation + +# For this wave, you don't need to check whether reservations conflict with each other (this will come in wave 2!) - # access the list of reservations for a specified room and a given date range diff --git a/test/reservation_test.rb b/test/reservation_test.rb index 6f1115d5c..8e12bf990 100644 --- a/test/reservation_test.rb +++ b/test/reservation_test.rb @@ -48,3 +48,20 @@ end end + +# User Stories - As a user of the hotel system...I can/want to...so that I can/can't + +# HotelController - access the list of all of the rooms in the hotel "Who has the list of all the rooms in the hotel?" +# HotelController - access the list of reservations for a specified room and a given date range "Who has access to that?" +# HotelController - access the list of reservations for a specific date, so that I can track reservations by date +# Reservation - get the total cost for a given reservation +# DateRange - want exception raised when an invalid date range is provided, so I can't make a reservation for an invalid date range + +# Details + +# HotelController - the hotel has 20 rooms, numbered 1 through 20 (array from 1 to 20) +# Reservation - every room is identical and costs $200/night (constant cost $200) +# DateRange - last day is the checkout day, guest shouldn't be charged for that day/night (end_day doesn't count) +# HotelController - given only start and end dates, determine which room to use for the reservation + +# For this wave, you don't need to check whether reservations conflict with each other (this will come in wave 2!) \ No newline at end of file From dd3bf9232ac59b9b9d7fbe3cfe6b8c12c1aed5c2 Mon Sep 17 00:00:00 2001 From: SuelyBarreto Date: Tue, 3 Mar 2020 12:49:48 -0800 Subject: [PATCH 12/37] available_rooms and room_available --- lib/date_range.rb | 4 ++++ lib/hotel_controller.rb | 29 ++++++++++++++++++++++++----- test/date_range_test.rb | 2 -- test/hotel_controller_test.rb | 3 +-- 4 files changed, 29 insertions(+), 9 deletions(-) diff --git a/lib/date_range.rb b/lib/date_range.rb index fc21298d4..c8bb60eac 100644 --- a/lib/date_range.rb +++ b/lib/date_range.rb @@ -2,6 +2,7 @@ module Hotel class DateRange attr_accessor :start_date, :end_date + # Constructor def initialize(start_date, end_date) unless start_date.instance_of?(Date) && end_date.instance_of?(Date) raise ArgumentError, "Parameters must be of class Date" @@ -13,6 +14,7 @@ def initialize(start_date, end_date) @end_date = end_date end + # Check Range Overlap def overlap?(other) unless other.instance_of?(Hotel::DateRange) raise ArgumentError, "Parameters must be of class Hotel::DateRange" @@ -21,6 +23,7 @@ def overlap?(other) return other.start_date < @end_date && other.end_date > @start_date end + # Check Date def include?(date) unless date.instance_of?(Date) raise ArgumentError, "Parameters must be of class Date" @@ -29,6 +32,7 @@ def include?(date) return date >= @start_date && date < @end_date end + # Calculate Number of Nights def nights return (@end_date - @start_date).to_i end diff --git a/lib/hotel_controller.rb b/lib/hotel_controller.rb index ce1e853a7..6905dacc0 100644 --- a/lib/hotel_controller.rb +++ b/lib/hotel_controller.rb @@ -14,17 +14,36 @@ def reserve_room(start_date, end_date) return reservation end - # on reservation list, for each reservation, check if the range include the date in question + # On reservation list, for each reservation, check if the range include the date in question def reservations(date) return @reservation_list.select do |reservation| reservation.range.include?(date) end end + # Reservations for a Given Room and Range room 2, jan 1st - jan 30 + def reservation_by_room(start_date, end_date, room) + range = Hotel::DateRange.new(start_date, end_date) + return @reservation_list.select do |reservation| # which reservations have an overlap with the ranged passed + reservation.range.overlap?(range) && reservation.room == room + end + end + + + # def reservation_by_date(start_date, end_date) + + # end + + # Wave 2 - def available_rooms(start_date, end_date) - # start_date and end_date should be instances of class Date - return [] + def available_rooms(start_date, end_date, room) # return the list of rooms available in this date range + return @rooms.select { |room| room_available(start_date, end_date, room)} + end + + def room_available(start_date, end_date, room) # return true if one room is available for this range + return reservation_by_room(start_date, end_date, room).size == 0 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 e40a9df64..7efeae673 100644 --- a/test/date_range_test.rb +++ b/test/date_range_test.rb @@ -38,11 +38,9 @@ describe "overlap?" do before do - def simple_range(start_day, end_day) return Hotel::DateRange.new(Date.new(2017, 01, start_day), Date.new(2017, 01, end_day)) end - end it "raises an ArgumentError if parameter is not a range" do diff --git a/test/hotel_controller_test.rb b/test/hotel_controller_test.rb index bde2ae979..894c753a5 100644 --- a/test/hotel_controller_test.rb +++ b/test/hotel_controller_test.rb @@ -60,8 +60,7 @@ it "takes two dates and returns a list" do start_date = @date end_date = start_date + 3 - - room_list = @hotel_controller.available_rooms(start_date, end_date) # DateRange? + room_list = @hotel_controller.available_rooms(start_date, end_date) expect(room_list).must_be_kind_of Array end From 1a046ff6c8094ed734d71b0751c2abc437f1ef5b Mon Sep 17 00:00:00 2001 From: SuelyBarreto Date: Tue, 3 Mar 2020 14:21:32 -0800 Subject: [PATCH 13/37] fixed errors after available_rooms (start_day, end_day, room) --- lib/date_range.rb | 2 +- lib/hotel_controller.rb | 29 ++++++++++++++--------------- lib/reservation.rb | 3 ++- test/hotel_controller_test.rb | 15 ++++++++++----- 4 files changed, 27 insertions(+), 22 deletions(-) diff --git a/lib/date_range.rb b/lib/date_range.rb index c8bb60eac..7b5764c10 100644 --- a/lib/date_range.rb +++ b/lib/date_range.rb @@ -23,7 +23,7 @@ def overlap?(other) return other.start_date < @end_date && other.end_date > @start_date end - # Check Date + # Check Date***? def include?(date) unless date.instance_of?(Date) raise ArgumentError, "Parameters must be of class Date" diff --git a/lib/hotel_controller.rb b/lib/hotel_controller.rb index 6905dacc0..e13faa577 100644 --- a/lib/hotel_controller.rb +++ b/lib/hotel_controller.rb @@ -2,26 +2,28 @@ module Hotel class HotelController attr_reader :rooms, :reservation_list + # Constructor def initialize @rooms = (1..20).to_a @reservation_list = [] end + # Access List of All Rooms in the Hotel def reserve_room(start_date, end_date) - room = available_rooms(start_date, end_date).first + room = available_rooms(start_date, end_date, room).first reservation = Hotel::Reservation.new(start_date, end_date, room) @reservation_list << reservation return reservation end - # On reservation list, for each reservation, check if the range include the date in question + # On Reservation List for each Reservation Check if Range Includes Date in Question def reservations(date) return @reservation_list.select do |reservation| reservation.range.include?(date) end end - # Reservations for a Given Room and Range room 2, jan 1st - jan 30 + # Returns Reservation(s) for Speciifc Room and Range def reservation_by_room(start_date, end_date, room) range = Hotel::DateRange.new(start_date, end_date) return @reservation_list.select do |reservation| # which reservations have an overlap with the ranged passed @@ -29,21 +31,18 @@ def reservation_by_room(start_date, end_date, room) end end - - # def reservation_by_date(start_date, end_date) - - # end - - - # Wave 2 - def available_rooms(start_date, end_date, room) # return the list of rooms available in this date range - return @rooms.select { |room| room_available(start_date, end_date, room)} - end - - def room_available(start_date, end_date, room) # return true if one room is available for this range + # Return Specific Room is Available for a Date Range + def room_available(start_date, end_date, room) return reservation_by_room(start_date, end_date, room).size == 0 end + # Return the List of Rooms Available for a Date Range + def available_rooms(start_date, end_date, room) + return @rooms.select { |room| room_available(start_date, end_date, room) } + end + # def reservation_by_date(start_date, end_date) + # end + # Wave 2 end end \ No newline at end of file diff --git a/lib/reservation.rb b/lib/reservation.rb index 6ba5d8ee3..9c0def203 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -3,12 +3,13 @@ class Reservation attr_reader :range, :room ROOM_COST = 200 + # Constructor def initialize(start_date, end_date, room) @range = Hotel::DateRange.new(start_date, end_date) @room = room end - # Every room is identical and costs $200/night + # Rooms Identical and Cost $200/night def cost return ROOM_COST * @range.nights end diff --git a/test/hotel_controller_test.rb b/test/hotel_controller_test.rb index 894c753a5..391e02318 100644 --- a/test/hotel_controller_test.rb +++ b/test/hotel_controller_test.rb @@ -60,7 +60,8 @@ it "takes two dates and returns a list" do start_date = @date end_date = start_date + 3 - room_list = @hotel_controller.available_rooms(start_date, end_date) + room = 10 + room_list = @hotel_controller.available_rooms(start_date, end_date, room) expect(room_list).must_be_kind_of Array end @@ -70,8 +71,7 @@ # access the list of reservations for a specified room and a given date range -# User Stories - As a user of the hotel system...I can/want to...so that I can/can't - +# ## User Stories - As a user of the hotel system...I can/want to...so that I can/can't # HotelController - access the list of all of the rooms in the hotel "Who has the list of all the rooms in the hotel?" # HotelController - access the list of reservations for a specified room and a given date range "Who has access to that?" # HotelController - access the list of reservations for a specific date, so that I can track reservations by date @@ -79,11 +79,16 @@ # DateRange - want exception raised when an invalid date range is provided, so I can't make a reservation for an invalid date range # Details - # HotelController - the hotel has 20 rooms, numbered 1 through 20 (array from 1 to 20) # Reservation - every room is identical and costs $200/night (constant cost $200) # DateRange - last day is the checkout day, guest shouldn't be charged for that day/night (end_day doesn't count) # HotelController - given only start and end dates, determine which room to use for the reservation -# For this wave, you don't need to check whether reservations conflict with each other (this will come in wave 2!) +### Wave Two: Room Availability +# ## User Stories - As a user of the hotel system...I can/want to...so that I can/can't +# 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 +# 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 +# want an exception raised if I try to reserve a room during a date range when all rooms are reserved, so that I cannot make two reservations for the same room that overlap by date +# Details +# A reservation is allowed start on the same day that another reservation for the same room ends From c2d27174e8b4dd0000b48d1f12dd536872d38122 Mon Sep 17 00:00:00 2001 From: SuelyBarreto Date: Tue, 3 Mar 2020 18:52:29 -0800 Subject: [PATCH 14/37] fixing comments --- lib/date_range.rb | 4 +++- lib/hotel_controller.rb | 18 ++++++++++-------- lib/reservation.rb | 1 + test/hotel_controller_test.rb | 18 ------------------ 4 files changed, 14 insertions(+), 27 deletions(-) diff --git a/lib/date_range.rb b/lib/date_range.rb index 7b5764c10..b7f9ec819 100644 --- a/lib/date_range.rb +++ b/lib/date_range.rb @@ -3,6 +3,7 @@ class DateRange attr_accessor :start_date, :end_date # Constructor + # Raise exception when an invalid date range is provided def initialize(start_date, end_date) unless start_date.instance_of?(Date) && end_date.instance_of?(Date) raise ArgumentError, "Parameters must be of class Date" @@ -23,7 +24,7 @@ def overlap?(other) return other.start_date < @end_date && other.end_date > @start_date end - # Check Date***? + # Check if a Date is Included in the DateRange def include?(date) unless date.instance_of?(Date) raise ArgumentError, "Parameters must be of class Date" @@ -33,6 +34,7 @@ def include?(date) end # Calculate Number of Nights + # Last day is the checkout day, guest shouldn't be charged for that day/night def nights return (@end_date - @start_date).to_i end diff --git a/lib/hotel_controller.rb b/lib/hotel_controller.rb index e13faa577..01b7401da 100644 --- a/lib/hotel_controller.rb +++ b/lib/hotel_controller.rb @@ -1,14 +1,17 @@ module Hotel class HotelController + # Access the list of all of the rooms in the hotel attr_reader :rooms, :reservation_list # Constructor + # Hotel has 20 rooms, numbered 1 through 20 def initialize @rooms = (1..20).to_a @reservation_list = [] end - # Access List of All Rooms in the Hotel + # Reserve a Room for a Given Date Range + # Given only start and end dates, determine which room to use for the reservation def reserve_room(start_date, end_date) room = available_rooms(start_date, end_date, room).first reservation = Hotel::Reservation.new(start_date, end_date, room) @@ -16,14 +19,15 @@ def reserve_room(start_date, end_date) return reservation end - # On Reservation List for each Reservation Check if Range Includes Date in Question + # Access the list of reservations for a specific date, so that I can track reservations by date def reservations(date) return @reservation_list.select do |reservation| reservation.range.include?(date) end end - # Returns Reservation(s) for Speciifc Room and Range + # Returns Reservation(s) Specific for Room and Range + # Access the list of reservations for a specified room and a given date range def reservation_by_room(start_date, end_date, room) range = Hotel::DateRange.new(start_date, end_date) return @reservation_list.select do |reservation| # which reservations have an overlap with the ranged passed @@ -31,18 +35,16 @@ def reservation_by_room(start_date, end_date, room) end end - # Return Specific Room is Available for a Date Range + # Returns Specific Room Available for a Date Range def room_available(start_date, end_date, room) return reservation_by_room(start_date, end_date, room).size == 0 end - # Return the List of Rooms Available for a Date Range + # Returns List of Rooms Available for a Date Range + # View a list of rooms that are not reserved for a given date range, so that I can see all available rooms for that date range def available_rooms(start_date, end_date, room) return @rooms.select { |room| room_available(start_date, end_date, room) } end - # def reservation_by_date(start_date, end_date) - # end - # Wave 2 end end \ No newline at end of file diff --git a/lib/reservation.rb b/lib/reservation.rb index 9c0def203..8ccdd33b6 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -9,6 +9,7 @@ def initialize(start_date, end_date, room) @room = room end + # Get the total cost for a given reservation # Rooms Identical and Cost $200/night def cost return ROOM_COST * @range.nights diff --git a/test/hotel_controller_test.rb b/test/hotel_controller_test.rb index 391e02318..246567f6a 100644 --- a/test/hotel_controller_test.rb +++ b/test/hotel_controller_test.rb @@ -68,25 +68,7 @@ end end end -# access the list of reservations for a specified room and a given date range - -# ## User Stories - As a user of the hotel system...I can/want to...so that I can/can't -# HotelController - access the list of all of the rooms in the hotel "Who has the list of all the rooms in the hotel?" -# HotelController - access the list of reservations for a specified room and a given date range "Who has access to that?" -# HotelController - access the list of reservations for a specific date, so that I can track reservations by date -# Reservation - get the total cost for a given reservation -# DateRange - want exception raised when an invalid date range is provided, so I can't make a reservation for an invalid date range - -# Details -# HotelController - the hotel has 20 rooms, numbered 1 through 20 (array from 1 to 20) -# Reservation - every room is identical and costs $200/night (constant cost $200) -# DateRange - last day is the checkout day, guest shouldn't be charged for that day/night (end_day doesn't count) -# HotelController - given only start and end dates, determine which room to use for the reservation - -### Wave Two: Room Availability -# ## User Stories - As a user of the hotel system...I can/want to...so that I can/can't -# 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 # 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 # want an exception raised if I try to reserve a room during a date range when all rooms are reserved, so that I cannot make two reservations for the same room that overlap by date From 3c75bc409c872be7d1c056c0a5d989db869faa28 Mon Sep 17 00:00:00 2001 From: SuelyBarreto Date: Tue, 3 Mar 2020 20:39:47 -0800 Subject: [PATCH 15/37] adding tests to hotel controller tests --- lib/hotel_controller.rb | 24 ++++---- test/hotel_controller_test.rb | 107 +++++++++++++++++++++++++--------- 2 files changed, 90 insertions(+), 41 deletions(-) diff --git a/lib/hotel_controller.rb b/lib/hotel_controller.rb index 01b7401da..22e8e9fc5 100644 --- a/lib/hotel_controller.rb +++ b/lib/hotel_controller.rb @@ -1,49 +1,49 @@ module Hotel class HotelController # Access the list of all of the rooms in the hotel - attr_reader :rooms, :reservation_list + attr_reader :rooms, :reservations # Constructor # Hotel has 20 rooms, numbered 1 through 20 def initialize @rooms = (1..20).to_a - @reservation_list = [] + @reservations = [] end # Reserve a Room for a Given Date Range # Given only start and end dates, determine which room to use for the reservation def reserve_room(start_date, end_date) - room = available_rooms(start_date, end_date, room).first + room = available_rooms(start_date, end_date).first reservation = Hotel::Reservation.new(start_date, end_date, room) - @reservation_list << reservation + @reservations << reservation return reservation end # Access the list of reservations for a specific date, so that I can track reservations by date - def reservations(date) - return @reservation_list.select do |reservation| + def reservations_by_date(date) + return @reservations.select do |reservation| reservation.range.include?(date) end end # Returns Reservation(s) Specific for Room and Range # Access the list of reservations for a specified room and a given date range - def reservation_by_room(start_date, end_date, room) + def reservations_by_room(start_date, end_date, room) range = Hotel::DateRange.new(start_date, end_date) - return @reservation_list.select do |reservation| # which reservations have an overlap with the ranged passed + return @reservations.select do |reservation| # which reservations have an overlap with the ranged passed reservation.range.overlap?(range) && reservation.room == room end end # Returns Specific Room Available for a Date Range - def room_available(start_date, end_date, room) - return reservation_by_room(start_date, end_date, room).size == 0 + def is_room_available?(start_date, end_date, room) + return reservations_by_room(start_date, end_date, room).size == 0 end # Returns List of Rooms Available for a Date Range # View a list of rooms that are not reserved for a given date range, so that I can see all available rooms for that date range - def available_rooms(start_date, end_date, room) - return @rooms.select { |room| room_available(start_date, end_date, room) } + def available_rooms(start_date, end_date) + return @rooms.select { |room| is_room_available?(start_date, end_date, room) } end end diff --git a/test/hotel_controller_test.rb b/test/hotel_controller_test.rb index 246567f6a..f0835e2e8 100644 --- a/test/hotel_controller_test.rb +++ b/test/hotel_controller_test.rb @@ -2,47 +2,67 @@ describe "Hotel::HotelController" do before do - @hotel_controller = Hotel::HotelController.new - @date = Date.parse("2020-08-04") + @hotel = Hotel::HotelController.new + def test_date(day) + return Date.new(2020, 01, day) + end end describe "wave 1" do - describe "rooms" do it "returns a list" do - expect(@hotel_controller.rooms).must_be_kind_of Array + expect(@hotel.rooms).must_be_kind_of Array end it "return 20 rooms" do - expect(@hotel_controller.rooms.size).must_equal 20 + expect(@hotel.rooms.size).must_equal 20 end it "return 20 rooms numbered 1 through 20" do (1..20).each do |room| - expect(@hotel_controller.rooms.include?(room)).must_equal true + expect(@hotel.rooms.include?(room)).must_equal true end end end describe "reserve_room" do it "takes two Date objects and returns a Reservation" do - start_date = @date - end_date = start_date + 3 - reservation = @hotel_controller.reserve_room(start_date, end_date) - + reservation = @hotel.reserve_room(test_date(1), test_date(4)) expect(reservation).must_be_kind_of Hotel::Reservation end + + it "adds to the list of reservations" do # **** + reservation1 = @hotel.reserve_room(test_date(1), test_date(4)) + expect(@hotel.reservations.size).must_equal 1 + reservation2 = @hotel.reserve_room(test_date(1), test_date(4)) + expect(@hotel.reservations.size).must_equal 2 + end + + it "selects the first available room" do + # reserve a room, than check if the reservation room equals to 1 + # reserve another room, than check if the reservationn room equals 2 + end + + it "returns error if no rooms are available" do + # reserve 20 rooms (20.times). The 21st should return an error. + end + + it "does not accept the same date for start and end" do + # create a reservation from 2020/01/05 to 2020/01/05. It should raise an exception + end + + it "does not accept a start date after the end date" do + # create a reservation from 2020/01/05 to 2020/01/01. It should raise an exception + end end # access the list of reservations for a specific date, so that I can track reservations by date - describe "reservations" do + describe "reservations_by_date" do it "takes a Date and returns a list of Reservations" do - start_date = @date - end_date = start_date + 3 - reservation1 = @hotel_controller.reserve_room(start_date, end_date) - reservation2 = @hotel_controller.reserve_room(start_date, end_date) + reservation1 = @hotel.reserve_room(test_date(1), test_date(4)) + reservation2 = @hotel.reserve_room(test_date(1), test_date(4)) - reservations_ondate = @hotel_controller.reservations(@date) + reservations_ondate = @hotel.reservations_by_date(test_date(1)) expect(reservations_ondate).must_be_kind_of Array expect(reservations_ondate.size).must_equal 2 @@ -51,26 +71,55 @@ expect(reservation).must_be_kind_of Hotel::Reservation end end + + it "returns the reservation when you ask for the start date" do + reservation1 = @hotel.reserve_room(test_date(1), test_date(4)) + reservations_ondate = @hotel.reservations_by_date(test_date(1)) + expect(reservations_ondate.size).must_equal 1 # **** + # create another reservation from 2020/01/01 to 2020/01/05, than get a list of reservations for 2020/01/01. It should return 2 + # create another reservation from 2020/01/05 to 2020/01/10, than get a list of reservations for 2020/01/01. It should return 2 + end + + it "returns the reservation when you ask for a date in the middle" do + # create a reservation from 2020/01/01 to 2020/01/10, than get a list of reservations for 2020/01/05. It should return 1 + # create a reservation from 2020/01/02 to 2020/01/15, than get a list of reservations for 2020/01/05. It should return 2 + # create a reservation from 2020/01/10 to 2020/01/15, than get a list of reservations for 2020/01/05. It should return 2 + end + + it "does not return a reservation when you ask for the end date" do + # create a reservation from 2020/01/01 to 2020/01/10, than get a list of reservations for 2020/01/10. It should return 0 + # create a reservation from 2020/01/05 to 2020/01/10, than get a list of reservations for 2020/01/10. It should return 0 + end + end end - # check whether reservations conflict with each other (this will come in wave 2!) + # check whether reservations conflict with each other describe "wave 2" do describe "available_rooms" do it "takes two dates and returns a list" do - start_date = @date - end_date = start_date + 3 - room = 10 - room_list = @hotel_controller.available_rooms(start_date, end_date, room) - + room_list = @hotel.available_rooms(test_date(1), test_date(4)) expect(room_list).must_be_kind_of Array end - end - end -end -# 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 -# want an exception raised if I try to reserve a room during a date range when all rooms are reserved, so that I cannot make two reservations for the same room that overlap by date + it "removes a room after it is reserved" do + reservation1 = @hotel.reserve_room(test_date(1), test_date(5)) + expect(@hotel.available_rooms(test_date(1), test_date(5)).size).must_equal 19 + reservation1 = @hotel.reserve_room(test_date(1), test_date(5)) + expect(@hotel.available_rooms(test_date(1), test_date(5)).size).must_equal 18 + end + + it "does not show the room available if there is an overlaping reservation" do + # create a reservation from 2020/01/05 to 2020/01/15. + # Check if the list of available rooms from 2020/01/10 to 2020/01/20 has 19 rooms + # Check if the list of available rooms from 2020/01/01 to 2020/01/10 has 19 rooms + # Check if the list of available rooms from 2020/01/07 to 2020/01/12 has 19 rooms + # Check if the list of available rooms from 2020/01/01 to 2020/01/20 has 19 rooms + # Check if the list of available rooms from 2020/01/01 to 2020/01/03 has 20 rooms + # Check if the list of available rooms from 2020/01/17 to 2020/01/20 has 20 rooms + # Check if the list of available rooms from 2020/01/15 to 2020/01/20 has 20 rooms + end -# Details -# A reservation is allowed start on the same day that another reservation for the same room ends + end + end +end \ No newline at end of file From 78579ecfc98be4b7e13fa1d7181d9087f5586260 Mon Sep 17 00:00:00 2001 From: SuelyBarreto Date: Wed, 4 Mar 2020 14:34:42 -0800 Subject: [PATCH 16/37] fixing errors --- lib/hotel_controller.rb | 6 ++++- test/date_range_test.rb | 19 +--------------- test/hotel_controller_test.rb | 43 +++++++++++++++++++++++++++-------- test/reservation_test.rb | 38 +++++++++---------------------- 4 files changed, 51 insertions(+), 55 deletions(-) diff --git a/lib/hotel_controller.rb b/lib/hotel_controller.rb index 22e8e9fc5..6b0b2a71f 100644 --- a/lib/hotel_controller.rb +++ b/lib/hotel_controller.rb @@ -15,7 +15,11 @@ def initialize def reserve_room(start_date, end_date) room = available_rooms(start_date, end_date).first reservation = Hotel::Reservation.new(start_date, end_date, room) - @reservations << reservation + if @reservations.size >= 20 + raise ArgumentError, "No Vacancy" + else + @reservations << reservation + end return reservation end diff --git a/test/date_range_test.rb b/test/date_range_test.rb index 7efeae673..0ce73e41d 100644 --- a/test/date_range_test.rb +++ b/test/date_range_test.rb @@ -148,21 +148,4 @@ def simple_range(start_day, end_day) end end -end - -# User Stories - As a user of the hotel system...I can/want to...so that I can/can't - -# HotelController - access the list of all of the rooms in the hotel "Who has the list of all the rooms in the hotel?" -# HotelController - access the list of reservations for a specified room and a given date range "Who has access to that?" -# HotelController - access the list of reservations for a specific date, so that I can track reservations by date -# Reservation - get the total cost for a given reservation -# DateRange - want exception raised when an invalid date range is provided, so I can't make a reservation for an invalid date range - -# Details - -# HotelController - the hotel has 20 rooms, numbered 1 through 20 (array from 1 to 20) -# Reservation - every room is identical and costs $200/night (constant cost $200) -# DateRange - last day is the checkout day, guest shouldn't be charged for that day/night (end_day doesn't count) -# HotelController - given only start and end dates, determine which room to use for the reservation - -# For this wave, you don't need to check whether reservations conflict with each other (this will come in wave 2!) \ No newline at end of file +end \ No newline at end of file diff --git a/test/hotel_controller_test.rb b/test/hotel_controller_test.rb index f0835e2e8..6a6814383 100644 --- a/test/hotel_controller_test.rb +++ b/test/hotel_controller_test.rb @@ -1,6 +1,8 @@ require_relative "test_helper" +require "awesome_print" describe "Hotel::HotelController" do + before do @hotel = Hotel::HotelController.new def test_date(day) @@ -31,7 +33,7 @@ def test_date(day) expect(reservation).must_be_kind_of Hotel::Reservation end - it "adds to the list of reservations" do # **** + it "adds to the list of reservations" do reservation1 = @hotel.reserve_room(test_date(1), test_date(4)) expect(@hotel.reservations.size).must_equal 1 reservation2 = @hotel.reserve_room(test_date(1), test_date(4)) @@ -40,23 +42,36 @@ def test_date(day) it "selects the first available room" do # reserve a room, than check if the reservation room equals to 1 - # reserve another room, than check if the reservationn room equals 2 + reservation1 = @hotel.reserve_room(test_date(1), test_date(4)) + expect(reservation1.room).must_equal 1 + expect(@hotel.reservations.last.room).must_equal 1 + # reserve another room, than check if the reservation room equals 2 + reservation2 = @hotel.reserve_room(test_date(1), test_date(4)) + expect(reservation2.room).must_equal 2 + expect(@hotel.reservations.last.room).must_equal 2 end - it "returns error if no rooms are available" do + it "returns error if no rooms are available" do # # reserve 20 rooms (20.times). The 21st should return an error. + (1..20).each do |reservation| + reservation = @hotel.reserve_room(test_date(1), test_date(4)) + end + # expect(@hotel.reservations.size).must_equal 20 + expect{ @hotel.reserve_room(test_date(1), test_date(4)) }.must_raise ArgumentError end - it "does not accept the same date for start and end" do + it "does not accept the same date for start and end" do # # create a reservation from 2020/01/05 to 2020/01/05. It should raise an exception + expect{ @hotel.reserve_room(test_date(1), test_date(1)) }.must_raise ArgumentError end - it "does not accept a start date after the end date" do + it "does not accept a start date after the end date" do # # create a reservation from 2020/01/05 to 2020/01/01. It should raise an exception + expect{ @hotel.reserve_room(test_date(5), test_date(1)) }.must_raise ArgumentError end end - # access the list of reservations for a specific date, so that I can track reservations by date + # access the list of reservations for a specific date, so that I can track reservations by date describe "reservations_by_date" do it "takes a Date and returns a list of Reservations" do reservation1 = @hotel.reserve_room(test_date(1), test_date(4)) @@ -73,11 +88,20 @@ def test_date(day) end it "returns the reservation when you ask for the start date" do + # create a reservation from 2020/01/01 to 2020/01/04, than get a list of reservations for 2020/01/01, should return 1 reservation1 = @hotel.reserve_room(test_date(1), test_date(4)) reservations_ondate = @hotel.reservations_by_date(test_date(1)) - expect(reservations_ondate.size).must_equal 1 # **** - # create another reservation from 2020/01/01 to 2020/01/05, than get a list of reservations for 2020/01/01. It should return 2 - # create another reservation from 2020/01/05 to 2020/01/10, than get a list of reservations for 2020/01/01. It should return 2 + expect(reservations_ondate.size).must_equal 1 # + # create another reservation from 2020/01/01 to 2020/01/05, than get a list of reservations for 2020/01/01, should return 2 + reservation2 = @hotel.reserve_room(test_date(1), test_date(5)) + reservations_ondate = @hotel.reservations_by_date(test_date(1)) + expect(reservations_ondate.size).must_equal 2 # + # create another reservation from 2020/01/05 to 2020/01/10, than get a list of reservations for 2020/01/05, should return 2 + reservation3 = @hotel.reserve_room(test_date(5), test_date(10)) + ap @hotel.reservations + reservations_ondate = @hotel.reservations_by_date(test_date(5)) + ap reservations_ondate + expect(reservations_ondate.size).must_equal 2 # end it "returns the reservation when you ask for a date in the middle" do @@ -122,4 +146,5 @@ def test_date(day) end end + end \ No newline at end of file diff --git a/test/reservation_test.rb b/test/reservation_test.rb index 8e12bf990..09079b694 100644 --- a/test/reservation_test.rb +++ b/test/reservation_test.rb @@ -1,11 +1,12 @@ require_relative "test_helper" describe "Hotel::Reservation" do + describe "constructor" do it "can be initialized with two dates and a room" do - start_date = Date.new(2017, 01, 01) - end_date = start_date + 3 - room = 10 + start_date = Date.new(2017, 01, 01) + end_date = start_date + 3 + room = 10 reservation = Hotel::Reservation.new(start_date, end_date, room) expect(reservation).must_be_kind_of Hotel::Reservation @@ -15,10 +16,10 @@ end it "raises an ArgumentError if invalid dates provided" do - start_date = Date.new(2017, 01, 01) + start_date = Date.new(2017, 01, 01) end_date1 = start_date - 1 end_date2 = "2017/01/04" - room = 10 + room = 10 # start date can not be after end date expect{ reservation = Hotel::Reservation.new(start_date, end_date1, room) }.must_raise ArgumentError @@ -31,9 +32,9 @@ describe "cost" do before do - start_date = Date.new(2017, 01, 01) - end_date = start_date + 3 - room = 10 + start_date = Date.new(2017, 01, 01) + end_date = start_date + 3 + room = 10 @reservation = Hotel::Reservation.new(start_date, end_date, room) end @@ -45,23 +46,6 @@ # Reservation - Every room is identical and a room always costs $200/night expect(@reservation.cost).must_equal 600 end - end -end - -# User Stories - As a user of the hotel system...I can/want to...so that I can/can't - -# HotelController - access the list of all of the rooms in the hotel "Who has the list of all the rooms in the hotel?" -# HotelController - access the list of reservations for a specified room and a given date range "Who has access to that?" -# HotelController - access the list of reservations for a specific date, so that I can track reservations by date -# Reservation - get the total cost for a given reservation -# DateRange - want exception raised when an invalid date range is provided, so I can't make a reservation for an invalid date range - -# Details - -# HotelController - the hotel has 20 rooms, numbered 1 through 20 (array from 1 to 20) -# Reservation - every room is identical and costs $200/night (constant cost $200) -# DateRange - last day is the checkout day, guest shouldn't be charged for that day/night (end_day doesn't count) -# HotelController - given only start and end dates, determine which room to use for the reservation - -# For this wave, you don't need to check whether reservations conflict with each other (this will come in wave 2!) \ No newline at end of file + +end \ No newline at end of file From 8aa44f1e54f96bcaa9758ef0025b02031a5e1d82 Mon Sep 17 00:00:00 2001 From: SuelyBarreto Date: Wed, 4 Mar 2020 15:19:03 -0800 Subject: [PATCH 17/37] returns the reservation when you ask for a date in the middle --- test/hotel_controller_test.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/test/hotel_controller_test.rb b/test/hotel_controller_test.rb index 6a6814383..d62790a8b 100644 --- a/test/hotel_controller_test.rb +++ b/test/hotel_controller_test.rb @@ -106,8 +106,14 @@ def test_date(day) it "returns the reservation when you ask for a date in the middle" do # create a reservation from 2020/01/01 to 2020/01/10, than get a list of reservations for 2020/01/05. It should return 1 + reservation1 = @hotel.reserve_room(test_date(1), test_date(10)) + expect(@hotel.reservations_by_date(test_date(5)).size).must_equal 1 # create a reservation from 2020/01/02 to 2020/01/15, than get a list of reservations for 2020/01/05. It should return 2 + reservation2 = @hotel.reserve_room(test_date(2), test_date(15)) + expect(@hotel.reservations_by_date(test_date(5)).size).must_equal 2 # create a reservation from 2020/01/10 to 2020/01/15, than get a list of reservations for 2020/01/05. It should return 2 + reservation3 = @hotel.reserve_room(test_date(10), test_date(15)) + expect(@hotel.reservations_by_date(test_date(5)).size).must_equal 2 end it "does not return a reservation when you ask for the end date" do From 269d56decf7c1673b66ff204f67a6d1499c1654d Mon Sep 17 00:00:00 2001 From: SuelyBarreto Date: Wed, 4 Mar 2020 16:14:07 -0800 Subject: [PATCH 18/37] more tests --- test/hotel_controller_test.rb | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/test/hotel_controller_test.rb b/test/hotel_controller_test.rb index d62790a8b..0f4c84c47 100644 --- a/test/hotel_controller_test.rb +++ b/test/hotel_controller_test.rb @@ -101,7 +101,7 @@ def test_date(day) ap @hotel.reservations reservations_ondate = @hotel.reservations_by_date(test_date(5)) ap reservations_ondate - expect(reservations_ondate.size).must_equal 2 # + expect(reservations_ondate.size).must_equal 1 # one instead of two! end it "returns the reservation when you ask for a date in the middle" do @@ -118,7 +118,11 @@ def test_date(day) it "does not return a reservation when you ask for the end date" do # create a reservation from 2020/01/01 to 2020/01/10, than get a list of reservations for 2020/01/10. It should return 0 + reservation1 = @hotel.reserve_room(test_date(1), test_date(10)) + expect(@hotel.reservations_by_date(test_date(10)).size).must_equal 0 # create a reservation from 2020/01/05 to 2020/01/10, than get a list of reservations for 2020/01/10. It should return 0 + reservation2 = @hotel.reserve_room(test_date(5), test_date(10)) + expect(@hotel.reservations_by_date(test_date(10)).size).must_equal 0 end end @@ -141,13 +145,21 @@ def test_date(day) it "does not show the room available if there is an overlaping reservation" do # create a reservation from 2020/01/05 to 2020/01/15. + reservation1 = @hotel.reserve_room(test_date(5), test_date(15)) # Check if the list of available rooms from 2020/01/10 to 2020/01/20 has 19 rooms + expect(@hotel.available_rooms(test_date(10), test_date(20)).size).must_equal 19 # Check if the list of available rooms from 2020/01/01 to 2020/01/10 has 19 rooms + expect(@hotel.available_rooms(test_date(1), test_date(10)).size).must_equal 19 # Check if the list of available rooms from 2020/01/07 to 2020/01/12 has 19 rooms + expect(@hotel.available_rooms(test_date(7), test_date(12)).size).must_equal 19 # Check if the list of available rooms from 2020/01/01 to 2020/01/20 has 19 rooms + expect(@hotel.available_rooms(test_date(1), test_date(20)).size).must_equal 19 # Check if the list of available rooms from 2020/01/01 to 2020/01/03 has 20 rooms + expect(@hotel.available_rooms(test_date(1), test_date(3)).size).must_equal 20 # Check if the list of available rooms from 2020/01/17 to 2020/01/20 has 20 rooms + expect(@hotel.available_rooms(test_date(17), test_date(20)).size).must_equal 20 # Check if the list of available rooms from 2020/01/15 to 2020/01/20 has 20 rooms + expect(@hotel.available_rooms(test_date(15), test_date(20)).size).must_equal 20 end end From 7f32c5d7f6ef8603121c986df4eaa1b57b9b38b0 Mon Sep 17 00:00:00 2001 From: SuelyBarreto Date: Wed, 4 Mar 2020 18:55:53 -0800 Subject: [PATCH 19/37] end of hotel controller tests --- lib/hotel_controller.rb | 7 +--- test/hotel_controller_test.rb | 76 ++++++++++++++++++----------------- 2 files changed, 42 insertions(+), 41 deletions(-) diff --git a/lib/hotel_controller.rb b/lib/hotel_controller.rb index 6b0b2a71f..59694a0e0 100644 --- a/lib/hotel_controller.rb +++ b/lib/hotel_controller.rb @@ -14,12 +14,9 @@ def initialize # Given only start and end dates, determine which room to use for the reservation def reserve_room(start_date, end_date) room = available_rooms(start_date, end_date).first + raise ArgumentError, "No Vacancy" if room == nil reservation = Hotel::Reservation.new(start_date, end_date, room) - if @reservations.size >= 20 - raise ArgumentError, "No Vacancy" - else - @reservations << reservation - end + @reservations << reservation return reservation end diff --git a/test/hotel_controller_test.rb b/test/hotel_controller_test.rb index 0f4c84c47..86ef13bd0 100644 --- a/test/hotel_controller_test.rb +++ b/test/hotel_controller_test.rb @@ -34,9 +34,9 @@ def test_date(day) end it "adds to the list of reservations" do - reservation1 = @hotel.reserve_room(test_date(1), test_date(4)) + @hotel.reserve_room(test_date(1), test_date(4)) expect(@hotel.reservations.size).must_equal 1 - reservation2 = @hotel.reserve_room(test_date(1), test_date(4)) + @hotel.reserve_room(test_date(1), test_date(4)) expect(@hotel.reservations.size).must_equal 2 end @@ -51,21 +51,24 @@ def test_date(day) expect(@hotel.reservations.last.room).must_equal 2 end - it "returns error if no rooms are available" do # + it "returns error if no rooms are available" do # reserve 20 rooms (20.times). The 21st should return an error. - (1..20).each do |reservation| - reservation = @hotel.reserve_room(test_date(1), test_date(4)) + (1..20).each do + @hotel.reserve_room(test_date(1), test_date(4)) end - # expect(@hotel.reservations.size).must_equal 20 + expect(@hotel.reservations.size).must_equal 20 expect{ @hotel.reserve_room(test_date(1), test_date(4)) }.must_raise ArgumentError + # it's ok to create more reservations in other date ranges + @hotel.reserve_room(test_date(15), test_date(20)) + expect(@hotel.reservations.size).must_equal 21 end - it "does not accept the same date for start and end" do # + it "does not accept the same date for start and end" do # create a reservation from 2020/01/05 to 2020/01/05. It should raise an exception - expect{ @hotel.reserve_room(test_date(1), test_date(1)) }.must_raise ArgumentError + expect{ @hotel.reserve_room(test_date(5), test_date(5)) }.must_raise ArgumentError end - it "does not accept a start date after the end date" do # + it "does not accept a start date after the end date" do # create a reservation from 2020/01/05 to 2020/01/01. It should raise an exception expect{ @hotel.reserve_room(test_date(5), test_date(1)) }.must_raise ArgumentError end @@ -74,9 +77,8 @@ def test_date(day) # access the list of reservations for a specific date, so that I can track reservations by date describe "reservations_by_date" do it "takes a Date and returns a list of Reservations" do - reservation1 = @hotel.reserve_room(test_date(1), test_date(4)) - reservation2 = @hotel.reserve_room(test_date(1), test_date(4)) - + @hotel.reserve_room(test_date(1), test_date(4)) + @hotel.reserve_room(test_date(1), test_date(4)) reservations_ondate = @hotel.reservations_by_date(test_date(1)) expect(reservations_ondate).must_be_kind_of Array @@ -89,39 +91,37 @@ def test_date(day) it "returns the reservation when you ask for the start date" do # create a reservation from 2020/01/01 to 2020/01/04, than get a list of reservations for 2020/01/01, should return 1 - reservation1 = @hotel.reserve_room(test_date(1), test_date(4)) + @hotel.reserve_room(test_date(1), test_date(4)) reservations_ondate = @hotel.reservations_by_date(test_date(1)) - expect(reservations_ondate.size).must_equal 1 # + expect(reservations_ondate.size).must_equal 1 # create another reservation from 2020/01/01 to 2020/01/05, than get a list of reservations for 2020/01/01, should return 2 - reservation2 = @hotel.reserve_room(test_date(1), test_date(5)) + @hotel.reserve_room(test_date(1), test_date(5)) reservations_ondate = @hotel.reservations_by_date(test_date(1)) - expect(reservations_ondate.size).must_equal 2 # - # create another reservation from 2020/01/05 to 2020/01/10, than get a list of reservations for 2020/01/05, should return 2 - reservation3 = @hotel.reserve_room(test_date(5), test_date(10)) - ap @hotel.reservations + expect(reservations_ondate.size).must_equal 2 + # create another reservation from 2020/01/05 to 2020/01/10, than get a list of reservations for 2020/01/05, should return 1 + @hotel.reserve_room(test_date(5), test_date(10)) reservations_ondate = @hotel.reservations_by_date(test_date(5)) - ap reservations_ondate - expect(reservations_ondate.size).must_equal 1 # one instead of two! + expect(reservations_ondate.size).must_equal 1 end it "returns the reservation when you ask for a date in the middle" do # create a reservation from 2020/01/01 to 2020/01/10, than get a list of reservations for 2020/01/05. It should return 1 - reservation1 = @hotel.reserve_room(test_date(1), test_date(10)) + @hotel.reserve_room(test_date(1), test_date(10)) expect(@hotel.reservations_by_date(test_date(5)).size).must_equal 1 # create a reservation from 2020/01/02 to 2020/01/15, than get a list of reservations for 2020/01/05. It should return 2 - reservation2 = @hotel.reserve_room(test_date(2), test_date(15)) + @hotel.reserve_room(test_date(2), test_date(15)) expect(@hotel.reservations_by_date(test_date(5)).size).must_equal 2 # create a reservation from 2020/01/10 to 2020/01/15, than get a list of reservations for 2020/01/05. It should return 2 - reservation3 = @hotel.reserve_room(test_date(10), test_date(15)) + @hotel.reserve_room(test_date(10), test_date(15)) expect(@hotel.reservations_by_date(test_date(5)).size).must_equal 2 end it "does not return a reservation when you ask for the end date" do # create a reservation from 2020/01/01 to 2020/01/10, than get a list of reservations for 2020/01/10. It should return 0 - reservation1 = @hotel.reserve_room(test_date(1), test_date(10)) + @hotel.reserve_room(test_date(1), test_date(10)) expect(@hotel.reservations_by_date(test_date(10)).size).must_equal 0 # create a reservation from 2020/01/05 to 2020/01/10, than get a list of reservations for 2020/01/10. It should return 0 - reservation2 = @hotel.reserve_room(test_date(5), test_date(10)) + @hotel.reserve_room(test_date(5), test_date(10)) expect(@hotel.reservations_by_date(test_date(10)).size).must_equal 0 end @@ -137,29 +137,33 @@ def test_date(day) end it "removes a room after it is reserved" do - reservation1 = @hotel.reserve_room(test_date(1), test_date(5)) + @hotel.reserve_room(test_date(1), test_date(5)) expect(@hotel.available_rooms(test_date(1), test_date(5)).size).must_equal 19 - reservation1 = @hotel.reserve_room(test_date(1), test_date(5)) + @hotel.reserve_room(test_date(1), test_date(5)) expect(@hotel.available_rooms(test_date(1), test_date(5)).size).must_equal 18 end it "does not show the room available if there is an overlaping reservation" do # create a reservation from 2020/01/05 to 2020/01/15. - reservation1 = @hotel.reserve_room(test_date(5), test_date(15)) - # Check if the list of available rooms from 2020/01/10 to 2020/01/20 has 19 rooms + @hotel.reserve_room(test_date(5), test_date(15)) + # Check overlap with same range + expect(@hotel.available_rooms(test_date(5), test_date(15)).size).must_equal 19 + # Check overlap in the back expect(@hotel.available_rooms(test_date(10), test_date(20)).size).must_equal 19 - # Check if the list of available rooms from 2020/01/01 to 2020/01/10 has 19 rooms + # Check overlap at the front expect(@hotel.available_rooms(test_date(1), test_date(10)).size).must_equal 19 - # Check if the list of available rooms from 2020/01/07 to 2020/01/12 has 19 rooms + # Check overlap fully contained expect(@hotel.available_rooms(test_date(7), test_date(12)).size).must_equal 19 - # Check if the list of available rooms from 2020/01/01 to 2020/01/20 has 19 rooms + # Check overlap fully containing expect(@hotel.available_rooms(test_date(1), test_date(20)).size).must_equal 19 - # Check if the list of available rooms from 2020/01/01 to 2020/01/03 has 20 rooms + # Check no overlap fully before expect(@hotel.available_rooms(test_date(1), test_date(3)).size).must_equal 20 - # Check if the list of available rooms from 2020/01/17 to 2020/01/20 has 20 rooms + # Check no overlap fully after expect(@hotel.available_rooms(test_date(17), test_date(20)).size).must_equal 20 - # Check if the list of available rooms from 2020/01/15 to 2020/01/20 has 20 rooms + # Check no overlap on checkout date after expect(@hotel.available_rooms(test_date(15), test_date(20)).size).must_equal 20 + # Check no overlap on checkout date before + expect(@hotel.available_rooms(test_date(1), test_date(5)).size).must_equal 20 end end From 80729250fabc51ef5ed66cd181a7902cf06c1e79 Mon Sep 17 00:00:00 2001 From: SuelyBarreto Date: Wed, 4 Mar 2020 20:20:13 -0800 Subject: [PATCH 20/37] Block class and block_test --- lib/block.rb | 35 ++++++++++++++++++++++++++ test/block_test.rb | 60 +++++++++++++++++++++++++++++++++++++++++++++ test/test_helper.rb | 1 + 3 files changed, 96 insertions(+) create mode 100644 lib/block.rb create mode 100644 test/block_test.rb diff --git a/lib/block.rb b/lib/block.rb new file mode 100644 index 000000000..af88e6527 --- /dev/null +++ b/lib/block.rb @@ -0,0 +1,35 @@ +module Hotel + class Block + attr_reader :range, :rooms, :rate + + # Constructor + def initialize(start_date, end_date, rooms, rate) + @range = Hotel::DateRange.new(start_date, end_date) + raise ArgumentError, "Rooms must be an array" unless rooms.instance_of?(Array) + raise ArgumentError, "No more than 5 rooms per block" if rooms.size > 5 + @rooms = rooms + raise ArgumentError, "Rate must be a number" unless rate.is_a?(Numeric) + raise ArgumentError, "Rate must be a positive number" if rate < 0 + @rate = rate + end + + def cost + return @rate * @range.nights * @rooms.size + end + end +end + +## Hotel Block +# I can create a Hotel Block if I give a date range, collection of rooms, and a discounted room rate +# A block can contain a maximum of 5 rooms + +## Hotel Controller +# I want an exception raised if I try to create a Hotel Block and at least one of the rooms is unavailable for the given date range +# Given a specific date, and that a room is set aside in a hotel block for that specific date, I cannot reserve that specific room for that specific date, because it is unavailable +# Given a specific date, and that a room is set aside in a hotel block for that specific date, I cannot create another hotel block that includes that specific room for that specific date, because it is unavailable +# I can check whether a given block has any rooms available +# I can reserve a specific room from a hotel block +# I can only reserve that room from a hotel block for the full duration of the block +# I can see a reservation made from a hotel block from the list of reservations for that date (see wave 1 requirements) +# When a room is reserved from a block of rooms, the reservation dates will always match the date range of the block +# All of the availability checking logic from Wave 2 should now respect room blocks as well as individual reservations \ No newline at end of file diff --git a/test/block_test.rb b/test/block_test.rb new file mode 100644 index 000000000..e337a59b1 --- /dev/null +++ b/test/block_test.rb @@ -0,0 +1,60 @@ +require_relative "test_helper" + +describe "Hotel::Block" do + + before do + @start_date = Date.new(2017, 01, 01) + @end_date = @start_date + 3 + @rooms = [3,4,5] + @rate = 150 + @block = Hotel::Block.new(@start_date, @end_date, @rooms, @rate) + end + + describe "constructor" do + it "can be initialized with two dates, an array of rooms and a rate" do + expect(@block).must_be_kind_of Hotel::Block + expect(@block.range.start_date).must_equal @start_date + expect(@block.range.end_date).must_equal @end_date + expect(@block.rooms).must_be_kind_of Array + expect(@block.rooms).must_equal @rooms + expect(@block.rate).must_equal @rate + end + + it "does not accept a number as rooms" do + # create a block with rooms = 3. Must raise argumenterror + expect{ Hotel::Block.new(@start_date, @end_date, 3, @rate) }.must_raise ArgumentError + end + + it "does not accept invalid rates" do + # create a block with rate = -1. Must raise argumenterror + expect{ Hotel::Block.new(@start_date, @end_date, @rooms, -1) }.must_raise ArgumentError + # create a block with rate = "100". Must raise argumenterror + expect{ Hotel::Block.new(@start_date, @end_date, @rooms, "100") }.must_raise ArgumentError + # create a block with rate = nil. Must raise argumenterror + expect{ Hotel::Block.new(@start_date, @end_date, @rooms, nil) }.must_raise ArgumentError + end + + it "raises an ArgumentError if invalid dates provided" do + end_date1 = @start_date - 1 + end_date2 = "2017/01/04" + # start date can not be after end date + expect{ Hotel::Block.new(@start_date, end_date1, @rooms, @rate) }.must_raise ArgumentError + # date can not be a string + expect{ Hotel::Block.new(@start_date, end_date2, @rooms, @rate) }.must_raise ArgumentError + # start date can not be the same as end date + expect{ Hotel::Block.new(@start_date, @start_date, @rooms, @rate) }.must_raise ArgumentError + end + end + + describe "cost" do + it "returns a number" do + expect(@block.cost).must_be_kind_of Numeric + end + + it "calculates the cost" do + # block cost for 3 nights, 3 rooms, at $150 room rate + expect(@block.cost).must_equal 1350 + end + end + +end \ No newline at end of file diff --git a/test/test_helper.rb b/test/test_helper.rb index 054b30230..e6518d098 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -14,3 +14,4 @@ require_relative "../lib/hotel_controller.rb" require_relative "../lib/reservation.rb" require_relative "../lib/date_range.rb" +require_relative "../lib/block.rb" \ No newline at end of file From 8d2def6d542188ea337b781dbc21b40bbe2851ce Mon Sep 17 00:00:00 2001 From: SuelyBarreto Date: Thu, 5 Mar 2020 21:43:48 -0800 Subject: [PATCH 21/37] Wave 3 basics, test outline --- lib/block.rb | 16 +---- lib/hotel_controller.rb | 68 +++++++++++++++++-- test/block_test.rb | 5 ++ test/hotel_controller_test.rb | 119 +++++++++++++++++++++++++++++++++- 4 files changed, 189 insertions(+), 19 deletions(-) diff --git a/lib/block.rb b/lib/block.rb index af88e6527..ec7300755 100644 --- a/lib/block.rb +++ b/lib/block.rb @@ -6,7 +6,8 @@ class Block def initialize(start_date, end_date, rooms, rate) @range = Hotel::DateRange.new(start_date, end_date) raise ArgumentError, "Rooms must be an array" unless rooms.instance_of?(Array) - raise ArgumentError, "No more than 5 rooms per block" if rooms.size > 5 + raise ArgumentError, "No more than 5 rooms per block" if rooms.size > 5 + raise ArgumentError, "At least one room per block" if rooms.size == 0 @rooms = rooms raise ArgumentError, "Rate must be a number" unless rate.is_a?(Numeric) raise ArgumentError, "Rate must be a positive number" if rate < 0 @@ -21,15 +22,4 @@ def cost ## Hotel Block # I can create a Hotel Block if I give a date range, collection of rooms, and a discounted room rate -# A block can contain a maximum of 5 rooms - -## Hotel Controller -# I want an exception raised if I try to create a Hotel Block and at least one of the rooms is unavailable for the given date range -# Given a specific date, and that a room is set aside in a hotel block for that specific date, I cannot reserve that specific room for that specific date, because it is unavailable -# Given a specific date, and that a room is set aside in a hotel block for that specific date, I cannot create another hotel block that includes that specific room for that specific date, because it is unavailable -# I can check whether a given block has any rooms available -# I can reserve a specific room from a hotel block -# I can only reserve that room from a hotel block for the full duration of the block -# I can see a reservation made from a hotel block from the list of reservations for that date (see wave 1 requirements) -# When a room is reserved from a block of rooms, the reservation dates will always match the date range of the block -# All of the availability checking logic from Wave 2 should now respect room blocks as well as individual reservations \ No newline at end of file +# A block can contain a maximum of 5 rooms \ No newline at end of file diff --git a/lib/hotel_controller.rb b/lib/hotel_controller.rb index 59694a0e0..3c43b4dec 100644 --- a/lib/hotel_controller.rb +++ b/lib/hotel_controller.rb @@ -1,13 +1,14 @@ module Hotel class HotelController # Access the list of all of the rooms in the hotel - attr_reader :rooms, :reservations + attr_reader :rooms, :reservations, :blocks # Constructor # Hotel has 20 rooms, numbered 1 through 20 def initialize @rooms = (1..20).to_a @reservations = [] + @blocks = [] end # Reserve a Room for a Given Date Range @@ -42,10 +43,67 @@ def is_room_available?(start_date, end_date, room) end # Returns List of Rooms Available for a Date Range - # View a list of rooms that are not reserved for a given date range, so that I can see all available rooms for that date range + # Checks also if the room is in a block def available_rooms(start_date, end_date) - return @rooms.select { |room| is_room_available?(start_date, end_date, room) } + no_reservation = @rooms.select { |room| is_room_available?(start_date, end_date, room) } + # Availability checking logic respects room blocks as well as individual reservations + # Given a specific date, and that a room is set aside in a hotel block for that specific date, I cannot reserve that specific room for that specific date, because it is unavailable + no_blocks = no_reservation.select { |room| is_room_unblocked?(start_date, end_date, room) } + return no_blocks end - end -end \ No newline at end of file + ##### Wave 3 + + # Create a Block for a Given Date Range, a Set of Rooms and a Rate + def create_block(start_date, end_date, rooms, rate) + raise ArgumentError, "Invalid room" unless block_rooms_valid?(rooms) + # Exception raised if I try to create a Hotel Block and at least one of the rooms is unavailable for the given date range + raise ArgumentError, "Rooms not available" unless block_rooms_available?(start_date, end_date, rooms) + block = Hotel::Block.new(start_date, end_date, rooms, rate) + @blocks << block + return block + end + + # Create a Reservation from a Block + # Reserve a specific room from a hotel block + def reserve_from_block(block, room) + raise ArgumentError, "Not a block" if block.instance_of?(Hotel::Block) + raise ArgumentError, "Room not in block" unless block.rooms.include?(room) + # Only reserves room from a hotel block for the full duration of the block + # When a room is reserved from a block of rooms, the reservation dates will always match the date range of the block + start_date = block.range.start_date + end_date = block.range.end_date + raise ArgumentError, "No Vacancy" unless is_room_available?(start_date, end_date, room) + reservation = Hotel::Reservation.new(start_date, end_date, room) + # Reservation made from a hotel block adds to the list of reservations for that date + @reservations << reservation + return reservation + end + + # Check if Rooms for a Block are Valid + def block_rooms_valid?(rooms) + + return true + + end + + # Check if the Rooms for a Block are Available in the Date Range + # Given a specific date, and that a room is set aside in a hotel block for that specific date, I cannot create another hotel block that includes that specific room for that specific date, because it is unavailable + def block_rooms_available?(start_date, end_date, rooms) + + return true + end + + # Check if a Room is Not blocked in a Specific Date Range + def is_room_unblocked?(start_date, end_date, room) + + return true + end + + # Checks whether a given block has any rooms available # test to fix this. + def available_rooms_in(block) + return block.rooms + end + + end # class HotelController +end # module Hotel *** add to others \ No newline at end of file diff --git a/test/block_test.rb b/test/block_test.rb index e337a59b1..1f9d0c978 100644 --- a/test/block_test.rb +++ b/test/block_test.rb @@ -25,6 +25,11 @@ expect{ Hotel::Block.new(@start_date, @end_date, 3, @rate) }.must_raise ArgumentError end + it "does not accept zero rooms or more than 5 rooms" do + expect{ Hotel::Block.new(@start_date, @end_date, [], @rate) }.must_raise ArgumentError + expect{ Hotel::Block.new(@start_date, @end_date, [1,2,3,4,5,6], @rate) }.must_raise ArgumentError + end + it "does not accept invalid rates" do # create a block with rate = -1. Must raise argumenterror expect{ Hotel::Block.new(@start_date, @end_date, @rooms, -1) }.must_raise ArgumentError diff --git a/test/hotel_controller_test.rb b/test/hotel_controller_test.rb index 86ef13bd0..04229d110 100644 --- a/test/hotel_controller_test.rb +++ b/test/hotel_controller_test.rb @@ -167,6 +167,123 @@ def test_date(day) end end - end + end # Wave 2 ***add coments to ends + + describe "wave 3" do + # Create a Block for a Given Date Range, a Set of Rooms and a Rate + describe "create_block" do + it "takes a start_date, end_date, rooms, rate" do + + end + + it "returns a Hotel::Block" do + + end + + it "raises and ArgumentError if the rooms are invalid " do + + end + + it "raises and ArgumentError if there are more than 5 rooms" do + + end + + + it "raises and ArgumentError if at least one room in unavailable " do + + end + + it "adds to the block list" do + + end + + end # Create Block + + # Create a Reservation from a Block + # Reserve a specific room from a hotel block + describe "reserve_from_block" do + it "takes a block and a room" do + + end + + it "raises an ArgumentError if the block is invalid" do + + end + + it "raises an ArgumentError if room is not in block" do + + end + + it "reserves room for the full duration of the block" do + + end + + it "raises an ArgumentError if the room is not available" do + + end + + it "returns a reservation" do + end + + it "adds to the list of reservations" do + + end + + end # Reserve from block + + # Check if Rooms for a Block are Valid + describe "block_rooms_valid?" do + it "returns true if the rooms are valid" do + + end + + it "returns false if the rooms are invalid" do + + end + + end + + # Check if the Rooms for a Block are Available in the Date Range + # Given a specific date, and that a room is set aside in a hotel block for that specific date, I cannot create another hotel block that includes that specific room for that specific date, because it is unavailable + describe "block_rooms_available?" do + it "returns true if the rooms are available in that date range" do + + end + + it "returns false if the rooms are not available in that date range" do + + end + end + + # Check if a Room is Not blocked in a Specific Date Range + describe "is_room_unblocked?" do + it "returns true if the room is not blocked in that date range" do + + end + + it "returns false if the room is blocked in that date range" do + + end + end + + # Checks whether a given block has any rooms available # test to fix this. + describe "available_rooms_in(block)" do + it "returns a list of rooms" do + + end + + it "returns all rooms if there are no reservations" do + + end + + it "returns fewer rooms after a reservation is made" do + + end + + it "returns an empty array if no rooms are left" do + + end + end + end # Wave 3 end \ No newline at end of file From 5b59d71d4a7c5c58d8a57c66f9f6fd085362adf1 Mon Sep 17 00:00:00 2001 From: SuelyBarreto Date: Fri, 6 Mar 2020 16:22:48 -0800 Subject: [PATCH 22/37] block_rooms_valid? --- lib/hotel_controller.rb | 6 +- test/hotel_controller_test.rb | 101 +++++++++++++++++----------------- 2 files changed, 53 insertions(+), 54 deletions(-) diff --git a/lib/hotel_controller.rb b/lib/hotel_controller.rb index 3c43b4dec..830a9cea1 100644 --- a/lib/hotel_controller.rb +++ b/lib/hotel_controller.rb @@ -82,9 +82,11 @@ def reserve_from_block(block, room) # Check if Rooms for a Block are Valid def block_rooms_valid?(rooms) - + return false unless rooms.instance_of?(Array) + rooms.each do |room| + return false unless @rooms.include?(room) + end return true - end # Check if the Rooms for a Block are Available in the Date Range diff --git a/test/hotel_controller_test.rb b/test/hotel_controller_test.rb index 04229d110..2254f7054 100644 --- a/test/hotel_controller_test.rb +++ b/test/hotel_controller_test.rb @@ -5,7 +5,7 @@ before do @hotel = Hotel::HotelController.new - def test_date(day) + def dt(day) return Date.new(2020, 01, day) end end @@ -29,24 +29,24 @@ def test_date(day) describe "reserve_room" do it "takes two Date objects and returns a Reservation" do - reservation = @hotel.reserve_room(test_date(1), test_date(4)) + reservation = @hotel.reserve_room(dt(1), dt(4)) expect(reservation).must_be_kind_of Hotel::Reservation end it "adds to the list of reservations" do - @hotel.reserve_room(test_date(1), test_date(4)) + @hotel.reserve_room(dt(1), dt(4)) expect(@hotel.reservations.size).must_equal 1 - @hotel.reserve_room(test_date(1), test_date(4)) + @hotel.reserve_room(dt(1), dt(4)) expect(@hotel.reservations.size).must_equal 2 end it "selects the first available room" do # reserve a room, than check if the reservation room equals to 1 - reservation1 = @hotel.reserve_room(test_date(1), test_date(4)) + reservation1 = @hotel.reserve_room(dt(1), dt(4)) expect(reservation1.room).must_equal 1 expect(@hotel.reservations.last.room).must_equal 1 # reserve another room, than check if the reservation room equals 2 - reservation2 = @hotel.reserve_room(test_date(1), test_date(4)) + reservation2 = @hotel.reserve_room(dt(1), dt(4)) expect(reservation2.room).must_equal 2 expect(@hotel.reservations.last.room).must_equal 2 end @@ -54,32 +54,32 @@ def test_date(day) it "returns error if no rooms are available" do # reserve 20 rooms (20.times). The 21st should return an error. (1..20).each do - @hotel.reserve_room(test_date(1), test_date(4)) + @hotel.reserve_room(dt(1), dt(4)) end expect(@hotel.reservations.size).must_equal 20 - expect{ @hotel.reserve_room(test_date(1), test_date(4)) }.must_raise ArgumentError + expect{ @hotel.reserve_room(dt(1), dt(4)) }.must_raise ArgumentError # it's ok to create more reservations in other date ranges - @hotel.reserve_room(test_date(15), test_date(20)) + @hotel.reserve_room(dt(15), dt(20)) expect(@hotel.reservations.size).must_equal 21 end it "does not accept the same date for start and end" do # create a reservation from 2020/01/05 to 2020/01/05. It should raise an exception - expect{ @hotel.reserve_room(test_date(5), test_date(5)) }.must_raise ArgumentError + expect{ @hotel.reserve_room(dt(5), dt(5)) }.must_raise ArgumentError end it "does not accept a start date after the end date" do # create a reservation from 2020/01/05 to 2020/01/01. It should raise an exception - expect{ @hotel.reserve_room(test_date(5), test_date(1)) }.must_raise ArgumentError + expect{ @hotel.reserve_room(dt(5), dt(1)) }.must_raise ArgumentError end end # access the list of reservations for a specific date, so that I can track reservations by date describe "reservations_by_date" do it "takes a Date and returns a list of Reservations" do - @hotel.reserve_room(test_date(1), test_date(4)) - @hotel.reserve_room(test_date(1), test_date(4)) - reservations_ondate = @hotel.reservations_by_date(test_date(1)) + @hotel.reserve_room(dt(1), dt(4)) + @hotel.reserve_room(dt(1), dt(4)) + reservations_ondate = @hotel.reservations_by_date(dt(1)) expect(reservations_ondate).must_be_kind_of Array expect(reservations_ondate.size).must_equal 2 @@ -91,38 +91,38 @@ def test_date(day) it "returns the reservation when you ask for the start date" do # create a reservation from 2020/01/01 to 2020/01/04, than get a list of reservations for 2020/01/01, should return 1 - @hotel.reserve_room(test_date(1), test_date(4)) - reservations_ondate = @hotel.reservations_by_date(test_date(1)) + @hotel.reserve_room(dt(1), dt(4)) + reservations_ondate = @hotel.reservations_by_date(dt(1)) expect(reservations_ondate.size).must_equal 1 # create another reservation from 2020/01/01 to 2020/01/05, than get a list of reservations for 2020/01/01, should return 2 - @hotel.reserve_room(test_date(1), test_date(5)) - reservations_ondate = @hotel.reservations_by_date(test_date(1)) + @hotel.reserve_room(dt(1), dt(5)) + reservations_ondate = @hotel.reservations_by_date(dt(1)) expect(reservations_ondate.size).must_equal 2 # create another reservation from 2020/01/05 to 2020/01/10, than get a list of reservations for 2020/01/05, should return 1 - @hotel.reserve_room(test_date(5), test_date(10)) - reservations_ondate = @hotel.reservations_by_date(test_date(5)) + @hotel.reserve_room(dt(5), dt(10)) + reservations_ondate = @hotel.reservations_by_date(dt(5)) expect(reservations_ondate.size).must_equal 1 end it "returns the reservation when you ask for a date in the middle" do # create a reservation from 2020/01/01 to 2020/01/10, than get a list of reservations for 2020/01/05. It should return 1 - @hotel.reserve_room(test_date(1), test_date(10)) - expect(@hotel.reservations_by_date(test_date(5)).size).must_equal 1 + @hotel.reserve_room(dt(1), dt(10)) + expect(@hotel.reservations_by_date(dt(5)).size).must_equal 1 # create a reservation from 2020/01/02 to 2020/01/15, than get a list of reservations for 2020/01/05. It should return 2 - @hotel.reserve_room(test_date(2), test_date(15)) - expect(@hotel.reservations_by_date(test_date(5)).size).must_equal 2 + @hotel.reserve_room(dt(2), dt(15)) + expect(@hotel.reservations_by_date(dt(5)).size).must_equal 2 # create a reservation from 2020/01/10 to 2020/01/15, than get a list of reservations for 2020/01/05. It should return 2 - @hotel.reserve_room(test_date(10), test_date(15)) - expect(@hotel.reservations_by_date(test_date(5)).size).must_equal 2 + @hotel.reserve_room(dt(10), dt(15)) + expect(@hotel.reservations_by_date(dt(5)).size).must_equal 2 end it "does not return a reservation when you ask for the end date" do # create a reservation from 2020/01/01 to 2020/01/10, than get a list of reservations for 2020/01/10. It should return 0 - @hotel.reserve_room(test_date(1), test_date(10)) - expect(@hotel.reservations_by_date(test_date(10)).size).must_equal 0 + @hotel.reserve_room(dt(1), dt(10)) + expect(@hotel.reservations_by_date(dt(10)).size).must_equal 0 # create a reservation from 2020/01/05 to 2020/01/10, than get a list of reservations for 2020/01/10. It should return 0 - @hotel.reserve_room(test_date(5), test_date(10)) - expect(@hotel.reservations_by_date(test_date(10)).size).must_equal 0 + @hotel.reserve_room(dt(5), dt(10)) + expect(@hotel.reservations_by_date(dt(10)).size).must_equal 0 end end @@ -132,38 +132,38 @@ def test_date(day) describe "wave 2" do describe "available_rooms" do it "takes two dates and returns a list" do - room_list = @hotel.available_rooms(test_date(1), test_date(4)) + room_list = @hotel.available_rooms(dt(1), dt(4)) expect(room_list).must_be_kind_of Array end it "removes a room after it is reserved" do - @hotel.reserve_room(test_date(1), test_date(5)) - expect(@hotel.available_rooms(test_date(1), test_date(5)).size).must_equal 19 - @hotel.reserve_room(test_date(1), test_date(5)) - expect(@hotel.available_rooms(test_date(1), test_date(5)).size).must_equal 18 + @hotel.reserve_room(dt(1), dt(5)) + expect(@hotel.available_rooms(dt(1), dt(5)).size).must_equal 19 + @hotel.reserve_room(dt(1), dt(5)) + expect(@hotel.available_rooms(dt(1), dt(5)).size).must_equal 18 end it "does not show the room available if there is an overlaping reservation" do # create a reservation from 2020/01/05 to 2020/01/15. - @hotel.reserve_room(test_date(5), test_date(15)) + @hotel.reserve_room(dt(5), dt(15)) # Check overlap with same range - expect(@hotel.available_rooms(test_date(5), test_date(15)).size).must_equal 19 + expect(@hotel.available_rooms(dt(5), dt(15)).size).must_equal 19 # Check overlap in the back - expect(@hotel.available_rooms(test_date(10), test_date(20)).size).must_equal 19 + expect(@hotel.available_rooms(dt(10), dt(20)).size).must_equal 19 # Check overlap at the front - expect(@hotel.available_rooms(test_date(1), test_date(10)).size).must_equal 19 + expect(@hotel.available_rooms(dt(1), dt(10)).size).must_equal 19 # Check overlap fully contained - expect(@hotel.available_rooms(test_date(7), test_date(12)).size).must_equal 19 + expect(@hotel.available_rooms(dt(7), dt(12)).size).must_equal 19 # Check overlap fully containing - expect(@hotel.available_rooms(test_date(1), test_date(20)).size).must_equal 19 + expect(@hotel.available_rooms(dt(1), dt(20)).size).must_equal 19 # Check no overlap fully before - expect(@hotel.available_rooms(test_date(1), test_date(3)).size).must_equal 20 + expect(@hotel.available_rooms(dt(1), dt(3)).size).must_equal 20 # Check no overlap fully after - expect(@hotel.available_rooms(test_date(17), test_date(20)).size).must_equal 20 + expect(@hotel.available_rooms(dt(17), dt(20)).size).must_equal 20 # Check no overlap on checkout date after - expect(@hotel.available_rooms(test_date(15), test_date(20)).size).must_equal 20 + expect(@hotel.available_rooms(dt(15), dt(20)).size).must_equal 20 # Check no overlap on checkout date before - expect(@hotel.available_rooms(test_date(1), test_date(5)).size).must_equal 20 + expect(@hotel.available_rooms(dt(1), dt(5)).size).must_equal 20 end end @@ -172,16 +172,13 @@ def test_date(day) describe "wave 3" do # Create a Block for a Given Date Range, a Set of Rooms and a Rate describe "create_block" do - it "takes a start_date, end_date, rooms, rate" do - - end - - it "returns a Hotel::Block" do - + it "takes start/end/rooms/rate and returns Block" do + block = @hotel.create_block(dt(1), dt(4), [3,4,5], 150) + expect(block).must_be_kind_of Hotel::Block end it "raises and ArgumentError if the rooms are invalid " do - + expect{ @hotel.create_block(dt(1), dt(4), [1, 10, 18, 20, 21], 150) }.must_raise ArgumentError end it "raises and ArgumentError if there are more than 5 rooms" do From 556539101700d911a1bda7407afb77b6917ccf24 Mon Sep 17 00:00:00 2001 From: SuelyBarreto Date: Fri, 6 Mar 2020 17:21:25 -0800 Subject: [PATCH 23/37] create block tests and code --- lib/hotel_controller.rb | 16 +++++++++++++--- test/hotel_controller_test.rb | 31 +++++++++++++++++++++++++------ 2 files changed, 38 insertions(+), 9 deletions(-) diff --git a/lib/hotel_controller.rb b/lib/hotel_controller.rb index 830a9cea1..6f5590bfc 100644 --- a/lib/hotel_controller.rb +++ b/lib/hotel_controller.rb @@ -92,14 +92,24 @@ def block_rooms_valid?(rooms) # Check if the Rooms for a Block are Available in the Date Range # Given a specific date, and that a room is set aside in a hotel block for that specific date, I cannot create another hotel block that includes that specific room for that specific date, because it is unavailable def block_rooms_available?(start_date, end_date, rooms) - + rooms.each do |room| + return false unless is_room_available?(start_date, end_date, room) + return false unless is_room_unblocked?(start_date, end_date, room) + end return true end # Check if a Room is Not blocked in a Specific Date Range def is_room_unblocked?(start_date, end_date, room) - - return true + return blocks_by_room(start_date, end_date, room).size == 0 + end + + # Returns a list of blocks for that Room in the Date Range + def blocks_by_room(start_date, end_date, room) + range = Hotel::DateRange.new(start_date, end_date) + return @blocks.select do |block| # which blocks have an overlap with the ranged passed + block.range.overlap?(range) && block.rooms.include?(room) + end end # Checks whether a given block has any rooms available # test to fix this. diff --git a/test/hotel_controller_test.rb b/test/hotel_controller_test.rb index 2254f7054..0f420cbd7 100644 --- a/test/hotel_controller_test.rb +++ b/test/hotel_controller_test.rb @@ -173,7 +173,7 @@ def dt(day) # Create a Block for a Given Date Range, a Set of Rooms and a Rate describe "create_block" do it "takes start/end/rooms/rate and returns Block" do - block = @hotel.create_block(dt(1), dt(4), [3,4,5], 150) + block = @hotel.create_block(dt(1), dt(4), [3, 4, 5], 150) expect(block).must_be_kind_of Hotel::Block end @@ -181,17 +181,36 @@ def dt(day) expect{ @hotel.create_block(dt(1), dt(4), [1, 10, 18, 20, 21], 150) }.must_raise ArgumentError end - it "raises and ArgumentError if there are more than 5 rooms" do - + it "raises and ArgumentError if more than 5 rooms" do + expect{ @hotel.create_block(dt(1), dt(4), [1, 2, 3, 4, 5, 6], 150) }.must_raise ArgumentError end - - it "raises and ArgumentError if at least one room in unavailable " do - + it "raises and ArgumentError if rooms is an empty array" do + expect{ @hotel.create_block(dt(1), dt(4), [], 150) }.must_raise ArgumentError + end + + it "raises and ArgumentError if room is unavailable " do + # Create a reservation and try to block a room that is already reserved + @hotel.reserve_room(dt(1), dt(4)) # this will reserve room number 1 + expect{ @hotel.create_block(dt(1), dt(4), [1, 2, 3], 150) }.must_raise ArgumentError + # Create a block and try to block a room that is already blocked (in another block) + @hotel.create_block(dt(1), dt(4), [2, 3, 4], 150) + expect{ @hotel.create_block(dt(1), dt(4), [4, 5, 6], 150) }.must_raise ArgumentError + expect{ @hotel.create_block(dt(2), dt(3), [4, 5, 6], 150) }.must_raise ArgumentError end it "adds to the block list" do + @hotel.create_block(dt(1), dt(4), [3, 4, 5], 150) + expect(@hotel.blocks.size).must_equal 1 + @hotel.create_block(dt(1), dt(4), [6, 7, 8], 150) + @hotel.create_block(dt(4), dt(8), [6, 7, 8], 150) + expect(@hotel.blocks.size).must_equal 3 + end + it "blocks reservations that conflict with a block" do # improve... + @hotel.create_block(dt(1), dt(4), [1, 2, 3], 150) + reservation = @hotel.reserve_room(dt(1), dt(4)) # this should reserve room number 4 instead of 1 + expect(reservation.room).must_equal 4 end end # Create Block From e36c6ffaab2e03a588c219e0cfe0bfa025e4a562 Mon Sep 17 00:00:00 2001 From: SuelyBarreto Date: Fri, 6 Mar 2020 19:17:17 -0800 Subject: [PATCH 24/37] reserve_from_block tests and code --- lib/hotel_controller.rb | 2 +- test/hotel_controller_test.rb | 47 ++++++++++++++++++++++++----------- 2 files changed, 33 insertions(+), 16 deletions(-) diff --git a/lib/hotel_controller.rb b/lib/hotel_controller.rb index 6f5590bfc..ba6ac9612 100644 --- a/lib/hotel_controller.rb +++ b/lib/hotel_controller.rb @@ -67,7 +67,7 @@ def create_block(start_date, end_date, rooms, rate) # Create a Reservation from a Block # Reserve a specific room from a hotel block def reserve_from_block(block, room) - raise ArgumentError, "Not a block" if block.instance_of?(Hotel::Block) + raise ArgumentError, "Not a block" unless block.instance_of?(Hotel::Block) raise ArgumentError, "Room not in block" unless block.rooms.include?(room) # Only reserves room from a hotel block for the full duration of the block # When a room is reserved from a block of rooms, the reservation dates will always match the date range of the block diff --git a/test/hotel_controller_test.rb b/test/hotel_controller_test.rb index 0f420cbd7..3c33c6c6a 100644 --- a/test/hotel_controller_test.rb +++ b/test/hotel_controller_test.rb @@ -41,11 +41,12 @@ def dt(day) end it "selects the first available room" do - # reserve a room, than check if the reservation room equals to 1 + # Reserve a room, than check if the reservation room equals to 1 reservation1 = @hotel.reserve_room(dt(1), dt(4)) expect(reservation1.room).must_equal 1 expect(@hotel.reservations.last.room).must_equal 1 - # reserve another room, than check if the reservation room equals 2 + + # Reserve another room, than check if the reservation room equals 2 reservation2 = @hotel.reserve_room(dt(1), dt(4)) expect(reservation2.room).must_equal 2 expect(@hotel.reservations.last.room).must_equal 2 @@ -74,7 +75,7 @@ def dt(day) end end - # access the list of reservations for a specific date, so that I can track reservations by date + # access the list of reservations for a specific date to track reservations by date describe "reservations_by_date" do it "takes a Date and returns a list of Reservations" do @hotel.reserve_room(dt(1), dt(4)) @@ -215,35 +216,51 @@ def dt(day) end # Create Block - # Create a Reservation from a Block - # Reserve a specific room from a hotel block describe "reserve_from_block" do - it "takes a block and a room" do + # Create a Reservation from a Block + # Reserve a specific room from a hotel block + it "takes a block and a room. Returns a reservation" do + block = @hotel.create_block(dt(1), dt(4), [1, 2, 3], 150) + reservation = @hotel.reserve_from_block(block, 2) + expect(reservation).must_be_kind_of Hotel::Reservation end + # trying to reserve from block without having a block it "raises an ArgumentError if the block is invalid" do - + expect{ @hotel.reserve_from_block(nil, 2) }.must_raise ArgumentError + expect{ @hotel.reserve_from_block(1, 2) }.must_raise ArgumentError + expect{ @hotel.reserve_from_block(dt(1), 2) }.must_raise ArgumentError end it "raises an ArgumentError if room is not in block" do - + block = @hotel.create_block(dt(1), dt(4), [1, 2, 3], 150) + expect{ @hotel.reserve_from_block(block, 4) }.must_raise ArgumentError end it "reserves room for the full duration of the block" do - + block = @hotel.create_block(dt(1), dt(4), [1, 2, 3], 150) + reservation = @hotel.reserve_from_block(block, 2) + expect(reservation.range.start_date).must_equal block.range.start_date + expect(reservation.range.end_date).must_equal block.range.end_date end it "raises an ArgumentError if the room is not available" do - - end - - it "returns a reservation" do - + block = @hotel.create_block(dt(1), dt(4), [1, 2, 3], 150) + @hotel.reserve_from_block(block, 2) + expect{ @hotel.reserve_from_block(block, 2) }.must_raise ArgumentError end it "adds to the list of reservations" do - + block = @hotel.create_block(dt(1), dt(4), [1, 2, 3], 150) + @hotel.reserve_from_block(block, 2) + expect(@hotel.reservations.size).must_equal 1 + reservation = @hotel.reserve_room(dt(1), dt(4)) + expect(@hotel.reservations.size).must_equal 2 + @hotel.reserve_from_block(block, 1) + expect(@hotel.reservations.size).must_equal 3 + @hotel.reserve_from_block(block, 3) + expect(@hotel.reservations.size).must_equal 4 end end # Reserve from block From f12aae3b9e8d5cfa05d41f8d9f4a922fa60eb5a7 Mon Sep 17 00:00:00 2001 From: SuelyBarreto Date: Fri, 6 Mar 2020 19:50:05 -0800 Subject: [PATCH 25/37] rooms_valid? --- lib/hotel_controller.rb | 7 ++++--- test/hotel_controller_test.rb | 16 ++++++++++++---- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/lib/hotel_controller.rb b/lib/hotel_controller.rb index ba6ac9612..f048094a6 100644 --- a/lib/hotel_controller.rb +++ b/lib/hotel_controller.rb @@ -56,7 +56,7 @@ def available_rooms(start_date, end_date) # Create a Block for a Given Date Range, a Set of Rooms and a Rate def create_block(start_date, end_date, rooms, rate) - raise ArgumentError, "Invalid room" unless block_rooms_valid?(rooms) + raise ArgumentError, "Invalid room" unless rooms_valid?(rooms) # Exception raised if I try to create a Hotel Block and at least one of the rooms is unavailable for the given date range raise ArgumentError, "Rooms not available" unless block_rooms_available?(start_date, end_date, rooms) block = Hotel::Block.new(start_date, end_date, rooms, rate) @@ -80,9 +80,10 @@ def reserve_from_block(block, room) return reservation end - # Check if Rooms for a Block are Valid - def block_rooms_valid?(rooms) + # Check if Rooms are Valid + def rooms_valid?(rooms) return false unless rooms.instance_of?(Array) + return false if rooms.size == 0 rooms.each do |room| return false unless @rooms.include?(room) end diff --git a/test/hotel_controller_test.rb b/test/hotel_controller_test.rb index 3c33c6c6a..67d6602fb 100644 --- a/test/hotel_controller_test.rb +++ b/test/hotel_controller_test.rb @@ -266,14 +266,22 @@ def dt(day) end # Reserve from block # Check if Rooms for a Block are Valid - describe "block_rooms_valid?" do + describe "rooms_valid?" do it "returns true if the rooms are valid" do - + expect(@hotel.rooms_valid?([1, 2, 3])).must_equal true + expect(@hotel.rooms_valid?([18, 19, 20])).must_equal true + expect(@hotel.rooms_valid?([1])).must_equal true + expect(@hotel.rooms_valid?([1, 2, 3, 4, 5, 20])).must_equal true + expect(@hotel.rooms_valid?((1..20).to_a)).must_equal true end it "returns false if the rooms are invalid" do - - end + expect(@hotel.rooms_valid?([])).must_equal false + expect(@hotel.rooms_valid?(nil)).must_equal false + expect(@hotel.rooms_valid?(["1, 2, 3"])).must_equal false + expect(@hotel.rooms_valid?([18, 19, 20, 21])).must_equal false + expect(@hotel.rooms_valid?(1..20)).must_equal false + end end From 41ed098ae75126443d33d520cdace6d0c698b16b Mon Sep 17 00:00:00 2001 From: SuelyBarreto Date: Fri, 6 Mar 2020 20:11:32 -0800 Subject: [PATCH 26/37] block_rooms_available? --- test/hotel_controller_test.rb | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/test/hotel_controller_test.rb b/test/hotel_controller_test.rb index 67d6602fb..590acccb5 100644 --- a/test/hotel_controller_test.rb +++ b/test/hotel_controller_test.rb @@ -289,11 +289,21 @@ def dt(day) # Given a specific date, and that a room is set aside in a hotel block for that specific date, I cannot create another hotel block that includes that specific room for that specific date, because it is unavailable describe "block_rooms_available?" do it "returns true if the rooms are available in that date range" do - + @hotel.reserve_room(dt(1), dt(4)) # this will reserve room number 1 + expect(@hotel.block_rooms_available?(dt(1), dt(4), [2, 3, 4])).must_equal true # different rooms + expect(@hotel.block_rooms_available?(dt(4), dt(8), [1, 2, 3])).must_equal true # different dates + @hotel.create_block(dt(1), dt(4), [2, 3, 4], 150) + expect(@hotel.block_rooms_available?(dt(1), dt(4), [5, 6, 7])).must_equal true # different rooms + expect(@hotel.block_rooms_available?(dt(4), dt(8), [1, 2, 3, 4])).must_equal true # different dates end it "returns false if the rooms are not available in that date range" do - + @hotel.reserve_room(dt(1), dt(4)) # this will reserve room number 1 + expect(@hotel.block_rooms_available?(dt(1), dt(4), [1, 2, 3])).must_equal false # room 1 in both + expect(@hotel.block_rooms_available?(dt(3), dt(6), [1, 2, 3])).must_equal false # overlapping dates + @hotel.create_block(dt(1), dt(4), [2, 3, 4], 150) + expect(@hotel.block_rooms_available?(dt(1), dt(4), [4, 5, 6])).must_equal false # room 4 in both + expect(@hotel.block_rooms_available?(dt(3), dt(6), [2, 3, 4])).must_equal false # overlapping dates end end From 10001c1429ff16670b7536705d07ef5922f3c3c2 Mon Sep 17 00:00:00 2001 From: SuelyBarreto Date: Fri, 6 Mar 2020 20:18:15 -0800 Subject: [PATCH 27/37] is_room_unblocked? --- test/hotel_controller_test.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/test/hotel_controller_test.rb b/test/hotel_controller_test.rb index 590acccb5..0b6998e99 100644 --- a/test/hotel_controller_test.rb +++ b/test/hotel_controller_test.rb @@ -310,11 +310,15 @@ def dt(day) # Check if a Room is Not blocked in a Specific Date Range describe "is_room_unblocked?" do it "returns true if the room is not blocked in that date range" do - + @hotel.create_block(dt(1), dt(4), [1, 2, 3, 4], 150) + expect(@hotel.is_room_unblocked?(dt(1), dt(4), 5)).must_equal true # different room + expect(@hotel.is_room_unblocked?(dt(4), dt(8), 1)).must_equal true # different dates end it "returns false if the room is blocked in that date range" do - + @hotel.create_block(dt(1), dt(4), [1, 2, 3, 4], 150) + expect(@hotel.is_room_unblocked?(dt(1), dt(4), 3)).must_equal false # room 3 is both + expect(@hotel.is_room_unblocked?(dt(3), dt(8), 3)).must_equal false # overlapping dates end end From 89d697b3f3534092474b5ea135cc8db8dee4df76 Mon Sep 17 00:00:00 2001 From: SuelyBarreto Date: Fri, 6 Mar 2020 20:32:50 -0800 Subject: [PATCH 28/37] available_rooms_in(block) test --- test/hotel_controller_test.rb | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/test/hotel_controller_test.rb b/test/hotel_controller_test.rb index 0b6998e99..333bff425 100644 --- a/test/hotel_controller_test.rb +++ b/test/hotel_controller_test.rb @@ -325,19 +325,33 @@ def dt(day) # Checks whether a given block has any rooms available # test to fix this. describe "available_rooms_in(block)" do it "returns a list of rooms" do - - end + block = @hotel.create_block(dt(1), dt(4), [1, 2, 3, 4], 150) + available = @hotel.available_rooms_in(block) + expect(available).must_be_kind_of Array + expect(@hotel.rooms_valid?(available)).must_equal true + end it "returns all rooms if there are no reservations" do - + block = @hotel.create_block(dt(1), dt(4), [1, 2, 3, 4], 150) + available = @hotel.available_rooms_in(block) + expect(available).must_equal [1, 2, 3, 4] end it "returns fewer rooms after a reservation is made" do - + block = @hotel.create_block(dt(1), dt(4), [1, 2, 3, 4], 150) + @hotel.reserve_from_block(block, 1) + @hotel.reserve_from_block(block, 3) + available = @hotel.available_rooms_in(block) + expect(available).must_equal [2, 4] end it "returns an empty array if no rooms are left" do - + block = @hotel.create_block(dt(1), dt(4), [1, 2, 3, 4], 150) + (1..4).each do |room| + @hotel.reserve_from_block(block, room) + end + available = @hotel.available_rooms_in(block) + expect(available.size).must_equal 0 end end end # Wave 3 From 5359065a22b19e3ca783bb5b116f9b02ec3b193e Mon Sep 17 00:00:00 2001 From: SuelyBarreto Date: Fri, 6 Mar 2020 20:42:13 -0800 Subject: [PATCH 29/37] wave 3 code completed, all test passed --- lib/hotel_controller.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/hotel_controller.rb b/lib/hotel_controller.rb index f048094a6..357023356 100644 --- a/lib/hotel_controller.rb +++ b/lib/hotel_controller.rb @@ -113,9 +113,9 @@ def blocks_by_room(start_date, end_date, room) end end - # Checks whether a given block has any rooms available # test to fix this. + # Checks whether a given block has any rooms available def available_rooms_in(block) - return block.rooms + return block.rooms.select { |room| is_room_available?(block.range.start_date, block.range.end_date, room)} end end # class HotelController From 65f1024e9f7cdbece48b1bd6db6b90f710e22239 Mon Sep 17 00:00:00 2001 From: SuelyBarreto Date: Sat, 7 Mar 2020 07:45:46 -0800 Subject: [PATCH 30/37] fixing comments and indentation for date range tests and code --- lib/date_range.rb | 11 ++++++----- test/date_range_test.rb | 8 ++++---- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/lib/date_range.rb b/lib/date_range.rb index b7f9ec819..8346f366f 100644 --- a/lib/date_range.rb +++ b/lib/date_range.rb @@ -1,10 +1,11 @@ module Hotel class DateRange + # Generator attr_accessor :start_date, :end_date # Constructor - # Raise exception when an invalid date range is provided def initialize(start_date, end_date) + # Raise an error when an invalid date range is provided unless start_date.instance_of?(Date) && end_date.instance_of?(Date) raise ArgumentError, "Parameters must be of class Date" end @@ -15,7 +16,7 @@ def initialize(start_date, end_date) @end_date = end_date end - # Check Range Overlap + # Check range overlap def overlap?(other) unless other.instance_of?(Hotel::DateRange) raise ArgumentError, "Parameters must be of class Hotel::DateRange" @@ -24,7 +25,7 @@ def overlap?(other) return other.start_date < @end_date && other.end_date > @start_date end - # Check if a Date is Included in the DateRange + # Check if a date is included in the date range def include?(date) unless date.instance_of?(Date) raise ArgumentError, "Parameters must be of class Date" @@ -33,8 +34,8 @@ def include?(date) return date >= @start_date && date < @end_date end - # Calculate Number of Nights - # Last day is the checkout day, guest shouldn't be charged for that day/night + # Calculate number of nights + # Checkout day not to be charged def nights return (@end_date - @start_date).to_i end diff --git a/test/date_range_test.rb b/test/date_range_test.rb index 0ce73e41d..04d857bf4 100644 --- a/test/date_range_test.rb +++ b/test/date_range_test.rb @@ -43,7 +43,7 @@ def simple_range(start_day, end_day) end end - it "raises an ArgumentError if parameter is not a range" do + it "raises an ArgumentError if parameter not a range" do range1 = simple_range(01, 05) range2 = Date.new(2017, 01, 01) expect{ range1.overlap?(range2) }.must_raise ArgumentError @@ -79,13 +79,13 @@ def simple_range(start_day, end_day) expect(range1.overlap?(range2)).must_equal true end - it "returns false for a range starting on the end_date date" do + it "returns false if range starts on end_date date" do range1 = simple_range(01, 05) range2 = simple_range(05, 10) expect(range1.overlap?(range2)).must_equal false end - it "returns false for a range ending on the start_date date" do + it "returns false if range starts on start_date date" do range1 = simple_range(05, 10) range2 = simple_range(01, 05) expect(range1.overlap?(range2)).must_equal false @@ -112,7 +112,7 @@ def simple_range(start_day, end_day) @range = Hotel::DateRange.new(start_date, end_date) end - it "raises an ArgumentError if parameter is not a date" do + it "raises an ArgumentError if parameter not a date" do expect{ @range.include?("2018/01/01") }.must_raise ArgumentError end From 1cfc58a9ccb3cc7408d76631a2957c60ce59da88 Mon Sep 17 00:00:00 2001 From: SuelyBarreto Date: Sat, 7 Mar 2020 08:18:34 -0800 Subject: [PATCH 31/37] fixing comments and indentation for reservation tests and codes --- lib/reservation.rb | 1 + test/reservation_test.rb | 10 +++++----- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/reservation.rb b/lib/reservation.rb index 8ccdd33b6..d00203995 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -1,5 +1,6 @@ module Hotel class Reservation + # Generator attr_reader :range, :room ROOM_COST = 200 diff --git a/test/reservation_test.rb b/test/reservation_test.rb index 09079b694..eac2fc561 100644 --- a/test/reservation_test.rb +++ b/test/reservation_test.rb @@ -25,10 +25,10 @@ expect{ reservation = Hotel::Reservation.new(start_date, end_date1, room) }.must_raise ArgumentError # date can not be a string expect{ reservation = Hotel::Reservation.new(start_date, end_date2, room) }.must_raise ArgumentError - # start date can not be the same as end date + # start date can not be same as end date expect{ reservation = Hotel::Reservation.new(start_date, start_date, room) }.must_raise ArgumentError end - end + end # describe "constructor" describe "cost" do before do @@ -43,9 +43,9 @@ end it "calculates the cost" do - # Reservation - Every room is identical and a room always costs $200/night + # Rooms identical and cost $200/night expect(@reservation.cost).must_equal 600 end - end + end # describe "cost" -end \ No newline at end of file +end # describe "Hotel::Reservation" \ No newline at end of file From cb368bdcbf463694a2dc9a41fdd81bd547c620b7 Mon Sep 17 00:00:00 2001 From: SuelyBarreto Date: Sat, 7 Mar 2020 10:33:42 -0800 Subject: [PATCH 32/37] fixing comments and indentation for hotel controller code --- lib/hotel_controller.rb | 51 +++++++++++++++++++++-------------------- 1 file changed, 26 insertions(+), 25 deletions(-) diff --git a/lib/hotel_controller.rb b/lib/hotel_controller.rb index 357023356..00213137d 100644 --- a/lib/hotel_controller.rb +++ b/lib/hotel_controller.rb @@ -1,18 +1,20 @@ module Hotel + class HotelController - # Access the list of all of the rooms in the hotel + # Generator + # Access list of all rooms in hotel attr_reader :rooms, :reservations, :blocks # Constructor - # Hotel has 20 rooms, numbered 1 through 20 + # Hotel has 20 rooms, 1 through 20 def initialize @rooms = (1..20).to_a @reservations = [] @blocks = [] end - # Reserve a Room for a Given Date Range - # Given only start and end dates, determine which room to use for the reservation + # Reserve a room for a given date range + # Determine which room to reserve given start and end dates def reserve_room(start_date, end_date) room = available_rooms(start_date, end_date).first raise ArgumentError, "No Vacancy" if room == nil @@ -21,55 +23,53 @@ def reserve_room(start_date, end_date) return reservation end - # Access the list of reservations for a specific date, so that I can track reservations by date + # Access list of reservations for a specific date to track reservations by date def reservations_by_date(date) return @reservations.select do |reservation| reservation.range.include?(date) end end - # Returns Reservation(s) Specific for Room and Range - # Access the list of reservations for a specified room and a given date range + # Returns list of reservations for specific room and given date range def reservations_by_room(start_date, end_date, room) range = Hotel::DateRange.new(start_date, end_date) - return @reservations.select do |reservation| # which reservations have an overlap with the ranged passed + return @reservations.select do |reservation| # which reservations have an overlap with range passed reservation.range.overlap?(range) && reservation.room == room end end - # Returns Specific Room Available for a Date Range + # Returns specific room available for date range def is_room_available?(start_date, end_date, room) return reservations_by_room(start_date, end_date, room).size == 0 end - # Returns List of Rooms Available for a Date Range - # Checks also if the room is in a block + # Returns list of rooms available for date range + # Checks if the room is in a block. Respects room blocks as well as individual reservations def available_rooms(start_date, end_date) no_reservation = @rooms.select { |room| is_room_available?(start_date, end_date, room) } - # Availability checking logic respects room blocks as well as individual reservations - # Given a specific date, and that a room is set aside in a hotel block for that specific date, I cannot reserve that specific room for that specific date, because it is unavailable - no_blocks = no_reservation.select { |room| is_room_unblocked?(start_date, end_date, room) } + # Cannot reserve specific room, for specific date, if room already in a block for that specific date + no_blocks = no_reservation.select { |room| is_room_unblocked?(start_date, end_date, room) } return no_blocks end - ##### Wave 3 + # Wave 3 - # Create a Block for a Given Date Range, a Set of Rooms and a Rate + # Create a block for given date range, set of rooms and rate def create_block(start_date, end_date, rooms, rate) raise ArgumentError, "Invalid room" unless rooms_valid?(rooms) - # Exception raised if I try to create a Hotel Block and at least one of the rooms is unavailable for the given date range + # Exception raised if try to create a block and at least one of the rooms is unavailable for given date range raise ArgumentError, "Rooms not available" unless block_rooms_available?(start_date, end_date, rooms) block = Hotel::Block.new(start_date, end_date, rooms, rate) @blocks << block return block end - # Create a Reservation from a Block + # Create a reservation from a block # Reserve a specific room from a hotel block def reserve_from_block(block, room) raise ArgumentError, "Not a block" unless block.instance_of?(Hotel::Block) raise ArgumentError, "Room not in block" unless block.rooms.include?(room) - # Only reserves room from a hotel block for the full duration of the block + # Only reserves a room from block for the full duration of the block # When a room is reserved from a block of rooms, the reservation dates will always match the date range of the block start_date = block.range.start_date end_date = block.range.end_date @@ -80,7 +80,7 @@ def reserve_from_block(block, room) return reservation end - # Check if Rooms are Valid + # Check if rooms are valid def rooms_valid?(rooms) return false unless rooms.instance_of?(Array) return false if rooms.size == 0 @@ -90,8 +90,8 @@ def rooms_valid?(rooms) return true end - # Check if the Rooms for a Block are Available in the Date Range - # Given a specific date, and that a room is set aside in a hotel block for that specific date, I cannot create another hotel block that includes that specific room for that specific date, because it is unavailable + # Check if rooms for a block are available in date range + # Cannot create another hotel block for specific date and room if room already in another block def block_rooms_available?(start_date, end_date, rooms) rooms.each do |room| return false unless is_room_available?(start_date, end_date, room) @@ -100,15 +100,15 @@ def block_rooms_available?(start_date, end_date, rooms) return true end - # Check if a Room is Not blocked in a Specific Date Range + # Check if a room is not blocked in a specific date range def is_room_unblocked?(start_date, end_date, room) return blocks_by_room(start_date, end_date, room).size == 0 end - # Returns a list of blocks for that Room in the Date Range + # Returns a list of blocks for that room in the date range def blocks_by_room(start_date, end_date, room) range = Hotel::DateRange.new(start_date, end_date) - return @blocks.select do |block| # which blocks have an overlap with the ranged passed + return @blocks.select do |block| # which blocks have an overlap with the range passed block.range.overlap?(range) && block.rooms.include?(room) end end @@ -119,4 +119,5 @@ def available_rooms_in(block) end end # class HotelController + end # module Hotel *** add to others \ No newline at end of file From f2f14f39f6bc6dc6545ea28b12a5c59e278a3cef Mon Sep 17 00:00:00 2001 From: SuelyBarreto Date: Sat, 7 Mar 2020 10:54:50 -0800 Subject: [PATCH 33/37] fixing comments and indentation for hotel controller tests --- test/hotel_controller_test.rb | 40 ++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/test/hotel_controller_test.rb b/test/hotel_controller_test.rb index 333bff425..0ef4051c0 100644 --- a/test/hotel_controller_test.rb +++ b/test/hotel_controller_test.rb @@ -11,6 +11,7 @@ def dt(day) end describe "wave 1" do + describe "rooms" do it "returns a list" do expect(@hotel.rooms).must_be_kind_of Array @@ -25,7 +26,7 @@ def dt(day) expect(@hotel.rooms.include?(room)).must_equal true end end - end + end # describe "rooms" describe "reserve_room" do it "takes two Date objects and returns a Reservation" do @@ -73,9 +74,9 @@ def dt(day) # create a reservation from 2020/01/05 to 2020/01/01. It should raise an exception expect{ @hotel.reserve_room(dt(5), dt(1)) }.must_raise ArgumentError end - end + end # describe "reserve_room" - # access the list of reservations for a specific date to track reservations by date + # Access the list of reservations for a specific date to track reservations by date describe "reservations_by_date" do it "takes a Date and returns a list of Reservations" do @hotel.reserve_room(dt(1), dt(4)) @@ -125,12 +126,13 @@ def dt(day) @hotel.reserve_room(dt(5), dt(10)) expect(@hotel.reservations_by_date(dt(10)).size).must_equal 0 end + end # describe "reservations_by_date" - end - end + end # describe "wave 1" # check whether reservations conflict with each other describe "wave 2" do + describe "available_rooms" do it "takes two dates and returns a list" do room_list = @hotel.available_rooms(dt(1), dt(4)) @@ -167,10 +169,12 @@ def dt(day) expect(@hotel.available_rooms(dt(1), dt(5)).size).must_equal 20 end - end - end # Wave 2 ***add coments to ends + end # describe "available_rooms" + + end # describe "wave 2" describe "wave 3" do + # Create a Block for a Given Date Range, a Set of Rooms and a Rate describe "create_block" do it "takes start/end/rooms/rate and returns Block" do @@ -213,11 +217,9 @@ def dt(day) reservation = @hotel.reserve_room(dt(1), dt(4)) # this should reserve room number 4 instead of 1 expect(reservation.room).must_equal 4 end - - end # Create Block + end # describe "create_block" describe "reserve_from_block" do - # Create a Reservation from a Block # Reserve a specific room from a hotel block it "takes a block and a room. Returns a reservation" do @@ -262,8 +264,7 @@ def dt(day) @hotel.reserve_from_block(block, 3) expect(@hotel.reservations.size).must_equal 4 end - - end # Reserve from block + end # describe "reserve_from_block" # Check if Rooms for a Block are Valid describe "rooms_valid?" do @@ -282,8 +283,7 @@ def dt(day) expect(@hotel.rooms_valid?([18, 19, 20, 21])).must_equal false expect(@hotel.rooms_valid?(1..20)).must_equal false end - - end + end # describe "rooms_valid?" # Check if the Rooms for a Block are Available in the Date Range # Given a specific date, and that a room is set aside in a hotel block for that specific date, I cannot create another hotel block that includes that specific room for that specific date, because it is unavailable @@ -305,7 +305,7 @@ def dt(day) expect(@hotel.block_rooms_available?(dt(1), dt(4), [4, 5, 6])).must_equal false # room 4 in both expect(@hotel.block_rooms_available?(dt(3), dt(6), [2, 3, 4])).must_equal false # overlapping dates end - end + end # describe "block_rooms_available?" # Check if a Room is Not blocked in a Specific Date Range describe "is_room_unblocked?" do @@ -320,7 +320,7 @@ def dt(day) expect(@hotel.is_room_unblocked?(dt(1), dt(4), 3)).must_equal false # room 3 is both expect(@hotel.is_room_unblocked?(dt(3), dt(8), 3)).must_equal false # overlapping dates end - end + end # describe "is_room_unblocked?" # Checks whether a given block has any rooms available # test to fix this. describe "available_rooms_in(block)" do @@ -353,6 +353,8 @@ def dt(day) available = @hotel.available_rooms_in(block) expect(available.size).must_equal 0 end - end - end # Wave 3 -end \ No newline at end of file + end # describe "available_rooms_in(block)" + + end # escribe "wave 3" + +end # describe "Hotel::HotelController" \ No newline at end of file From 07ae984a89eeac44fd75a21c36de06eaa6cfec3c Mon Sep 17 00:00:00 2001 From: SuelyBarreto Date: Sat, 7 Mar 2020 13:11:04 -0800 Subject: [PATCH 34/37] fixed comments and indentation for block tests and code --- lib/block.rb | 4 ++++ test/block_test.rb | 8 ++++---- test/hotel_controller_test.rb | 18 +++++++++--------- 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/lib/block.rb b/lib/block.rb index ec7300755..bae2ca0e7 100644 --- a/lib/block.rb +++ b/lib/block.rb @@ -1,5 +1,7 @@ module Hotel + class Block + # Generator attr_reader :range, :rooms, :rate # Constructor @@ -14,10 +16,12 @@ def initialize(start_date, end_date, rooms, rate) @rate = rate end + # Calculate cost def cost return @rate * @range.nights * @rooms.size end end + end ## Hotel Block diff --git a/test/block_test.rb b/test/block_test.rb index 1f9d0c978..f15f43161 100644 --- a/test/block_test.rb +++ b/test/block_test.rb @@ -11,7 +11,7 @@ end describe "constructor" do - it "can be initialized with two dates, an array of rooms and a rate" do + it "initialize with two dates, array of rooms and rate" do expect(@block).must_be_kind_of Hotel::Block expect(@block.range.start_date).must_equal @start_date expect(@block.range.end_date).must_equal @end_date @@ -49,7 +49,7 @@ # start date can not be the same as end date expect{ Hotel::Block.new(@start_date, @start_date, @rooms, @rate) }.must_raise ArgumentError end - end + end # describe "constructor" describe "cost" do it "returns a number" do @@ -60,6 +60,6 @@ # block cost for 3 nights, 3 rooms, at $150 room rate expect(@block.cost).must_equal 1350 end - end + end # describe "cost" -end \ No newline at end of file +end # describe "Hotel::Block" \ No newline at end of file diff --git a/test/hotel_controller_test.rb b/test/hotel_controller_test.rb index 0ef4051c0..34ae498e8 100644 --- a/test/hotel_controller_test.rb +++ b/test/hotel_controller_test.rb @@ -78,7 +78,7 @@ def dt(day) # Access the list of reservations for a specific date to track reservations by date describe "reservations_by_date" do - it "takes a Date and returns a list of Reservations" do + it "takes a date and returns a list of reservations" do @hotel.reserve_room(dt(1), dt(4)) @hotel.reserve_room(dt(1), dt(4)) reservations_ondate = @hotel.reservations_by_date(dt(1)) @@ -91,7 +91,7 @@ def dt(day) end end - it "returns the reservation when you ask for the start date" do + it "returns reservation when asked for start date" do # create a reservation from 2020/01/01 to 2020/01/04, than get a list of reservations for 2020/01/01, should return 1 @hotel.reserve_room(dt(1), dt(4)) reservations_ondate = @hotel.reservations_by_date(dt(1)) @@ -106,7 +106,7 @@ def dt(day) expect(reservations_ondate.size).must_equal 1 end - it "returns the reservation when you ask for a date in the middle" do + it "returns reservation when ask for date in the middle" do # create a reservation from 2020/01/01 to 2020/01/10, than get a list of reservations for 2020/01/05. It should return 1 @hotel.reserve_room(dt(1), dt(10)) expect(@hotel.reservations_by_date(dt(5)).size).must_equal 1 @@ -118,7 +118,7 @@ def dt(day) expect(@hotel.reservations_by_date(dt(5)).size).must_equal 2 end - it "does not return a reservation when you ask for the end date" do + it "does not return reservation when ask for end date" do # create a reservation from 2020/01/01 to 2020/01/10, than get a list of reservations for 2020/01/10. It should return 0 @hotel.reserve_room(dt(1), dt(10)) expect(@hotel.reservations_by_date(dt(10)).size).must_equal 0 @@ -146,7 +146,7 @@ def dt(day) expect(@hotel.available_rooms(dt(1), dt(5)).size).must_equal 18 end - it "does not show the room available if there is an overlaping reservation" do + it "room not available if overlaping reservation" do # create a reservation from 2020/01/05 to 2020/01/15. @hotel.reserve_room(dt(5), dt(15)) # Check overlap with same range @@ -288,7 +288,7 @@ def dt(day) # Check if the Rooms for a Block are Available in the Date Range # Given a specific date, and that a room is set aside in a hotel block for that specific date, I cannot create another hotel block that includes that specific room for that specific date, because it is unavailable describe "block_rooms_available?" do - it "returns true if the rooms are available in that date range" do + it "returns true if rooms are available in date range" do @hotel.reserve_room(dt(1), dt(4)) # this will reserve room number 1 expect(@hotel.block_rooms_available?(dt(1), dt(4), [2, 3, 4])).must_equal true # different rooms expect(@hotel.block_rooms_available?(dt(4), dt(8), [1, 2, 3])).must_equal true # different dates @@ -297,7 +297,7 @@ def dt(day) expect(@hotel.block_rooms_available?(dt(4), dt(8), [1, 2, 3, 4])).must_equal true # different dates end - it "returns false if the rooms are not available in that date range" do + it "returns false if rooms not available in date range" do @hotel.reserve_room(dt(1), dt(4)) # this will reserve room number 1 expect(@hotel.block_rooms_available?(dt(1), dt(4), [1, 2, 3])).must_equal false # room 1 in both expect(@hotel.block_rooms_available?(dt(3), dt(6), [1, 2, 3])).must_equal false # overlapping dates @@ -309,13 +309,13 @@ def dt(day) # Check if a Room is Not blocked in a Specific Date Range describe "is_room_unblocked?" do - it "returns true if the room is not blocked in that date range" do + it "returns true if room is not blocked in date range" do @hotel.create_block(dt(1), dt(4), [1, 2, 3, 4], 150) expect(@hotel.is_room_unblocked?(dt(1), dt(4), 5)).must_equal true # different room expect(@hotel.is_room_unblocked?(dt(4), dt(8), 1)).must_equal true # different dates end - it "returns false if the room is blocked in that date range" do + it "returns false if room is blocked in date range" do @hotel.create_block(dt(1), dt(4), [1, 2, 3, 4], 150) expect(@hotel.is_room_unblocked?(dt(1), dt(4), 3)).must_equal false # room 3 is both expect(@hotel.is_room_unblocked?(dt(3), dt(8), 3)).must_equal false # overlapping dates From 26acb36892d9d90660104040d205e699c67cb67d Mon Sep 17 00:00:00 2001 From: SuelyBarreto Date: Sat, 7 Mar 2020 13:12:03 -0800 Subject: [PATCH 35/37] it initializes block test --- test/block_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/block_test.rb b/test/block_test.rb index f15f43161..09efe886b 100644 --- a/test/block_test.rb +++ b/test/block_test.rb @@ -11,7 +11,7 @@ end describe "constructor" do - it "initialize with two dates, array of rooms and rate" do + it "initializes with two dates, array of rooms and rate" do expect(@block).must_be_kind_of Hotel::Block expect(@block.range.start_date).must_equal @start_date expect(@block.range.end_date).must_equal @end_date From 324d8d7e0bac403efa62a8705936cb27f4ad0018 Mon Sep 17 00:00:00 2001 From: SuelyBarreto Date: Sat, 7 Mar 2020 17:04:56 -0800 Subject: [PATCH 36/37] CLI (optional) --- main.rb | 179 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 179 insertions(+) create mode 100755 main.rb diff --git a/main.rb b/main.rb new file mode 100755 index 000000000..841826c2f --- /dev/null +++ b/main.rb @@ -0,0 +1,179 @@ +#!/usr/bin/ruby +# +# Title : Hotel Main - Ada Cohort 13 - Space +# Author : Suely Barreto +# Date : March 2020 +# +require "Date" +require_relative "lib/date_range" +require_relative "lib/reservation" +require_relative "lib/block" +require_relative "lib/hotel_controller" + +DISCOUNT_RATE = 150 + +# Function to prompt for a date +def get_date(message) + valid_date = false + while !valid_date + print message + begin + date = Date.parse(gets.chomp) + valid_date = true + rescue => exception + puts "Error: #{exception.message}" + end + end + return date +end + +# Function to prompt for a positive integer with min, max +def get_integer(prompt, min, max) + expression = /[0-9]+/ + # Repeat until number within range + number = 0 + input = "" + while number < min || number > max || input == "" + # Repeat until valid number + input = "" + while input.match(expression).to_s != input || input == "" + print prompt + input = gets.chomp + puts "This is not a valid number!" if input.match(expression).to_s != input || input == "" + end + number = input.to_i + puts "Number is too large. Maximum is #{max}." if number > max + puts "Number is too small. Minimum is #{min}." if number < min + end + return number +end + +# Reserve a room +def make_reservation(hotel) + puts "Enter the data for the new reservation" + start_date = get_date("Start date: ") + end_date = get_date("End date: ") + print "\nDo you confirm you want to add this reservation? (Y/N) " + confirm = gets.chomp.upcase + if confirm == "Y" || confirm == "YES" + begin + reservation = hotel.reserve_room(start_date, end_date) + puts "New reservation was made for room #{reservation.room}." + rescue => exception + puts "Error: #{exception.message}" + end + else + puts "Ok, new reservation was NOT made." + end +end + +# Make a block +def make_block(hotel) + puts "Enter the data for the new block" + start_date = get_date("Start date: ") + end_date = get_date("End date: ") + start_room = get_integer("Start room: ", 1, 20) + end_room = get_integer("End room: ", start_room, 20) + print "\nDo you confirm you want to add this block? (Y/N) " + confirm = gets.chomp.upcase + if confirm == "Y" || confirm == "YES" + begin + block = hotel.create_block(start_date, end_date, (start_room..end_room).to_a, DISCOUNT_RATE) + puts "New block was made." + rescue => exception + puts "Error: #{exception.message}" + end + else + puts "Ok, new block was NOT made." + end +end + +# Make a new reservation from the block +def make_block_reservation(hotel) + show_blocks(hotel) + return if hotel.blocks.size == 0 + puts "\nEnter the data for the block reservation." + start_date = get_date("Start date: ") + end_date = get_date("End date: ") + room = get_integer("Room to reserve: ", 1, 20) + begin + block = hotel.blocks_by_room(start_date, end_date, room).first + rescue => exception + puts "Error: #{exception.message}" + block = nil + end + if block == nil + puts "\nCould not find that block." + else + begin + hotel.reserve_from_block(block, room) + rescue => exception + puts "Error: #{exception.message}" + end + end +end + +# Show reservations +def show_reservations(hotel) + puts "List of reservations\n\n" + puts "No reservations!" if hotel.reservations.size == 0 + hotel.reservations.each do |reservation| + puts "Start date: #{reservation.range.start_date}, end date: #{reservation.range.end_date}, room: #{reservation.room}" + end +end + +# Show blocks +def show_blocks(hotel) + puts "List of blocks\n\n" + puts "No blocks!" if hotel.blocks.size == 0 + hotel.blocks.each do |block| + print "Start date: #{block.range.start_date}, end date: #{block.range.end_date}, rooms:" + block.rooms.each do |room| + print " #{room}" + end + print ", available rooms:" + available = hotel.available_rooms_in(block) + available.each do |room| + print " #{room}" + end + print "None" if available.size == 0 + puts + end +end + +# Main method to show CLI options and call other methods +def main + hotel = Hotel::HotelController.new + + puts "\nWelcome to Hotel Ada Lovelace!" + choice = "" + while choice != "exit" + puts "\nMain menu:" + puts "1 - Make Reservation (mr, r)" + puts "2 - Make Block (mb, b)" + puts "3 - Make Block Reservation (mbr, br)" + puts "4 - Show Reservations (sr, s)" + puts "5 - Show Blocks (sb)" + puts "6 - Exit (x, q)" + print "What would you like to do? " + choice = gets.chomp.downcase + case choice + when "make reservation", "reservation", "mr", "r", "1" + make_reservation(hotel) + when "make block", "block", "mb", "b", "2" + make_block(hotel) + when "make block reservation", "mbr", "br", "3" + make_block_reservation(hotel) + when "show reservations", "sr", "s", "4" + show_reservations(hotel) + when "show blocks", "sb", "5" + show_blocks(hotel) + when "exit", "x", "quit", "q", "e", "6" + choice = "exit" + else + puts "Invalid choice!" + end + end +end + +main \ No newline at end of file From 6f8836d8093fdd8b3e09dc7a1f48eef67201eb8e Mon Sep 17 00:00:00 2001 From: SuelyBarreto Date: Sun, 8 Mar 2020 12:47:02 -0700 Subject: [PATCH 37/37] spacing and indentation --- lib/block.rb | 6 +----- lib/date_range.rb | 2 ++ lib/hotel_controller.rb | 2 +- lib/reservation.rb | 2 ++ main.rb | 4 ++-- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/block.rb b/lib/block.rb index bae2ca0e7..19c74cebd 100644 --- a/lib/block.rb +++ b/lib/block.rb @@ -22,8 +22,4 @@ def cost end end -end - -## Hotel Block -# I can create a Hotel Block if I give a date range, collection of rooms, and a discounted room rate -# A block can contain a maximum of 5 rooms \ No newline at end of file +end \ No newline at end of file diff --git a/lib/date_range.rb b/lib/date_range.rb index 8346f366f..8340ea82e 100644 --- a/lib/date_range.rb +++ b/lib/date_range.rb @@ -1,4 +1,5 @@ module Hotel + class DateRange # Generator attr_accessor :start_date, :end_date @@ -41,4 +42,5 @@ def nights end end + end diff --git a/lib/hotel_controller.rb b/lib/hotel_controller.rb index 00213137d..a4aead81b 100644 --- a/lib/hotel_controller.rb +++ b/lib/hotel_controller.rb @@ -120,4 +120,4 @@ def available_rooms_in(block) end # class HotelController -end # module Hotel *** add to others \ No newline at end of file +end # module Hotel \ No newline at end of file diff --git a/lib/reservation.rb b/lib/reservation.rb index d00203995..219903bf9 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -1,4 +1,5 @@ module Hotel + class Reservation # Generator attr_reader :range, :room @@ -16,4 +17,5 @@ def cost return ROOM_COST * @range.nights end end + end diff --git a/main.rb b/main.rb index 841826c2f..3ffc38bb8 100755 --- a/main.rb +++ b/main.rb @@ -148,14 +148,14 @@ def main puts "\nWelcome to Hotel Ada Lovelace!" choice = "" while choice != "exit" - puts "\nMain menu:" + puts "\nHotel Main Menu:" puts "1 - Make Reservation (mr, r)" puts "2 - Make Block (mb, b)" puts "3 - Make Block Reservation (mbr, br)" puts "4 - Show Reservations (sr, s)" puts "5 - Show Blocks (sb)" puts "6 - Exit (x, q)" - print "What would you like to do? " + print "\nWhat would you like to do? " choice = gets.chomp.downcase case choice when "make reservation", "reservation", "mr", "r", "1"