Skip to content
Open
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
16 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 @@ -9,6 +9,7 @@
/test/tmp/
/test/version_tmp/
/tmp/
/DS_Store/

# Used by dotenv library to load environment variables.
# .env
Expand Down
22 changes: 22 additions & 0 deletions lib/date_range.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module Booking
class DateRange

attr_reader :start_date, :end_date

def initialize(start_date, end_date)
@start_date = start_date
@end_date = end_date
end

def total_nights
nights = end_date - start_date

if nights == 0
return raise ArgumentError, "Must book the room for at least one night."
else
return nights
end
end

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

require 'pry'

module Booking
class Hotel

attr_reader :reservations, :unavailable_rooms
attr_accessor :rooms

def initialize(num_of_rooms)
if num_of_rooms == nil || num_of_rooms <= 0
raise ArgumentError, "Hotel must have a postive number of rooms greater than 0."
end

@rooms = make_rooms(num_of_rooms)
@reservations = []
@unavailable_rooms = []
end

def make_rooms(num)
room_array = []
i = 1
while i < num + 1
room = Booking::Room.new(i)
room_array << room
i += 1
end
return room_array
end

def make_reservation(start_date, end_date)
available_rooms_list = available_rooms(start_date,end_date)
new_date_range = Booking::DateRange.new(start_date, end_date)
new_total_nights = new_date_range.total_nights
new_cost = new_total_nights * 200
room = available_rooms_list.sample
new_reservation = Booking::Reservation.new(new_date_range, new_total_nights, new_cost, room)

reservations.push(new_reservation)
end

def find_reservation(start_date, end_date)
reservation_array = []
reservations.each do |reservation|
if reservation.date_range.start_date == start_date && reservation.date_range.end_date == end_date

Choose a reason for hiding this comment

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

this is reaching too far into your reservations and date ranges. This should be cleared up by the POODR chapter 4 discussion.

reservation_array << reservation
end
end
return reservation_array
end

def available_rooms(start_date, end_date)
if start_date > end_date
raise ArgumentError, "End date of reservation must be after start date."
else
reservations.each do |reservation|
if reservation.date_range.end_date > start_date || reservation.date_range.start_date < end_date
if !unavailable_rooms.include?(reservation.room.number)
unavailable_rooms << reservation.room.number
end
end
end

a_rooms = rooms
i = 0
a_rooms.each do |room|
if unavailable_rooms.include?(room.number)
a_rooms.delete(room)
else
i += 1
end
end

if a_rooms == []
raise StandardError, "There are no rooms available for that date range."
else
return a_rooms
end
end
end

end
end
6 changes: 6 additions & 0 deletions lib/hotel_block.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module Booking
class Hotel_Block < Reservation
# def initialize
# end
end
end
17 changes: 17 additions & 0 deletions lib/reservation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require_relative 'hotel'

Choose a reason for hiding this comment

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

/Users/devin/Documents/Ada/c12/hotel/lib/reservation.rb:1: warning: loading in progress, circular require considered harmful - /Users/devin/Documents/Ada/c12/hotel/lib/hotel.rb

require 'pry'



module Booking
class Reservation
attr_reader :date_range, :total_cost, :total_nights, :room

def initialize(date_range, total_nights, total_cost, room)
@date_range = date_range
@room = room
@total_nights = total_nights
@total_cost = total_cost
end

Choose a reason for hiding this comment

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

If you have a class that has no methods besides initialize, either the class should be a struct of some sort, or some other part of your code is doing its work for it.

end
end
13 changes: 13 additions & 0 deletions lib/room.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Booking
class Room

attr_reader :number, :cost

def initialize (number, cost: 200)

Choose a reason for hiding this comment

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

This space causes this warning:

/Users/devin/Documents/Ada/c12/hotel/lib/room.rb:6: warning: parentheses after method name is interpreted as an argument list, not a decomposed argument

@number = number
@cost = cost

end

end
end
5 changes: 5 additions & 0 deletions refactors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
1. I wanted to have hotel_block be a child of reservation but I am not sure if it would work with the room instace variable that was
written into reservation.

