Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Leaves - Emily Vomacka #26

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,4 @@ build-iPhoneSimulator/

# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
.rvmrc
coverage
20 changes: 20 additions & 0 deletions lib/block.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Block
attr_reader :date_range, :name, :room_quantity, :cost
attr_accessor :room_nums

def initialize(block_range, block_name, room_quantity, cost)
@date_range = block_range
@name = block_name
@room_quantity = room_quantity
@room_nums = []
@cost = cost
end

def total_cost
return @cost * (date_range.end_date - date_range.start_date)
end

def unbooked_rooms_count
return @room_nums.length
end
end
14 changes: 14 additions & 0 deletions lib/daterange.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require 'date'

class DateRange
attr_reader :start_date, :end_date, :range

def initialize(start_year, start_month, start_day, end_year, end_month, end_day)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's reasonable to just accept to Date objects here and require the user to do the parsing/building themselves.

@start_date = Date.new(start_year, start_month, start_day)
@end_date = Date.new(end_year, end_month, end_day)
@range = (@start_date...@end_date)
if @end_date <= @start_date
raise ArgumentError, "End date must be later than start date."
end
end
end
88 changes: 88 additions & 0 deletions lib/hotel.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
require_relative 'room'
require_relative 'reservation'
require_relative 'daterange'
require_relative 'block'

class Hotel
attr_reader :rooms, :blocks

def initialize(rooms)
@rooms = []
@blocks = []
rooms.times do |i|
@rooms << Room.new(i + 1)
end
end

def list_rooms
@rooms.each do |curr_room|
puts "Room Number #{curr_room.room_num}"
end
end

def make_reservation(start_year, start_month, start_day, end_year, end_month, end_day)
res_range = DateRange.new(start_year, start_month, start_day, end_year, end_month, end_day)
new_reservation = Reservation.new(res_range)
potential_rooms = available_rooms(res_range)
if potential_rooms.length == 0
raise ArgumentError, "No vacancy"
end
new_reservation.room_num = potential_rooms.sample.room_num
@rooms[new_reservation.room_num - 1].reservations << new_reservation
return new_reservation
end

#reserve a room within a previously scheduled block
def make_block_reservation(start_year, start_month, start_day, end_year, end_month, end_day, input_name)
res_range = DateRange.new(start_year, start_month, start_day, end_year, end_month, end_day)
block = blocks.find {|block| block.name == input_name}
if block == nil
raise ArgumentError, "No block under that name was found in the hotel system."
end
if block.date_range.range != res_range.range
raise ArgumentError, "The requested booking dates do not match the dates specified by the block."
end
new_block_reservation = Reservation.new(res_range, input_name)
new_block_reservation.room_num = block.room_nums.shift
@rooms[new_block_reservation.room_num - 1].reservations << new_block_reservation
return new_block_reservation
end

#create a new block of multiple rooms
def make_block(start_year, start_month, start_day, end_year, end_month, end_day, block_name, room_quantity, cost)
block_range = DateRange.new(start_year, start_month, start_day, end_year, end_month, end_day)
block = Block.new(block_range, block_name, room_quantity, cost)
if @blocks.any? { |block| block.name == block_name && block.date_range.range == block_range.range }
raise ArgumentError, "A block with identical name and dates already exists at this hotel. Please choose a different name or dates."
end
block_rooms = available_rooms(block_range).sample(room_quantity)
if block_rooms.length < room_quantity
raise ArgumentError, "The hotel does not have a sufficient number of empty rooms to book this block."

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since there's nothing wrong with the arguments themselves this shouldn't be an ArgumentError; the user can't change their arguments to make a room available.

This should be a custom error or something like a StandardError.

end
block_rooms.each do |block_room|
block.room_nums << block_room.room_num
@rooms[block_room.room_num - 1].blocks << block
end
@blocks << block
return block
end

#shows which rooms are neither reserved nor blocked on a given day, require date_range
def available_rooms(req_range)
return @rooms.select { |room|
room.reservations.all? {|res| res.date_range.range.grep(req_range.range).length == 0 } && room.blocks.all? {|block| block.date_range.range.grep(req_range.range).length == 0 }
}
end

#returns all reservations for a given date
def reservations_by_date(year, month, day)
requested_date = Date.new(year, month, day)
reservations_by_date = []
@rooms.each do |room|
if valid_date_res = room.reservations.find {|res|res.date_range.range.include?(requested_date)}
reservations_by_date << valid_date_res
end
end
return reservations_by_date
end
end
13 changes: 13 additions & 0 deletions lib/reservation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Reservation
attr_reader :date_range, :block_name
attr_accessor :room_num

def initialize(res_range, block_name = nil)
@date_range = res_range
@block_name = block_name
end

def total_cost
return 200 * (date_range.end_date - date_range.start_date)
end
end
10 changes: 10 additions & 0 deletions lib/room.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Room
attr_reader :room_num
attr_accessor :reservations, :blocks

def initialize(room_num)
@room_num = room_num
@reservations = []
@blocks = []
end
end
8 changes: 8 additions & 0 deletions refactors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Changes I could make to hotel:

-Edit reservation to include name, promo codes/discounts, details like check-out time, additional charges
-Track reservations in a hash using name as a key, to make look up faster
-Figure out how to eliminate reservations from past dates, so the system doesn't become overloaded as time progresses.
-Allow more aspects of a reservation to be edited. (change stay duration, for example)
-Eliminate or reduce dependencies as I understand them more

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's impossible to eliminate dependencies unless you put everything in one file. Dependencies are okay, it's just important to be mindful when you decide to add one since they have a cost.


37 changes: 37 additions & 0 deletions test/block_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
require_relative 'test_helper'

describe "Block class" do
describe "Block instantiation" do
before do
@date_range = DateRange.new(2019, 7, 6, 2019, 7, 9)
@block = Block.new(@date_range, "name", 1, 100)
end

it "is an instance of Block" do
expect(@block).must_be_kind_of Block
end
end

describe "total cost" do
before do
@date_range = DateRange.new(2019, 7, 6, 2019, 7, 9)
@block = Block.new(@date_range, "name", 1, 100)
end

it "can calculate total cost" do
expect(@block.total_cost).must_equal 300
end
end

describe "available_rooms" do
before do
@hotel = Hotel.new(4)
@block = @hotel.make_block(2019, 7, 6, 2019, 7, 9, "name", 3, 100)
end

it "can return a list of available rooms" do
expect(@block.room_nums.length).must_equal @block.room_quantity
end

end
end
29 changes: 29 additions & 0 deletions test/daterange_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require_relative 'test_helper'

describe "DateRange class" do
describe "DateRange instantiation" do
before do
@date_range = DateRange.new(2019, 7, 6, 2019, 7, 9)
end

it "is an instance of DateRange" do
expect(@date_range).must_be_kind_of DateRange
end

it "includes a start date" do
expect(@date_range.start_date).must_be_kind_of Date
end

it "includes an end date" do
expect(@date_range.end_date).must_be_kind_of Date
end

it "does not include last day of range" do
expect(@date_range.range).wont_include @date_range.end_date
end

it "raises an ArgumentError if end date is before start date" do
expect{DateRange.new(2019, 7, 6, 2019, 7, 1)}.must_raise ArgumentError
end
end
end
Loading