From e30668e57932cc69789c0841fa4dae88c06b54cd Mon Sep 17 00:00:00 2001 From: Angela Wilson Date: Tue, 5 Sep 2017 20:45:51 -0700 Subject: [PATCH 01/19] Create all files for both folders lib and specs --- lib/DateRange.rb | 0 lib/Reservation.rb | 0 lib/Room.rb | 0 lib/hotel.rb | 0 specs/date_range_spec.rb | 1 + specs/hotel_spec.rb | 1 + specs/reservation_spec.rb | 1 + specs/room_spec.rb | 1 + specs/spec_helper.rb | 22 ++++++++++++++++++++++ 9 files changed, 26 insertions(+) create mode 100644 lib/DateRange.rb create mode 100644 lib/Reservation.rb create mode 100644 lib/Room.rb create mode 100644 lib/hotel.rb create mode 100644 specs/date_range_spec.rb create mode 100644 specs/hotel_spec.rb create mode 100644 specs/reservation_spec.rb create mode 100644 specs/room_spec.rb create mode 100644 specs/spec_helper.rb diff --git a/lib/DateRange.rb b/lib/DateRange.rb new file mode 100644 index 000000000..e69de29bb diff --git a/lib/Reservation.rb b/lib/Reservation.rb new file mode 100644 index 000000000..e69de29bb diff --git a/lib/Room.rb b/lib/Room.rb new file mode 100644 index 000000000..e69de29bb diff --git a/lib/hotel.rb b/lib/hotel.rb new file mode 100644 index 000000000..e69de29bb diff --git a/specs/date_range_spec.rb b/specs/date_range_spec.rb new file mode 100644 index 000000000..41e9cd29d --- /dev/null +++ b/specs/date_range_spec.rb @@ -0,0 +1 @@ +require_relative '../specs/spec_helper' diff --git a/specs/hotel_spec.rb b/specs/hotel_spec.rb new file mode 100644 index 000000000..41e9cd29d --- /dev/null +++ b/specs/hotel_spec.rb @@ -0,0 +1 @@ +require_relative '../specs/spec_helper' diff --git a/specs/reservation_spec.rb b/specs/reservation_spec.rb new file mode 100644 index 000000000..41e9cd29d --- /dev/null +++ b/specs/reservation_spec.rb @@ -0,0 +1 @@ +require_relative '../specs/spec_helper' diff --git a/specs/room_spec.rb b/specs/room_spec.rb new file mode 100644 index 000000000..41e9cd29d --- /dev/null +++ b/specs/room_spec.rb @@ -0,0 +1 @@ +require_relative '../specs/spec_helper' diff --git a/specs/spec_helper.rb b/specs/spec_helper.rb new file mode 100644 index 000000000..726e02892 --- /dev/null +++ b/specs/spec_helper.rb @@ -0,0 +1,22 @@ +require 'simplecov' +SimpleCov.start + +# specs/spec_helper.rb +require 'minitest' +require 'minitest/autorun' +require 'minitest/reporters' +require 'minitest/pride' + + + +# Require any classes +# ex require_relative 'lib/foo.rb' + +require_relative '../lib/daterange' +require_relative '../lib/hotel' +require_relative '../lib/reservation' +require_relative '../lib/room' + + + +Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new From 5e10fe18d6e62e158b5f192bbcd30d5c5306c9b9 Mon Sep 17 00:00:00 2001 From: Angela Wilson Date: Wed, 6 Sep 2017 16:25:57 -0700 Subject: [PATCH 02/19] Write implimentation and specs for Room class. Write test for parameters passed to Room instances. --- Rakefile | 13 +++++++ lib/Room.rb | 29 +++++++++++++++ specs/hotel_spec.rb | 50 ++++++++++++++++++++++++++ specs/room_spec.rb | 85 ++++++++++++++++++++++++++++++++++++++++++++ specs/spec_helper.rb | 1 + 5 files changed, 178 insertions(+) create mode 100644 Rakefile diff --git a/Rakefile b/Rakefile new file mode 100644 index 000000000..50cabef64 --- /dev/null +++ b/Rakefile @@ -0,0 +1,13 @@ +require 'rake/testtask' + +Rake::TestTask.new do |t| + t.libs = ["lib"] + t.warning = true + t.test_files = FileList['specs/*_spec.rb'] + puts "" +end + +task default: :test + + +puts diff --git a/lib/Room.rb b/lib/Room.rb index e69de29bb..ca2d4fb8c 100644 --- a/lib/Room.rb +++ b/lib/Room.rb @@ -0,0 +1,29 @@ +=begin +- Room class makes ojects of room. +- Its attributes are: +- room number + + +- Each room should have a room number. + - pass in the room number to the room object. + - to automate making all 20 rooms we need a times loop from 1-20. + - set an iterative variable to 1. + - errors for room numbers not on the sequence 1-20 + - errors for reservation status not being either + + + +=end + + +module Hotel + class Room + + attr_reader :room_num, :reservation_status + + def initialize(room_num, reservation_status) + @room_num = room_num + @reservation_status = reservation_status + end + end +end diff --git a/specs/hotel_spec.rb b/specs/hotel_spec.rb index 41e9cd29d..6f4c6f870 100644 --- a/specs/hotel_spec.rb +++ b/specs/hotel_spec.rb @@ -1 +1,51 @@ require_relative '../specs/spec_helper' + +=begin +Each room should have a room number. +- pass in the room number to the room object. +- to automate making all 20 rooms we need a times loop from 1-20. +- set an iterative variable to 1. +=end + + +it "Takes strings for both room number and reservation status" do + room_num = "1" + reservation_status = "Reserved" + + room_num.must_be_instance_of String + reservation_status.must_be_instance_of String + +end + +it "Raises an error for room number not being in range 1 -20" do + room_num = "#" + reservation_status = "Reserved" + proc { + Hotel::Room.new(room_num, reservation_status) + }.must_raise ArgumentError + + room_num = "21" + proc{ + @room.new(room_num, reservation_status) + }.must_raise ArgumentError +end + +it "Raises an error for reservation status not being either 'Reserved' or 'vacant'." do + room_num = "20" + reservation_status = "Rainbow" + + proc { + @room.new(room_num, reservation_status) + }.must_raise ArgumentError + + reservation_status = "123" + proc { + @room.new(room_num, reservation_status) + }.must_raise ArgumentError + + reservation_status = "!@#" + proc{ + @room.new(room_num, reservation_status) + }.must_raise ArgumentError + +end diff --git a/specs/room_spec.rb b/specs/room_spec.rb index 41e9cd29d..70fd97720 100644 --- a/specs/room_spec.rb +++ b/specs/room_spec.rb @@ -1 +1,86 @@ require_relative '../specs/spec_helper' + +=begin + + +- Room class makes ojects of room. +- Its attributes are: +- room number: each room must be 1-20 + +The hotel has 20 rooms, and they are numbered 1 through 20 +- Every room is identical, and a room always costs $200/night +- The last day of a reservation is the checkout day, so the guest should not be charged for that night +- For this wave, any room can be reserved at any time, and you don't need to check whether reservations conflict with each other (this will come in wave 2!) +- Your code should raise an error when an invalid date range is provided +- A reservation is allowed start on the same day that another reservation for the same room ends +- Your code should raise an exception when asked to reserve a room that is not available + + +- Each room should have a room number. +- pass in the room number to the room object. +- to automate making all 20 rooms we need a times loop from 1-20. +- set an iterative variable to 1. +- errors for room numbers not on the sequence 1-20 +- errors for reservation status not being either "reserved" or "vacant" + + +=end + +describe "Room Class" do + before do + @room = Hotel::Room + end + + describe "initialize" do + # it "Takes strings for both room number and reservation status" do + # room_num = "1" + # reservation_status = "Reserved" + # + # room_num.must_be_instance_of String + # reservation_status.must_be_instance_of String + # + # end + # + # it "Raises an error for room number not being in range 1 -20" do + # room_num = "#" + # reservation_status = "Reserved" + # proc { + # Hotel::Room.new(room_num, reservation_status) + # }.must_raise ArgumentError + # + # room_num = "21" + # proc{ + # @room.new(room_num, reservation_status) + # }.must_raise ArgumentError + # end + # + # it "Raises an error for reservation status not being either 'Reserved' or 'vacant'" do + # room_num = "20" + # reservation_status = "Rainbow" + # + # proc { + # @room.new(room_num, reservation_status) + # }.must_raise ArgumentError + # + # reservation_status = "123" + # proc { + # @room.new(room_num, reservation_status) + # }.must_raise ArgumentError + # + # reservation_status = "!@#" + # proc{ + # @room.new(room_num, reservation_status) + # }.must_raise ArgumentError + # + # end + + it "Returns an instance of Room class" do + room_num = "1" + reservation_status = "Reserved" + + @room.new(room_num, reservation_status).must_be_instance_of Hotel::Room + + end + end # end initialize + +end # end room class diff --git a/specs/spec_helper.rb b/specs/spec_helper.rb index 726e02892..ebcb4f941 100644 --- a/specs/spec_helper.rb +++ b/specs/spec_helper.rb @@ -6,6 +6,7 @@ require 'minitest/autorun' require 'minitest/reporters' require 'minitest/pride' +require 'minitest/skip_dsl' From 3688ea98b7e678b6b8e1e51ad6884584b77c6252 Mon Sep 17 00:00:00 2001 From: Angela Wilson Date: Fri, 8 Sep 2017 14:40:15 -0700 Subject: [PATCH 03/19] Remove Room class and specs because I realized it had no functionalitiy. Write initialize test for DateRange class and write DateRange implimintation code. Rename Hotel class to Admin class. Stored unused work in unused-work folder for potential future use. --- lib/Admin.rb | 1 + lib/DateRange.rb | 27 ++++++++++++++ lib/Reservation.rb | 14 ++++++++ lib/hotel.rb | 0 specs/admin_spec.rb | 15 ++++++++ specs/date_range_spec.rb | 55 +++++++++++++++++++++++++++++ specs/hotel_spec.rb | 51 -------------------------- specs/reservation_spec.rb | 11 ++++++ specs/spec_helper.rb | 4 +-- {lib => unused-work}/Room.rb | 0 unused-work/date-range-spec.rb | 17 +++++++++ {specs => unused-work}/room_spec.rb | 0 12 files changed, 142 insertions(+), 53 deletions(-) create mode 100644 lib/Admin.rb delete mode 100644 lib/hotel.rb create mode 100644 specs/admin_spec.rb delete mode 100644 specs/hotel_spec.rb rename {lib => unused-work}/Room.rb (100%) create mode 100644 unused-work/date-range-spec.rb rename {specs => unused-work}/room_spec.rb (100%) diff --git a/lib/Admin.rb b/lib/Admin.rb new file mode 100644 index 000000000..8b1378917 --- /dev/null +++ b/lib/Admin.rb @@ -0,0 +1 @@ + diff --git a/lib/DateRange.rb b/lib/DateRange.rb index e69de29bb..9c7aa07fe 100644 --- a/lib/DateRange.rb +++ b/lib/DateRange.rb @@ -0,0 +1,27 @@ + + +module Hotel + + class DateRange + + attr_reader :check_in, :check_out, :date_range_array + + def initialize(check_in, check_out) + raise ArgumentError.new("Please enter a Check Out date that comes after Check In date.") if check_in > check_out + + @check_in = check_in + @check_out = check_out + @date_range_array = (check_in...check_out).to_a + end + + + # def include?(date) + # reserevation.include?(Date.new(2017,2,2)) + # end + + + + + + end +end diff --git a/lib/Reservation.rb b/lib/Reservation.rb index e69de29bb..6c39ea5b9 100644 --- a/lib/Reservation.rb +++ b/lib/Reservation.rb @@ -0,0 +1,14 @@ +require_relative './daterange' + +module Hotel + class Reservation + + def initialize(Hotel::DateRange.new(check_in, check_out)) + @reservation_id = 0 + @total_cost = 0 + @room_num = 0 + end + + + end +end diff --git a/lib/hotel.rb b/lib/hotel.rb deleted file mode 100644 index e69de29bb..000000000 diff --git a/specs/admin_spec.rb b/specs/admin_spec.rb new file mode 100644 index 000000000..d6df4cf53 --- /dev/null +++ b/specs/admin_spec.rb @@ -0,0 +1,15 @@ +require_relative '../specs/spec_helper' + +=begin +Each room should have a room number. +- pass in the room number to the room object. +- to automate making all 20 rooms we need a times loop from 1-20. +- set an iterative variable to 1. +=end + describe "Admin Class" do + describe "initialize" do + + end # end initialize + + + end # end admin class diff --git a/specs/date_range_spec.rb b/specs/date_range_spec.rb index 41e9cd29d..e6ec17b74 100644 --- a/specs/date_range_spec.rb +++ b/specs/date_range_spec.rb @@ -1 +1,56 @@ require_relative '../specs/spec_helper' + +=begin +- the date range class will be instanciated in the Admin class. +1. initialize the method with a start date and and end date. +2. Does the range of dates have a reservation? +3. + + + +=end + +describe "DateRange class" do + before do + check_in = Date.new(2017,2,3) + check_out = Date.new(2017, 2,7) + @date_range = Hotel::DateRange.new(check_in, check_out) + end + describe "initialize" do + + it "Takes a start date object" do + check_in = Date.new(2017,2,3) + check_out = Date.new(2017, 2,7) + Hotel::DateRange.new(check_in, check_out).must_respond_to :check_in + end + + it "Takes an end date object" do + check_in = Date.new(2017,2,3) + check_out = Date.new(2017, 2,7) + Hotel::DateRange.new(check_in, check_out).must_respond_to :check_out + end + + it "Returns array of dates in range" do + @date_range.date_range_array.must_be_instance_of Array + end + + it "Raises an error for invalid date ranges" do + check_in = Date.new(2017, 2, 3) + check_out = Date.new(2015, 2, 7) + proc{Hotel::DateRange.new(check_in, check_out)}.must_raise ArgumentError + end + + it "Returns a DateRange object" do + @date_range.must_be_instance_of Hotel::DateRange + end + + end # end initialize + + describe "over lap method" do + it "text" do + + end + end + + +end # end date range class diff --git a/specs/hotel_spec.rb b/specs/hotel_spec.rb deleted file mode 100644 index 6f4c6f870..000000000 --- a/specs/hotel_spec.rb +++ /dev/null @@ -1,51 +0,0 @@ -require_relative '../specs/spec_helper' - -=begin -Each room should have a room number. -- pass in the room number to the room object. -- to automate making all 20 rooms we need a times loop from 1-20. -- set an iterative variable to 1. -=end - - -it "Takes strings for both room number and reservation status" do - room_num = "1" - reservation_status = "Reserved" - - room_num.must_be_instance_of String - reservation_status.must_be_instance_of String - -end - -it "Raises an error for room number not being in range 1 -20" do - room_num = "#" - reservation_status = "Reserved" - proc { - Hotel::Room.new(room_num, reservation_status) - }.must_raise ArgumentError - - room_num = "21" - proc{ - @room.new(room_num, reservation_status) - }.must_raise ArgumentError -end - -it "Raises an error for reservation status not being either 'Reserved' or 'vacant'." do - room_num = "20" - reservation_status = "Rainbow" - - proc { - @room.new(room_num, reservation_status) - }.must_raise ArgumentError - - reservation_status = "123" - proc { - @room.new(room_num, reservation_status) - }.must_raise ArgumentError - - reservation_status = "!@#" - proc{ - @room.new(room_num, reservation_status) - }.must_raise ArgumentError - -end diff --git a/specs/reservation_spec.rb b/specs/reservation_spec.rb index 41e9cd29d..609716931 100644 --- a/specs/reservation_spec.rb +++ b/specs/reservation_spec.rb @@ -1 +1,12 @@ require_relative '../specs/spec_helper' + + +describe "reservations" do + describe "initialize" do + it "Returns a Reservation object" do + Hotel::Reservation.new.must_be_instance_of Hotel::Reservation + end + + + end +end diff --git a/specs/spec_helper.rb b/specs/spec_helper.rb index ebcb4f941..d3f967da9 100644 --- a/specs/spec_helper.rb +++ b/specs/spec_helper.rb @@ -14,9 +14,9 @@ # ex require_relative 'lib/foo.rb' require_relative '../lib/daterange' -require_relative '../lib/hotel' +require_relative '../lib/admin' require_relative '../lib/reservation' -require_relative '../lib/room' + diff --git a/lib/Room.rb b/unused-work/Room.rb similarity index 100% rename from lib/Room.rb rename to unused-work/Room.rb diff --git a/unused-work/date-range-spec.rb b/unused-work/date-range-spec.rb new file mode 100644 index 000000000..f1397bb53 --- /dev/null +++ b/unused-work/date-range-spec.rb @@ -0,0 +1,17 @@ +it "include? method returns True" do + # Boundry cases + date = Date.new(2017,2,3) + @date_range.include?(date).must_equal true + + date = Date.new(2017,2, 6) + @date_range.include?(date).must_equal true + + # edge cases + date = Date.new(2017,2, 2) + @date_range.include?(date).must_equal false + + date = Date.new(2017,2,7) + @date_range.include?(date).must_equal false + + +end diff --git a/specs/room_spec.rb b/unused-work/room_spec.rb similarity index 100% rename from specs/room_spec.rb rename to unused-work/room_spec.rb From ec3daeb187fb89e685208d53281ac7e6593e1cf9 Mon Sep 17 00:00:00 2001 From: Angela Wilson Date: Sat, 9 Sep 2017 09:31:55 -0700 Subject: [PATCH 04/19] Reservation class: add methods reservation_id and total_cost. Made tests for reservation class' added methods. Starting to work on Admin class and tests. --- lib/Admin.rb | 18 ++++++++++++++++++ lib/DateRange.rb | 2 +- lib/Reservation.rb | 39 +++++++++++++++++++++++++++++++++------ 3 files changed, 52 insertions(+), 7 deletions(-) diff --git a/lib/Admin.rb b/lib/Admin.rb index 8b1378917..2235108f2 100644 --- a/lib/Admin.rb +++ b/lib/Admin.rb @@ -1 +1,19 @@ +module Hotel + class Admin + attr_reader :room_num + + def initialize + @reservations = Hotel::Reservation.reservation_array + @room_num = [] + 20.times do |i| + @room_num << (i + 1) + end + end + + # def + # + # end + + end +end diff --git a/lib/DateRange.rb b/lib/DateRange.rb index 9c7aa07fe..afea7326f 100644 --- a/lib/DateRange.rb +++ b/lib/DateRange.rb @@ -11,7 +11,7 @@ def initialize(check_in, check_out) @check_in = check_in @check_out = check_out - @date_range_array = (check_in...check_out).to_a + @date_range_array = (check_in..check_out).to_a end diff --git a/lib/Reservation.rb b/lib/Reservation.rb index 6c39ea5b9..8bed9b2d3 100644 --- a/lib/Reservation.rb +++ b/lib/Reservation.rb @@ -1,14 +1,41 @@ -require_relative './daterange' +require 'date' + +require_relative 'daterange' module Hotel class Reservation + COST_PER_NIGHT = 200 + + attr_reader :reservation_array, :total_cost, :room_num, :date_range_array - def initialize(Hotel::DateRange.new(check_in, check_out)) - @reservation_id = 0 + def initialize( check_in, check_out) + + @reservation_array = [] @total_cost = 0 - @room_num = 0 + @room_num = room_num + @date_range_array = DateRange.new(check_in, check_out) + end + + + def reservation_id + @reservation_array.length + 1 end - end -end + def total_cost + (@date_range_array.length - 1) * COST_PER_NIGHT + end + + + + + + end # end class +end #end module + + +check_in = Date.new(2017,2,3) +check_out = Date.new(2017, 2,7) +reservation = Hotel::Reservation.new(check_in, check_out) + +puts "#{reservation.reservation_array}" From bd0473fe70dde8e8304b5f38d9a1511646af856a Mon Sep 17 00:00:00 2001 From: Angela Wilson Date: Sat, 9 Sep 2017 21:51:01 -0700 Subject: [PATCH 05/19] Admin Class: Add methods list_of_rooms, add_reservation, list_reservation(date), and list_vacancies. Need to write tests for almost all the listed methods. --- lib/Admin.rb | 69 +++++++++++++++++++++++++++++++++++++++++----- lib/Reservation.rb | 33 ++++++++++++---------- 2 files changed, 80 insertions(+), 22 deletions(-) diff --git a/lib/Admin.rb b/lib/Admin.rb index 2235108f2..1b4bee45d 100644 --- a/lib/Admin.rb +++ b/lib/Admin.rb @@ -1,19 +1,74 @@ +require_relative 'reservation' + module Hotel class Admin - attr_reader :room_num + attr_reader :room_nums, :reservations def initialize - @reservations = Hotel::Reservation.reservation_array - @room_num = [] + + @reservations = [] + + + @room_nums = [] 20.times do |i| - @room_num << (i + 1) + @room_nums << (i + 1) end end - # def - # - # end + def list_of_rooms + return Array.new(room_nums) + end + + def add_reservation(room_selection, check_in, check_out) + @reservations << Hotel::Reservation.new(room_selection, check_in, check_out) + end + + def list_reservations(date) + rez_by_date = [] + + @reservations.each do |reservation| + if reservation.date_range_array.include?(date) + rez_by_date << reservation + end + end + + return rez_by_date + end + + + def list_vacancies(check_in, check_out) + available_rooms = list_of_rooms + + date_range = Hotel::DateRange.new(check_in, check_out).date_range_array + + date_range.each do |date| + reservations.each do |reservation| + if reservation.date_range_array.include?(date) + available_rooms.delete(reservation.room_num) + end + + end + end + + return available_rooms + end + end end + +# puts Hotel::Admin.new.room_selection + +# room_num1 = 1 +# check_in = Date.new(2017,2,3) +# check_out = Date.new(2017, 2,7) +# # # puts Hotel::DateRange.new(check_in, check_out).date_range_array +# # reservation = Hotel::Reservation.new(room_num1, check_in, check_out) +# # +# # puts "#{reservation.room_nums}" +# +# +# puts Hotel::Admin.new.add_reservation(room_num1, check_in, check_out) +# +# puts "This should be the reservations array #{Hotel::Admin.new.reservations}" diff --git a/lib/Reservation.rb b/lib/Reservation.rb index 8bed9b2d3..00d6b0bf1 100644 --- a/lib/Reservation.rb +++ b/lib/Reservation.rb @@ -8,34 +8,37 @@ class Reservation attr_reader :reservation_array, :total_cost, :room_num, :date_range_array - def initialize( check_in, check_out) + def initialize(room_num, check_in, check_out) - @reservation_array = [] + # @reservation_array = [] @total_cost = 0 @room_num = room_num - @date_range_array = DateRange.new(check_in, check_out) + @date_range_array = DateRange.new(check_in, check_out).date_range_array end - def reservation_id - @reservation_array.length + 1 - end + # def reservation_id + # Hotel::Admin.reservations.length + 1 + # end + + def total_cost (@date_range_array.length - 1) * COST_PER_NIGHT end - - + # def self.find_all(check_in, check_out) + # + # # take array of reservations + # # interate over each to find the reservations for reserved status by date + # @reservation_array.each do |reservation| + # list_reservations = + # end + # return list_reservations + # end + # end # end class end #end module - - -check_in = Date.new(2017,2,3) -check_out = Date.new(2017, 2,7) -reservation = Hotel::Reservation.new(check_in, check_out) - -puts "#{reservation.reservation_array}" From 15c644bccfd87be5c97b985e5f39ec64ef5b48c7 Mon Sep 17 00:00:00 2001 From: Angela Wilson Date: Sat, 9 Sep 2017 21:54:14 -0700 Subject: [PATCH 06/19] realize previous git commits were committed for lib folder. So specs file was not staged. Specs are written for code except list_of_rooms, add_reservation, list_reservation(date), and list_vacancies methods. --- specs/admin_spec.rb | 115 ++++++++++++++++++++++++++++++++++++-- specs/reservation_spec.rb | 28 +++++++++- specs/spec_helper.rb | 4 +- unused-work/room_spec.rb | 82 +++++++++++++-------------- 4 files changed, 181 insertions(+), 48 deletions(-) diff --git a/specs/admin_spec.rb b/specs/admin_spec.rb index d6df4cf53..6cba3cf3f 100644 --- a/specs/admin_spec.rb +++ b/specs/admin_spec.rb @@ -6,10 +6,117 @@ - to automate making all 20 rooms we need a times loop from 1-20. - set an iterative variable to 1. =end - describe "Admin Class" do - describe "initialize" do - end # end initialize +# Hotel +# Admin +# Reservations +describe "Admin Class" do - end # end admin class + before do + + @admin = Hotel::Admin.new + + room_nums = 1 + check_in = Date.new(2017,2,3) + check_out = Date.new(2017, 2,7) + @admin.add_reservation(room_nums, check_in, check_out) + end + + describe "initialize" do + it "@reservations returns reservations array" do + @admin.reservations.must_be_kind_of Array + end + + # it "@reservations array is empty" do + # @admin.reservations.must_be_empty + # end + + it "@room_nums returns an array" do + @admin.room_nums.must_be_kind_of Array + end + + it "@room_nums is between 1-20" do + # normative classes + @admin.room_nums.first.must_equal 1 + @admin.room_nums.last.must_equal 20 + + # edge classes + @admin.room_nums.first.wont_equal 0 + @admin.room_nums.last.wont_equal 21 + @admin.room_nums.first.wont_equal ("!"..")") + end + + describe "List of rooms method" do + it "returns of all rooms" do + @admin.list_of_rooms.must_be_kind_of Array + end + + end + + + # it "Admin responds to room_nums method" do + # @admin.room_nums.must_respond_to :room_nums + # end + end # end initialize + + describe "add_reservation" do + it "thing that is added is a reservation" do + @admin.reservations[0].must_be_instance_of Hotel::Reservation + end + + it "verify reservation is added to array" do + @admin.reservations.length.must_equal 1 + end + end + + describe "list_reservations tests" do + it "list test" do + date = Date.new(2017, 2, 4) + @admin.list_reservations(date).must_be_instance_of Array + + date = Date.new(2017, 2, 9) + @admin.list_reservations(date).wont_be_instance_of Hotel::Reservation + end + end + + + it "list_vacancies" do + check_in = Date.new(2017, 2, 4) + check_out = Date.new(2017, 2, 5) + # x = Hotel::DateRange.new(check_in, check_out) + @admin.list_vacancies(check_in, check_out) + + + @admin.list_of_rooms + end +end # end admin class + +=begin + +Reservations +Block + Discount + Reservations + Room number + dates + Date + Price + Name + +=end + +=begin + +Block.new([rooms], [dates], discount) + +Block + Discount (float) + Reservations (array) + Room number + dates + Date + Price + Name + +=end diff --git a/specs/reservation_spec.rb b/specs/reservation_spec.rb index 609716931..3a2d59db4 100644 --- a/specs/reservation_spec.rb +++ b/specs/reservation_spec.rb @@ -2,11 +2,37 @@ describe "reservations" do + + before do + room_num = 1 + check_in = Date.new(2017,2,3) + check_out = Date.new(2017, 2,7) + @reservation = Hotel::Reservation.new(room_num, check_in, check_out) + end describe "initialize" do it "Returns a Reservation object" do - Hotel::Reservation.new.must_be_instance_of Hotel::Reservation + @reservation.must_be_instance_of Hotel::Reservation end + # it "@reservation_id is an integer" do + # @reservation.reservation_id.must_be_kind_of Integer + # end + # + # it "@reservation_array is an array" do + # @reservation.reservation_array.must_be_kind_of Array + # end + + + end + + # it "@reservation_id increments by 1" do + # @reservation.reservation_id.must_equal 1 + # + # end + + + it "total_cost method is an integer" do + @reservation.total_cost.must_equal 800 end end diff --git a/specs/spec_helper.rb b/specs/spec_helper.rb index d3f967da9..c3c2f389a 100644 --- a/specs/spec_helper.rb +++ b/specs/spec_helper.rb @@ -19,5 +19,5 @@ - -Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new +# +# Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new diff --git a/unused-work/room_spec.rb b/unused-work/room_spec.rb index 70fd97720..a49b15dfb 100644 --- a/unused-work/room_spec.rb +++ b/unused-work/room_spec.rb @@ -32,47 +32,47 @@ end describe "initialize" do - # it "Takes strings for both room number and reservation status" do - # room_num = "1" - # reservation_status = "Reserved" - # - # room_num.must_be_instance_of String - # reservation_status.must_be_instance_of String - # - # end - # - # it "Raises an error for room number not being in range 1 -20" do - # room_num = "#" - # reservation_status = "Reserved" - # proc { - # Hotel::Room.new(room_num, reservation_status) - # }.must_raise ArgumentError - # - # room_num = "21" - # proc{ - # @room.new(room_num, reservation_status) - # }.must_raise ArgumentError - # end - # - # it "Raises an error for reservation status not being either 'Reserved' or 'vacant'" do - # room_num = "20" - # reservation_status = "Rainbow" - # - # proc { - # @room.new(room_num, reservation_status) - # }.must_raise ArgumentError - # - # reservation_status = "123" - # proc { - # @room.new(room_num, reservation_status) - # }.must_raise ArgumentError - # - # reservation_status = "!@#" - # proc{ - # @room.new(room_num, reservation_status) - # }.must_raise ArgumentError - # - # end + it "Takes strings for both room number and reservation status" do + room_num = "1" + reservation_status = "Reserved" + + room_num.must_be_instance_of String + reservation_status.must_be_instance_of String + + end + + it "Raises an error for room number not being in range 1 -20" do + room_num = "#" + reservation_status = "Reserved" + proc { + Hotel::Room.new(room_num, reservation_status) + }.must_raise ArgumentError + + room_num = "21" + proc{ + @room.new(room_num, reservation_status) + }.must_raise ArgumentError + end + + it "Raises an error for reservation status not being either 'Reserved' or 'vacant'" do + room_num = "20" + reservation_status = "Rainbow" + + proc { + @room.new(room_num, reservation_status) + }.must_raise ArgumentError + + reservation_status = "123" + proc { + @room.new(room_num, reservation_status) + }.must_raise ArgumentError + + reservation_status = "!@#" + proc{ + @room.new(room_num, reservation_status) + }.must_raise ArgumentError + + end it "Returns an instance of Room class" do room_num = "1" From 87cc7022ca0f0460e48dd23041af5e2a97bed76e Mon Sep 17 00:00:00 2001 From: Angela Wilson Date: Sun, 10 Sep 2017 07:14:25 -0700 Subject: [PATCH 07/19] Write tests for methods list_of_rooms, add_reservation, and list_reservations. --- lib/Admin.rb | 13 +++----- specs/admin_spec.rb | 78 ++++++++++++++++++++++++++++----------------- 2 files changed, 54 insertions(+), 37 deletions(-) diff --git a/lib/Admin.rb b/lib/Admin.rb index 1b4bee45d..b489e7cf4 100644 --- a/lib/Admin.rb +++ b/lib/Admin.rb @@ -20,8 +20,8 @@ def list_of_rooms return Array.new(room_nums) end - def add_reservation(room_selection, check_in, check_out) - @reservations << Hotel::Reservation.new(room_selection, check_in, check_out) + def add_reservation(room_num, check_in, check_out) + @reservations << Hotel::Reservation.new(room_num, check_in, check_out) end def list_reservations(date) @@ -49,14 +49,11 @@ def list_vacancies(check_in, check_out) end end - end - + end # end date_range loop return available_rooms end - - - end -end + end # end class +end # end module # puts Hotel::Admin.new.room_selection diff --git a/specs/admin_spec.rb b/specs/admin_spec.rb index 6cba3cf3f..c5a97393b 100644 --- a/specs/admin_spec.rb +++ b/specs/admin_spec.rb @@ -17,10 +17,17 @@ @admin = Hotel::Admin.new - room_nums = 1 + room_num = 1 + room_num.must_be_kind_of Integer + room_num.wont_be_instance_of String + check_in = Date.new(2017,2,3) + check_in.must_be_instance_of Date + check_out = Date.new(2017, 2,7) - @admin.add_reservation(room_nums, check_in, check_out) + check_in.must_be_instance_of Date + + @admin.add_reservation(room_num, check_in, check_out) end describe "initialize" do @@ -28,10 +35,6 @@ @admin.reservations.must_be_kind_of Array end - # it "@reservations array is empty" do - # @admin.reservations.must_be_empty - # end - it "@room_nums returns an array" do @admin.room_nums.must_be_kind_of Array end @@ -44,39 +47,56 @@ # edge classes @admin.room_nums.first.wont_equal 0 @admin.room_nums.last.wont_equal 21 - @admin.room_nums.first.wont_equal ("!"..")") + @admin.room_nums.first.wont_be_instance_of String + @admin.room_nums.last.wont_be_instance_of String end describe "List of rooms method" do it "returns of all rooms" do + @admin.room_nums.must_be_kind_of Array @admin.list_of_rooms.must_be_kind_of Array - end - - end + # normative classes + @admin.list_of_rooms.first.must_equal 1 + @admin.list_of_rooms.last.must_equal 20 - # it "Admin responds to room_nums method" do - # @admin.room_nums.must_respond_to :room_nums - # end + # edge classes + @admin.list_of_rooms.first.wont_equal 0 + @admin.list_of_rooms.last.wont_equal 21 + @admin.room_nums.first.wont_be_instance_of String + @admin.room_nums.last.wont_be_instance_of String + end + end end # end initialize describe "add_reservation" do it "thing that is added is a reservation" do @admin.reservations[0].must_be_instance_of Hotel::Reservation + @admin.reservations[-1].must_be_instance_of Hotel::Reservation + end it "verify reservation is added to array" do @admin.reservations.length.must_equal 1 + @admin.reservations.length.wont_equal 2 + end end describe "list_reservations tests" do it "list test" do date = Date.new(2017, 2, 4) - @admin.list_reservations(date).must_be_instance_of Array + date.must_be_instance_of Date + @admin.list_reservations(date).must_be_instance_of Array + @admin.list_reservations(date).first.must_be_instance_of Hotel::Reservation + @admin.list_reservations(date).last.must_be_instance_of Hotel::Reservation + # edge cases date = Date.new(2017, 2, 9) - @admin.list_reservations(date).wont_be_instance_of Hotel::Reservation + date.must_be_instance_of Date + @admin.list_reservations(date).wont_be_instance_of Hotel::Reservation + @admin.list_reservations(date).wont_be_kind_of Integer + @admin.list_reservations(date).wont_be_kind_of String end end @@ -96,13 +116,13 @@ Reservations Block - Discount - Reservations - Room number - dates - Date - Price - Name +Discount +Reservations +Room number +dates +Date +Price +Name =end @@ -111,12 +131,12 @@ Block.new([rooms], [dates], discount) Block - Discount (float) - Reservations (array) - Room number - dates - Date - Price - Name +Discount (float) +Reservations (array) +Room number +dates +Date +Price +Name =end From c727184058a07aafecdf8cfc3b3bcb0281564e16 Mon Sep 17 00:00:00 2001 From: Angela Wilson Date: Sun, 10 Sep 2017 08:23:33 -0700 Subject: [PATCH 08/19] Write reserve available room by date range method and tests. --- lib/Admin.rb | 38 ++++++++++++++++---------------------- lib/Reservation.rb | 16 ---------------- specs/admin_spec.rb | 28 +++++++++++++++++++++++++--- 3 files changed, 41 insertions(+), 41 deletions(-) diff --git a/lib/Admin.rb b/lib/Admin.rb index b489e7cf4..5ea9cee43 100644 --- a/lib/Admin.rb +++ b/lib/Admin.rb @@ -6,24 +6,27 @@ class Admin attr_reader :room_nums, :reservations def initialize - @reservations = [] - - @room_nums = [] 20.times do |i| @room_nums << (i + 1) end end + + def list_of_rooms return Array.new(room_nums) end + + def add_reservation(room_num, check_in, check_out) - @reservations << Hotel::Reservation.new(room_num, check_in, check_out) + @reservations << Reservation.new(room_num, check_in, check_out) end + + def list_reservations(date) rez_by_date = [] @@ -32,40 +35,31 @@ def list_reservations(date) rez_by_date << reservation end end - return rez_by_date end + # lists room vacancies that can be utilized for booking available rooms def list_vacancies(check_in, check_out) available_rooms = list_of_rooms - date_range = Hotel::DateRange.new(check_in, check_out).date_range_array + date_range = DateRange.new(check_in, check_out).date_range_array date_range.each do |date| reservations.each do |reservation| if reservation.date_range_array.include?(date) available_rooms.delete(reservation.room_num) end - end end # end date_range loop return available_rooms end + + + # reserves available room for a given date range. + def reserve_by_date(check_in, check_out) + room_num = self.list_vacancies(check_in, check_out).sample + Hotel::Reservation.new(room_num, check_in, check_out) + end end # end class end # end module - -# puts Hotel::Admin.new.room_selection - -# room_num1 = 1 -# check_in = Date.new(2017,2,3) -# check_out = Date.new(2017, 2,7) -# # # puts Hotel::DateRange.new(check_in, check_out).date_range_array -# # reservation = Hotel::Reservation.new(room_num1, check_in, check_out) -# # -# # puts "#{reservation.room_nums}" -# -# -# puts Hotel::Admin.new.add_reservation(room_num1, check_in, check_out) -# -# puts "This should be the reservations array #{Hotel::Admin.new.reservations}" diff --git a/lib/Reservation.rb b/lib/Reservation.rb index 00d6b0bf1..5921f300d 100644 --- a/lib/Reservation.rb +++ b/lib/Reservation.rb @@ -18,26 +18,10 @@ def initialize(room_num, check_in, check_out) - # def reservation_id - # Hotel::Admin.reservations.length + 1 - # end - - - def total_cost (@date_range_array.length - 1) * COST_PER_NIGHT end - # def self.find_all(check_in, check_out) - # - # # take array of reservations - # # interate over each to find the reservations for reserved status by date - # @reservation_array.each do |reservation| - # list_reservations = - # end - # return list_reservations - # end - # end # end class diff --git a/specs/admin_spec.rb b/specs/admin_spec.rb index c5a97393b..786ba36b6 100644 --- a/specs/admin_spec.rb +++ b/specs/admin_spec.rb @@ -87,6 +87,7 @@ it "list test" do date = Date.new(2017, 2, 4) date.must_be_instance_of Date + @admin.list_reservations(date).must_be_instance_of Array @admin.list_reservations(date).first.must_be_instance_of Hotel::Reservation @admin.list_reservations(date).last.must_be_instance_of Hotel::Reservation @@ -103,12 +104,33 @@ it "list_vacancies" do check_in = Date.new(2017, 2, 4) + check_in.must_be_instance_of Date + check_out = Date.new(2017, 2, 5) - # x = Hotel::DateRange.new(check_in, check_out) - @admin.list_vacancies(check_in, check_out) + check_in.must_be_instance_of Date + @admin.list_vacancies(check_in, check_out).must_be_instance_of Array + @admin.list_vacancies(check_in, check_out)[0].must_be_kind_of Integer + @admin.list_vacancies(check_in, check_out)[-1].must_be_kind_of Integer - @admin.list_of_rooms + # edge cases + @admin.list_vacancies(check_in, check_out).wont_be_instance_of Hotel::Reservation + @admin.list_vacancies(check_in, check_out).wont_be_kind_of Integer + @admin.list_vacancies(check_in, check_out).wont_be_kind_of String + end + + describe "reserve available room by date range" do + it "validates input" do + check_in = Date.new(2017, 2, 4) + check_in.must_be_instance_of Date + + check_out = Date.new(2017, 2, 5) + check_in.must_be_instance_of Date + + @admin.must_respond_to :list_vacancies + @admin.reserve_by_date(check_in, check_out).total_cost.must_equal 200 + @admin.reserve_by_date(check_in, check_out). wont_equal 0 + end end end # end admin class From 07544d918a2f454818f12a2284d5416a9b41bd94 Mon Sep 17 00:00:00 2001 From: Angela Wilson Date: Sun, 10 Sep 2017 13:25:40 -0700 Subject: [PATCH 09/19] Big Picture: Refactoring Reservation class to dual as Block and Reservation functions. This commit, refactor the initialize method to take a discount and refactor total_cost method to take discount. --- lib/Admin.rb | 2 +- lib/Reservation.rb | 14 +++++++++----- specs/admin_spec.rb | 2 ++ specs/reservation_spec.rb | 24 +++++++----------------- unused-work/reservation-class.rb | 28 ++++++++++++++++++++++++++++ 5 files changed, 47 insertions(+), 23 deletions(-) create mode 100644 unused-work/reservation-class.rb diff --git a/lib/Admin.rb b/lib/Admin.rb index 5ea9cee43..3d951d40e 100644 --- a/lib/Admin.rb +++ b/lib/Admin.rb @@ -59,7 +59,7 @@ def list_vacancies(check_in, check_out) # reserves available room for a given date range. def reserve_by_date(check_in, check_out) room_num = self.list_vacancies(check_in, check_out).sample - Hotel::Reservation.new(room_num, check_in, check_out) + Hotel::Reservation.new(room_num, check_in, check_out, discount_percent: 0.0) end end # end class end # end module diff --git a/lib/Reservation.rb b/lib/Reservation.rb index 5921f300d..4a4b6c509 100644 --- a/lib/Reservation.rb +++ b/lib/Reservation.rb @@ -4,22 +4,26 @@ module Hotel class Reservation - COST_PER_NIGHT = 200 + COST_PER_NIGHT = 200.00 - attr_reader :reservation_array, :total_cost, :room_num, :date_range_array + attr_reader :room_num, :date_range_array, :discount_percent - def initialize(room_num, check_in, check_out) + def initialize(room_num, check_in, check_out, discount_percent: 0.0) # @reservation_array = [] - @total_cost = 0 + # @total_cost = 0 @room_num = room_num @date_range_array = DateRange.new(check_in, check_out).date_range_array + @discount_percent = discount_percent end def total_cost - (@date_range_array.length - 1) * COST_PER_NIGHT + full_price = (@date_range_array.length - 1) * COST_PER_NIGHT + discount = full_price * discount_percent + return total_cost = full_price - discount + end diff --git a/specs/admin_spec.rb b/specs/admin_spec.rb index 786ba36b6..5f59f073d 100644 --- a/specs/admin_spec.rb +++ b/specs/admin_spec.rb @@ -152,6 +152,8 @@ Block.new([rooms], [dates], discount) +[] + Block Discount (float) Reservations (array) diff --git a/specs/reservation_spec.rb b/specs/reservation_spec.rb index 3a2d59db4..97400d594 100644 --- a/specs/reservation_spec.rb +++ b/specs/reservation_spec.rb @@ -7,32 +7,22 @@ room_num = 1 check_in = Date.new(2017,2,3) check_out = Date.new(2017, 2,7) - @reservation = Hotel::Reservation.new(room_num, check_in, check_out) + @reservation = Hotel::Reservation.new(room_num, check_in, check_out, discount_percent: 0.3) end describe "initialize" do it "Returns a Reservation object" do @reservation.must_be_instance_of Hotel::Reservation + + puts "This is the cost #{@reservation.total_cost}" end - # it "@reservation_id is an integer" do - # @reservation.reservation_id.must_be_kind_of Integer - # end - # - # it "@reservation_array is an array" do - # @reservation.reservation_array.must_be_kind_of Array - # end - end - # it "@reservation_id increments by 1" do - # @reservation.reservation_id.must_equal 1 - # - # end + end - - it "total_cost method is an integer" do - @reservation.total_cost.must_equal 800 - end + # it "total_cost method is an integer" do + # @reservation.total_cost.must_equal 800.00 + # end end diff --git a/unused-work/reservation-class.rb b/unused-work/reservation-class.rb new file mode 100644 index 000000000..7ad58ba80 --- /dev/null +++ b/unused-work/reservation-class.rb @@ -0,0 +1,28 @@ +require 'date' + +require_relative 'daterange' + +module Hotel + class Reservation + COST_PER_NIGHT = 200.00 + + attr_reader :total_cost, :room_num, :date_range_array + + def initialize(room_num, check_in, check_out) + + # @reservation_array = [] + @total_cost = 0 + @room_num = room_num + @date_range_array = DateRange.new(check_in, check_out).date_range_array + end + + + + def total_cost + (@date_range_array.length - 1) * COST_PER_NIGHT + end + + + + end # end class +end #end module From 11e312e7e0d3948a5dee1ed27c5b6ec96fc2acfb Mon Sep 17 00:00:00 2001 From: Angela Wilson Date: Sun, 10 Sep 2017 19:04:48 -0700 Subject: [PATCH 10/19] Refactor Admin class to create method create_block_by_date. --- lib/Admin.rb | 49 +++++++++++++++-------------- lib/Block.rb | 37 ++++++++++++++++++++++ lib/Reservation.rb | 16 ++++------ specs/admin_spec.rb | 53 ++++++++++++++++++++------------ specs/reservation_spec.rb | 17 ++++++---- specs/spec_helper.rb | 1 + unused-work/reservation-class.rb | 28 ----------------- 7 files changed, 115 insertions(+), 86 deletions(-) create mode 100644 lib/Block.rb delete mode 100644 unused-work/reservation-class.rb diff --git a/lib/Admin.rb b/lib/Admin.rb index 3d951d40e..5f79fadcf 100644 --- a/lib/Admin.rb +++ b/lib/Admin.rb @@ -1,12 +1,13 @@ require_relative 'reservation' +require_relative 'block' module Hotel class Admin - attr_reader :room_nums, :reservations + attr_reader :room_nums, :blocks def initialize - @reservations = [] + @blocks = [] @room_nums = [] 20.times do |i| @room_nums << (i + 1) @@ -21,22 +22,22 @@ def list_of_rooms - def add_reservation(room_num, check_in, check_out) - @reservations << Reservation.new(room_num, check_in, check_out) + def add_block(room_num_array, check_in, check_out, discount_percent: 0.0) + @blocks << Hotel::Block.new(room_num_array, check_in, check_out, discount_percent: 0.0) end - - - def list_reservations(date) - rez_by_date = [] - - @reservations.each do |reservation| - if reservation.date_range_array.include?(date) - rez_by_date << reservation - end - end - return rez_by_date - end + #TODO + #TODO: + # def list_reservations(date) + # block_by_date = [] + # + # @blocks.each do |block| + # if block.date_range_array.include?(date) + # block_by_date << block + # end + # end + # return block_by_date + # end # lists room vacancies that can be utilized for booking available rooms @@ -46,9 +47,11 @@ def list_vacancies(check_in, check_out) date_range = DateRange.new(check_in, check_out).date_range_array date_range.each do |date| - reservations.each do |reservation| - if reservation.date_range_array.include?(date) - available_rooms.delete(reservation.room_num) + @blocks.each do |block| + if block.block_date_range_array.include?(date) + block.room_num_array.each do |room_num| + available_rooms.delete(room_num) + end end end end # end date_range loop @@ -56,10 +59,10 @@ def list_vacancies(check_in, check_out) end - # reserves available room for a given date range. - def reserve_by_date(check_in, check_out) - room_num = self.list_vacancies(check_in, check_out).sample - Hotel::Reservation.new(room_num, check_in, check_out, discount_percent: 0.0) + # creates block with available room for a given date range. + def create_block_by_date(rooms_per_block, check_in, check_out, discount_percent: 0.0) + room_num_array = self.list_vacancies(check_in, check_out).sample(rooms_per_block).to_a + @blocks << Hotel::Block.new(room_num_array, check_in, check_out, discount_percent: 0.0) end end # end class end # end module diff --git a/lib/Block.rb b/lib/Block.rb new file mode 100644 index 000000000..4ca21ca77 --- /dev/null +++ b/lib/Block.rb @@ -0,0 +1,37 @@ +require 'date' + +require_relative 'daterange' + +module Hotel + class Block + COST_PER_NIGHT = 200.00 + + attr_reader :room_num_array, :block_date_range_array, :discount_percent, :reservation_array + + def initialize(room_num_array, check_in, check_out, discount_percent: 0) + + + @room_num_array = room_num_array + @block_date_range_array = DateRange.new(check_in, check_out).date_range_array + @discount_percent = discount_percent + + @reservations_array = [] + end + + + + def total_cost + full_price = (@block_date_range_array.length - 1) * COST_PER_NIGHT + discount = full_price * (discount_percent/100.0) + return total_cost = full_price - discount + end + + def add_reservation_to_block(room_num, check_in, check_out) + + @reservations_array << Hotel::Reservation.new(room_num, check_in, check_out) + end + + + + end # end class +end #end module diff --git a/lib/Reservation.rb b/lib/Reservation.rb index 4a4b6c509..3c46dfdb7 100644 --- a/lib/Reservation.rb +++ b/lib/Reservation.rb @@ -6,25 +6,21 @@ module Hotel class Reservation COST_PER_NIGHT = 200.00 - attr_reader :room_num, :date_range_array, :discount_percent + attr_reader :total_cost, :room_num, :date_range_array - def initialize(room_num, check_in, check_out, discount_percent: 0.0) + def initialize(room_num, check_in, check_out) # @reservation_array = [] - # @total_cost = 0 + @total_cost = 0 @room_num = room_num @date_range_array = DateRange.new(check_in, check_out).date_range_array - @discount_percent = discount_percent end - def total_cost - full_price = (@date_range_array.length - 1) * COST_PER_NIGHT - discount = full_price * discount_percent - return total_cost = full_price - discount - - end + # def total_cost + # (@date_ranges_array.length - 1) * COST_PER_NIGHT + # end diff --git a/specs/admin_spec.rb b/specs/admin_spec.rb index 5f59f073d..cdb498958 100644 --- a/specs/admin_spec.rb +++ b/specs/admin_spec.rb @@ -11,15 +11,25 @@ # Admin # Reservations -describe "Admin Class" do +test_ob = Hotel::Admin.new + +puts "This is a block #{test_ob.add_block([1,2,3,4], Date.new(2017,2,3), Date.new(2017, 2,7))}" + +puts "List of available rooms #{test_ob.list_vacancies(Date.new(2017,2,3), Date.new(2017, 2,7))}" + +puts test_ob.create_block_by_date(4, Date.new(2017,2,3), Date.new(2017, 2,7)) + +puts "List of available rooms #{test_ob.list_vacancies(Date.new(2017,2,3), Date.new(2017, 2,7))}" + +xdescribe "Admin Class" do before do @admin = Hotel::Admin.new - room_num = 1 - room_num.must_be_kind_of Integer - room_num.wont_be_instance_of String + rooms_per_block = 4 + rooms_per_block.must_be_kind_of Integer + rooms_per_block.wont_be_instance_of String check_in = Date.new(2017,2,3) check_in.must_be_instance_of Date @@ -27,12 +37,12 @@ check_out = Date.new(2017, 2,7) check_in.must_be_instance_of Date - @admin.add_reservation(room_num, check_in, check_out) + @admin.add_block(rooms_per_block, check_in, check_out, discount_percent: 0.0) end describe "initialize" do - it "@reservations returns reservations array" do - @admin.reservations.must_be_kind_of Array + it "@blocks returns blocks array" do + @admin.blocks.must_be_kind_of Array end it "@room_nums returns an array" do @@ -51,7 +61,7 @@ @admin.room_nums.last.wont_be_instance_of String end - describe "List of rooms method" do + describe "List_of_rooms method" do it "returns of all rooms" do @admin.room_nums.must_be_kind_of Array @admin.list_of_rooms.must_be_kind_of Array @@ -69,21 +79,21 @@ end end # end initialize - describe "add_reservation" do - it "thing that is added is a reservation" do - @admin.reservations[0].must_be_instance_of Hotel::Reservation - @admin.reservations[-1].must_be_instance_of Hotel::Reservation + xdescribe "add_block" do + it "thing that is added is a block" do + @admin.blocks[0].must_be_instance_of Hotel::Block + @admin.blocks[-1].must_be_instance_of Hotel::Block end - it "verify reservation is added to array" do - @admin.reservations.length.must_equal 1 - @admin.reservations.length.wont_equal 2 + it "verify block is added to array" do + @admin.blocks.length.must_equal 1 + @admin.blocks.length.wont_equal 2 end end - describe "list_reservations tests" do + xdescribe "list_reservations tests" do it "list test" do date = Date.new(2017, 2, 4) date.must_be_instance_of Date @@ -114,13 +124,16 @@ @admin.list_vacancies(check_in, check_out)[-1].must_be_kind_of Integer # edge cases - @admin.list_vacancies(check_in, check_out).wont_be_instance_of Hotel::Reservation + @admin.list_vacancies(check_in, check_out).wont_be_instance_of Hotel::Block @admin.list_vacancies(check_in, check_out).wont_be_kind_of Integer @admin.list_vacancies(check_in, check_out).wont_be_kind_of String end describe "reserve available room by date range" do it "validates input" do + rooms_per_block = 4 + rooms_per_block.must_be_kind_of Integer + check_in = Date.new(2017, 2, 4) check_in.must_be_instance_of Date @@ -128,8 +141,10 @@ check_in.must_be_instance_of Date @admin.must_respond_to :list_vacancies - @admin.reserve_by_date(check_in, check_out).total_cost.must_equal 200 - @admin.reserve_by_date(check_in, check_out). wont_equal 0 + + puts "this is the list of blocked rooms #{@admin.create_block_by_date(rooms_per_block, check_in, check_out)}" + # @admin.create_block_by_date(rooms_per_block, check_in, check_out).total_cost.must_equal 200 + # @admin.create_block_by_date(rooms_per_block, check_in, check_out).wont_equal 0 end end end # end admin class diff --git a/specs/reservation_spec.rb b/specs/reservation_spec.rb index 97400d594..bd0a0e07d 100644 --- a/specs/reservation_spec.rb +++ b/specs/reservation_spec.rb @@ -7,22 +7,27 @@ room_num = 1 check_in = Date.new(2017,2,3) check_out = Date.new(2017, 2,7) - @reservation = Hotel::Reservation.new(room_num, check_in, check_out, discount_percent: 0.3) + @reservation = Hotel::Reservation.new(room_num, check_in, check_out) end describe "initialize" do it "Returns a Reservation object" do @reservation.must_be_instance_of Hotel::Reservation - puts "This is the cost #{@reservation.total_cost}" - end + end end - - # it "total_cost method is an integer" do - # @reservation.total_cost.must_equal 800.00 + # describe "total_cost method" do + # it "total_cost method is an integer" do + # @reservation.total_cost.must_equal 800.00 + # end + # + # it "total_cost method is an integer" do + # @reservation.total_cost.must_equal 800.00 + # end # end + end diff --git a/specs/spec_helper.rb b/specs/spec_helper.rb index c3c2f389a..062fd3414 100644 --- a/specs/spec_helper.rb +++ b/specs/spec_helper.rb @@ -16,6 +16,7 @@ require_relative '../lib/daterange' require_relative '../lib/admin' require_relative '../lib/reservation' +require_relative '../lib/block' diff --git a/unused-work/reservation-class.rb b/unused-work/reservation-class.rb deleted file mode 100644 index 7ad58ba80..000000000 --- a/unused-work/reservation-class.rb +++ /dev/null @@ -1,28 +0,0 @@ -require 'date' - -require_relative 'daterange' - -module Hotel - class Reservation - COST_PER_NIGHT = 200.00 - - attr_reader :total_cost, :room_num, :date_range_array - - def initialize(room_num, check_in, check_out) - - # @reservation_array = [] - @total_cost = 0 - @room_num = room_num - @date_range_array = DateRange.new(check_in, check_out).date_range_array - end - - - - def total_cost - (@date_range_array.length - 1) * COST_PER_NIGHT - end - - - - end # end class -end #end module From a6a88f929bc5b2a559c236fa416a0edb24a53ae1 Mon Sep 17 00:00:00 2001 From: Angela Wilson Date: Sun, 10 Sep 2017 21:20:27 -0700 Subject: [PATCH 11/19] Admin Class: Add methods create_block_by_date, find_block, list_available_blocked_rooms, and find_rooms_from_block. Also added method add_reservation_to_block. --- lib/Admin.rb | 77 +++++++++++++++++++++++++++++++++++++++++---- lib/Block.rb | 10 ++++-- specs/admin_spec.rb | 39 ++++++++++++++++++++--- specs/block_spec.rb | 5 +++ 4 files changed, 119 insertions(+), 12 deletions(-) create mode 100644 specs/block_spec.rb diff --git a/lib/Admin.rb b/lib/Admin.rb index 5f79fadcf..f01e83f8a 100644 --- a/lib/Admin.rb +++ b/lib/Admin.rb @@ -4,7 +4,7 @@ module Hotel class Admin - attr_reader :room_nums, :blocks + attr_reader :blocks, :room_nums, :reservations_array def initialize @blocks = [] @@ -12,7 +12,9 @@ def initialize 20.times do |i| @room_nums << (i + 1) end - end + + @reservations_array = [] + end # end initialize @@ -22,10 +24,40 @@ def list_of_rooms - def add_block(room_num_array, check_in, check_out, discount_percent: 0.0) - @blocks << Hotel::Block.new(room_num_array, check_in, check_out, discount_percent: 0.0) + def add_block(room_num_array, check_in, check_out, block_id, discount_percent: 0) + @blocks << Hotel::Block.new(room_num_array, check_in, check_out, block_id, discount_percent: 0) + end + + # Wave 2 requirement. Add reservation only no block + # def add_reservation(room_selection, check_in, check_out) + # @reservations << Hotel::Reservation.new(room_selection, check_in, check_out) + # end + + + def list_reservations(date) + rez_by_date = [] + + @blocks.each do |block| + block.reservations_array.each do |reservation| + rez_by_date << reservation + end + end + return rez_by_date end + # def list_reservations(date) + # rez_by_date = [] + # + # @reservations_array.each do |reservation| + # if reservation.date_range_array.include?(date) + # rez_by_date << reservation + # end + # end + # + # return rez_by_date + # end + + #TODO #TODO: # def list_reservations(date) @@ -60,9 +92,42 @@ def list_vacancies(check_in, check_out) # creates block with available room for a given date range. - def create_block_by_date(rooms_per_block, check_in, check_out, discount_percent: 0.0) + def create_block_by_date(rooms_per_block, check_in, check_out, block_id, discount_percent: 0.0) room_num_array = self.list_vacancies(check_in, check_out).sample(rooms_per_block).to_a - @blocks << Hotel::Block.new(room_num_array, check_in, check_out, discount_percent: 0.0) + @blocks << Hotel::Block.new(room_num_array, check_in, check_out, block_id, discount_percent: 0.0) end + + def find_block(id) + @blocks.each do |block| + if block.block_id == id + return block + end + end + end + + def list_available_blocked_rooms(id) + block = self.find_block(id) + return block.room_num_array + end + + + def find_rooms_from_block(id, num_rooms_to_reserve) + return self.list_available_blocked_rooms(id).sample(num_rooms_to_reserve) + end + + + + def add_reservation_to_block(id, num_rooms_to_reserve, check_in, check_out) + block = self.find_block(id) + num_rooms_to_reserve = self.find_rooms_from_block(id, num_rooms_to_reserve) + + num_rooms_to_reserve.length.times do |room_num| + block.reservations_array << Hotel::Reservation.new(room_num, check_in, check_out) + end + end + + + + end # end class end # end module diff --git a/lib/Block.rb b/lib/Block.rb index 4ca21ca77..b7c0a9bfd 100644 --- a/lib/Block.rb +++ b/lib/Block.rb @@ -6,15 +6,18 @@ module Hotel class Block COST_PER_NIGHT = 200.00 - attr_reader :room_num_array, :block_date_range_array, :discount_percent, :reservation_array + attr_reader :room_num_array, :block_date_range_array, :discount_percent, :block_id, :reservations_array - def initialize(room_num_array, check_in, check_out, discount_percent: 0) + def initialize(room_num_array, check_in, check_out, block_id, discount_percent: 0) @room_num_array = room_num_array @block_date_range_array = DateRange.new(check_in, check_out).date_range_array + # what to automate block_id and was stumped on implementation. So I opted for manuel entry + @block_id = block_id @discount_percent = discount_percent + @reservations_array = [] end @@ -26,8 +29,11 @@ def total_cost return total_cost = full_price - discount end + + def add_reservation_to_block(room_num, check_in, check_out) + @reservations_array << Hotel::Reservation.new(room_num, check_in, check_out) end diff --git a/specs/admin_spec.rb b/specs/admin_spec.rb index cdb498958..d635fd4f0 100644 --- a/specs/admin_spec.rb +++ b/specs/admin_spec.rb @@ -13,13 +13,44 @@ test_ob = Hotel::Admin.new -puts "This is a block #{test_ob.add_block([1,2,3,4], Date.new(2017,2,3), Date.new(2017, 2,7))}" +puts " -puts "List of available rooms #{test_ob.list_vacancies(Date.new(2017,2,3), Date.new(2017, 2,7))}" +This is a block #{test_ob.add_block([1,2,3,4], Date.new(2017,2,3), Date.new(2017, 2,7), 1)} + +" + + + +puts "List of available rooms #{test_ob.list_vacancies(Date.new(2017,2,3), Date.new(2017, 2,7))} + +" + +#second block +puts test_ob.create_block_by_date(4, Date.new(2017,2,3), Date.new(2017, 2,7), 2) + +puts "List of available rooms after second block is created. #{test_ob.list_vacancies(Date.new(2017,2,3), Date.new(2017, 2,7))} + +" + + +puts "list_available_blocked_rooms #{test_ob.list_available_blocked_rooms(1)} + +" +puts "finding rooms from block #{test_ob.find_rooms_from_block(1, 2)} + +" + + +puts "Adding #{test_ob.add_reservation_to_block(1, 2, Date.new(2017,2,3), Date.new(2017, 2,7))} reservation to block + +" +puts "list of reservations #{test_ob.list_reservations(Date.new(2017,2,3))} + +" + +puts test_ob.reservations_array -puts test_ob.create_block_by_date(4, Date.new(2017,2,3), Date.new(2017, 2,7)) -puts "List of available rooms #{test_ob.list_vacancies(Date.new(2017,2,3), Date.new(2017, 2,7))}" xdescribe "Admin Class" do diff --git a/specs/block_spec.rb b/specs/block_spec.rb new file mode 100644 index 000000000..6e9e11bc6 --- /dev/null +++ b/specs/block_spec.rb @@ -0,0 +1,5 @@ +# require_relative '../specs/spec_helper' +# +# test_ob = Hotel::Block.new +# +# test_ob.add_reservation_to_block From 3306518cae5527c2d1d2801d9bb27f7eb3ef6816 Mon Sep 17 00:00:00 2001 From: Angela Wilson Date: Mon, 11 Sep 2017 08:52:17 -0700 Subject: [PATCH 12/19] The tests for refactored Admin class were turned off during wave 3's block refactor. Work on refactoring the specs for block code. --- lib/Admin.rb | 25 ++++--- lib/Block.rb | 9 +-- lib/Reservation.rb | 4 +- specs/admin_spec.rb | 154 +++++++++++++++++++++++++++++++------------- 4 files changed, 127 insertions(+), 65 deletions(-) diff --git a/lib/Admin.rb b/lib/Admin.rb index f01e83f8a..d89edf7d0 100644 --- a/lib/Admin.rb +++ b/lib/Admin.rb @@ -1,5 +1,7 @@ require_relative 'reservation' require_relative 'block' +require 'pry' +require 'date' module Hotel class Admin @@ -13,7 +15,7 @@ def initialize @room_nums << (i + 1) end - @reservations_array = [] + @reservations_array1 = [] end # end initialize @@ -38,7 +40,7 @@ def list_reservations(date) rez_by_date = [] @blocks.each do |block| - block.reservations_array.each do |reservation| + block.reservations_array2.each do |reservation| rez_by_date << reservation end end @@ -58,8 +60,7 @@ def list_reservations(date) # end - #TODO - #TODO: + # wave two requirement # def list_reservations(date) # block_by_date = [] # @@ -87,13 +88,14 @@ def list_vacancies(check_in, check_out) end end end # end date_range loop + return available_rooms end # creates block with available room for a given date range. def create_block_by_date(rooms_per_block, check_in, check_out, block_id, discount_percent: 0.0) - room_num_array = self.list_vacancies(check_in, check_out).sample(rooms_per_block).to_a + room_num_array = self.list_vacancies(check_in, check_out).take(rooms_per_block).to_a @blocks << Hotel::Block.new(room_num_array, check_in, check_out, block_id, discount_percent: 0.0) end @@ -106,23 +108,26 @@ def find_block(id) end def list_available_blocked_rooms(id) - block = self.find_block(id) - return block.room_num_array + block_array = self.find_block(id) + block_array.each do |block| + return block.room_num_array + end + end def find_rooms_from_block(id, num_rooms_to_reserve) - return self.list_available_blocked_rooms(id).sample(num_rooms_to_reserve) + return self.list_available_blocked_rooms(id).take(num_rooms_to_reserve) end - def add_reservation_to_block(id, num_rooms_to_reserve, check_in, check_out) + def add_reservation_to_block(id, num_rooms_to_reserve, check_in, check_out, discount_percent: 0) block = self.find_block(id) num_rooms_to_reserve = self.find_rooms_from_block(id, num_rooms_to_reserve) num_rooms_to_reserve.length.times do |room_num| - block.reservations_array << Hotel::Reservation.new(room_num, check_in, check_out) + block.reservations_array << Hotel::Reservation.new(room_num, check_in, check_out, discount_percent: 0) end end diff --git a/lib/Block.rb b/lib/Block.rb index b7c0a9bfd..2d2807fc4 100644 --- a/lib/Block.rb +++ b/lib/Block.rb @@ -6,7 +6,7 @@ module Hotel class Block COST_PER_NIGHT = 200.00 - attr_reader :room_num_array, :block_date_range_array, :discount_percent, :block_id, :reservations_array + attr_reader :room_num_array, :block_date_range_array, :discount_percent, :block_id, :reservations_array2 def initialize(room_num_array, check_in, check_out, block_id, discount_percent: 0) @@ -18,7 +18,7 @@ def initialize(room_num_array, check_in, check_out, block_id, discount_percent: @discount_percent = discount_percent - @reservations_array = [] + @reservations_array2 = [] end @@ -31,11 +31,6 @@ def total_cost - def add_reservation_to_block(room_num, check_in, check_out) - - - @reservations_array << Hotel::Reservation.new(room_num, check_in, check_out) - end diff --git a/lib/Reservation.rb b/lib/Reservation.rb index 3c46dfdb7..717a094a3 100644 --- a/lib/Reservation.rb +++ b/lib/Reservation.rb @@ -8,7 +8,7 @@ class Reservation attr_reader :total_cost, :room_num, :date_range_array - def initialize(room_num, check_in, check_out) + def initialize(room_num, check_in, check_out, discount_percent: 0) # @reservation_array = [] @total_cost = 0 @@ -17,7 +17,7 @@ def initialize(room_num, check_in, check_out) end - + # def total_cost # (@date_ranges_array.length - 1) * COST_PER_NIGHT # end diff --git a/specs/admin_spec.rb b/specs/admin_spec.rb index d635fd4f0..ffe3ce929 100644 --- a/specs/admin_spec.rb +++ b/specs/admin_spec.rb @@ -11,48 +11,10 @@ # Admin # Reservations -test_ob = Hotel::Admin.new - -puts " - -This is a block #{test_ob.add_block([1,2,3,4], Date.new(2017,2,3), Date.new(2017, 2,7), 1)} - -" -puts "List of available rooms #{test_ob.list_vacancies(Date.new(2017,2,3), Date.new(2017, 2,7))} - -" - -#second block -puts test_ob.create_block_by_date(4, Date.new(2017,2,3), Date.new(2017, 2,7), 2) - -puts "List of available rooms after second block is created. #{test_ob.list_vacancies(Date.new(2017,2,3), Date.new(2017, 2,7))} - -" - - -puts "list_available_blocked_rooms #{test_ob.list_available_blocked_rooms(1)} - -" -puts "finding rooms from block #{test_ob.find_rooms_from_block(1, 2)} - -" - - -puts "Adding #{test_ob.add_reservation_to_block(1, 2, Date.new(2017,2,3), Date.new(2017, 2,7))} reservation to block - -" -puts "list of reservations #{test_ob.list_reservations(Date.new(2017,2,3))} - -" - -puts test_ob.reservations_array - - - -xdescribe "Admin Class" do +describe "Admin Class" do before do @@ -68,7 +30,11 @@ check_out = Date.new(2017, 2,7) check_in.must_be_instance_of Date - @admin.add_block(rooms_per_block, check_in, check_out, discount_percent: 0.0) + block_id = 1 + + @admin.create_block_by_date(rooms_per_block, check_in, check_out, block_id, discount_percent: 0.0) + + end describe "initialize" do @@ -110,11 +76,12 @@ end end # end initialize - xdescribe "add_block" do + describe "add_block" do it "thing that is added is a block" do @admin.blocks[0].must_be_instance_of Hotel::Block @admin.blocks[-1].must_be_instance_of Hotel::Block + end it "verify block is added to array" do @@ -124,7 +91,7 @@ end end - xdescribe "list_reservations tests" do + describe "list_reservations tests" do it "list test" do date = Date.new(2017, 2, 4) date.must_be_instance_of Date @@ -159,8 +126,10 @@ @admin.list_vacancies(check_in, check_out).wont_be_kind_of Integer @admin.list_vacancies(check_in, check_out).wont_be_kind_of String end + # + - describe "reserve available room by date range" do + describe "create block by date range" do it "validates input" do rooms_per_block = 4 rooms_per_block.must_be_kind_of Integer @@ -171,15 +140,108 @@ check_out = Date.new(2017, 2, 5) check_in.must_be_instance_of Date + block_id = 2 + block_id.must_be_kind_of Integer + + # @admin.create_block_by_date(rooms_per_block, check_in, check_out,block_id) + # + # @admin.blocks.must_equal 1 + @admin.must_respond_to :list_vacancies - puts "this is the list of blocked rooms #{@admin.create_block_by_date(rooms_per_block, check_in, check_out)}" - # @admin.create_block_by_date(rooms_per_block, check_in, check_out).total_cost.must_equal 200 - # @admin.create_block_by_date(rooms_per_block, check_in, check_out).wont_equal 0 + @admin.blocks.length.must_equal 1 + + # @admin.create_block_by_date(rooms_per_block, check_in, check_out, 2).total_cost.must_equal 200 + # @admin.create_block_by_date(rooms_per_block, check_in, check_out, 2).wont_equal 0 end + + describe "find_block" do + it "takes integer id" do + id = 1 + @admin.find_block(id).must_be_kind_of Hotel::Block + + @admin.find_block(id).must_be_kind_of Hotel::Block + + @admin.find_block(id).wont_be_kind_of String + + + id = 0 + @admin.find_block(id).wont_be_kind_of Hotel::Block + end + + it "returns block object" do + @admin.find_block(1).must_be_instance_of Hotel::Block + end + end + + describe "list_available_blocked_rooms" do + it "returns an array with room numbers" do + @admin.list_available_blocked_rooms(2).must_be_instance_of Array + @admin.list_available_blocked_rooms(2).first.must_be_kind_of Integer + @admin.list_available_blocked_rooms(2).last.must_be_kind_of Integer + + + #edge cases + + @admin.list_available_blocked_rooms(2).first.wont_be_kind_of String + @admin.list_available_blocked_rooms(2).last.wont_be_kind_of String + end + end + + describe "find_rooms_from_block" do + it "returns array of a specific block's rooms" do + @admin.find_rooms_from_block(2, 4).must_be_kind_of Array + @admin.find_rooms_from_block(2, 4).first.must_equal 1 + @admin.find_rooms_from_block(2, 4).last.must_equal 4 + @admin.find_rooms_from_block(2, 4).first.wont_equal 0 + @admin.find_rooms_from_block(2, 4).last.wont_equal 99 + + end + end + + describe "add_reservation_to_block" do + + end + end end # end admin class +test_ob = Hotel::Admin.new + + + + + +puts "List of available rooms #{test_ob.list_vacancies(Date.new(2017,2,3), Date.new(2017, 2,7))} + +" + +#second block +puts test_ob.create_block_by_date(4, Date.new(2017,2,3), Date.new(2017, 2,7), 2) + +puts "List of available rooms after second block is created. #{test_ob.list_vacancies(Date.new(2017,2,3), Date.new(2017, 2,7))} + +" + +# +# puts "list_available_blocked_rooms #{test_ob.list_available_blocked_rooms(1)} +# +# " +# puts "finding rooms from block #{test_ob.find_rooms_from_block(1, 2)} +# +# " +# +# +# puts "Adding #{test_ob.add_reservation_to_block(1, 2, Date.new(2017,2,3), Date.new(2017, 2,7))} reservation to block +# +# " +# puts "list of reservations #{test_ob.list_reservations(Date.new(2017,2,3))} +# +# " + +puts "testing list_available_blocked_rooms" +puts test_ob.list_available_blocked_rooms(1) + =begin Reservations From e8f505b8163997ec54f6e81071a0ecf77fc1f413 Mon Sep 17 00:00:00 2001 From: Angela Wilson Date: Sun, 1 Oct 2017 14:58:48 -0700 Subject: [PATCH 13/19] try to refactor DateRange class to instructional staff's recommendations. Logic was a bit too much for me to grasp alone. I Will ask for help. So I decided to finish writing my tests and then will go back and refactor. --- design-activity.md | 21 +++++++++ lib/Admin.rb | 109 +++++++++++++++++++++++++++++++++----------- lib/Block.rb | 8 ++-- lib/DateRange.rb | 10 +++- specs/admin_spec.rb | 59 ++++++++++++------------ specs/block_spec.rb | 9 ++++ 6 files changed, 156 insertions(+), 60 deletions(-) create mode 100644 design-activity.md diff --git a/design-activity.md b/design-activity.md new file mode 100644 index 000000000..75a3d392d --- /dev/null +++ b/design-activity.md @@ -0,0 +1,21 @@ +What classes does each implementation include? Are the lists the same? + - Each implementation has the same class names: + 1. CartEntry + 2. ShoppingCart + 3. Order + - The lists, what I interpret as list of classes, is the same. + +Write down a sentence to describe each class. + 1. CartEntry - + 2. ShoppingCart + 3. Order + +How do the classes relate to each other? It might be helpful to draw a diagram on a whiteboard or piece of paper. +What data does each class store? How (if at all) does this differ between the two implementations? +What methods does each class have? How (if at all) does this differ between the two implementations? +Consider the Order#total_price method. In each implementation: +Is logic to compute the price delegated to "lower level" classes like ShoppingCart and CartEntry, or is it retained in Order? +Does total_price directly manipulate the instance variables of other classes? +If we decide items are cheaper if bought in bulk, how would this change the code? Which implementation is easier to modify? +Which implementation better adheres to the single responsibility principle? +Bonus question once you've read Metz ch. 3: Which implementation is more loosely coupled? diff --git a/lib/Admin.rb b/lib/Admin.rb index d89edf7d0..1cc4d67bb 100644 --- a/lib/Admin.rb +++ b/lib/Admin.rb @@ -15,7 +15,8 @@ def initialize @room_nums << (i + 1) end - @reservations_array1 = [] + # @reservations_array1 = [] + end # end initialize @@ -31,22 +32,47 @@ def add_block(room_num_array, check_in, check_out, block_id, discount_percent: 0 end # Wave 2 requirement. Add reservation only no block - # def add_reservation(room_selection, check_in, check_out) - # @reservations << Hotel::Reservation.new(room_selection, check_in, check_out) - # end + def add_reservation(room_selection, check_in, check_out) + # self.add_block() + # self.add_reservation_to_block() + @reservations << Hotel::Reservation.new(room_selection, check_in, check_out) + end def list_reservations(date) rez_by_date = [] + # blocks = @blocks.select do |block| + # block.block_date_range_array.include?(date) + # end + # + # blocks.each do |block| + # block.reservations_array.each do |reserevation| + # rez_by_date << reserevation + # end + # end @blocks.each do |block| - block.reservations_array2.each do |reservation| - rez_by_date << reservation + if block.block_date_range_array.include?(date) + block.reservations_array.each do |reservation| + # return reservation + rez_by_date << reservation + end end end return rez_by_date end + def list_blocks(date) + blocks_by_date = [] + + @blocks.each do |block| + if block.block_date_range_array.include?(date) + blocks_by_date << block + end + end + return blocks_by_date + end + # def list_reservations(date) # rez_by_date = [] # @@ -73,26 +99,56 @@ def list_reservations(date) # end - # lists room vacancies that can be utilized for booking available rooms + # to find the vacancies by date range we need to look at all the blocked rooms and then remove each room from the list + def list_vacancies(check_in, check_out) - available_rooms = list_of_rooms + # available_rooms = list_of_rooms + blocked_rooms = [] date_range = DateRange.new(check_in, check_out).date_range_array date_range.each do |date| - @blocks.each do |block| - if block.block_date_range_array.include?(date) - block.room_num_array.each do |room_num| - available_rooms.delete(room_num) - end - end + + self.list_blocks(date).each do |block| + block.room_num_array end - end # end date_range loop - return available_rooms + # @blocks.each do |block| + + + # if block.block_date_range_array.include?(date) + # block.room_num_array.each do |room_num| + # available_rooms.delete(room_num) + # end + # end + # end + end # end date_range loop + return blocked_rooms + # return available_rooms end + + # lists room vacancies that can be utilized for booking available rooms + # def list_vacancies(check_in, check_out) + # available_rooms = list_of_rooms + # + # date_range = DateRange.new(check_in, check_out).date_range_array + # + # date_range.each do |date| + # @blocks.each do |block| + # if block.block_date_range_array.include?(date) + # block.room_num_array.each do |room_num| + # available_rooms.delete(room_num) + # end + # end + # end + # end # end date_range loop + # + # return available_rooms + # end + # + # creates block with available room for a given date range. def create_block_by_date(rooms_per_block, check_in, check_out, block_id, discount_percent: 0.0) room_num_array = self.list_vacancies(check_in, check_out).take(rooms_per_block).to_a @@ -108,11 +164,9 @@ def find_block(id) end def list_available_blocked_rooms(id) - block_array = self.find_block(id) - block_array.each do |block| - return block.room_num_array - end - + #find the block. + # access its assigned rooms + return self.find_block(id).room_num_array end @@ -127,12 +181,15 @@ def add_reservation_to_block(id, num_rooms_to_reserve, check_in, check_out, disc num_rooms_to_reserve = self.find_rooms_from_block(id, num_rooms_to_reserve) num_rooms_to_reserve.length.times do |room_num| - block.reservations_array << Hotel::Reservation.new(room_num, check_in, check_out, discount_percent: 0) + # block.reservations_array << Hotel::Reservation.new(room_num, check_in, check_out, discount_percent: 0) + block.make_reservation(room_num, check_in, check_out, discount_percent: 0) end end - - - - end # end class end # end module +puts "testing" +a = Hotel::Admin.new +puts a.find_block(1) +puts a.list_blocks(Date.new(2017,2,2)) +puts a.list_vacancies(Date.new(2017,2,2), Date.new(2017,2,7)) +puts "testing" diff --git a/lib/Block.rb b/lib/Block.rb index 2d2807fc4..9d675cedb 100644 --- a/lib/Block.rb +++ b/lib/Block.rb @@ -6,7 +6,7 @@ module Hotel class Block COST_PER_NIGHT = 200.00 - attr_reader :room_num_array, :block_date_range_array, :discount_percent, :block_id, :reservations_array2 + attr_reader :room_num_array, :block_date_range_array, :discount_percent, :block_id, :reservations_array def initialize(room_num_array, check_in, check_out, block_id, discount_percent: 0) @@ -18,7 +18,7 @@ def initialize(room_num_array, check_in, check_out, block_id, discount_percent: @discount_percent = discount_percent - @reservations_array2 = [] + @reservations_array = [] end @@ -30,7 +30,9 @@ def total_cost end - + def make_reservation(room_num, check_in, check_out, discount_percent: 0) + reservations_array << Hotel::Reservation.new(room_num, check_in, check_out, discount_percent: 0) + end diff --git a/lib/DateRange.rb b/lib/DateRange.rb index afea7326f..6574affc8 100644 --- a/lib/DateRange.rb +++ b/lib/DateRange.rb @@ -15,10 +15,16 @@ def initialize(check_in, check_out) end - # def include?(date) - # reserevation.include?(Date.new(2017,2,2)) + # def include(date) + # if date >= @check_in && date <= @check_out + # return true + # end # end + def overlap?(other) + return !(other.checkout <= @checkin || other.checkin >= @checkout) + end + diff --git a/specs/admin_spec.rb b/specs/admin_spec.rb index ffe3ce929..4ae24dd42 100644 --- a/specs/admin_spec.rb +++ b/specs/admin_spec.rb @@ -35,6 +35,7 @@ @admin.create_block_by_date(rooms_per_block, check_in, check_out, block_id, discount_percent: 0.0) + end describe "initialize" do @@ -52,10 +53,10 @@ @admin.room_nums.last.must_equal 20 # edge classes - @admin.room_nums.first.wont_equal 0 - @admin.room_nums.last.wont_equal 21 - @admin.room_nums.first.wont_be_instance_of String - @admin.room_nums.last.wont_be_instance_of String + # @admin.room_nums.first.wont_equal 0 + # @admin.room_nums.last.wont_equal 21 + # @admin.room_nums.first.wont_be_instance_of String + # @admin.room_nums.last.wont_be_instance_of String end describe "List_of_rooms method" do @@ -68,10 +69,10 @@ @admin.list_of_rooms.last.must_equal 20 # edge classes - @admin.list_of_rooms.first.wont_equal 0 - @admin.list_of_rooms.last.wont_equal 21 - @admin.room_nums.first.wont_be_instance_of String - @admin.room_nums.last.wont_be_instance_of String + # @admin.list_of_rooms.first.wont_equal 0 + # @admin.list_of_rooms.last.wont_equal 21 + # @admin.room_nums.first.wont_be_instance_of String + # @admin.room_nums.last.wont_be_instance_of String end end end # end initialize @@ -86,7 +87,7 @@ it "verify block is added to array" do @admin.blocks.length.must_equal 1 - @admin.blocks.length.wont_equal 2 + end end @@ -96,6 +97,8 @@ date = Date.new(2017, 2, 4) date.must_be_instance_of Date + test_ob = @admin.add_reservation_to_block(1,1, Date.new(2017,2,2), Date.new(2017,2,7), discount_percent: 0) + @admin.list_reservations(date).must_be_instance_of Array @admin.list_reservations(date).first.must_be_instance_of Hotel::Reservation @admin.list_reservations(date).last.must_be_instance_of Hotel::Reservation @@ -118,7 +121,7 @@ check_in.must_be_instance_of Date @admin.list_vacancies(check_in, check_out).must_be_instance_of Array - @admin.list_vacancies(check_in, check_out)[0].must_be_kind_of Integer + # @admin.list_vacancies(check_in, check_out)[0].must_be_kind_of Integer @admin.list_vacancies(check_in, check_out)[-1].must_be_kind_of Integer # edge cases @@ -126,7 +129,7 @@ @admin.list_vacancies(check_in, check_out).wont_be_kind_of Integer @admin.list_vacancies(check_in, check_out).wont_be_kind_of String end - # + describe "create block by date range" do @@ -143,13 +146,11 @@ block_id = 2 block_id.must_be_kind_of Integer - # @admin.create_block_by_date(rooms_per_block, check_in, check_out,block_id) - # - # @admin.blocks.must_equal 1 + @admin.create_block_by_date(rooms_per_block, check_in, check_out,block_id) @admin.must_respond_to :list_vacancies - @admin.blocks.length.must_equal 1 + @admin.blocks.length.must_equal 2 # @admin.create_block_by_date(rooms_per_block, check_in, check_out, 2).total_cost.must_equal 200 # @admin.create_block_by_date(rooms_per_block, check_in, check_out, 2).wont_equal 0 @@ -176,9 +177,9 @@ describe "list_available_blocked_rooms" do it "returns an array with room numbers" do - @admin.list_available_blocked_rooms(2).must_be_instance_of Array - @admin.list_available_blocked_rooms(2).first.must_be_kind_of Integer - @admin.list_available_blocked_rooms(2).last.must_be_kind_of Integer + @admin.list_available_blocked_rooms(1).must_be_instance_of Array + # @admin.list_available_blocked_rooms(1).first.must_be_instance_of Integer + @admin.list_available_blocked_rooms(1).last.must_be_kind_of Integer #edge cases @@ -190,11 +191,11 @@ describe "find_rooms_from_block" do it "returns array of a specific block's rooms" do - @admin.find_rooms_from_block(2, 4).must_be_kind_of Array - @admin.find_rooms_from_block(2, 4).first.must_equal 1 - @admin.find_rooms_from_block(2, 4).last.must_equal 4 - @admin.find_rooms_from_block(2, 4).first.wont_equal 0 - @admin.find_rooms_from_block(2, 4).last.wont_equal 99 + @admin.find_rooms_from_block(1, 4).must_be_kind_of Array + @admin.find_rooms_from_block(1, 4).first.must_equal 1 + @admin.find_rooms_from_block(1, 4).last.must_equal 4 + # @admin.find_rooms_from_block(2, 4).first.wont_equal 0 + # @admin.find_rooms_from_block(2, 4).last.wont_equal 99 end end @@ -212,10 +213,10 @@ -puts "List of available rooms #{test_ob.list_vacancies(Date.new(2017,2,3), Date.new(2017, 2,7))} - -" - +# puts "List of available rooms #{test_ob.list_vacancies(Date.new(2017,2,3), Date.new(2017, 2,7))} +# +# " +# #second block puts test_ob.create_block_by_date(4, Date.new(2017,2,3), Date.new(2017, 2,7), 2) @@ -239,8 +240,8 @@ # # " -puts "testing list_available_blocked_rooms" -puts test_ob.list_available_blocked_rooms(1) +# puts "testing list_available_blocked_rooms" +# puts test_ob.list_available_blocked_rooms(1) =begin diff --git a/specs/block_spec.rb b/specs/block_spec.rb index 6e9e11bc6..96f6335a2 100644 --- a/specs/block_spec.rb +++ b/specs/block_spec.rb @@ -3,3 +3,12 @@ # test_ob = Hotel::Block.new # # test_ob.add_reservation_to_block + +describe "Block tests" do + describe "initialize" do + it "Returns a block object" do + + end + end # initialize + +end # block tests From 727c36e958d1df7256244c7200d40a3c0e2f6b21 Mon Sep 17 00:00:00 2001 From: Angela Wilson Date: Sun, 1 Oct 2017 17:06:12 -0700 Subject: [PATCH 14/19] Regarding testing: all tests from the first submission now pass. Working towards refactoring and adding more tests for all classes. --- lib/Admin.rb | 73 +++++++++++---------------------------------- specs/admin_spec.rb | 45 ++-------------------------- specs/block_spec.rb | 11 +++++++ 3 files changed, 32 insertions(+), 97 deletions(-) diff --git a/lib/Admin.rb b/lib/Admin.rb index 1cc4d67bb..2e470c0da 100644 --- a/lib/Admin.rb +++ b/lib/Admin.rb @@ -27,11 +27,12 @@ def list_of_rooms - def add_block(room_num_array, check_in, check_out, block_id, discount_percent: 0) - @blocks << Hotel::Block.new(room_num_array, check_in, check_out, block_id, discount_percent: 0) - end + # def add_block(room_num_array, check_in, check_out, block_id, discount_percent: 0) + # @blocks << Hotel::Block.new(room_num_array, check_in, check_out, block_id, discount_percent: 0) + # end # Wave 2 requirement. Add reservation only no block + # Kept this code because wave 2 requires it. As for instructor feedback provided after first submition. Please see the create_block_by_date method on line 122'ish def add_reservation(room_selection, check_in, check_out) # self.add_block() # self.add_reservation_to_block() @@ -41,16 +42,6 @@ def add_reservation(room_selection, check_in, check_out) def list_reservations(date) rez_by_date = [] - - # blocks = @blocks.select do |block| - # block.block_date_range_array.include?(date) - # end - # - # blocks.each do |block| - # block.reservations_array.each do |reserevation| - # rez_by_date << reserevation - # end - # end @blocks.each do |block| if block.block_date_range_array.include?(date) block.reservations_array.each do |reservation| @@ -62,30 +53,17 @@ def list_reservations(date) return rez_by_date end - def list_blocks(date) - blocks_by_date = [] - - @blocks.each do |block| - if block.block_date_range_array.include?(date) - blocks_by_date << block - end - end - return blocks_by_date - end - - # def list_reservations(date) - # rez_by_date = [] + # def list_blocks(date) + # blocks_by_date = [] # - # @reservations_array.each do |reservation| - # if reservation.date_range_array.include?(date) - # rez_by_date << reservation + # @blocks.each do |block| + # if block.block_date_range_array.include?(date) + # blocks_by_date << block # end # end - # - # return rez_by_date + # return blocks_by_date # end - # wave two requirement # def list_reservations(date) # block_by_date = [] @@ -102,29 +80,20 @@ def list_blocks(date) # to find the vacancies by date range we need to look at all the blocked rooms and then remove each room from the list def list_vacancies(check_in, check_out) - # available_rooms = list_of_rooms - blocked_rooms = [] + available_rooms = list_of_rooms date_range = DateRange.new(check_in, check_out).date_range_array date_range.each do |date| - - self.list_blocks(date).each do |block| - block.room_num_array + @blocks.each do |block| + if block.block_date_range_array.include?(date) + block.room_num_array.each do |room_num| + available_rooms.delete(room_num) + end + end end - - # @blocks.each do |block| - - - # if block.block_date_range_array.include?(date) - # block.room_num_array.each do |room_num| - # available_rooms.delete(room_num) - # end - # end - # end end # end date_range loop - return blocked_rooms - # return available_rooms + return available_rooms end @@ -187,9 +156,3 @@ def add_reservation_to_block(id, num_rooms_to_reserve, check_in, check_out, disc end end # end class end # end module -puts "testing" -a = Hotel::Admin.new -puts a.find_block(1) -puts a.list_blocks(Date.new(2017,2,2)) -puts a.list_vacancies(Date.new(2017,2,2), Date.new(2017,2,7)) -puts "testing" diff --git a/specs/admin_spec.rb b/specs/admin_spec.rb index 4ae24dd42..3966b1410 100644 --- a/specs/admin_spec.rb +++ b/specs/admin_spec.rb @@ -22,7 +22,6 @@ rooms_per_block = 4 rooms_per_block.must_be_kind_of Integer - rooms_per_block.wont_be_instance_of String check_in = Date.new(2017,2,3) check_in.must_be_instance_of Date @@ -34,6 +33,7 @@ @admin.create_block_by_date(rooms_per_block, check_in, check_out, block_id, discount_percent: 0.0) + @admin.add_reservation_to_block(1, 1, check_in, check_out, discount_percent: 0) end @@ -178,14 +178,13 @@ describe "list_available_blocked_rooms" do it "returns an array with room numbers" do @admin.list_available_blocked_rooms(1).must_be_instance_of Array - # @admin.list_available_blocked_rooms(1).first.must_be_instance_of Integer + @admin.list_available_blocked_rooms(1).first.must_be_kind_of Integer @admin.list_available_blocked_rooms(1).last.must_be_kind_of Integer #edge cases - @admin.list_available_blocked_rooms(2).first.wont_be_kind_of String - @admin.list_available_blocked_rooms(2).last.wont_be_kind_of String + end end @@ -194,9 +193,6 @@ @admin.find_rooms_from_block(1, 4).must_be_kind_of Array @admin.find_rooms_from_block(1, 4).first.must_equal 1 @admin.find_rooms_from_block(1, 4).last.must_equal 4 - # @admin.find_rooms_from_block(2, 4).first.wont_equal 0 - # @admin.find_rooms_from_block(2, 4).last.wont_equal 99 - end end @@ -207,41 +203,6 @@ end end # end admin class -test_ob = Hotel::Admin.new - - - - - -# puts "List of available rooms #{test_ob.list_vacancies(Date.new(2017,2,3), Date.new(2017, 2,7))} -# -# " -# -#second block -puts test_ob.create_block_by_date(4, Date.new(2017,2,3), Date.new(2017, 2,7), 2) - -puts "List of available rooms after second block is created. #{test_ob.list_vacancies(Date.new(2017,2,3), Date.new(2017, 2,7))} - -" - -# -# puts "list_available_blocked_rooms #{test_ob.list_available_blocked_rooms(1)} -# -# " -# puts "finding rooms from block #{test_ob.find_rooms_from_block(1, 2)} -# -# " -# -# -# puts "Adding #{test_ob.add_reservation_to_block(1, 2, Date.new(2017,2,3), Date.new(2017, 2,7))} reservation to block -# -# " -# puts "list of reservations #{test_ob.list_reservations(Date.new(2017,2,3))} -# -# " - -# puts "testing list_available_blocked_rooms" -# puts test_ob.list_available_blocked_rooms(1) =begin diff --git a/specs/block_spec.rb b/specs/block_spec.rb index 96f6335a2..f8b0b86cd 100644 --- a/specs/block_spec.rb +++ b/specs/block_spec.rb @@ -5,10 +5,21 @@ # test_ob.add_reservation_to_block describe "Block tests" do + before do + @block = Hotel::Block.new([1,2,3,4], Date.new(2017,2,2), Date.new(2017,2, 7), discount_percent: 0) + @block.make_reservation(1, Date.new(2017,2,2), Date.new(2017,2, 7), discount_percent: 0) + end + describe "initialize" do it "Returns a block object" do + @block.must_be_instance_of Hotel::Block end end # initialize + it "adds to reservations_array" do + @block.reservations_array.must_be_instance_of Array + @block.reservations_array.length.must_equal 1 + end + end # block tests From 1392f27e8dc18b4105b43b435ff5549c19bbc7e6 Mon Sep 17 00:00:00 2001 From: Angela Wilson Date: Sun, 1 Oct 2017 18:33:05 -0700 Subject: [PATCH 15/19] Add test for calculating both full and discount price --- lib/Admin.rb | 46 ++++++++------------------------------------- lib/Block.rb | 2 +- specs/admin_spec.rb | 15 +++++++-------- specs/block_spec.rb | 32 ++++++++++++++++++++++++++++++- 4 files changed, 47 insertions(+), 48 deletions(-) diff --git a/lib/Admin.rb b/lib/Admin.rb index 2e470c0da..ea7700d3f 100644 --- a/lib/Admin.rb +++ b/lib/Admin.rb @@ -26,13 +26,13 @@ def list_of_rooms end - + # I do not remember why I had this method. Kept it for future reference. # def add_block(room_num_array, check_in, check_out, block_id, discount_percent: 0) # @blocks << Hotel::Block.new(room_num_array, check_in, check_out, block_id, discount_percent: 0) # end # Wave 2 requirement. Add reservation only no block - # Kept this code because wave 2 requires it. As for instructor feedback provided after first submition. Please see the create_block_by_date method on line 122'ish + # Kept this code because wave 2 requires it. As for instructor feedback provided after first submition. Please see the create_block_by_date method on line 87'ish def add_reservation(room_selection, check_in, check_out) # self.add_block() # self.add_reservation_to_block() @@ -45,7 +45,6 @@ def list_reservations(date) @blocks.each do |block| if block.block_date_range_array.include?(date) block.reservations_array.each do |reservation| - # return reservation rez_by_date << reservation end end @@ -64,18 +63,6 @@ def list_reservations(date) # return blocks_by_date # end - # wave two requirement - # def list_reservations(date) - # block_by_date = [] - # - # @blocks.each do |block| - # if block.date_range_array.include?(date) - # block_by_date << block - # end - # end - # return block_by_date - # end - # to find the vacancies by date range we need to look at all the blocked rooms and then remove each room from the list @@ -96,31 +83,15 @@ def list_vacancies(check_in, check_out) return available_rooms end - - - # lists room vacancies that can be utilized for booking available rooms - # def list_vacancies(check_in, check_out) - # available_rooms = list_of_rooms - # - # date_range = DateRange.new(check_in, check_out).date_range_array - # - # date_range.each do |date| - # @blocks.each do |block| - # if block.block_date_range_array.include?(date) - # block.room_num_array.each do |room_num| - # available_rooms.delete(room_num) - # end - # end - # end - # end # end date_range loop - # - # return available_rooms - # end - # - # creates block with available room for a given date range. def create_block_by_date(rooms_per_block, check_in, check_out, block_id, discount_percent: 0.0) + while self.list_vacancies(check_in, check_out).empty? + raise StandardError("No more vacancies!") + end + room_num_array = self.list_vacancies(check_in, check_out).take(rooms_per_block).to_a + + @blocks << Hotel::Block.new(room_num_array, check_in, check_out, block_id, discount_percent: 0.0) end @@ -144,7 +115,6 @@ def find_rooms_from_block(id, num_rooms_to_reserve) end - def add_reservation_to_block(id, num_rooms_to_reserve, check_in, check_out, discount_percent: 0) block = self.find_block(id) num_rooms_to_reserve = self.find_rooms_from_block(id, num_rooms_to_reserve) diff --git a/lib/Block.rb b/lib/Block.rb index 9d675cedb..a8c3143de 100644 --- a/lib/Block.rb +++ b/lib/Block.rb @@ -31,7 +31,7 @@ def total_cost def make_reservation(room_num, check_in, check_out, discount_percent: 0) - reservations_array << Hotel::Reservation.new(room_num, check_in, check_out, discount_percent: 0) + reservations_array << Hotel::Reservation.new(room_num, check_in, check_out, discount_percent: 0) end diff --git a/specs/admin_spec.rb b/specs/admin_spec.rb index 3966b1410..3c2c0e3ba 100644 --- a/specs/admin_spec.rb +++ b/specs/admin_spec.rb @@ -106,9 +106,7 @@ # edge cases date = Date.new(2017, 2, 9) date.must_be_instance_of Date - @admin.list_reservations(date).wont_be_instance_of Hotel::Reservation - @admin.list_reservations(date).wont_be_kind_of Integer - @admin.list_reservations(date).wont_be_kind_of String + end end @@ -125,9 +123,8 @@ @admin.list_vacancies(check_in, check_out)[-1].must_be_kind_of Integer # edge cases - @admin.list_vacancies(check_in, check_out).wont_be_instance_of Hotel::Block - @admin.list_vacancies(check_in, check_out).wont_be_kind_of Integer - @admin.list_vacancies(check_in, check_out).wont_be_kind_of String + + end @@ -151,11 +148,13 @@ @admin.must_respond_to :list_vacancies @admin.blocks.length.must_equal 2 + end - # @admin.create_block_by_date(rooms_per_block, check_in, check_out, 2).total_cost.must_equal 200 - # @admin.create_block_by_date(rooms_per_block, check_in, check_out, 2).wont_equal 0 + it "Raise an error for no more available rooms" do + proc { Hotel::Admin.create_block_by_date(rooms_per_block, check_in, check_out, block_id)}.must_raise StandardError end + describe "find_block" do it "takes integer id" do id = 1 diff --git a/specs/block_spec.rb b/specs/block_spec.rb index f8b0b86cd..10f23aff1 100644 --- a/specs/block_spec.rb +++ b/specs/block_spec.rb @@ -6,7 +6,7 @@ describe "Block tests" do before do - @block = Hotel::Block.new([1,2,3,4], Date.new(2017,2,2), Date.new(2017,2, 7), discount_percent: 0) + @block = Hotel::Block.new([1,2,3,4], Date.new(2017,2,2), Date.new(2017,2, 7),1) @block.make_reservation(1, Date.new(2017,2,2), Date.new(2017,2, 7), discount_percent: 0) end @@ -22,4 +22,34 @@ @block.reservations_array.length.must_equal 1 end + describe "total_cost method" do + it "calculating both full and discount price" do + @block.total_cost.must_equal 1000 + + Hotel::Block.new([1,2,3,4], Date.new(2017,2,2), Date.new(2017,2, 7), 1, discount_percent: 30.0).total_cost.must_equal 700 + + end + end + end # block tests + + + + + + + + + + + + + + + + + + + + +puts From 37f305dc242d63a41b0307e0406b5649efeb8a41 Mon Sep 17 00:00:00 2001 From: Angela Wilson Date: Sun, 1 Oct 2017 19:48:11 -0700 Subject: [PATCH 16/19] Admin Class: Refactor create_block_... method to automatically create one reservation. Refactor add_reservation_to_block method to loosely couple the Admin and Block classes. Tests write for both refactors. --- lib/Admin.rb | 10 +++++++++- specs/admin_spec.rb | 39 ++++++++++++++++++++++++--------------- 2 files changed, 33 insertions(+), 16 deletions(-) diff --git a/lib/Admin.rb b/lib/Admin.rb index ea7700d3f..aa39b1d93 100644 --- a/lib/Admin.rb +++ b/lib/Admin.rb @@ -91,8 +91,16 @@ def create_block_by_date(rooms_per_block, check_in, check_out, block_id, discoun room_num_array = self.list_vacancies(check_in, check_out).take(rooms_per_block).to_a + # @blocks << Hotel::Block.new(room_num_array, check_in, check_out, block_id, discount_percent: 0.0) - @blocks << Hotel::Block.new(room_num_array, check_in, check_out, block_id, discount_percent: 0.0) + # take one of the rooms from the room_num_array to make a reservation + room_num = room_num_array.first + + new_block = Hotel::Block.new(room_num_array, check_in, check_out, block_id, discount_percent: 0.0) + + new_block.make_reservation(room_num, check_in, check_out, discount_percent: 0) + + @blocks << new_block end def find_block(id) diff --git a/specs/admin_spec.rb b/specs/admin_spec.rb index 3c2c0e3ba..41f1a08c9 100644 --- a/specs/admin_spec.rb +++ b/specs/admin_spec.rb @@ -31,9 +31,9 @@ block_id = 1 - @admin.create_block_by_date(rooms_per_block, check_in, check_out, block_id, discount_percent: 0.0) + @new_block = @admin.create_block_by_date(rooms_per_block, check_in, check_out, block_id, discount_percent: 0.0) - @admin.add_reservation_to_block(1, 1, check_in, check_out, discount_percent: 0) + # @admin.add_reservation_to_block(1, 1, check_in, check_out, discount_percent: 0) end @@ -53,10 +53,7 @@ @admin.room_nums.last.must_equal 20 # edge classes - # @admin.room_nums.first.wont_equal 0 - # @admin.room_nums.last.wont_equal 21 - # @admin.room_nums.first.wont_be_instance_of String - # @admin.room_nums.last.wont_be_instance_of String + end describe "List_of_rooms method" do @@ -69,10 +66,6 @@ @admin.list_of_rooms.last.must_equal 20 # edge classes - # @admin.list_of_rooms.first.wont_equal 0 - # @admin.list_of_rooms.last.wont_equal 21 - # @admin.room_nums.first.wont_be_instance_of String - # @admin.room_nums.last.wont_be_instance_of String end end end # end initialize @@ -81,14 +74,10 @@ it "thing that is added is a block" do @admin.blocks[0].must_be_instance_of Hotel::Block @admin.blocks[-1].must_be_instance_of Hotel::Block - - end it "verify block is added to array" do @admin.blocks.length.must_equal 1 - - end end @@ -150,6 +139,12 @@ @admin.blocks.length.must_equal 2 end + it "automatically makes one reservation for each block" do + @admin.blocks.length.must_equal 1 + + @admin.blocks.first.reservations_array.length.must_equal 1 + end + it "Raise an error for no more available rooms" do proc { Hotel::Admin.create_block_by_date(rooms_per_block, check_in, check_out, block_id)}.must_raise StandardError end @@ -196,9 +191,23 @@ end describe "add_reservation_to_block" do + it "reservation is added to array" do - end + check_in = Date.new(2017,2,3) + check_out = Date.new(2017, 2,7) + + @admin.add_reservation_to_block(1, 1, check_in, check_out, discount_percent: 0) + + # One reservation was already in the reservations_array so total is now 2 + @admin.blocks.first.reservations_array.length.must_equal 2 + + @admin.blocks.first.reservations_array.first.must_be_instance_of Hotel::Reservation + @admin.blocks.first.reservations_array.last.must_be_instance_of Hotel::Reservation + + + end + end end end # end admin class From 9f6caa3bcd234641698785a022dfa1c42908b981 Mon Sep 17 00:00:00 2001 From: Angela Wilson Date: Sun, 1 Oct 2017 21:39:16 -0700 Subject: [PATCH 17/19] complete the desigh_assignemnt. Some original feedback comments were not addressed for Hotel Revisited. However, the comments I did address deepened my understanding. --- design-activity.md | 23 ++++++++++++++++++++--- lib/Admin.rb | 20 +++----------------- lib/DateRange.rb | 2 +- lib/Reservation.rb | 12 +++++++++--- specs/admin_spec.rb | 29 +++++++++++++++++++---------- specs/reservation_spec.rb | 18 ++++++------------ 6 files changed, 58 insertions(+), 46 deletions(-) diff --git a/design-activity.md b/design-activity.md index 75a3d392d..723e5eaa5 100644 --- a/design-activity.md +++ b/design-activity.md @@ -6,16 +6,33 @@ What classes does each implementation include? Are the lists the same? - The lists, what I interpret as list of classes, is the same. Write down a sentence to describe each class. - 1. CartEntry - - 2. ShoppingCart - 3. Order + 1. CartEntry - Responsible for creating entries for the cart + 2. ShoppingCart - responsible for making carts that can hold entries and their prices + 3. Order - calculates the shopping carts' entries total price How do the classes relate to each other? It might be helpful to draw a diagram on a whiteboard or piece of paper. + - Order relies on ShoppingCart and ShoppingCart relies on CartEntry + What data does each class store? How (if at all) does this differ between the two implementations? + - From what I can tell, each implementation's classes store the same thing. ShoppingCart = entries and order = shopping cart instances. + What methods does each class have? How (if at all) does this differ between the two implementations? + + 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? + - Imp A delegated to both ShoppingCart and CartEntry by utilizing their instance variables + - Imp B did not, it was wrapped up nicely so that only messages were sent not responsibilities. + Does total_price directly manipulate the instance variables of other classes? + - Imp A yes. it manipulates CartEntry#unit_price and CartEntry#quantity + - Imp B does not. + If we decide items are cheaper if bought in bulk, how would this change the code? Which implementation is easier to modify? + - Imp B is easier to change than Imp A. We would add the logic to CartEntry#price + Which implementation better adheres to the single responsibility principle? + - Imp B + Bonus question once you've read Metz ch. 3: Which implementation is more loosely coupled? + - Imp B diff --git a/lib/Admin.rb b/lib/Admin.rb index aa39b1d93..c604133e5 100644 --- a/lib/Admin.rb +++ b/lib/Admin.rb @@ -52,20 +52,6 @@ def list_reservations(date) return rez_by_date end - # def list_blocks(date) - # blocks_by_date = [] - # - # @blocks.each do |block| - # if block.block_date_range_array.include?(date) - # blocks_by_date << block - # end - # end - # return blocks_by_date - # end - - - # to find the vacancies by date range we need to look at all the blocked rooms and then remove each room from the list - def list_vacancies(check_in, check_out) available_rooms = list_of_rooms @@ -83,7 +69,7 @@ def list_vacancies(check_in, check_out) return available_rooms end - # creates block with available room for a given date range. + # creates block with available room for a given date range. Dan, I read you original feedback about the discount_percent passing through to calc price and ran out of time for testing. Now I worked on it for a while and I was not able to solve it. I will ask Charles for help. def create_block_by_date(rooms_per_block, check_in, check_out, block_id, discount_percent: 0.0) while self.list_vacancies(check_in, check_out).empty? raise StandardError("No more vacancies!") @@ -91,8 +77,6 @@ def create_block_by_date(rooms_per_block, check_in, check_out, block_id, discoun room_num_array = self.list_vacancies(check_in, check_out).take(rooms_per_block).to_a - # @blocks << Hotel::Block.new(room_num_array, check_in, check_out, block_id, discount_percent: 0.0) - # take one of the rooms from the room_num_array to make a reservation room_num = room_num_array.first @@ -111,6 +95,8 @@ def find_block(id) end end + + # Dan I saw your original feedback about the below method not being necessary. My thought at the time was single responsibility for this method and then utiling it in the find_rooms_from_block method. If my thinking is incorrect, please let me know. def list_available_blocked_rooms(id) #find the block. # access its assigned rooms diff --git a/lib/DateRange.rb b/lib/DateRange.rb index 6574affc8..6eac0fd75 100644 --- a/lib/DateRange.rb +++ b/lib/DateRange.rb @@ -7,7 +7,7 @@ class DateRange attr_reader :check_in, :check_out, :date_range_array def initialize(check_in, check_out) - raise ArgumentError.new("Please enter a Check Out date that comes after Check In date.") if check_in > check_out + raise ArgumentError.new("Please enter a Check Out date that comes after Check In date.") if check_in >= check_out @check_in = check_in @check_out = check_out diff --git a/lib/Reservation.rb b/lib/Reservation.rb index 717a094a3..87b853fe2 100644 --- a/lib/Reservation.rb +++ b/lib/Reservation.rb @@ -16,12 +16,18 @@ def initialize(room_num, check_in, check_out, discount_percent: 0) @date_range_array = DateRange.new(check_in, check_out).date_range_array end - - # def total_cost - # (@date_ranges_array.length - 1) * COST_PER_NIGHT + # full_price = (@block_date_range_array.length - 1) * COST_PER_NIGHT + # discount = full_price * (discount_percent/100.0) + # return total_cost = full_price - discount # end + def total_cost + full_price = (@date_range_array.length - 1) * COST_PER_NIGHT + discount = full_price * (discount_percent/100.0) + return total_cost = full_price - discount + end + end # end class diff --git a/specs/admin_spec.rb b/specs/admin_spec.rb index 41f1a08c9..495474636 100644 --- a/specs/admin_spec.rb +++ b/specs/admin_spec.rb @@ -113,6 +113,17 @@ # edge cases + # I read original feedback "What if there are no reservations? (it should return a list of all the rooms)" and was not able to figure it out. Will follow up for clarification soon. + # @admin.list_vacancies(Date.new(2017,1,1), Date.new(2017,1,3)).must_equal[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20] + + @admin.list_vacancies(check_in, check_out).must_equal [5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20] + + @admin.create_block_by_date(16, check_in, check_out, 2) + + @admin.list_vacancies(check_in, check_out).must_equal [] + + + end @@ -193,20 +204,18 @@ describe "add_reservation_to_block" do it "reservation is added to array" do - check_in = Date.new(2017,2,3) - check_out = Date.new(2017, 2,7) - - @admin.add_reservation_to_block(1, 1, check_in, check_out, discount_percent: 0) - - # One reservation was already in the reservations_array so total is now 2 - @admin.blocks.first.reservations_array.length.must_equal 2 + check_in = Date.new(2017,2,3) + check_out = Date.new(2017, 2,7) - @admin.blocks.first.reservations_array.first.must_be_instance_of Hotel::Reservation + @admin.add_reservation_to_block(1, 1, check_in, check_out, discount_percent: 0) - @admin.blocks.first.reservations_array.last.must_be_instance_of Hotel::Reservation + # One reservation was already in the reservations_array so total is now 2 + @admin.blocks.first.reservations_array.length.must_equal 2 + @admin.blocks.first.reservations_array.first.must_be_instance_of Hotel::Reservation - end + @admin.blocks.first.reservations_array.last.must_be_instance_of Hotel::Reservation + end end end end # end admin class diff --git a/specs/reservation_spec.rb b/specs/reservation_spec.rb index bd0a0e07d..182ce577a 100644 --- a/specs/reservation_spec.rb +++ b/specs/reservation_spec.rb @@ -12,22 +12,16 @@ describe "initialize" do it "Returns a Reservation object" do @reservation.must_be_instance_of Hotel::Reservation - - end + end + xdescribe "total_cost method" do + it "calculating both full and discount price" do + @reservation.total_cost.must_equal 1000 + Hotel::Reservation.new(2, Date.new(2017,2,2), Date.new(2017,2, 7), 1, discount_percent: 30.0).total_cost.must_equal 700 + end end - # describe "total_cost method" do - # it "total_cost method is an integer" do - # @reservation.total_cost.must_equal 800.00 - # end - # - # it "total_cost method is an integer" do - # @reservation.total_cost.must_equal 800.00 - # end - # end - end From fbe5f24704db3d2f98563b841eb1bf8381af9660 Mon Sep 17 00:00:00 2001 From: Angela Wilson Date: Mon, 2 Oct 2017 18:56:27 -0700 Subject: [PATCH 18/19] Refactored classes and methods that depended on DateRange instance variable @date_range_array. Now we have loosely coupled classes. DateRange#overlap and DateRange#include are up and running. --- design-activity.md | 6 ++++++ lib/Admin.rb | 10 +++++----- lib/Block.rb | 6 +++--- lib/DateRange.rb | 15 +++++++++------ specs/date_range_spec.rb | 15 ++++++++++++++- 5 files changed, 37 insertions(+), 15 deletions(-) diff --git a/design-activity.md b/design-activity.md index 723e5eaa5..f17b508c8 100644 --- a/design-activity.md +++ b/design-activity.md @@ -36,3 +36,9 @@ Which implementation better adheres to the single responsibility principle? Bonus question once you've read Metz ch. 3: Which implementation is more loosely coupled? - Imp B + +____________________________________ + +My Hotel + +Hotel::Admin#add_reservation_to_block method was tightly coupled to Block class per Dan's comment. So I refactored the code so only messages were sent between the two classes and those loosely coupled them. For example, I added the instance reservation directly to Block#reservation_array and this was a poor idea. diff --git a/lib/Admin.rb b/lib/Admin.rb index c604133e5..c30e2d343 100644 --- a/lib/Admin.rb +++ b/lib/Admin.rb @@ -43,7 +43,7 @@ def add_reservation(room_selection, check_in, check_out) def list_reservations(date) rez_by_date = [] @blocks.each do |block| - if block.block_date_range_array.include?(date) + if block.block_date_range.include(date) block.reservations_array.each do |reservation| rez_by_date << reservation end @@ -55,17 +55,17 @@ def list_reservations(date) def list_vacancies(check_in, check_out) available_rooms = list_of_rooms - date_range = DateRange.new(check_in, check_out).date_range_array + date_range = DateRange.new(check_in, check_out) + - date_range.each do |date| @blocks.each do |block| - if block.block_date_range_array.include?(date) + if block.block_date_range.overlap?(date_range) block.room_num_array.each do |room_num| available_rooms.delete(room_num) end end end - end # end date_range loop + return available_rooms end diff --git a/lib/Block.rb b/lib/Block.rb index a8c3143de..8f982a335 100644 --- a/lib/Block.rb +++ b/lib/Block.rb @@ -6,13 +6,13 @@ module Hotel class Block COST_PER_NIGHT = 200.00 - attr_reader :room_num_array, :block_date_range_array, :discount_percent, :block_id, :reservations_array + attr_reader :room_num_array, :block_date_range, :discount_percent, :block_id, :reservations_array def initialize(room_num_array, check_in, check_out, block_id, discount_percent: 0) @room_num_array = room_num_array - @block_date_range_array = DateRange.new(check_in, check_out).date_range_array + @block_date_range = DateRange.new(check_in, check_out) # what to automate block_id and was stumped on implementation. So I opted for manuel entry @block_id = block_id @discount_percent = discount_percent @@ -24,7 +24,7 @@ def initialize(room_num_array, check_in, check_out, block_id, discount_percent: def total_cost - full_price = (@block_date_range_array.length - 1) * COST_PER_NIGHT + full_price = (@block_date_range.total_num_of_nights) * COST_PER_NIGHT discount = full_price * (discount_percent/100.0) return total_cost = full_price - discount end diff --git a/lib/DateRange.rb b/lib/DateRange.rb index 6eac0fd75..9742543b5 100644 --- a/lib/DateRange.rb +++ b/lib/DateRange.rb @@ -15,16 +15,19 @@ def initialize(check_in, check_out) end - # def include(date) - # if date >= @check_in && date <= @check_out - # return true - # end - # end + def include(date) + if date >= @check_in && date <= @check_out + return true + end + end def overlap?(other) - return !(other.checkout <= @checkin || other.checkin >= @checkout) + return !(other.check_out <= @check_in || other.check_in >= @check_out) end + def total_num_of_nights + return check_out - check_in + end diff --git a/specs/date_range_spec.rb b/specs/date_range_spec.rb index e6ec17b74..80d11aa01 100644 --- a/specs/date_range_spec.rb +++ b/specs/date_range_spec.rb @@ -47,8 +47,21 @@ end # end initialize describe "over lap method" do - it "text" do + it "returns true if other date range overlaps self" do + check_in = Date.new(2017,2,2) + check_out = check_in + 2 + other = Hotel::DateRange.new(check_in, check_out) + + @date_range.overlap?(other).must_equal true + end + end + + describe "total_num_of_nights" do + it "calc is correct" do + @date_range.total_num_of_nights.must_equal 4 + + # interesting test case is check in and check out are same day. end end From 980c8996e30300a1bb2ff0b4fdb88d0afe61c66f Mon Sep 17 00:00:00 2001 From: Angela Wilson Date: Mon, 2 Oct 2017 19:10:33 -0700 Subject: [PATCH 19/19] Completely refactored code so functionality is not dependent on DateRange#date_range_array method and completely removed it. --- lib/DateRange.rb | 3 +-- lib/Reservation.rb | 6 +++--- specs/date_range_spec.rb | 26 +++++++++++++++++++++++--- 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/lib/DateRange.rb b/lib/DateRange.rb index 9742543b5..b94713b54 100644 --- a/lib/DateRange.rb +++ b/lib/DateRange.rb @@ -4,14 +4,13 @@ module Hotel class DateRange - attr_reader :check_in, :check_out, :date_range_array + attr_reader :check_in, :check_out def initialize(check_in, check_out) raise ArgumentError.new("Please enter a Check Out date that comes after Check In date.") if check_in >= check_out @check_in = check_in @check_out = check_out - @date_range_array = (check_in..check_out).to_a end diff --git a/lib/Reservation.rb b/lib/Reservation.rb index 87b853fe2..8755bfe58 100644 --- a/lib/Reservation.rb +++ b/lib/Reservation.rb @@ -6,14 +6,14 @@ module Hotel class Reservation COST_PER_NIGHT = 200.00 - attr_reader :total_cost, :room_num, :date_range_array + attr_reader :total_cost, :room_num, :date_range def initialize(room_num, check_in, check_out, discount_percent: 0) # @reservation_array = [] @total_cost = 0 @room_num = room_num - @date_range_array = DateRange.new(check_in, check_out).date_range_array + @date_range = DateRange.new(check_in, check_out) end # def total_cost @@ -23,7 +23,7 @@ def initialize(room_num, check_in, check_out, discount_percent: 0) # end def total_cost - full_price = (@date_range_array.length - 1) * COST_PER_NIGHT + full_price = (@date_range.total_num_of_nights) * COST_PER_NIGHT discount = full_price * (discount_percent/100.0) return total_cost = full_price - discount end diff --git a/specs/date_range_spec.rb b/specs/date_range_spec.rb index 80d11aa01..0169460b0 100644 --- a/specs/date_range_spec.rb +++ b/specs/date_range_spec.rb @@ -30,9 +30,6 @@ Hotel::DateRange.new(check_in, check_out).must_respond_to :check_out end - it "Returns array of dates in range" do - @date_range.date_range_array.must_be_instance_of Array - end it "Raises an error for invalid date ranges" do check_in = Date.new(2017, 2, 3) @@ -61,6 +58,29 @@ it "calc is correct" do @date_range.total_num_of_nights.must_equal 4 + + + + + + + + # TODO: Add tests for include method. + + + + + + + + + + + + + + + # interesting test case is check in and check out are same day. end end