2. The naming could definitely be clearer throughout the whole project.
3. There are a few methods inside of hotel that have quite a few nested if statements and I would clean those up to be clearer.
48 changes: 48 additions & 0 deletions test/date_range_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
require_relative 'test_helper'
require 'pry'

describe "DateRange" do
describe "Initializer" do

before do
start_date = Date.new(2016, 10, 21)
end_date = Date.new(2016, 10, 30)
@test_date_range = Booking::DateRange.new(start_date, end_date)
end

it "creates and instance of DateRange" do
expect(@test_date_range).must_be_kind_of Booking::DateRange
end

it "is set up for specific attributes and data types" do
[:start_date, :end_date].each do |attribute|
expect(@test_date_range).must_respond_to attribute
end

expect(@test_date_range.start_date).must_be_kind_of Date
expect(@test_date_range.end_date).must_be_kind_of Date

end

end

describe "Total_Nights" do

it "calculates the total amount of nights for a reservation" do
start_date = Date.new(2016, 10, 21)
end_date = Date.new(2016, 10, 30)
@test_date_range = Booking::DateRange.new(start_date, end_date)
expect(@test_date_range.total_nights).must_equal 9
end

it "raises an ArgumentError if end and start date are the same" do
start_date = Date.new(2016, 10, 21)
end_date = Date.new(2016, 10, 21)
@test_date_range = Booking::DateRange.new(start_date, end_date)

expect{@test_date_range.total_nights}.must_raise ArgumentError

end

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

describe 'Hotel_Block' do
describe 'Initialize' do

it "creates an instance of Hotel_Block" do
start_date = Date.new(2019, 10, 10)
end_date = Date.new(2019 10, 15)

Choose a reason for hiding this comment

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

this errors out every time. Run a final rake before you commit code! Even if the test isn't complete or doing anything, never check in code that crashes.

date_range = Booking::DateRange.new(start_date, end_date)
total_nights = 5
total_cost = 1000
wedding_block = Booking::Hotel_Block.new(date_range, total_nights, total_cost, room)

Choose a reason for hiding this comment

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

room does not exist. This doesn't cause a crash but is a pretty easy-to-fix error

expect(wedding_block).must_be_kind_of Booking::Hotel_Block
end
end
end
121 changes: 121 additions & 0 deletions test/hotel_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
require_relative 'test_helper'

def build_test_hotel
return Booking::Hotel.new(
20
)
end

describe "Hotel Class" do

describe "Initializer" do

it "is set up for specific attributes and data types" do
the_grand_budapest_hotel = build_test_hotel
[:rooms, :reservations].each do |attribute|
expect(the_grand_budapest_hotel).must_respond_to attribute
end

expect(the_grand_budapest_hotel.rooms).must_be_kind_of Array
expect(the_grand_budapest_hotel.reservations).must_be_kind_of Array
end

it "is an instance of Hotel Class" do
the_grand_budapest_hotel = build_test_hotel
expect(the_grand_budapest_hotel).must_be_kind_of Booking::Hotel
end

it "@rooms is the correct length" do
the_grand_budapest_hotel = build_test_hotel
expect(the_grand_budapest_hotel.rooms.length).must_equal 20
end

it "must be initiated with a a postive amount of rooms greater than 0" do
expect{Booking::Hotel.new(0)}.must_raise ArgumentError
expect{Booking::Hotel.new(nil)}.must_raise ArgumentError
end
end

describe "Make_Rooms" do
it "creates an instance of room" do
the_grand_budapest_hotel = build_test_hotel
array = the_grand_budapest_hotel.make_rooms(20)
array.each do |room|
expect(room).must_be_kind_of Booking::Room
end
end
end

describe "Make_Reservation" do
before do
@the_grand_budapest_hotel = build_test_hotel
start_date = Date.new(2019, 2, 15)
end_date = Date.new(2019, 2, 20)
@new_reservation = @the_grand_budapest_hotel.make_reservation(start_date, end_date)
end

