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

Ampers - Maddie Shields #39

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
9 changes: 9 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require 'rake/testtask'

Rake::TestTask.new do |t|
t.libs = ["lib"]
t.warning = true
t.test_files = FileList['specs/*_spec.rb']
end

task default: :test
19 changes: 19 additions & 0 deletions lib/block.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module Hotel

class Block

attr_reader :block_id, :party, :start_date, :end_date

def initialize(input)
@block_id = input[:block_id]
@party = input[:party]
@start_date = input[:start_date]
@end_date = input[:end_date]

if @end_date < @start_date
raise ArgumentError.new("Start date must be before end date.")
end
end

end
end
129 changes: 129 additions & 0 deletions lib/front_desk.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
require 'date'

require_relative 'room'
require_relative 'reservation'
require_relative 'block'


module Hotel
class FrontDesk
attr_reader :rooms, :reservations, :blocked_rooms

def initialize
@rooms = load_rooms
@reservations = []
@blocked_rooms = []
end

def find_room(room_number)
check_room_number(room_number)
@rooms.find{ |room| room.room_number == room_number }
end

def load_rooms
all_rooms = []
20.times do |room_index|
all_rooms << Hotel::Room.new(room_number: room_index + 1)
end
return all_rooms

end

def store_occupied_rooms(start_date, end_date)
occupied_rooms = []
range = (start_date...end_date)

@reservations.each do |reservation|
if (range.include? reservation.start_date) || (range.include? reservation.end_date - 1)
occupied_rooms << reservation.room_number
end
end
return occupied_rooms
end

def get_available_rooms(start_date, end_date)
@available_rooms = []
@rooms.each do |room|
if !store_occupied_rooms(start_date, end_date).include? room
@available_rooms << room
end
end
return @available_rooms
end

def first_available_room(start_date, end_date)
first_available_room = get_available_rooms(start_date, end_date).first
return first_available_room
end

def reserve_room(input)
room = first_available_room(input[:start_date], input[:end_date])

reservation_data = {
reservation_id: reservations.length + 1,
block_id: input[:block_id],
room: room,
start_date: input[:start_date],
end_date: input[:end_date],
}

new_reservation = Reservation.new(reservation_data)

room.add_reservation(new_reservation)
@reservations << new_reservation
p new_reservation
return new_reservation
end

def get_reservation_list(specified_date)
reservations_on_date = []
@reservations.each do |reservation|
if reservation.date_range.include? specified_date
reservations_on_date << reservation
end
end
return reservations_on_date
end

def get_available_blocked_rooms(number_of_rooms, start_date, end_date)
if number_of_rooms > 5
raise ArgumentError.new("Cannot block more than 5 rooms!")
end

# if get_available_rooms(start_date, end_date) < number_of_rooms
# raise ArgumentError.new("There are not enough rooms available for the requested dates")
# end

available_block = (get_available_rooms(start_date, end_date)).take(number_of_rooms)
# p available_block
return available_block
end

def block_rooms(number_of_rooms, start_date, end_date)

party = get_available_blocked_rooms(number_of_rooms, start_date, end_date)

block_data = {
block_id: @blocked_rooms.length + 1,
party: party,
start_date: start_date,
end_date: end_date,
}

new_block = Block.new(block_data)

@blocked_rooms << new_block
# p "++++TEST++++"
# p @blocked_rooms
return new_block
end

private

def check_room_number(room_number)
if room_number == nil || room_number < 1 || room_number > 20
raise ArgumentError.new("Room number cannot be blank or less than zero. (got #{room_number})")
end
end
end
end
47 changes: 47 additions & 0 deletions lib/reservation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# require 'csv'


module Hotel
RATE_PER_NIGHT = 200
BLOCK_DISCOUNT = 0.80

class Reservation

attr_reader :reservation_id, :room_number, :start_date, :end_date, :cost, :duration

def initialize(input)
@reservation_id = input[:reservation_id]
@block_id = input[:block_id] == nil ? nil : input[:block_id]
@room_number = input[:room_number]
@start_date = (input[:start_date])
@end_date = (input[:end_date])
@cost = calculate_cost

if @end_date < @start_date
raise ArgumentError.new("Start date must be before end date.")
end
end

def date_range
@range = (@start_date...@end_date)
return @range
end

def duration
duration = ((@end_date - 1) - @start_date)
# Convert Rational to Integer
duration = duration.to_i
return duration
end

def calculate_cost
if @block_id == nil
cost = RATE_PER_NIGHT * duration
else
cost = (RATE_PER_NIGHT * duration) * BLOCK_DISCOUNT
end
return cost
end

end
end
26 changes: 26 additions & 0 deletions lib/room.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require_relative 'reservation'

module Hotel
class Room
attr_reader :room_number, :reservations

def initialize(input)
if input[:room_number] == nil || input[:room_number] <= 0
raise ArgumentError.new("ID cannot be blank or less than zero. (got #{input[:id]})")
end

@room_number = input[:room_number]

@reservations = input[:reservations] == nil ? [] : input[:reservations]
end

def add_reservation(reservation)
if reservation.class != Reservation
raise ArgumentError.new("Can only add reservation instance to reservation collection")
end

@reservations << reservation

end
end
end
46 changes: 46 additions & 0 deletions specs/block_speck.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
require_relative 'spec_helper'

describe "Block class" do

before do
start_date = Date.new(2018, 03, 06)
end_date = start_date + 5 # 5 days after start date
number_of_rooms = 5

@block_data = {
block_id: 8,
party: Hotel::Room.new(room_number: 3),
start_date: start_date,
end_date: end_date,
}

@block = Hotel::Block.new(@block_data)
end


describe "initialize" do
it "is an instance of Block" do
@block.must_be_kind_of Hotel::Block
end

before do
start_date = Date.new(2018, 03, 06)
end_date = start_date - 5 # 5 days before start date

@reservation_data = {
block_id: 8,
party: Hotel::Room.new(room_number: 3),
start_date: start_date,
end_date: end_date,
}
end

it "raises an error if the end date is before the start date" do

proc {Hotel::Reservation.new(@reservation_data)}.must_raise ArgumentError
end
#
end


end
Loading