From ef50e2c1397c13b461bfb8e55047208b535930eb Mon Sep 17 00:00:00 2001 From: Karinna Iniguez Date: Mon, 5 Mar 2018 12:26:06 -0800 Subject: [PATCH 01/20] Add directories, Rakefile, Coverage directory, and room production and spec files. --- Rakefile | 9 +++++++++ lib/room.rb | 0 specs/room_spec.rb | 8 ++++++++ specs/spec_helper.rb | 10 ++++++++++ 4 files changed, 27 insertions(+) create mode 100644 Rakefile create mode 100644 lib/room.rb create mode 100644 specs/room_spec.rb create mode 100644 specs/spec_helper.rb diff --git a/Rakefile b/Rakefile new file mode 100644 index 000000000..deb52f2cd --- /dev/null +++ b/Rakefile @@ -0,0 +1,9 @@ +require 'rake/testtask' + +Rake::TestTask.new do |t| + t.libs = ["lib"] + t.warning = true + t.test_files = FileList['specs/*_spec.rb'] +end + +task default: :test diff --git a/lib/room.rb b/lib/room.rb new file mode 100644 index 000000000..e69de29bb diff --git a/specs/room_spec.rb b/specs/room_spec.rb new file mode 100644 index 000000000..e2a91fef8 --- /dev/null +++ b/specs/room_spec.rb @@ -0,0 +1,8 @@ +require_relative 'spec_helper' + +describe "Testing Tests" do + it "this should fail" do + result = 6 + result.must_be_nil + end +end diff --git a/specs/spec_helper.rb b/specs/spec_helper.rb new file mode 100644 index 000000000..fd87ac692 --- /dev/null +++ b/specs/spec_helper.rb @@ -0,0 +1,10 @@ +require 'simplecov' +SimpleCov.start + +require 'minitest' +require 'minitest/autorun' +require 'minitest/reporters' + +Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new + +require_relative '../lib/room' From 595cb255c5a27e810751739f49c92c06d7812f0a Mon Sep 17 00:00:00 2001 From: Karinna Iniguez Date: Mon, 5 Mar 2018 13:39:21 -0800 Subject: [PATCH 02/20] Add spec file and production file for reservation class --- lib/reservation.rb | 16 ++++++++++++++++ lib/room.rb | 14 ++++++++++++++ specs/reservation_spec.rb | 8 ++++++++ 3 files changed, 38 insertions(+) create mode 100644 lib/reservation.rb create mode 100644 specs/reservation_spec.rb diff --git a/lib/reservation.rb b/lib/reservation.rb new file mode 100644 index 000000000..5569aa866 --- /dev/null +++ b/lib/reservation.rb @@ -0,0 +1,16 @@ + + + + + +module Hotel + class Reservation + + def initialize + + end + + + + end # Class Reservation +end # Module Hotel diff --git a/lib/room.rb b/lib/room.rb index e69de29bb..824009dfe 100644 --- a/lib/room.rb +++ b/lib/room.rb @@ -0,0 +1,14 @@ + + + +module Hotel + class Room + + def initialize + + end + + + + end # Class Room +end # Module Hotel diff --git a/specs/reservation_spec.rb b/specs/reservation_spec.rb new file mode 100644 index 000000000..e2a91fef8 --- /dev/null +++ b/specs/reservation_spec.rb @@ -0,0 +1,8 @@ +require_relative 'spec_helper' + +describe "Testing Tests" do + it "this should fail" do + result = 6 + result.must_be_nil + end +end From 051489bbfb17724ef1adb55877356ad1e024ceac Mon Sep 17 00:00:00 2001 From: Karinna Iniguez Date: Mon, 5 Mar 2018 13:42:58 -0800 Subject: [PATCH 03/20] Add spec and production files for booking class --- lib/booking.rb | 18 ++++++++++++++++++ lib/reservation.rb | 2 +- specs/booking_spec.rb | 8 ++++++++ specs/spec_helper.rb | 1 + 4 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 lib/booking.rb create mode 100644 specs/booking_spec.rb diff --git a/lib/booking.rb b/lib/booking.rb new file mode 100644 index 000000000..8cd1a4094 --- /dev/null +++ b/lib/booking.rb @@ -0,0 +1,18 @@ + + +require_relative 'reservation' +require_relative 'room' + + + +module Hotel + class Booking + + def initialize + + end + + + + end # Class Booking +end # Module Hotel diff --git a/lib/reservation.rb b/lib/reservation.rb index 5569aa866..6aca78546 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -1,5 +1,5 @@ - +require_relative 'room' diff --git a/specs/booking_spec.rb b/specs/booking_spec.rb new file mode 100644 index 000000000..e2a91fef8 --- /dev/null +++ b/specs/booking_spec.rb @@ -0,0 +1,8 @@ +require_relative 'spec_helper' + +describe "Testing Tests" do + it "this should fail" do + result = 6 + result.must_be_nil + end +end diff --git a/specs/spec_helper.rb b/specs/spec_helper.rb index fd87ac692..6eb8ca809 100644 --- a/specs/spec_helper.rb +++ b/specs/spec_helper.rb @@ -8,3 +8,4 @@ Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new require_relative '../lib/room' +require_relative '../lib/reservation' From 8df9ae73d5aa0dcb5d9ab98c800fad9ca9241209 Mon Sep 17 00:00:00 2001 From: Karinna Iniguez Date: Mon, 5 Mar 2018 13:56:32 -0800 Subject: [PATCH 04/20] Add tests and functionality for Room#number method --- specs/block_spec.rb | 12 +++++++++++ specs/booking_spec.rb | 16 +++++++++------ specs/reservation_spec.rb | 16 +++++++++------ specs/room_spec.rb | 43 +++++++++++++++++++++++++++++++++------ specs/spec_helper.rb | 2 ++ 5 files changed, 71 insertions(+), 18 deletions(-) create mode 100644 specs/block_spec.rb diff --git a/specs/block_spec.rb b/specs/block_spec.rb new file mode 100644 index 000000000..0aba6d643 --- /dev/null +++ b/specs/block_spec.rb @@ -0,0 +1,12 @@ +require_relative 'spec_helper' + +describe "Block Class" do + describe "#initialize" do + it "can be created" do + result = Hotel::Block.new + result.must_be_kind_of Hotel::Block + end + end # initialize + + +end # Block Class diff --git a/specs/booking_spec.rb b/specs/booking_spec.rb index e2a91fef8..2398abd28 100644 --- a/specs/booking_spec.rb +++ b/specs/booking_spec.rb @@ -1,8 +1,12 @@ require_relative 'spec_helper' -describe "Testing Tests" do - it "this should fail" do - result = 6 - result.must_be_nil - end -end +describe "Booking Class" do + describe "#initialize" do + it "can be created" do + result = Hotel::Booking.new + result.must_be_kind_of Hotel::Booking + end + end # initialize + + +end # Booking Class diff --git a/specs/reservation_spec.rb b/specs/reservation_spec.rb index e2a91fef8..748a6d392 100644 --- a/specs/reservation_spec.rb +++ b/specs/reservation_spec.rb @@ -1,8 +1,12 @@ require_relative 'spec_helper' -describe "Testing Tests" do - it "this should fail" do - result = 6 - result.must_be_nil - end -end +describe "Reservation Class" do + describe "#initialize" do + it "can be created" do + result = Hotel::Reservation.new + result.must_be_kind_of Hotel::Reservation + end + end # initialize + + +end # Reservation Class diff --git a/specs/room_spec.rb b/specs/room_spec.rb index e2a91fef8..c04e2eb9d 100644 --- a/specs/room_spec.rb +++ b/specs/room_spec.rb @@ -1,8 +1,39 @@ require_relative 'spec_helper' -describe "Testing Tests" do - it "this should fail" do - result = 6 - result.must_be_nil - end -end +describe "Room Class" do + + describe "#initialize" do + + it "can be created" do + result = Hotel::Room.new(4) + result.must_be_kind_of Hotel::Room + end + + it "takes a room number" do + room = Hotel::Room.new(4) + room.must_respond_to :number + end + + it "has a room number that is an integer" do + room = Hotel::Room.new(4) + room.number.must_be_kind_of Integer + end + + it "raises an argument if incorrect room number provided" do + proc{ + room = Hotel::Room.new("whatever") + }.must_raise ArgumentError + proc{ + room = Hotel::Room.new(4.5) + }.must_raise ArgumentError + end + + + + + + + end # initialize + + +end # Room Class diff --git a/specs/spec_helper.rb b/specs/spec_helper.rb index 6eb8ca809..db6861b9d 100644 --- a/specs/spec_helper.rb +++ b/specs/spec_helper.rb @@ -9,3 +9,5 @@ require_relative '../lib/room' require_relative '../lib/reservation' +require_relative '../lib/booking' +require_relative '../lib/block' From 33647b2acecd3669f3b755aa7d6d2e5ff476d81f Mon Sep 17 00:00:00 2001 From: Karinna Iniguez Date: Mon, 5 Mar 2018 13:57:22 -0800 Subject: [PATCH 05/20] Tests for Room#number passing --- lib/block.rb | 18 ++++++++++++++++++ lib/room.rb | 10 +++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 lib/block.rb diff --git a/lib/block.rb b/lib/block.rb new file mode 100644 index 000000000..81f87d3b9 --- /dev/null +++ b/lib/block.rb @@ -0,0 +1,18 @@ + + +require_relative 'reservation' +require_relative 'room' + + + +module Hotel + class Block + + def initialize + + end + + + + end # Class Block +end # Module Hotel diff --git a/lib/room.rb b/lib/room.rb index 824009dfe..98985bb48 100644 --- a/lib/room.rb +++ b/lib/room.rb @@ -4,7 +4,15 @@ module Hotel class Room - def initialize + attr_reader :number + + def initialize(number) + if number.class != Integer || number < 1 + raise ArgumentError.new("Rooms must have a positive integer for room number. Received #{number}") + end + @number = number + + end From a7717bc2da3849b0dfb44344c08c9cd721b6f318 Mon Sep 17 00:00:00 2001 From: Karinna Iniguez Date: Mon, 5 Mar 2018 14:21:28 -0800 Subject: [PATCH 06/20] Add tests and functionality for Room#price --- lib/room.rb | 11 +++++------ specs/room_spec.rb | 27 ++++++++++++++++++++------- 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/lib/room.rb b/lib/room.rb index 98985bb48..7e5d829c0 100644 --- a/lib/room.rb +++ b/lib/room.rb @@ -5,15 +5,14 @@ module Hotel class Room attr_reader :number + attr_accessor :price - def initialize(number) - if number.class != Integer || number < 1 + def initialize(data) + if data[:number] == nil || data[:number].class != Integer || data[:number] < 1 raise ArgumentError.new("Rooms must have a positive integer for room number. Received #{number}") end - @number = number - - - + @number = data[:number] + @price = data[:price].to_f ||= 200.00 end diff --git a/specs/room_spec.rb b/specs/room_spec.rb index c04e2eb9d..006c8d991 100644 --- a/specs/room_spec.rb +++ b/specs/room_spec.rb @@ -5,32 +5,45 @@ describe "#initialize" do it "can be created" do - result = Hotel::Room.new(4) + data = {number: 4, price: 400} + result = Hotel::Room.new(data) result.must_be_kind_of Hotel::Room end it "takes a room number" do - room = Hotel::Room.new(4) + data = {number: 4, price: 400} + room = Hotel::Room.new(data) room.must_respond_to :number end it "has a room number that is an integer" do - room = Hotel::Room.new(4) + data = {number: 4} + room = Hotel::Room.new(data) room.number.must_be_kind_of Integer end it "raises an argument if incorrect room number provided" do + data = {number: "whatever", price: 400} proc{ - room = Hotel::Room.new("whatever") + room = Hotel::Room.new(data) }.must_raise ArgumentError + data = {number: 4.56, price: 400} proc{ - room = Hotel::Room.new(4.5) + room = Hotel::Room.new(data) }.must_raise ArgumentError end + it "has a price that is a float" do + room = Hotel::Room.new({number: 5}) + room.must_respond_to :price + room.price.must_be_kind_of Float + end - - + it "accepts an optional price" do + price = 456.82 + room = Hotel::Room.new({number: 5, price: price}) + room.price.must_equal price + end end # initialize From e84bbd795b60a3519beeea2dd60a82e1c2d691c0 Mon Sep 17 00:00:00 2001 From: Karinna Iniguez Date: Mon, 5 Mar 2018 14:44:42 -0800 Subject: [PATCH 07/20] Add tests for Booking class. An admin can access the list of all the rooms in the hotel --- lib/booking.rb | 16 ++++++++++++++-- specs/booking_spec.rb | 17 ++++++++++++++++- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/lib/booking.rb b/lib/booking.rb index 8cd1a4094..ce3773de7 100644 --- a/lib/booking.rb +++ b/lib/booking.rb @@ -8,9 +8,21 @@ module Hotel class Booking - def initialize + NUM_OF_ROOMS = 20 + + attr_reader :rooms - end + def initialize + @rooms = [] + create_rooms + end # initialize + + def create_rooms + NUM_OF_ROOMS.times do |i| + data = {number: i+1} + @rooms << Room.new(data) + end + end # create_rooms diff --git a/specs/booking_spec.rb b/specs/booking_spec.rb index 2398abd28..c6f1679db 100644 --- a/specs/booking_spec.rb +++ b/specs/booking_spec.rb @@ -1,12 +1,27 @@ require_relative 'spec_helper' describe "Booking Class" do + describe "#initialize" do + it "can be created" do result = Hotel::Booking.new result.must_be_kind_of Hotel::Booking end - end # initialize + it "has a rooms array" do + booking = Hotel::Booking.new + booking.rooms.must_be_kind_of Array + end + + it "has collection of 20 rooms" do + booking = Hotel::Booking.new + booking.rooms.length.must_equal 20 + booking.rooms.each do |room| + room.must_be_kind_of Hotel::Room + end + end + + end # initialize end # Booking Class From 7fdc8410fbe05a9c529bd63bbc6f88941d747b35 Mon Sep 17 00:00:00 2001 From: Karinna Iniguez Date: Mon, 5 Mar 2018 16:11:42 -0800 Subject: [PATCH 08/20] Add reservation and booking tests for initalize methods. Add functionality to make tests pass. --- lib/booking.rb | 7 +++++ lib/reservation.rb | 27 +++++++++++++++-- specs/booking_spec.rb | 23 +++++++++++++++ specs/reservation_spec.rb | 62 ++++++++++++++++++++++++++++++++++++++- 4 files changed, 115 insertions(+), 4 deletions(-) diff --git a/lib/booking.rb b/lib/booking.rb index ce3773de7..8101b89bf 100644 --- a/lib/booking.rb +++ b/lib/booking.rb @@ -14,6 +14,7 @@ class Booking def initialize @rooms = [] + @reservations = [] create_rooms end # initialize @@ -24,6 +25,12 @@ def create_rooms end end # create_rooms + def room_by_num(num) + @rooms.find do |room| + room.number == num + end + + end end # Class Booking diff --git a/lib/reservation.rb b/lib/reservation.rb index 6aca78546..3eb37c196 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -1,13 +1,34 @@ require_relative 'room' - - module Hotel class Reservation - def initialize + OLDEST_RES_DATE = Date.new(1900, 01, 01) + NEWEST_RES_DATE = Date.new(3999, 12, 31) + + attr_reader :start_date, :end_date, :room + + def initialize(data) + @start_date = validate_date(data[:start_date]) + @end_date = validate_date(data[:end_date]) + + if @start_date > @end_date + raise ArgumentError.new("Start date cannot be after end date") + end + + # OPTIONAL ITEMS + @room = data[:room] + @block = data[:block] + + end + def validate_date(string) + date = Date.strptime(string, '%m/%d/%Y') + if !date || date < OLDEST_RES_DATE || date > NEWEST_RES_DATE + raise ArgumentError.new("Invalid date. Received #{string}") + end + return date end diff --git a/specs/booking_spec.rb b/specs/booking_spec.rb index c6f1679db..0ce81199f 100644 --- a/specs/booking_spec.rb +++ b/specs/booking_spec.rb @@ -24,4 +24,27 @@ end # initialize + describe "#room_by_num" do + + before do + @booking = Hotel::Booking.new + end + + it "returns an instance of room" do + result = @booking.room_by_num(12) + result.must_be_kind_of Hotel::Room + end + + it "returns nil if room does not exist" do + result = @booking.room_by_num(67) + result.must_be_nil + end + + it "returns nil if invalid argument given" do + result = @booking.room_by_num("Some String") + result.must_be_nil + end + + end # room_by_num + end # Booking Class diff --git a/specs/reservation_spec.rb b/specs/reservation_spec.rb index 748a6d392..2668c0615 100644 --- a/specs/reservation_spec.rb +++ b/specs/reservation_spec.rb @@ -1,11 +1,71 @@ require_relative 'spec_helper' describe "Reservation Class" do + describe "#initialize" do + it "can be created" do - result = Hotel::Reservation.new + data = {start_date: "11/24/2018", end_date: "12/01/2018"} + result = Hotel::Reservation.new(data) result.must_be_kind_of Hotel::Reservation end + + it "has start date and end date" do + data = {start_date: "11/24/2018", end_date: "12/01/2018"} + reservation = Hotel::Reservation.new(data) + reservation.start_date.must_be_kind_of Date + reservation.end_date.must_be_kind_of Date + end + + it "raises an error if end date is before start date" do + data = {start_date: "11/24/2018", end_date: "11/01/2018"} + proc { + Hotel::Reservation.new(data) + }.must_raise ArgumentError + end + + it "raises an error if incorrect date format is provided" do + data = {start_date: "Nov. 17", end_date: "Dec 1st"} + proc { + Hotel::Reservation.new(data) + }.must_raise ArgumentError + end + + it "has a room instance" do + data = {start_date: "11/24/2018", end_date: "12/01/2018"} + reservation = Hotel::Reservation.new(data) + reservation.must_respond_to :room + end + + + it "raises error if no available rooms on that date" do + # data = {start_date: "11/24/2018", end_date: "12/01/2018"} + # reservation = Hotel::Reservation.new(data) + # reservation.room.must_be_kind_of Hotel::Room + end + + + it "accepts optional room number argument" do + data = {room: 8, start_date: "11/24/2018", end_date: "12/01/2018"} + reservation = Hotel::Reservation.new(data) + reservation.room.must_equal 8 + end + + it "raises error if specified room is unavailable" do + # data = {room: 25, start_date: "11/24/2018", end_date: "12/01/2018"} + # proc { + # Hotel::Reservation.new(data) + # }.must_raise ArgumentError + + end + + + + + + + + end # initialize From 11b42adb30deee0cc9342484b9902134f7cf6349 Mon Sep 17 00:00:00 2001 From: Karinna Iniguez Date: Tue, 6 Mar 2018 08:50:27 -0800 Subject: [PATCH 09/20] Add tests and functionality room#reserve and room#reserved? methods. --- lib/room.rb | 30 ++++++++++++++++++++++++-- specs/room_spec.rb | 52 +++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 79 insertions(+), 3 deletions(-) diff --git a/lib/room.rb b/lib/room.rb index 7e5d829c0..59b6fa4b0 100644 --- a/lib/room.rb +++ b/lib/room.rb @@ -1,10 +1,10 @@ - +require 'date' module Hotel class Room - attr_reader :number + attr_reader :number, :reserved, :blocked attr_accessor :price def initialize(data) @@ -13,6 +13,32 @@ def initialize(data) end @number = data[:number] @price = data[:price].to_f ||= 200.00 + @reserved = [] + @blocked = {} + end + + def reserved?(date) + if @reserved.include?(date) + return true + else + return false + end + end + + def reserve(start_date, end_date) + stay = end_date - start_date + + stay.to_i.times do |index| + @reserved << start_date + index + end + end + + def blocked?(date) + + end + + def block(date, name) + end diff --git a/specs/room_spec.rb b/specs/room_spec.rb index 006c8d991..1ed3197bf 100644 --- a/specs/room_spec.rb +++ b/specs/room_spec.rb @@ -45,8 +45,58 @@ room.price.must_equal price end - end # initialize + describe "#reserve" do + before do + @room = Hotel::Room.new(number:5) + end + + it "adds each day of stay into reserved array" do + @room.reserved.length.must_equal 0 + start_date = Date.new(2018, 05, 03) + end_date = Date.new(2018, 05, 13) + days_in_stay = end_date - start_date + result = @room.reserve(start_date, end_date) + @room.reserved.length.must_equal days_in_stay + end + + end # reserve + + describe "#reserved?" do + + before do + start_date = Date.new(2018, 04, 12) + end_date = Date.new(2018, 04, 15) + @room = Hotel::Room.new(number: 12) + @room.reserve(start_date, end_date) + end + + it "returns true if room is reserved on that day" do + result = @room.reserved?(Date.new(2018, 04, 13)) + result.must_equal true + end + + it "returns false if room is not reserved on that day" do + result = @room.reserved?(Date.new(2018, 04, 17)) + result.must_equal false + end + + it "returns false for checkout day" do + result = @room.reserved?(Date.new(2018, 04, 15)) + result.must_equal false + end + + end # reserved? + + describe "#block" do + + + end # block + + describe "#blocked?" do + + end # blocked? + end # Room Class From 41e3e7aa8323bda6db932fb6cf3f344cffe0a24d Mon Sep 17 00:00:00 2001 From: Karinna Iniguez Date: Tue, 6 Mar 2018 11:19:48 -0800 Subject: [PATCH 10/20] Change reservations. Room is now a required argument and must be instance of room. Tests failing --- lib/booking.rb | 1 - lib/reservation.rb | 10 +++++++--- specs/room_spec.rb | 6 +++--- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/lib/booking.rb b/lib/booking.rb index 8101b89bf..11d5d5e74 100644 --- a/lib/booking.rb +++ b/lib/booking.rb @@ -29,7 +29,6 @@ def room_by_num(num) @rooms.find do |room| room.number == num end - end diff --git a/lib/reservation.rb b/lib/reservation.rb index 3eb37c196..6b0847ad2 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -10,6 +10,12 @@ class Reservation attr_reader :start_date, :end_date, :room def initialize(data) + if data[:room].class != Room + raise ArgumentError.new("Must enter room to be reserved") + end + @room = data[:room] # MUST BE INSTANCE OF ROOM + + @start_date = validate_date(data[:start_date]) @end_date = validate_date(data[:end_date]) @@ -17,10 +23,8 @@ def initialize(data) raise ArgumentError.new("Start date cannot be after end date") end - # OPTIONAL ITEMS - @room = data[:room] + # OPTIONAL @block = data[:block] - end def validate_date(string) diff --git a/specs/room_spec.rb b/specs/room_spec.rb index 1ed3197bf..9046cf7fd 100644 --- a/specs/room_spec.rb +++ b/specs/room_spec.rb @@ -25,11 +25,11 @@ it "raises an argument if incorrect room number provided" do data = {number: "whatever", price: 400} proc{ - room = Hotel::Room.new(data) + Hotel::Room.new(data) }.must_raise ArgumentError data = {number: 4.56, price: 400} proc{ - room = Hotel::Room.new(data) + Hotel::Room.new(data) }.must_raise ArgumentError end @@ -57,7 +57,7 @@ start_date = Date.new(2018, 05, 03) end_date = Date.new(2018, 05, 13) days_in_stay = end_date - start_date - result = @room.reserve(start_date, end_date) + @room.reserve(start_date, end_date) @room.reserved.length.must_equal days_in_stay end From dc7b4ad2257af2bdec73859e070a120177b6783d Mon Sep 17 00:00:00 2001 From: Karinna Iniguez Date: Tue, 6 Mar 2018 11:35:11 -0800 Subject: [PATCH 11/20] Pass tests requiring reservation to have a room instance as argument --- specs/reservation_spec.rb | 39 ++++++++++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/specs/reservation_spec.rb b/specs/reservation_spec.rb index 2668c0615..e2aca24fa 100644 --- a/specs/reservation_spec.rb +++ b/specs/reservation_spec.rb @@ -5,36 +5,57 @@ describe "#initialize" do it "can be created" do - data = {start_date: "11/24/2018", end_date: "12/01/2018"} + data = {room: Hotel::Room.new(number:5), start_date: "11/24/2018", end_date: "12/01/2018"} result = Hotel::Reservation.new(data) result.must_be_kind_of Hotel::Reservation end it "has start date and end date" do - data = {start_date: "11/24/2018", end_date: "12/01/2018"} + data = {room: Hotel::Room.new(number: 9), start_date: "11/24/2018", end_date: "12/01/2018"} reservation = Hotel::Reservation.new(data) reservation.start_date.must_be_kind_of Date reservation.end_date.must_be_kind_of Date end it "raises an error if end date is before start date" do - data = {start_date: "11/24/2018", end_date: "11/01/2018"} + data = {room: Hotel::Room.new(number: 14), start_date: "11/24/2018", end_date: "11/01/2018"} proc { Hotel::Reservation.new(data) }.must_raise ArgumentError end it "raises an error if incorrect date format is provided" do - data = {start_date: "Nov. 17", end_date: "Dec 1st"} + data = {room: Hotel::Room.new(number: 17), start_date: "Nov. 17", end_date: "Dec 1st"} proc { Hotel::Reservation.new(data) }.must_raise ArgumentError end - it "has a room instance" do - data = {start_date: "11/24/2018", end_date: "12/01/2018"} + it "required room is room instance" do + data = {room: Hotel::Room.new(number: 8), start_date: "11/24/2018", end_date: "12/01/2018"} reservation = Hotel::Reservation.new(data) reservation.must_respond_to :room + reservation.room.must_be_kind_of Hotel::Room + reservation.room.number.must_equal 8 + end + + it "raises error if room entered is not instance of room" do + data = {room: 18, start_date: "11/24/2018", end_date: "12/01/2018"} + proc { + Hotel::Reservation.new(data) + }.must_raise ArgumentError + + data = {room: "Something else", start_date: "11/24/2018", end_date: "12/01/2018"} + proc { + Hotel::Reservation.new(data) + }.must_raise ArgumentError + end + + it "raises error if no room argument given" do + data = {start_date: "11/24/2018", end_date: "12/01/2018"} + proc { + Hotel::Reservation.new(data) + }.must_raise ArgumentError end @@ -45,11 +66,7 @@ end - it "accepts optional room number argument" do - data = {room: 8, start_date: "11/24/2018", end_date: "12/01/2018"} - reservation = Hotel::Reservation.new(data) - reservation.room.must_equal 8 - end + it "raises error if specified room is unavailable" do # data = {room: 25, start_date: "11/24/2018", end_date: "12/01/2018"} From c1f7940df18b67f22eeffc5712d272575f9ebcfe Mon Sep 17 00:00:00 2001 From: Karinna Iniguez Date: Tue, 6 Mar 2018 14:36:34 -0800 Subject: [PATCH 12/20] Add helper methods to validate dates and validate stays in reservation, room, and booking class. --- lib/booking.rb | 40 ++++++++++++++++++++++++++++++++++++++++ lib/reservation.rb | 32 ++++++++++++++++++++++---------- lib/room.rb | 22 ++++++++++++++++++++++ 3 files changed, 84 insertions(+), 10 deletions(-) diff --git a/lib/booking.rb b/lib/booking.rb index 11d5d5e74..404294bd9 100644 --- a/lib/booking.rb +++ b/lib/booking.rb @@ -31,6 +31,46 @@ def room_by_num(num) end end + def find_next_room(start_date, end_date) + + end + + def available_rooms(date) + + end + + def reserve(data) + start_date = data[:start_date] + end_date = data[:end_date] + if data[:room_num] + room = room_by_num(data[:room_num]) + else + room = find_next_room + end + end + + def validate_date(entry) + if entry.class == String + entry = Date.strptime(entry, '%m/%d/%Y') + if entry.class != Date || entry < OLDEST_RES_DATE || entry > NEWEST_RES_DATE + raise ArgumentError.new("Invalid date. Please enter MM/DD/YYYY Received #{entry}") + end + elsif entry.class == Date + if entry < OLDEST_RES_DATE || entry > NEWEST_RES_DATE + raise ArgumentError.new("Date: Invalid date. Received #{entry}") + end + else + raise ArgumentError.new("Other: Invalid date. Received #{entry}") + end + return entry + end + + def validate_stay(beginning, ending) + if beginning > ending + raise ArgumentError.new("Start date cannot be after end date") + end + end + end # Class Booking end # Module Hotel diff --git a/lib/reservation.rb b/lib/reservation.rb index 6b0847ad2..692a1ed77 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -10,29 +10,41 @@ class Reservation attr_reader :start_date, :end_date, :room def initialize(data) + # MUST BE INSTANCE OF ROOM if data[:room].class != Room raise ArgumentError.new("Must enter room to be reserved") end - @room = data[:room] # MUST BE INSTANCE OF ROOM - + @room = data[:room] @start_date = validate_date(data[:start_date]) @end_date = validate_date(data[:end_date]) - if @start_date > @end_date - raise ArgumentError.new("Start date cannot be after end date") - end + validate_stay(@start_date, @end_date) # OPTIONAL @block = data[:block] end - def validate_date(string) - date = Date.strptime(string, '%m/%d/%Y') - if !date || date < OLDEST_RES_DATE || date > NEWEST_RES_DATE - raise ArgumentError.new("Invalid date. Received #{string}") + def validate_date(entry) + if entry.class == String + entry = Date.strptime(entry, '%m/%d/%Y') + if entry.class != Date || entry < OLDEST_RES_DATE || entry > NEWEST_RES_DATE + raise ArgumentError.new("Invalid date. Please enter MM/DD/YYYY Received #{entry}") + end + elsif entry.class == Date + if entry < OLDEST_RES_DATE || entry > NEWEST_RES_DATE + raise ArgumentError.new("Date: Invalid date. Received #{entry}") + end + else + raise ArgumentError.new("Other: Invalid date. Received #{entry}") + end + return entry + end + + def validate_stay(beginning, ending) + if beginning > ending + raise ArgumentError.new("Start date cannot be after end date") end - return date end diff --git a/lib/room.rb b/lib/room.rb index 59b6fa4b0..5e74688fe 100644 --- a/lib/room.rb +++ b/lib/room.rb @@ -41,6 +41,28 @@ def block(date, name) end + def validate_date(entry) + if entry.class == String + entry = Date.strptime(entry, '%m/%d/%Y') + if entry.class != Date || entry < OLDEST_RES_DATE || entry > NEWEST_RES_DATE + raise ArgumentError.new("Invalid date. Please enter MM/DD/YYYY Received #{entry}") + end + elsif entry.class == Date + if entry < OLDEST_RES_DATE || entry > NEWEST_RES_DATE + raise ArgumentError.new("Date: Invalid date. Received #{entry}") + end + else + raise ArgumentError.new("Other: Invalid date. Received #{entry}") + end + return entry + end + + def validate_stay(beginning, ending) + if beginning > ending + raise ArgumentError.new("Start date cannot be after end date") + end + end + end # Class Room From f699e43b6a290a701ec431d06642b353994cbc3d Mon Sep 17 00:00:00 2001 From: Karinna Iniguez Date: Tue, 6 Mar 2018 15:43:29 -0800 Subject: [PATCH 13/20] Add loader class for helper methods used in all classes and have each class inherit instead of repeating code --- lib/booking.rb | 26 ++------------------------ lib/loader.rb | 39 +++++++++++++++++++++++++++++++++++++++ lib/reservation.rb | 25 ++----------------------- lib/room.rb | 30 ++++++------------------------ specs/spec_helper.rb | 1 + 5 files changed, 50 insertions(+), 71 deletions(-) create mode 100644 lib/loader.rb diff --git a/lib/booking.rb b/lib/booking.rb index 404294bd9..b0c0ceb00 100644 --- a/lib/booking.rb +++ b/lib/booking.rb @@ -1,12 +1,12 @@ - +require_relative 'loader' require_relative 'reservation' require_relative 'room' module Hotel - class Booking + class Booking < Loader NUM_OF_ROOMS = 20 @@ -49,28 +49,6 @@ def reserve(data) end end - def validate_date(entry) - if entry.class == String - entry = Date.strptime(entry, '%m/%d/%Y') - if entry.class != Date || entry < OLDEST_RES_DATE || entry > NEWEST_RES_DATE - raise ArgumentError.new("Invalid date. Please enter MM/DD/YYYY Received #{entry}") - end - elsif entry.class == Date - if entry < OLDEST_RES_DATE || entry > NEWEST_RES_DATE - raise ArgumentError.new("Date: Invalid date. Received #{entry}") - end - else - raise ArgumentError.new("Other: Invalid date. Received #{entry}") - end - return entry - end - - def validate_stay(beginning, ending) - if beginning > ending - raise ArgumentError.new("Start date cannot be after end date") - end - end - end # Class Booking end # Module Hotel diff --git a/lib/loader.rb b/lib/loader.rb new file mode 100644 index 000000000..0e9d5a8e8 --- /dev/null +++ b/lib/loader.rb @@ -0,0 +1,39 @@ + +module Hotel + class Loader + + OLDEST_RES_DATE = Date.new(1900, 01, 01) + NEWEST_RES_DATE = Date.new(3999, 12, 31) + + def validate_date(entry) + if entry.class == String + entry = Date.strptime(entry, '%m/%d/%Y') + if entry.class != Date || entry < OLDEST_RES_DATE || entry > NEWEST_RES_DATE + raise ArgumentError.new("Invalid date. Please enter MM/DD/YYYY Received #{entry}") + end + elsif entry.class == Date + if entry < OLDEST_RES_DATE || entry > NEWEST_RES_DATE + raise ArgumentError.new("Date: Invalid date. Received #{entry}") + end + else + raise ArgumentError.new("Other: Invalid date. Received #{entry}") + end + return entry + end + + def validate_stay(beginning, ending) + beginning = validate_date(beginning) + ending = validate_date(ending) + if beginning > ending + raise ArgumentError.new("Start date cannot be after end date") + end + end + + + + + + + end # class loader + +end # module Hotel diff --git a/lib/reservation.rb b/lib/reservation.rb index 692a1ed77..8866934f1 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -1,8 +1,9 @@ +require_relative 'loader' require_relative 'room' module Hotel - class Reservation + class Reservation < Loader OLDEST_RES_DATE = Date.new(1900, 01, 01) NEWEST_RES_DATE = Date.new(3999, 12, 31) @@ -25,28 +26,6 @@ def initialize(data) @block = data[:block] end - def validate_date(entry) - if entry.class == String - entry = Date.strptime(entry, '%m/%d/%Y') - if entry.class != Date || entry < OLDEST_RES_DATE || entry > NEWEST_RES_DATE - raise ArgumentError.new("Invalid date. Please enter MM/DD/YYYY Received #{entry}") - end - elsif entry.class == Date - if entry < OLDEST_RES_DATE || entry > NEWEST_RES_DATE - raise ArgumentError.new("Date: Invalid date. Received #{entry}") - end - else - raise ArgumentError.new("Other: Invalid date. Received #{entry}") - end - return entry - end - - def validate_stay(beginning, ending) - if beginning > ending - raise ArgumentError.new("Start date cannot be after end date") - end - end - end # Class Reservation diff --git a/lib/room.rb b/lib/room.rb index 5e74688fe..16b73b687 100644 --- a/lib/room.rb +++ b/lib/room.rb @@ -1,8 +1,9 @@ +require_relative 'loader' require 'date' module Hotel - class Room + class Room < Loader attr_reader :number, :reserved, :blocked attr_accessor :price @@ -18,6 +19,7 @@ def initialize(data) end def reserved?(date) + date = validate_date(date) if @reserved.include?(date) return true else @@ -26,6 +28,9 @@ def reserved?(date) end def reserve(start_date, end_date) + start_date = validate_date(start_date) + end_date = validate_date(end_date) + validate_stay(start_date, end_date) stay = end_date - start_date stay.to_i.times do |index| @@ -41,29 +46,6 @@ def block(date, name) end - def validate_date(entry) - if entry.class == String - entry = Date.strptime(entry, '%m/%d/%Y') - if entry.class != Date || entry < OLDEST_RES_DATE || entry > NEWEST_RES_DATE - raise ArgumentError.new("Invalid date. Please enter MM/DD/YYYY Received #{entry}") - end - elsif entry.class == Date - if entry < OLDEST_RES_DATE || entry > NEWEST_RES_DATE - raise ArgumentError.new("Date: Invalid date. Received #{entry}") - end - else - raise ArgumentError.new("Other: Invalid date. Received #{entry}") - end - return entry - end - - def validate_stay(beginning, ending) - if beginning > ending - raise ArgumentError.new("Start date cannot be after end date") - end - end - - end # Class Room end # Module Hotel diff --git a/specs/spec_helper.rb b/specs/spec_helper.rb index db6861b9d..fb92aff86 100644 --- a/specs/spec_helper.rb +++ b/specs/spec_helper.rb @@ -11,3 +11,4 @@ require_relative '../lib/reservation' require_relative '../lib/booking' require_relative '../lib/block' +require_relative '../lib/loader' From bc6d77fb851635159aad217d13adf7fc3e686096 Mon Sep 17 00:00:00 2001 From: Karinna Iniguez Date: Wed, 7 Mar 2018 16:47:33 -0800 Subject: [PATCH 14/20] Add tests for Booking#reserve method and add functionality including helper methods with SETS, thanks for the brainstorm Sam: --- lib/booking.rb | 36 +++++++++++++++++++------ lib/loader.rb | 6 +---- lib/reservation.rb | 12 ++++----- specs/booking_spec.rb | 61 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 96 insertions(+), 19 deletions(-) diff --git a/lib/booking.rb b/lib/booking.rb index b0c0ceb00..aa69a739e 100644 --- a/lib/booking.rb +++ b/lib/booking.rb @@ -10,7 +10,7 @@ class Booking < Loader NUM_OF_ROOMS = 20 - attr_reader :rooms + attr_reader :rooms, :reservations def initialize @rooms = [] @@ -31,22 +31,42 @@ def room_by_num(num) end end - def find_next_room(start_date, end_date) + def rooms_by_day(date) + date = validate_date(date) + @rooms.reject do |room| + room.reserved?(date) && room.blocked?(date) + end end - def available_rooms(date) + def rooms_by_range(start_date, end_date) + start_date = validate_date(start_date) + end_date = validate_date(end_date) + date_set = Set.new(start_date...end_date) + avail_array = @rooms.reject do |room| + date_set.intersect? room.reserved.to_set + end + return avail_array end + def reserve(data) - start_date = data[:start_date] - end_date = data[:end_date] - if data[:room_num] - room = room_by_num(data[:room_num]) + start_date = validate_date(data[:start_date]) + end_date = validate_date(data[:end_date]) + available_rooms = rooms_by_range(start_date, end_date) + if available_rooms.length == 0 + raise ArgumentError.new("Sorry, we do not have a room available at this time") + else - room = find_next_room + room = available_rooms.sample end + + + reservation = Reservation.new(start_date: start_date, end_date: end_date, room: room) + @reservations << reservation + room.reserve(start_date, end_date) + return reservation end diff --git a/lib/loader.rb b/lib/loader.rb index 0e9d5a8e8..f6fea2946 100644 --- a/lib/loader.rb +++ b/lib/loader.rb @@ -27,13 +27,9 @@ def validate_stay(beginning, ending) if beginning > ending raise ArgumentError.new("Start date cannot be after end date") end + return (ending - beginning).to_i end - - - - - end # class loader end # module Hotel diff --git a/lib/reservation.rb b/lib/reservation.rb index 8866934f1..818a3a214 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -5,13 +5,9 @@ module Hotel class Reservation < Loader - OLDEST_RES_DATE = Date.new(1900, 01, 01) - NEWEST_RES_DATE = Date.new(3999, 12, 31) - attr_reader :start_date, :end_date, :room def initialize(data) - # MUST BE INSTANCE OF ROOM if data[:room].class != Room raise ArgumentError.new("Must enter room to be reserved") end @@ -19,14 +15,18 @@ def initialize(data) @start_date = validate_date(data[:start_date]) @end_date = validate_date(data[:end_date]) - validate_stay(@start_date, @end_date) # OPTIONAL - @block = data[:block] + @block = data[:block] # NIL if not provided end + def total_cost + end + + def stay_length + end end # Class Reservation end # Module Hotel diff --git a/specs/booking_spec.rb b/specs/booking_spec.rb index 0ce81199f..60ccdc9eb 100644 --- a/specs/booking_spec.rb +++ b/specs/booking_spec.rb @@ -47,4 +47,65 @@ end # room_by_num + describe "#reserve" do + + it "returns a reservation with start date and end date." do + book = Hotel::Booking.new + start = Date.new(2018, 04, 02) + + result = book.reserve(start_date: start, end_date: start + 3) + result.must_be_kind_of Hotel::Reservation + end + + it "updates the reservations array" do + book = Hotel::Booking.new + before = book.reservations.length + book.reserve(start_date: "05/02/2018", end_date: "05/08/2018") + after = book.reservations.length + after.must_equal before + 1 + book.reservations[0].must_be_kind_of Hotel::Reservation + end + + it "raises an error if no available rooms" do + book = Hotel::Booking.new + 20.times do + book.reserve(start_date: "06/01/2018", end_date: "06/03/2018") + end + proc { + book.reserve(start_date: "06/01/2018", end_date: "06/03/2018") + }.must_raise ArgumentError + end + + it "raises an error if incorrect date information is given" do + book = Hotel::Booking.new + proc { + book.reserve(start_date: "August 29th", end_date: "09/30/2018") + }.must_raise ArgumentError + end + + it "raises an error if no start date or end date is given" do + book = Hotel::Booking.new + proc { + book.reserve(end_date: "09/30/2018") + }.must_raise ArgumentError + + proc { + book.reserve(start_date: "09/30/2018") + }.must_raise ArgumentError + + proc { + book.reserve() + }.must_raise ArgumentError + end + + it "raises an error if end date is before start date" do + book = Hotel::Booking.new + proc { + book.reserve(start_date: "11/01/2018", end_date: "09/30/2018") + }.must_raise ArgumentError + end + + + end # reserve + end # Booking Class From 7e28235b302bb3ead4df0540c14c9e9de5a3eadd Mon Sep 17 00:00:00 2001 From: Karinna Iniguez Date: Thu, 8 Mar 2018 07:57:33 -0800 Subject: [PATCH 15/20] Adding filter to remove specs from coverage --- specs/spec_helper.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/specs/spec_helper.rb b/specs/spec_helper.rb index fb92aff86..9d2b59c52 100644 --- a/specs/spec_helper.rb +++ b/specs/spec_helper.rb @@ -1,5 +1,7 @@ require 'simplecov' -SimpleCov.start +SimpleCov.start do + add_filter "/specs" +end require 'minitest' require 'minitest/autorun' From 54156731805d2d67696ba9d91e9e5346be6129ec Mon Sep 17 00:00:00 2001 From: Karinna Iniguez Date: Thu, 8 Mar 2018 11:31:42 -0800 Subject: [PATCH 16/20] Add tests and functionality for Reservation#total_cost method and helper method #stay_length for same class --- lib/booking.rb | 5 ++--- lib/reservation.rb | 2 ++ specs/reservation_spec.rb | 43 ++++++++++++++++++++++++++++++++++++++- 3 files changed, 46 insertions(+), 4 deletions(-) diff --git a/lib/booking.rb b/lib/booking.rb index aa69a739e..d28310419 100644 --- a/lib/booking.rb +++ b/lib/booking.rb @@ -3,8 +3,6 @@ require_relative 'reservation' require_relative 'room' - - module Hotel class Booking < Loader @@ -40,9 +38,11 @@ def rooms_by_day(date) end def rooms_by_range(start_date, end_date) + start_date = validate_date(start_date) end_date = validate_date(end_date) date_set = Set.new(start_date...end_date) + avail_array = @rooms.reject do |room| date_set.intersect? room.reserved.to_set end @@ -62,7 +62,6 @@ def reserve(data) room = available_rooms.sample end - reservation = Reservation.new(start_date: start_date, end_date: end_date, room: room) @reservations << reservation room.reserve(start_date, end_date) diff --git a/lib/reservation.rb b/lib/reservation.rb index 818a3a214..19ac6e45b 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -22,10 +22,12 @@ def initialize(data) end def total_cost + stay_length * @room.price end def stay_length + @end_date - @start_date end end # Class Reservation diff --git a/specs/reservation_spec.rb b/specs/reservation_spec.rb index e2aca24fa..2394bcbb0 100644 --- a/specs/reservation_spec.rb +++ b/specs/reservation_spec.rb @@ -76,14 +76,55 @@ end + end # initialize + describe "#stay_length helper method" do + it "returns acurate days in integer form for one day reservation" do + room = Hotel::Room.new(number: 4) + reservation = Hotel::Reservation.new(room: room, start_date: "07/15/2018", end_date: "07/16/2018") + expected_price = 1 + result = reservation.stay_length + result.must_equal expected_price + end + it "returns zero if checkin and checkout are same day" do + room = Hotel::Room.new(number: 4) + reservation = Hotel::Reservation.new(room: room, start_date: "07/15/2018", end_date: "07/15/2018") + expected_price = 0 + result = reservation.stay_length + result.must_equal expected_price + end + end # stay_length + describe "#total_cost" do + it "returns an accurate total cost for 1 day stay" do + expected_price = 100 + room = Hotel::Room.new(number: 5, price: expected_price) + reservation = Hotel::Reservation.new(room: room, start_date: "05/01/2018", end_date: "05/02/2018") + result = reservation.total_cost + result.must_equal expected_price + end + + it "returns an accurate total cost for 3 day stay" do + room = Hotel::Room.new(number: 5, price: 100) + reservation = Hotel::Reservation.new(room: room, start_date: "05/01/2018", end_date: "05/04/2018") + expected_price = room.price * 3 + result = reservation.total_cost + result.must_equal expected_price + end + + it "returns zero if checkout is same day" do + room = Hotel::Room.new(number: 5, price: 300) + reservation = Hotel::Reservation.new(room: room, start_date: "05/01/2018", end_date: "05/01/2018") + expected_price = 0 + result = reservation.total_cost + result.must_equal expected_price + end - end # initialize + end # total_cost end # Reservation Class From 78ff62426141c61e3cb015d2677a986ceb9e4ee9 Mon Sep 17 00:00:00 2001 From: Karinna Iniguez Date: Sun, 11 Mar 2018 21:40:31 -0700 Subject: [PATCH 17/20] Change loader class to more appropriate name. New name is Validate class. All other classes inheriting --- lib/booking.rb | 23 ++++++++++++++--------- lib/reservation.rb | 4 ++-- lib/room.rb | 19 ++++++++++++++----- lib/{loader.rb => validate.rb} | 4 ++-- specs/room_spec.rb | 19 +++++++++++++++++++ 5 files changed, 51 insertions(+), 18 deletions(-) rename lib/{loader.rb => validate.rb} (96%) diff --git a/lib/booking.rb b/lib/booking.rb index d28310419..8e92beaaa 100644 --- a/lib/booking.rb +++ b/lib/booking.rb @@ -1,10 +1,10 @@ - -require_relative 'loader' +require 'set' +require_relative 'validate' require_relative 'reservation' require_relative 'room' module Hotel - class Booking < Loader + class Booking < Validate NUM_OF_ROOMS = 20 @@ -13,6 +13,7 @@ class Booking < Loader def initialize @rooms = [] @reservations = [] + @blocks = [] create_rooms end # initialize @@ -30,15 +31,14 @@ def room_by_num(num) end - def rooms_by_day(date) + def rooms_by_day(date, block: nil) date = validate_date(date) @rooms.reject do |room| room.reserved?(date) && room.blocked?(date) end end - def rooms_by_range(start_date, end_date) - + def rooms_by_range(start_date, end_date, block: nil) start_date = validate_date(start_date) end_date = validate_date(end_date) date_set = Set.new(start_date...end_date) @@ -47,6 +47,12 @@ def rooms_by_range(start_date, end_date) date_set.intersect? room.reserved.to_set end + if block + avail_array = avail_array.reject do |room| + date_set.intersect? room.booked[block].to_set + end + end + return avail_array end @@ -54,17 +60,16 @@ def rooms_by_range(start_date, end_date) def reserve(data) start_date = validate_date(data[:start_date]) end_date = validate_date(data[:end_date]) - available_rooms = rooms_by_range(start_date, end_date) + available_rooms = rooms_by_range(start_date, end_date, block: data[:block]) if available_rooms.length == 0 raise ArgumentError.new("Sorry, we do not have a room available at this time") - else room = available_rooms.sample end reservation = Reservation.new(start_date: start_date, end_date: end_date, room: room) @reservations << reservation - room.reserve(start_date, end_date) + room.reserve(start_date, end_date, block: data[:block]) return reservation end diff --git a/lib/reservation.rb b/lib/reservation.rb index 19ac6e45b..5d16dd233 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -1,9 +1,9 @@ -require_relative 'loader' +require_relative 'validate' require_relative 'room' module Hotel - class Reservation < Loader + class Reservation < Validate attr_reader :start_date, :end_date, :room diff --git a/lib/room.rb b/lib/room.rb index 16b73b687..c4e72cb85 100644 --- a/lib/room.rb +++ b/lib/room.rb @@ -1,9 +1,9 @@ -require_relative 'loader' +require_relative 'validate' require 'date' module Hotel - class Room < Loader + class Room < Validate attr_reader :number, :reserved, :blocked attr_accessor :price @@ -27,7 +27,7 @@ def reserved?(date) end end - def reserve(start_date, end_date) + def reserve(start_date, end_date, block: nil) start_date = validate_date(start_date) end_date = validate_date(end_date) validate_stay(start_date, end_date) @@ -39,11 +39,20 @@ def reserve(start_date, end_date) end def blocked?(date) - + date = validate_date(date) + existing_blocks = @blocked.find do |name, dates| + dates.include?(date) + end + return existing_blocks.keys end def block(date, name) - + date = validate_date(date) + if @blocked[name] + @blocked[name] << date + else + @blocked[name] = [date] + end end diff --git a/lib/loader.rb b/lib/validate.rb similarity index 96% rename from lib/loader.rb rename to lib/validate.rb index f6fea2946..3f2792eae 100644 --- a/lib/loader.rb +++ b/lib/validate.rb @@ -1,6 +1,6 @@ - +require 'date' module Hotel - class Loader + class Validate OLDEST_RES_DATE = Date.new(1900, 01, 01) NEWEST_RES_DATE = Date.new(3999, 12, 31) diff --git a/specs/room_spec.rb b/specs/room_spec.rb index 9046cf7fd..718fff4a4 100644 --- a/specs/room_spec.rb +++ b/specs/room_spec.rb @@ -90,12 +90,31 @@ end # reserved? describe "#block" do + it "updates the room's blocked hash with name key value" do + + end + + it "updates the value of key to array of dates" do + + end + + it "raises error if incorrect date format entered" do + + end end # block describe "#blocked?" do + it "returns the block name if room is currently blocked" do + + end + + it "returns nil if the room is not blocked" do + + end + end # blocked? From e3eb8483b1fa575910c770f1f62baec72449080d Mon Sep 17 00:00:00 2001 From: Karinna Iniguez Date: Sun, 11 Mar 2018 21:44:33 -0700 Subject: [PATCH 18/20] remove unused files for block class. Program not using that class after all. --- specs/block_spec.rb | 12 ------------ specs/spec_helper.rb | 3 +-- 2 files changed, 1 insertion(+), 14 deletions(-) delete mode 100644 specs/block_spec.rb diff --git a/specs/block_spec.rb b/specs/block_spec.rb deleted file mode 100644 index 0aba6d643..000000000 --- a/specs/block_spec.rb +++ /dev/null @@ -1,12 +0,0 @@ -require_relative 'spec_helper' - -describe "Block Class" do - describe "#initialize" do - it "can be created" do - result = Hotel::Block.new - result.must_be_kind_of Hotel::Block - end - end # initialize - - -end # Block Class diff --git a/specs/spec_helper.rb b/specs/spec_helper.rb index 9d2b59c52..9b5f8e338 100644 --- a/specs/spec_helper.rb +++ b/specs/spec_helper.rb @@ -12,5 +12,4 @@ require_relative '../lib/room' require_relative '../lib/reservation' require_relative '../lib/booking' -require_relative '../lib/block' -require_relative '../lib/loader' +require_relative '../lib/validate' From 2226e78bb688e21762db12765ef4d9340e13c96f Mon Sep 17 00:00:00 2001 From: Karinna Iniguez Date: Sun, 11 Mar 2018 21:45:26 -0700 Subject: [PATCH 19/20] removing file --- lib/block.rb | 18 ------------------ 1 file changed, 18 deletions(-) delete mode 100644 lib/block.rb diff --git a/lib/block.rb b/lib/block.rb deleted file mode 100644 index 81f87d3b9..000000000 --- a/lib/block.rb +++ /dev/null @@ -1,18 +0,0 @@ - - -require_relative 'reservation' -require_relative 'room' - - - -module Hotel - class Block - - def initialize - - end - - - - end # Class Block -end # Module Hotel From 9ad17c08618b9d2988badd52306114419aa830c9 Mon Sep 17 00:00:00 2001 From: Karinna Iniguez Date: Mon, 12 Mar 2018 08:35:57 -0700 Subject: [PATCH 20/20] Refactor --- lib/reservation.rb | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/reservation.rb b/lib/reservation.rb index 5d16dd233..584db1176 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -5,6 +5,8 @@ module Hotel class Reservation < Validate + BLOCK_DISCOUNT = 0.20 + attr_reader :start_date, :end_date, :room def initialize(data) @@ -22,8 +24,11 @@ def initialize(data) end def total_cost - stay_length * @room.price - + cost = stay_length * @room.price + if @block + cost *= BLOCK_DISCOUNT + end + return cost end def stay_length