it "Adds new reservation to all of Hotel's reservations list" do
expect(@the_grand_budapest_hotel.reservations.length).must_equal 1
end
end

describe "Find_Reservation" do
before do
@the_grand_budapest_hotel = build_test_hotel

@res_1_start_date = Date.new(2019, 2, 15)
@res_1_end_date = Date.new(2019, 2, 20)
@res_1 = @the_grand_budapest_hotel.make_reservation(@res_1_start_date, @res_1_end_date)

@res_2_start_date = Date.new(2019, 2, 15)
@res_2_end_date = Date.new(2019, 2, 20)
@res_2 = @the_grand_budapest_hotel.make_reservation(@res_2_start_date, @res_2_end_date)

@res_3_start_date = Date.new(2019, 6, 10)
@res_3_end_date = Date.new(2019, 6, 23)
@res_3 = @the_grand_budapest_hotel.make_reservation(@res_3_start_date, @res_3_end_date)
end

it "returns an array of reservation objects" do
expect(@the_grand_budapest_hotel.find_reservation(@res_1_start_date, @res_1_end_date)).must_be_kind_of Array
end

it "returns an array of the correct length" do
array = @the_grand_budapest_hotel.find_reservation(@res_1_start_date, @res_1_end_date)
expect(array.length).must_equal 2
end
end

describe "Available_Rooms" do

it "raises an argument if end_date is before start_date" do
the_grand_budapest_hotel = build_test_hotel
start_date = Date.new(2019, 2, 20)
end_date = Date.new(2019, 2, 15)

expect{the_grand_budapest_hotel.available_rooms(start_date, end_date)}.must_raise ArgumentError
end

it "will return an array of rooms that are available given the date range" do
@the_grand_budapest_hotel = build_test_hotel
start_date = Date.new(2019, 10, 10)
end_date = Date.new(2019, 10, 24)
expect(@the_grand_budapest_hotel.available_rooms(start_date, end_date)).must_be_kind_of Array
end

it "raises an exception if there are no rooms available" do
@the_grand_budapest_hotel = build_test_hotel
20.times do
res_start_date = Date.new(2019, 2, 15)
res_end_date = Date.new(2019, 2, 20)
res = @the_grand_budapest_hotel.make_reservation(res_start_date, res_end_date)

Choose a reason for hiding this comment

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

/Users/devin/Documents/Ada/c12/hotel/test/hotel_test.rb:111: warning: assigned but unused variable - res

end
res_21_start_date =Date.new(2019, 2, 15)
res_21_end_date = Date.new(2019, 2, 20)
expect{@the_grand_budapest_hotel.available_rooms(res_21_start_date, res_21_end_date)}.must_raise StandardError
end
end
end



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

def build_test_reservation
start_date = Date.new(2019, 2, 15)

Choose a reason for hiding this comment

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

This seems like you meant it to be a before block.

Choose a reason for hiding this comment

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

perhaps a let:

end_date = Date.new(2019, 2, 20)
res_date_range = Booking::DateRange.new(start_date, end_date)
res_room = Booking::Room.new(5)
res_total_nights = res_date_range.total_nights
res_total_cost = res_total_nights * 200
reservation = Booking::Reservation.new(res_date_range, res_total_nights, res_total_cost, res_room)

Choose a reason for hiding this comment

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

/Users/devin/Documents/Ada/c12/hotel/test/reservation_test.rb:11: warning: assigned but unused variable - reservation

end

describe "Reservation Class" do
describe "Initialize" do

it "is an instance of Reservation Class" do
wes_anderson_reservation = build_test_reservation
expect(wes_anderson_reservation).must_be_kind_of Booking::Reservation
end

it "date_range is an instance of DateRange" do
wes_anderson_reservation = build_test_reservation
expect(wes_anderson_reservation.date_range).must_be_kind_of Booking::DateRange
end

it "is assigned a room number" do
wes_anderson_reservation = build_test_reservation
expect(wes_anderson_reservation.room).must_be_kind_of Booking::Room
end
end
end
Loading