Skip to content
13 changes: 13 additions & 0 deletions lib/date_range.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Hotel
class Date_Range
attr_reader :start_date, :end_date

def initialize(start_date: , end_date:)
@start_date = start_date
@end_date = end_date
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

If you use a class without any methods besides initialize, you probably either don't need a class or your other classes are doing work that belongs to the empty class.


raise ArgumentError, "start date cannot be after end date" if start_date > end_date
end
end
end

78 changes: 78 additions & 0 deletions lib/hotel_system.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
require "Pry"
module Hotel
class Hotel_System
attr_reader :rooms
attr_accessor :reservations

def initialize(rooms: Hotel::Room.load_all, reservations: [])
@rooms = rooms
@reservations = reservations
end

def list_rooms
rooms
end

def no_rooms_available
raise ArgumentError, "There are no rooms available for that given date range."
end

def list_available_room_numbers(start_date, end_date)
available_rooms = list_available_rooms(start_date, end_date)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

list_available_room_numbers calls list_available_rooms. This is confusing. :(

available_room_numbers = available_rooms.map do |room|
available_rooms[0].room_number
end
available_room_numbers.sample
end

def make_reservation(start_date, end_date)

if list_available_room_numbers(start_date, end_date) == nil
no_rooms_available
else
reservations << Hotel::Reservation.new(
room: Hotel::Room.new(room_number: list_available_room_numbers(start_date, end_date), room_cost: 200),
date_range: Hotel::Date_Range.new(start_date: start_date,
end_date: end_date))
end
end

def find_reservation(start_date, end_date)
found_reservations = []
reservations.each do |reservation|

if (reservation.date_range.start_date >= start_date && reservation.date_range.start_date < end_date) ||
(reservation.date_range.end_date >= start_date && reservation.date_range.end_date <= end_date)
found_reservations.push(reservation)
end
end
found_reservations
end

def list_available_rooms (start_date, end_date)
Copy link
Copy Markdown

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/hotel_system.rb:52: warning: parentheses after method name is interpreted as an argument list, not a decomposed argument

This is because you put a space after list_available_rooms

booked_rooms = list_reserved_room_numbers(start_date, end_date)
available_rooms = []
rooms.each do |room|
if booked_rooms.include?(room.room_number) == false
available_rooms.push(room)
end
end
available_rooms
end

def list_reserved_room_numbers(start_date, end_date)
reserved_room_numbers = []
reservations.each do |reservation|

if (reservation.date_range.start_date >= start_date && reservation.date_range.start_date < end_date) ||
(reservation.date_range.end_date >= start_date && reservation.date_range.end_date <= end_date)
reserved_room_numbers.push(reservation.room.room_number)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The discussion of POODR chapter 4 probably helps here: your Hotel class should be delegating this work to the other two classes as in the example that I gave.

end
end
reserved_room_numbers

end
end
end


11 changes: 11 additions & 0 deletions lib/refactors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Room Class

I definitely did not need a room class. My current room class made things has no useful methods. Also, since the rooms instance variable
in hotel system is an array of room objects, it made it harder to access the room numbers. I ended up making unnecessary methods in hotel system
like "list_available_room_numbers" because I had to extract the room numbers from the room objects. If I had no room class and
simply just made the rooms instance variable in hotel system an array of integers 1 -20, I wouldn't need the extra methods.
Also, there was no reason to list the room cost. Every room is $200 and total cost is calculated in the reservations class.
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

That seems wrong to me? Isn't it 200 per day?


Use of Let

Try to use let when you refactor hotel. Before is useful, but I feel like let could dry up your code.
18 changes: 18 additions & 0 deletions lib/reservation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require "pry"

module Hotel
class Reservation
attr_reader :room, :date_range, :total_cost

def initialize(room: , date_range: )
@room = room
@date_range = date_range
@total_cost = calculate_reservation_cost
end

def calculate_reservation_cost
duration_of_stay = date_range.end_date - date_range.start_date
duration_of_stay.to_i * 200
end
end
end
17 changes: 17 additions & 0 deletions lib/room.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module Hotel
class Room
attr_reader :room_number, :room_cost

def initialize(room_number: , room_cost:)
@room_number = room_number
@room_cost = room_cost
end

def self.load_all
(1..20).map do |i|
self.new(room_number: i,
room_cost: 200)
end
end
end
end
28 changes: 28 additions & 0 deletions test/date_range_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
require_relative "test_helper"
require "date"

describe "Date_Range" do
describe "Date_Range instantiation" do
before do

start_date = Date.parse("2019-02-03")
end_date = Date.parse("2019-02-05")

@date_range = Hotel::Date_Range.new(
start_date: start_date,
end_date: end_date
)
end

it "is an instance of Date_Range" do
expect(@date_range).must_be_kind_of Hotel::Date_Range
end

it "throws an argument error if start date is after end date" do
start_date = Date.parse("2019-02-05")
end_date = Date.parse("2019-02-04")

expect{Hotel::Date_Range.new(start_date: start_date, end_date: end_date)}.must_raise ArgumentError
end
end
end
105 changes: 105 additions & 0 deletions test/hotel_system_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
require_relative "test_helper"
require "Pry"
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

you left in all of your require 'pry''s, which causes a warning.

require "minitest/skip_dsl"

describe "Hotel_System" do
before do
rooms = Hotel::Room.load_all
reservations =
[
Hotel::Reservation.new(
room: Hotel::Room.new(room_number: 3, room_cost: 200),
date_range: Hotel::Date_Range.new(start_date: Date.parse("2019-02-03"),
end_date: Date.parse("2019-02-05")))
]

@hotel_system = Hotel::Hotel_System.new(rooms: rooms,
reservations: reservations)
end

describe "Hotel_System instantiation" do
it "is an instance of Hotel" do
expect(@hotel_system).must_be_kind_of Hotel::Hotel_System
end

it "stores an instance of room" do
expect(@hotel_system.rooms[0]).must_be_kind_of Hotel::Room
end

it "stores an instance of reservation" do
expect(@hotel_system.reservations[0]).must_be_kind_of Hotel::Reservation
end
end

describe "list_rooms" do
it "returns a list of all the rooms in the hotel" do

expect(@hotel_system.list_rooms).must_be_kind_of Array

expect(@hotel_system.list_rooms.length).must_equal 20
expect(@hotel_system.list_rooms[0]).must_be_kind_of Hotel::Room
end
end

describe "make_reservation" do
it "makes a reservation when given a date range" do
start_date = Date.parse("2019-02-08")
end_date = Date.parse("2019-02-12")

result = @hotel_system.make_reservation(start_date, end_date)

expect(result).must_be_kind_of Array
expect(result[1]).must_be_kind_of Hotel::Reservation
expect(result[1].date_range.start_date).must_equal start_date
expect(result[1].date_range.end_date).must_equal end_date
end

it "pushes a new reservation to Hotel_System's reservation instance variable" do
start_date = Date.parse("2019-02-08")
end_date = Date.parse("2019-02-12")

result = @hotel_system.make_reservation(start_date, end_date)
Copy link
Copy Markdown

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_system_test.rb:61: warning: assigned but unused variable - result


expect(@hotel_system.reservations.length).must_equal 2
end

it "Raises an argument error if there are no rooms available for the given date range" do
start_date = Date.parse("2019-02-03")
end_date = Date.parse("2019-02-05")

19.times do
@hotel_system.make_reservation(start_date, end_date)
end

expect{@hotel_system.make_reservation(start_date, end_date)}.must_raise ArgumentError
end
end

describe "find_reservation" do
it "finds reservations booked for a specific date" do
start_date = Date.parse("2019-12-11")
end_date = Date.parse("2019-12-17")

@hotel_system.make_reservation(start_date, end_date)
result = @hotel_system.find_reservation(start_date, end_date)

expect(result).must_be_kind_of Array
expect(result[0]).must_be_kind_of Hotel::Reservation
expect(result.length).must_equal 1
end
end

describe "list_available_rooms" do
it "lists available rooms for a given date" do
start_date = Date.parse("2019-02-03")
end_date = Date.parse("2019-02-05")

result = @hotel_system.list_available_rooms(start_date, end_date)

expect(result).must_be_kind_of Array
result.each {|room| expect(room).must_be_kind_of Hotel::Room}
expect(result.length).must_equal 19
end
end
end

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

describe "Reservation" do
before do
room = Hotel::Room.new(room_number: 1, room_cost: 200)
date_range = Hotel::Date_Range.new(start_date: Date.parse("2019-02-03"), end_date: Date.parse("2019-02-05"))

@reservation = Hotel::Reservation.new(
room: room,
date_range: date_range
)
end

describe "Reservation instantiation" do
it "is instance of Reservation" do
expect(@reservation).must_be_kind_of Hotel::Reservation
end

it "stores an instance of room" do
expect(@reservation.room).must_be_kind_of Hotel::Room
end

it "stores an instance of date_range" do
expect(@reservation.date_range).must_be_kind_of Hotel::Date_Range
end
end

describe "calculate_reservation_cost method" do
it "calculates the cost of a reservation" do
result = @reservation.calculate_reservation_cost

expect(result).must_be_kind_of Integer
expect(result).must_be :>, 0
end
end
end
38 changes: 38 additions & 0 deletions test/room_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
require_relative "test_helper"

describe "Room" do
before do
#Arrange
room_number = 2
room_cost = 200

#Act
@room = Hotel::Room.new(
room_number: room_number,
room_cost: room_cost
)
end

describe "Room instantiation" do
it "is an instance of Room" do
expect(@room).must_be_kind_of Hotel::Room
end
end

describe "load_all method" do
it "returns array of 20 elements" do
result = Hotel::Room.load_all

expect(result).must_be_kind_of Array
expect(result.length).must_equal 20
end

it "each element in returned array is a room object" do
result = Hotel::Room.load_all

result.each do |object|
expect(object).must_be_kind_of Hotel::Room
end
end
end
end
9 changes: 9 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
# Add simplecov
require "simplecov"
SimpleCov.start

require "minitest"
require "minitest/autorun"
require "minitest/reporters"
require "minitest/skip_dsl"

Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new

# require_relative your lib files here!
require_relative '../lib/hotel_system'
require_relative '../lib/reservation'
require_relative '../lib/room'
require_relative '../lib/date_range'