From 7d8109cb0e393b9452f7ccd7c803911c54b3c2f5 Mon Sep 17 00:00:00 2001 From: Laura Date: Tue, 4 Sep 2018 15:56:27 -0700 Subject: [PATCH 01/49] calculates number of nights --- lib/reservation.rb | 11 +++++++++++ spec/reservation_spec.rb | 21 +++++++++++++++++++++ spec/spec_helper.rb | 5 +++++ 3 files changed, 37 insertions(+) create mode 100644 lib/reservation.rb create mode 100644 spec/reservation_spec.rb diff --git a/lib/reservation.rb b/lib/reservation.rb new file mode 100644 index 000000000..e6769e30a --- /dev/null +++ b/lib/reservation.rb @@ -0,0 +1,11 @@ +class Reservation + attr_reader :start_time, :end_time, :nights + @@rate = 200 + def initialize(data) + @start_time = data[:start_time] + @end_time = data[:end_time] + sec = @end_time - @start_time + @nights = sec.to_i / 86400 + end + +end diff --git a/spec/reservation_spec.rb b/spec/reservation_spec.rb new file mode 100644 index 000000000..3ec33dd1d --- /dev/null +++ b/spec/reservation_spec.rb @@ -0,0 +1,21 @@ +require_relative 'spec_helper' + +describe Reservation do + before do + @reservation = Reservation.new(start_time: Time.parse("2016-08-08"), end_time: Time.parse("2016-08-09")) + end + + it "is an instance of a reservation" do + expect(@reservation).must_be_kind_of Reservation + end + + it "calculate number of nights" do + expect(@reservation.nights).must_equal 1 + end + + it "calculate cost" do + skip + expect(@reservation.cost).must_equal 200 + end + +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 4d1e3fdc8..876d2d59b 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,8 +1,13 @@ + +require 'simplecov' +SimpleCov.start require 'minitest' require 'minitest/autorun' require 'minitest/reporters' # Add simplecov + Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new # Require_relative your lib files here! +require_relative '../lib/reservation' From a888ce79b09963dc76ba66dd5ca24276b2d286e3 Mon Sep 17 00:00:00 2001 From: Laura Date: Tue, 4 Sep 2018 16:01:25 -0700 Subject: [PATCH 02/49] calculates cost per reservation --- lib/reservation.rb | 3 ++- spec/reservation_spec.rb | 1 - 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/reservation.rb b/lib/reservation.rb index e6769e30a..90ade4c70 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -1,11 +1,12 @@ class Reservation - attr_reader :start_time, :end_time, :nights + attr_reader :start_time, :end_time, :nights, :cost @@rate = 200 def initialize(data) @start_time = data[:start_time] @end_time = data[:end_time] sec = @end_time - @start_time @nights = sec.to_i / 86400 + @cost = @nights * 200 end end diff --git a/spec/reservation_spec.rb b/spec/reservation_spec.rb index 3ec33dd1d..54d059ff1 100644 --- a/spec/reservation_spec.rb +++ b/spec/reservation_spec.rb @@ -14,7 +14,6 @@ end it "calculate cost" do - skip expect(@reservation.cost).must_equal 200 end From 2bb49d642d7108e96e8ee5824391ba8bbcad8211 Mon Sep 17 00:00:00 2001 From: Laura Date: Tue, 4 Sep 2018 16:48:08 -0700 Subject: [PATCH 03/49] raise standard error for invalid range --- lib/reservation.rb | 6 ++++-- spec/reservation_spec.rb | 11 ++++++++++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/lib/reservation.rb b/lib/reservation.rb index 90ade4c70..7892a80d8 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -1,12 +1,14 @@ class Reservation attr_reader :start_time, :end_time, :nights, :cost - @@rate = 200 + @@RATE = 200 + #@@all_reservations = [] def initialize(data) @start_time = data[:start_time] @end_time = data[:end_time] sec = @end_time - @start_time @nights = sec.to_i / 86400 - @cost = @nights * 200 + @cost = @nights * @@RATE + raise StandardError if sec < 0 end end diff --git a/spec/reservation_spec.rb b/spec/reservation_spec.rb index 54d059ff1..09ee169bd 100644 --- a/spec/reservation_spec.rb +++ b/spec/reservation_spec.rb @@ -1,6 +1,6 @@ require_relative 'spec_helper' -describe Reservation do +describe "#initialize" do before do @reservation = Reservation.new(start_time: Time.parse("2016-08-08"), end_time: Time.parse("2016-08-09")) end @@ -17,4 +17,13 @@ expect(@reservation.cost).must_equal 200 end + it "raises an Standard error for invalid date range, end time before start time" do + @reservation = {start_time: Time.parse("2016-08-09"), end_time: Time.parse("2016-08-08")} + expect{ + Reservation.new(@reservation) + }.must_raise StandardError + end end + + + # From bfbc1f833ca8f835285c61b79fcbd6f2f28ac183 Mon Sep 17 00:00:00 2001 From: Laura Date: Wed, 5 Sep 2018 15:16:56 -0700 Subject: [PATCH 04/49] created instances of reservations --- lib/admin.rb | 27 +++++++++++++++++ lib/reservation.rb | 16 ++++++++-- spec/admin_spec.rb | 36 ++++++++++++++++++++++ spec/reservation_spec.rb | 47 +++++++++++++++++++++++++++++ spec/spec_helper.rb | 5 ++- spec/test_data/test_reservation.csv | 3 ++ 6 files changed, 130 insertions(+), 4 deletions(-) create mode 100644 lib/admin.rb create mode 100644 spec/admin_spec.rb create mode 100644 spec/test_data/test_reservation.csv diff --git a/lib/admin.rb b/lib/admin.rb new file mode 100644 index 000000000..47e04938d --- /dev/null +++ b/lib/admin.rb @@ -0,0 +1,27 @@ +require 'csv' +require 'pry' + +class Admin + attr_reader :reservations + def initialize + @reservations = load_reservations('spec/test_data/test_reservation.csv') + end + + def load_reservations(filename) + reservations = [] + + CSV.read(filename, headers: true).each do |line| + data = line.to_h + input_data = {} + input_data[:id] = data["id"].to_i + input_data[:start_time] = Time.parse(data["start_time"]) + input_data[:end_time] = Time.parse(data["end_time"]) + reservations << Reservation.new(input_data) + end + + return reservations + end + # reservations sorted by date range + + +end diff --git a/lib/reservation.rb b/lib/reservation.rb index 7892a80d8..bf5c179b1 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -1,14 +1,24 @@ class Reservation - attr_reader :start_time, :end_time, :nights, :cost + attr_reader :start_time, :end_time, :nights, :cost, :reservations, :id @@RATE = 200 - #@@all_reservations = [] + def initialize(data) + @id = data[:id] @start_time = data[:start_time] @end_time = data[:end_time] sec = @end_time - @start_time @nights = sec.to_i / 86400 @cost = @nights * @@RATE - raise StandardError if sec < 0 + raise StandardError if sec < 0 #invalid range end +# # create a class that creates a class variables that adds reservations - a booking class +# def add_reservation +# @@reservations << self +# end + + # def self.find(start_time, end_time) + # reservations = @@reservations.select {|instance| instance.start_time == start_time && instance.end_time == end_time} # returns instance in array + # end + end diff --git a/spec/admin_spec.rb b/spec/admin_spec.rb new file mode 100644 index 000000000..ce2d0c79f --- /dev/null +++ b/spec/admin_spec.rb @@ -0,0 +1,36 @@ +require_relative 'spec_helper' + +# runs from project directory +USER_TEST_FILE = 'spec/test_data/test_reservation.csv' +@reservation = Reservation.new(start_time: Time.parse("2016-08-08"), end_time: Time.parse("2016-08-09")) +xdescribe "Initializer" do + it "is an instance of TripDispatcher" do + dispatcher = Admin.new(USER_TEST_FILE) + expect(dispatcher).must_be_kind_of Admin + end + +end + +describe "Reservation loads" do + before do + @admin = Admin.new + end + + it "accurately loads reservation information into reservations array" do + first_reservation = @admin.reservations.first + last_reservation = @admin.reservations.last + + expect(first_reservation.start_time).must_equal Time.parse("2018-08-08 00:00:00 -0700") + expect(first_reservation.end_time).must_equal Time.parse("2018-08-09 00:00:00 -0700") + expect(first_reservation.nights).must_equal 1 + expect(first_reservation.cost).must_equal 200 + expect(first_reservation.id).must_equal 1 + + expect(last_reservation.start_time).must_equal Time.parse("2018-08-07 00:00:00 -0700") + expect(last_reservation.end_time).must_equal Time.parse("2018-08-09 00:00:00 -0700") + expect(last_reservation.nights).must_equal 2 + expect(last_reservation.cost).must_equal 400 + expect(last_reservation.id).must_equal 2 + end + +end diff --git a/spec/reservation_spec.rb b/spec/reservation_spec.rb index 09ee169bd..04b7562b5 100644 --- a/spec/reservation_spec.rb +++ b/spec/reservation_spec.rb @@ -25,5 +25,52 @@ end end +xdescribe "#add reservation" do + before do + @reservation1 = Reservation.new(start_time: Time.parse("2016-08-08"), end_time: Time.parse("2016-08-09")) + @reservation2 = Reservation.new(start_time: Time.parse("2016-08-08"), end_time: Time.parse("2016-08-09")) + @@reservations = [@reservation1, @reservation2] + reservation = Reservation.new(@reservation1) + end + + it "Increases the number of collection" do + before_count = reservation.count + @reservation1.add_reservation + expected_count = before_count + 1 + expect(reservation.reservations.count).must_equal expected_count + end + + it "Is added to the collection of reservations" do + skip + order = Order.new(1337, products, customer) + + order.add_product("sandwich", 4.25) + expect(order.products.include?("sandwich")).must_equal true + end + + it "Raises an ArgumentError if the reservation is already present" do + skip + order = Order.new(1337, products, customer) + before_total = order.total + + expect { + order.add_product("banana", 4.25) + }.must_raise ArgumentError + + # The list of products should not have been modified + expect(order.total).must_equal before_total + end +end + +xdescribe "#find reservation" do + it "return the reservations that have a spicific date" do + skip + reservations = Reservation.find(Time.parse("2016-08-08")) + + expect(reservations).must_be_kind_of Array + expect(first.start_time).must_equal Time.parse("2016-08-08") + end +end + # diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 876d2d59b..7ba81649b 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,9 +1,11 @@ - +require 'time' require 'simplecov' SimpleCov.start require 'minitest' require 'minitest/autorun' require 'minitest/reporters' +require 'minitest/skip_dsl' + # Add simplecov @@ -11,3 +13,4 @@ # Require_relative your lib files here! require_relative '../lib/reservation' +require_relative '../lib/admin' diff --git a/spec/test_data/test_reservation.csv b/spec/test_data/test_reservation.csv new file mode 100644 index 000000000..60744fbdb --- /dev/null +++ b/spec/test_data/test_reservation.csv @@ -0,0 +1,3 @@ +id,start_time,end_time +1,2018-08-08,2018-08-09 +2,2018-08-07,2018-08-09 From e9885e49681720a4042041f17c720c8ebf2d1553 Mon Sep 17 00:00:00 2001 From: Laura Date: Wed, 5 Sep 2018 15:25:50 -0700 Subject: [PATCH 05/49] sorts reservations by dates --- lib/admin.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/admin.rb b/lib/admin.rb index 47e04938d..ce2cc2c86 100644 --- a/lib/admin.rb +++ b/lib/admin.rb @@ -5,6 +5,7 @@ class Admin attr_reader :reservations def initialize @reservations = load_reservations('spec/test_data/test_reservation.csv') + sort_reservations end def load_reservations(filename) @@ -21,7 +22,11 @@ def load_reservations(filename) return reservations end - # reservations sorted by date range + def sort_reservations + @reservations.sort_by { |object| object.start_time } + end + + end From c813c551b55d7724ae326c1f38c7897c329cbf70 Mon Sep 17 00:00:00 2001 From: Laura Date: Wed, 5 Sep 2018 15:30:49 -0700 Subject: [PATCH 06/49] verify that Admin is an instance of an admin --- lib/admin.rb | 4 +++- lib/reservation.rb | 4 ---- spec/admin_spec.rb | 14 +++++++------- 3 files changed, 10 insertions(+), 12 deletions(-) diff --git a/lib/admin.rb b/lib/admin.rb index ce2cc2c86..500c1089f 100644 --- a/lib/admin.rb +++ b/lib/admin.rb @@ -27,6 +27,8 @@ def sort_reservations @reservations.sort_by { |object| object.start_time } end - + def find_reservation(date) + @reservations.select {|instance| instance.start_time == start_time || instance.end_time == end_time} + end end diff --git a/lib/reservation.rb b/lib/reservation.rb index bf5c179b1..959d0a9b3 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -17,8 +17,4 @@ def initialize(data) # @@reservations << self # end - # def self.find(start_time, end_time) - # reservations = @@reservations.select {|instance| instance.start_time == start_time && instance.end_time == end_time} # returns instance in array - # end - end diff --git a/spec/admin_spec.rb b/spec/admin_spec.rb index ce2d0c79f..b3770635f 100644 --- a/spec/admin_spec.rb +++ b/spec/admin_spec.rb @@ -1,21 +1,21 @@ require_relative 'spec_helper' # runs from project directory -USER_TEST_FILE = 'spec/test_data/test_reservation.csv' -@reservation = Reservation.new(start_time: Time.parse("2016-08-08"), end_time: Time.parse("2016-08-09")) -xdescribe "Initializer" do - it "is an instance of TripDispatcher" do - dispatcher = Admin.new(USER_TEST_FILE) +describe "#Admin - initializer" do + before do + @admin = Admin.new + end + it "is an instance of Admin" do + dispatcher = Admin.new expect(dispatcher).must_be_kind_of Admin end end -describe "Reservation loads" do +describe "Admin - reservations load" do before do @admin = Admin.new end - it "accurately loads reservation information into reservations array" do first_reservation = @admin.reservations.first last_reservation = @admin.reservations.last From baf978d7dbd587d761755b5f6b7c988c60e311b3 Mon Sep 17 00:00:00 2001 From: Laura Date: Wed, 5 Sep 2018 15:36:26 -0700 Subject: [PATCH 07/49] made helper methods private --- lib/admin.rb | 10 ++++---- spec/admin_spec.rb | 15 +++++++----- spec/reservation_spec.rb | 49 +--------------------------------------- 3 files changed, 15 insertions(+), 59 deletions(-) diff --git a/lib/admin.rb b/lib/admin.rb index 500c1089f..4c916881b 100644 --- a/lib/admin.rb +++ b/lib/admin.rb @@ -23,12 +23,12 @@ def load_reservations(filename) return reservations end - def sort_reservations - @reservations.sort_by { |object| object.start_time } - end - def find_reservation(date) @reservations.select {|instance| instance.start_time == start_time || instance.end_time == end_time} end - + + private + def sort_reservations + @reservations.sort_by { |object| object.start_time } + end end diff --git a/spec/admin_spec.rb b/spec/admin_spec.rb index b3770635f..884fc0094 100644 --- a/spec/admin_spec.rb +++ b/spec/admin_spec.rb @@ -10,12 +10,6 @@ expect(dispatcher).must_be_kind_of Admin end -end - -describe "Admin - reservations load" do - before do - @admin = Admin.new - end it "accurately loads reservation information into reservations array" do first_reservation = @admin.reservations.first last_reservation = @admin.reservations.last @@ -33,4 +27,13 @@ expect(last_reservation.id).must_equal 2 end + xdescribe "#find reservation" do + it "return the reservations that have a spicific date" do + skip + reservations = Reservation.find(Time.parse("2016-08-08")) + + expect(reservations).must_be_kind_of Array + expect(first.start_time).must_equal Time.parse("2016-08-08") + end + end end diff --git a/spec/reservation_spec.rb b/spec/reservation_spec.rb index 04b7562b5..e074d0f64 100644 --- a/spec/reservation_spec.rb +++ b/spec/reservation_spec.rb @@ -1,6 +1,6 @@ require_relative 'spec_helper' -describe "#initialize" do +describe "#initialize method in reservation class" do before do @reservation = Reservation.new(start_time: Time.parse("2016-08-08"), end_time: Time.parse("2016-08-09")) end @@ -25,52 +25,5 @@ end end -xdescribe "#add reservation" do - before do - @reservation1 = Reservation.new(start_time: Time.parse("2016-08-08"), end_time: Time.parse("2016-08-09")) - @reservation2 = Reservation.new(start_time: Time.parse("2016-08-08"), end_time: Time.parse("2016-08-09")) - @@reservations = [@reservation1, @reservation2] - reservation = Reservation.new(@reservation1) - end - - it "Increases the number of collection" do - before_count = reservation.count - @reservation1.add_reservation - expected_count = before_count + 1 - expect(reservation.reservations.count).must_equal expected_count - end - - it "Is added to the collection of reservations" do - skip - order = Order.new(1337, products, customer) - - order.add_product("sandwich", 4.25) - expect(order.products.include?("sandwich")).must_equal true - end - - it "Raises an ArgumentError if the reservation is already present" do - skip - order = Order.new(1337, products, customer) - before_total = order.total - - expect { - order.add_product("banana", 4.25) - }.must_raise ArgumentError - - # The list of products should not have been modified - expect(order.total).must_equal before_total - end -end - -xdescribe "#find reservation" do - it "return the reservations that have a spicific date" do - skip - reservations = Reservation.find(Time.parse("2016-08-08")) - - expect(reservations).must_be_kind_of Array - expect(first.start_time).must_equal Time.parse("2016-08-08") - end -end - # From 7c173ff8a2366cef2ad7055488b8e3174923ee22 Mon Sep 17 00:00:00 2001 From: Laura Date: Thu, 6 Sep 2018 14:56:03 -0700 Subject: [PATCH 08/49] admin can find reservations based on a date --- lib/admin.rb | 23 ++++++++++++++++++----- spec/admin_spec.rb | 19 ++++++++++++------- 2 files changed, 30 insertions(+), 12 deletions(-) diff --git a/lib/admin.rb b/lib/admin.rb index 4c916881b..5683b6933 100644 --- a/lib/admin.rb +++ b/lib/admin.rb @@ -2,7 +2,7 @@ require 'pry' class Admin - attr_reader :reservations + attr_reader :reservations, :find_reservation def initialize @reservations = load_reservations('spec/test_data/test_reservation.csv') sort_reservations @@ -22,13 +22,26 @@ def load_reservations(filename) return reservations end - - def find_reservation(date) - @reservations.select {|instance| instance.start_time == start_time || instance.end_time == end_time} +#As an administrator, I can access the list of reservations for a specific date + def find_reservations(date) + date = Time.parse(date) + reservations = @reservations.select do |instance| + start_date = instance.start_time + end_date = instance.end_time + if date_in_range(start_date, end_date, date) + instance + end + end + return reservations end - + private def sort_reservations @reservations.sort_by { |object| object.start_time } end + + def date_in_range(start_date, first_date, date) + range = (start_date..first_date) + range.include?(date) + end end diff --git a/spec/admin_spec.rb b/spec/admin_spec.rb index 884fc0094..21c37e18f 100644 --- a/spec/admin_spec.rb +++ b/spec/admin_spec.rb @@ -26,14 +26,19 @@ expect(last_reservation.cost).must_equal 400 expect(last_reservation.id).must_equal 2 end +end + +describe "#find reservation" do + before do + @admin = Admin.new + end + it "return the reservations that have a specific date as a start date" do + #arrange + date = "2018-08-07 00:00:00 -0700" - xdescribe "#find reservation" do - it "return the reservations that have a spicific date" do - skip - reservations = Reservation.find(Time.parse("2016-08-08")) + reservations = @admin.find_reservations(date) - expect(reservations).must_be_kind_of Array - expect(first.start_time).must_equal Time.parse("2016-08-08") - end + expect(reservations).must_be_kind_of Array + expect(reservations.id).must_equal 1 end end From 0009d3a80326953743ce370481d563d71e2531fa Mon Sep 17 00:00:00 2001 From: Laura Date: Thu, 6 Sep 2018 15:14:08 -0700 Subject: [PATCH 09/49] administrator can check the cost of a reservation --- lib/admin.rb | 9 ++++++++- spec/admin_spec.rb | 14 ++++++++++++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/lib/admin.rb b/lib/admin.rb index 5683b6933..590bc29ed 100644 --- a/lib/admin.rb +++ b/lib/admin.rb @@ -22,7 +22,7 @@ def load_reservations(filename) return reservations end -#As an administrator, I can access the list of reservations for a specific date + #As an administrator, I can access the list of reservations for a specific date def find_reservations(date) date = Time.parse(date) reservations = @reservations.select do |instance| @@ -35,6 +35,13 @@ def find_reservations(date) return reservations end + #As an administrator, I can get the total cost for a given reservation + def find_cost(reservation_id) + reservation = @reservations.select { |instance| instance.id == reservation_id }[0] + cost = reservation.cost + return cost + end + private def sort_reservations @reservations.sort_by { |object| object.start_time } diff --git a/spec/admin_spec.rb b/spec/admin_spec.rb index 21c37e18f..e586cd534 100644 --- a/spec/admin_spec.rb +++ b/spec/admin_spec.rb @@ -28,7 +28,7 @@ end end -describe "#find reservation" do +describe "#reservation information" do before do @admin = Admin.new end @@ -39,6 +39,16 @@ reservations = @admin.find_reservations(date) expect(reservations).must_be_kind_of Array - expect(reservations.id).must_equal 1 + expect(reservations.first.id).must_equal 2 + end + + it "return the cost of a reservation" do + #arrange + reservation_id = 2 + + reservation_cost = @admin.find_cost(reservation_id) + + expect(reservation_cost).must_be_kind_of Integer + expect(reservation_cost).must_equal 400 end end From c4d82cfb366bed1187d5f62009a7c8de5cc8ed4f Mon Sep 17 00:00:00 2001 From: Laura Date: Thu, 6 Sep 2018 15:37:20 -0700 Subject: [PATCH 10/49] admin can view rooms --- lib/admin.rb | 17 +++++++++++++++++ spec/admin_spec.rb | 12 ++++++++++++ 2 files changed, 29 insertions(+) diff --git a/lib/admin.rb b/lib/admin.rb index 590bc29ed..4810d1af6 100644 --- a/lib/admin.rb +++ b/lib/admin.rb @@ -5,9 +5,14 @@ class Admin attr_reader :reservations, :find_reservation def initialize @reservations = load_reservations('spec/test_data/test_reservation.csv') + @rooms = [1,2,4] sort_reservations end + # def create_rooms(20) + # + # end + def load_reservations(filename) reservations = [] @@ -22,6 +27,18 @@ def load_reservations(filename) return reservations end + + # As an administrator, I can access the list of all of the rooms in the hotel + def view_rooms + return @rooms + end + + #As an administrator, I can reserve a room for a given date range + def reserve_room(start_date, end_date) + + end + + #As an administrator, I can access the list of reservations for a specific date def find_reservations(date) date = Time.parse(date) diff --git a/spec/admin_spec.rb b/spec/admin_spec.rb index e586cd534..cd43f3775 100644 --- a/spec/admin_spec.rb +++ b/spec/admin_spec.rb @@ -51,4 +51,16 @@ expect(reservation_cost).must_be_kind_of Integer expect(reservation_cost).must_equal 400 end + + describe "#rooms information" do + before do + @admin = Admin.new + end + it "view all of the rooms" do + + rooms = @admin.view_rooms + + expect(rooms).must_be_kind_of Array + end + end end From 50456635c4b6f9554849d2f42d0e8b29ce9c3062 Mon Sep 17 00:00:00 2001 From: Laura Date: Thu, 6 Sep 2018 16:01:22 -0700 Subject: [PATCH 11/49] wrote method to create rooms, and class for rooms --- lib/admin.rb | 20 ++++++++++++++------ lib/room.rb | 8 ++++++++ spec/admin_spec.rb | 7 +++++++ spec/room_spec.rb | 1 + spec/spec_helper.rb | 1 + 5 files changed, 31 insertions(+), 6 deletions(-) create mode 100644 lib/room.rb create mode 100644 spec/room_spec.rb diff --git a/lib/admin.rb b/lib/admin.rb index 4810d1af6..fd6df856d 100644 --- a/lib/admin.rb +++ b/lib/admin.rb @@ -2,16 +2,24 @@ require 'pry' class Admin - attr_reader :reservations, :find_reservation + attr_reader :reservations, :find_reservation, :rooms def initialize @reservations = load_reservations('spec/test_data/test_reservation.csv') - @rooms = [1,2,4] + @rooms = create_rooms(20) sort_reservations end - # def create_rooms(20) - # - # end + def create_rooms(number) + rooms = [] + number.times do |num| + input_data = {} + room_number = num + 1 + input_data[:number] = room_number + input_data[:status] = "available" + rooms << Room.new(input_data) + end + return rooms + end def load_reservations(filename) reservations = [] @@ -32,7 +40,7 @@ def load_reservations(filename) def view_rooms return @rooms end - + #As an administrator, I can reserve a room for a given date range def reserve_room(start_date, end_date) diff --git a/lib/room.rb b/lib/room.rb new file mode 100644 index 000000000..4ef9b5995 --- /dev/null +++ b/lib/room.rb @@ -0,0 +1,8 @@ +class Room + attr_reader :number, :status + + def initialize(data) + @number = data[:number] + @status = data[:status] + end +end diff --git a/spec/admin_spec.rb b/spec/admin_spec.rb index cd43f3775..878f051d9 100644 --- a/spec/admin_spec.rb +++ b/spec/admin_spec.rb @@ -26,6 +26,13 @@ expect(last_reservation.cost).must_equal 400 expect(last_reservation.id).must_equal 2 end + + it "accurately create_rooms" do + first_room = @admin.rooms.first + last_room = @admin.rooms.last + expect(first_room.number).must_equal 1 + expect(last_room.number).must_equal 20 + end end describe "#reservation information" do diff --git a/spec/room_spec.rb b/spec/room_spec.rb new file mode 100644 index 000000000..ae9c220ea --- /dev/null +++ b/spec/room_spec.rb @@ -0,0 +1 @@ +require_relative 'spec_helper' diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 7ba81649b..e36f0d897 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -14,3 +14,4 @@ # Require_relative your lib files here! require_relative '../lib/reservation' require_relative '../lib/admin' +require_relative '../lib/room' From 6ce551a98d6dc64b01fd85639f34cefd8a5c036d Mon Sep 17 00:00:00 2001 From: Laura Date: Thu, 6 Sep 2018 16:11:11 -0700 Subject: [PATCH 12/49] created spec file for room class --- lib/admin.rb | 5 +++++ spec/admin_spec.rb | 17 +++++++++-------- spec/room_spec.rb | 18 ++++++++++++++++++ 3 files changed, 32 insertions(+), 8 deletions(-) diff --git a/lib/admin.rb b/lib/admin.rb index fd6df856d..94606be01 100644 --- a/lib/admin.rb +++ b/lib/admin.rb @@ -36,6 +36,11 @@ def load_reservations(filename) return reservations end + #As an administrator, I can reserve a room for a given date range + def reserve_room(date) + + end + # As an administrator, I can access the list of all of the rooms in the hotel def view_rooms return @rooms diff --git a/spec/admin_spec.rb b/spec/admin_spec.rb index 878f051d9..3822f9779 100644 --- a/spec/admin_spec.rb +++ b/spec/admin_spec.rb @@ -59,15 +59,16 @@ expect(reservation_cost).must_equal 400 end - describe "#rooms information" do - before do - @admin = Admin.new - end - it "view all of the rooms" do +end + +describe "#rooms information" do + before do + @admin = Admin.new + end + it "view all of the rooms" do - rooms = @admin.view_rooms + rooms = @admin.view_rooms - expect(rooms).must_be_kind_of Array - end + expect(rooms).must_be_kind_of Array end end diff --git a/spec/room_spec.rb b/spec/room_spec.rb index ae9c220ea..b3f0575d7 100644 --- a/spec/room_spec.rb +++ b/spec/room_spec.rb @@ -1 +1,19 @@ require_relative 'spec_helper' + +describe "#Room - initialize method" do + before do + @Room = Room.new(number: 1, status: "available") + end + + it "is an instance of a Room" do + expect(@Room).must_be_kind_of Room + end + + it "has a room number" do + expect(@Room.number).must_equal 1 + end + + it "is a status" do + expect(@Room.status).must_equal "available" + end +end From 329707da7e92a3677576078197651c433c6d4073 Mon Sep 17 00:00:00 2001 From: Laura Date: Thu, 6 Sep 2018 16:38:08 -0700 Subject: [PATCH 13/49] admin can reserve a room --- lib/admin.rb | 10 ++-------- lib/room.rb | 5 +++++ spec/admin_spec.rb | 11 ++++++++++- 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/lib/admin.rb b/lib/admin.rb index 94606be01..7bdd6bab4 100644 --- a/lib/admin.rb +++ b/lib/admin.rb @@ -37,8 +37,8 @@ def load_reservations(filename) end #As an administrator, I can reserve a room for a given date range - def reserve_room(date) - + def reserve_room(start_date, end_date) + @rooms.first.change_status end # As an administrator, I can access the list of all of the rooms in the hotel @@ -46,12 +46,6 @@ def view_rooms return @rooms end - #As an administrator, I can reserve a room for a given date range - def reserve_room(start_date, end_date) - - end - - #As an administrator, I can access the list of reservations for a specific date def find_reservations(date) date = Time.parse(date) diff --git a/lib/room.rb b/lib/room.rb index 4ef9b5995..6e4147560 100644 --- a/lib/room.rb +++ b/lib/room.rb @@ -5,4 +5,9 @@ def initialize(data) @number = data[:number] @status = data[:status] end + + def change_status + @status = "unavailable" + end + end diff --git a/spec/admin_spec.rb b/spec/admin_spec.rb index 3822f9779..3434edbff 100644 --- a/spec/admin_spec.rb +++ b/spec/admin_spec.rb @@ -68,7 +68,16 @@ it "view all of the rooms" do rooms = @admin.view_rooms - expect(rooms).must_be_kind_of Array end + + it "can make a reservation" do + start_date = Time.parse("2018-08-07 00:00:00 -0700") + end_date = Time.parse("2018-08-09 00:00:00 -0700") + @admin.reserve_room(start_date, end_date) # change status of room + + rooms = @admin.view_rooms + + expect(rooms.first.status).must_equal "unavailable" + end end From 0d62fb72e0803e6404f924c105e3fc58e5a323bc Mon Sep 17 00:00:00 2001 From: Laura Date: Thu, 6 Sep 2018 16:41:25 -0700 Subject: [PATCH 14/49] reserve_room converts string into computer date --- lib/admin.rb | 2 ++ spec/admin_spec.rb | 6 +++--- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/admin.rb b/lib/admin.rb index 7bdd6bab4..588f78c4d 100644 --- a/lib/admin.rb +++ b/lib/admin.rb @@ -38,6 +38,8 @@ def load_reservations(filename) #As an administrator, I can reserve a room for a given date range def reserve_room(start_date, end_date) + start_date = Time.parse(start_date) + end_date = Time.parse(end_date) @rooms.first.change_status end diff --git a/spec/admin_spec.rb b/spec/admin_spec.rb index 3434edbff..9b5df6fd3 100644 --- a/spec/admin_spec.rb +++ b/spec/admin_spec.rb @@ -72,11 +72,11 @@ end it "can make a reservation" do - start_date = Time.parse("2018-08-07 00:00:00 -0700") - end_date = Time.parse("2018-08-09 00:00:00 -0700") + start_date = "2018-08-07 00:00:00 -0700" + end_date = "2018-08-09 00:00:00 -0700" @admin.reserve_room(start_date, end_date) # change status of room - rooms = @admin.view_rooms + rooms = @admin.view_rooms expect(rooms.first.status).must_equal "unavailable" end From 79b0714fbf7fb83a3cbbba27841f4c848cc12c65 Mon Sep 17 00:00:00 2001 From: Laura Date: Thu, 6 Sep 2018 16:53:05 -0700 Subject: [PATCH 15/49] wrote create_range method --- lib/admin.rb | 10 +++++++--- lib/room.rb | 5 +++++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/lib/admin.rb b/lib/admin.rb index 588f78c4d..9da134ba4 100644 --- a/lib/admin.rb +++ b/lib/admin.rb @@ -54,7 +54,8 @@ def find_reservations(date) reservations = @reservations.select do |instance| start_date = instance.start_time end_date = instance.end_time - if date_in_range(start_date, end_date, date) + range = create_range(start_date, end_date) + if date_in_range(range, date) instance end end @@ -73,8 +74,11 @@ def sort_reservations @reservations.sort_by { |object| object.start_time } end - def date_in_range(start_date, first_date, date) - range = (start_date..first_date) + def date_in_range(range, date) range.include?(date) end + + def create_range(start_date, end_date) + range = (start_date..end_date) + end end diff --git a/lib/room.rb b/lib/room.rb index 6e4147560..f6af7ec47 100644 --- a/lib/room.rb +++ b/lib/room.rb @@ -4,10 +4,15 @@ class Room def initialize(data) @number = data[:number] @status = data[:status] + @ranges = [] end def change_status @status = "unavailable" end + def add_reservation(range) + @ranges << range + end + end From fd9c9a20444a01f42a2cc8c969b17331a2fd9998 Mon Sep 17 00:00:00 2001 From: Laura Date: Thu, 6 Sep 2018 16:58:53 -0700 Subject: [PATCH 16/49] removed create range it is only one line --- lib/admin.rb | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/lib/admin.rb b/lib/admin.rb index 9da134ba4..7bdd6bab4 100644 --- a/lib/admin.rb +++ b/lib/admin.rb @@ -38,8 +38,6 @@ def load_reservations(filename) #As an administrator, I can reserve a room for a given date range def reserve_room(start_date, end_date) - start_date = Time.parse(start_date) - end_date = Time.parse(end_date) @rooms.first.change_status end @@ -54,8 +52,7 @@ def find_reservations(date) reservations = @reservations.select do |instance| start_date = instance.start_time end_date = instance.end_time - range = create_range(start_date, end_date) - if date_in_range(range, date) + if date_in_range(start_date, end_date, date) instance end end @@ -74,11 +71,8 @@ def sort_reservations @reservations.sort_by { |object| object.start_time } end - def date_in_range(range, date) + def date_in_range(start_date, first_date, date) + range = (start_date..first_date) range.include?(date) end - - def create_range(start_date, end_date) - range = (start_date..end_date) - end end From a143e438fc56c28ec236f993309de6e4ab4ff6a8 Mon Sep 17 00:00:00 2001 From: Laura Date: Thu, 6 Sep 2018 17:18:51 -0700 Subject: [PATCH 17/49] removes status instance from room class, and added ranges instance --- lib/admin.rb | 9 ++++++--- lib/room.rb | 7 +------ spec/admin_spec.rb | 3 ++- spec/room_spec.rb | 4 ---- 4 files changed, 9 insertions(+), 14 deletions(-) diff --git a/lib/admin.rb b/lib/admin.rb index 7bdd6bab4..42a75c86b 100644 --- a/lib/admin.rb +++ b/lib/admin.rb @@ -38,7 +38,10 @@ def load_reservations(filename) #As an administrator, I can reserve a room for a given date range def reserve_room(start_date, end_date) - @rooms.first.change_status + start_date = Time.parse(start_date) + end_date = Time.parse(end_date) + range = (start_date..end_date) + @rooms.first.add_reservation(range) end # As an administrator, I can access the list of all of the rooms in the hotel @@ -71,8 +74,8 @@ def sort_reservations @reservations.sort_by { |object| object.start_time } end - def date_in_range(start_date, first_date, date) - range = (start_date..first_date) + def date_in_range(start_date, end_date, date) + range = (start_date..end_date) range.include?(date) end end diff --git a/lib/room.rb b/lib/room.rb index f6af7ec47..894a11877 100644 --- a/lib/room.rb +++ b/lib/room.rb @@ -1,16 +1,11 @@ class Room - attr_reader :number, :status + attr_reader :number, :status, :ranges def initialize(data) @number = data[:number] - @status = data[:status] @ranges = [] end - def change_status - @status = "unavailable" - end - def add_reservation(range) @ranges << range end diff --git a/spec/admin_spec.rb b/spec/admin_spec.rb index 9b5df6fd3..dc476e1df 100644 --- a/spec/admin_spec.rb +++ b/spec/admin_spec.rb @@ -74,10 +74,11 @@ it "can make a reservation" do start_date = "2018-08-07 00:00:00 -0700" end_date = "2018-08-09 00:00:00 -0700" + range = (start_date..end_date) @admin.reserve_room(start_date, end_date) # change status of room rooms = @admin.view_rooms - expect(rooms.first.status).must_equal "unavailable" + expect(rooms.first.ranges).include?(range) end end diff --git a/spec/room_spec.rb b/spec/room_spec.rb index b3f0575d7..4253f0bcc 100644 --- a/spec/room_spec.rb +++ b/spec/room_spec.rb @@ -12,8 +12,4 @@ it "has a room number" do expect(@Room.number).must_equal 1 end - - it "is a status" do - expect(@Room.status).must_equal "available" - end end From 73f3b25eb4f631692623b566e63cdd5e00cfd329 Mon Sep 17 00:00:00 2001 From: Laura Date: Thu, 6 Sep 2018 17:21:47 -0700 Subject: [PATCH 18/49] changes method from view_rooms to view_all_rooms --- lib/admin.rb | 2 +- spec/admin_spec.rb | 4 ++-- spec/room_spec.rb | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/admin.rb b/lib/admin.rb index 42a75c86b..e3c5a9a43 100644 --- a/lib/admin.rb +++ b/lib/admin.rb @@ -45,7 +45,7 @@ def reserve_room(start_date, end_date) end # As an administrator, I can access the list of all of the rooms in the hotel - def view_rooms + def view_all_rooms return @rooms end diff --git a/spec/admin_spec.rb b/spec/admin_spec.rb index dc476e1df..1496bf0f9 100644 --- a/spec/admin_spec.rb +++ b/spec/admin_spec.rb @@ -67,7 +67,7 @@ end it "view all of the rooms" do - rooms = @admin.view_rooms + rooms = @admin.view_all_rooms expect(rooms).must_be_kind_of Array end @@ -77,7 +77,7 @@ range = (start_date..end_date) @admin.reserve_room(start_date, end_date) # change status of room - rooms = @admin.view_rooms + rooms = @admin.view_all_rooms expect(rooms.first.ranges).include?(range) end diff --git a/spec/room_spec.rb b/spec/room_spec.rb index 4253f0bcc..e4af4b1c3 100644 --- a/spec/room_spec.rb +++ b/spec/room_spec.rb @@ -2,7 +2,7 @@ describe "#Room - initialize method" do before do - @Room = Room.new(number: 1, status: "available") + @Room = Room.new(number: 1) end it "is an instance of a Room" do From 65ba818a2d7f6b8d804f241fa8475823082f17a6 Mon Sep 17 00:00:00 2001 From: Laura Date: Thu, 6 Sep 2018 18:43:09 -0700 Subject: [PATCH 19/49] binary search but range greater than target range is not possible --- lib/admin.rb | 44 +++++++++++++++++++++++++++++++++++++++++++- lib/room.rb | 3 +++ spec/admin_spec.rb | 11 +++++++++++ 3 files changed, 57 insertions(+), 1 deletion(-) diff --git a/lib/admin.rb b/lib/admin.rb index e3c5a9a43..dd1679a6d 100644 --- a/lib/admin.rb +++ b/lib/admin.rb @@ -2,11 +2,13 @@ require 'pry' class Admin - attr_reader :reservations, :find_reservation, :rooms + attr_reader :reservations, :find_reservation, :rooms, :vacant_rooms, :booked_rooms def initialize @reservations = load_reservations('spec/test_data/test_reservation.csv') @rooms = create_rooms(20) sort_reservations + @vacant_rooms = [] + @booked_rooms = [] end def create_rooms(number) @@ -37,6 +39,7 @@ def load_reservations(filename) end #As an administrator, I can reserve a room for a given date range + # must be updated to use vacant_rooms def reserve_room(start_date, end_date) start_date = Time.parse(start_date) end_date = Time.parse(end_date) @@ -69,6 +72,21 @@ def find_cost(reservation_id) return cost end + #As an administrator, I can view a list of rooms that are not reserved for a given date range + def view_vacant_rooms(date_range) + @rooms.each do |room| + ranges = room.ranges + length = ranges.length + vacant = binary_search(ranges, length, date_range) + if vacant + booked_rooms << room + else + vacant_rooms << room + end + end + return vacant_rooms + end + private def sort_reservations @reservations.sort_by { |object| object.start_time } @@ -78,4 +96,28 @@ def date_in_range(start_date, end_date, date) range = (start_date..end_date) range.include?(date) end + + def binary_search(array, length, value_to_find) + binding.pry + + low = array[0] + high = array[length - 1] + index = 0 + + while low < high + mid = (low + high) / 2 + if mid == value_to_find + return true + elsif mid > value_to_find + high = mid - 1 + elsif mid < value_to_find + low = mid + 1 + end + if low == value_to_find || high == value_to_find + return true + else + return false + end + end + end end diff --git a/lib/room.rb b/lib/room.rb index 894a11877..b4939c635 100644 --- a/lib/room.rb +++ b/lib/room.rb @@ -10,4 +10,7 @@ def add_reservation(range) @ranges << range end + def sort_ranges + @ranges.sort_by { |range| object.start_time } + end end diff --git a/spec/admin_spec.rb b/spec/admin_spec.rb index 1496bf0f9..2ddba1fc4 100644 --- a/spec/admin_spec.rb +++ b/spec/admin_spec.rb @@ -81,4 +81,15 @@ expect(rooms.first.ranges).include?(range) end + + it "view vacant rooms" do + start_date = "2018-08-07 00:00:00 -0700" + end_date = "2018-08-09 00:00:00 -0700" + @admin.reserve_room(start_date, end_date) + range = (start_date..end_date) + @admin.view_vacant_rooms(range) + + expect(@admin.view_vacant_rooms.first).must_be_kind_of Room + expect(@admin.view_vacant_rooms.first.number).must_equal 1 + end end From 8fae7052bbe4e93fb92d32ca7750512bed8e3257 Mon Sep 17 00:00:00 2001 From: Laura Date: Thu, 6 Sep 2018 19:17:08 -0700 Subject: [PATCH 20/49] admin can view vacant rooms --- lib/admin.rb | 32 ++++++++------------------------ lib/room.rb | 6 +++--- spec/admin_spec.rb | 10 +++++++--- 3 files changed, 18 insertions(+), 30 deletions(-) diff --git a/lib/admin.rb b/lib/admin.rb index dd1679a6d..5eb5a887e 100644 --- a/lib/admin.rb +++ b/lib/admin.rb @@ -73,18 +73,17 @@ def find_cost(reservation_id) end #As an administrator, I can view a list of rooms that are not reserved for a given date range - def view_vacant_rooms(date_range) + def view_vacant_rooms(target_range) @rooms.each do |room| ranges = room.ranges - length = ranges.length - vacant = binary_search(ranges, length, date_range) - if vacant + found = range_search(ranges, target_range) + if found booked_rooms << room else vacant_rooms << room end end - return vacant_rooms + return vacant_rooms end private @@ -97,27 +96,12 @@ def date_in_range(start_date, end_date, date) range.include?(date) end - def binary_search(array, length, value_to_find) - binding.pry - - low = array[0] - high = array[length - 1] - index = 0 - - while low < high - mid = (low + high) / 2 - if mid == value_to_find + def range_search(ranges, target) + ranges.each do |range| + if range == target return true - elsif mid > value_to_find - high = mid - 1 - elsif mid < value_to_find - low = mid + 1 - end - if low == value_to_find || high == value_to_find - return true - else - return false end end + return false end end diff --git a/lib/room.rb b/lib/room.rb index b4939c635..0c6e54120 100644 --- a/lib/room.rb +++ b/lib/room.rb @@ -10,7 +10,7 @@ def add_reservation(range) @ranges << range end - def sort_ranges - @ranges.sort_by { |range| object.start_time } - end + # def sort_ranges + # @ranges.sort_by { |range| object.start_time } + # end end diff --git a/spec/admin_spec.rb b/spec/admin_spec.rb index 2ddba1fc4..e9e909e75 100644 --- a/spec/admin_spec.rb +++ b/spec/admin_spec.rb @@ -83,13 +83,17 @@ end it "view vacant rooms" do + # for reserve_room start_date = "2018-08-07 00:00:00 -0700" end_date = "2018-08-09 00:00:00 -0700" @admin.reserve_room(start_date, end_date) - range = (start_date..end_date) + # for range and testing view vacant_rooms + start_time = Time.parse("2018-08-07 00:00:00 -0700") + end_time = Time.parse("2018-08-09 00:00:00 -0700") + range = (start_time..end_time) @admin.view_vacant_rooms(range) - expect(@admin.view_vacant_rooms.first).must_be_kind_of Room - expect(@admin.view_vacant_rooms.first.number).must_equal 1 + expect(@admin.vacant_rooms.first).must_be_kind_of Room + expect(@admin.vacant_rooms.length).must_equal 19 end end From d50ed7d69732d25a470d7daa4bf28d58e886ba71 Mon Sep 17 00:00:00 2001 From: Laura Date: Thu, 6 Sep 2018 19:52:15 -0700 Subject: [PATCH 21/49] updated reserve room method to take into account vacant room but test for view vacant room broke --- lib/admin.rb | 10 ++++++++-- spec/admin_spec.rb | 8 +++++--- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/lib/admin.rb b/lib/admin.rb index 5eb5a887e..9038c70e8 100644 --- a/lib/admin.rb +++ b/lib/admin.rb @@ -39,12 +39,17 @@ def load_reservations(filename) end #As an administrator, I can reserve a room for a given date range - # must be updated to use vacant_rooms + # continue here: must be updated to use vacant_rooms def reserve_room(start_date, end_date) start_date = Time.parse(start_date) end_date = Time.parse(end_date) range = (start_date..end_date) - @rooms.first.add_reservation(range) + @vacant_rooms = view_vacant_rooms(range) if @vacant_rooms == [] + #returns range + #room = @vacant_rooms.first.add_reservation(range) + vacant_rooms.first.add_reservation(range) + room = @vacant_rooms.first + @booked_rooms << room end # As an administrator, I can access the list of all of the rooms in the hotel @@ -73,6 +78,7 @@ def find_cost(reservation_id) end #As an administrator, I can view a list of rooms that are not reserved for a given date range + ## broke because it I used this in reserve_room def view_vacant_rooms(target_range) @rooms.each do |room| ranges = room.ranges diff --git a/spec/admin_spec.rb b/spec/admin_spec.rb index e9e909e75..5328285d1 100644 --- a/spec/admin_spec.rb +++ b/spec/admin_spec.rb @@ -71,15 +71,17 @@ expect(rooms).must_be_kind_of Array end + # needs to update this test + # before and after from driver it "can make a reservation" do start_date = "2018-08-07 00:00:00 -0700" end_date = "2018-08-09 00:00:00 -0700" range = (start_date..end_date) - @admin.reserve_room(start_date, end_date) # change status of room + @admin.reserve_room(start_date, end_date) - rooms = @admin.view_all_rooms - expect(rooms.first.ranges).include?(range) + expect(@admin.booked_rooms.length).must_equal 1 + end it "view vacant rooms" do From b91977db7ed91ff438554d5256e19554908ae74d Mon Sep 17 00:00:00 2001 From: Laura Date: Fri, 7 Sep 2018 10:13:45 -0700 Subject: [PATCH 22/49] made view vacant room --- lib/admin.rb | 7 ++++--- spec/admin_spec.rb | 6 ++---- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/lib/admin.rb b/lib/admin.rb index 9038c70e8..de06e9f1c 100644 --- a/lib/admin.rb +++ b/lib/admin.rb @@ -7,7 +7,7 @@ def initialize @reservations = load_reservations('spec/test_data/test_reservation.csv') @rooms = create_rooms(20) sort_reservations - @vacant_rooms = [] + #@vacant_rooms = [] @booked_rooms = [] end @@ -44,11 +44,11 @@ def reserve_room(start_date, end_date) start_date = Time.parse(start_date) end_date = Time.parse(end_date) range = (start_date..end_date) - @vacant_rooms = view_vacant_rooms(range) if @vacant_rooms == [] + vacant_rooms = view_vacant_rooms(range) #returns range #room = @vacant_rooms.first.add_reservation(range) vacant_rooms.first.add_reservation(range) - room = @vacant_rooms.first + room = vacant_rooms.first @booked_rooms << room end @@ -80,6 +80,7 @@ def find_cost(reservation_id) #As an administrator, I can view a list of rooms that are not reserved for a given date range ## broke because it I used this in reserve_room def view_vacant_rooms(target_range) + vacant_rooms = [] @rooms.each do |room| ranges = room.ranges found = range_search(ranges, target_range) diff --git a/spec/admin_spec.rb b/spec/admin_spec.rb index 5328285d1..5251ca53c 100644 --- a/spec/admin_spec.rb +++ b/spec/admin_spec.rb @@ -93,9 +93,7 @@ start_time = Time.parse("2018-08-07 00:00:00 -0700") end_time = Time.parse("2018-08-09 00:00:00 -0700") range = (start_time..end_time) - @admin.view_vacant_rooms(range) - - expect(@admin.vacant_rooms.first).must_be_kind_of Room - expect(@admin.vacant_rooms.length).must_equal 19 + vacant_rooms_result = @admin.view_vacant_rooms(range) + expect(vacant_rooms_result.length).must_equal 19 end end From 916f26e565629e8452190f7d919b3c289daaeaf7 Mon Sep 17 00:00:00 2001 From: Laura Date: Fri, 7 Sep 2018 12:54:41 -0700 Subject: [PATCH 23/49] rate is defaults as 200 if it is not specified --- lib/admin.rb | 22 +++++++++++++++++++--- lib/reservation.rb | 11 +++-------- spec/admin_spec.rb | 3 +++ 3 files changed, 25 insertions(+), 11 deletions(-) diff --git a/lib/admin.rb b/lib/admin.rb index de06e9f1c..9118380d7 100644 --- a/lib/admin.rb +++ b/lib/admin.rb @@ -1,6 +1,9 @@ require 'csv' require 'pry' +#REFACTOR_IDEAS: +# create helper method that creates instance of time, and a range + class Admin attr_reader :reservations, :find_reservation, :rooms, :vacant_rooms, :booked_rooms def initialize @@ -39,14 +42,11 @@ def load_reservations(filename) end #As an administrator, I can reserve a room for a given date range - # continue here: must be updated to use vacant_rooms def reserve_room(start_date, end_date) start_date = Time.parse(start_date) end_date = Time.parse(end_date) range = (start_date..end_date) vacant_rooms = view_vacant_rooms(range) - #returns range - #room = @vacant_rooms.first.add_reservation(range) vacant_rooms.first.add_reservation(range) room = vacant_rooms.first @booked_rooms << room @@ -93,6 +93,22 @@ def view_vacant_rooms(target_range) return vacant_rooms end + #As an administrator, I can create a block of rooms + def create_block_rooms(start_date, end_date, discounted_rate) + start_date = Time.parse(start_date) + end_date = Time.parse(end_date) + range = (start_date..end_date) + collection_rooms_options = view_vacant_rooms(range) + # create blocks of five every time? + # collections_rooms # it's an array + # collections_rooms.each do |room| + # room + # end + + + + end + private def sort_reservations @reservations.sort_by { |object| object.start_time } diff --git a/lib/reservation.rb b/lib/reservation.rb index 959d0a9b3..a90d64512 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -1,6 +1,6 @@ +# the roal of this class is to calculate cost of a reservation, and check for invalid range class Reservation attr_reader :start_time, :end_time, :nights, :cost, :reservations, :id - @@RATE = 200 def initialize(data) @id = data[:id] @@ -8,13 +8,8 @@ def initialize(data) @end_time = data[:end_time] sec = @end_time - @start_time @nights = sec.to_i / 86400 - @cost = @nights * @@RATE + rate = 200 if data[:rate].nil? + @cost = @nights * rate raise StandardError if sec < 0 #invalid range end - -# # create a class that creates a class variables that adds reservations - a booking class -# def add_reservation -# @@reservations << self -# end - end diff --git a/spec/admin_spec.rb b/spec/admin_spec.rb index 5251ca53c..feedddd55 100644 --- a/spec/admin_spec.rb +++ b/spec/admin_spec.rb @@ -1,4 +1,7 @@ require_relative 'spec_helper' +# REFACTOR IDEAS: +# replace before with let inside of it, use a nested describe that way I only have to create a single let variable +# # runs from project directory describe "#Admin - initializer" do From b1d79f94a7685d53fa9eff0b8f357973a46a251b Mon Sep 17 00:00:00 2001 From: Laura Date: Fri, 7 Sep 2018 14:07:14 -0700 Subject: [PATCH 24/49] added create range for hotel to not count the last day --- lib/admin.rb | 19 ++++++++++++++++++- spec/admin_spec.rb | 15 +++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/lib/admin.rb b/lib/admin.rb index 9118380d7..cf419b5a8 100644 --- a/lib/admin.rb +++ b/lib/admin.rb @@ -42,6 +42,7 @@ def load_reservations(filename) end #As an administrator, I can reserve a room for a given date range + # uses view_vacant_rooms create range with one day less def reserve_room(start_date, end_date) start_date = Time.parse(start_date) end_date = Time.parse(end_date) @@ -58,6 +59,7 @@ def view_all_rooms end #As an administrator, I can access the list of reservations for a specific date + # do I have to return a reservation that has specific date at the end? def find_reservations(date) date = Time.parse(date) reservations = @reservations.select do |instance| @@ -71,6 +73,7 @@ def find_reservations(date) end #As an administrator, I can get the total cost for a given reservation + #input to be a reservation instance? def find_cost(reservation_id) reservation = @reservations.select { |instance| instance.id == reservation_id }[0] cost = reservation.cost @@ -78,7 +81,9 @@ def find_cost(reservation_id) end #As an administrator, I can view a list of rooms that are not reserved for a given date range - ## broke because it I used this in reserve_room + # it does this by seeing which rooms have the same range, but if not, it marks it as vacant + # raise argument error if array is empty + # must check if date for every single date is in the range def view_vacant_rooms(target_range) vacant_rooms = [] @rooms.each do |room| @@ -109,16 +114,28 @@ def create_block_rooms(start_date, end_date, discounted_rate) end + #last day not counted + def create_hotel_range(start_date, end_date) + start_date = Time.parse(start_date) + end_date = Time.parse(end_date) + end_date = end_date - 1 + range = (start_date .. end_date) + return range + end + private + #never used currently def sort_reservations @reservations.sort_by { |object| object.start_time } end + # it returns true if last day is the target date. Okay for listing all reservations with a specific date def date_in_range(start_date, end_date, date) range = (start_date..end_date) range.include?(date) end + # find range in an array of ranges, NOT USEFUL you need to check every day in the range not just that the ranges are not equal def range_search(ranges, target) ranges.each do |range| if range == target diff --git a/spec/admin_spec.rb b/spec/admin_spec.rb index feedddd55..a2f0b5122 100644 --- a/spec/admin_spec.rb +++ b/spec/admin_spec.rb @@ -100,3 +100,18 @@ expect(vacant_rooms_result.length).must_equal 19 end end + +describe "#range tests" do + before do + @admin = Admin.new + end + + it "does not count the last day" do + start_date = "2018-08-07 00:00:00 -0700" + end_date = "2018-08-09 00:00:00 -0700" + test_date = Time.parse("2018-08-09 00:00:00 -0700") # last day + + result = @admin.create_hotel_range(start_date, end_date) + expect(result.include?(test_date)).must_equal false + end +end From 65fd636a1b6f6fde0ed1fca3a2e750f7a921de5b Mon Sep 17 00:00:00 2001 From: Laura Date: Fri, 7 Sep 2018 14:40:36 -0700 Subject: [PATCH 25/49] fixed find cost method to intake a instance of a reservation --- lib/admin.rb | 11 ++++------- spec/admin_spec.rb | 8 ++++---- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/lib/admin.rb b/lib/admin.rb index cf419b5a8..41b0d1b37 100644 --- a/lib/admin.rb +++ b/lib/admin.rb @@ -44,10 +44,8 @@ def load_reservations(filename) #As an administrator, I can reserve a room for a given date range # uses view_vacant_rooms create range with one day less def reserve_room(start_date, end_date) - start_date = Time.parse(start_date) - end_date = Time.parse(end_date) - range = (start_date..end_date) - vacant_rooms = view_vacant_rooms(range) + range = create_hotel_range(start_date, end_date) + vacant_rooms = view_vacant_rooms(range) # needs to be updated vacant_rooms.first.add_reservation(range) room = vacant_rooms.first @booked_rooms << room @@ -59,7 +57,7 @@ def view_all_rooms end #As an administrator, I can access the list of reservations for a specific date - # do I have to return a reservation that has specific date at the end? + # do I have to return a reservation that has specific date at the end? No def find_reservations(date) date = Time.parse(date) reservations = @reservations.select do |instance| @@ -74,8 +72,7 @@ def find_reservations(date) #As an administrator, I can get the total cost for a given reservation #input to be a reservation instance? - def find_cost(reservation_id) - reservation = @reservations.select { |instance| instance.id == reservation_id }[0] + def find_cost(reservation) cost = reservation.cost return cost end diff --git a/spec/admin_spec.rb b/spec/admin_spec.rb index a2f0b5122..0bb947792 100644 --- a/spec/admin_spec.rb +++ b/spec/admin_spec.rb @@ -54,12 +54,11 @@ it "return the cost of a reservation" do #arrange - reservation_id = 2 - - reservation_cost = @admin.find_cost(reservation_id) + reservation = @admin.reservations.first + reservation_cost = @admin.find_cost(reservation) expect(reservation_cost).must_be_kind_of Integer - expect(reservation_cost).must_equal 400 + expect(reservation_cost).must_equal 200 end end @@ -88,6 +87,7 @@ end it "view vacant rooms" do + skip # for reserve_room start_date = "2018-08-07 00:00:00 -0700" end_date = "2018-08-09 00:00:00 -0700" From 1c96e4682d672380a33bee8836793a5f848b377c Mon Sep 17 00:00:00 2001 From: Laura Date: Fri, 7 Sep 2018 15:05:54 -0700 Subject: [PATCH 26/49] find reservation method does not return a reservation that has target date at the end --- lib/admin.rb | 14 +++++++------- spec/admin_spec.rb | 5 +++-- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/lib/admin.rb b/lib/admin.rb index 41b0d1b37..f55a8de90 100644 --- a/lib/admin.rb +++ b/lib/admin.rb @@ -34,7 +34,7 @@ def load_reservations(filename) input_data = {} input_data[:id] = data["id"].to_i input_data[:start_time] = Time.parse(data["start_time"]) - input_data[:end_time] = Time.parse(data["end_time"]) + input_data[:end_time] = Time.parse(data["end_time"]) # no need to subtract last day because reservation class only calculates cost reservations << Reservation.new(input_data) end @@ -44,6 +44,8 @@ def load_reservations(filename) #As an administrator, I can reserve a room for a given date range # uses view_vacant_rooms create range with one day less def reserve_room(start_date, end_date) + start_date = Time.parse(start_date) + end_date = Time.parse(end_date) range = create_hotel_range(start_date, end_date) vacant_rooms = view_vacant_rooms(range) # needs to be updated vacant_rooms.first.add_reservation(range) @@ -57,7 +59,7 @@ def view_all_rooms end #As an administrator, I can access the list of reservations for a specific date - # do I have to return a reservation that has specific date at the end? No + # I do not have to return a reservation that has specific date at the end def find_reservations(date) date = Time.parse(date) reservations = @reservations.select do |instance| @@ -71,7 +73,7 @@ def find_reservations(date) end #As an administrator, I can get the total cost for a given reservation - #input to be a reservation instance? + #input is a reservation instance def find_cost(reservation) cost = reservation.cost return cost @@ -113,8 +115,6 @@ def create_block_rooms(start_date, end_date, discounted_rate) #last day not counted def create_hotel_range(start_date, end_date) - start_date = Time.parse(start_date) - end_date = Time.parse(end_date) end_date = end_date - 1 range = (start_date .. end_date) return range @@ -126,9 +126,9 @@ def sort_reservations @reservations.sort_by { |object| object.start_time } end - # it returns true if last day is the target date. Okay for listing all reservations with a specific date + # does not take into account last day of reservation def date_in_range(start_date, end_date, date) - range = (start_date..end_date) + range = create_hotel_range(start_date, end_date) range.include?(date) end diff --git a/spec/admin_spec.rb b/spec/admin_spec.rb index 0bb947792..ef3fe6532 100644 --- a/spec/admin_spec.rb +++ b/spec/admin_spec.rb @@ -45,11 +45,10 @@ it "return the reservations that have a specific date as a start date" do #arrange date = "2018-08-07 00:00:00 -0700" - reservations = @admin.find_reservations(date) expect(reservations).must_be_kind_of Array - expect(reservations.first.id).must_equal 2 + expect(reservations.length).must_equal 1 end it "return the cost of a reservation" do @@ -109,6 +108,8 @@ it "does not count the last day" do start_date = "2018-08-07 00:00:00 -0700" end_date = "2018-08-09 00:00:00 -0700" + start_date = Time.parse(start_date) + end_date = Time.parse(end_date) test_date = Time.parse("2018-08-09 00:00:00 -0700") # last day result = @admin.create_hotel_range(start_date, end_date) From a0360c7648f7de58c9832b6b02fdee29a622f319 Mon Sep 17 00:00:00 2001 From: Laura Date: Fri, 7 Sep 2018 15:33:49 -0700 Subject: [PATCH 27/49] can make a reservation without checking availability --- lib/admin.rb | 24 +++++++++++++----------- lib/room.rb | 3 ++- spec/admin_spec.rb | 13 ++++++++++--- 3 files changed, 25 insertions(+), 15 deletions(-) diff --git a/lib/admin.rb b/lib/admin.rb index f55a8de90..05f9990a2 100644 --- a/lib/admin.rb +++ b/lib/admin.rb @@ -20,7 +20,6 @@ def create_rooms(number) input_data = {} room_number = num + 1 input_data[:number] = room_number - input_data[:status] = "available" rooms << Room.new(input_data) end return rooms @@ -47,10 +46,11 @@ def reserve_room(start_date, end_date) start_date = Time.parse(start_date) end_date = Time.parse(end_date) range = create_hotel_range(start_date, end_date) - vacant_rooms = view_vacant_rooms(range) # needs to be updated - vacant_rooms.first.add_reservation(range) - room = vacant_rooms.first - @booked_rooms << room + @rooms.first.add_range(range) # temporary + # vacant_rooms = view_vacant_rooms(range) # needs to be updated + # vacant_rooms.first.add_range(range) + # room = vacant_rooms.first + #@booked_rooms << room end # As an administrator, I can access the list of all of the rooms in the hotel @@ -87,12 +87,13 @@ def view_vacant_rooms(target_range) vacant_rooms = [] @rooms.each do |room| ranges = room.ranges - found = range_search(ranges, target_range) - if found - booked_rooms << room - else - vacant_rooms << room - end + binding.pry + # found = range_search(ranges, target_range) + # if found + # booked_rooms << room + # else + # vacant_rooms << room + # end end return vacant_rooms end @@ -114,6 +115,7 @@ def create_block_rooms(start_date, end_date, discounted_rate) end #last day not counted + #expect input to be time instances def create_hotel_range(start_date, end_date) end_date = end_date - 1 range = (start_date .. end_date) diff --git a/lib/room.rb b/lib/room.rb index 0c6e54120..51ecd6d91 100644 --- a/lib/room.rb +++ b/lib/room.rb @@ -3,10 +3,11 @@ class Room def initialize(data) @number = data[:number] + # rooms do not store the last day. Last day is reservation last day - 1 @ranges = [] end - def add_reservation(range) + def add_range(range) @ranges << range end diff --git a/spec/admin_spec.rb b/spec/admin_spec.rb index ef3fe6532..11b370f66 100644 --- a/spec/admin_spec.rb +++ b/spec/admin_spec.rb @@ -77,11 +77,17 @@ it "can make a reservation" do start_date = "2018-08-07 00:00:00 -0700" end_date = "2018-08-09 00:00:00 -0700" - range = (start_date..end_date) @admin.reserve_room(start_date, end_date) - - expect(@admin.booked_rooms.length).must_equal 1 + # reserve room removes last day because it is not counted as a paying night + start_date = "2018-08-07 00:00:00 -0700" + end_date = "2018-08-09 00:00:00 -0700" + start_date = Time.parse(start_date) + end_date = Time.parse(end_date) + end_date = end_date - 1 + range = [(start_date..end_date)] + expect(@admin.rooms.first.ranges). must_equal range + #expect(@admin.booked_rooms.length).must_equal 1 end @@ -94,6 +100,7 @@ # for range and testing view vacant_rooms start_time = Time.parse("2018-08-07 00:00:00 -0700") end_time = Time.parse("2018-08-09 00:00:00 -0700") + end_date = end_date - 1 range = (start_time..end_time) vacant_rooms_result = @admin.view_vacant_rooms(range) expect(vacant_rooms_result.length).must_equal 19 From 04de7862a5dfbb60c0e65b960b5c528d98789bf6 Mon Sep 17 00:00:00 2001 From: Laura Date: Fri, 7 Sep 2018 15:53:27 -0700 Subject: [PATCH 28/49] created array of dates --- lib/admin.rb | 47 +++++++++++++++++++++++++++------------------- spec/admin_spec.rb | 12 +++++++++++- 2 files changed, 39 insertions(+), 20 deletions(-) diff --git a/lib/admin.rb b/lib/admin.rb index 05f9990a2..05e23d21b 100644 --- a/lib/admin.rb +++ b/lib/admin.rb @@ -53,6 +53,23 @@ def reserve_room(start_date, end_date) #@booked_rooms << room end + #As an administrator, I can view a list of rooms that are not reserved for a given date range + # raise argument error if array is empty + # must check if date for every single date is in the range + ## expects dates to be instances of time + def view_vacant_rooms(start_date, end_date) + vacant_rooms = [] + @rooms.each do |room| + ranges = room.ranges + # found = range_search(ranges, target_range) + # if found + # booked_rooms << room + # else + # vacant_rooms << room + # end + end + return vacant_rooms + end # As an administrator, I can access the list of all of the rooms in the hotel def view_all_rooms return @rooms @@ -79,25 +96,6 @@ def find_cost(reservation) return cost end - #As an administrator, I can view a list of rooms that are not reserved for a given date range - # it does this by seeing which rooms have the same range, but if not, it marks it as vacant - # raise argument error if array is empty - # must check if date for every single date is in the range - def view_vacant_rooms(target_range) - vacant_rooms = [] - @rooms.each do |room| - ranges = room.ranges - binding.pry - # found = range_search(ranges, target_range) - # if found - # booked_rooms << room - # else - # vacant_rooms << room - # end - end - return vacant_rooms - end - #As an administrator, I can create a block of rooms def create_block_rooms(start_date, end_date, discounted_rate) start_date = Time.parse(start_date) @@ -122,6 +120,17 @@ def create_hotel_range(start_date, end_date) return range end + # expectes input to be isntances + def create_array_of_dates(start_date, end_date) + dates = [] + difference = end_date - start_date + difference = difference/86400 + difference.to_i.times do |i| + dates << start_date + (1+i) + end + return dates + end + private #never used currently def sort_reservations diff --git a/spec/admin_spec.rb b/spec/admin_spec.rb index 11b370f66..320a093eb 100644 --- a/spec/admin_spec.rb +++ b/spec/admin_spec.rb @@ -74,6 +74,8 @@ # needs to update this test # before and after from driver + # needs to test when reservation is 1 - 2 + # and another one with reservation 1 - 3, should include 2 rooms it "can make a reservation" do start_date = "2018-08-07 00:00:00 -0700" end_date = "2018-08-09 00:00:00 -0700" @@ -99,7 +101,8 @@ @admin.reserve_room(start_date, end_date) # for range and testing view vacant_rooms start_time = Time.parse("2018-08-07 00:00:00 -0700") - end_time = Time.parse("2018-08-09 00:00:00 -0700") + # one day less because the last night is not payed and view_vacant_rooms takes care of that + end_time = Time.parse("2018-08-08 00:00:00 -0700") end_date = end_date - 1 range = (start_time..end_time) vacant_rooms_result = @admin.view_vacant_rooms(range) @@ -122,4 +125,11 @@ result = @admin.create_hotel_range(start_date, end_date) expect(result.include?(test_date)).must_equal false end + + it "it creates an array of dates" do + start_date = Time.parse("2018-08-07 00:00:00 -0700") + end_date = Time.parse("2018-08-10 00:00:00 -0700") + result = @admin.create_array_of_dates(start_date, end_date) + expect(result.length).must_equal 3 + end end From 0ea9157111f2ba949cdc9aa9923ad9dbc506b54d Mon Sep 17 00:00:00 2001 From: Laura Date: Sat, 8 Sep 2018 12:55:40 -0700 Subject: [PATCH 29/49] find reservations does not include the last day --- lib/admin.rb | 19 +++++++++++++++---- spec/admin_spec.rb | 10 +++++++--- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/lib/admin.rb b/lib/admin.rb index 05e23d21b..13c3a8faf 100644 --- a/lib/admin.rb +++ b/lib/admin.rb @@ -58,9 +58,16 @@ def reserve_room(start_date, end_date) # must check if date for every single date is in the range ## expects dates to be instances of time def view_vacant_rooms(start_date, end_date) + end_date = end_date - 1 # last day does not count vacant_rooms = [] + target_dates = create_array_of_dates(start_date, end_date) @rooms.each do |room| ranges = room.ranges + # does range include every date of target_dates? if true do not include in vacant_rooms + ranges.each do |ranges| + + end + # found = range_search(ranges, target_range) # if found # booked_rooms << room @@ -77,11 +84,15 @@ def view_all_rooms #As an administrator, I can access the list of reservations for a specific date # I do not have to return a reservation that has specific date at the end + # reservations have real checkout date + # rooms have dates - 1 + # REFACTOR idea to keep take out -1 day when you load reservation, but reservations calculates the cost based of nights - I wil have to fix that too def find_reservations(date) date = Time.parse(date) reservations = @reservations.select do |instance| start_date = instance.start_time end_date = instance.end_time + end_date = end_date - 1 # make test to only get the right one if date_in_range(start_date, end_date, date) instance end @@ -120,7 +131,7 @@ def create_hotel_range(start_date, end_date) return range end - # expectes input to be isntances + # expects input to be time instances def create_array_of_dates(start_date, end_date) dates = [] difference = end_date - start_date @@ -137,13 +148,13 @@ def sort_reservations @reservations.sort_by { |object| object.start_time } end - # does not take into account last day of reservation def date_in_range(start_date, end_date, date) - range = create_hotel_range(start_date, end_date) + range = (start_date .. end_date) range.include?(date) end - # find range in an array of ranges, NOT USEFUL you need to check every day in the range not just that the ranges are not equal + # find range in an array of ranges, + #NOT USEFUL you need to check every day in the range not just that the ranges are not equal def range_search(ranges, target) ranges.each do |range| if range == target diff --git a/spec/admin_spec.rb b/spec/admin_spec.rb index 320a093eb..05c5b9409 100644 --- a/spec/admin_spec.rb +++ b/spec/admin_spec.rb @@ -42,13 +42,17 @@ before do @admin = Admin.new end - it "return the reservations that have a specific date as a start date" do + it "return the reservations that have a specific date, not including the last day" do #arrange - date = "2018-08-07 00:00:00 -0700" + # reservations are: + # 2,2018-08-07,2018-08-09 + # 1,2018-08-08,2018-08-09 + + date = "2018-08-08 00:00:00 -0700" reservations = @admin.find_reservations(date) expect(reservations).must_be_kind_of Array - expect(reservations.length).must_equal 1 + expect(reservations.length).must_equal 2 end it "return the cost of a reservation" do From 04b1f1f47f4b2f8dd36a0e52c820cfad5e6d66e7 Mon Sep 17 00:00:00 2001 From: Laura Date: Sat, 8 Sep 2018 15:52:27 -0700 Subject: [PATCH 30/49] vacant method checks every day in the array of ranges --- lib/admin.rb | 49 ++++++++++++++++++++++++---------------------- spec/admin_spec.rb | 10 ++++------ 2 files changed, 30 insertions(+), 29 deletions(-) diff --git a/lib/admin.rb b/lib/admin.rb index 13c3a8faf..6970596ef 100644 --- a/lib/admin.rb +++ b/lib/admin.rb @@ -46,37 +46,32 @@ def reserve_room(start_date, end_date) start_date = Time.parse(start_date) end_date = Time.parse(end_date) range = create_hotel_range(start_date, end_date) - @rooms.first.add_range(range) # temporary + @rooms.first.add_range(range) # temporary - must be updated once view_vacant_rooms is fixed # vacant_rooms = view_vacant_rooms(range) # needs to be updated # vacant_rooms.first.add_range(range) # room = vacant_rooms.first - #@booked_rooms << room + # must create instance of reservation for this room using one more day end #As an administrator, I can view a list of rooms that are not reserved for a given date range # raise argument error if array is empty - # must check if date for every single date is in the range ## expects dates to be instances of time def view_vacant_rooms(start_date, end_date) - end_date = end_date - 1 # last day does not count - vacant_rooms = [] - target_dates = create_array_of_dates(start_date, end_date) - @rooms.each do |room| - ranges = room.ranges - # does range include every date of target_dates? if true do not include in vacant_rooms - ranges.each do |ranges| - - end - - # found = range_search(ranges, target_range) - # if found - # booked_rooms << room - # else - # vacant_rooms << room - # end + vacant_rooms = @rooms + target_range = create_hotel_range(start_date, end_date) + vacant_rooms.each do |room| + ranges = room.ranges + ranges.each do |range| + # nil means no intersection + if intersection(target_range, range) != nil # there was a overlap + vacant_rooms.delete(room) + break + end + end end return vacant_rooms end + # As an administrator, I can access the list of all of the rooms in the hotel def view_all_rooms return @rooms @@ -84,15 +79,15 @@ def view_all_rooms #As an administrator, I can access the list of reservations for a specific date # I do not have to return a reservation that has specific date at the end - # reservations have real checkout date - # rooms have dates - 1 - # REFACTOR idea to keep take out -1 day when you load reservation, but reservations calculates the cost based of nights - I wil have to fix that too + # In my program, the reservations have real checkout date + # In my program, the room class have real dates, not including the last check out date + # REFACTOR idea: to keep take out -1 day when you load reservation, but reservations calculates the cost based of nights - I wil have to fix that too def find_reservations(date) date = Time.parse(date) reservations = @reservations.select do |instance| start_date = instance.start_time end_date = instance.end_time - end_date = end_date - 1 # make test to only get the right one + end_date = end_date - 1 if date_in_range(start_date, end_date, date) instance end @@ -148,6 +143,14 @@ def sort_reservations @reservations.sort_by { |object| object.start_time } end + def intersection(range, range2) + if (range.max < range2.begin or range2.max < range.begin) + return nil + else + return [range.begin, range2.begin].max..[range.max, range2.max].min + end + end + def date_in_range(start_date, end_date, date) range = (start_date .. end_date) range.include?(date) diff --git a/spec/admin_spec.rb b/spec/admin_spec.rb index 05c5b9409..a7041937c 100644 --- a/spec/admin_spec.rb +++ b/spec/admin_spec.rb @@ -98,18 +98,16 @@ end it "view vacant rooms" do - skip # for reserve_room start_date = "2018-08-07 00:00:00 -0700" end_date = "2018-08-09 00:00:00 -0700" @admin.reserve_room(start_date, end_date) # for range and testing view vacant_rooms - start_time = Time.parse("2018-08-07 00:00:00 -0700") + s_date = Time.parse("2018-08-07 00:00:00 -0700") # one day less because the last night is not payed and view_vacant_rooms takes care of that - end_time = Time.parse("2018-08-08 00:00:00 -0700") - end_date = end_date - 1 - range = (start_time..end_time) - vacant_rooms_result = @admin.view_vacant_rooms(range) + e_date = Time.parse("2018-08-08 00:00:00 -0700") + e_date = e_date - 1 + vacant_rooms_result = @admin.view_vacant_rooms(s_date, e_date) expect(vacant_rooms_result.length).must_equal 19 end end From 568f72382c181cd06023732c2dcb27fd10e04b15 Mon Sep 17 00:00:00 2001 From: Laura Date: Sun, 9 Sep 2018 17:57:29 -0700 Subject: [PATCH 31/49] no rooms available for 20 reservations with the same date --- lib/admin.rb | 22 +++++++++++++++++----- lib/reservation.rb | 5 +++-- spec/admin_spec.rb | 24 ++++++++++++++++-------- 3 files changed, 36 insertions(+), 15 deletions(-) diff --git a/lib/admin.rb b/lib/admin.rb index 6970596ef..5243b57c5 100644 --- a/lib/admin.rb +++ b/lib/admin.rb @@ -34,6 +34,7 @@ def load_reservations(filename) input_data[:id] = data["id"].to_i input_data[:start_time] = Time.parse(data["start_time"]) input_data[:end_time] = Time.parse(data["end_time"]) # no need to subtract last day because reservation class only calculates cost + # REFACTOR idea: make the reservation instance a separate method - isolating dependencies reservations << Reservation.new(input_data) end @@ -45,11 +46,22 @@ def load_reservations(filename) def reserve_room(start_date, end_date) start_date = Time.parse(start_date) end_date = Time.parse(end_date) - range = create_hotel_range(start_date, end_date) - @rooms.first.add_range(range) # temporary - must be updated once view_vacant_rooms is fixed - # vacant_rooms = view_vacant_rooms(range) # needs to be updated - # vacant_rooms.first.add_range(range) - # room = vacant_rooms.first + range = create_hotel_range(start_date, end_date) # for hotel + e_date = end_date - 1 # rooms do not keep track of last night + vacant_rooms = view_vacant_rooms(start_date, e_date) + if vacant_rooms.nil? + # REFACTOR idea: begin rescue block, exception handleing + raise ArgumentError, "no rooms are available" + else + room = vacant_rooms.first + rooms.first.add_range(range) + input_data = {} + input_data[:start_time] = start_date + input_data[:end_time] = end_date + input_data[:room_num] = room.number + reservations << Reservation.new(input_data) + end + # must create instance of reservation for this room using one more day end diff --git a/lib/reservation.rb b/lib/reservation.rb index a90d64512..b0145f5ec 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -1,11 +1,12 @@ # the roal of this class is to calculate cost of a reservation, and check for invalid range class Reservation - attr_reader :start_time, :end_time, :nights, :cost, :reservations, :id + attr_reader :start_time, :end_time, :nights, :cost, :reservations, :id, :room_num def initialize(data) - @id = data[:id] + #@id = data[:id] || 0 @start_time = data[:start_time] @end_time = data[:end_time] + @room_num = data[:room_num] || 0 sec = @end_time - @start_time @nights = sec.to_i / 86400 rate = 200 if data[:rate].nil? diff --git a/spec/admin_spec.rb b/spec/admin_spec.rb index a7041937c..73d668675 100644 --- a/spec/admin_spec.rb +++ b/spec/admin_spec.rb @@ -4,7 +4,7 @@ # # runs from project directory -describe "#Admin - initializer" do +xdescribe "#Admin - initializer" do before do @admin = Admin.new end @@ -38,7 +38,7 @@ end end -describe "#reservation information" do +xdescribe "#reservation information" do before do @admin = Admin.new end @@ -66,7 +66,7 @@ end -describe "#rooms information" do +describe "#rooms information and reservation" do before do @admin = Admin.new end @@ -76,8 +76,8 @@ expect(rooms).must_be_kind_of Array end - # needs to update this test - # before and after from driver + # REFACTOR idea: + # before and after count of rooms from driver # needs to test when reservation is 1 - 2 # and another one with reservation 1 - 3, should include 2 rooms it "can make a reservation" do @@ -85,7 +85,7 @@ end_date = "2018-08-09 00:00:00 -0700" @admin.reserve_room(start_date, end_date) - # reserve room removes last day because it is not counted as a paying night + # reserve room removes last day because it is not counted as a paying night for my reservation class start_date = "2018-08-07 00:00:00 -0700" end_date = "2018-08-09 00:00:00 -0700" start_date = Time.parse(start_date) @@ -93,8 +93,16 @@ end_date = end_date - 1 range = [(start_date..end_date)] expect(@admin.rooms.first.ranges). must_equal range - #expect(@admin.booked_rooms.length).must_equal 1 + end + it "no rooms available for 20 reservations" do + start_date = Time.parse("2018-08-07 00:00:00 -0700") + end_date = Time.parse("2018-08-09 00:00:00 -0700") + expect { + 20.times do + @admin.make_reservation(start_date, end_date) + end + }.must_raise StandardError end it "view vacant rooms" do @@ -112,7 +120,7 @@ end end -describe "#range tests" do +xdescribe "#range tests" do before do @admin = Admin.new end From c6ad0c03041be99974c6f999d6129fc135023440 Mon Sep 17 00:00:00 2001 From: Laura Date: Sun, 9 Sep 2018 18:54:20 -0700 Subject: [PATCH 32/49] create block rooms --- lib/admin.rb | 46 ++++++++++++++++++++++++++++++++-------------- lib/room.rb | 10 +++++++++- spec/admin_spec.rb | 25 +++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 15 deletions(-) diff --git a/lib/admin.rb b/lib/admin.rb index 5243b57c5..cd159956e 100644 --- a/lib/admin.rb +++ b/lib/admin.rb @@ -10,8 +10,6 @@ def initialize @reservations = load_reservations('spec/test_data/test_reservation.csv') @rooms = create_rooms(20) sort_reservations - #@vacant_rooms = [] - @booked_rooms = [] end def create_rooms(number) @@ -51,7 +49,7 @@ def reserve_room(start_date, end_date) vacant_rooms = view_vacant_rooms(start_date, e_date) if vacant_rooms.nil? # REFACTOR idea: begin rescue block, exception handleing - raise ArgumentError, "no rooms are available" + raise StandardError, "no rooms are available" else room = vacant_rooms.first rooms.first.add_range(range) @@ -73,6 +71,7 @@ def view_vacant_rooms(start_date, end_date) target_range = create_hotel_range(start_date, end_date) vacant_rooms.each do |room| ranges = room.ranges + blocks = room.blocks ranges.each do |range| # nil means no intersection if intersection(target_range, range) != nil # there was a overlap @@ -80,6 +79,14 @@ def view_vacant_rooms(start_date, end_date) break end end + # remove rooms if it has blocks on that date + blocks.each do |block| + # nil means no intersection + if intersection(target_range, blocks[:range]) != nil # there was a overlap + vacant_rooms.delete(room) + break + end + end end return vacant_rooms end @@ -115,17 +122,28 @@ def find_cost(reservation) end #As an administrator, I can create a block of rooms - def create_block_rooms(start_date, end_date, discounted_rate) - start_date = Time.parse(start_date) - end_date = Time.parse(end_date) - range = (start_date..end_date) - collection_rooms_options = view_vacant_rooms(range) - # create blocks of five every time? - # collections_rooms # it's an array - # collections_rooms.each do |room| - # room - # end - + def create_block_rooms(data) + start_date = Time.parse(data[:start_date]) + end_date = Time.parse(data[:end_date]) + e_date = end_date - 1 + range = (start_date..e_date) + rooms = data[:rooms] + discounted_rate = data[:discounted_rate] + available_rooms = view_vacant_rooms(start_date, e_date) + + if available_rooms.length < rooms + return "no available rooms for block" + else + max = rooms - 1 # because of the 0 index + block_rooms = available_rooms[0 .. max] + input_data = {} + input_data[:discounted_rate] = discounted_rate + input_data[:range] = range + input_data[:status] = "available" + block_rooms.each do |room| + room.add_block(input_data) + end + end end diff --git a/lib/room.rb b/lib/room.rb index 51ecd6d91..7d7b2475f 100644 --- a/lib/room.rb +++ b/lib/room.rb @@ -1,16 +1,24 @@ class Room - attr_reader :number, :status, :ranges + attr_reader :number, :status, :ranges, :blocks def initialize(data) @number = data[:number] # rooms do not store the last day. Last day is reservation last day - 1 @ranges = [] + @blocks = [] end def add_range(range) @ranges << range end + def add_block(data) + input_data = {} + input_data[:range] = data[:range] + input_data[:discounted_rate] = data[:discounted_rate] + input_data[:status] = data[:status] + @blocks << input_data + end # def sort_ranges # @ranges.sort_by { |range| object.start_time } # end diff --git a/spec/admin_spec.rb b/spec/admin_spec.rb index 73d668675..2333e360e 100644 --- a/spec/admin_spec.rb +++ b/spec/admin_spec.rb @@ -117,6 +117,7 @@ e_date = e_date - 1 vacant_rooms_result = @admin.view_vacant_rooms(s_date, e_date) expect(vacant_rooms_result.length).must_equal 19 + # do I delete rooms from rooms? check for that end end @@ -143,3 +144,27 @@ expect(result.length).must_equal 3 end end + +describe "#blocks" do + before do + @admin = Admin.new + end + + it "create a block of rooms" do + start_date = "2018-08-07 00:00:00 -0700" + end_date = "2018-08-09 00:00:00 -0700" + @admin.reserve_room(start_date, end_date) + # for range and testing view vacant_rooms + # one day less because the last night is not payed and view_vacant_rooms takes care of that + + data = {} + data[:start_date] = start_date + data[:end_date] = end_date + data[:rooms] = 4 + data[:discounted_rate] = 100 + @admin.create_block_rooms(data) + (0..3).each do |i| + expect(@admin.rooms[i].blocks).wont_be_empty + end + end +end From 5ab322ae77846ba5cce455c6275e4f15b1893036 Mon Sep 17 00:00:00 2001 From: Laura Date: Sun, 9 Sep 2018 18:58:24 -0700 Subject: [PATCH 33/49] created test to not allow admin to create a block for more than 5 rooms --- lib/admin.rb | 1 + spec/admin_spec.rb | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/lib/admin.rb b/lib/admin.rb index cd159956e..1ea9fd3cd 100644 --- a/lib/admin.rb +++ b/lib/admin.rb @@ -128,6 +128,7 @@ def create_block_rooms(data) e_date = end_date - 1 range = (start_date..e_date) rooms = data[:rooms] + raise StandardError if rooms > 5 #block discounted_rate = data[:discounted_rate] available_rooms = view_vacant_rooms(start_date, e_date) diff --git a/spec/admin_spec.rb b/spec/admin_spec.rb index 2333e360e..9d0595fdc 100644 --- a/spec/admin_spec.rb +++ b/spec/admin_spec.rb @@ -80,6 +80,7 @@ # before and after count of rooms from driver # needs to test when reservation is 1 - 2 # and another one with reservation 1 - 3, should include 2 rooms + # needs a test with arrange to have block rooms and to check result it "can make a reservation" do start_date = "2018-08-07 00:00:00 -0700" end_date = "2018-08-09 00:00:00 -0700" @@ -167,4 +168,22 @@ expect(@admin.rooms[i].blocks).wont_be_empty end end + + it "raises an Standard error for invalid rooms for block" do + start_date = "2018-08-07 00:00:00 -0700" + end_date = "2018-08-09 00:00:00 -0700" + @admin.reserve_room(start_date, end_date) + # for range and testing view vacant_rooms + # one day less because the last night is not payed and view_vacant_rooms takes care of that + + data = {} + data[:start_date] = start_date + data[:end_date] = end_date + data[:rooms] = 6 + data[:discounted_rate] = 100 + expect{ + @admin.create_block_rooms(data) + }.must_raise StandardError + end + end From 2b9fd30bc860c8dfb82841e260782344d9e938b7 Mon Sep 17 00:00:00 2001 From: Laura Date: Sun, 9 Sep 2018 19:51:04 -0700 Subject: [PATCH 34/49] sucessfuly updates status of room in a block --- lib/admin.rb | 10 +++++++++- lib/room.rb | 5 +++++ spec/admin_spec.rb | 35 +++++++++++++++++++++++++++++++++-- 3 files changed, 47 insertions(+), 3 deletions(-) diff --git a/lib/admin.rb b/lib/admin.rb index 1ea9fd3cd..cc8ca3eda 100644 --- a/lib/admin.rb +++ b/lib/admin.rb @@ -128,7 +128,7 @@ def create_block_rooms(data) e_date = end_date - 1 range = (start_date..e_date) rooms = data[:rooms] - raise StandardError if rooms > 5 #block + raise StandardError if rooms > 5 #max block size discounted_rate = data[:discounted_rate] available_rooms = view_vacant_rooms(start_date, e_date) @@ -149,6 +149,14 @@ def create_block_rooms(data) end +# As an administrator, I can reserve a room from within a block of rooms + def reserve_room_in_block(data) + room_num = data[:room_num] + range = data[:range] + room = @rooms.select { |room| room.number == room_num}[0] + room.reserve_room_block(range) + end + #last day not counted #expect input to be time instances def create_hotel_range(start_date, end_date) diff --git a/lib/room.rb b/lib/room.rb index 7d7b2475f..1abf6117a 100644 --- a/lib/room.rb +++ b/lib/room.rb @@ -19,6 +19,11 @@ def add_block(data) input_data[:status] = data[:status] @blocks << input_data end + + def reserve_room_block(range) + target_block = blocks.select {|block| block[:range] == range}[0] + target_block[:status] = "booked" + end # def sort_ranges # @ranges.sort_by { |range| object.start_time } # end diff --git a/spec/admin_spec.rb b/spec/admin_spec.rb index 9d0595fdc..7919fe44b 100644 --- a/spec/admin_spec.rb +++ b/spec/admin_spec.rb @@ -152,13 +152,14 @@ end it "create a block of rooms" do + # reserve a normal room start_date = "2018-08-07 00:00:00 -0700" end_date = "2018-08-09 00:00:00 -0700" @admin.reserve_room(start_date, end_date) - # for range and testing view vacant_rooms - # one day less because the last night is not payed and view_vacant_rooms takes care of that + # reserve a block room data = {} + data[:block_id] = 1 data[:start_date] = start_date data[:end_date] = end_date data[:rooms] = 4 @@ -177,6 +178,7 @@ # one day less because the last night is not payed and view_vacant_rooms takes care of that data = {} + data[:block_id] = 1 data[:start_date] = start_date data[:end_date] = end_date data[:rooms] = 6 @@ -186,4 +188,33 @@ }.must_raise StandardError end + it "updates status of room block" do + start_date = "2018-08-07 00:00:00 -0700" + end_date = "2018-08-09 00:00:00 -0700" + @admin.reserve_room(start_date, end_date) + # for range and testing view vacant_rooms + # one day less because the last night is not payed and view_vacant_rooms takes care of that + + data = {} + data[:block_id] = 1 + data[:start_date] = start_date + data[:end_date] = end_date + data[:rooms] = 4 + data[:discounted_rate] = 100 + @admin.create_block_rooms(data) + info = {} + + start_date = Time.parse("2018-08-07 00:00:00 -0700") + end_date = Time.parse("2018-08-09 00:00:00 -0700") + e_date = end_date - 1 + range = (start_date .. e_date) + info = {} + info[:room_num] = 2 + info[:range] = range + @admin.reserve_room_in_block(info) + room = @admin.rooms.select {|room| room.number == 2}.first + blocks = room.blocks + target_block = blocks.select {|block| block[:range] == range}[0] + expect(target_block[:status]).must_equal "booked" + end end From 588787cc106dfdfd0093d29ecf20e318aa4b4c53 Mon Sep 17 00:00:00 2001 From: Laura Date: Sun, 9 Sep 2018 20:34:42 -0700 Subject: [PATCH 35/49] fixed a bug in reserve room in block and made sure that person from outside a block cannot reserve a block --- lib/admin.rb | 52 ++++++++++++++++++++++++++++++++++++---------- spec/admin_spec.rb | 2 ++ 2 files changed, 43 insertions(+), 11 deletions(-) diff --git a/lib/admin.rb b/lib/admin.rb index cc8ca3eda..4b83da39d 100644 --- a/lib/admin.rb +++ b/lib/admin.rb @@ -64,7 +64,6 @@ def reserve_room(start_date, end_date) end #As an administrator, I can view a list of rooms that are not reserved for a given date range - # raise argument error if array is empty ## expects dates to be instances of time def view_vacant_rooms(start_date, end_date) vacant_rooms = @rooms @@ -79,14 +78,16 @@ def view_vacant_rooms(start_date, end_date) break end end - # remove rooms if it has blocks on that date - blocks.each do |block| - # nil means no intersection - if intersection(target_range, blocks[:range]) != nil # there was a overlap - vacant_rooms.delete(room) - break - end - end + end + # remove rooms that have a block in them + # must add test for this case + vacant_rooms.each do |room| + blocks = room.blocks + blocks.each do |block| + blocks.empty? == false + binding.pry + vacant_rooms.delete(room) + end end return vacant_rooms end @@ -130,7 +131,8 @@ def create_block_rooms(data) rooms = data[:rooms] raise StandardError if rooms > 5 #max block size discounted_rate = data[:discounted_rate] - available_rooms = view_vacant_rooms(start_date, e_date) + # view vacant_rooms - current method + available_rooms = view_vacant_rooms_for_blocks(start_date, e_date) if available_rooms.length < rooms return "no available rooms for block" @@ -145,8 +147,30 @@ def create_block_rooms(data) room.add_block(input_data) end end + end - + def view_vacant_rooms_for_blocks(start_date, end_date) + vacant_rooms = @rooms + target_range = create_hotel_range(start_date, end_date) + vacant_rooms.each do |room| + ranges = room.ranges + blocks = room.blocks + ranges.each do |range| + # nil means no intersection + if intersection(target_range, range) != nil # there was a overlap + vacant_rooms.delete(room) + break + end + end + blocks.each do |block| + # nil means no intersection + if intersection(target_range, blocks[:range]) != nil # there was a overlap + vacant_rooms.delete(room) + break + end + end + end + return vacant_rooms end # As an administrator, I can reserve a room from within a block of rooms @@ -157,6 +181,12 @@ def reserve_room_in_block(data) room.reserve_room_block(range) end + # As an administrator, I can check whether a given block has any rooms available + def view_vacant_rooms_in_block(range) + rooms = @rooms.select { |room| room.blocks[:range] == range}[0] + end + + #last day not counted #expect input to be time instances def create_hotel_range(start_date, end_date) diff --git a/spec/admin_spec.rb b/spec/admin_spec.rb index 7919fe44b..bb2c8124d 100644 --- a/spec/admin_spec.rb +++ b/spec/admin_spec.rb @@ -217,4 +217,6 @@ target_block = blocks.select {|block| block[:range] == range}[0] expect(target_block[:status]).must_equal "booked" end + + # needs test for this one: view_vacant_rooms_in_block end From db4b3ed7593717ef757de120e81777682bc2ce38 Mon Sep 17 00:00:00 2001 From: Laura Date: Sun, 9 Sep 2018 21:07:01 -0700 Subject: [PATCH 36/49] method to find a block --- lib/admin.rb | 17 ++++++++++++++--- spec/admin_spec.rb | 23 +++++++++++++++++++++++ 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/lib/admin.rb b/lib/admin.rb index 4b83da39d..4010703c6 100644 --- a/lib/admin.rb +++ b/lib/admin.rb @@ -179,14 +179,25 @@ def reserve_room_in_block(data) range = data[:range] room = @rooms.select { |room| room.number == room_num}[0] room.reserve_room_block(range) + # needs to create instance of reservation end # As an administrator, I can check whether a given block has any rooms available - def view_vacant_rooms_in_block(range) - rooms = @rooms.select { |room| room.blocks[:range] == range}[0] + def find_block(range) + #find block + rooms_with_blocks = @rooms.select { |room| room.blocks.empty? == false} + #blocks are an array + block_set = [] + rooms_with_blocks.each do |room| + blocks = room.blocks + blocks.each do |block| + if block[:range] == range + block_set << room + end + end + end end - #last day not counted #expect input to be time instances def create_hotel_range(start_date, end_date) diff --git a/spec/admin_spec.rb b/spec/admin_spec.rb index bb2c8124d..58acfbb47 100644 --- a/spec/admin_spec.rb +++ b/spec/admin_spec.rb @@ -218,5 +218,28 @@ expect(target_block[:status]).must_equal "booked" end + it "find a block" do + start_date = "2018-08-07 00:00:00 -0700" + end_date = "2018-08-09 00:00:00 -0700" + data = {} + data[:block_id] = 1 + data[:start_date] = start_date + data[:end_date] = end_date + data[:rooms] = 4 + data[:discounted_rate] = 100 + @admin.create_block_rooms(data) + info = {} + + start_date = Time.parse("2018-08-07 00:00:00 -0700") + end_date = Time.parse("2018-08-09 00:00:00 -0700") + e_date = end_date - 1 + range = (start_date .. e_date) + info = {} + info[:room_num] = 2 + info[:range] = range + @admin.reserve_room_in_block(info) + rooms = @admin.find_block(range) + expect(rooms.length).must_equal 4 + end # needs test for this one: view_vacant_rooms_in_block end From 6029c5699935348cc78098ecf2eedafff04e780f Mon Sep 17 00:00:00 2001 From: Laura Date: Sun, 9 Sep 2018 21:13:11 -0700 Subject: [PATCH 37/49] view vacant rooms in a block --- lib/admin.rb | 17 ++++++++++++++++- spec/admin_spec.rb | 25 ++++++++++++++++++++++++- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/lib/admin.rb b/lib/admin.rb index 4010703c6..ed5712d6c 100644 --- a/lib/admin.rb +++ b/lib/admin.rb @@ -183,10 +183,25 @@ def reserve_room_in_block(data) end # As an administrator, I can check whether a given block has any rooms available + def view_vacant_rooms_in_block(range) + available_rooms = [] + block_rooms = find_block(range) + block_rooms.each do |room| + #blocks variable is an array + blocks = room.blocks + blocks.each do |block| + if block[:status] == "available" + available_rooms << room + end + end + end + return available_rooms + end + def find_block(range) #find block rooms_with_blocks = @rooms.select { |room| room.blocks.empty? == false} - #blocks are an array + #blocks variable are an array block_set = [] rooms_with_blocks.each do |room| blocks = room.blocks diff --git a/spec/admin_spec.rb b/spec/admin_spec.rb index 58acfbb47..a91b94f30 100644 --- a/spec/admin_spec.rb +++ b/spec/admin_spec.rb @@ -241,5 +241,28 @@ rooms = @admin.find_block(range) expect(rooms.length).must_equal 4 end - # needs test for this one: view_vacant_rooms_in_block + + it "view_vacant_rooms_in_block" do + start_date = "2018-08-07 00:00:00 -0700" + end_date = "2018-08-09 00:00:00 -0700" + data = {} + data[:block_id] = 1 + data[:start_date] = start_date + data[:end_date] = end_date + data[:rooms] = 4 + data[:discounted_rate] = 100 + @admin.create_block_rooms(data) + info = {} + + start_date = Time.parse("2018-08-07 00:00:00 -0700") + end_date = Time.parse("2018-08-09 00:00:00 -0700") + e_date = end_date - 1 + range = (start_date .. e_date) + info = {} + info[:room_num] = 2 + info[:range] = range + @admin.reserve_room_in_block(info) + rooms = @admin.view_vacant_rooms_in_block(range) + expect(rooms.length).must_equal 3 + end end From db41b55d7af4e43dd38780a8bceb18662b89e3d3 Mon Sep 17 00:00:00 2001 From: Laura Date: Sun, 9 Sep 2018 21:48:28 -0700 Subject: [PATCH 38/49] view vacant rooms in block --- lib/admin.rb | 3 --- spec/admin_spec.rb | 41 +++++++++++++++++++++++++++++++++++------ 2 files changed, 35 insertions(+), 9 deletions(-) diff --git a/lib/admin.rb b/lib/admin.rb index ed5712d6c..7c9c1f277 100644 --- a/lib/admin.rb +++ b/lib/admin.rb @@ -59,8 +59,6 @@ def reserve_room(start_date, end_date) input_data[:room_num] = room.number reservations << Reservation.new(input_data) end - - # must create instance of reservation for this room using one more day end #As an administrator, I can view a list of rooms that are not reserved for a given date range @@ -85,7 +83,6 @@ def view_vacant_rooms(start_date, end_date) blocks = room.blocks blocks.each do |block| blocks.empty? == false - binding.pry vacant_rooms.delete(room) end end diff --git a/spec/admin_spec.rb b/spec/admin_spec.rb index a91b94f30..8c1898bb8 100644 --- a/spec/admin_spec.rb +++ b/spec/admin_spec.rb @@ -4,7 +4,7 @@ # # runs from project directory -xdescribe "#Admin - initializer" do +describe "#Admin - initializer" do before do @admin = Admin.new end @@ -38,13 +38,13 @@ end end -xdescribe "#reservation information" do +describe "#reservation information" do before do @admin = Admin.new end it "return the reservations that have a specific date, not including the last day" do #arrange - # reservations are: + # reservations are from test file # 2,2018-08-07,2018-08-09 # 1,2018-08-08,2018-08-09 @@ -78,9 +78,12 @@ # REFACTOR idea: # before and after count of rooms from driver - # needs to test when reservation is 1 - 2 - # and another one with reservation 1 - 3, should include 2 rooms + ## FUTURE WORK: needs a test for this case + # reservation is 1 - 2 + # and another one is 1 - 3, should include 2 rooms # needs a test with arrange to have block rooms and to check result + + ## FUTURE WORK: needs a test that reservation was made # checked in binding.pry, but needs the test it "can make a reservation" do start_date = "2018-08-07 00:00:00 -0700" end_date = "2018-08-09 00:00:00 -0700" @@ -122,7 +125,7 @@ end end -xdescribe "#range tests" do +describe "#range tests" do before do @admin = Admin.new end @@ -265,4 +268,30 @@ rooms = @admin.view_vacant_rooms_in_block(range) expect(rooms.length).must_equal 3 end + + it "an outsider of a party can't reserve a room in a block" do + start_date = "2018-08-07 00:00:00 -0700" + end_date = "2018-08-09 00:00:00 -0700" + data = {} + data[:block_id] = 1 + data[:start_date] = start_date + data[:end_date] = end_date + data[:rooms] = 4 + data[:discounted_rate] = 100 + # it creates 4 blocks + @admin.create_block_rooms(data) + + start_date = "2018-08-07 00:00:00 -0700" + end_date = "2018-08-09 00:00:00 -0700" + @admin.reserve_room(start_date, end_date) + # FUTURE WORK: a binding.pry at this point shows that it didn't reserve a room, it removed rooms one and three?? + + # to test the expected result + start_date = "2018-08-07 00:00:00 -0700" + end_date = "2018-08-09 00:00:00 -0700" + start_date = Time.parse(start_date) + end_date = Time.parse(end_date) + end_date = end_date - 1 + range = [(start_date..end_date)] + end end From 4d35280043e7c5402d252f7deebb36caf2d68c3e Mon Sep 17 00:00:00 2001 From: Laura Date: Sun, 9 Sep 2018 22:07:44 -0700 Subject: [PATCH 39/49] all tests work and comments for future work --- lib/admin.rb | 3 ++- spec/admin_spec.rb | 2 -- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/admin.rb b/lib/admin.rb index 7c9c1f277..606dedde7 100644 --- a/lib/admin.rb +++ b/lib/admin.rb @@ -176,7 +176,8 @@ def reserve_room_in_block(data) range = data[:range] room = @rooms.select { |room| room.number == room_num}[0] room.reserve_room_block(range) - # needs to create instance of reservation + ### FUTURE WORK: 1. use method view_vacant_rooms_in_block before making a reservation + # => 2. create isntance of reservation end # As an administrator, I can check whether a given block has any rooms available diff --git a/spec/admin_spec.rb b/spec/admin_spec.rb index 8c1898bb8..7b6cbb3fb 100644 --- a/spec/admin_spec.rb +++ b/spec/admin_spec.rb @@ -21,13 +21,11 @@ expect(first_reservation.end_time).must_equal Time.parse("2018-08-09 00:00:00 -0700") expect(first_reservation.nights).must_equal 1 expect(first_reservation.cost).must_equal 200 - expect(first_reservation.id).must_equal 1 expect(last_reservation.start_time).must_equal Time.parse("2018-08-07 00:00:00 -0700") expect(last_reservation.end_time).must_equal Time.parse("2018-08-09 00:00:00 -0700") expect(last_reservation.nights).must_equal 2 expect(last_reservation.cost).must_equal 400 - expect(last_reservation.id).must_equal 2 end it "accurately create_rooms" do From af356694cafada8ad7ca3a850ccb3cd835736482 Mon Sep 17 00:00:00 2001 From: Laura Date: Mon, 10 Sep 2018 08:50:02 -0700 Subject: [PATCH 40/49] refactor.txt --- refactor.txt | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 refactor.txt diff --git a/refactor.txt b/refactor.txt new file mode 100644 index 000000000..5606fb1c9 --- /dev/null +++ b/refactor.txt @@ -0,0 +1,7 @@ +1. Room number variable same across all classes +2. Create helper method that subtracts once day from date range because we don't keep track of that anywhere. +2.2 Update reservation class to calculate the correct cost for the reservation when one day is subtracted everywhere +3. Test spec file: use let when possible to make it faster +3.2 used nested describes (check if it works first) to create an instance of admin only once +4.1 In the room class, rename the add_range method to add_block +4.2 explore difference of creating a hash or a key word argument, and apply it when you only have two key-word pairs like From 1eba79deb20cc015b05163c193dde12697f3bf40 Mon Sep 17 00:00:00 2001 From: Laura Date: Wed, 12 Sep 2018 09:32:18 -0700 Subject: [PATCH 41/49] range to not include last day --- refactor.txt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/refactor.txt b/refactor.txt index 5606fb1c9..f27d29d3a 100644 --- a/refactor.txt +++ b/refactor.txt @@ -1,7 +1,7 @@ 1. Room number variable same across all classes -2. Create helper method that subtracts once day from date range because we don't keep track of that anywhere. -2.2 Update reservation class to calculate the correct cost for the reservation when one day is subtracted everywhere 3. Test spec file: use let when possible to make it faster 3.2 used nested describes (check if it works first) to create an instance of admin only once -4.1 In the room class, rename the add_range method to add_block -4.2 explore difference of creating a hash or a key word argument, and apply it when you only have two key-word pairs like +4.In the room class, rename the add_range method to add_block +4.2 explore difference of creating a hash or a key word argument, and apply it when you only have two key-word pairs like +5. for range use ... to not include the last day (this way you don't have to keep subtracting) +6. move all helper methods into it's own class and keep admin tasks in its class From 472c3004eb2323c6ad5ad9d7e41181e2af846c47 Mon Sep 17 00:00:00 2001 From: Laura Date: Thu, 20 Sep 2018 13:33:39 -0700 Subject: [PATCH 42/49] added refactor goal for the weekend --- refactor.txt | 5 +++++ spec/admin_spec.rb | 2 ++ 2 files changed, 7 insertions(+) diff --git a/refactor.txt b/refactor.txt index f27d29d3a..2f029cfea 100644 --- a/refactor.txt +++ b/refactor.txt @@ -5,3 +5,8 @@ 4.2 explore difference of creating a hash or a key word argument, and apply it when you only have two key-word pairs like 5. for range use ... to not include the last day (this way you don't have to keep subtracting) 6. move all helper methods into it's own class and keep admin tasks in its class + +one refactor can be the bug that I have where a reserve_room - check this method is grabbing the two rooms from my block +-check if room -> block <- is the @number here, the room for the block? verify this! + +finish the reservation to reflect current reservations including the block room diff --git a/spec/admin_spec.rb b/spec/admin_spec.rb index 7b6cbb3fb..2f6f5e192 100644 --- a/spec/admin_spec.rb +++ b/spec/admin_spec.rb @@ -278,10 +278,12 @@ data[:discounted_rate] = 100 # it creates 4 blocks @admin.create_block_rooms(data) + binding.pry start_date = "2018-08-07 00:00:00 -0700" end_date = "2018-08-09 00:00:00 -0700" @admin.reserve_room(start_date, end_date) + binding.pry # FUTURE WORK: a binding.pry at this point shows that it didn't reserve a room, it removed rooms one and three?? # to test the expected result From 14c675b48fc547b6615a4cc2edffcb6cf94d8b64 Mon Sep 17 00:00:00 2001 From: Laura Date: Sat, 29 Sep 2018 20:56:28 -0700 Subject: [PATCH 43/49] single responsibility practice --- design-activity.md | 55 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 design-activity.md diff --git a/design-activity.md b/design-activity.md new file mode 100644 index 000000000..c3077492d --- /dev/null +++ b/design-activity.md @@ -0,0 +1,55 @@ +# What classes does each implementation include? Are the lists the same? +IMPLEMENTATION A includes the CartEntry class, the ShoppingCart class, and the Order class + +IMPLEMENTATION B includes the CartEntry class, the ShoppingCart class, and the Order class +The both have the same classes' names +# Write down a sentence to describe each class. +CartEntry class initializes a instance of an entry, it has price of the entry and the quantity of this entry +ShoppingCart class initializes an instance of a Cart, it has the an array of entries +The Order class calculates the total cost for the entries in the cart +# How do the classes relate to each other? It might be helpful to draw a diagram on a whiteboard or piece of paper. +IMPLEMENTATION A: The Order class depends on the ShoppingCart class to create the cart with an entries array. The Order class, calculates the total price for all entries in the cart. + +IMPLEMENTATION B: The Order class depends on the ShoppingCart class to create the cart with an entries array. The ShoppingCart class calculates the price for all the items in the cart +# What data does each class store? How (if at all) does this differ between the two implementations? +IMPLEMENTATION A: +CartEntry stores unit price and quantity of an entry +ShoppingCart stores all the entries in an array +Order stores the cart that contains all the entries + +IMPLEMENTATION B: +CartEntry stores the unit price and quantity of an entry +ShoppingCart stores the all the entries in an array +Order stores the cart that contains all the entries +This does not differ between the two implementations + +#What methods does each class have? How (if at all) does this differ between the two implementations? +IMPLEMENTATION A: +CartEntry: no methods +ShoppingCart: no methods +Order: total_price method to calculate the price for each entry, the price for all entries, and the total price including tax + +IMPLEMENTATION B: +CartEntry: price method to calculate the price for the entry (quantity and unit price) +ShoppingCart: price method to calculate total price for the cart (all entries) +Order: total price method to calculate the total price including tax + +#Consider the Order#total_price method. In each implementation: +#Is logic to compute the price delegated to "lower level" classes like ShoppingCart and CartEntry, or is it retained in Order? +#Does total_price directly manipulate the instance variables of other classes? +IMPLEMENTATION A: It's retained in Order +total_price directly manipulate instance variables of other classes (it calls the unit price and quantity instance variables from the Cart Entry, and the entries instance variable from the ShoppingCart class) + +IMPLEMENTATION B: It computes the price delegated to "lower lever" classes. The computes the total cost by applying the tax. +total_price doesn't directly manipulate instance variables of other classes (it calls the price method, but it is not an instance variable) + +#If we decide items are cheaper if bought in bulk, how would this change the code? Which implementation is easier to modify? +IMPLEMENTATION A: you will have to add an if statement in the do loop to calculate a discount if entry.quantity is of a certain number before adding it to the sum variable + +IMPLEMENTATION B: you will need to change the price method in the CartEntry class by calculating a discount if quantity is of certain items. + +IMPLEMENTATION B is easier to modify +#Which implementation better adheres to the single responsibility principle? +IMPLEMENTATION B because code that reads or modifies the instance variables of a class is wrapped in a method of that class +#Bonus question once you've read Metz ch. 3: Which implementation is more loosely coupled? +IMPLEMENTATION B is more loosely coupled because code that reads or modifies the instance variables of a class is wrapped in an instance method on that class. From 961d6b22743b152a7b428e262bc0bb5ffc5686c8 Mon Sep 17 00:00:00 2001 From: Laura Date: Sat, 29 Sep 2018 22:06:34 -0700 Subject: [PATCH 44/49] design analysis of my hotel --- design-analysis-of-my-hotel.md | 36 ++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 design-analysis-of-my-hotel.md diff --git a/design-analysis-of-my-hotel.md b/design-analysis-of-my-hotel.md new file mode 100644 index 000000000..7743aba69 --- /dev/null +++ b/design-analysis-of-my-hotel.md @@ -0,0 +1,36 @@ +#What is this class's responsibility? +ROOM class: it keeps track of the rooms are reserved in a block reservation or in a regular reservation +Behaviors: +-it adds the range to the instance variable range +-it adds the block to the instance variable block +-it adds the reservation status of "booked" to the block variable instance + +RESERVATION class: it calculates cost of a reservation by creating instance variables + +ADMIN class: It holds the rooms and the reservations, checks if rooms are available for block reservations or regular reservations, and provides views for current reservations. +Behaviors: +-it creates the rooms instances +-it creates the reservation instances +-it reserves a room by calling the methods in the ROOM class +-it views vacant rooms by reading the instance variable "ranges" of the ROOM class ---improve here +-it views all the rooms by reading it's instance variable: rooms +-it finds a reservation by reading it's instance variable: reservation +-it finds the cost of a reservation by reading the instance variable of the RESERVATION class. +-it creates a block of rooms, by modifying the block instance variable of the ROOM class --- improve here +-it finds vacant rooms available for a block by reading the instance variable, ranges, of the ROOM class --- improve here +-it reserves a room in a block by modifying the block instance variable of the ROOM class --- improve here + +#Is this class responsible for exactly one thing? +ROOM class: not sure. I think no because it keeps track of blocks and regular reservation, and reserves rooms; but it doesn't change the instance variable of other classes so it is loosely coupled. +RESERVATION class: yes +ADMIN class: no (it checks if two reservations clashes before calling the reservation method in the ROOM class) + +#Does this class take on any responsibility that should be delegated to "lower level" classes? +ROOM class: no +RESERVATION class: no +ADMIN class: yes, viewing vacant rooms should be moved to the ROOM class, creating a block of rooms should be moved to the ROOM class, and reserving a room should be move to the ROOM class + +#Is there code in other classes that directly manipulates this class's instance variables? +ROOM class: yes, range and block are modified by methods in the ADMIN class (create block of rooms, and reserve a room) +RESERVATION class: no +ADMIN: no From 007602cd077f7ae4ba1e1bb679c9a5eeb1a60b22 Mon Sep 17 00:00:00 2001 From: Laura Date: Sat, 29 Sep 2018 22:26:49 -0700 Subject: [PATCH 45/49] added comments --- lib/admin.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/admin.rb b/lib/admin.rb index 606dedde7..03289c2a3 100644 --- a/lib/admin.rb +++ b/lib/admin.rb @@ -195,7 +195,7 @@ def view_vacant_rooms_in_block(range) end return available_rooms end - + # it is used in view_vacant_rooms_in_block method def find_block(range) #find block rooms_with_blocks = @rooms.select { |room| room.blocks.empty? == false} From 08732871be2f11648a54f2e64813277d20b90067 Mon Sep 17 00:00:00 2001 From: Laura Date: Sat, 29 Sep 2018 22:32:56 -0700 Subject: [PATCH 46/49] evaluation of my refactor notes from weeks ago --- refactor-analysis.md | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 refactor-analysis.md diff --git a/refactor-analysis.md b/refactor-analysis.md new file mode 100644 index 000000000..5cc64c1d5 --- /dev/null +++ b/refactor-analysis.md @@ -0,0 +1,7 @@ +#How easy is it to follow your own instructions? +They make sense because I wrote them, but they could be more explicit for other to understand +5 bullet point, it's not clear where it appears. It should have something like "when you create a date range, if you don't want to include the last item in this case date, use 3 dots" +#Do these refactors improve the clarity of your code? +Mostly, some of them are not accurate like 4.2, the add_range does not add a block, it makes a reservation so it should be called create_reservation +#Do you still agree with your previous assesment, or could your refactor be further improved? +It could try to make sure that method from one class do not read or modifies instance variables loose coupled. From 467dd624286322cbc64b43e9c7180a717f4b0dc7 Mon Sep 17 00:00:00 2001 From: Laura Date: Sat, 29 Sep 2018 22:50:01 -0700 Subject: [PATCH 47/49] answered question about variable names --- refactor.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/refactor.txt b/refactor.txt index 2f029cfea..fe2930611 100644 --- a/refactor.txt +++ b/refactor.txt @@ -7,6 +7,6 @@ 6. move all helper methods into it's own class and keep admin tasks in its class one refactor can be the bug that I have where a reserve_room - check this method is grabbing the two rooms from my block --check if room -> block <- is the @number here, the room for the block? verify this! +-check if room -> block <- is the @number here, the room for the block? verify this! Yes, it is the room number -finish the reservation to reflect current reservations including the block room +finish the reservation to reflect current reservations including the block room From 4d13273093a2807b78220c736c11227f15814a2b Mon Sep 17 00:00:00 2001 From: Laura Date: Sun, 30 Sep 2018 00:14:26 -0700 Subject: [PATCH 48/49] finished test about bug that checked an outsider of a party can't reserve a room in a block --- spec/admin_spec.rb | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/spec/admin_spec.rb b/spec/admin_spec.rb index 2f6f5e192..a503a8575 100644 --- a/spec/admin_spec.rb +++ b/spec/admin_spec.rb @@ -4,7 +4,7 @@ # # runs from project directory -describe "#Admin - initializer" do +xdescribe "#Admin - initializer" do before do @admin = Admin.new end @@ -36,7 +36,7 @@ end end -describe "#reservation information" do +xdescribe "#reservation information" do before do @admin = Admin.new end @@ -64,7 +64,7 @@ end -describe "#rooms information and reservation" do +xdescribe "#rooms information and reservation" do before do @admin = Admin.new end @@ -123,7 +123,7 @@ end end -describe "#range tests" do +xdescribe "#range tests" do before do @admin = Admin.new end @@ -153,6 +153,7 @@ end it "create a block of rooms" do + skip # reserve a normal room start_date = "2018-08-07 00:00:00 -0700" end_date = "2018-08-09 00:00:00 -0700" @@ -172,6 +173,7 @@ end it "raises an Standard error for invalid rooms for block" do + skip start_date = "2018-08-07 00:00:00 -0700" end_date = "2018-08-09 00:00:00 -0700" @admin.reserve_room(start_date, end_date) @@ -190,6 +192,7 @@ end it "updates status of room block" do + skip start_date = "2018-08-07 00:00:00 -0700" end_date = "2018-08-09 00:00:00 -0700" @admin.reserve_room(start_date, end_date) @@ -220,6 +223,7 @@ end it "find a block" do + skip start_date = "2018-08-07 00:00:00 -0700" end_date = "2018-08-09 00:00:00 -0700" data = {} @@ -244,6 +248,7 @@ end it "view_vacant_rooms_in_block" do + skip start_date = "2018-08-07 00:00:00 -0700" end_date = "2018-08-09 00:00:00 -0700" data = {} @@ -276,22 +281,19 @@ data[:end_date] = end_date data[:rooms] = 4 data[:discounted_rate] = 100 - # it creates 4 blocks + # it creates 4 room blocks @admin.create_block_rooms(data) - binding.pry start_date = "2018-08-07 00:00:00 -0700" end_date = "2018-08-09 00:00:00 -0700" @admin.reserve_room(start_date, end_date) - binding.pry - # FUTURE WORK: a binding.pry at this point shows that it didn't reserve a room, it removed rooms one and three?? - # to test the expected result start_date = "2018-08-07 00:00:00 -0700" end_date = "2018-08-09 00:00:00 -0700" start_date = Time.parse(start_date) end_date = Time.parse(end_date) - end_date = end_date - 1 - range = [(start_date..end_date)] + range = [(start_date...end_date)] + # 4 is 5 room - index starts at 0 + expect(@admin.rooms[4].ranges).wont_be_empty end end From 77e605cccf3e009d4a5bd79dfb1cc4c9a556a41a Mon Sep 17 00:00:00 2001 From: Laura Date: Sun, 30 Sep 2018 00:16:03 -0700 Subject: [PATCH 49/49] fixed bug about an outsider of a party can't reserve a room in a block --- lib/admin.rb | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/lib/admin.rb b/lib/admin.rb index 03289c2a3..9ac150920 100644 --- a/lib/admin.rb +++ b/lib/admin.rb @@ -52,7 +52,7 @@ def reserve_room(start_date, end_date) raise StandardError, "no rooms are available" else room = vacant_rooms.first - rooms.first.add_range(range) + room.add_range(range) input_data = {} input_data[:start_time] = start_date input_data[:end_time] = end_date @@ -64,29 +64,44 @@ def reserve_room(start_date, end_date) #As an administrator, I can view a list of rooms that are not reserved for a given date range ## expects dates to be instances of time def view_vacant_rooms(start_date, end_date) + # I am editing the rooms! it's not making a copy + # I am just creating a new pointer that points to the same data as what rooms points to vacant_rooms = @rooms target_range = create_hotel_range(start_date, end_date) vacant_rooms.each do |room| ranges = room.ranges - blocks = room.blocks ranges.each do |range| # nil means no intersection if intersection(target_range, range) != nil # there was a overlap vacant_rooms.delete(room) - break + break # where does this go? it goes to the next room, if one range conflicts, I do not have to check if the others conflicts end end end + + # remove rooms that have a block in them - # must add test for this case - vacant_rooms.each do |room| - blocks = room.blocks - blocks.each do |block| - blocks.empty? == false - vacant_rooms.delete(room) + vacant_rooms_result = vacant_rooms.select do | room | + if room.blocks.empty? + room.blocks.empty? + else + intersection(target_range, room.blocks.first[:range]) == nil end end - return vacant_rooms + + return vacant_rooms_result + + # QUEstion for later: how come this does not work without using select: Can't escape from eval with next + + # vacant_rooms.each do |room| + # if room.blocks.empty? == false + # if intersection(target_range, room.blocks.first[:range]) != nil # there was a overlap + # vacant_rooms.delete(room) + # end + # else # it does not have a block + # next # go to the next room + # end + # end end # As an administrator, I can access the list of all of the rooms in the hotel