diff --git a/Guardfile b/Guardfile index 6760f9177..e0b8bea5b 100644 --- a/Guardfile +++ b/Guardfile @@ -1,5 +1,5 @@ -guard :minitest, bundler: false, rubygems: false do - # with Minitest::Spec +guard :minitest, bundler: false, autorun: true, rubygems: false do + # With Minitest Reporters watch(%r{^spec/(.*)_spec\.rb$}) watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" } watch(%r{^spec/spec_helper\.rb$}) { 'spec' } diff --git a/lib/design-activity.md b/lib/design-activity.md new file mode 100644 index 000000000..209993c9e --- /dev/null +++ b/lib/design-activity.md @@ -0,0 +1,26 @@ +In Implementation A classes: +CartEntry + initialize two instance variables unit_price & quantity. +ShoppingCart + initialize an instance variables with an empty array. +Order + instantiate a new instance of an entry from the Shopping Cart class with instance variable cart. + SALES_TAX is a constant +Total_Price + calculating the total price of each item in the shopping cart. + +Implementation B classes +CartEntry + creates a method to calculate the price of all the same items per quantity. +ShoppingCart + Adds the sum of all items purchased. +Order + instantiate an instance of ShoppingCart and calculates the total of the order with sales tax. + +I think it is better to have total_price in order because if a calculation changed in shopping cart it would automatically be reflected in order. + +I don't believe it does manipulate the instance variable in other classes. + +If we were to add a bulk option I would add it to the price method in CartEntry as a conditional. + +I think Implementation B has a single responsibility because Implementation A has some classes with zero responsibility. diff --git a/lib/front_desk.rb b/lib/front_desk.rb new file mode 100644 index 000000000..d19077702 --- /dev/null +++ b/lib/front_desk.rb @@ -0,0 +1,116 @@ +require_relative "rooms.rb" +require_relative "reservations.rb" + + +class Front_Desk + + attr_reader :reservations, :rooms, :block_hold + + def initialize + @reservations = [] + @block_reservations = [] + @rooms = load_rooms + end + + def load_rooms + room_number = 0 + rooms = [] + + 20.times do + room_number += 1 + rooms << Room.new({ + :room_number => room_number + }) + end + return rooms + end + + def reserve_room(room_number, start_date, end_date) + if start_date > end_date + raise StandardError.new("Checkin Date: #{start_date} must be before Checkout Date: #{end_date}") + end + + available_rooms = available_rooms(start_date,end_date) + if !available_rooms.find { |room| room.room_number == room_number } + raise AlreadyReservedError.new("Room #{room} already has a reservation between #{start_date} and #{end_date}") + end + + input = {} + input[:room_number] = room_number + input[:start_date] = start_date + input[:end_date] = end_date + + new_reservation = Reservation.new(input) + @reservations << new_reservation + + return new_reservation + end + + #method to grab reservation by Date + def search_reserved_by_date(search_date) + search_date = Date.parse(search_date) + results = [] + + @reservations.each do |reservation| + if reservation.start_date <= search_date && search_date < reservation.end_date + results << @reservations + end + end + + @block_reservations.each do |reservation| + if reservation.start_date <= search_date && search_date < reservation.end_date + results << @block_reservations + end + end + + return results + end + + def available_rooms(start_date,end_date) + start_date = Date.parse(start_date) + end_date = Date.parse(end_date) + + list_of_rooms = @rooms.dup + + @reservations.each do |reservation| + if !((start_date < reservation.start_date && end_date <= reservation.start_date) || + (end_date > reservation.end_date && start_date >= reservation.end_date)) + list_of_rooms.reject! { |room| room.room_number == reservation.room_number } + end + end + + @block_reservations.each do |block_room| + if !((start_date < block_room.start_date && end_date <= block_room.start_date) || + (end_date > block_room.end_date && start_date >= block_room.end_date)) + list_of_rooms.reject! { |room| room.room_number == block_room.room_number } + end + end + + return list_of_rooms + end + + + def block_hold(start_date, end_date, number_of_rooms) + available_rooms = available_rooms(start_date,end_date) + + if start_date > end_date + raise StandardError.new("Checkin Date: #{start_date} must be before Checkout Date: #{end_date}") + elsif number_of_rooms > 5 + raise StandardError + end + + number_of_rooms.times do |index| + input = {} + input[:room_number] = available_rooms[index].room_number + input[:start_date] = start_date + input[:end_date] = end_date + + block_hold = Reservation.new(input) + @block_reservations << block_hold + + end + return @block_reservations + end + + +end diff --git a/lib/reservations.rb b/lib/reservations.rb new file mode 100644 index 000000000..e8e12b52c --- /dev/null +++ b/lib/reservations.rb @@ -0,0 +1,20 @@ +require "date" +require "pry" + +class Reservation + + attr_reader :room_number, :start_date, :end_date + def initialize(input) + @room_number = input[:room_number] + @start_date = Date.parse(input[:start_date]) + @end_date = Date.parse(input[:end_date]) + @@cost = total_cost + end + + def total_cost + nights = (end_date - start_date).to_i + cost = (nights * 200) + return cost + end + +end diff --git a/lib/rooms.rb b/lib/rooms.rb new file mode 100644 index 000000000..e3badcaac --- /dev/null +++ b/lib/rooms.rb @@ -0,0 +1,9 @@ +class Room + + attr_reader :room_number + + def initialize(input) + @room_number = input[:room_number] + end + +end diff --git a/refactors.txt b/refactors.txt new file mode 100644 index 000000000..2d6c3dd7b --- /dev/null +++ b/refactors.txt @@ -0,0 +1,7 @@ +#complete wave 3 requirements +#add a block reservation class +#rethink the room class +#create more test for all classes +#add comments to the code +#create methods for invalid start and end dates +#look at major dependency from each class diff --git a/spec/front_desk_spec.rb b/spec/front_desk_spec.rb new file mode 100644 index 000000000..f2c585cb1 --- /dev/null +++ b/spec/front_desk_spec.rb @@ -0,0 +1,87 @@ +require_relative 'spec_helper' + +describe "Front Desk Class" do + describe "Initializer" do + + before do + @admin = Front_Desk.new + @new_reservation = @admin.reserve_room(5,('2018-02-03'),('2018-02-06')) + + end + + it "has a room number assisnged to the reservation" do + expect(@new_reservation.room_number).must_equal 5 + end + + it "totals the cost based on each night stay" do + expect(@new_reservation.total_cost).must_equal 600 + end + + it "return a instance of reservation" do + expect(@new_reservation).must_be_kind_of Reservation + end + + it "adds a new reservation to the reservations array" do + expect(@admin.reservations).must_include(@new_reservation) + end + + it "returns all reservation within that date" do + @admin.block_hold(('2018-02-03'),('2018-02-06'),2) + @admin.reserve_room(5,('2018-02-01'),('2018-02-03')) + expect(@admin.search_reserved_by_date('2018-02-05').length).must_equal 3 + end + + it "returns zero for date of checkout" do + @admin.block_hold(('2018-02-03'),('2018-02-06'),2) + expect(@admin.search_reserved_by_date('2018-02-06').length).must_equal 0 + end + + it "room is not-available" do + @admin.reserve_room(6,('2018-02-05'),('2018-02-10')) + expect {@admin.reserve_room(6,('2018-02-05'),('2018-02-10'))}.must_raise StandardError + end + + it "allows a reservation to start on the same day another reservation ends" do + expect(@admin.reserve_room(5,('2018-02-06'),('2018-02-10'))).must_be_kind_of Reservation + end + + it "allows a reservation to end on the same day another reservation starts" do + expect(@admin.reserve_room(5,('2018-02-01'),('2018-02-03'))).must_be_kind_of Reservation + end + + it "raises an error if start date after the end date" do + expect{@admin.reserve_room(6,('2018-02-05'),('2018-02-03'))}.must_raise StandardError + end + + + describe "block hold reservation" do + + it "makes a block reservation for 1 to 5 rooms" do + expect(@admin.block_hold(('2018-02-01'),('2018-02-03'),2).length).must_equal 2 + end + + it "raises error if standard reservation conflicts with block reservation" do + @admin.block_hold(('2018-02-01'),('2018-02-11'),2) + expect {@admin.reserve_room(1,('2018-02-01'),('2018-02-10'))}.must_raise StandardError + end + + it "raises error if block reservation exceeds maximum(5) block reservation" do + expect {@admin.block_hold(('2018-02-01'),('2018-02-11'),6)}.must_raise StandardError + end + + it "raises error if block reservations conflict" do + @admin.block_hold(('2018-02-01'),('2018-02-11'),5) + @admin.block_hold(('2018-02-01'),('2018-02-11'),5) + @admin.block_hold(('2018-02-01'),('2018-02-11'),5) + @admin.block_hold(('2018-02-01'),('2018-02-11'),4) + expect {@admin.block_hold(('2018-02-01'),('2018-02-10'),2)}.must_raise StandardError + end + + + + end + end +end +#reserve_room creates an instance of reservation +#reservation is an array +#at index 0 cost diff --git a/spec/reservation_spec.rb b/spec/reservation_spec.rb new file mode 100644 index 000000000..fa5a6fef6 --- /dev/null +++ b/spec/reservation_spec.rb @@ -0,0 +1,70 @@ +require_relative 'spec_helper' + +describe "Reservation Class" do + describe "Initializer in Reservation" do + + it "loads 20 rooms" do + admin = Front_Desk.new + rooms = admin.load_rooms + expect(rooms).must_be_kind_of Array + expect(rooms.length).must_equal 20 + end + + it "returns an array that has instances of room" do + admin = Front_Desk.new + rooms = admin.load_rooms + rooms.each do |room| + expect(room).must_be_kind_of Room + end + end + + before do + @reservation_info = { + room_number: 1, + start_date: ('2018-02-03'), + end_date: ('2018-02-05'), + } + + @reservation = Reservation.new(@reservation_info) + + end + + it "is an instance of Reservation Class" do + expect(@reservation).must_be_kind_of Reservation + end + + it "has a room number assisnged to the reservation" do + expect(@reservation.room_number).must_equal 1 + + end + + it "totals the cost based on each night stay" do + expect(@reservation.total_cost).must_equal 400 + end + + + + +#given, when, then + + end +end + + + + + + + + + # describe "total calculated" do + # + # before do + # start_date = Date.parse("2018-05-25") + # end_date = Date.parse("2018-05-25") + # end + # + # it "will correctly calculate total revenue" do + # + # expect(@driver.total_revenue).must_equal ((101.65-1.65)*2)*0.8 + # end diff --git a/spec/rooms_spec.rb b/spec/rooms_spec.rb new file mode 100644 index 000000000..e14491551 --- /dev/null +++ b/spec/rooms_spec.rb @@ -0,0 +1,30 @@ +require_relative 'spec_helper' + + +# describe "Rooms class" do +# describe "Initializer in Rooms" do +# +# it "is an instance of Room Class and Number" do +# test_room = Room.new(12) +# +# expect(test_room).must_be_kind_of Room +# expect(test_room.room).must_equal 12 +# end +# +# it "list all the rooms in the hotel" do +# test_all_rooms = Room.create_rooms +# expect(test_all_rooms.length).must_equal 20 +# end +# +# it "list summary of rooms" do +# test_summary_of_rooms = Room.all +# expect(test_summary_of_rooms.length).must_equal 20 +# expect(test_summary_of_rooms).must_be_kind_of Array +# end +# +# +# end +# end + +# if !(reservation.start_date < start_date) && !(reservation.end_date <= start_date) || +# !(reservation.start_date < end_date) && !(reservation.end_date < end_date) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 4d1e3fdc8..7bc775b51 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,8 +1,12 @@ -require 'minitest' +require 'simplecov' +SimpleCov.start require 'minitest/autorun' require 'minitest/reporters' -# Add simplecov + Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new # Require_relative your lib files here! +require_relative '../lib/rooms' +require_relative '../lib/reservations' +require_relative '../lib/front_desk'