From 094e9670526d1e783f980fee93fe6864adfd6020 Mon Sep 17 00:00:00 2001 From: Iuliia Chikulaeva Date: Tue, 5 Sep 2017 16:20:13 -0700 Subject: [PATCH 01/44] Created new files: date_range and gate_range_spec --- lib/date_range.rb | 6 ++++++ specs/date_range_spec.rb | 0 2 files changed, 6 insertions(+) create mode 100644 lib/date_range.rb create mode 100644 specs/date_range_spec.rb diff --git a/lib/date_range.rb b/lib/date_range.rb new file mode 100644 index 000000000..b72e2c869 --- /dev/null +++ b/lib/date_range.rb @@ -0,0 +1,6 @@ +module BookingSystem + class DateRange + + end #end of class + +end #end of module diff --git a/specs/date_range_spec.rb b/specs/date_range_spec.rb new file mode 100644 index 000000000..e69de29bb From e4bdaebde615f6907edec0abf8281815f11eb464 Mon Sep 17 00:00:00 2001 From: Iuliia Chikulaeva Date: Tue, 5 Sep 2017 16:36:02 -0700 Subject: [PATCH 02/44] Added test for initialize method for class DateRange --- lib/date_range.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/lib/date_range.rb b/lib/date_range.rb index b72e2c869..be3da79ee 100644 --- a/lib/date_range.rb +++ b/lib/date_range.rb @@ -1,6 +1,17 @@ module BookingSystem + class DateRange + def initialize(check_in, check_out) + @check_in = Date.strptime(check_in, '%d-%m-%Y') + @check_out = Date.strptime(check_out, '%d-%m-%Y') + end #end of initialize + + def populate_dates + + end #end of method + + end #end of class end #end of module From 6f6baa7fbc486f4a4f62a5d198b5a952fd2dd373 Mon Sep 17 00:00:00 2001 From: Iuliia Chikulaeva Date: Tue, 5 Sep 2017 16:44:44 -0700 Subject: [PATCH 03/44] Added spec_helper and Rakefile to the project --- Rakefile | 9 +++++++++ specs/date_range_spec.rb | 20 ++++++++++++++++++++ specs/spec_helper.rb | 14 ++++++++++++++ 3 files changed, 43 insertions(+) create mode 100644 Rakefile create mode 100644 specs/spec_helper.rb diff --git a/Rakefile b/Rakefile new file mode 100644 index 000000000..f4f704fca --- /dev/null +++ b/Rakefile @@ -0,0 +1,9 @@ +require 'rake/testtask' + +Rake::TestTask.new do |t| + t.libs = ["lib"] + t.warning = false + t.test_files = FileList['specs/*_spec.rb'] +end + +task default: :test diff --git a/specs/date_range_spec.rb b/specs/date_range_spec.rb index e69de29bb..cd26c83cb 100644 --- a/specs/date_range_spec.rb +++ b/specs/date_range_spec.rb @@ -0,0 +1,20 @@ +require_relative 'date_range' + +describe "#initialize" do + it "Check_in value can be called by .check_in" do + new_date_range = BookingSystem::DateRange.new("09-15-2017", "09-17-2017") + new_date_range.check_in.must_equal "2017-09-15" + new_date_range.check_out.must_equal "2017-09-17" + end + + xdescribe "" do + it "Returns true if given date range is valid" do + date_range = BookingSystem::DateRange.new("09-15-2017", "09-17-2017") + + end + end + + + + +end #end of discribe diff --git a/specs/spec_helper.rb b/specs/spec_helper.rb new file mode 100644 index 000000000..5af53f455 --- /dev/null +++ b/specs/spec_helper.rb @@ -0,0 +1,14 @@ +require 'simplecov' +SimpleCov.start + +require 'minitest/autorun' +require 'minitest/reporters' +require 'minitest/skip_dsl' +require 'minitest/pride' + +# Require any classes +require_relative '../lib/date_range' +# require_relative '../lib/scoring' +# require_relative '../lib/tile_bag' + +Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new From a9371e0bd5f0de7d47684df9af5dac68b2c851c7 Mon Sep 17 00:00:00 2001 From: Iuliia Chikulaeva Date: Tue, 5 Sep 2017 20:42:49 -0700 Subject: [PATCH 04/44] Added tests for #initialize method and passed all of them --- lib/date_range.rb | 22 +++++++++++++++++--- specs/date_range_spec.rb | 44 +++++++++++++++++++++++++++++++--------- 2 files changed, 53 insertions(+), 13 deletions(-) diff --git a/lib/date_range.rb b/lib/date_range.rb index be3da79ee..4b1ff6161 100644 --- a/lib/date_range.rb +++ b/lib/date_range.rb @@ -1,16 +1,32 @@ +require 'date' + module BookingSystem class DateRange + attr_reader :check_in, :check_out + def initialize(check_in, check_out) - @check_in = Date.strptime(check_in, '%d-%m-%Y') - @check_out = Date.strptime(check_out, '%d-%m-%Y') + @check_in = check_in + @check_out = check_out + + if check_in.class != Date || check_out.class != Date || @check_in > @check_out + raise ArgumentError.new("Invalid date range") + end end #end of initialize - def populate_dates + + def dates_within_range end #end of method + # def valid_dates? + # if @check_in < @check_out + # return true + # end + # raise ArgumentError.new("Invalid date range") + # end #end of method + end #end of class diff --git a/specs/date_range_spec.rb b/specs/date_range_spec.rb index cd26c83cb..4ee435d1f 100644 --- a/specs/date_range_spec.rb +++ b/specs/date_range_spec.rb @@ -1,19 +1,43 @@ -require_relative 'date_range' +require_relative 'spec_helper' -describe "#initialize" do - it "Check_in value can be called by .check_in" do - new_date_range = BookingSystem::DateRange.new("09-15-2017", "09-17-2017") - new_date_range.check_in.must_equal "2017-09-15" - new_date_range.check_out.must_equal "2017-09-17" +describe "DateRange" do + before do + @check_in = Date.new(2017,9,15) + @check_out = Date.new(2017,9,17) + @new_date_range = BookingSystem::DateRange.new(@check_in, @check_out) end - xdescribe "" do - it "Returns true if given date range is valid" do - date_range = BookingSystem::DateRange.new("09-15-2017", "09-17-2017") - + describe "#initialize" do + it "Check_in and check_out values are types of Date class" do + @new_date_range.check_in.must_be_kind_of Date + @new_date_range.check_out.must_be_kind_of Date + end + it "Raise an error if check_in or check_out values are not types of Date class" do + proc { BookingSystem::DateRange.new("2017-9-15", "2017-9-17") }.must_raise ArgumentError + end + it "Raise an error if given check_out date is before check_in date" do + proc { BookingSystem::DateRange.new(@check_out, @check_in) }.must_raise ArgumentError end end + # xdescribe "#valid_dates?" do + # it "Returns true if given check_out date is after check_in date" do + # @new_date_range.valid_dates?.must_equal true + # end + # it "Returns false if given check_out date is before check_in date" do + # date_range = BookingSystem::DateRange.new("17-09-2017", "15-09-2017") + # proc { date_range.valid_dates? }.must_raise ArgumentError + # end + # end + # + # xdescribe "dates_within_range" do + # it "Returns an array" do + # @new_date_range.dates_within_range.must_be_kind_of Array + # end + # it "Returns an array of dates within the given range" do + # @new_date_range.dates_within_range.must_equal [a, b, c] + # end + # end From 095414926bb95ce68ea3d32116f14745b6922e2c Mon Sep 17 00:00:00 2001 From: Iuliia Chikulaeva Date: Tue, 5 Sep 2017 21:01:33 -0700 Subject: [PATCH 05/44] Added tests for #dates_within_range method and passed all of them --- lib/date_range.rb | 15 ++++----------- specs/date_range_spec.rb | 33 +++++++++++++-------------------- 2 files changed, 17 insertions(+), 31 deletions(-) diff --git a/lib/date_range.rb b/lib/date_range.rb index 4b1ff6161..3f6f18c24 100644 --- a/lib/date_range.rb +++ b/lib/date_range.rb @@ -10,24 +10,17 @@ def initialize(check_in, check_out) @check_in = check_in @check_out = check_out - if check_in.class != Date || check_out.class != Date || @check_in > @check_out - raise ArgumentError.new("Invalid date range") + if @check_in.class != Date || @check_out.class != Date || @check_in > @check_out + raise ArgumentError.new("Invalid date or date range") end end #end of initialize def dates_within_range - + dates = (@check_in .. @check_out - 1).map { |date| date } + return dates end #end of method - # def valid_dates? - # if @check_in < @check_out - # return true - # end - # raise ArgumentError.new("Invalid date range") - # end #end of method - - end #end of class end #end of module diff --git a/specs/date_range_spec.rb b/specs/date_range_spec.rb index 4ee435d1f..72f98c3be 100644 --- a/specs/date_range_spec.rb +++ b/specs/date_range_spec.rb @@ -20,25 +20,18 @@ end end - # xdescribe "#valid_dates?" do - # it "Returns true if given check_out date is after check_in date" do - # @new_date_range.valid_dates?.must_equal true - # end - # it "Returns false if given check_out date is before check_in date" do - # date_range = BookingSystem::DateRange.new("17-09-2017", "15-09-2017") - # proc { date_range.valid_dates? }.must_raise ArgumentError - # end - # end - # - # xdescribe "dates_within_range" do - # it "Returns an array" do - # @new_date_range.dates_within_range.must_be_kind_of Array - # end - # it "Returns an array of dates within the given range" do - # @new_date_range.dates_within_range.must_equal [a, b, c] - # end - # end - - + describe "dates_within_range" do + it "Returns an array" do + @new_date_range.dates_within_range.must_be_kind_of Array + end + it "Returns the array of right length for short ranges" do + @new_date_range.dates_within_range.length.must_equal 2 + end + it "Returns the array of right length for long ranges" do + check_in = Date.new(2017,9,15) + check_out = Date.new(2017,10,15) + BookingSystem::DateRange.new(check_in, check_out).dates_within_range.length.must_equal 30 + end + end end #end of discribe From 375743498361e73657309a715aa2dfef3015fa12 Mon Sep 17 00:00:00 2001 From: Iuliia Chikulaeva Date: Tue, 5 Sep 2017 21:12:45 -0700 Subject: [PATCH 06/44] Added new files: reservation.rb and reservation_spec.rb --- lib/reservation.rb | 0 specs/reservation_spec.rb | 20 ++++++++++++++++++++ specs/spec_helper.rb | 2 +- 3 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 lib/reservation.rb create mode 100644 specs/reservation_spec.rb diff --git a/lib/reservation.rb b/lib/reservation.rb new file mode 100644 index 000000000..e69de29bb diff --git a/specs/reservation_spec.rb b/specs/reservation_spec.rb new file mode 100644 index 000000000..0d7732417 --- /dev/null +++ b/specs/reservation_spec.rb @@ -0,0 +1,20 @@ +require_relative 'spec_helper' + +describe "Reservation" do + before do + @check_in = Date.new(2017,9,15) + @check_out = Date.new(2017,9,17) + @date_range = BookingSystem::DateRange.new(@check_in, @check_out) + @new_reservation = BookingSystem::Reservation.new(@date_range) + end + + describe "#initialize" do + it "ID should be an integer" do + @new_reservation.id.must_be_kind_of Integer + end + it "" do + + end + end + +end #end of discribe diff --git a/specs/spec_helper.rb b/specs/spec_helper.rb index 5af53f455..686227ebb 100644 --- a/specs/spec_helper.rb +++ b/specs/spec_helper.rb @@ -8,7 +8,7 @@ # Require any classes require_relative '../lib/date_range' -# require_relative '../lib/scoring' +require_relative '../lib/reservation' # require_relative '../lib/tile_bag' Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new From f6859ad8ed76af98ae2fd490a2e1b294eb2585d4 Mon Sep 17 00:00:00 2001 From: Iuliia Chikulaeva Date: Tue, 5 Sep 2017 21:41:20 -0700 Subject: [PATCH 07/44] Added tests for initialize method for reservation class and passed some of them --- lib/reservation.rb | 25 +++++++++++++++++++++++++ specs/reservation_spec.rb | 18 ++++++++++++++++-- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/lib/reservation.rb b/lib/reservation.rb index e69de29bb..6737e0a67 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -0,0 +1,25 @@ +require_relative 'date_range' + +module BookingSystem + class Reservation + + @@id_count = 1 + COST = 200 + + attr_reader :id, :date_range, :room, :total_cost + + def initialize(date_range) + @id = @@id_count + @@id_count += 1 + @date_range = date_range + @room = room + @total_cost = COST * date_range.dates_within_range.length + + end #end of initialize + + + + + end #end of class + +end #end of module diff --git a/specs/reservation_spec.rb b/specs/reservation_spec.rb index 0d7732417..d2e1c9fd2 100644 --- a/specs/reservation_spec.rb +++ b/specs/reservation_spec.rb @@ -6,15 +6,29 @@ @check_out = Date.new(2017,9,17) @date_range = BookingSystem::DateRange.new(@check_in, @check_out) @new_reservation = BookingSystem::Reservation.new(@date_range) + @another_reservation = BookingSystem::Reservation.new(@date_range) end describe "#initialize" do it "ID should be an integer" do @new_reservation.id.must_be_kind_of Integer end - it "" do - + it "ID should be 1 for first reservation" do + @new_reservation.id.must_equal 1 end + it "ID should be 2 for second reservation" do + @another_reservation.id.must_equal 2 + end + it "Date_range can be called by .date_range" do + @new_reservation.date_range.must_equal @date_range + end + it "Total cost should be an integer" do + @new_reservation.total_cost.must_be_kind_of Integer + end + it "Returns the rigth total cost" do + @new_reservation.total_cost.must_equal 400 + end + end end #end of discribe From 44584d12d91b66390d8973073ead9b708ba01fa9 Mon Sep 17 00:00:00 2001 From: Iuliia Chikulaeva Date: Tue, 5 Sep 2017 21:44:55 -0700 Subject: [PATCH 08/44] Added new files: hotel.rb and hotel_spec.rb --- lib/hotel.rb | 9 +++++++++ specs/hotel_spec.rb | 7 +++++++ specs/spec_helper.rb | 2 +- 3 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 lib/hotel.rb create mode 100644 specs/hotel_spec.rb diff --git a/lib/hotel.rb b/lib/hotel.rb new file mode 100644 index 000000000..39205b21e --- /dev/null +++ b/lib/hotel.rb @@ -0,0 +1,9 @@ +require_relative 'reservation' +require_relative 'date_range' + +module BookingSystem + class Hotel + + end #end of class + +end #end of module diff --git a/specs/hotel_spec.rb b/specs/hotel_spec.rb new file mode 100644 index 000000000..e4d2810cf --- /dev/null +++ b/specs/hotel_spec.rb @@ -0,0 +1,7 @@ +require_relative 'spec_helper' + +describe "Hotel" do + describe "#initialize" do + + end +end #end of describe diff --git a/specs/spec_helper.rb b/specs/spec_helper.rb index 686227ebb..3e8f818a1 100644 --- a/specs/spec_helper.rb +++ b/specs/spec_helper.rb @@ -9,6 +9,6 @@ # Require any classes require_relative '../lib/date_range' require_relative '../lib/reservation' -# require_relative '../lib/tile_bag' +require_relative '../lib/hotel' Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new From 8f9f4a8749aa4e138d11b88cf05dc7823731f277 Mon Sep 17 00:00:00 2001 From: Iuliia Chikulaeva Date: Wed, 6 Sep 2017 11:26:37 -0700 Subject: [PATCH 09/44] Added method room_unavailable in class hotel, tests and passed all of them --- lib/hotel.rb | 26 ++++++++++++++++++++++++++ lib/reservation.rb | 3 ++- specs/hotel_spec.rb | 27 ++++++++++++++++++++++++++- specs/reservation_spec.rb | 7 ------- 4 files changed, 54 insertions(+), 9 deletions(-) diff --git a/lib/hotel.rb b/lib/hotel.rb index 39205b21e..939299e7b 100644 --- a/lib/hotel.rb +++ b/lib/hotel.rb @@ -4,6 +4,32 @@ module BookingSystem class Hotel + attr_reader :rooms, :all_reservations + + def initialize(number_of_rooms) + @rooms = (1 .. number_of_rooms).to_a + @all_reservations = [] #array of instances of class Reservation + end #end of initialize + + def room_unavailable(room) + dates = [] + all_reservations.each do |reservation| + if reservation.room == room + reservation.date_range.dates_within_range.each do |date| + dates << date + end + end + end + return dates #array of dates on which this room is unavailable + end #end of method + + # def make_reservation(date_range) + # @all_reservations.each do |reservation| + # if + # end + # + # end #end of method + end #end of class end #end of module diff --git a/lib/reservation.rb b/lib/reservation.rb index 6737e0a67..cdc672b4b 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -14,10 +14,11 @@ def initialize(date_range) @date_range = date_range @room = room @total_cost = COST * date_range.dates_within_range.length + puts "Reservation was created" end #end of initialize - + end #end of class diff --git a/specs/hotel_spec.rb b/specs/hotel_spec.rb index e4d2810cf..46f943f3c 100644 --- a/specs/hotel_spec.rb +++ b/specs/hotel_spec.rb @@ -1,7 +1,32 @@ require_relative 'spec_helper' describe "Hotel" do + before do + @new_hotel = BookingSystem::Hotel.new(20) + end describe "#initialize" do - + it "@rooms should be kind of array" do + @new_hotel.rooms.must_be_kind_of Array + end + it "Should return an array of the rigth length" do + @new_hotel.rooms.length.must_equal 20 + end + it "Each room should be kind of integer" do + @new_hotel.rooms[5].must_be_kind_of Integer + end + it "All_reservations must be kind of array" do + @new_hotel.all_reservations.must_be_kind_of Array + end + end + + describe "#room unavailable" do + it "Returns an array of dates for given room number" do + @new_hotel.room_unavailable(5).must_be_kind_of Array + end + it "Returns empty array if no reservations were made for given room number" do + @new_hotel.room_unavailable(5).length.must_equal 0 + end end + + end #end of describe diff --git a/specs/reservation_spec.rb b/specs/reservation_spec.rb index d2e1c9fd2..c06fc69a2 100644 --- a/specs/reservation_spec.rb +++ b/specs/reservation_spec.rb @@ -6,19 +6,12 @@ @check_out = Date.new(2017,9,17) @date_range = BookingSystem::DateRange.new(@check_in, @check_out) @new_reservation = BookingSystem::Reservation.new(@date_range) - @another_reservation = BookingSystem::Reservation.new(@date_range) end describe "#initialize" do it "ID should be an integer" do @new_reservation.id.must_be_kind_of Integer end - it "ID should be 1 for first reservation" do - @new_reservation.id.must_equal 1 - end - it "ID should be 2 for second reservation" do - @another_reservation.id.must_equal 2 - end it "Date_range can be called by .date_range" do @new_reservation.date_range.must_equal @date_range end From 56ea850c85898db98ad96e4311b5b14ac76e7cda Mon Sep 17 00:00:00 2001 From: Iuliia Chikulaeva Date: Wed, 6 Sep 2017 11:46:05 -0700 Subject: [PATCH 10/44] Added new method find_room and tests for class Hotel; passed all the tests --- lib/hotel.rb | 25 ++++++++++++++++++------- specs/hotel_spec.rb | 14 +++++++++++++- 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/lib/hotel.rb b/lib/hotel.rb index 939299e7b..cdb8203b2 100644 --- a/lib/hotel.rb +++ b/lib/hotel.rb @@ -13,7 +13,7 @@ def initialize(number_of_rooms) def room_unavailable(room) dates = [] - all_reservations.each do |reservation| + @all_reservations.each do |reservation| if reservation.room == room reservation.date_range.dates_within_range.each do |date| dates << date @@ -23,12 +23,23 @@ def room_unavailable(room) return dates #array of dates on which this room is unavailable end #end of method - # def make_reservation(date_range) - # @all_reservations.each do |reservation| - # if - # end - # - # end #end of method + def find_room(date_range) + available_room = nil + @rooms.each_with_index do |room, i| + if !room_unavailable(i + 1).include?(date_range.dates_within_range) + available_room = room + break + end + end + return available_room + end #end of method + + def make_reservation(date_range) + + + + + end #end of method end #end of class diff --git a/specs/hotel_spec.rb b/specs/hotel_spec.rb index 46f943f3c..943a9a9a0 100644 --- a/specs/hotel_spec.rb +++ b/specs/hotel_spec.rb @@ -3,6 +3,9 @@ describe "Hotel" do before do @new_hotel = BookingSystem::Hotel.new(20) + @check_in = Date.new(2017,9,15) + @check_out = Date.new(2017,9,17) + @date_range = BookingSystem::DateRange.new(@check_in, @check_out) end describe "#initialize" do it "@rooms should be kind of array" do @@ -19,7 +22,7 @@ end end - describe "#room unavailable" do + describe "#room_unavailable" do it "Returns an array of dates for given room number" do @new_hotel.room_unavailable(5).must_be_kind_of Array end @@ -28,5 +31,14 @@ end end + describe "find_room" do + it "Returns an integer" do + @new_hotel.find_room(@date_range).must_be_kind_of Integer + end + it "Should return 1 when all rooms are available" do + @new_hotel.find_room(@date_range).must_equal 1 + end + end + end #end of describe From f6da1fdccb83ff5d038469929de588a9dfe50464 Mon Sep 17 00:00:00 2001 From: Iuliia Chikulaeva Date: Wed, 6 Sep 2017 12:13:02 -0700 Subject: [PATCH 11/44] Changed #make_reservation in Hotel and #initialize in Reservation; added new parameter - room --- lib/hotel.rb | 10 ++++++---- lib/reservation.rb | 4 ++-- specs/hotel_spec.rb | 15 ++++++++++++++- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/lib/hotel.rb b/lib/hotel.rb index cdb8203b2..170969438 100644 --- a/lib/hotel.rb +++ b/lib/hotel.rb @@ -35,10 +35,12 @@ def find_room(date_range) end #end of method def make_reservation(date_range) - - - - + room = find_room(date_range) + if room != nil + new_reservation = Reservation.new(date_range, room) + @all_reservations << new_reservation + end + return new_reservation #new instance of class Reservation end #end of method end #end of class diff --git a/lib/reservation.rb b/lib/reservation.rb index cdc672b4b..40ab90fbb 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -8,7 +8,7 @@ class Reservation attr_reader :id, :date_range, :room, :total_cost - def initialize(date_range) + def initialize(date_range, room) @id = @@id_count @@id_count += 1 @date_range = date_range @@ -18,7 +18,7 @@ def initialize(date_range) end #end of initialize - + end #end of class diff --git a/specs/hotel_spec.rb b/specs/hotel_spec.rb index 943a9a9a0..933d488dd 100644 --- a/specs/hotel_spec.rb +++ b/specs/hotel_spec.rb @@ -22,6 +22,7 @@ end end + #TODO more tests using make_reservation describe "#room_unavailable" do it "Returns an array of dates for given room number" do @new_hotel.room_unavailable(5).must_be_kind_of Array @@ -29,9 +30,13 @@ it "Returns empty array if no reservations were made for given room number" do @new_hotel.room_unavailable(5).length.must_equal 0 end + xit "" do + + end end - describe "find_room" do + #TODO more tests using make_reservation + describe "#find_room" do it "Returns an integer" do @new_hotel.find_room(@date_range).must_be_kind_of Integer end @@ -40,5 +45,13 @@ end end + describe "#make_reservation" do + it "Returns an instance of class reservation" do + @new_hotel.make_reservation(@date_range).must_be_kind_of Reservation + end + end + + + end #end of describe From c1f31602cd1291480711024c08ab9c975957260a Mon Sep 17 00:00:00 2001 From: Iuliia Chikulaeva Date: Wed, 6 Sep 2017 12:14:42 -0700 Subject: [PATCH 12/44] Fixed reservation_spec adding new parameter-room --- specs/hotel_spec.rb | 6 +++--- specs/reservation_spec.rb | 3 ++- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/specs/hotel_spec.rb b/specs/hotel_spec.rb index 933d488dd..c60537257 100644 --- a/specs/hotel_spec.rb +++ b/specs/hotel_spec.rb @@ -23,7 +23,7 @@ end #TODO more tests using make_reservation - describe "#room_unavailable" do + xdescribe "#room_unavailable" do it "Returns an array of dates for given room number" do @new_hotel.room_unavailable(5).must_be_kind_of Array end @@ -36,7 +36,7 @@ end #TODO more tests using make_reservation - describe "#find_room" do + xdescribe "#find_room" do it "Returns an integer" do @new_hotel.find_room(@date_range).must_be_kind_of Integer end @@ -45,7 +45,7 @@ end end - describe "#make_reservation" do + xdescribe "#make_reservation" do it "Returns an instance of class reservation" do @new_hotel.make_reservation(@date_range).must_be_kind_of Reservation end diff --git a/specs/reservation_spec.rb b/specs/reservation_spec.rb index c06fc69a2..4a162a1a8 100644 --- a/specs/reservation_spec.rb +++ b/specs/reservation_spec.rb @@ -4,8 +4,9 @@ before do @check_in = Date.new(2017,9,15) @check_out = Date.new(2017,9,17) + @room = 5 @date_range = BookingSystem::DateRange.new(@check_in, @check_out) - @new_reservation = BookingSystem::Reservation.new(@date_range) + @new_reservation = BookingSystem::Reservation.new(@date_range, @room) end describe "#initialize" do From 82c0bdeed47dbd2dfafe615207ff01d93ea4f9c0 Mon Sep 17 00:00:00 2001 From: Iuliia Chikulaeva Date: Wed, 6 Sep 2017 12:40:22 -0700 Subject: [PATCH 13/44] Debugged all the tests --- specs/hotel_spec.rb | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/specs/hotel_spec.rb b/specs/hotel_spec.rb index c60537257..9ac119e47 100644 --- a/specs/hotel_spec.rb +++ b/specs/hotel_spec.rb @@ -20,23 +20,27 @@ it "All_reservations must be kind of array" do @new_hotel.all_reservations.must_be_kind_of Array end + it "All_reservations must be empty if no reservations were made" do + @new_hotel.all_reservations.length.must_equal 0 + end end #TODO more tests using make_reservation - xdescribe "#room_unavailable" do + describe "#room_unavailable" do it "Returns an array of dates for given room number" do @new_hotel.room_unavailable(5).must_be_kind_of Array end it "Returns empty array if no reservations were made for given room number" do @new_hotel.room_unavailable(5).length.must_equal 0 end - xit "" do - + it "Returns an array of one element if one reservation was made for given room number" do + new_reservation = @new_hotel.make_reservation(@date_range) + @new_hotel.room_unavailable(1).length.must_equal @date_range.dates_within_range.length end end #TODO more tests using make_reservation - xdescribe "#find_room" do + describe "#find_room" do it "Returns an integer" do @new_hotel.find_room(@date_range).must_be_kind_of Integer end @@ -45,9 +49,13 @@ end end - xdescribe "#make_reservation" do + describe "#make_reservation" do it "Returns an instance of class reservation" do - @new_hotel.make_reservation(@date_range).must_be_kind_of Reservation + @new_hotel.make_reservation(@date_range).must_be_kind_of BookingSystem::Reservation + end + it "Adds new reservation to all_reservations array" do + first_reservation = @new_hotel.make_reservation(@date_range) + @new_hotel.all_reservations.length.must_equal 1 end end From 093781daedbf152c88701a98a244eaeb79dbe04c Mon Sep 17 00:00:00 2001 From: Iuliia Chikulaeva Date: Wed, 6 Sep 2017 13:05:27 -0700 Subject: [PATCH 14/44] Raised an error if no available room found --- lib/hotel.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/hotel.rb b/lib/hotel.rb index 170969438..9f5ba2b54 100644 --- a/lib/hotel.rb +++ b/lib/hotel.rb @@ -31,11 +31,14 @@ def find_room(date_range) break end end - return available_room + return available_room #room number end #end of method def make_reservation(date_range) room = find_room(date_range) + if room == nil + raise ArgumentError.new("No room is available on these dates") + end if room != nil new_reservation = Reservation.new(date_range, room) @all_reservations << new_reservation From 94cc7ebc6937a4e3e3f61ce20012bd8e6d7c181d Mon Sep 17 00:00:00 2001 From: Iuliia Chikulaeva Date: Wed, 6 Sep 2017 13:55:46 -0700 Subject: [PATCH 15/44] Added new test to check that the next available room will be picked during reservation --- lib/hotel.rb | 20 ++++++++++++++++++-- specs/hotel_spec.rb | 21 +++++++++++++++++++++ 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/lib/hotel.rb b/lib/hotel.rb index 9f5ba2b54..e6b3bca16 100644 --- a/lib/hotel.rb +++ b/lib/hotel.rb @@ -24,12 +24,28 @@ def room_unavailable(room) end #end of method def find_room(date_range) + # puts "Checking #{date_range}, #{date_range.dates_within_range}" available_room = nil @rooms.each_with_index do |room, i| - if !room_unavailable(i + 1).include?(date_range.dates_within_range) + booked_dates = room_unavailable(i + 1) #array of dates + # puts "Room #{room} unavailable dates #{booked_dates}" + + count = 0 + date_range.dates_within_range.each do |date| + if !booked_dates.include?(date) + count += 1 + end + end + if count == date_range.dates_within_range.length available_room = room break end + + # if !booked_dates.include?(date_range.dates_within_range) + # available_room = room + # puts room + # break + # end end return available_room #room number end #end of method @@ -37,7 +53,7 @@ def find_room(date_range) def make_reservation(date_range) room = find_room(date_range) if room == nil - raise ArgumentError.new("No room is available on these dates") + raise ArgumentError.new("No room is available on given dates") end if room != nil new_reservation = Reservation.new(date_range, room) diff --git a/specs/hotel_spec.rb b/specs/hotel_spec.rb index 9ac119e47..ac5c614b0 100644 --- a/specs/hotel_spec.rb +++ b/specs/hotel_spec.rb @@ -57,6 +57,27 @@ first_reservation = @new_hotel.make_reservation(@date_range) @new_hotel.all_reservations.length.must_equal 1 end + it "Pick the next available room out of all rooms" do + hotel = BookingSystem::Hotel.new(2) + check_in = Date.new(2017,9,15) + check_out = Date.new(2017,9,17) + second_check_in = Date.new(2017,9,17) + second_check_out = Date.new(2017,9,19) + third_check_in = Date.new(2017,9,15) + third_check_out = Date.new(2017,9,17) + date_range = BookingSystem::DateRange.new(check_in, check_out) + second_date_range = BookingSystem::DateRange.new(second_check_in, second_check_out) + third_date_range = BookingSystem::DateRange.new(third_check_in, third_check_out) + first_reservation = hotel.make_reservation(date_range) + hotel.all_reservations[0].room.must_equal 1 + hotel.room_unavailable(1).length.must_equal 2 + second_reservation = hotel.make_reservation(second_date_range) + hotel.room_unavailable(1).length.must_equal 4 + hotel.room_unavailable(2).length.must_equal 0 + third_reservation = hotel.make_reservation(third_date_range) + hotel.room_unavailable(1).length.must_equal 4 + hotel.room_unavailable(2).length.must_equal 2 + end end From d81201378b76cc96befa0744b0f6491856225985 Mon Sep 17 00:00:00 2001 From: Iuliia Chikulaeva Date: Wed, 6 Sep 2017 14:03:18 -0700 Subject: [PATCH 16/44] Added new file for custom error message no_room_available --- lib/hotel.rb | 9 ++++----- lib/no_room_available.rb | 4 ++++ specs/spec_helper.rb | 1 + 3 files changed, 9 insertions(+), 5 deletions(-) create mode 100644 lib/no_room_available.rb diff --git a/lib/hotel.rb b/lib/hotel.rb index e6b3bca16..ad7a6ab54 100644 --- a/lib/hotel.rb +++ b/lib/hotel.rb @@ -53,12 +53,11 @@ def find_room(date_range) def make_reservation(date_range) room = find_room(date_range) if room == nil - raise ArgumentError.new("No room is available on given dates") - end - if room != nil - new_reservation = Reservation.new(date_range, room) - @all_reservations << new_reservation + raise NoRoomAvailable.new("No room is available on given dates") end + new_reservation = Reservation.new(date_range, room) + @all_reservations << new_reservation + return new_reservation #new instance of class Reservation end #end of method diff --git a/lib/no_room_available.rb b/lib/no_room_available.rb new file mode 100644 index 000000000..5969f27d1 --- /dev/null +++ b/lib/no_room_available.rb @@ -0,0 +1,4 @@ +module BookingSystem + class NoRoomAvailable < ArgumentError + end +end #end of module diff --git a/specs/spec_helper.rb b/specs/spec_helper.rb index 3e8f818a1..1cb9b383f 100644 --- a/specs/spec_helper.rb +++ b/specs/spec_helper.rb @@ -10,5 +10,6 @@ require_relative '../lib/date_range' require_relative '../lib/reservation' require_relative '../lib/hotel' +require_relative '../lib/no_room_available' Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new From 040ea78460adf076f33c21623609496f2a8546a5 Mon Sep 17 00:00:00 2001 From: Iuliia Chikulaeva Date: Wed, 6 Sep 2017 14:09:29 -0700 Subject: [PATCH 17/44] Added test for raised error in hotel class --- lib/hotel.rb | 11 ++--------- lib/no_room_available.rb | 2 +- lib/reservation.rb | 1 - specs/hotel_spec.rb | 11 +++++++++++ 4 files changed, 14 insertions(+), 11 deletions(-) diff --git a/lib/hotel.rb b/lib/hotel.rb index ad7a6ab54..d56b1ed65 100644 --- a/lib/hotel.rb +++ b/lib/hotel.rb @@ -1,5 +1,6 @@ require_relative 'reservation' require_relative 'date_range' +require_relative 'no_room_available' module BookingSystem class Hotel @@ -24,11 +25,9 @@ def room_unavailable(room) end #end of method def find_room(date_range) - # puts "Checking #{date_range}, #{date_range.dates_within_range}" available_room = nil @rooms.each_with_index do |room, i| booked_dates = room_unavailable(i + 1) #array of dates - # puts "Room #{room} unavailable dates #{booked_dates}" count = 0 date_range.dates_within_range.each do |date| @@ -40,12 +39,6 @@ def find_room(date_range) available_room = room break end - - # if !booked_dates.include?(date_range.dates_within_range) - # available_room = room - # puts room - # break - # end end return available_room #room number end #end of method @@ -53,7 +46,7 @@ def find_room(date_range) def make_reservation(date_range) room = find_room(date_range) if room == nil - raise NoRoomAvailable.new("No room is available on given dates") + raise NoRoomAvailableError.new("No room is available on given dates") end new_reservation = Reservation.new(date_range, room) @all_reservations << new_reservation diff --git a/lib/no_room_available.rb b/lib/no_room_available.rb index 5969f27d1..cb3526712 100644 --- a/lib/no_room_available.rb +++ b/lib/no_room_available.rb @@ -1,4 +1,4 @@ module BookingSystem - class NoRoomAvailable < ArgumentError + class NoRoomAvailableError < ArgumentError end end #end of module diff --git a/lib/reservation.rb b/lib/reservation.rb index 40ab90fbb..4683b482c 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -14,7 +14,6 @@ def initialize(date_range, room) @date_range = date_range @room = room @total_cost = COST * date_range.dates_within_range.length - puts "Reservation was created" end #end of initialize diff --git a/specs/hotel_spec.rb b/specs/hotel_spec.rb index ac5c614b0..67b6ef39e 100644 --- a/specs/hotel_spec.rb +++ b/specs/hotel_spec.rb @@ -78,9 +78,20 @@ hotel.room_unavailable(1).length.must_equal 4 hotel.room_unavailable(2).length.must_equal 2 end + it "Raise an error if no room is available withing given dates" do + hotel = BookingSystem::Hotel.new(2) + check_in = Date.new(2017,9,15) + check_out = Date.new(2017,9,17) + date_range = BookingSystem::DateRange.new(check_in, check_out) + reservation = hotel.make_reservation(date_range) + second_reservation = hotel.make_reservation(date_range) + proc { hotel.make_reservation(date_range) }.must_raise BookingSystem::NoRoomAvailableError + end end + + end #end of describe From 6b1e338a4fae02d13945a25fc7790b7cc0caf061 Mon Sep 17 00:00:00 2001 From: Iuliia Chikulaeva Date: Wed, 6 Sep 2017 14:21:31 -0700 Subject: [PATCH 18/44] Added new method list_of_reservations and tests for it --- lib/hotel.rb | 13 +++++++++++++ specs/hotel_spec.rb | 20 ++++++++++++++++++-- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/lib/hotel.rb b/lib/hotel.rb index d56b1ed65..2d3121f1e 100644 --- a/lib/hotel.rb +++ b/lib/hotel.rb @@ -54,6 +54,19 @@ def make_reservation(date_range) return new_reservation #new instance of class Reservation end #end of method + def list_of_reservations(date) + list = [] + @all_reservations.each do |reservation| + if reservation.date_range.dates_within_range.include?(date) + list << reservation + end + end + + # list = @all_reservations.map { |reservation| reservation if reservation.date_range.dates_within_range.include?(date) } + + return list + end #end of method + end #end of class end #end of module diff --git a/specs/hotel_spec.rb b/specs/hotel_spec.rb index 67b6ef39e..0d0dcbc6b 100644 --- a/specs/hotel_spec.rb +++ b/specs/hotel_spec.rb @@ -25,7 +25,6 @@ end end - #TODO more tests using make_reservation describe "#room_unavailable" do it "Returns an array of dates for given room number" do @new_hotel.room_unavailable(5).must_be_kind_of Array @@ -39,7 +38,6 @@ end end - #TODO more tests using make_reservation describe "#find_room" do it "Returns an integer" do @new_hotel.find_room(@date_range).must_be_kind_of Integer @@ -89,6 +87,24 @@ end end + describe "#list_of_reservations" do + before do + @hotel = BookingSystem::Hotel.new(2) + @check_in = Date.new(2017,9,15) + @check_out = Date.new(2017,9,17) + @date_range = BookingSystem::DateRange.new(@check_in, @check_out) + @reservation = @hotel.make_reservation(@date_range) + @second_reservation = @hotel.make_reservation(@date_range) + @date = Date.new(2017,9,15) + end + it "Returns an instance of array class " do + @hotel.list_of_reservations(@date).must_be_kind_of Array + end + it "Returns an array of the right length" do + @hotel.list_of_reservations(@date).length.must_equal 2 + end + end + From d4b29ec7c83560104dd2d3b91d2f72a4626ebff9 Mon Sep 17 00:00:00 2001 From: Iuliia Chikulaeva Date: Wed, 6 Sep 2017 15:28:26 -0700 Subject: [PATCH 19/44] Added new method list_of_available_rooms in hotel class and tests for it --- lib/hotel.rb | 31 ++++++++++++++++------- specs/hotel_spec.rb | 60 ++++++++++++++++++++++----------------------- 2 files changed, 51 insertions(+), 40 deletions(-) diff --git a/lib/hotel.rb b/lib/hotel.rb index 2d3121f1e..c086d9ea7 100644 --- a/lib/hotel.rb +++ b/lib/hotel.rb @@ -24,6 +24,24 @@ def room_unavailable(room) return dates #array of dates on which this room is unavailable end #end of method + def list_of_available_rooms(date_range) + available_rooms = [] + @rooms.each_with_index do |room, i| + booked_dates = room_unavailable(i + 1) #array of dates + + count = 0 + date_range.dates_within_range.each do |date| + if !booked_dates.include?(date) + count += 1 + end + end + if count == date_range.dates_within_range.length + available_rooms << room + end + end + return available_rooms #array of rooms as integers + end #end of method + def find_room(date_range) available_room = nil @rooms.each_with_index do |room, i| @@ -55,18 +73,13 @@ def make_reservation(date_range) end #end of method def list_of_reservations(date) - list = [] - @all_reservations.each do |reservation| - if reservation.date_range.dates_within_range.include?(date) - list << reservation - end - end - - # list = @all_reservations.map { |reservation| reservation if reservation.date_range.dates_within_range.include?(date) } + list = @all_reservations.map { |reservation| reservation if reservation.date_range.dates_within_range.include?(date) } - return list + return list #array of reservations end #end of method + + end #end of class end #end of module diff --git a/specs/hotel_spec.rb b/specs/hotel_spec.rb index 0d0dcbc6b..5059aaba5 100644 --- a/specs/hotel_spec.rb +++ b/specs/hotel_spec.rb @@ -2,7 +2,7 @@ describe "Hotel" do before do - @new_hotel = BookingSystem::Hotel.new(20) + @new_hotel = BookingSystem::Hotel.new(2) @check_in = Date.new(2017,9,15) @check_out = Date.new(2017,9,17) @date_range = BookingSystem::DateRange.new(@check_in, @check_out) @@ -12,10 +12,10 @@ @new_hotel.rooms.must_be_kind_of Array end it "Should return an array of the rigth length" do - @new_hotel.rooms.length.must_equal 20 + @new_hotel.rooms.length.must_equal 2 end it "Each room should be kind of integer" do - @new_hotel.rooms[5].must_be_kind_of Integer + @new_hotel.rooms[1].must_be_kind_of Integer end it "All_reservations must be kind of array" do @new_hotel.all_reservations.must_be_kind_of Array @@ -56,52 +56,50 @@ @new_hotel.all_reservations.length.must_equal 1 end it "Pick the next available room out of all rooms" do - hotel = BookingSystem::Hotel.new(2) - check_in = Date.new(2017,9,15) - check_out = Date.new(2017,9,17) second_check_in = Date.new(2017,9,17) second_check_out = Date.new(2017,9,19) third_check_in = Date.new(2017,9,15) third_check_out = Date.new(2017,9,17) - date_range = BookingSystem::DateRange.new(check_in, check_out) second_date_range = BookingSystem::DateRange.new(second_check_in, second_check_out) third_date_range = BookingSystem::DateRange.new(third_check_in, third_check_out) - first_reservation = hotel.make_reservation(date_range) - hotel.all_reservations[0].room.must_equal 1 - hotel.room_unavailable(1).length.must_equal 2 - second_reservation = hotel.make_reservation(second_date_range) - hotel.room_unavailable(1).length.must_equal 4 - hotel.room_unavailable(2).length.must_equal 0 - third_reservation = hotel.make_reservation(third_date_range) - hotel.room_unavailable(1).length.must_equal 4 - hotel.room_unavailable(2).length.must_equal 2 + first_reservation = @new_hotel.make_reservation(@date_range) + @new_hotel.all_reservations[0].room.must_equal 1 + @new_hotel.room_unavailable(1).length.must_equal 2 + second_reservation = @new_hotel.make_reservation(second_date_range) + @new_hotel.room_unavailable(1).length.must_equal 4 + @new_hotel.room_unavailable(2).length.must_equal 0 + third_reservation = @new_hotel.make_reservation(third_date_range) + @new_hotel.room_unavailable(1).length.must_equal 4 + @new_hotel.room_unavailable(2).length.must_equal 2 end it "Raise an error if no room is available withing given dates" do - hotel = BookingSystem::Hotel.new(2) - check_in = Date.new(2017,9,15) - check_out = Date.new(2017,9,17) - date_range = BookingSystem::DateRange.new(check_in, check_out) - reservation = hotel.make_reservation(date_range) - second_reservation = hotel.make_reservation(date_range) - proc { hotel.make_reservation(date_range) }.must_raise BookingSystem::NoRoomAvailableError + reservation = @new_hotel.make_reservation(@date_range) + second_reservation = @new_hotel.make_reservation(@date_range) + proc { @new_hotel.make_reservation(@date_range) }.must_raise BookingSystem::NoRoomAvailableError end end describe "#list_of_reservations" do before do - @hotel = BookingSystem::Hotel.new(2) - @check_in = Date.new(2017,9,15) - @check_out = Date.new(2017,9,17) - @date_range = BookingSystem::DateRange.new(@check_in, @check_out) - @reservation = @hotel.make_reservation(@date_range) - @second_reservation = @hotel.make_reservation(@date_range) + @reservation = @new_hotel.make_reservation(@date_range) + @second_reservation = @new_hotel.make_reservation(@date_range) @date = Date.new(2017,9,15) end it "Returns an instance of array class " do - @hotel.list_of_reservations(@date).must_be_kind_of Array + @new_hotel.list_of_reservations(@date).must_be_kind_of Array end it "Returns an array of the right length" do - @hotel.list_of_reservations(@date).length.must_equal 2 + @new_hotel.list_of_reservations(@date).length.must_equal 2 + end + end + + describe "#list_of_available_rooms" do + it "Returns an instanse of array" do + @new_hotel.list_of_available_rooms(@date_range).must_be_kind_of Array + end + it "Returns the 2nd room if the 1st one is booked for hotel with 2 rooms" do + reservation = @new_hotel.make_reservation(@date_range) + @new_hotel.list_of_available_rooms(@date_range)[0].must_equal 2 end end From 3c24b40865caf355d94b076f0f4037786f597a05 Mon Sep 17 00:00:00 2001 From: Iuliia Chikulaeva Date: Wed, 6 Sep 2017 15:31:02 -0700 Subject: [PATCH 20/44] Changed method find_room by usage of list_of_available_rooms method in hotel class --- lib/hotel.rb | 17 +---------------- 1 file changed, 1 insertion(+), 16 deletions(-) diff --git a/lib/hotel.rb b/lib/hotel.rb index c086d9ea7..9825e4426 100644 --- a/lib/hotel.rb +++ b/lib/hotel.rb @@ -43,22 +43,7 @@ def list_of_available_rooms(date_range) end #end of method def find_room(date_range) - available_room = nil - @rooms.each_with_index do |room, i| - booked_dates = room_unavailable(i + 1) #array of dates - - count = 0 - date_range.dates_within_range.each do |date| - if !booked_dates.include?(date) - count += 1 - end - end - if count == date_range.dates_within_range.length - available_room = room - break - end - end - return available_room #room number + return list_of_available_rooms(date_range)[0] #room number end #end of method def make_reservation(date_range) From e9d89771189df648fa92c616334ba96ce5c34b14 Mon Sep 17 00:00:00 2001 From: Iuliia Chikulaeva Date: Wed, 6 Sep 2017 15:33:45 -0700 Subject: [PATCH 21/44] Wave 2 is finished with passing all the tests and 100% coverage --- lib/date_range.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/date_range.rb b/lib/date_range.rb index 3f6f18c24..8443a5247 100644 --- a/lib/date_range.rb +++ b/lib/date_range.rb @@ -15,7 +15,6 @@ def initialize(check_in, check_out) end end #end of initialize - def dates_within_range dates = (@check_in .. @check_out - 1).map { |date| date } return dates From 3e26440621011794ad735ba022e06049c56def2d Mon Sep 17 00:00:00 2001 From: Iuliia Chikulaeva Date: Wed, 6 Sep 2017 15:46:31 -0700 Subject: [PATCH 22/44] Added new files block.rb and block_spec.rb --- lib/block.rb | 5 +++++ lib/hotel.rb | 1 + specs/block_spec.rb | 1 + specs/spec_helper.rb | 1 + 4 files changed, 8 insertions(+) create mode 100644 lib/block.rb create mode 100644 specs/block_spec.rb diff --git a/lib/block.rb b/lib/block.rb new file mode 100644 index 000000000..4461fb29e --- /dev/null +++ b/lib/block.rb @@ -0,0 +1,5 @@ +module BookingSystem + class Block + + end #end of class +end #end of module diff --git a/lib/hotel.rb b/lib/hotel.rb index 9825e4426..987180427 100644 --- a/lib/hotel.rb +++ b/lib/hotel.rb @@ -63,6 +63,7 @@ def list_of_reservations(date) return list #array of reservations end #end of method + end #end of class diff --git a/specs/block_spec.rb b/specs/block_spec.rb new file mode 100644 index 000000000..ae9c220ea --- /dev/null +++ b/specs/block_spec.rb @@ -0,0 +1 @@ +require_relative 'spec_helper' diff --git a/specs/spec_helper.rb b/specs/spec_helper.rb index 1cb9b383f..793a2ac21 100644 --- a/specs/spec_helper.rb +++ b/specs/spec_helper.rb @@ -11,5 +11,6 @@ require_relative '../lib/reservation' require_relative '../lib/hotel' require_relative '../lib/no_room_available' +require_relative '../lib/block' Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new From f58da3374f1283dab03ed8613f0f2ef0594359c1 Mon Sep 17 00:00:00 2001 From: Iuliia Chikulaeva Date: Wed, 6 Sep 2017 16:02:30 -0700 Subject: [PATCH 23/44] Added class block and test for initialize method --- lib/block.rb | 18 ++++++++++++++++++ specs/block_spec.rb | 26 ++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/lib/block.rb b/lib/block.rb index 4461fb29e..1c7a389ec 100644 --- a/lib/block.rb +++ b/lib/block.rb @@ -1,5 +1,23 @@ module BookingSystem class Block + COST = 200 + DISCOUNT = 0.9 + @@id_count = 1 + + attr_reader :date_range, :number_of_rooms, :id, :block_cost + + def initialize(date_range, number_of_rooms) + @id = @@id_count + @@id_count += 1 + @date_range = date_range + @rooms = [] + @block_cost = (COST * DISCOUNT * date_range.dates_within_range.length * number_of_rooms).to_i + + end #end of initialize + + + + end #end of class end #end of module diff --git a/specs/block_spec.rb b/specs/block_spec.rb index ae9c220ea..0f970fde1 100644 --- a/specs/block_spec.rb +++ b/specs/block_spec.rb @@ -1 +1,27 @@ require_relative 'spec_helper' + +describe "Block" do + before do + @number_of_rooms = 3 + @check_in = Date.new(2017,9,15) + @check_out = Date.new(2017,9,17) + @date_range = BookingSystem::DateRange.new(@check_in, @check_out) + @new_block = BookingSystem::Block.new(@date_range, @number_of_rooms) + end + describe "#initialize" do + it "ID should be an integer" do + @new_block.id.must_be_kind_of Integer + end + it "Date_range can be called by .date_range" do + @new_block.date_range.must_equal @date_range + end + it "Total cost should be an integer" do + @new_block.block_cost.must_be_kind_of Integer + end + it "Returns the rigth total cost" do + @new_block.block_cost.must_equal 1080 + end + + end + +end #end of describe From d68577eebf8657223d41169aa623e14eee71ad33 Mon Sep 17 00:00:00 2001 From: Iuliia Chikulaeva Date: Wed, 6 Sep 2017 17:10:54 -0700 Subject: [PATCH 24/44] Added tests for create_block method in hotel class --- lib/block.rb | 10 ++++------ lib/hotel.rb | 25 ++++++++++++++++++++++--- specs/block_spec.rb | 4 ++-- specs/hotel_spec.rb | 17 +++++++++++++++++ 4 files changed, 45 insertions(+), 11 deletions(-) diff --git a/lib/block.rb b/lib/block.rb index 1c7a389ec..bba1bdadf 100644 --- a/lib/block.rb +++ b/lib/block.rb @@ -5,19 +5,17 @@ class Block DISCOUNT = 0.9 @@id_count = 1 - attr_reader :date_range, :number_of_rooms, :id, :block_cost + attr_reader :date_range, :rooms, :id, :block_cost - def initialize(date_range, number_of_rooms) + def initialize(date_range, rooms) @id = @@id_count @@id_count += 1 @date_range = date_range - @rooms = [] - @block_cost = (COST * DISCOUNT * date_range.dates_within_range.length * number_of_rooms).to_i + @rooms = rooms + @block_cost = (COST * DISCOUNT * date_range.dates_within_range.length * rooms.length).to_i end #end of initialize - - end #end of class end #end of module diff --git a/lib/hotel.rb b/lib/hotel.rb index 987180427..3155d7bef 100644 --- a/lib/hotel.rb +++ b/lib/hotel.rb @@ -10,6 +10,7 @@ class Hotel def initialize(number_of_rooms) @rooms = (1 .. number_of_rooms).to_a @all_reservations = [] #array of instances of class Reservation + @all_blocks = [] #array of instances of class Block end #end of initialize def room_unavailable(room) @@ -21,8 +22,17 @@ def room_unavailable(room) end end end - return dates #array of dates on which this room is unavailable - end #end of method + @all_blocks.each do |block| + block.rooms.each do |block_room| + if block_room == room + block.date_range.dates_within_range.each do |date| + dates << date + end + end + end + end + return dates.uniq #array of dates on which this room is unavailable + end #end of method def list_of_available_rooms(date_range) available_rooms = [] @@ -63,7 +73,16 @@ def list_of_reservations(date) return list #array of reservations end #end of method - + def create_block(date_range, number_of_rooms) + if list_of_available_rooms(date_range).length < number_of_rooms + raise NoRoomAvailableError.new("No room is available on given dates") + end + block_rooms = list_of_available_rooms(date_range)[0..number_of_rooms-1] + new_block = Block.new(date_range, block_rooms) + @all_blocks << new_block + + return new_block #an array of rooms as integers + end #end of method end #end of class diff --git a/specs/block_spec.rb b/specs/block_spec.rb index 0f970fde1..b1b63a937 100644 --- a/specs/block_spec.rb +++ b/specs/block_spec.rb @@ -2,11 +2,11 @@ describe "Block" do before do - @number_of_rooms = 3 + @rooms = [1, 2, 3] @check_in = Date.new(2017,9,15) @check_out = Date.new(2017,9,17) @date_range = BookingSystem::DateRange.new(@check_in, @check_out) - @new_block = BookingSystem::Block.new(@date_range, @number_of_rooms) + @new_block = BookingSystem::Block.new(@date_range, @rooms) end describe "#initialize" do it "ID should be an integer" do diff --git a/specs/hotel_spec.rb b/specs/hotel_spec.rb index 5059aaba5..894a9c484 100644 --- a/specs/hotel_spec.rb +++ b/specs/hotel_spec.rb @@ -103,6 +103,23 @@ end end + describe "#create_block" do + before do + @hotel = BookingSystem::Hotel.new(5) + @new_block = @hotel.create_block(@date_range, 3) + end + it "New block should be an instance of Block class " do + @new_block.must_be_kind_of BookingSystem::Block + end + it "Created block should include given number of rooms" do + @new_block.rooms.length.must_equal 3 + end + it "Raise an error if no rooms available for given date range" do + proc { @hotel.create_block(@date_range, 4) }.must_raise BookingSystem::NoRoomAvailableError + end + + end #end of describe + From 5eb680719444e224404f4102bba2f81f3b692af2 Mon Sep 17 00:00:00 2001 From: Iuliia Chikulaeva Date: Wed, 6 Sep 2017 20:03:25 -0700 Subject: [PATCH 25/44] Added tests for method has_available_rooms? in hotel class and passed all of them --- lib/hotel.rb | 18 ++++++++++++++++++ specs/hotel_spec.rb | 16 ++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/lib/hotel.rb b/lib/hotel.rb index 3155d7bef..3df3530c1 100644 --- a/lib/hotel.rb +++ b/lib/hotel.rb @@ -1,6 +1,7 @@ require_relative 'reservation' require_relative 'date_range' require_relative 'no_room_available' +require_relative 'block' module BookingSystem class Hotel @@ -84,6 +85,23 @@ def create_block(date_range, number_of_rooms) return new_block #an array of rooms as integers end #end of method + def has_available_rooms?(block) + return block.rooms.length > 0 + end #end of method + + def make_reservation_from_block(block) + if !has_available_rooms?(block) + raise NoRoomAvailableError.new("No room is available on given dates") + end + date_range = block.date_range + room = block.rooms[0] + new_reservation_from_block = Reservation.new(date_range, room) + # block.rooms = block.rooms[1..block.rooms.length-1] + block.rooms.delete_at(0) + return new_reservation_from_block + + end #end of method + end #end of class diff --git a/specs/hotel_spec.rb b/specs/hotel_spec.rb index 894a9c484..0f0285f31 100644 --- a/specs/hotel_spec.rb +++ b/specs/hotel_spec.rb @@ -118,6 +118,22 @@ proc { @hotel.create_block(@date_range, 4) }.must_raise BookingSystem::NoRoomAvailableError end + describe "#has_available_rooms?" do + before do + @hotel = BookingSystem::Hotel.new(5) + @new_block = @hotel.create_block(@date_range, 3) + end + it "Returns true if there are available rooms in given block" do + @hotel.has_available_rooms?(@new_block).must_equal true + end + it "Returns false if there is no available rooms in given block" do + 3.times do + @hotel.make_reservation_from_block(@new_block) + end + @hotel.has_available_rooms?(@new_block).must_equal false + end + end + end #end of describe From cf544f3ed7cb2106807c6953224796a9182561cb Mon Sep 17 00:00:00 2001 From: Iuliia Chikulaeva Date: Wed, 6 Sep 2017 20:20:14 -0700 Subject: [PATCH 26/44] Added tests for make_reservation_from_block method in hotel class and passed all of trem --- lib/hotel.rb | 3 +-- specs/hotel_spec.rb | 24 ++++++++++++++++++++++-- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/lib/hotel.rb b/lib/hotel.rb index 3df3530c1..183251986 100644 --- a/lib/hotel.rb +++ b/lib/hotel.rb @@ -96,9 +96,8 @@ def make_reservation_from_block(block) date_range = block.date_range room = block.rooms[0] new_reservation_from_block = Reservation.new(date_range, room) - # block.rooms = block.rooms[1..block.rooms.length-1] block.rooms.delete_at(0) - return new_reservation_from_block + return new_reservation_from_block #instance of class Block end #end of method diff --git a/specs/hotel_spec.rb b/specs/hotel_spec.rb index 0f0285f31..2d3682ec6 100644 --- a/specs/hotel_spec.rb +++ b/specs/hotel_spec.rb @@ -27,10 +27,10 @@ describe "#room_unavailable" do it "Returns an array of dates for given room number" do - @new_hotel.room_unavailable(5).must_be_kind_of Array + @new_hotel.room_unavailable(1).must_be_kind_of Array end it "Returns empty array if no reservations were made for given room number" do - @new_hotel.room_unavailable(5).length.must_equal 0 + @new_hotel.room_unavailable(1).length.must_equal 0 end it "Returns an array of one element if one reservation was made for given room number" do new_reservation = @new_hotel.make_reservation(@date_range) @@ -134,6 +134,26 @@ end end + describe "#make_reservation_from_block" do + before do + @hotel = BookingSystem::Hotel.new(5) + @new_block = @hotel.create_block(@date_range, 3) + @reservation_from_block = @hotel.make_reservation_from_block(@new_block) + end + it "Returns an instance of class Reservation" do + @reservation_from_block.must_be_kind_of BookingSystem::Reservation + end + it "Booked room should be removed from rooms in block" do + @new_block.rooms.length.must_equal 2 + end + it "Raise an error if no available rooms for reservation from block" do + 2.times do + @hotel.make_reservation_from_block(@new_block) + end + proc { @hotel.make_reservation_from_block(@new_block) }.must_raise BookingSystem::NoRoomAvailableError + end + end + end #end of describe From 9b63ba1ca27434a21eb3b66a667d41dd15357d9b Mon Sep 17 00:00:00 2001 From: Iuliia Chikulaeva Date: Thu, 7 Sep 2017 09:52:36 -0700 Subject: [PATCH 27/44] Moved has_available_rooms? method from hotel class to block class and changed tests --- lib/block.rb | 4 ++++ lib/hotel.rb | 20 ++++++++++++++++---- specs/block_spec.rb | 15 +++++++++++++++ specs/hotel_spec.rb | 30 +++++++++++++++--------------- 4 files changed, 50 insertions(+), 19 deletions(-) diff --git a/lib/block.rb b/lib/block.rb index bba1bdadf..28a1df30c 100644 --- a/lib/block.rb +++ b/lib/block.rb @@ -16,6 +16,10 @@ def initialize(date_range, rooms) end #end of initialize + def has_available_rooms? + return @rooms.length > 0 + end #end of method + end #end of class end #end of module diff --git a/lib/hotel.rb b/lib/hotel.rb index 183251986..af16b5a23 100644 --- a/lib/hotel.rb +++ b/lib/hotel.rb @@ -85,12 +85,24 @@ def create_block(date_range, number_of_rooms) return new_block #an array of rooms as integers end #end of method - def has_available_rooms?(block) - return block.rooms.length > 0 - end #end of method + # def has_available_rooms?(block) + # return block.rooms.length > 0 + # end #end of method + + # def make_reservation_from_block(block) + # if !has_available_rooms?(block) + # raise NoRoomAvailableError.new("No room is available on given dates") + # end + # date_range = block.date_range + # room = block.rooms[0] + # new_reservation_from_block = Reservation.new(date_range, room) + # block.rooms.delete_at(0) + # return new_reservation_from_block #instance of class Block + # + # end #end of method def make_reservation_from_block(block) - if !has_available_rooms?(block) + if !block.has_available_rooms? raise NoRoomAvailableError.new("No room is available on given dates") end date_range = block.date_range diff --git a/specs/block_spec.rb b/specs/block_spec.rb index b1b63a937..fac0e82f0 100644 --- a/specs/block_spec.rb +++ b/specs/block_spec.rb @@ -21,7 +21,22 @@ it "Returns the rigth total cost" do @new_block.block_cost.must_equal 1080 end + end + describe "#has_available_rooms?" do + before do + @hotel = BookingSystem::Hotel.new(5) + @new_block = @hotel.create_block(@date_range, 3) + end + it "Returns true if there are available rooms in given block" do + @new_block.has_available_rooms?.must_equal true + end + it "Returns false if there is no available rooms in given block" do + 3.times do + @hotel.make_reservation_from_block(@new_block) + end + @new_block.has_available_rooms?.must_equal false + end end end #end of describe diff --git a/specs/hotel_spec.rb b/specs/hotel_spec.rb index 2d3682ec6..4efb01ba9 100644 --- a/specs/hotel_spec.rb +++ b/specs/hotel_spec.rb @@ -118,21 +118,21 @@ proc { @hotel.create_block(@date_range, 4) }.must_raise BookingSystem::NoRoomAvailableError end - describe "#has_available_rooms?" do - before do - @hotel = BookingSystem::Hotel.new(5) - @new_block = @hotel.create_block(@date_range, 3) - end - it "Returns true if there are available rooms in given block" do - @hotel.has_available_rooms?(@new_block).must_equal true - end - it "Returns false if there is no available rooms in given block" do - 3.times do - @hotel.make_reservation_from_block(@new_block) - end - @hotel.has_available_rooms?(@new_block).must_equal false - end - end + # describe "#has_available_rooms?" do + # before do + # @hotel = BookingSystem::Hotel.new(5) + # @new_block = @hotel.create_block(@date_range, 3) + # end + # it "Returns true if there are available rooms in given block" do + # @hotel.has_available_rooms?(@new_block).must_equal true + # end + # it "Returns false if there is no available rooms in given block" do + # 3.times do + # @hotel.make_reservation_from_block(@new_block) + # end + # @hotel.has_available_rooms?(@new_block).must_equal false + # end + # end describe "#make_reservation_from_block" do before do From 9f867c46e92f3991c6486a43600d38e2cce68c8e Mon Sep 17 00:00:00 2001 From: Iuliia Chikulaeva Date: Thu, 7 Sep 2017 10:03:58 -0700 Subject: [PATCH 28/44] Changed block_cost to room_cost in block class and refuctored tests for it --- lib/block.rb | 4 ++-- lib/hotel.rb | 19 ++----------------- lib/reservation.rb | 3 ++- specs/block_spec.rb | 4 ++-- specs/hotel_spec.rb | 18 +----------------- 5 files changed, 9 insertions(+), 39 deletions(-) diff --git a/lib/block.rb b/lib/block.rb index 28a1df30c..3e91b48df 100644 --- a/lib/block.rb +++ b/lib/block.rb @@ -5,14 +5,14 @@ class Block DISCOUNT = 0.9 @@id_count = 1 - attr_reader :date_range, :rooms, :id, :block_cost + attr_reader :date_range, :rooms, :id, :room_cost def initialize(date_range, rooms) @id = @@id_count @@id_count += 1 @date_range = date_range @rooms = rooms - @block_cost = (COST * DISCOUNT * date_range.dates_within_range.length * rooms.length).to_i + @room_cost = (COST * DISCOUNT).to_i end #end of initialize diff --git a/lib/hotel.rb b/lib/hotel.rb index af16b5a23..259387bef 100644 --- a/lib/hotel.rb +++ b/lib/hotel.rb @@ -85,29 +85,14 @@ def create_block(date_range, number_of_rooms) return new_block #an array of rooms as integers end #end of method - # def has_available_rooms?(block) - # return block.rooms.length > 0 - # end #end of method - - # def make_reservation_from_block(block) - # if !has_available_rooms?(block) - # raise NoRoomAvailableError.new("No room is available on given dates") - # end - # date_range = block.date_range - # room = block.rooms[0] - # new_reservation_from_block = Reservation.new(date_range, room) - # block.rooms.delete_at(0) - # return new_reservation_from_block #instance of class Block - # - # end #end of method - def make_reservation_from_block(block) if !block.has_available_rooms? raise NoRoomAvailableError.new("No room is available on given dates") end date_range = block.date_range room = block.rooms[0] - new_reservation_from_block = Reservation.new(date_range, room) + cost = block.room_cost + new_reservation_from_block = Reservation.new(date_range, room, cost) block.rooms.delete_at(0) return new_reservation_from_block #instance of class Block diff --git a/lib/reservation.rb b/lib/reservation.rb index 4683b482c..ff56da490 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -8,11 +8,12 @@ class Reservation attr_reader :id, :date_range, :room, :total_cost - def initialize(date_range, room) + def initialize(date_range, room, cost=COST) @id = @@id_count @@id_count += 1 @date_range = date_range @room = room + @cost = cost @total_cost = COST * date_range.dates_within_range.length end #end of initialize diff --git a/specs/block_spec.rb b/specs/block_spec.rb index fac0e82f0..23a570a76 100644 --- a/specs/block_spec.rb +++ b/specs/block_spec.rb @@ -16,10 +16,10 @@ @new_block.date_range.must_equal @date_range end it "Total cost should be an integer" do - @new_block.block_cost.must_be_kind_of Integer + @new_block.room_cost.must_be_kind_of Integer end it "Returns the rigth total cost" do - @new_block.block_cost.must_equal 1080 + @new_block.room_cost.must_equal 180 end end diff --git a/specs/hotel_spec.rb b/specs/hotel_spec.rb index 4efb01ba9..108ecbfe9 100644 --- a/specs/hotel_spec.rb +++ b/specs/hotel_spec.rb @@ -118,23 +118,7 @@ proc { @hotel.create_block(@date_range, 4) }.must_raise BookingSystem::NoRoomAvailableError end - # describe "#has_available_rooms?" do - # before do - # @hotel = BookingSystem::Hotel.new(5) - # @new_block = @hotel.create_block(@date_range, 3) - # end - # it "Returns true if there are available rooms in given block" do - # @hotel.has_available_rooms?(@new_block).must_equal true - # end - # it "Returns false if there is no available rooms in given block" do - # 3.times do - # @hotel.make_reservation_from_block(@new_block) - # end - # @hotel.has_available_rooms?(@new_block).must_equal false - # end - # end - - describe "#make_reservation_from_block" do + describe "#make_reservation_from_block" do before do @hotel = BookingSystem::Hotel.new(5) @new_block = @hotel.create_block(@date_range, 3) From 45a7b2eac89f08392687b728684ecbf9aade080e Mon Sep 17 00:00:00 2001 From: Iuliia Chikulaeva Date: Thu, 7 Sep 2017 10:18:53 -0700 Subject: [PATCH 29/44] Debugged total_cost issues and added tests for it --- lib/block.rb | 3 +-- lib/hotel.rb | 1 + lib/reservation.rb | 2 +- specs/hotel_spec.rb | 15 +++++++++++++++ specs/reservation_spec.rb | 3 +-- 5 files changed, 19 insertions(+), 5 deletions(-) diff --git a/lib/block.rb b/lib/block.rb index 3e91b48df..1ceb33eeb 100644 --- a/lib/block.rb +++ b/lib/block.rb @@ -1,7 +1,6 @@ module BookingSystem class Block - COST = 200 DISCOUNT = 0.9 @@id_count = 1 @@ -12,7 +11,7 @@ def initialize(date_range, rooms) @@id_count += 1 @date_range = date_range @rooms = rooms - @room_cost = (COST * DISCOUNT).to_i + @room_cost = (Reservation::COST * DISCOUNT).to_i end #end of initialize diff --git a/lib/hotel.rb b/lib/hotel.rb index 259387bef..90d09fe80 100644 --- a/lib/hotel.rb +++ b/lib/hotel.rb @@ -92,6 +92,7 @@ def make_reservation_from_block(block) date_range = block.date_range room = block.rooms[0] cost = block.room_cost + puts cost new_reservation_from_block = Reservation.new(date_range, room, cost) block.rooms.delete_at(0) return new_reservation_from_block #instance of class Block diff --git a/lib/reservation.rb b/lib/reservation.rb index ff56da490..fda7cad6b 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -14,7 +14,7 @@ def initialize(date_range, room, cost=COST) @date_range = date_range @room = room @cost = cost - @total_cost = COST * date_range.dates_within_range.length + @total_cost = cost * date_range.dates_within_range.length end #end of initialize diff --git a/specs/hotel_spec.rb b/specs/hotel_spec.rb index 108ecbfe9..85ca83e0c 100644 --- a/specs/hotel_spec.rb +++ b/specs/hotel_spec.rb @@ -138,6 +138,21 @@ end end + describe "Check total cost for two types of reservations" do + before do + @hotel = BookingSystem::Hotel.new(5) + @new_block = @hotel.create_block(@date_range, 3) + @general_reservation = @hotel.make_reservation(@date_range) + @reservation_from_block = @hotel.make_reservation_from_block(@new_block) + end + it "Returns the right total cost for general reservation" do + @general_reservation.total_cost.must_equal 400 + end + it "Returns the right total cost for reservation from block" do + @reservation_from_block.total_cost.must_equal 360 + end + end + end #end of describe diff --git a/specs/reservation_spec.rb b/specs/reservation_spec.rb index 4a162a1a8..5accec522 100644 --- a/specs/reservation_spec.rb +++ b/specs/reservation_spec.rb @@ -22,7 +22,6 @@ it "Returns the rigth total cost" do @new_reservation.total_cost.must_equal 400 end - - end + end end #end of discribe From 2edc08c489a8f76858df7c2ccd4117a9914b9f7b Mon Sep 17 00:00:00 2001 From: Iuliia Chikulaeva Date: Thu, 7 Sep 2017 13:19:13 -0700 Subject: [PATCH 30/44] Added new condition for create_ block that each block can have 5 rooms max; added tests and passed them --- lib/hotel.rb | 5 +++- specs/hotel_spec.rb | 72 ++++++++++++++++++++++++--------------------- 2 files changed, 43 insertions(+), 34 deletions(-) diff --git a/lib/hotel.rb b/lib/hotel.rb index 90d09fe80..0cd818418 100644 --- a/lib/hotel.rb +++ b/lib/hotel.rb @@ -75,6 +75,9 @@ def list_of_reservations(date) end #end of method def create_block(date_range, number_of_rooms) + if number_of_rooms > 5 + number_of_rooms = 5 + end if list_of_available_rooms(date_range).length < number_of_rooms raise NoRoomAvailableError.new("No room is available on given dates") end @@ -82,7 +85,7 @@ def create_block(date_range, number_of_rooms) new_block = Block.new(date_range, block_rooms) @all_blocks << new_block - return new_block #an array of rooms as integers + return new_block #array of rooms as integers end #end of method def make_reservation_from_block(block) diff --git a/specs/hotel_spec.rb b/specs/hotel_spec.rb index 85ca83e0c..9b1d46cbd 100644 --- a/specs/hotel_spec.rb +++ b/specs/hotel_spec.rb @@ -7,6 +7,7 @@ @check_out = Date.new(2017,9,17) @date_range = BookingSystem::DateRange.new(@check_in, @check_out) end + describe "#initialize" do it "@rooms should be kind of array" do @new_hotel.rooms.must_be_kind_of Array @@ -105,55 +106,60 @@ describe "#create_block" do before do - @hotel = BookingSystem::Hotel.new(5) - @new_block = @hotel.create_block(@date_range, 3) + @hotel = BookingSystem::Hotel.new(7) + @new_block = @hotel.create_block(@date_range, 2) + @another_block = @hotel.create_block(@date_range, 7) + end + it "Should set number_of_rooms to 5 if given integer grater than 5" do + @another_block.rooms.length.must_equal 5 end it "New block should be an instance of Block class " do @new_block.must_be_kind_of BookingSystem::Block end it "Created block should include given number of rooms" do - @new_block.rooms.length.must_equal 3 + @new_block.rooms.length.must_equal 2 end it "Raise an error if no rooms available for given date range" do proc { @hotel.create_block(@date_range, 4) }.must_raise BookingSystem::NoRoomAvailableError end + end + describe "#make_reservation_from_block" do - before do - @hotel = BookingSystem::Hotel.new(5) - @new_block = @hotel.create_block(@date_range, 3) - @reservation_from_block = @hotel.make_reservation_from_block(@new_block) - end - it "Returns an instance of class Reservation" do - @reservation_from_block.must_be_kind_of BookingSystem::Reservation - end - it "Booked room should be removed from rooms in block" do - @new_block.rooms.length.must_equal 2 - end - it "Raise an error if no available rooms for reservation from block" do - 2.times do - @hotel.make_reservation_from_block(@new_block) - end - proc { @hotel.make_reservation_from_block(@new_block) }.must_raise BookingSystem::NoRoomAvailableError + before do + @hotel = BookingSystem::Hotel.new(5) + @new_block = @hotel.create_block(@date_range, 3) + @reservation_from_block = @hotel.make_reservation_from_block(@new_block) + end + it "Returns an instance of class Reservation" do + @reservation_from_block.must_be_kind_of BookingSystem::Reservation + end + it "Booked room should be removed from rooms in block" do + @new_block.rooms.length.must_equal 2 + end + it "Raise an error if no available rooms for reservation from block" do + 2.times do + @hotel.make_reservation_from_block(@new_block) end + proc { @hotel.make_reservation_from_block(@new_block) }.must_raise BookingSystem::NoRoomAvailableError end + end - describe "Check total cost for two types of reservations" do - before do - @hotel = BookingSystem::Hotel.new(5) - @new_block = @hotel.create_block(@date_range, 3) - @general_reservation = @hotel.make_reservation(@date_range) - @reservation_from_block = @hotel.make_reservation_from_block(@new_block) - end - it "Returns the right total cost for general reservation" do - @general_reservation.total_cost.must_equal 400 - end - it "Returns the right total cost for reservation from block" do - @reservation_from_block.total_cost.must_equal 360 - end + describe "Check total cost for two types of reservations" do + before do + @hotel = BookingSystem::Hotel.new(5) + @new_block = @hotel.create_block(@date_range, 3) + @general_reservation = @hotel.make_reservation(@date_range) + @reservation_from_block = @hotel.make_reservation_from_block(@new_block) end + it "Returns the right total cost for general reservation" do + @general_reservation.total_cost.must_equal 400 + end + it "Returns the right total cost for reservation from block" do + @reservation_from_block.total_cost.must_equal 360 + end + end - end #end of describe From 22bad22bbffb3defa52095671446a88f6c62e7cc Mon Sep 17 00:00:00 2001 From: Iuliia Chikulaeva Date: Thu, 7 Sep 2017 13:35:56 -0700 Subject: [PATCH 31/44] Added new check for dates in the past in daterange class and new test --- lib/date_range.rb | 2 +- specs/date_range_spec.rb | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/date_range.rb b/lib/date_range.rb index 8443a5247..afe68e835 100644 --- a/lib/date_range.rb +++ b/lib/date_range.rb @@ -10,7 +10,7 @@ def initialize(check_in, check_out) @check_in = check_in @check_out = check_out - if @check_in.class != Date || @check_out.class != Date || @check_in > @check_out + if @check_in.class != Date || @check_out.class != Date || @check_in > @check_out || @check_in < Date.today raise ArgumentError.new("Invalid date or date range") end end #end of initialize diff --git a/specs/date_range_spec.rb b/specs/date_range_spec.rb index 72f98c3be..59b5005bc 100644 --- a/specs/date_range_spec.rb +++ b/specs/date_range_spec.rb @@ -5,6 +5,7 @@ @check_in = Date.new(2017,9,15) @check_out = Date.new(2017,9,17) @new_date_range = BookingSystem::DateRange.new(@check_in, @check_out) + @past_date = Date.new(2015,5,3) end describe "#initialize" do @@ -18,6 +19,9 @@ it "Raise an error if given check_out date is before check_in date" do proc { BookingSystem::DateRange.new(@check_out, @check_in) }.must_raise ArgumentError end + it "Raise an error if given date is in the past" do + proc { BookingSystem::DateRange.new(@past_date, @check_out) }.must_raise ArgumentError + end end describe "dates_within_range" do From 4d28a71be2cc8cc486a13f46e212a6373fb8b215 Mon Sep 17 00:00:00 2001 From: Iuliia Chikulaeva Date: Thu, 7 Sep 2017 20:19:07 -0700 Subject: [PATCH 32/44] Moved price constants from reservation and block classes to hotel class --- lib/block.rb | 3 +-- lib/hotel.rb | 5 ++++- lib/reservation.rb | 7 ++++--- specs/block_spec.rb | 2 +- specs/reservation_spec.rb | 4 +++- 5 files changed, 13 insertions(+), 8 deletions(-) diff --git a/lib/block.rb b/lib/block.rb index 1ceb33eeb..0282102de 100644 --- a/lib/block.rb +++ b/lib/block.rb @@ -1,7 +1,6 @@ module BookingSystem class Block - DISCOUNT = 0.9 @@id_count = 1 attr_reader :date_range, :rooms, :id, :room_cost @@ -11,7 +10,7 @@ def initialize(date_range, rooms) @@id_count += 1 @date_range = date_range @rooms = rooms - @room_cost = (Reservation::COST * DISCOUNT).to_i + @room_cost = (Hotel::COST * (1 - Hotel::BLOCK_DISCOUNT / 100.0)).round(2) end #end of initialize diff --git a/lib/hotel.rb b/lib/hotel.rb index 0cd818418..1228e5259 100644 --- a/lib/hotel.rb +++ b/lib/hotel.rb @@ -6,6 +6,9 @@ module BookingSystem class Hotel + COST = 200 + BLOCK_DISCOUNT = 10 + attr_reader :rooms, :all_reservations def initialize(number_of_rooms) @@ -57,7 +60,7 @@ def find_room(date_range) return list_of_available_rooms(date_range)[0] #room number end #end of method - def make_reservation(date_range) + def make_reservation(date_range) #, stratgey = :cheapest room = find_room(date_range) if room == nil raise NoRoomAvailableError.new("No room is available on given dates") diff --git a/lib/reservation.rb b/lib/reservation.rb index fda7cad6b..d11ecb05a 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -4,17 +4,18 @@ module BookingSystem class Reservation @@id_count = 1 - COST = 200 + + # price = {1 => 150, 2 => 200, 3 => 350, 4 => 120, 5 => 150, 6 => 170, 7 => 180, 8 => 150, 9 => 190, 10 => 200, 11 => 220, 12 => 135, 13 => 200, 14 => 190, 15 => 220, 16 => 200, 17 => 220, 18 => 250, 19 => 200, 20 => 250} attr_reader :id, :date_range, :room, :total_cost - def initialize(date_range, room, cost=COST) + def initialize(date_range, room, cost=Hotel::COST) @id = @@id_count @@id_count += 1 @date_range = date_range @room = room @cost = cost - @total_cost = cost * date_range.dates_within_range.length + @total_cost = (cost * date_range.dates_within_range.length).to_f end #end of initialize diff --git a/specs/block_spec.rb b/specs/block_spec.rb index 23a570a76..2c3a7c1ce 100644 --- a/specs/block_spec.rb +++ b/specs/block_spec.rb @@ -16,7 +16,7 @@ @new_block.date_range.must_equal @date_range end it "Total cost should be an integer" do - @new_block.room_cost.must_be_kind_of Integer + @new_block.room_cost.must_be_kind_of Float end it "Returns the rigth total cost" do @new_block.room_cost.must_equal 180 diff --git a/specs/reservation_spec.rb b/specs/reservation_spec.rb index 5accec522..5d1a7c178 100644 --- a/specs/reservation_spec.rb +++ b/specs/reservation_spec.rb @@ -17,9 +17,11 @@ @new_reservation.date_range.must_equal @date_range end it "Total cost should be an integer" do - @new_reservation.total_cost.must_be_kind_of Integer + # @new_reservation.total_cost.must_be_kind_of Integer + @new_reservation.total_cost.must_be_kind_of Float end it "Returns the rigth total cost" do + # @new_reservation.total_cost.must_equal 400 @new_reservation.total_cost.must_equal 400 end end From 61b8116b6ff8600dc331cc9639c8b11d5d5158f0 Mon Sep 17 00:00:00 2001 From: Iuliia Chikulaeva Date: Thu, 7 Sep 2017 21:32:22 -0700 Subject: [PATCH 33/44] Moved cost and discount into initialized method in hotel class and refuctored some tests to pass last changes --- lib/block.rb | 5 +++-- lib/hotel.rb | 17 ++++++++++------- lib/reservation.rb | 5 +++-- specs/block_spec.rb | 6 ------ specs/reservation_spec.rb | 3 ++- 5 files changed, 18 insertions(+), 18 deletions(-) diff --git a/lib/block.rb b/lib/block.rb index 0282102de..fb95b9f38 100644 --- a/lib/block.rb +++ b/lib/block.rb @@ -3,14 +3,15 @@ class Block @@id_count = 1 - attr_reader :date_range, :rooms, :id, :room_cost + attr_reader :date_range, :rooms, :id#, :room_cost def initialize(date_range, rooms) @id = @@id_count @@id_count += 1 @date_range = date_range @rooms = rooms - @room_cost = (Hotel::COST * (1 - Hotel::BLOCK_DISCOUNT / 100.0)).round(2) + # @room_cost = (Hotel::COST * (1 - Hotel::BLOCK_DISCOUNT / 100.0)).round(2) + # @room_cost = room_cost end #end of initialize diff --git a/lib/hotel.rb b/lib/hotel.rb index 1228e5259..bfe781230 100644 --- a/lib/hotel.rb +++ b/lib/hotel.rb @@ -6,15 +6,17 @@ module BookingSystem class Hotel - COST = 200 - BLOCK_DISCOUNT = 10 + # COST = 200 + # BLOCK_DISCOUNT = 10 - attr_reader :rooms, :all_reservations + attr_reader :rooms, :all_reservations, :cost, :block_discount - def initialize(number_of_rooms) + def initialize(number_of_rooms, cost=200, block_discount=10) @rooms = (1 .. number_of_rooms).to_a @all_reservations = [] #array of instances of class Reservation @all_blocks = [] #array of instances of class Block + @cost = cost + @block_discount = block_discount end #end of initialize def room_unavailable(room) @@ -60,12 +62,13 @@ def find_room(date_range) return list_of_available_rooms(date_range)[0] #room number end #end of method - def make_reservation(date_range) #, stratgey = :cheapest + def make_reservation(date_range) # strategy = :cheapest room = find_room(date_range) if room == nil raise NoRoomAvailableError.new("No room is available on given dates") end - new_reservation = Reservation.new(date_range, room) + cost = @cost + new_reservation = Reservation.new(date_range, room, cost) @all_reservations << new_reservation return new_reservation #new instance of class Reservation @@ -97,7 +100,7 @@ def make_reservation_from_block(block) end date_range = block.date_range room = block.rooms[0] - cost = block.room_cost + cost = @cost * (1 - @block_discount / 100.0) puts cost new_reservation_from_block = Reservation.new(date_range, room, cost) block.rooms.delete_at(0) diff --git a/lib/reservation.rb b/lib/reservation.rb index d11ecb05a..255c54948 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -7,15 +7,16 @@ class Reservation # price = {1 => 150, 2 => 200, 3 => 350, 4 => 120, 5 => 150, 6 => 170, 7 => 180, 8 => 150, 9 => 190, 10 => 200, 11 => 220, 12 => 135, 13 => 200, 14 => 190, 15 => 220, 16 => 200, 17 => 220, 18 => 250, 19 => 200, 20 => 250} - attr_reader :id, :date_range, :room, :total_cost + attr_reader :id, :date_range, :room, :cost, :total_cost - def initialize(date_range, room, cost=Hotel::COST) + def initialize(date_range, room, cost) @id = @@id_count @@id_count += 1 @date_range = date_range @room = room @cost = cost @total_cost = (cost * date_range.dates_within_range.length).to_f + # @total__block_cost = @total_cost * end #end of initialize diff --git a/specs/block_spec.rb b/specs/block_spec.rb index 2c3a7c1ce..ee4e67b4b 100644 --- a/specs/block_spec.rb +++ b/specs/block_spec.rb @@ -15,12 +15,6 @@ it "Date_range can be called by .date_range" do @new_block.date_range.must_equal @date_range end - it "Total cost should be an integer" do - @new_block.room_cost.must_be_kind_of Float - end - it "Returns the rigth total cost" do - @new_block.room_cost.must_equal 180 - end end describe "#has_available_rooms?" do diff --git a/specs/reservation_spec.rb b/specs/reservation_spec.rb index 5d1a7c178..0bc5519cf 100644 --- a/specs/reservation_spec.rb +++ b/specs/reservation_spec.rb @@ -5,8 +5,9 @@ @check_in = Date.new(2017,9,15) @check_out = Date.new(2017,9,17) @room = 5 + @cost = 200 @date_range = BookingSystem::DateRange.new(@check_in, @check_out) - @new_reservation = BookingSystem::Reservation.new(@date_range, @room) + @new_reservation = BookingSystem::Reservation.new(@date_range, @room, @cost) end describe "#initialize" do From 3f98f11036e8613f2a98fab894b2e7a47baf9c5b Mon Sep 17 00:00:00 2001 From: Iuliia Chikulaeva Date: Thu, 7 Sep 2017 21:35:37 -0700 Subject: [PATCH 34/44] Wafe 3 changed with 100% coverage --- lib/hotel.rb | 6 +++--- lib/reservation.rb | 6 ------ 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/lib/hotel.rb b/lib/hotel.rb index bfe781230..ff56d7729 100644 --- a/lib/hotel.rb +++ b/lib/hotel.rb @@ -6,9 +6,6 @@ module BookingSystem class Hotel - # COST = 200 - # BLOCK_DISCOUNT = 10 - attr_reader :rooms, :all_reservations, :cost, :block_discount def initialize(number_of_rooms, cost=200, block_discount=10) @@ -17,6 +14,9 @@ def initialize(number_of_rooms, cost=200, block_discount=10) @all_blocks = [] #array of instances of class Block @cost = cost @block_discount = block_discount + + # cost = {1 => 150, 2 => 200, 3 => 350, 4 => 120, 5 => 150, 6 => 170, 7 => 180, 8 => 150, 9 => 190, 10 => 200, 11 => 220, 12 => 135, 13 => 200, 14 => 190, 15 => 220, 16 => 200, 17 => 220, 18 => 250, 19 => 200, 20 => 250} + end #end of initialize def room_unavailable(room) diff --git a/lib/reservation.rb b/lib/reservation.rb index 255c54948..c5af3a419 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -5,8 +5,6 @@ class Reservation @@id_count = 1 - # price = {1 => 150, 2 => 200, 3 => 350, 4 => 120, 5 => 150, 6 => 170, 7 => 180, 8 => 150, 9 => 190, 10 => 200, 11 => 220, 12 => 135, 13 => 200, 14 => 190, 15 => 220, 16 => 200, 17 => 220, 18 => 250, 19 => 200, 20 => 250} - attr_reader :id, :date_range, :room, :cost, :total_cost def initialize(date_range, room, cost) @@ -16,13 +14,9 @@ def initialize(date_range, room, cost) @room = room @cost = cost @total_cost = (cost * date_range.dates_within_range.length).to_f - # @total__block_cost = @total_cost * end #end of initialize - - - end #end of class end #end of module From f4ea4889f3157c794ca126eebf25b4ebd4852207 Mon Sep 17 00:00:00 2001 From: Iuliia Chikulaeva Date: Thu, 7 Sep 2017 21:58:32 -0700 Subject: [PATCH 35/44] Changed cost from fixed rate to individual pricing for each room and changed some tests to pass them --- lib/hotel.rb | 12 ++++++------ specs/block_spec.rb | 3 ++- specs/hotel_spec.rb | 17 +++++++++-------- 3 files changed, 17 insertions(+), 15 deletions(-) diff --git a/lib/hotel.rb b/lib/hotel.rb index ff56d7729..9cfbb1018 100644 --- a/lib/hotel.rb +++ b/lib/hotel.rb @@ -8,15 +8,14 @@ class Hotel attr_reader :rooms, :all_reservations, :cost, :block_discount - def initialize(number_of_rooms, cost=200, block_discount=10) + def initialize(number_of_rooms, cost, block_discount=10) @rooms = (1 .. number_of_rooms).to_a @all_reservations = [] #array of instances of class Reservation @all_blocks = [] #array of instances of class Block + cost = {1 => 150, 2 => 200, 3 => 350, 4 => 120, 5 => 150, 6 => 170, 7 => 180, 8 => 150, 9 => 190, 10 => 200, 11 => 220, 12 => 135, 13 => 200, 14 => 190, 15 => 220, 16 => 200, 17 => 220, 18 => 250, 19 => 200, 20 => 250} @cost = cost @block_discount = block_discount - # cost = {1 => 150, 2 => 200, 3 => 350, 4 => 120, 5 => 150, 6 => 170, 7 => 180, 8 => 150, 9 => 190, 10 => 200, 11 => 220, 12 => 135, 13 => 200, 14 => 190, 15 => 220, 16 => 200, 17 => 220, 18 => 250, 19 => 200, 20 => 250} - end #end of initialize def room_unavailable(room) @@ -67,7 +66,7 @@ def make_reservation(date_range) # strategy = :cheapest if room == nil raise NoRoomAvailableError.new("No room is available on given dates") end - cost = @cost + cost = @cost[room] new_reservation = Reservation.new(date_range, room, cost) @all_reservations << new_reservation @@ -100,8 +99,9 @@ def make_reservation_from_block(block) end date_range = block.date_range room = block.rooms[0] - cost = @cost * (1 - @block_discount / 100.0) - puts cost + puts "room: #{room}" + cost = @cost[room] * (1 - @block_discount / 100.0) + puts "cost for room #{room} is #{cost}" new_reservation_from_block = Reservation.new(date_range, room, cost) block.rooms.delete_at(0) return new_reservation_from_block #instance of class Block diff --git a/specs/block_spec.rb b/specs/block_spec.rb index ee4e67b4b..6a5e09772 100644 --- a/specs/block_spec.rb +++ b/specs/block_spec.rb @@ -7,6 +7,7 @@ @check_out = Date.new(2017,9,17) @date_range = BookingSystem::DateRange.new(@check_in, @check_out) @new_block = BookingSystem::Block.new(@date_range, @rooms) + @cost = {1 => 150, 2 => 200, 3 => 350, 4 => 120, 5 => 150} end describe "#initialize" do it "ID should be an integer" do @@ -19,7 +20,7 @@ describe "#has_available_rooms?" do before do - @hotel = BookingSystem::Hotel.new(5) + @hotel = BookingSystem::Hotel.new(5, @cost) @new_block = @hotel.create_block(@date_range, 3) end it "Returns true if there are available rooms in given block" do diff --git a/specs/hotel_spec.rb b/specs/hotel_spec.rb index 9b1d46cbd..4f7f01836 100644 --- a/specs/hotel_spec.rb +++ b/specs/hotel_spec.rb @@ -2,10 +2,11 @@ describe "Hotel" do before do - @new_hotel = BookingSystem::Hotel.new(2) + @new_hotel = BookingSystem::Hotel.new(2, 200) @check_in = Date.new(2017,9,15) @check_out = Date.new(2017,9,17) @date_range = BookingSystem::DateRange.new(@check_in, @check_out) + @cost = {1 => 150, 2 => 200, 3 => 350, 4 => 120, 5 => 150} end describe "#initialize" do @@ -106,7 +107,7 @@ describe "#create_block" do before do - @hotel = BookingSystem::Hotel.new(7) + @hotel = BookingSystem::Hotel.new(7, @cost) @new_block = @hotel.create_block(@date_range, 2) @another_block = @hotel.create_block(@date_range, 7) end @@ -127,7 +128,7 @@ describe "#make_reservation_from_block" do before do - @hotel = BookingSystem::Hotel.new(5) + @hotel = BookingSystem::Hotel.new(5, @cost) @new_block = @hotel.create_block(@date_range, 3) @reservation_from_block = @hotel.make_reservation_from_block(@new_block) end @@ -147,16 +148,16 @@ describe "Check total cost for two types of reservations" do before do - @hotel = BookingSystem::Hotel.new(5) - @new_block = @hotel.create_block(@date_range, 3) + @hotel = BookingSystem::Hotel.new(5, @cost) + @block = @hotel.create_block(@date_range, 3) @general_reservation = @hotel.make_reservation(@date_range) - @reservation_from_block = @hotel.make_reservation_from_block(@new_block) + @reservation_from_block = @hotel.make_reservation_from_block(@block) end it "Returns the right total cost for general reservation" do - @general_reservation.total_cost.must_equal 400 + @general_reservation.total_cost.must_equal @cost[4] * @date_range.dates_within_range.length end it "Returns the right total cost for reservation from block" do - @reservation_from_block.total_cost.must_equal 360 + @reservation_from_block.total_cost.must_equal @cost[1] * @date_range.dates_within_range.length * 0.9 end end From 68205897a185f23cf811abaac427e1205c8ad9af Mon Sep 17 00:00:00 2001 From: Iuliia Chikulaeva Date: Thu, 7 Sep 2017 22:05:12 -0700 Subject: [PATCH 36/44] Added new file invalid_date for custom error in date range class --- lib/date_range.rb | 3 ++- lib/invalid_date.rb | 6 ++++++ specs/date_range_spec.rb | 6 +++--- 3 files changed, 11 insertions(+), 4 deletions(-) create mode 100644 lib/invalid_date.rb diff --git a/lib/date_range.rb b/lib/date_range.rb index afe68e835..afe9d5fe7 100644 --- a/lib/date_range.rb +++ b/lib/date_range.rb @@ -1,4 +1,5 @@ require 'date' +require_relative 'invalid_date' module BookingSystem @@ -11,7 +12,7 @@ def initialize(check_in, check_out) @check_out = check_out if @check_in.class != Date || @check_out.class != Date || @check_in > @check_out || @check_in < Date.today - raise ArgumentError.new("Invalid date or date range") + raise InvalidDate.new("Invalid date or date range") end end #end of initialize diff --git a/lib/invalid_date.rb b/lib/invalid_date.rb new file mode 100644 index 000000000..0ef7283fa --- /dev/null +++ b/lib/invalid_date.rb @@ -0,0 +1,6 @@ +module BookingSystem + + class InvalidDate < ArgumentError + end #end of class + +end #end of module diff --git a/specs/date_range_spec.rb b/specs/date_range_spec.rb index 59b5005bc..dc46d38ce 100644 --- a/specs/date_range_spec.rb +++ b/specs/date_range_spec.rb @@ -14,13 +14,13 @@ @new_date_range.check_out.must_be_kind_of Date end it "Raise an error if check_in or check_out values are not types of Date class" do - proc { BookingSystem::DateRange.new("2017-9-15", "2017-9-17") }.must_raise ArgumentError + proc { BookingSystem::DateRange.new("2017-9-15", "2017-9-17") }.must_raise BookingSystem::InvalidDate end it "Raise an error if given check_out date is before check_in date" do - proc { BookingSystem::DateRange.new(@check_out, @check_in) }.must_raise ArgumentError + proc { BookingSystem::DateRange.new(@check_out, @check_in) }.must_raise BookingSystem::InvalidDate end it "Raise an error if given date is in the past" do - proc { BookingSystem::DateRange.new(@past_date, @check_out) }.must_raise ArgumentError + proc { BookingSystem::DateRange.new(@past_date, @check_out) }.must_raise BookingSystem::InvalidDate end end From 521417eeae4eceda8e7a69c54e11c5578f5e6e1c Mon Sep 17 00:00:00 2001 From: Iuliia Chikulaeva Date: Fri, 8 Sep 2017 12:47:36 -0700 Subject: [PATCH 37/44] Changed the way of organizing rooms variable from array to hash, refuctored all the tests and passed them all --- lib/block.rb | 6 ++--- lib/hotel.rb | 66 ++++++++++++++++++++++++++------------------- lib/reservation.rb | 1 + specs/block_spec.rb | 5 ++-- specs/hotel_spec.rb | 50 +++++++++++++++++++++++----------- 5 files changed, 79 insertions(+), 49 deletions(-) diff --git a/lib/block.rb b/lib/block.rb index fb95b9f38..e4bac6f50 100644 --- a/lib/block.rb +++ b/lib/block.rb @@ -3,15 +3,13 @@ class Block @@id_count = 1 - attr_reader :date_range, :rooms, :id#, :room_cost + attr_reader :date_range, :rooms, :id def initialize(date_range, rooms) @id = @@id_count @@id_count += 1 @date_range = date_range - @rooms = rooms - # @room_cost = (Hotel::COST * (1 - Hotel::BLOCK_DISCOUNT / 100.0)).round(2) - # @room_cost = room_cost + @rooms = rooms #hash end #end of initialize diff --git a/lib/hotel.rb b/lib/hotel.rb index 9cfbb1018..ca1032ee5 100644 --- a/lib/hotel.rb +++ b/lib/hotel.rb @@ -6,14 +6,16 @@ module BookingSystem class Hotel - attr_reader :rooms, :all_reservations, :cost, :block_discount + DEFAULT_ROOMS = {1 => 150, 2 => 200, 3 => 350, 4 => 120, 5 => 150, 6 => 170, 7 => 180, 8 => 150, 9 => 190, 10 => 200, 11 => 220, 12 => 135, 13 => 200, 14 => 190, 15 => 220, 16 => 200, 17 => 220, 18 => 250, 19 => 200, 20 => 250} - def initialize(number_of_rooms, cost, block_discount=10) - @rooms = (1 .. number_of_rooms).to_a + DEFAULT_DISCOUNT = 10 + + attr_reader :rooms, :all_reservations, :block_discount, :all_blocks + + def initialize(rooms=DEFAULT_ROOMS, block_discount=DEFAULT_DISCOUNT) @all_reservations = [] #array of instances of class Reservation @all_blocks = [] #array of instances of class Block - cost = {1 => 150, 2 => 200, 3 => 350, 4 => 120, 5 => 150, 6 => 170, 7 => 180, 8 => 150, 9 => 190, 10 => 200, 11 => 220, 12 => 135, 13 => 200, 14 => 190, 15 => 220, 16 => 200, 17 => 220, 18 => 250, 19 => 200, 20 => 250} - @cost = cost + @rooms = rooms @block_discount = block_discount end #end of initialize @@ -27,8 +29,9 @@ def room_unavailable(room) end end end + puts dates @all_blocks.each do |block| - block.rooms.each do |block_room| + block.rooms.each do |block_room, price| if block_room == room block.date_range.dates_within_range.each do |date| dates << date @@ -36,37 +39,43 @@ def room_unavailable(room) end end end - return dates.uniq #array of dates on which this room is unavailable + puts dates + return dates #array of dates on which this room is unavailable end #end of method def list_of_available_rooms(date_range) - available_rooms = [] - @rooms.each_with_index do |room, i| - booked_dates = room_unavailable(i + 1) #array of dates - + available_rooms = {} + @rooms.each do |room, price| + booked_dates = room_unavailable(room) #array of dates + puts "booked dates: #{booked_dates} for room: #{room}" count = 0 date_range.dates_within_range.each do |date| if !booked_dates.include?(date) count += 1 + puts "#{count} after each new date" end end + puts "final count: #{count}" + puts "number of nights: #{date_range.dates_within_range.length}" if count == date_range.dates_within_range.length - available_rooms << room + available_rooms[room] = price end end - return available_rooms #array of rooms as integers + puts available_rooms + return available_rooms #hash of rooms end #end of method def find_room(date_range) - return list_of_available_rooms(date_range)[0] #room number + if list_of_available_rooms(date_range).empty? + raise NoRoomAvailableError.new("No room is available on given dates") + end + return list_of_available_rooms(date_range).first[0] #room number end #end of method - def make_reservation(date_range) # strategy = :cheapest + #picks the first available room + def make_reservation(date_range) room = find_room(date_range) - if room == nil - raise NoRoomAvailableError.new("No room is available on given dates") - end - cost = @cost[room] + cost = @rooms[room] new_reservation = Reservation.new(date_range, room, cost) @all_reservations << new_reservation @@ -84,13 +93,13 @@ def create_block(date_range, number_of_rooms) number_of_rooms = 5 end if list_of_available_rooms(date_range).length < number_of_rooms - raise NoRoomAvailableError.new("No room is available on given dates") + raise NoRoomAvailableError.new("Not enough available rooms on given dates") end - block_rooms = list_of_available_rooms(date_range)[0..number_of_rooms-1] + block_rooms = Hash[list_of_available_rooms(date_range).sort_by { |k, v| k }[0..number_of_rooms-1]] new_block = Block.new(date_range, block_rooms) @all_blocks << new_block - return new_block #array of rooms as integers + return new_block #has hash of rooms and prices end #end of method def make_reservation_from_block(block) @@ -98,16 +107,19 @@ def make_reservation_from_block(block) raise NoRoomAvailableError.new("No room is available on given dates") end date_range = block.date_range - room = block.rooms[0] - puts "room: #{room}" - cost = @cost[room] * (1 - @block_discount / 100.0) - puts "cost for room #{room} is #{cost}" + room = block.rooms.first[0] + cost = @rooms[room] * (1 - @block_discount / 100.0) new_reservation_from_block = Reservation.new(date_range, room, cost) - block.rooms.delete_at(0) + block.rooms.delete(room) return new_reservation_from_block #instance of class Block end #end of method + # def reset_hotel + # @all_reservations = [] + # @all_blocks = [] + # end #end of method + end #end of class diff --git a/lib/reservation.rb b/lib/reservation.rb index c5af3a419..b57c48a20 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -8,6 +8,7 @@ class Reservation attr_reader :id, :date_range, :room, :cost, :total_cost def initialize(date_range, room, cost) + @id = @@id_count @@id_count += 1 @date_range = date_range diff --git a/specs/block_spec.rb b/specs/block_spec.rb index 6a5e09772..957934966 100644 --- a/specs/block_spec.rb +++ b/specs/block_spec.rb @@ -2,12 +2,11 @@ describe "Block" do before do - @rooms = [1, 2, 3] + @rooms = {1 => 150, 2 => 200, 3 => 350, 4 => 120, 5 => 150} @check_in = Date.new(2017,9,15) @check_out = Date.new(2017,9,17) @date_range = BookingSystem::DateRange.new(@check_in, @check_out) @new_block = BookingSystem::Block.new(@date_range, @rooms) - @cost = {1 => 150, 2 => 200, 3 => 350, 4 => 120, 5 => 150} end describe "#initialize" do it "ID should be an integer" do @@ -20,7 +19,7 @@ describe "#has_available_rooms?" do before do - @hotel = BookingSystem::Hotel.new(5, @cost) + @hotel = BookingSystem::Hotel.new(@rooms) @new_block = @hotel.create_block(@date_range, 3) end it "Returns true if there are available rooms in given block" do diff --git a/specs/hotel_spec.rb b/specs/hotel_spec.rb index 4f7f01836..3ca055415 100644 --- a/specs/hotel_spec.rb +++ b/specs/hotel_spec.rb @@ -2,22 +2,29 @@ describe "Hotel" do before do - @new_hotel = BookingSystem::Hotel.new(2, 200) + @rooms = {1 => 150, 2 => 200} + @new_rooms = {1 => 150, 2 => 200, 3 => 350, 4 => 120, 5 => 170} + @seven_rooms = {1 => 150, 2 => 200, 3 => 350, 4 => 120, 5 => 150, 6 => 170, 7 => 180} + @new_hotel = BookingSystem::Hotel.new(@rooms) @check_in = Date.new(2017,9,15) @check_out = Date.new(2017,9,17) @date_range = BookingSystem::DateRange.new(@check_in, @check_out) - @cost = {1 => 150, 2 => 200, 3 => 350, 4 => 120, 5 => 150} + @hotel = BookingSystem::Hotel.new(@new_rooms) end + # after do + # @new_hotel.reset_hotel + # end + describe "#initialize" do - it "@rooms should be kind of array" do - @new_hotel.rooms.must_be_kind_of Array + it "@rooms should be kind of hash" do + @new_hotel.rooms.must_be_kind_of Hash end it "Should return an array of the rigth length" do @new_hotel.rooms.length.must_equal 2 end it "Each room should be kind of integer" do - @new_hotel.rooms[1].must_be_kind_of Integer + @new_hotel.rooms.keys[0].must_be_kind_of Integer end it "All_reservations must be kind of array" do @new_hotel.all_reservations.must_be_kind_of Array @@ -25,6 +32,12 @@ it "All_reservations must be empty if no reservations were made" do @new_hotel.all_reservations.length.must_equal 0 end + it "All_blocks must be kind of array" do + @new_hotel.all_blocks.must_be_kind_of Array + end + it "All_blocks must be empty if no blocks were created" do + @new_hotel.all_blocks.length.must_equal 0 + end end describe "#room_unavailable" do @@ -38,6 +51,10 @@ new_reservation = @new_hotel.make_reservation(@date_range) @new_hotel.room_unavailable(1).length.must_equal @date_range.dates_within_range.length end + it "Returns an array of dates if one block was created" do + new_block = @hotel.create_block(@date_range, 3) + @hotel.room_unavailable(1).must_equal @date_range.dates_within_range + end end describe "#find_room" do @@ -47,6 +64,10 @@ it "Should return 1 when all rooms are available" do @new_hotel.find_room(@date_range).must_equal 1 end + it "Should return 2 when 1st rooms is booked" do + new_reservation = @new_hotel.make_reservation(@date_range) + @new_hotel.find_room(@date_range).must_equal 2 + end end describe "#make_reservation" do @@ -74,7 +95,7 @@ @new_hotel.room_unavailable(1).length.must_equal 4 @new_hotel.room_unavailable(2).length.must_equal 2 end - it "Raise an error if no room is available withing given dates" do + it "Raise an error if no room is available for given date range" do reservation = @new_hotel.make_reservation(@date_range) second_reservation = @new_hotel.make_reservation(@date_range) proc { @new_hotel.make_reservation(@date_range) }.must_raise BookingSystem::NoRoomAvailableError @@ -96,18 +117,18 @@ end describe "#list_of_available_rooms" do - it "Returns an instanse of array" do - @new_hotel.list_of_available_rooms(@date_range).must_be_kind_of Array + it "Returns an instanse of hash" do + @new_hotel.list_of_available_rooms(@date_range).must_be_kind_of Hash end it "Returns the 2nd room if the 1st one is booked for hotel with 2 rooms" do reservation = @new_hotel.make_reservation(@date_range) - @new_hotel.list_of_available_rooms(@date_range)[0].must_equal 2 + @new_hotel.list_of_available_rooms(@date_range).first[0].must_equal 2 end end describe "#create_block" do before do - @hotel = BookingSystem::Hotel.new(7, @cost) + @hotel = BookingSystem::Hotel.new(@seven_rooms) @new_block = @hotel.create_block(@date_range, 2) @another_block = @hotel.create_block(@date_range, 7) end @@ -123,12 +144,11 @@ it "Raise an error if no rooms available for given date range" do proc { @hotel.create_block(@date_range, 4) }.must_raise BookingSystem::NoRoomAvailableError end - end describe "#make_reservation_from_block" do before do - @hotel = BookingSystem::Hotel.new(5, @cost) + @hotel = BookingSystem::Hotel.new(@new_rooms) @new_block = @hotel.create_block(@date_range, 3) @reservation_from_block = @hotel.make_reservation_from_block(@new_block) end @@ -148,16 +168,16 @@ describe "Check total cost for two types of reservations" do before do - @hotel = BookingSystem::Hotel.new(5, @cost) + @hotel = BookingSystem::Hotel.new(@new_rooms) @block = @hotel.create_block(@date_range, 3) @general_reservation = @hotel.make_reservation(@date_range) @reservation_from_block = @hotel.make_reservation_from_block(@block) end it "Returns the right total cost for general reservation" do - @general_reservation.total_cost.must_equal @cost[4] * @date_range.dates_within_range.length + @general_reservation.total_cost.must_equal @new_rooms[4] * @date_range.dates_within_range.length end it "Returns the right total cost for reservation from block" do - @reservation_from_block.total_cost.must_equal @cost[1] * @date_range.dates_within_range.length * 0.9 + @reservation_from_block.total_cost.must_equal @new_rooms[1] * @date_range.dates_within_range.length * 0.9 end end From 478ef7f7c12581b24db92902ccd4a85b3cc1ac36 Mon Sep 17 00:00:00 2001 From: Iuliia Chikulaeva Date: Fri, 8 Sep 2017 13:21:29 -0700 Subject: [PATCH 38/44] Changed the way room is getting picked for general reservation --- lib/hotel.rb | 22 ++------ specs/hotel_spec.rb | 131 ++++++++++++++++++-------------------------- 2 files changed, 59 insertions(+), 94 deletions(-) diff --git a/lib/hotel.rb b/lib/hotel.rb index ca1032ee5..bd7d65ec1 100644 --- a/lib/hotel.rb +++ b/lib/hotel.rb @@ -29,7 +29,6 @@ def room_unavailable(room) end end end - puts dates @all_blocks.each do |block| block.rooms.each do |block_room, price| if block_room == room @@ -39,7 +38,6 @@ def room_unavailable(room) end end end - puts dates return dates #array of dates on which this room is unavailable end #end of method @@ -47,34 +45,26 @@ def list_of_available_rooms(date_range) available_rooms = {} @rooms.each do |room, price| booked_dates = room_unavailable(room) #array of dates - puts "booked dates: #{booked_dates} for room: #{room}" count = 0 date_range.dates_within_range.each do |date| if !booked_dates.include?(date) count += 1 - puts "#{count} after each new date" end end - puts "final count: #{count}" - puts "number of nights: #{date_range.dates_within_range.length}" if count == date_range.dates_within_range.length available_rooms[room] = price end end - puts available_rooms - return available_rooms #hash of rooms - end #end of method - - def find_room(date_range) - if list_of_available_rooms(date_range).empty? + if available_rooms.length == 0 raise NoRoomAvailableError.new("No room is available on given dates") end - return list_of_available_rooms(date_range).first[0] #room number + return available_rooms #hash of rooms end #end of method - #picks the first available room - def make_reservation(date_range) - room = find_room(date_range) + def make_reservation(date_range, room) + if !list_of_available_rooms(date_range).include?(room) + raise ArgumentError.new + end cost = @rooms[room] new_reservation = Reservation.new(date_range, room, cost) @all_reservations << new_reservation diff --git a/specs/hotel_spec.rb b/specs/hotel_spec.rb index 3ca055415..418ab2157 100644 --- a/specs/hotel_spec.rb +++ b/specs/hotel_spec.rb @@ -2,135 +2,110 @@ describe "Hotel" do before do - @rooms = {1 => 150, 2 => 200} - @new_rooms = {1 => 150, 2 => 200, 3 => 350, 4 => 120, 5 => 170} + @two_rooms = {1 => 150, 2 => 200} + @five_rooms = {1 => 150, 2 => 200, 3 => 350, 4 => 120, 5 => 170} @seven_rooms = {1 => 150, 2 => 200, 3 => 350, 4 => 120, 5 => 150, 6 => 170, 7 => 180} - @new_hotel = BookingSystem::Hotel.new(@rooms) + @two_rooms_hotel = BookingSystem::Hotel.new(@two_rooms) @check_in = Date.new(2017,9,15) @check_out = Date.new(2017,9,17) @date_range = BookingSystem::DateRange.new(@check_in, @check_out) - @hotel = BookingSystem::Hotel.new(@new_rooms) + @five_rooms_hotel = BookingSystem::Hotel.new(@five_rooms) end # after do - # @new_hotel.reset_hotel + # @two_rooms_hotel.reset_hotel # end describe "#initialize" do it "@rooms should be kind of hash" do - @new_hotel.rooms.must_be_kind_of Hash + @two_rooms_hotel.rooms.must_be_kind_of Hash end it "Should return an array of the rigth length" do - @new_hotel.rooms.length.must_equal 2 + @two_rooms_hotel.rooms.length.must_equal 2 end it "Each room should be kind of integer" do - @new_hotel.rooms.keys[0].must_be_kind_of Integer + @two_rooms_hotel.rooms.keys[0].must_be_kind_of Integer end it "All_reservations must be kind of array" do - @new_hotel.all_reservations.must_be_kind_of Array + @two_rooms_hotel.all_reservations.must_be_kind_of Array end it "All_reservations must be empty if no reservations were made" do - @new_hotel.all_reservations.length.must_equal 0 + @two_rooms_hotel.all_reservations.length.must_equal 0 end it "All_blocks must be kind of array" do - @new_hotel.all_blocks.must_be_kind_of Array + @two_rooms_hotel.all_blocks.must_be_kind_of Array end it "All_blocks must be empty if no blocks were created" do - @new_hotel.all_blocks.length.must_equal 0 + @two_rooms_hotel.all_blocks.length.must_equal 0 end end describe "#room_unavailable" do it "Returns an array of dates for given room number" do - @new_hotel.room_unavailable(1).must_be_kind_of Array + @two_rooms_hotel.room_unavailable(1).must_be_kind_of Array end it "Returns empty array if no reservations were made for given room number" do - @new_hotel.room_unavailable(1).length.must_equal 0 + @two_rooms_hotel.room_unavailable(1).length.must_equal 0 end it "Returns an array of one element if one reservation was made for given room number" do - new_reservation = @new_hotel.make_reservation(@date_range) - @new_hotel.room_unavailable(1).length.must_equal @date_range.dates_within_range.length + new_reservation = @two_rooms_hotel.make_reservation(@date_range, 1) + @two_rooms_hotel.room_unavailable(1).length.must_equal @date_range.dates_within_range.length end it "Returns an array of dates if one block was created" do - new_block = @hotel.create_block(@date_range, 3) - @hotel.room_unavailable(1).must_equal @date_range.dates_within_range - end - end - - describe "#find_room" do - it "Returns an integer" do - @new_hotel.find_room(@date_range).must_be_kind_of Integer - end - it "Should return 1 when all rooms are available" do - @new_hotel.find_room(@date_range).must_equal 1 - end - it "Should return 2 when 1st rooms is booked" do - new_reservation = @new_hotel.make_reservation(@date_range) - @new_hotel.find_room(@date_range).must_equal 2 + new_block = @five_rooms_hotel.create_block(@date_range, 3) + @five_rooms_hotel.room_unavailable(1).must_equal @date_range.dates_within_range end end describe "#make_reservation" do it "Returns an instance of class reservation" do - @new_hotel.make_reservation(@date_range).must_be_kind_of BookingSystem::Reservation + @two_rooms_hotel.make_reservation(@date_range, 1).must_be_kind_of BookingSystem::Reservation end it "Adds new reservation to all_reservations array" do - first_reservation = @new_hotel.make_reservation(@date_range) - @new_hotel.all_reservations.length.must_equal 1 - end - it "Pick the next available room out of all rooms" do - second_check_in = Date.new(2017,9,17) - second_check_out = Date.new(2017,9,19) - third_check_in = Date.new(2017,9,15) - third_check_out = Date.new(2017,9,17) - second_date_range = BookingSystem::DateRange.new(second_check_in, second_check_out) - third_date_range = BookingSystem::DateRange.new(third_check_in, third_check_out) - first_reservation = @new_hotel.make_reservation(@date_range) - @new_hotel.all_reservations[0].room.must_equal 1 - @new_hotel.room_unavailable(1).length.must_equal 2 - second_reservation = @new_hotel.make_reservation(second_date_range) - @new_hotel.room_unavailable(1).length.must_equal 4 - @new_hotel.room_unavailable(2).length.must_equal 0 - third_reservation = @new_hotel.make_reservation(third_date_range) - @new_hotel.room_unavailable(1).length.must_equal 4 - @new_hotel.room_unavailable(2).length.must_equal 2 + first_reservation = @two_rooms_hotel.make_reservation(@date_range, 1) + @two_rooms_hotel.all_reservations.length.must_equal 1 end - it "Raise an error if no room is available for given date range" do - reservation = @new_hotel.make_reservation(@date_range) - second_reservation = @new_hotel.make_reservation(@date_range) - proc { @new_hotel.make_reservation(@date_range) }.must_raise BookingSystem::NoRoomAvailableError + + it "Raise an error if asked to reserve a room that is not available" do + reservation = @two_rooms_hotel.make_reservation(@date_range, 1) + proc { @two_rooms_hotel.make_reservation(@date_range, 1) }.must_raise ArgumentError end end describe "#list_of_reservations" do before do - @reservation = @new_hotel.make_reservation(@date_range) - @second_reservation = @new_hotel.make_reservation(@date_range) + @reservation = @two_rooms_hotel.make_reservation(@date_range, 1) + @second_reservation = @two_rooms_hotel.make_reservation(@date_range, 2) @date = Date.new(2017,9,15) end it "Returns an instance of array class " do - @new_hotel.list_of_reservations(@date).must_be_kind_of Array + @two_rooms_hotel.list_of_reservations(@date).must_be_kind_of Array end it "Returns an array of the right length" do - @new_hotel.list_of_reservations(@date).length.must_equal 2 + @two_rooms_hotel.list_of_reservations(@date).length.must_equal 2 end end describe "#list_of_available_rooms" do it "Returns an instanse of hash" do - @new_hotel.list_of_available_rooms(@date_range).must_be_kind_of Hash + @two_rooms_hotel.list_of_available_rooms(@date_range).must_be_kind_of Hash end - it "Returns the 2nd room if the 1st one is booked for hotel with 2 rooms" do - reservation = @new_hotel.make_reservation(@date_range) - @new_hotel.list_of_available_rooms(@date_range).first[0].must_equal 2 + it "Returns the right list of available rooms if 1st room was booked" do + reservation = @two_rooms_hotel.make_reservation(@date_range, 1) + @two_rooms_hotel.list_of_available_rooms(@date_range).must_equal ({2 => 200}) + end + it "Raise an error if no room is available for given date range" do + reservation = @two_rooms_hotel.make_reservation(@date_range, 1) + second_reservation = @two_rooms_hotel.make_reservation(@date_range, 2) + proc { @two_rooms_hotel.list_of_available_rooms(@date_range) }.must_raise BookingSystem::NoRoomAvailableError end end describe "#create_block" do before do - @hotel = BookingSystem::Hotel.new(@seven_rooms) - @new_block = @hotel.create_block(@date_range, 2) - @another_block = @hotel.create_block(@date_range, 7) + @five_rooms_hotel = BookingSystem::Hotel.new(@seven_rooms) + @new_block = @five_rooms_hotel.create_block(@date_range, 2) + @another_block = @five_rooms_hotel.create_block(@date_range, 7) end it "Should set number_of_rooms to 5 if given integer grater than 5" do @another_block.rooms.length.must_equal 5 @@ -142,15 +117,15 @@ @new_block.rooms.length.must_equal 2 end it "Raise an error if no rooms available for given date range" do - proc { @hotel.create_block(@date_range, 4) }.must_raise BookingSystem::NoRoomAvailableError + proc { @five_rooms_hotel.create_block(@date_range, 4) }.must_raise BookingSystem::NoRoomAvailableError end end describe "#make_reservation_from_block" do before do - @hotel = BookingSystem::Hotel.new(@new_rooms) - @new_block = @hotel.create_block(@date_range, 3) - @reservation_from_block = @hotel.make_reservation_from_block(@new_block) + @five_rooms_hotel = BookingSystem::Hotel.new(@five_rooms) + @new_block = @five_rooms_hotel.create_block(@date_range, 3) + @reservation_from_block = @five_rooms_hotel.make_reservation_from_block(@new_block) end it "Returns an instance of class Reservation" do @reservation_from_block.must_be_kind_of BookingSystem::Reservation @@ -160,24 +135,24 @@ end it "Raise an error if no available rooms for reservation from block" do 2.times do - @hotel.make_reservation_from_block(@new_block) + @five_rooms_hotel.make_reservation_from_block(@new_block) end - proc { @hotel.make_reservation_from_block(@new_block) }.must_raise BookingSystem::NoRoomAvailableError + proc { @five_rooms_hotel.make_reservation_from_block(@new_block) }.must_raise BookingSystem::NoRoomAvailableError end end describe "Check total cost for two types of reservations" do before do - @hotel = BookingSystem::Hotel.new(@new_rooms) - @block = @hotel.create_block(@date_range, 3) - @general_reservation = @hotel.make_reservation(@date_range) - @reservation_from_block = @hotel.make_reservation_from_block(@block) + @five_rooms_hotel = BookingSystem::Hotel.new(@five_rooms) + @block = @five_rooms_hotel.create_block(@date_range, 3) + @general_reservation = @five_rooms_hotel.make_reservation(@date_range, 4) + @reservation_from_block = @five_rooms_hotel.make_reservation_from_block(@block) end it "Returns the right total cost for general reservation" do - @general_reservation.total_cost.must_equal @new_rooms[4] * @date_range.dates_within_range.length + @general_reservation.total_cost.must_equal @five_rooms[4] * @date_range.dates_within_range.length end it "Returns the right total cost for reservation from block" do - @reservation_from_block.total_cost.must_equal @new_rooms[1] * @date_range.dates_within_range.length * 0.9 + @reservation_from_block.total_cost.must_equal @five_rooms[1] * @date_range.dates_within_range.length * 0.9 end end From 63a2ad9a55aeb2ec062fdd5641a12eb91b8806a8 Mon Sep 17 00:00:00 2001 From: Iuliia Chikulaeva Date: Fri, 8 Sep 2017 14:09:26 -0700 Subject: [PATCH 39/44] Chahged the way of picking the room to reserve from block, all the tests --- lib/hotel.rb | 14 ++---- specs/block_spec.rb | 12 ++--- specs/hotel_spec.rb | 99 ++++++++++++++++++--------------------- specs/reservation_spec.rb | 2 - 4 files changed, 57 insertions(+), 70 deletions(-) diff --git a/lib/hotel.rb b/lib/hotel.rb index bd7d65ec1..fbd2d675a 100644 --- a/lib/hotel.rb +++ b/lib/hotel.rb @@ -92,12 +92,14 @@ def create_block(date_range, number_of_rooms) return new_block #has hash of rooms and prices end #end of method - def make_reservation_from_block(block) + def make_reservation_from_block(block, room) if !block.has_available_rooms? - raise NoRoomAvailableError.new("No room is available on given dates") + raise NoRoomAvailableError.new("No available rooms in block") + end + if !block.rooms.keys.include?(room) + raise NoRoomAvailableError.new("Requested room is unavailable") end date_range = block.date_range - room = block.rooms.first[0] cost = @rooms[room] * (1 - @block_discount / 100.0) new_reservation_from_block = Reservation.new(date_range, room, cost) block.rooms.delete(room) @@ -105,12 +107,6 @@ def make_reservation_from_block(block) end #end of method - # def reset_hotel - # @all_reservations = [] - # @all_blocks = [] - # end #end of method - - end #end of class end #end of module diff --git a/specs/block_spec.rb b/specs/block_spec.rb index 957934966..9fafa6dee 100644 --- a/specs/block_spec.rb +++ b/specs/block_spec.rb @@ -2,11 +2,11 @@ describe "Block" do before do - @rooms = {1 => 150, 2 => 200, 3 => 350, 4 => 120, 5 => 150} + @five_rooms = {1 => 150, 2 => 200, 3 => 350, 4 => 120, 5 => 150} @check_in = Date.new(2017,9,15) @check_out = Date.new(2017,9,17) @date_range = BookingSystem::DateRange.new(@check_in, @check_out) - @new_block = BookingSystem::Block.new(@date_range, @rooms) + @new_block = BookingSystem::Block.new(@date_range, @five_rooms) end describe "#initialize" do it "ID should be an integer" do @@ -19,15 +19,15 @@ describe "#has_available_rooms?" do before do - @hotel = BookingSystem::Hotel.new(@rooms) - @new_block = @hotel.create_block(@date_range, 3) + @five_room_hotel = BookingSystem::Hotel.new(@five_rooms) + @new_block = @five_room_hotel.create_block(@date_range, 3) end it "Returns true if there are available rooms in given block" do @new_block.has_available_rooms?.must_equal true end it "Returns false if there is no available rooms in given block" do - 3.times do - @hotel.make_reservation_from_block(@new_block) + 3.times do |i| + @five_room_hotel.make_reservation_from_block(@new_block, i+1) end @new_block.has_available_rooms?.must_equal false end diff --git a/specs/hotel_spec.rb b/specs/hotel_spec.rb index 418ab2157..ee21ac69d 100644 --- a/specs/hotel_spec.rb +++ b/specs/hotel_spec.rb @@ -4,108 +4,104 @@ before do @two_rooms = {1 => 150, 2 => 200} @five_rooms = {1 => 150, 2 => 200, 3 => 350, 4 => 120, 5 => 170} - @seven_rooms = {1 => 150, 2 => 200, 3 => 350, 4 => 120, 5 => 150, 6 => 170, 7 => 180} - @two_rooms_hotel = BookingSystem::Hotel.new(@two_rooms) + @eight_rooms = {1 => 150, 2 => 200, 3 => 350, 4 => 120, 5 => 150, 6 => 170, 7 => 180, 8 => 150} + @two_room_hotel = BookingSystem::Hotel.new(@two_rooms) @check_in = Date.new(2017,9,15) @check_out = Date.new(2017,9,17) @date_range = BookingSystem::DateRange.new(@check_in, @check_out) - @five_rooms_hotel = BookingSystem::Hotel.new(@five_rooms) + @five_room_hotel = BookingSystem::Hotel.new(@five_rooms) + @eight_room_hotel = BookingSystem::Hotel.new(@eight_rooms) end - # after do - # @two_rooms_hotel.reset_hotel - # end - describe "#initialize" do it "@rooms should be kind of hash" do - @two_rooms_hotel.rooms.must_be_kind_of Hash + @two_room_hotel.rooms.must_be_kind_of Hash end it "Should return an array of the rigth length" do - @two_rooms_hotel.rooms.length.must_equal 2 + @two_room_hotel.rooms.length.must_equal 2 end it "Each room should be kind of integer" do - @two_rooms_hotel.rooms.keys[0].must_be_kind_of Integer + @two_room_hotel.rooms.keys[0].must_be_kind_of Integer end it "All_reservations must be kind of array" do - @two_rooms_hotel.all_reservations.must_be_kind_of Array + @two_room_hotel.all_reservations.must_be_kind_of Array end it "All_reservations must be empty if no reservations were made" do - @two_rooms_hotel.all_reservations.length.must_equal 0 + @two_room_hotel.all_reservations.length.must_equal 0 end it "All_blocks must be kind of array" do - @two_rooms_hotel.all_blocks.must_be_kind_of Array + @two_room_hotel.all_blocks.must_be_kind_of Array end it "All_blocks must be empty if no blocks were created" do - @two_rooms_hotel.all_blocks.length.must_equal 0 + @two_room_hotel.all_blocks.length.must_equal 0 end end describe "#room_unavailable" do it "Returns an array of dates for given room number" do - @two_rooms_hotel.room_unavailable(1).must_be_kind_of Array + @two_room_hotel.room_unavailable(1).must_be_kind_of Array end it "Returns empty array if no reservations were made for given room number" do - @two_rooms_hotel.room_unavailable(1).length.must_equal 0 + @two_room_hotel.room_unavailable(1).length.must_equal 0 end it "Returns an array of one element if one reservation was made for given room number" do - new_reservation = @two_rooms_hotel.make_reservation(@date_range, 1) - @two_rooms_hotel.room_unavailable(1).length.must_equal @date_range.dates_within_range.length + new_reservation = @two_room_hotel.make_reservation(@date_range, 1) + @two_room_hotel.room_unavailable(1).length.must_equal @date_range.dates_within_range.length end it "Returns an array of dates if one block was created" do - new_block = @five_rooms_hotel.create_block(@date_range, 3) - @five_rooms_hotel.room_unavailable(1).must_equal @date_range.dates_within_range + new_block = @five_room_hotel.create_block(@date_range, 3) + @five_room_hotel.room_unavailable(1).must_equal @date_range.dates_within_range end end describe "#make_reservation" do it "Returns an instance of class reservation" do - @two_rooms_hotel.make_reservation(@date_range, 1).must_be_kind_of BookingSystem::Reservation + @two_room_hotel.make_reservation(@date_range, 1).must_be_kind_of BookingSystem::Reservation end it "Adds new reservation to all_reservations array" do - first_reservation = @two_rooms_hotel.make_reservation(@date_range, 1) - @two_rooms_hotel.all_reservations.length.must_equal 1 + first_reservation = @two_room_hotel.make_reservation(@date_range, 1) + @two_room_hotel.all_reservations.length.must_equal 1 end it "Raise an error if asked to reserve a room that is not available" do - reservation = @two_rooms_hotel.make_reservation(@date_range, 1) - proc { @two_rooms_hotel.make_reservation(@date_range, 1) }.must_raise ArgumentError + reservation = @two_room_hotel.make_reservation(@date_range, 1) + proc { @two_room_hotel.make_reservation(@date_range, 1) }.must_raise ArgumentError end end describe "#list_of_reservations" do before do - @reservation = @two_rooms_hotel.make_reservation(@date_range, 1) - @second_reservation = @two_rooms_hotel.make_reservation(@date_range, 2) + @reservation = @two_room_hotel.make_reservation(@date_range, 1) + @second_reservation = @two_room_hotel.make_reservation(@date_range, 2) @date = Date.new(2017,9,15) end it "Returns an instance of array class " do - @two_rooms_hotel.list_of_reservations(@date).must_be_kind_of Array + @two_room_hotel.list_of_reservations(@date).must_be_kind_of Array end it "Returns an array of the right length" do - @two_rooms_hotel.list_of_reservations(@date).length.must_equal 2 + @two_room_hotel.list_of_reservations(@date).length.must_equal 2 end end describe "#list_of_available_rooms" do it "Returns an instanse of hash" do - @two_rooms_hotel.list_of_available_rooms(@date_range).must_be_kind_of Hash + @two_room_hotel.list_of_available_rooms(@date_range).must_be_kind_of Hash end it "Returns the right list of available rooms if 1st room was booked" do - reservation = @two_rooms_hotel.make_reservation(@date_range, 1) - @two_rooms_hotel.list_of_available_rooms(@date_range).must_equal ({2 => 200}) + reservation = @two_room_hotel.make_reservation(@date_range, 1) + @two_room_hotel.list_of_available_rooms(@date_range).must_equal ({2 => 200}) end it "Raise an error if no room is available for given date range" do - reservation = @two_rooms_hotel.make_reservation(@date_range, 1) - second_reservation = @two_rooms_hotel.make_reservation(@date_range, 2) - proc { @two_rooms_hotel.list_of_available_rooms(@date_range) }.must_raise BookingSystem::NoRoomAvailableError + reservation = @two_room_hotel.make_reservation(@date_range, 1) + second_reservation = @two_room_hotel.make_reservation(@date_range, 2) + proc { @two_room_hotel.list_of_available_rooms(@date_range) }.must_raise BookingSystem::NoRoomAvailableError end end describe "#create_block" do before do - @five_rooms_hotel = BookingSystem::Hotel.new(@seven_rooms) - @new_block = @five_rooms_hotel.create_block(@date_range, 2) - @another_block = @five_rooms_hotel.create_block(@date_range, 7) + @new_block = @eight_room_hotel.create_block(@date_range, 2) + @another_block = @eight_room_hotel.create_block(@date_range, 7) end it "Should set number_of_rooms to 5 if given integer grater than 5" do @another_block.rooms.length.must_equal 5 @@ -117,15 +113,14 @@ @new_block.rooms.length.must_equal 2 end it "Raise an error if no rooms available for given date range" do - proc { @five_rooms_hotel.create_block(@date_range, 4) }.must_raise BookingSystem::NoRoomAvailableError + proc { @eight_room_hotel.create_block(@date_range, 3) }.must_raise BookingSystem::NoRoomAvailableError end end describe "#make_reservation_from_block" do before do - @five_rooms_hotel = BookingSystem::Hotel.new(@five_rooms) - @new_block = @five_rooms_hotel.create_block(@date_range, 3) - @reservation_from_block = @five_rooms_hotel.make_reservation_from_block(@new_block) + @new_block = @five_room_hotel.create_block(@date_range, 3) + @reservation_from_block = @five_room_hotel.make_reservation_from_block(@new_block, 1) end it "Returns an instance of class Reservation" do @reservation_from_block.must_be_kind_of BookingSystem::Reservation @@ -134,19 +129,21 @@ @new_block.rooms.length.must_equal 2 end it "Raise an error if no available rooms for reservation from block" do - 2.times do - @five_rooms_hotel.make_reservation_from_block(@new_block) + 2.times do |i| + @five_room_hotel.make_reservation_from_block(@new_block, i+2) end - proc { @five_rooms_hotel.make_reservation_from_block(@new_block) }.must_raise BookingSystem::NoRoomAvailableError + proc { @five_room_hotel.make_reservation_from_block(@new_block, 1) }.must_raise BookingSystem::NoRoomAvailableError + end + it "Raise an error if requested room is unavailable" do + proc { @five_room_hotel.make_reservation_from_block(@new_block, 1) }.must_raise BookingSystem::NoRoomAvailableError end end describe "Check total cost for two types of reservations" do before do - @five_rooms_hotel = BookingSystem::Hotel.new(@five_rooms) - @block = @five_rooms_hotel.create_block(@date_range, 3) - @general_reservation = @five_rooms_hotel.make_reservation(@date_range, 4) - @reservation_from_block = @five_rooms_hotel.make_reservation_from_block(@block) + @block = @five_room_hotel.create_block(@date_range, 3) + @general_reservation = @five_room_hotel.make_reservation(@date_range, 4) + @reservation_from_block = @five_room_hotel.make_reservation_from_block(@block, 1) end it "Returns the right total cost for general reservation" do @general_reservation.total_cost.must_equal @five_rooms[4] * @date_range.dates_within_range.length @@ -158,8 +155,4 @@ - - - - end #end of describe diff --git a/specs/reservation_spec.rb b/specs/reservation_spec.rb index 0bc5519cf..fafb49fc8 100644 --- a/specs/reservation_spec.rb +++ b/specs/reservation_spec.rb @@ -18,11 +18,9 @@ @new_reservation.date_range.must_equal @date_range end it "Total cost should be an integer" do - # @new_reservation.total_cost.must_be_kind_of Integer @new_reservation.total_cost.must_be_kind_of Float end it "Returns the rigth total cost" do - # @new_reservation.total_cost.must_equal 400 @new_reservation.total_cost.must_equal 400 end end From 6457100f720c2ecaf12b5c9fe7de400a75cf44d2 Mon Sep 17 00:00:00 2001 From: Iuliia Chikulaeva Date: Fri, 8 Sep 2017 14:38:07 -0700 Subject: [PATCH 40/44] Completed 1 optional with 100% coverage --- lib/hotel.rb | 2 +- specs/hotel_spec.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/hotel.rb b/lib/hotel.rb index fbd2d675a..dcbadd97c 100644 --- a/lib/hotel.rb +++ b/lib/hotel.rb @@ -63,7 +63,7 @@ def list_of_available_rooms(date_range) def make_reservation(date_range, room) if !list_of_available_rooms(date_range).include?(room) - raise ArgumentError.new + raise NoRoomAvailableError.new("Requested room is unavailable") end cost = @rooms[room] new_reservation = Reservation.new(date_range, room, cost) diff --git a/specs/hotel_spec.rb b/specs/hotel_spec.rb index ee21ac69d..0cd66048f 100644 --- a/specs/hotel_spec.rb +++ b/specs/hotel_spec.rb @@ -65,7 +65,7 @@ it "Raise an error if asked to reserve a room that is not available" do reservation = @two_room_hotel.make_reservation(@date_range, 1) - proc { @two_room_hotel.make_reservation(@date_range, 1) }.must_raise ArgumentError + proc { @two_room_hotel.make_reservation(@date_range, 1) }.must_raise BookingSystem::NoRoomAvailableError end end From 662cb25d839c2003a966bc960dd0bde0aa3d43a3 Mon Sep 17 00:00:00 2001 From: Iuliia Chikulaeva Date: Fri, 8 Sep 2017 15:59:35 -0700 Subject: [PATCH 41/44] Added new methods get_rates and add_reservation to read and write from csv file --- lib/hotel.rb | 15 ++++++++++++++- lib/reservation.rb | 7 +++++++ support/rates.csv | 20 ++++++++++++++++++++ support/reservations.csv | 13 +++++++++++++ 4 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 support/rates.csv create mode 100644 support/reservations.csv diff --git a/lib/hotel.rb b/lib/hotel.rb index dcbadd97c..7239f792b 100644 --- a/lib/hotel.rb +++ b/lib/hotel.rb @@ -2,6 +2,7 @@ require_relative 'date_range' require_relative 'no_room_available' require_relative 'block' +require 'csv' module BookingSystem class Hotel @@ -10,6 +11,8 @@ class Hotel DEFAULT_DISCOUNT = 10 + DEFAULT_RESERVATIONS = "support/reservations.csv" + attr_reader :rooms, :all_reservations, :block_discount, :all_blocks def initialize(rooms=DEFAULT_ROOMS, block_discount=DEFAULT_DISCOUNT) @@ -20,6 +23,14 @@ def initialize(rooms=DEFAULT_ROOMS, block_discount=DEFAULT_DISCOUNT) end #end of initialize + def get_rates(file) + rates = {} + CSV.open(file, "r").each do |line| + rates[line[0]] = line[1] + end + return rates + end #end of method + def room_unavailable(room) dates = [] @all_reservations.each do |reservation| @@ -61,7 +72,7 @@ def list_of_available_rooms(date_range) return available_rooms #hash of rooms end #end of method - def make_reservation(date_range, room) + def make_reservation(date_range, room, file=DEFAULT_RESERVATIONS) if !list_of_available_rooms(date_range).include?(room) raise NoRoomAvailableError.new("Requested room is unavailable") end @@ -69,6 +80,8 @@ def make_reservation(date_range, room) new_reservation = Reservation.new(date_range, room, cost) @all_reservations << new_reservation + new_reservation.add_reservation(file) + return new_reservation #new instance of class Reservation end #end of method diff --git a/lib/reservation.rb b/lib/reservation.rb index b57c48a20..5cab86c1a 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -1,4 +1,5 @@ require_relative 'date_range' +require 'csv' module BookingSystem class Reservation @@ -18,6 +19,12 @@ def initialize(date_range, room, cost) end #end of initialize + def add_reservation(file) + CSV.open(file, "a") do |line| + line << ["#{self.id}","#{self.room}","#{self.date_range.check_in.strftime("%m/%d/%Y")}","#{self.date_range.check_out.strftime("%m/%d/%Y")}","#{self.total_cost}"] + end + end #end of method + end #end of class end #end of module diff --git a/support/rates.csv b/support/rates.csv new file mode 100644 index 000000000..5a376c5a1 --- /dev/null +++ b/support/rates.csv @@ -0,0 +1,20 @@ +1,130 +2,345 +3,150 +4,180 +5,190 +6,200 +7,210 +8,135 +9,180 +10,90 +11,95 +12,100 +13,150 +14,160 +15,170 +16,210 +17,139 +18,130 +19,300 +20,238 diff --git a/support/reservations.csv b/support/reservations.csv new file mode 100644 index 000000000..5c1e01a48 --- /dev/null +++ b/support/reservations.csv @@ -0,0 +1,13 @@ +1,1,09/15/2017,09/17/2017,300.0 +2,2,09/15/2017,09/17/2017,400.0 +3,1,09/15/2017,09/17/2017,300.0 +4,4,09/15/2017,09/17/2017,240.0 +6,4,09/15/2017,09/17/2017,240.0 +14,1,09/15/2017,09/17/2017,300.0 +15,2,09/15/2017,09/17/2017,400.0 +16,1,09/15/2017,09/17/2017,300.0 +17,2,09/15/2017,09/17/2017,400.0 +22,1,09/15/2017,09/17/2017,300.0 +23,1,09/15/2017,09/17/2017,300.0 +24,1,09/15/2017,09/17/2017,300.0 +28,1,09/15/2017,09/17/2017,300.0 From 3574c02ee1f591c65c02cc1fe89149793ea082cc Mon Sep 17 00:00:00 2001 From: Iuliia Chikulaeva Date: Sun, 10 Sep 2017 21:35:31 -0700 Subject: [PATCH 42/44] Added comments and minor styling changes --- lib/hotel.rb | 28 ++++++++++++++++------------ lib/reservation.rb | 1 + support/reservations.csv | 26 ++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 12 deletions(-) diff --git a/lib/hotel.rb b/lib/hotel.rb index 7239f792b..5ddb51a56 100644 --- a/lib/hotel.rb +++ b/lib/hotel.rb @@ -13,9 +13,21 @@ class Hotel DEFAULT_RESERVATIONS = "support/reservations.csv" + DEFAULT_RATES = "support/rates.csv" + attr_reader :rooms, :all_reservations, :block_discount, :all_blocks + #didn't finish this part, method get_rates is unused + def get_rates(file=DEFAULT_RATES) + rates = {} + CSV.open(file, "r").each do |line| + rates[line[0]] = line[1] + end + return rates + end #end of method + def initialize(rooms=DEFAULT_ROOMS, block_discount=DEFAULT_DISCOUNT) + @all_reservations = [] #array of instances of class Reservation @all_blocks = [] #array of instances of class Block @rooms = rooms @@ -23,14 +35,6 @@ def initialize(rooms=DEFAULT_ROOMS, block_discount=DEFAULT_DISCOUNT) end #end of initialize - def get_rates(file) - rates = {} - CSV.open(file, "r").each do |line| - rates[line[0]] = line[1] - end - return rates - end #end of method - def room_unavailable(room) dates = [] @all_reservations.each do |reservation| @@ -50,7 +54,7 @@ def room_unavailable(room) end end return dates #array of dates on which this room is unavailable - end #end of method + end #end of method def list_of_available_rooms(date_range) available_rooms = {} @@ -98,9 +102,9 @@ def create_block(date_range, number_of_rooms) if list_of_available_rooms(date_range).length < number_of_rooms raise NoRoomAvailableError.new("Not enough available rooms on given dates") end - block_rooms = Hash[list_of_available_rooms(date_range).sort_by { |k, v| k }[0..number_of_rooms-1]] - new_block = Block.new(date_range, block_rooms) - @all_blocks << new_block + block_rooms = Hash[list_of_available_rooms(date_range).sort_by { |k, v| k }[0..number_of_rooms-1]] + new_block = Block.new(date_range, block_rooms) + @all_blocks << new_block return new_block #has hash of rooms and prices end #end of method diff --git a/lib/reservation.rb b/lib/reservation.rb index 5cab86c1a..9b853c4a7 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -19,6 +19,7 @@ def initialize(date_range, room, cost) end #end of initialize + #method seems to be working fine, but don't have any tests for loading data back def add_reservation(file) CSV.open(file, "a") do |line| line << ["#{self.id}","#{self.room}","#{self.date_range.check_in.strftime("%m/%d/%Y")}","#{self.date_range.check_out.strftime("%m/%d/%Y")}","#{self.total_cost}"] diff --git a/support/reservations.csv b/support/reservations.csv index 5c1e01a48..83d8087f6 100644 --- a/support/reservations.csv +++ b/support/reservations.csv @@ -11,3 +11,29 @@ 23,1,09/15/2017,09/17/2017,300.0 24,1,09/15/2017,09/17/2017,300.0 28,1,09/15/2017,09/17/2017,300.0 +7,1,09/15/2017,09/17/2017,300.0 +8,1,09/15/2017,09/17/2017,300.0 +9,1,09/15/2017,09/17/2017,300.0 +14,1,09/15/2017,09/17/2017,300.0 +15,1,09/15/2017,09/17/2017,300.0 +16,2,09/15/2017,09/17/2017,400.0 +20,4,09/15/2017,09/17/2017,240.0 +22,4,09/15/2017,09/17/2017,240.0 +24,1,09/15/2017,09/17/2017,300.0 +25,1,09/15/2017,09/17/2017,300.0 +26,2,09/15/2017,09/17/2017,400.0 +27,1,09/15/2017,09/17/2017,300.0 +28,2,09/15/2017,09/17/2017,400.0 +1,4,09/15/2017,09/17/2017,240.0 +3,4,09/15/2017,09/17/2017,240.0 +5,1,09/15/2017,09/17/2017,300.0 +6,2,09/15/2017,09/17/2017,400.0 +7,1,09/15/2017,09/17/2017,300.0 +18,1,09/15/2017,09/17/2017,300.0 +19,2,09/15/2017,09/17/2017,400.0 +20,1,09/15/2017,09/17/2017,300.0 +21,2,09/15/2017,09/17/2017,400.0 +22,1,09/15/2017,09/17/2017,300.0 +23,1,09/15/2017,09/17/2017,300.0 +24,1,09/15/2017,09/17/2017,300.0 +28,1,09/15/2017,09/17/2017,300.0 From 1ac37c0fd4259c8edf77f3f8e6004029600b3725 Mon Sep 17 00:00:00 2001 From: Iuliia Chikulaeva Date: Sun, 1 Oct 2017 14:33:17 -0700 Subject: [PATCH 43/44] Added new method reserve_room for Block class and refuctored some tests --- design-activity.md | 36 ++++++++ lib/block.rb | 12 +++ lib/hotel.rb | 8 +- lib/invalid_date.rb | 2 - specs/block_spec.rb | 23 +++++- specs/date_range_spec.rb | 10 +-- specs/hotel_spec.rb | 19 +---- specs/reservation_spec.rb | 4 +- support/reservations.csv | 169 ++++++++++++++++++++++++++++++++++++++ 9 files changed, 250 insertions(+), 33 deletions(-) create mode 100644 design-activity.md diff --git a/design-activity.md b/design-activity.md new file mode 100644 index 000000000..17da185c4 --- /dev/null +++ b/design-activity.md @@ -0,0 +1,36 @@ +## What classes does each implementation include? Are the lists the same? +Implementations A and B include same three classes: CartEntry, ShoppingCart, Order. + +## Write down a sentence to describe each class. +Class CartEntry stores the information about product and its price. It is responsible for the price for each particular product. +Class ShoppingCart contains all products (several cart entries). +Class Order creates new instance of class ShoppingCart and is responsible for the final price of the whole order include taxes. + +## How do the classes relate to each other? It might be helpful to draw a diagram on a whiteboard or piece of paper. +Class Order is depends on the ShoppingCart class which is closely related to CartEntry class. Every time customer pick a new product(cart entry), it is getting stored to the instance of class ShoppingCar which is initialized in class Order. + +## What data does each class store? How (if at all) does this differ between the two implementations? +Class CartEntry stores price for product(unit_price) and quantity. +Class ShoppingCart stores all products that customer picked. +Class Order stores the total price for the order. +The difference between two implementations is that in implementation A all calculation logic is in Order class whereas in implementation B it is located in each class (CartEntry, ShoppingCart) where it belongs. + +##What methods does each class have? How (if at all) does this differ between the two implementations? +Class CartEntry has methods: initialize(A), and initialize and price(B). The difference between the two implementations is that in implementation B unit_price and quantity can't be accessed from the outside of class and instead it has a price method. +Class ShoppingCart has a similar difference between two implementations. In implementation A it has only one method initialize and can be accessed from outside of class. In implementation B private data is encapsulated. +Class Order in both implementations has two methods: initialize and total_price. The only difference is how it accesses data from two other classes. In implementation A, it uses data from CartEntry and ShoppingCart classes and calculates total_price, whereas in implementation B, it uses methods of these classes. + +##Consider the Order#total_price method. In each implementation: is logic to compute the price delegated to "lower level" classes like ShoppingCart and CartEntry, or is it retained in Order? +In implementation A #total_price method is completely retained in Order. In implementation B it delegated to "lower level" classes by having #price methods in them. + +##Does total_price directly manipulate the instance variables of other classes? +In implementation A #total_price method directly manipulate the instance variables of "lower level" classes such as entries, unit_price and quantity. + +##If we decide items are cheaper if bought in bulk, how would this change the code? Which implementation is easier to modify? +The implementation B is easier to modify because each class has a single responsibility. To add a discount for items in bulk I would add it to #price method of CartEntry class. + +##Which implementation better adheres to the single responsibility principle? +The implementation B is better adheres to the single responsibility principle. Each class has a certain logic calculations and can be describe in one sentence. + +##Bonus question once you've read Metz ch. 3: Which implementation is more loosely coupled? +The implementation B is more loosely coupled. The private data of classes is encapsulated and classes do not know much about each other. diff --git a/lib/block.rb b/lib/block.rb index e4bac6f50..3e4f45338 100644 --- a/lib/block.rb +++ b/lib/block.rb @@ -17,6 +17,18 @@ def has_available_rooms? return @rooms.length > 0 end #end of method + def reserve_room(room) + if !has_available_rooms? + raise NoRoomAvailableError.new("No available rooms in block") + end + if !rooms.keys.include?(room) + raise NoRoomAvailableError.new("Requested room is unavailable") + end + + @rooms.delete(room) + + end #end of method + end #end of class end #end of module diff --git a/lib/hotel.rb b/lib/hotel.rb index 5ddb51a56..c2cfb44f7 100644 --- a/lib/hotel.rb +++ b/lib/hotel.rb @@ -110,16 +110,10 @@ def create_block(date_range, number_of_rooms) end #end of method def make_reservation_from_block(block, room) - if !block.has_available_rooms? - raise NoRoomAvailableError.new("No available rooms in block") - end - if !block.rooms.keys.include?(room) - raise NoRoomAvailableError.new("Requested room is unavailable") - end date_range = block.date_range + block.reserve_room(room) cost = @rooms[room] * (1 - @block_discount / 100.0) new_reservation_from_block = Reservation.new(date_range, room, cost) - block.rooms.delete(room) return new_reservation_from_block #instance of class Block end #end of method diff --git a/lib/invalid_date.rb b/lib/invalid_date.rb index 0ef7283fa..0a5fd6299 100644 --- a/lib/invalid_date.rb +++ b/lib/invalid_date.rb @@ -1,6 +1,4 @@ module BookingSystem - class InvalidDate < ArgumentError end #end of class - end #end of module diff --git a/specs/block_spec.rb b/specs/block_spec.rb index 9fafa6dee..ed0781d5a 100644 --- a/specs/block_spec.rb +++ b/specs/block_spec.rb @@ -3,8 +3,8 @@ describe "Block" do before do @five_rooms = {1 => 150, 2 => 200, 3 => 350, 4 => 120, 5 => 150} - @check_in = Date.new(2017,9,15) - @check_out = Date.new(2017,9,17) + @check_in = Date.new(2017,10,15) + @check_out = Date.new(2017,10,17) @date_range = BookingSystem::DateRange.new(@check_in, @check_out) @new_block = BookingSystem::Block.new(@date_range, @five_rooms) end @@ -31,6 +31,25 @@ end @new_block.has_available_rooms?.must_equal false end + + describe "#reserve_room" do + before do + @block = @five_room_hotel.create_block(@date_range, 2) + @block.reserve_room(4) + end + it "Booked room should be removed from rooms in block" do + @block.rooms.length.must_equal 1 + end + it "Raise an error if requested room is unavailable" do + proc { @block.reserve_room(4) }.must_raise BookingSystem::NoRoomAvailableError + end + it "Raise an error if no available rooms for reservation from block" do + @block.reserve_room(5) + proc { @block.reserve_room(4) }.must_raise BookingSystem::NoRoomAvailableError + end + + + end end end #end of describe diff --git a/specs/date_range_spec.rb b/specs/date_range_spec.rb index dc46d38ce..6c20350c2 100644 --- a/specs/date_range_spec.rb +++ b/specs/date_range_spec.rb @@ -2,8 +2,8 @@ describe "DateRange" do before do - @check_in = Date.new(2017,9,15) - @check_out = Date.new(2017,9,17) + @check_in = Date.new(2017,10,15) + @check_out = Date.new(2017,10,17) @new_date_range = BookingSystem::DateRange.new(@check_in, @check_out) @past_date = Date.new(2015,5,3) end @@ -32,9 +32,9 @@ @new_date_range.dates_within_range.length.must_equal 2 end it "Returns the array of right length for long ranges" do - check_in = Date.new(2017,9,15) - check_out = Date.new(2017,10,15) - BookingSystem::DateRange.new(check_in, check_out).dates_within_range.length.must_equal 30 + check_in = Date.new(2017,10,15) + check_out = Date.new(2017,11,15) + BookingSystem::DateRange.new(check_in, check_out).dates_within_range.length.must_equal 31 end end diff --git a/specs/hotel_spec.rb b/specs/hotel_spec.rb index 0cd66048f..8dac8ff74 100644 --- a/specs/hotel_spec.rb +++ b/specs/hotel_spec.rb @@ -6,8 +6,8 @@ @five_rooms = {1 => 150, 2 => 200, 3 => 350, 4 => 120, 5 => 170} @eight_rooms = {1 => 150, 2 => 200, 3 => 350, 4 => 120, 5 => 150, 6 => 170, 7 => 180, 8 => 150} @two_room_hotel = BookingSystem::Hotel.new(@two_rooms) - @check_in = Date.new(2017,9,15) - @check_out = Date.new(2017,9,17) + @check_in = Date.new(2017,10,15) + @check_out = Date.new(2017,10,17) @date_range = BookingSystem::DateRange.new(@check_in, @check_out) @five_room_hotel = BookingSystem::Hotel.new(@five_rooms) @eight_room_hotel = BookingSystem::Hotel.new(@eight_rooms) @@ -73,7 +73,7 @@ before do @reservation = @two_room_hotel.make_reservation(@date_range, 1) @second_reservation = @two_room_hotel.make_reservation(@date_range, 2) - @date = Date.new(2017,9,15) + @date = Date.new(2017,10,15) end it "Returns an instance of array class " do @two_room_hotel.list_of_reservations(@date).must_be_kind_of Array @@ -125,18 +125,7 @@ it "Returns an instance of class Reservation" do @reservation_from_block.must_be_kind_of BookingSystem::Reservation end - it "Booked room should be removed from rooms in block" do - @new_block.rooms.length.must_equal 2 - end - it "Raise an error if no available rooms for reservation from block" do - 2.times do |i| - @five_room_hotel.make_reservation_from_block(@new_block, i+2) - end - proc { @five_room_hotel.make_reservation_from_block(@new_block, 1) }.must_raise BookingSystem::NoRoomAvailableError - end - it "Raise an error if requested room is unavailable" do - proc { @five_room_hotel.make_reservation_from_block(@new_block, 1) }.must_raise BookingSystem::NoRoomAvailableError - end + end describe "Check total cost for two types of reservations" do diff --git a/specs/reservation_spec.rb b/specs/reservation_spec.rb index fafb49fc8..cc5c96a25 100644 --- a/specs/reservation_spec.rb +++ b/specs/reservation_spec.rb @@ -2,8 +2,8 @@ describe "Reservation" do before do - @check_in = Date.new(2017,9,15) - @check_out = Date.new(2017,9,17) + @check_in = Date.new(2017,10,15) + @check_out = Date.new(2017,10,17) @room = 5 @cost = 200 @date_range = BookingSystem::DateRange.new(@check_in, @check_out) diff --git a/support/reservations.csv b/support/reservations.csv index 83d8087f6..0a84f8615 100644 --- a/support/reservations.csv +++ b/support/reservations.csv @@ -37,3 +37,172 @@ 23,1,09/15/2017,09/17/2017,300.0 24,1,09/15/2017,09/17/2017,300.0 28,1,09/15/2017,09/17/2017,300.0 +1,1,10/15/2017,10/17/2017,300.0 +2,1,10/15/2017,10/17/2017,300.0 +3,2,10/15/2017,10/17/2017,400.0 +4,1,10/15/2017,10/17/2017,300.0 +5,1,10/15/2017,10/17/2017,300.0 +6,1,10/15/2017,10/17/2017,300.0 +7,1,10/15/2017,10/17/2017,300.0 +8,2,10/15/2017,10/17/2017,400.0 +9,1,10/15/2017,10/17/2017,300.0 +10,2,10/15/2017,10/17/2017,400.0 +11,4,10/15/2017,10/17/2017,240.0 +13,4,10/15/2017,10/17/2017,240.0 +24,1,10/15/2017,10/17/2017,300.0 +1,1,10/15/2017,10/17/2017,300.0 +2,1,10/15/2017,10/17/2017,300.0 +3,1,10/15/2017,10/17/2017,300.0 +4,1,10/15/2017,10/17/2017,300.0 +8,1,10/15/2017,10/17/2017,300.0 +9,1,10/15/2017,10/17/2017,300.0 +10,2,10/15/2017,10/17/2017,400.0 +15,1,10/15/2017,10/17/2017,300.0 +16,2,10/15/2017,10/17/2017,400.0 +17,1,10/15/2017,10/17/2017,300.0 +18,2,10/15/2017,10/17/2017,400.0 +19,4,10/15/2017,10/17/2017,240.0 +21,4,10/15/2017,10/17/2017,240.0 +1,1,10/15/2017,10/17/2017,300.0 +2,1,10/15/2017,10/17/2017,300.0 +3,1,10/15/2017,10/17/2017,300.0 +4,1,10/15/2017,10/17/2017,300.0 +9,1,10/15/2017,10/17/2017,300.0 +10,2,10/15/2017,10/17/2017,400.0 +11,1,10/15/2017,10/17/2017,300.0 +12,2,10/15/2017,10/17/2017,400.0 +13,4,10/15/2017,10/17/2017,240.0 +15,4,10/15/2017,10/17/2017,240.0 +17,1,10/15/2017,10/17/2017,300.0 +18,1,10/15/2017,10/17/2017,300.0 +19,2,10/15/2017,10/17/2017,400.0 +1,1,10/15/2017,10/17/2017,300.0 +2,1,10/15/2017,10/17/2017,300.0 +3,2,10/15/2017,10/17/2017,400.0 +4,1,10/15/2017,10/17/2017,300.0 +5,2,10/15/2017,10/17/2017,400.0 +6,1,10/15/2017,10/17/2017,300.0 +7,2,10/15/2017,10/17/2017,400.0 +8,4,10/15/2017,10/17/2017,240.0 +10,4,10/15/2017,10/17/2017,240.0 +12,1,10/15/2017,10/17/2017,300.0 +13,1,10/15/2017,10/17/2017,300.0 +14,1,10/15/2017,10/17/2017,300.0 +28,1,10/15/2017,10/17/2017,300.0 +1,1,10/15/2017,10/17/2017,300.0 +2,1,10/15/2017,10/17/2017,300.0 +3,2,10/15/2017,10/17/2017,400.0 +17,1,10/15/2017,10/17/2017,300.0 +18,2,10/15/2017,10/17/2017,400.0 +19,1,10/15/2017,10/17/2017,300.0 +20,2,10/15/2017,10/17/2017,400.0 +21,4,10/15/2017,10/17/2017,240.0 +23,4,10/15/2017,10/17/2017,240.0 +25,1,10/15/2017,10/17/2017,300.0 +26,1,10/15/2017,10/17/2017,300.0 +27,1,10/15/2017,10/17/2017,300.0 +28,1,10/15/2017,10/17/2017,300.0 +1,1,10/15/2017,10/17/2017,300.0 +2,2,10/15/2017,10/17/2017,400.0 +3,1,10/15/2017,10/17/2017,300.0 +4,2,10/15/2017,10/17/2017,400.0 +5,4,10/15/2017,10/17/2017,240.0 +7,4,10/15/2017,10/17/2017,240.0 +10,1,10/15/2017,10/17/2017,300.0 +11,1,10/15/2017,10/17/2017,300.0 +12,1,10/15/2017,10/17/2017,300.0 +13,1,10/15/2017,10/17/2017,300.0 +18,1,10/15/2017,10/17/2017,300.0 +19,2,10/15/2017,10/17/2017,400.0 +20,1,10/15/2017,10/17/2017,300.0 +5,1,10/15/2017,10/17/2017,300.0 +6,1,10/15/2017,10/17/2017,300.0 +7,2,10/15/2017,10/17/2017,400.0 +11,4,10/15/2017,10/17/2017,240.0 +13,4,10/15/2017,10/17/2017,240.0 +16,1,10/15/2017,10/17/2017,300.0 +17,1,10/15/2017,10/17/2017,300.0 +18,1,10/15/2017,10/17/2017,300.0 +19,1,10/15/2017,10/17/2017,300.0 +20,2,10/15/2017,10/17/2017,400.0 +21,1,10/15/2017,10/17/2017,300.0 +22,2,10/15/2017,10/17/2017,400.0 +23,1,10/15/2017,10/17/2017,300.0 +1,1,10/15/2017,10/17/2017,300.0 +2,1,10/15/2017,10/17/2017,300.0 +3,2,10/15/2017,10/17/2017,400.0 +4,1,10/15/2017,10/17/2017,300.0 +5,4,10/15/2017,10/17/2017,240.0 +7,4,10/15/2017,10/17/2017,240.0 +9,1,10/15/2017,10/17/2017,300.0 +10,1,10/15/2017,10/17/2017,300.0 +11,1,10/15/2017,10/17/2017,300.0 +12,1,10/15/2017,10/17/2017,300.0 +13,2,10/15/2017,10/17/2017,400.0 +14,1,10/15/2017,10/17/2017,300.0 +15,2,10/15/2017,10/17/2017,400.0 +1,1,10/15/2017,10/17/2017,300.0 +2,1,10/15/2017,10/17/2017,300.0 +3,1,10/15/2017,10/17/2017,300.0 +4,1,10/15/2017,10/17/2017,300.0 +9,4,10/15/2017,10/17/2017,240.0 +11,4,10/15/2017,10/17/2017,240.0 +13,1,10/15/2017,10/17/2017,300.0 +14,2,10/15/2017,10/17/2017,400.0 +15,1,10/15/2017,10/17/2017,300.0 +19,1,10/15/2017,10/17/2017,300.0 +20,2,10/15/2017,10/17/2017,400.0 +21,1,10/15/2017,10/17/2017,300.0 +22,2,10/15/2017,10/17/2017,400.0 +1,1,10/15/2017,10/17/2017,300.0 +2,2,10/15/2017,10/17/2017,400.0 +3,1,10/15/2017,10/17/2017,300.0 +4,2,10/15/2017,10/17/2017,400.0 +5,4,10/15/2017,10/17/2017,240.0 +7,4,10/15/2017,10/17/2017,240.0 +12,1,10/15/2017,10/17/2017,300.0 +13,1,10/15/2017,10/17/2017,300.0 +14,2,10/15/2017,10/17/2017,400.0 +15,1,10/15/2017,10/17/2017,300.0 +21,1,10/15/2017,10/17/2017,300.0 +22,1,10/15/2017,10/17/2017,300.0 +23,1,10/15/2017,10/17/2017,300.0 +1,1,10/15/2017,10/17/2017,300.0 +2,1,10/15/2017,10/17/2017,300.0 +3,2,10/15/2017,10/17/2017,400.0 +12,1,10/15/2017,10/17/2017,300.0 +13,4,10/15/2017,10/17/2017,240.0 +15,4,10/15/2017,10/17/2017,240.0 +17,1,10/15/2017,10/17/2017,300.0 +18,1,10/15/2017,10/17/2017,300.0 +19,1,10/15/2017,10/17/2017,300.0 +20,1,10/15/2017,10/17/2017,300.0 +21,2,10/15/2017,10/17/2017,400.0 +22,1,10/15/2017,10/17/2017,300.0 +23,2,10/15/2017,10/17/2017,400.0 +2,1,10/15/2017,10/17/2017,300.0 +7,4,10/15/2017,10/17/2017,240.0 +9,4,10/15/2017,10/17/2017,240.0 +14,1,10/15/2017,10/17/2017,300.0 +15,2,10/15/2017,10/17/2017,400.0 +16,1,10/15/2017,10/17/2017,300.0 +17,1,10/15/2017,10/17/2017,300.0 +18,2,10/15/2017,10/17/2017,400.0 +19,1,10/15/2017,10/17/2017,300.0 +20,2,10/15/2017,10/17/2017,400.0 +21,1,10/15/2017,10/17/2017,300.0 +22,1,10/15/2017,10/17/2017,300.0 +23,1,10/15/2017,10/17/2017,300.0 +1,1,10/15/2017,10/17/2017,300.0 +2,1,10/15/2017,10/17/2017,300.0 +3,1,10/15/2017,10/17/2017,300.0 +5,1,10/15/2017,10/17/2017,300.0 +6,1,10/15/2017,10/17/2017,300.0 +7,2,10/15/2017,10/17/2017,400.0 +8,1,10/15/2017,10/17/2017,300.0 +16,1,10/15/2017,10/17/2017,300.0 +17,2,10/15/2017,10/17/2017,400.0 +18,1,10/15/2017,10/17/2017,300.0 +19,2,10/15/2017,10/17/2017,400.0 +20,4,10/15/2017,10/17/2017,240.0 +22,4,10/15/2017,10/17/2017,240.0 From 8f329513fda636cb0ab1b5e8d65d250d6b6710d5 Mon Sep 17 00:00:00 2001 From: Iuliia Chikulaeva Date: Sun, 1 Oct 2017 14:45:40 -0700 Subject: [PATCH 44/44] Added and finished design-activity.md file --- design-activity.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/design-activity.md b/design-activity.md index 17da185c4..df81355ef 100644 --- a/design-activity.md +++ b/design-activity.md @@ -24,7 +24,7 @@ Class Order in both implementations has two methods: initialize and total_price. In implementation A #total_price method is completely retained in Order. In implementation B it delegated to "lower level" classes by having #price methods in them. ##Does total_price directly manipulate the instance variables of other classes? -In implementation A #total_price method directly manipulate the instance variables of "lower level" classes such as entries, unit_price and quantity. +In implementation A #total_price method directly manipulates the instance variables of "lower level" classes such as entries, unit_price and quantity. ##If we decide items are cheaper if bought in bulk, how would this change the code? Which implementation is easier to modify? The implementation B is easier to modify because each class has a single responsibility. To add a discount for items in bulk I would add it to #price method of CartEntry class. @@ -34,3 +34,7 @@ The implementation B is better adheres to the single responsibility principle. E ##Bonus question once you've read Metz ch. 3: Which implementation is more loosely coupled? The implementation B is more loosely coupled. The private data of classes is encapsulated and classes do not know much about each other. + +##My improvements: + +To improve design in Hotel project I added new method #reserve_room to class Block and changed method #make_reservation_from_block in class Hotel. It made my classes Hotel and Block more loosely coupled. In my new implementation the Hotel class knows less information about the Block class. This change did not break any of my tests but I still refactored them a little to check some new functionality.