From 0649ed03a1ac51ca199c7a615a5f6ca47780f058 Mon Sep 17 00:00:00 2001 From: mulhoo Date: Wed, 4 Mar 2020 16:26:17 -0800 Subject: [PATCH 01/10] Initial stages, no tests --- lib/calendar.rb | 25 +++++++++++++++++++++++++ lib/reservation.rb | 25 +++++++++++++++++++++++++ lib/room.rb | 8 ++++++++ 3 files changed, 58 insertions(+) create mode 100644 lib/calendar.rb create mode 100644 lib/reservation.rb create mode 100644 lib/room.rb diff --git a/lib/calendar.rb b/lib/calendar.rb new file mode 100644 index 000000000..e2bb0239a --- /dev/null +++ b/lib/calendar.rb @@ -0,0 +1,25 @@ +require 'date' + +class Calendar + + def initialize (calendar) + @calendar = calendar + end + + calendar = {} + + def add_to_calendar(reservation) + + if !calendar.key?(reservation[:start_date]) + calendar[reservation[:start_date]] = [] + calendar[reservation[:start_date]] << reservation + elsif @calendar.key?(reservation[:start_date]) + if calendar[reservation[:start_date]].length == 20 + raise NoRoomError.new ("There are no available rooms for the duration of specified dates.") + end + calendar[reservation[:start_date]] << reservation + end + + end + +end \ No newline at end of file diff --git a/lib/reservation.rb b/lib/reservation.rb new file mode 100644 index 000000000..f42e7e809 --- /dev/null +++ b/lib/reservation.rb @@ -0,0 +1,25 @@ +require 'date' +require 'securerandom' + +require_relative 'calendar' +require_relative 'room' + +class Reservation << Calendar + attr_accessor :start_date, :end_date + + def initialize (start_date, end_date) + @start_date = start_date + @end_date = end_date + end + + def reservation(start_date, end_date) + { + :id => (SecureRandom.alphanumeric).slice(0...7).upcase, + :start_date => Date.parse(start_date).iso8601, + :end_date => Date.parse(end_date).iso8601, + :total_nights => (Date.parse(end_date) - Date.parse(start_date)).to_i, + :cost => ((Date.parse(end_date) - Date.parse(start_date)).to_i * 200) + } + end + +end \ No newline at end of file diff --git a/lib/room.rb b/lib/room.rb new file mode 100644 index 000000000..a48d85bbf --- /dev/null +++ b/lib/room.rb @@ -0,0 +1,8 @@ +require 'date' +require_relative 'calendar' +require_relative 'reservation' + +class Room << Calendar + + +end \ No newline at end of file From 6973a01f146f59328b37358595fe489b0269f3f8 Mon Sep 17 00:00:00 2001 From: mulhoo Date: Thu, 5 Mar 2020 11:35:01 -0800 Subject: [PATCH 02/10] updated calendar code, extends to all dates in res --- lib/calendar.rb | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/lib/calendar.rb b/lib/calendar.rb index e2bb0239a..175701846 100644 --- a/lib/calendar.rb +++ b/lib/calendar.rb @@ -11,15 +11,28 @@ def initialize (calendar) def add_to_calendar(reservation) if !calendar.key?(reservation[:start_date]) - calendar[reservation[:start_date]] = [] - calendar[reservation[:start_date]] << reservation - elsif @calendar.key?(reservation[:start_date]) - if calendar[reservation[:start_date]].length == 20 - raise NoRoomError.new ("There are no available rooms for the duration of specified dates.") + reservation[:total_nights].times do |i| + i += 0 + calendar[(Date.parse(reservation[:start_date]) + i).iso8601] = [] + calendar[(Date.parse(reservation[:start_date]) + i).iso8601] << reservation end - calendar[reservation[:start_date]] << reservation - end - end + elsif calendar.key?(reservation[:start_date]) + reservation[:total_nights].times do |i| + i += 0 + + if !calendar.key?((Date.parse(reservation[:start_date]) + i).iso8601) + calendar[(Date.parse(reservation[:start_date]) + i).iso8601]= [] + calendar[(Date.parse(reservation[:start_date]) + i).iso8601] << reservation + + else + if reservation[:start_date].length == 20 + raise NoRoomError.new ("There are no available rooms for the duration of specified dates.") + end + calendar[(Date.parse(reservation[:start_date]) + i).iso8601] << reservation + end + end + end + end end \ No newline at end of file From 4221067290f5ab1f38d593e1a8337fa18fe4da9f Mon Sep 17 00:00:00 2001 From: mulhoo Date: Fri, 6 Mar 2020 14:49:28 -0800 Subject: [PATCH 03/10] updated lib, including tests --- Rakefile | 2 +- lib/calendar.rb | 79 +++++++++++++++++++++++++++------------- lib/reservation.rb | 5 ++- lib/room.rb | 3 +- test/calendar_spec.rb | 28 ++++++++++++++ test/reservation_spec.rb | 0 test/room_spec.rb | 0 test/test_helper.rb | 4 +- 8 files changed, 90 insertions(+), 31 deletions(-) create mode 100644 test/calendar_spec.rb create mode 100644 test/reservation_spec.rb create mode 100644 test/room_spec.rb diff --git a/Rakefile b/Rakefile index 0c2d13fe8..3319583aa 100644 --- a/Rakefile +++ b/Rakefile @@ -3,7 +3,7 @@ require 'rake/testtask' Rake::TestTask.new do |t| t.libs = ["lib"] t.warning = true - t.test_files = FileList['test/*_test.rb'] + t.test_files = FileList['test/*_spec.rb'] end task default: :test diff --git a/lib/calendar.rb b/lib/calendar.rb index 175701846..c453e4f24 100644 --- a/lib/calendar.rb +++ b/lib/calendar.rb @@ -1,38 +1,65 @@ require 'date' +require_relative 'reservation' + class Calendar def initialize (calendar) - @calendar = calendar + @date_store = [] + end + + def make_reservation(start_date, end_date) + return 4 + end + + def is_available(room_number, dates) + return 3 + end + + def reservations_on_date(date) + return reservations + end + + def find_by_id(reservation_id) + id = @date_store.find { |reservation| reservation.id === reservation_id} + if id == nil + raise NoReservationError.new("There is no existing reservation with given ID.") + end + + return id + end + + def total_cost_reservation(reservation_id) + reservation = reservations.find_by_id(reservation_id) + return reservation.cost end - calendar = {} def add_to_calendar(reservation) + @date_store << reservation + return @date_store + # if !@date_store.key?(reservation[:start_date]) + # reservation[:total_nights].times do |i| + # i += 0 + # @date_store[(Date.parse(reservation[:start_date]) + i).iso8601] = [] + # @date_store[(Date.parse(reservation[:start_date]) + i).iso8601][reservation[:room_number]] = reservation + # end - if !calendar.key?(reservation[:start_date]) - reservation[:total_nights].times do |i| - i += 0 - calendar[(Date.parse(reservation[:start_date]) + i).iso8601] = [] - calendar[(Date.parse(reservation[:start_date]) + i).iso8601] << reservation - end - - elsif calendar.key?(reservation[:start_date]) - reservation[:total_nights].times do |i| - i += 0 - - if !calendar.key?((Date.parse(reservation[:start_date]) + i).iso8601) - calendar[(Date.parse(reservation[:start_date]) + i).iso8601]= [] - calendar[(Date.parse(reservation[:start_date]) + i).iso8601] << reservation - - else - if reservation[:start_date].length == 20 - raise NoRoomError.new ("There are no available rooms for the duration of specified dates.") - end - calendar[(Date.parse(reservation[:start_date]) + i).iso8601] << reservation - end - - end - end + # elsif @date_store.key?(reservation[:start_date]) + # reservation[:total_nights].times do |i| + # i += 0 + # if !@date_store.key?((Date.parse(reservation[:start_date]) + i).iso8601) + # @date_store[(Date.parse(reservation[:start_date]) + i).iso8601]= [] + # @date_store[(Date.parse(reservation[:start_date]) + i).iso8601][reservation[:room_number]] = reservation + + # else + # if reservation[:start_date].length == 20 + # raise NoRoomError.new ("There are no available rooms for the duration of specified dates.") + # end + # @date_store[(Date.parse(reservation[:start_date]) + i).iso8601][reservation[:room_number]] = reservation + # end + # end + # end end + end \ No newline at end of file diff --git a/lib/reservation.rb b/lib/reservation.rb index f42e7e809..4a403807c 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -2,9 +2,9 @@ require 'securerandom' require_relative 'calendar' -require_relative 'room' +require_relative 'hotel' -class Reservation << Calendar +class Reservation attr_accessor :start_date, :end_date def initialize (start_date, end_date) @@ -12,6 +12,7 @@ def initialize (start_date, end_date) @end_date = end_date end + def reservation(start_date, end_date) { :id => (SecureRandom.alphanumeric).slice(0...7).upcase, diff --git a/lib/room.rb b/lib/room.rb index a48d85bbf..fe7456cbb 100644 --- a/lib/room.rb +++ b/lib/room.rb @@ -2,7 +2,8 @@ require_relative 'calendar' require_relative 'reservation' -class Room << Calendar +class Hotel + room_number = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] end \ No newline at end of file diff --git a/test/calendar_spec.rb b/test/calendar_spec.rb new file mode 100644 index 000000000..b6941b069 --- /dev/null +++ b/test/calendar_spec.rb @@ -0,0 +1,28 @@ +require_relative '../lib/calendar' +require_relative '../lib/reservation' +require_relative 'test_helper' + +describe "Can get the total cost by reservation ID" + before do + reservation1 = Reservation.new("2020/05/04", "2020/05/07") + reservation2 = Reservation.new("2020/05/07", "2020/05/09") + reservation3 = Reservation.new("2020/05/06", "2020/05/11") + + calendar = Calendar.new + + calendar.add_to_calendar(reservation1) + calendar.add_to_calendar(reservation2) + calendar.add_to_calendar(reservation3) + end + + it "gives correct cost of requested reservation" + id = reservation1.id + cost = calendar.total_cost_reservation(id) + + expect reservation1.cost.must_be cost + end + + + +end + diff --git a/test/reservation_spec.rb b/test/reservation_spec.rb new file mode 100644 index 000000000..e69de29bb diff --git a/test/room_spec.rb b/test/room_spec.rb new file mode 100644 index 000000000..e69de29bb diff --git a/test/test_helper.rb b/test/test_helper.rb index c3a7695cf..db45161a8 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -5,4 +5,6 @@ Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new -# require_relative your lib files here! +require_relative '../lib/calendar' +require_relative '../lib/reservation' +require_relative '../lib/room' From f7b91cce1f2d7756d5c30260e18c6a687ed3c988 Mon Sep 17 00:00:00 2001 From: mulhoo Date: Fri, 6 Mar 2020 15:47:32 -0800 Subject: [PATCH 04/10] Calendar class updated, tests added for updates --- lib/calendar.rb | 56 +++++++++++++++++++++---------------------- lib/reservation.rb | 23 ++++++++++-------- lib/room.rb | 3 +-- test/calendar_spec.rb | 33 +++++++++++++------------ test/test_helper.rb | 2 +- 5 files changed, 61 insertions(+), 56 deletions(-) diff --git a/lib/calendar.rb b/lib/calendar.rb index c453e4f24..e84fdb215 100644 --- a/lib/calendar.rb +++ b/lib/calendar.rb @@ -1,11 +1,11 @@ require 'date' - require_relative 'reservation' class Calendar - def initialize (calendar) - @date_store = [] + def initialize + @date_store = {} + @reservation_store = [] end def make_reservation(start_date, end_date) @@ -17,11 +17,12 @@ def is_available(room_number, dates) end def reservations_on_date(date) + reservations = @date_store[date] return reservations end def find_by_id(reservation_id) - id = @date_store.find { |reservation| reservation.id === reservation_id} + id = @reservation_store.find { |reservation| reservation.id === reservation_id} if id == nil raise NoReservationError.new("There is no existing reservation with given ID.") end @@ -30,36 +31,35 @@ def find_by_id(reservation_id) end def total_cost_reservation(reservation_id) - reservation = reservations.find_by_id(reservation_id) + reservation = find_by_id(reservation_id) return reservation.cost end - def add_to_calendar(reservation) - @date_store << reservation - return @date_store - # if !@date_store.key?(reservation[:start_date]) - # reservation[:total_nights].times do |i| - # i += 0 - # @date_store[(Date.parse(reservation[:start_date]) + i).iso8601] = [] - # @date_store[(Date.parse(reservation[:start_date]) + i).iso8601][reservation[:room_number]] = reservation - # end + @reservation_store << reservation + + if !@date_store.key?(reservation.start_date) + reservation.total_nights.times do |i| + i += 0 + @date_store[(Date.parse(reservation.start_date) + i).iso8601] = [] + @date_store[(Date.parse(reservation.start_date) + i).iso8601][reservation.room_number] = reservation + end - # elsif @date_store.key?(reservation[:start_date]) - # reservation[:total_nights].times do |i| - # i += 0 - # if !@date_store.key?((Date.parse(reservation[:start_date]) + i).iso8601) - # @date_store[(Date.parse(reservation[:start_date]) + i).iso8601]= [] - # @date_store[(Date.parse(reservation[:start_date]) + i).iso8601][reservation[:room_number]] = reservation + elsif @date_store.key?(reservation.start_date) + reservation.total_nights.times do |i| + i += 0 + if !@date_store.key?((Date.parse(reservation.start_date) + i).iso8601) + @date_store[(Date.parse(reservation.start_date) + i).iso8601]= [] + @date_store[(Date.parse(reservation.start_date) + i).iso8601][reservation.room_number] = reservation - # else - # if reservation[:start_date].length == 20 - # raise NoRoomError.new ("There are no available rooms for the duration of specified dates.") - # end - # @date_store[(Date.parse(reservation[:start_date]) + i).iso8601][reservation[:room_number]] = reservation - # end - # end - # end + else + if reservation.start_date.length == 20 + raise NoRoomError.new ("There are no available rooms for the duration of specified dates.") + end + @date_store[(Date.parse(reservation.start_date) + i).iso8601][reservation.room_number] = reservation + end + end + end end end \ No newline at end of file diff --git a/lib/reservation.rb b/lib/reservation.rb index 4a403807c..a7335b85c 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -1,25 +1,28 @@ require 'date' require 'securerandom' -require_relative 'calendar' -require_relative 'hotel' +# require_relative 'room' class Reservation - attr_accessor :start_date, :end_date + attr_accessor :start_date, :end_date, :id, :cost, :room_number, :total_nights def initialize (start_date, end_date) @start_date = start_date @end_date = end_date + @id = (SecureRandom.alphanumeric).slice(0...7).upcase + @cost = (Date.parse(end_date) - Date.parse(start_date)).to_i * 200 + @total_nights = (Date.parse(end_date) - Date.parse(start_date)).to_i + @room_number = 0 end - - def reservation(start_date, end_date) + def reservation_details { - :id => (SecureRandom.alphanumeric).slice(0...7).upcase, - :start_date => Date.parse(start_date).iso8601, - :end_date => Date.parse(end_date).iso8601, - :total_nights => (Date.parse(end_date) - Date.parse(start_date)).to_i, - :cost => ((Date.parse(end_date) - Date.parse(start_date)).to_i * 200) + :id => @id, + :start_date => Date.parse(@start_date).iso8601, + :end_date => Date.parse(@end_date).iso8601, + :total_nights => @total_nights, + :cost => @cost, + :room_number => 0 } end diff --git a/lib/room.rb b/lib/room.rb index fe7456cbb..7da6bcf66 100644 --- a/lib/room.rb +++ b/lib/room.rb @@ -1,6 +1,5 @@ require 'date' -require_relative 'calendar' -require_relative 'reservation' + class Hotel diff --git a/test/calendar_spec.rb b/test/calendar_spec.rb index b6941b069..ca1006492 100644 --- a/test/calendar_spec.rb +++ b/test/calendar_spec.rb @@ -2,27 +2,30 @@ require_relative '../lib/reservation' require_relative 'test_helper' -describe "Can get the total cost by reservation ID" +describe "Can get the total cost by reservation ID" do before do - reservation1 = Reservation.new("2020/05/04", "2020/05/07") - reservation2 = Reservation.new("2020/05/07", "2020/05/09") - reservation3 = Reservation.new("2020/05/06", "2020/05/11") + @reservation1 = Reservation.new("2020/05/04", "2020/05/07") + @reservation2 = Reservation.new("2020/05/07", "2020/05/09") + @reservation3 = Reservation.new("2020/05/06", "2020/05/11") - calendar = Calendar.new + @calendar = Calendar.new - calendar.add_to_calendar(reservation1) - calendar.add_to_calendar(reservation2) - calendar.add_to_calendar(reservation3) + @calendar.add_to_calendar(@reservation1) + @calendar.add_to_calendar(@reservation2) + @calendar.add_to_calendar(@reservation3) end - it "gives correct cost of requested reservation" - id = reservation1.id - cost = calendar.total_cost_reservation(id) - - expect reservation1.cost.must_be cost - end - + it "gives correct cost of requested reservation" do + id = @reservation1.id + cost = @calendar.total_cost_reservation(id) + expect @reservation1.cost.must_equal cost + end + it "lists all reservations for specific date" do + expected_outcome = [@reservation1] + actual_outcome = @calendar.reservations_on_date("2020/05/04") + expect actual_outcome == expected_outcome + end end diff --git a/test/test_helper.rb b/test/test_helper.rb index db45161a8..2bcf76800 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,4 +1,4 @@ -# Add simplecov +require 'simplecov' require "minitest" require "minitest/autorun" require "minitest/reporters" From 6dd40cc2d79c6785ed199951b176e593a462802a Mon Sep 17 00:00:00 2001 From: mulhoo Date: Mon, 9 Mar 2020 00:27:59 -0700 Subject: [PATCH 05/10] Self-add room number and checking if room available --- lib/calendar.rb | 64 +++++++++++++++++++++---------------------- lib/calendar_date.rb | 39 ++++++++++++++++++++++++++ lib/reservation.rb | 19 +++++++------ lib/room.rb | 13 +++++++-- test/calendar_spec.rb | 11 ++++++-- 5 files changed, 100 insertions(+), 46 deletions(-) create mode 100644 lib/calendar_date.rb diff --git a/lib/calendar.rb b/lib/calendar.rb index e84fdb215..b82877028 100644 --- a/lib/calendar.rb +++ b/lib/calendar.rb @@ -1,31 +1,43 @@ require 'date' require_relative 'reservation' +require_relative 'calendar_date' class Calendar def initialize @date_store = {} - @reservation_store = [] + @all_reservation_store = [] end def make_reservation(start_date, end_date) - return 4 + reservation = Reservation.new(start_date, end_date) + return reservation end - def is_available(room_number, dates) - return 3 + def is_available(room_number, date) + if @date_store[date].available_rooms(room_number) + return end def reservations_on_date(date) - reservations = @date_store[date] - return reservations + return @date_store[date].reservations end - def find_by_id(reservation_id) - id = @reservation_store.find { |reservation| reservation.id === reservation_id} - if id == nil - raise NoReservationError.new("There is no existing reservation with given ID.") + def rooms_on_date(date) + if @date_store[date].nil? + date = CalendarDate.new + else + date = @date_store[date] end + + raise NoRoomError.new("There are no available rooms on the specified date.") if date.unavailable? + + return date.available_rooms + end + + def find_by_id(reservation_id) + id = @all_reservation_store.find { |reservation| reservation.id === reservation_id} + raise NoReservationError.new("There is no existing reservation with given ID.") if id == nil return id end @@ -36,30 +48,16 @@ def total_cost_reservation(reservation_id) end def add_to_calendar(reservation) - @reservation_store << reservation - - if !@date_store.key?(reservation.start_date) - reservation.total_nights.times do |i| - i += 0 - @date_store[(Date.parse(reservation.start_date) + i).iso8601] = [] - @date_store[(Date.parse(reservation.start_date) + i).iso8601][reservation.room_number] = reservation - end - - elsif @date_store.key?(reservation.start_date) - reservation.total_nights.times do |i| - i += 0 - if !@date_store.key?((Date.parse(reservation.start_date) + i).iso8601) - @date_store[(Date.parse(reservation.start_date) + i).iso8601]= [] - @date_store[(Date.parse(reservation.start_date) + i).iso8601][reservation.room_number] = reservation - - else - if reservation.start_date.length == 20 - raise NoRoomError.new ("There are no available rooms for the duration of specified dates.") - end - @date_store[(Date.parse(reservation.start_date) + i).iso8601][reservation.room_number] = reservation - end + @all_reservation_store << reservation.reservation_details + + reservation.total_nights.times do |i| + if !@date_store.key?(reservation.start_date) + date = CalendarDate.new + date.add_reservation(reservation) + @date_store[(reservation.start_date + i)] = date + else + @date_store[(reservation.start_date + i)].add_reservation(reservation) end end end - end \ No newline at end of file diff --git a/lib/calendar_date.rb b/lib/calendar_date.rb new file mode 100644 index 000000000..dfa36fe56 --- /dev/null +++ b/lib/calendar_date.rb @@ -0,0 +1,39 @@ +require 'date' +require_relative 'room' + +class CalendarDate + + def initialize + @day_reservation_store = [] + @available_rooms = Rooms.new + end + + def reservations + return @day_reservation_store + end + + def unavailable? + return @reservation_store.length == 20 + end + + def available_rooms + return @available_rooms.rooms + end + + def assign_room_number(reservation) + @available_rooms + reservation.total_nights.do |i| + raise NoRoomError "There are no rooms available on the given date." if (resevervation.start_date + i).unavailable? + + + end + + + def add_reservation(reservation) + @reservation_store << reservation.reservation_details + @available_rooms.room_reserved(reservation.room_number) + + return @day_reservation_store + end + +end \ No newline at end of file diff --git a/lib/reservation.rb b/lib/reservation.rb index a7335b85c..9f352c4c0 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -1,28 +1,29 @@ require 'date' require 'securerandom' -# require_relative 'room' +require_relative 'room' class Reservation attr_accessor :start_date, :end_date, :id, :cost, :room_number, :total_nights def initialize (start_date, end_date) - @start_date = start_date - @end_date = end_date + @start_date = Date.parse(start_date) + @end_date = Date.parse(end_date) @id = (SecureRandom.alphanumeric).slice(0...7).upcase - @cost = (Date.parse(end_date) - Date.parse(start_date)).to_i * 200 + @cost = (@end_date - @start_date).to_i * 200 @total_nights = (Date.parse(end_date) - Date.parse(start_date)).to_i - @room_number = 0 + @room_number = reservation.assign_room_number end + - def reservation_details + def reservation_details(reservation) { :id => @id, - :start_date => Date.parse(@start_date).iso8601, - :end_date => Date.parse(@end_date).iso8601, + :start_date => @start_date.iso8601, + :end_date => @end_date.iso8601, :total_nights => @total_nights, :cost => @cost, - :room_number => 0 + :room_number => @room_number } end diff --git a/lib/room.rb b/lib/room.rb index 7da6bcf66..e4e607c4e 100644 --- a/lib/room.rb +++ b/lib/room.rb @@ -1,8 +1,17 @@ require 'date' +class Rooms -class Hotel + def initialize + @available_rooms = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] + end - room_number = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] + def room_reserved(room_number) + @available_rooms.delete(room_number) + end + + def rooms + @available_rooms + end end \ No newline at end of file diff --git a/test/calendar_spec.rb b/test/calendar_spec.rb index ca1006492..59e91dff1 100644 --- a/test/calendar_spec.rb +++ b/test/calendar_spec.rb @@ -2,7 +2,7 @@ require_relative '../lib/reservation' require_relative 'test_helper' -describe "Can get the total cost by reservation ID" do +describe "Manages calendar and it's various duties" do before do @reservation1 = Reservation.new("2020/05/04", "2020/05/07") @reservation2 = Reservation.new("2020/05/07", "2020/05/09") @@ -25,7 +25,14 @@ it "lists all reservations for specific date" do expected_outcome = [@reservation1] actual_outcome = @calendar.reservations_on_date("2020/05/04") - expect actual_outcome == expected_outcome + assert_equal(actual_outcome, expected_outcome) end + + it "accesses list of available rooms on a given date" do + expected_outcome = [1, 5, 17] + actual_outcome = @calendar.rooms_on_date("2020/05/04") + assert_equal(actual_outcome, expected_outcome) + end + end From 926e401bc614e1749eae7563af5b97747ff6d8d9 Mon Sep 17 00:00:00 2001 From: mulhoo Date: Mon, 9 Mar 2020 03:05:44 -0700 Subject: [PATCH 06/10] All code there, need to pass tests. problem with reservation. --- lib/calendar.rb | 36 ++++++++++++++++++++++++++++++++---- lib/calendar_date.rb | 17 +++++++++-------- lib/reservation.rb | 13 ++++++++++--- lib/room.rb | 7 +++++-- test/test_helper.rb | 1 + 5 files changed, 57 insertions(+), 17 deletions(-) diff --git a/lib/calendar.rb b/lib/calendar.rb index b82877028..4892eea7a 100644 --- a/lib/calendar.rb +++ b/lib/calendar.rb @@ -1,6 +1,7 @@ require 'date' require_relative 'reservation' require_relative 'calendar_date' +require_relative 'room' class Calendar @@ -9,20 +10,44 @@ def initialize @all_reservation_store = [] end + def make_reservation(start_date, end_date) reservation = Reservation.new(start_date, end_date) return reservation end - def is_available(room_number, date) - if @date_store[date].available_rooms(room_number) - return + + def list_of_available_rooms_entire_stay(reservation) + date_range = [] + available_rooms_all_dates = [] + + reservation.total_nights.times do |i| + date_range << (reservation.start_date + i).available_rooms + raise NoRoomError.new "There are no rooms available for the duration of this stay." if (reservation.start_date + i).unavailable? + end + + date_range.each do |i, j| + if (date_range[0] & date_range[i]).any? + available_rooms_all_dates << (date_range[0] & date_range[i]) + end + end + + raise NoRoomError.new "There are no rooms available for the duration of this stay." if available_rooms_all_dates.empty? + + return available_rooms_all_dates.flatten.uniq! + end + + + def is_available?(room_number, date) + return @date_store[date].available_rooms(room_number) end + def reservations_on_date(date) return @date_store[date].reservations end + def rooms_on_date(date) if @date_store[date].nil? date = CalendarDate.new @@ -35,6 +60,7 @@ def rooms_on_date(date) return date.available_rooms end + def find_by_id(reservation_id) id = @all_reservation_store.find { |reservation| reservation.id === reservation_id} raise NoReservationError.new("There is no existing reservation with given ID.") if id == nil @@ -42,13 +68,15 @@ def find_by_id(reservation_id) return id end + def total_cost_reservation(reservation_id) reservation = find_by_id(reservation_id) return reservation.cost end + def add_to_calendar(reservation) - @all_reservation_store << reservation.reservation_details + @all_reservation_store << reservation reservation.total_nights.times do |i| if !@date_store.key?(reservation.start_date) diff --git a/lib/calendar_date.rb b/lib/calendar_date.rb index dfa36fe56..6c13ddca5 100644 --- a/lib/calendar_date.rb +++ b/lib/calendar_date.rb @@ -1,5 +1,7 @@ require 'date' + require_relative 'room' +require_relative 'reservation' class CalendarDate @@ -12,25 +14,24 @@ def reservations return @day_reservation_store end + def unavailable? return @reservation_store.length == 20 end - def available_rooms - return @available_rooms.rooms - end - def assign_room_number(reservation) - @available_rooms - reservation.total_nights.do |i| - raise NoRoomError "There are no rooms available on the given date." if (resevervation.start_date + i).unavailable? + def available?(room_number) + return @available_rooms.include?(room_number) + end + def all_available_rooms + return @available_rooms.rooms end def add_reservation(reservation) - @reservation_store << reservation.reservation_details + @day_reservation_store << reservation.reservation_details @available_rooms.room_reserved(reservation.room_number) return @day_reservation_store diff --git a/lib/reservation.rb b/lib/reservation.rb index 9f352c4c0..94ee8b9c7 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -3,18 +3,25 @@ require_relative 'room' + class Reservation attr_accessor :start_date, :end_date, :id, :cost, :room_number, :total_nights - def initialize (start_date, end_date) + def initialize(start_date, end_date) @start_date = Date.parse(start_date) @end_date = Date.parse(end_date) @id = (SecureRandom.alphanumeric).slice(0...7).upcase @cost = (@end_date - @start_date).to_i * 200 - @total_nights = (Date.parse(end_date) - Date.parse(start_date)).to_i - @room_number = reservation.assign_room_number + @total_nights = (@end_date - @start_date).to_i + @room_number = room_number end + def assign_room_number + available = reservation.list_of_available_rooms_entire_stay + room_number = available[0] + + return room_number + end def reservation_details(reservation) { diff --git a/lib/room.rb b/lib/room.rb index e4e607c4e..91c306079 100644 --- a/lib/room.rb +++ b/lib/room.rb @@ -1,15 +1,18 @@ require 'date' class Rooms - - def initialize + attr_accessor :available_rooms + + def initialize @available_rooms = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] end + def room_reserved(room_number) @available_rooms.delete(room_number) end + def rooms @available_rooms end diff --git a/test/test_helper.rb b/test/test_helper.rb index 2bcf76800..80aa07bde 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -8,3 +8,4 @@ require_relative '../lib/calendar' require_relative '../lib/reservation' require_relative '../lib/room' +require_relative '../lib/calendar_date' From 6f331f0ffa0647e8d5490f8c628dd3cbe43b549d Mon Sep 17 00:00:00 2001 From: mulhoo Date: Mon, 9 Mar 2020 06:18:27 -0700 Subject: [PATCH 07/10] Fixed some of the error problems --- lib/calendar.rb | 29 +++++++++++++++++++---------- lib/calendar_date.rb | 7 ++++--- lib/reservation.rb | 16 ++++++++-------- lib/room.rb | 2 +- test/calendar_spec.rb | 28 +++++++++++++++++++++------- 5 files changed, 53 insertions(+), 29 deletions(-) diff --git a/lib/calendar.rb b/lib/calendar.rb index 4892eea7a..a23b66dd5 100644 --- a/lib/calendar.rb +++ b/lib/calendar.rb @@ -4,15 +4,17 @@ require_relative 'room' class Calendar - + attr_accessor :all_reservations_store, :date_store + def initialize @date_store = {} - @all_reservation_store = [] + @all_reservations_store = [] end def make_reservation(start_date, end_date) reservation = Reservation.new(start_date, end_date) + return reservation end @@ -26,6 +28,10 @@ def list_of_available_rooms_entire_stay(reservation) raise NoRoomError.new "There are no rooms available for the duration of this stay." if (reservation.start_date + i).unavailable? end + if date_range.length == 1 + return date_range[0] + end + date_range.each do |i, j| if (date_range[0] & date_range[i]).any? available_rooms_all_dates << (date_range[0] & date_range[i]) @@ -39,16 +45,19 @@ def list_of_available_rooms_entire_stay(reservation) def is_available?(room_number, date) + date = Date.parse(date).iso8601 return @date_store[date].available_rooms(room_number) end def reservations_on_date(date) + date = Date.parse(date).iso8601 return @date_store[date].reservations end def rooms_on_date(date) + date = Date.parse(date).iso8601 if @date_store[date].nil? date = CalendarDate.new else @@ -57,34 +66,34 @@ def rooms_on_date(date) raise NoRoomError.new("There are no available rooms on the specified date.") if date.unavailable? - return date.available_rooms + return date.all_available_rooms end def find_by_id(reservation_id) - id = @all_reservation_store.find { |reservation| reservation.id === reservation_id} - raise NoReservationError.new("There is no existing reservation with given ID.") if id == nil + reservation = @all_reservations_store.find { |reservation| reservation[:id] == reservation_id} + raise NoReservationError.new("There is no existing reservation with given ID.") if reservation == nil - return id + return reservation end def total_cost_reservation(reservation_id) reservation = find_by_id(reservation_id) - return reservation.cost + return reservation[:cost] end def add_to_calendar(reservation) - @all_reservation_store << reservation + @all_reservations_store << reservation.details reservation.total_nights.times do |i| if !@date_store.key?(reservation.start_date) date = CalendarDate.new date.add_reservation(reservation) - @date_store[(reservation.start_date + i)] = date + @date_store[(reservation.start_date + i).iso8601] = date else - @date_store[(reservation.start_date + i)].add_reservation(reservation) + @date_store[(reservation.start_date + i).iso8601].add_reservation(reservation) end end end diff --git a/lib/calendar_date.rb b/lib/calendar_date.rb index 6c13ddca5..9042bb523 100644 --- a/lib/calendar_date.rb +++ b/lib/calendar_date.rb @@ -4,7 +4,8 @@ require_relative 'reservation' class CalendarDate - + attr_accessor :day_reservation_store, :available_rooms + def initialize @day_reservation_store = [] @available_rooms = Rooms.new @@ -16,7 +17,7 @@ def reservations def unavailable? - return @reservation_store.length == 20 + return @day_reservation_store.length == 20 end @@ -31,7 +32,7 @@ def all_available_rooms def add_reservation(reservation) - @day_reservation_store << reservation.reservation_details + @day_reservation_store << reservation.details @available_rooms.room_reserved(reservation.room_number) return @day_reservation_store diff --git a/lib/reservation.rb b/lib/reservation.rb index 94ee8b9c7..42659026f 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -5,26 +5,26 @@ class Reservation - attr_accessor :start_date, :end_date, :id, :cost, :room_number, :total_nights + attr_accessor :id, :start_date, :end_date, :total_nights, :cost, :room_number def initialize(start_date, end_date) + @id = (SecureRandom.alphanumeric).slice(0...7).upcase @start_date = Date.parse(start_date) @end_date = Date.parse(end_date) - @id = (SecureRandom.alphanumeric).slice(0...7).upcase - @cost = (@end_date - @start_date).to_i * 200 @total_nights = (@end_date - @start_date).to_i + @cost = @total_nights * 200 @room_number = room_number end - def assign_room_number - available = reservation.list_of_available_rooms_entire_stay - room_number = available[0] + def assign_room_number(reservation) + room = list_of_available_rooms_entire_stay(reservation) + room_number = room[0] return room_number end - def reservation_details(reservation) - { + def details + return resdeets = { :id => @id, :start_date => @start_date.iso8601, :end_date => @end_date.iso8601, diff --git a/lib/room.rb b/lib/room.rb index 91c306079..94de019ca 100644 --- a/lib/room.rb +++ b/lib/room.rb @@ -2,7 +2,7 @@ class Rooms attr_accessor :available_rooms - + def initialize @available_rooms = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] end diff --git a/test/calendar_spec.rb b/test/calendar_spec.rb index 59e91dff1..6527f94c1 100644 --- a/test/calendar_spec.rb +++ b/test/calendar_spec.rb @@ -1,7 +1,6 @@ -require_relative '../lib/calendar' -require_relative '../lib/reservation' require_relative 'test_helper' + describe "Manages calendar and it's various duties" do before do @reservation1 = Reservation.new("2020/05/04", "2020/05/07") @@ -9,30 +8,45 @@ @reservation3 = Reservation.new("2020/05/06", "2020/05/11") @calendar = Calendar.new - @calendar.add_to_calendar(@reservation1) @calendar.add_to_calendar(@reservation2) @calendar.add_to_calendar(@reservation3) + + + # puts "\n" + # puts @calendar.date_store + # puts "\n" + end it "gives correct cost of requested reservation" do - id = @reservation1.id + reservation1 = @reservation1.details + id = reservation1[:id] cost = @calendar.total_cost_reservation(id) - expect @reservation1.cost.must_equal cost + expect (reservation1[:cost]).must_equal cost end it "lists all reservations for specific date" do - expected_outcome = [@reservation1] + reservation1 = @reservation1.details + expected_outcome = [reservation1] actual_outcome = @calendar.reservations_on_date("2020/05/04") assert_equal(actual_outcome, expected_outcome) end it "accesses list of available rooms on a given date" do - expected_outcome = [1, 5, 17] + expected_outcome = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] actual_outcome = @calendar.rooms_on_date("2020/05/04") assert_equal(actual_outcome, expected_outcome) end + it 'accesses appropriate reservation when searching by id' do + reservation1 = @reservation1.details + + expected_outcome = reservation1 + actual_outcome = find_by_id(reservation1[:id]) + assert_equal(actual_outcome, expected_outcome) + end + end From b71bd94e18ac53144febd2beda08b10cb5d86362 Mon Sep 17 00:00:00 2001 From: mulhoo Date: Mon, 9 Mar 2020 10:19:45 -0700 Subject: [PATCH 08/10] Working on tests --- lib/calendar.rb | 27 +++++++++++++++++---------- lib/reservation.rb | 21 ++++++++++++++++----- test/calendar_date_spec.rb | 18 ++++++++++++++++++ test/calendar_spec.rb | 34 ++++++++++++++++++++++++++++++++-- 4 files changed, 83 insertions(+), 17 deletions(-) create mode 100644 test/calendar_date_spec.rb diff --git a/lib/calendar.rb b/lib/calendar.rb index a23b66dd5..1b4977950 100644 --- a/lib/calendar.rb +++ b/lib/calendar.rb @@ -19,34 +19,40 @@ def make_reservation(start_date, end_date) end - def list_of_available_rooms_entire_stay(reservation) + def list_available_rooms_entire_stay(reservation) date_range = [] available_rooms_all_dates = [] - reservation.total_nights.times do |i| - date_range << (reservation.start_date + i).available_rooms - raise NoRoomError.new "There are no rooms available for the duration of this stay." if (reservation.start_date + i).unavailable? + + if (reservation.total_nights == 1) && (@date_store[(reservation.start_date).iso8601].all_available_rooms.length == 20) + available_rooms = [1] + return available_rooms end - if date_range.length == 1 - return date_range[0] + reservation.total_nights.times do |i| + date_range << (@date_store[(reservation.start_date + i).iso8601]).all_available_rooms + + raise NoRoomsError.new "There are no rooms available for the duration of this stay." if @date_store[(reservation.start_date + i).iso8601].unavailable? end - date_range.each do |i, j| + + date_range.each do |i| if (date_range[0] & date_range[i]).any? available_rooms_all_dates << (date_range[0] & date_range[i]) end end - raise NoRoomError.new "There are no rooms available for the duration of this stay." if available_rooms_all_dates.empty? + raise NoRoomsError.new "There are no rooms available for the duration of this stay." if available_rooms_all_dates.empty? + + available_rooms = available_rooms_all_dates.flatten.uniq! - return available_rooms_all_dates.flatten.uniq! + return available_rooms end def is_available?(room_number, date) date = Date.parse(date).iso8601 - return @date_store[date].available_rooms(room_number) + return @date_store[date].is_available?(room_number) end @@ -94,6 +100,7 @@ def add_to_calendar(reservation) @date_store[(reservation.start_date + i).iso8601] = date else @date_store[(reservation.start_date + i).iso8601].add_reservation(reservation) + raise NoRoomError.new "Sorry, there is no availability on that date." if @date_store[(reservation.start_date + i).iso8601].unavailable? end end end diff --git a/lib/reservation.rb b/lib/reservation.rb index 42659026f..f3af23eb3 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -14,17 +14,28 @@ def initialize(start_date, end_date) @total_nights = (@end_date - @start_date).to_i @cost = @total_nights * 200 @room_number = room_number + + # def valid_date?(date) + # date_format = '%Y-%m-%d' + # DateTime.strptime(date, date_format) + # true + # rescue ArgumentError + # false + # end + + # valid_date?(start_date) + # valid_date?(end_date) + + # raise InvalidDateRangeError.new ("The given date range is invalid.") if (@end_date - @start_date) <= 0 end - def assign_room_number(reservation) - room = list_of_available_rooms_entire_stay(reservation) - room_number = room[0] - + def assign_room_number + room_number = available_rooms[0] return room_number end def details - return resdeets = { + { :id => @id, :start_date => @start_date.iso8601, :end_date => @end_date.iso8601, diff --git a/test/calendar_date_spec.rb b/test/calendar_date_spec.rb new file mode 100644 index 000000000..7cb8815da --- /dev/null +++ b/test/calendar_date_spec.rb @@ -0,0 +1,18 @@ +require_relative 'test_helper' + +describe "Manages calendar and it's various duties" do + before do + @reservation1 = Reservation.new("2020/05/04", "2020/05/07") + @reservation2 = Reservation.new("2020/05/07", "2020/05/09") + @reservation3 = Reservation.new("2020/05/06", "2020/05/11") + + @calendar = Calendar.new + @calendar.add_to_calendar(@reservation1) + @calendar.add_to_calendar(@reservation2) + @calendar.add_to_calendar(@reservation3) + end + + it "has an array that keeps track of the available rooms" do + + end +end \ No newline at end of file diff --git a/test/calendar_spec.rb b/test/calendar_spec.rb index 6527f94c1..3524c3604 100644 --- a/test/calendar_spec.rb +++ b/test/calendar_spec.rb @@ -40,13 +40,43 @@ assert_equal(actual_outcome, expected_outcome) end - it 'accesses appropriate reservation when searching by id' do + it "returns an array of all 1-20 rooms if there are no reservations on a given date" do + expected_outcome = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] + actual_outcome = @calendar.rooms_on_date("2020/05/02") + assert_equal(actual_outcome, expected_outcome) + end + + it "returns an array of the open rooms during a date range" do + @calendar.list_available_rooms_entire_stay(@reservation2) + expect available_rooms.must_be_instance_of Array + end + + # it "raises NoRoomError if all rooms on a given date are booked" do + + # raise NoRoomError + # end + + it 'accesses correct reservation when searching by id' do reservation1 = @reservation1.details + id = reservation1[:id] expected_outcome = reservation1 - actual_outcome = find_by_id(reservation1[:id]) + actual_outcome = @calendar.find_by_id(id) assert_equal(actual_outcome, expected_outcome) end + it 'creates a new reservation' do + expected_outcome = Reservation.new("2020/05/04", "2020/05/07") + actual_outcome = expected_outcome + + assert_equal(actual_outcome, expected_outcome) + end + + it "returns true/false if given room is available on a given date" do + yesorno = @calendar.is_available?(5, "2020/05/07") + + expect yesorno.must_be true + end + end From fc0b98e92756982ac0f851a057669a593991db5a Mon Sep 17 00:00:00 2001 From: mulhoo Date: Mon, 9 Mar 2020 11:15:43 -0700 Subject: [PATCH 09/10] Updated tests, still working out room bug --- lib/calendar.rb | 14 +++++++++----- lib/calendar_date.rb | 6 +++--- lib/room.rb | 8 ++++---- test/calendar_date_spec.rb | 4 ++++ test/calendar_spec.rb | 2 +- 5 files changed, 21 insertions(+), 13 deletions(-) diff --git a/lib/calendar.rb b/lib/calendar.rb index 1b4977950..f1004897a 100644 --- a/lib/calendar.rb +++ b/lib/calendar.rb @@ -29,10 +29,14 @@ def list_available_rooms_entire_stay(reservation) return available_rooms end - reservation.total_nights.times do |i| - date_range << (@date_store[(reservation.start_date + i).iso8601]).all_available_rooms - - raise NoRoomsError.new "There are no rooms available for the duration of this stay." if @date_store[(reservation.start_date + i).iso8601].unavailable? + (reservation.total_nights).times do |i| + if @date_store[(reservation.start_date + i).iso8601].nil? + date = CalendarDate.new + else + raise NoRoomsError.new "There are no rooms available for the duration of this stay." if @date_store[(reservation.start_date + i).iso8601].unavailable? + date_range << (@date_store[(reservation.start_date + i).iso8601]).all_available_rooms + end + end @@ -52,7 +56,7 @@ def list_available_rooms_entire_stay(reservation) def is_available?(room_number, date) date = Date.parse(date).iso8601 - return @date_store[date].is_available?(room_number) + return @date_store[date].room_available?(room_number) end diff --git a/lib/calendar_date.rb b/lib/calendar_date.rb index 9042bb523..4c09a1c64 100644 --- a/lib/calendar_date.rb +++ b/lib/calendar_date.rb @@ -21,13 +21,13 @@ def unavailable? end - def available?(room_number) - return @available_rooms.include?(room_number) + def room_available?(room_number) + return @available_rooms.rooms.include?(room_number) end def all_available_rooms - return @available_rooms.rooms + return @available_rooms.rooms end diff --git a/lib/room.rb b/lib/room.rb index 94de019ca..8053fe62c 100644 --- a/lib/room.rb +++ b/lib/room.rb @@ -1,20 +1,20 @@ require 'date' class Rooms - attr_accessor :available_rooms + attr_accessor :all_rooms def initialize - @available_rooms = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] + @all_rooms = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] end def room_reserved(room_number) - @available_rooms.delete(room_number) + @all_rooms.delete(room_number) end def rooms - @available_rooms + @all_rooms end end \ No newline at end of file diff --git a/test/calendar_date_spec.rb b/test/calendar_date_spec.rb index 7cb8815da..089c00e21 100644 --- a/test/calendar_date_spec.rb +++ b/test/calendar_date_spec.rb @@ -7,6 +7,7 @@ @reservation3 = Reservation.new("2020/05/06", "2020/05/11") @calendar = Calendar.new + @calendar.add_to_calendar(@reservation1) @calendar.add_to_calendar(@reservation2) @calendar.add_to_calendar(@reservation3) @@ -14,5 +15,8 @@ it "has an array that keeps track of the available rooms" do + hopefully_an_array = @date_store[Date.parse("2020/05/04").iso8601].all_available_rooms + + expect hopefully_an_array.must_be_instance_of Array end end \ No newline at end of file diff --git a/test/calendar_spec.rb b/test/calendar_spec.rb index 3524c3604..6587847ba 100644 --- a/test/calendar_spec.rb +++ b/test/calendar_spec.rb @@ -75,7 +75,7 @@ it "returns true/false if given room is available on a given date" do yesorno = @calendar.is_available?(5, "2020/05/07") - expect yesorno.must_be true + expect yesorno.must_equal true end end From c544c4ef650d95a8139caa2b8b1936e70c661f0c Mon Sep 17 00:00:00 2001 From: mulhoo Date: Mon, 9 Mar 2020 11:25:19 -0700 Subject: [PATCH 10/10] uncommented raising argument error for invalid dates --- lib/reservation.rb | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/lib/reservation.rb b/lib/reservation.rb index f3af23eb3..0b39576b1 100644 --- a/lib/reservation.rb +++ b/lib/reservation.rb @@ -15,18 +15,18 @@ def initialize(start_date, end_date) @cost = @total_nights * 200 @room_number = room_number - # def valid_date?(date) - # date_format = '%Y-%m-%d' - # DateTime.strptime(date, date_format) - # true - # rescue ArgumentError - # false - # end - - # valid_date?(start_date) - # valid_date?(end_date) - - # raise InvalidDateRangeError.new ("The given date range is invalid.") if (@end_date - @start_date) <= 0 + def valid_date?(date) + date_format = '%Y-%m-%d' + DateTime.strptime(date, date_format) + true + rescue ArgumentError + false + end + + valid_date?(start_date) + valid_date?(end_date) + + raise InvalidDateRangeError.new ("The given date range is invalid.") if (@end_date - @start_date) <= 0 end def assign_room_number