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

Pipes - Tanisha - Hotel #37

Open
wants to merge 62 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
62 commits
Select commit Hold shift + click to select a range
516eacb
project setup - rakefile, specs, and lib files
tanham Sep 5, 2017
bc70bdf
fix file naming in spec_helper
tanham Sep 5, 2017
e64f41a
write initialize tests and code
tanham Sep 5, 2017
5b103ba
set up sekelton for methos and tests
tanham Sep 6, 2017
ee9d593
add 20 room numbers to rooms array and add is_available method
tanham Sep 6, 2017
6119117
add tests for rooms instance variable
tanham Sep 6, 2017
02055df
initialize DateRange method
tanham Sep 6, 2017
aa8219a
add test for check DateRange initialize
tanham Sep 6, 2017
bffecd2
add tests that verify Date Range instance variables
tanham Sep 6, 2017
3431b4c
add return date range method and tests
tanham Sep 6, 2017
58ea02d
add reservation class and initilize method and tests
tanham Sep 6, 2017
4326348
add booker class and spec files
tanham Sep 6, 2017
fb53b43
move methods from hotel class to reservations class
tanham Sep 6, 2017
3c68423
add initialize and add_reservatin method
tanham Sep 7, 2017
763b52c
change @date_range instance variable to be a new instance of DateRang…
tanham Sep 7, 2017
99ba523
change variable names
tanham Sep 7, 2017
70b18ff
add reserve_room
tanham Sep 8, 2017
e52b31a
add new instance of DateRange and write test
tanham Sep 8, 2017
24c2dbc
add test to validate @reservations instance variable
tanham Sep 8, 2017
1b499bd
refactor access_reservations test
tanham Sep 8, 2017
713754e
add room_id variable to reserve room method
tanham Sep 8, 2017
6eb2839
add room_id variable
tanham Sep 8, 2017
00c1d51
remove require pry
tanham Sep 8, 2017
1d01f42
add file
tanham Sep 8, 2017
95c52a2
remove get_total method and test, this method will now live in the re…
tanham Sep 8, 2017
30b7644
remove @id instance variable and test varables for it
tanham Sep 8, 2017
468d623
add code and test for get_total method
tanham Sep 8, 2017
01acdbb
add error handling test
tanham Sep 8, 2017
18309f5
add error handling for invalid date range and tests
tanham Sep 8, 2017
555ad70
add view_available rooms method
tanham Sep 8, 2017
35e588d
add tests for view_available_rooms method
tanham Sep 8, 2017
b2d0764
remove error handling test because it goes in DateRange class
tanham Sep 8, 2017
4367173
add overlap? method and tests
tanham Sep 8, 2017
7237aaa
fix whitespace
tanham Sep 8, 2017
275cdc5
remove '@' from instance mehtods with attr_readers
tanham Sep 8, 2017
d4ad0cc
add awewome_print
tanham Sep 8, 2017
6246d58
add attr_reader for room_id instance variable
tanham Sep 11, 2017
f3539c8
revise view_available_rooms methods and tests
tanham Sep 11, 2017
b610edf
revise overlap? method
tanham Sep 11, 2017
2965813
add error handling for reserving unavailabe rooms
tanham Sep 11, 2017
377a537
update spec_helper to include blocks.rb file
tanham Sep 11, 2017
0acdb2b
add initialize blocks class
tanham Sep 11, 2017
62f5335
skip test
tanham Sep 11, 2017
50dc7d5
fix whitespace
tanham Sep 11, 2017
5e425b9
update access_reservations method
tanham Sep 11, 2017
84771c5
update create_block method and tests
tanham Sep 11, 2017
6e257cb
change test
tanham Sep 11, 2017
0797cb3
delete booker files because I never made a booker class
tanham Sep 11, 2017
ccb5f6a
update create block method
tanham Sep 11, 2017
a4ccc23
add design file
tanham Sep 25, 2017
2f41d70
answer evaluating responsibility questions
tanham Sep 25, 2017
12d39b8
change test dates to future date
tanham Sep 26, 2017
c802e2f
change HotelClass to Hotel
tanham Sep 26, 2017
ac4aa9f
change class names using proper naming conventions
tanham Sep 27, 2017
9577569
add test for reserve_room method
tanham Oct 2, 2017
3595c4c
change num_of_nights from instance variable to a method and restructu…
tanham Oct 2, 2017
f8aa314
remove number_of_nights instance variable
tanham Oct 2, 2017
3e2663d
remove unneeded comments
tanham Oct 2, 2017
364de8f
modify view_avaiable_rooms and reserve_rooms methods
tanham Oct 2, 2017
f9244a3
modify how I instaniated hotel_test to better work with my program
tanham Oct 2, 2017
3fae1c7
add valid_reservations array before to access_reservations method
tanham Oct 2, 2017
eed8807
update design-activity.md
tanham Oct 2, 2017
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/
/design.md

# Used by dotenv library to load environment variables.
# .env
Expand Down
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
55 changes: 55 additions & 0 deletions design-activity.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
* What classes does each implementation include? Are the lists the same?

a. Both implementation A and implementation B have three classes: CartEntry, ShoppingCart, and Order.

* Write down a sentence to describe each class.

a. CartEntry class - In both implementations, this class initializes two instance variable unit price and quantity. In implementation B the CartEntry class also contains a price method that calculates price by multiplying unit price and quantity.

b. ShoppingCart class- In implementation A, ShoppingCart is simply a collection of entries. In implementation B, ShoppingCart is a collection of entries and calculates the price for each entry.

c. Order class - Order creates a new instance of `@cart`, sets a constant variable for sales tax, and calculates the total price of the cart. Implementation A calculates the entry total and multiplies it by the sales tax while implementation B simply sets the subtotal to `@cart.price` and multiplies the sales tax.


* How do the classes relate to each other? It might be helpful to draw a diagram on a whiteboard or piece of paper.

a. It seems that Order knows about ShoppingCart and ShoppingCart should know about CartEntry (this is assuming that each CartEntry gets pushed on to the `@entries` variable).

* What data does each class store? How (if at all) does this differ between the two implementations?

a. CartEntry stores `@unit_price` and `@quantity`, ShoppingCart stores `@entries`, Order stores `@cart` and `SALES_TAX`. The data is the same across both implementations.

* What methods does each class have? How (if at all) does this differ between the two implementations?

a. Both implementations have a total_price method but implementation B also has two price methods. In implementation B, a price in calculated for each cart entry, the shopping cart, and the entire order. In implementation A, price is only calculated once in the Order class.

* Consider the Order#total_price method. In each implementation:
* Is logic to compute the price delegated to "lower level" classes like ShoppingCart and CartEntry, or is it retained in Order?

a. implementation A - logic to compute price is all retained in the Order class.
b. implementation B - logic to compute price is delegated to the "lower level" classes, ShoppingCart and CartEntry.

* Does total_price directly manipulate the instance variables of other classes?
a. implementation A - Yes
b. implementation B - No

* If we decide items are cheaper if bought in bulk, how would this change the code? Which implementation is easier to modify?

a. To make this modification, you would have to add a bulk discount to the method that calculates price. Implementation B is easier to modify because you can add the bulk discount to the CartEntry price method.

* Which implementation better adheres to the single responsibility principle?

a. Implementation B

* Bonus question once you've read Metz ch. 3: Which implementation is more loosely coupled?

a. Implementation B


# Revisiting Hotel

Describe in `design-activity.md` what changes you would need to make to improve this design, and how why the resulting design would be an improvement.

* In the DateRange class, I was calculating number_of_nights in using aq instance variable and making use of that instance variable in the Reservation Class to return the price in the total_price method. The change I made here was change number_of_nights from an instance variable to a instance method and I called on that instance method in the total_price method in the Reservation class.

* In the view_available_rooms method in the Hotel class, I got rid of the available_room_nums instance variable and made available_rooms a local variable that references the instance variable rooms. This helped to get rid of some redundancy and dry up my code. With this change, I was also able to reference the view_available_rooms method inside of the reserve_room method. I also removed the reserved_room_nums instance variable and made reserved_rooms into a local variable to dry up the code.
29 changes: 29 additions & 0 deletions lib/block.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module Hotel
class Block
attr_reader :date_range, :block_rooms, :discount, :hotel
def initialize
@hotel = Hotel.new
@block_rooms = []
@discount = 0.2
end

# currently broken/incomplete
def create_block(rooms,check_in,check_out)
rooms = []
# check what rooms are available
available_for_block = hotel.view_available_rooms(check_in,check_out)
# push 5 available room onto block_rooms
# ap "printing available:#{available_for_block}"
available_for_block.each do |room|
if rooms.length < 5
rooms << room
end
end
# rooms.each do |room|
# hotel.reserve_room(room_id, check_in, check_out)
# end
# ap "printing rooms :#{rooms}"
end

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

module Hotel
class DateRange
attr_reader :check_in, :check_out
def initialize(check_in, check_out)
@check_in = Date.parse(check_in)
@check_out = Date.parse(check_out)
# @number_of_nights = (@check_out - @check_in).to_i
unless @check_in > Date.today
raise ArgumentError.new("That date has passed.")
end
unless @check_in < @check_out
raise ArgumentError.new("Check in date must be before Check out date.")
end
end # end of initialize

def overlap?(start_date, end_date)
# comparing one instance of date to another
if (self.check_in < Date.parse(end_date)) && (self.check_out >= Date.parse(start_date))
return true
else
return false
end
end

def number_of_nights
# changed number_of_nights from an intance variable to an instance method
return (@check_out - @check_in).to_i
end

def return_date_range
date_range = "#{check_in} to #{check_out}"
# Hotel::HotelClass.reservations << date_range
puts date_range
return date_range
end # end of return_date_range
end # end of DateRange Clas
end # end of Module
69 changes: 69 additions & 0 deletions lib/hotel.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
require_relative 'reservation'

module Hotel
class Hotel
attr_reader :rooms, :reservations, :reserved
def initialize
# access the list of all of the rooms in the hotel
@rooms = (1..20).to_a
@reservations = []
@reserved = []
end

def view_available_rooms(check_in, check_out)
# view a list of rooms that are not reserved for a given date range
# loop thru reservations.date_range to see if it include the date_range passed in, if not return room_ids

available_rooms = @rooms
reserved_rooms = []

reservations.each do |reservation|
if reservation.date_range.overlap?(check_in, check_out)
reserved << reservation
end
end

reserved.each do |reserved_room|
reserved_rooms << reserved_room.room_id
end

available_rooms -= reserved_rooms
return available_rooms
end

def reserve_room(room_id, check_in, check_out)
unless @rooms.include?(room_id)
raise ArgumentError.new("That room does not exist")
end

unless view_available_rooms(check_in, check_out).include?(room_id)
raise ArgumentError.new("Room #{room_id} is not available for those dates")
end

new_reservation = Reservation.new(room_id, check_in, check_out)
reservations << new_reservation

return new_reservation
end

def access_reservations(date)
# access the list of reservations for a specific date
# loop over @reservations.date_range
# if date_range includes passed in date
# return reservations

valid_reservations = []

reservations.each do |reservation|
# broken
# the if statement seems to be always returning false and what is being returned from reservations are
# therefore nothing is being pushed on valid_reservations
if (reservation.date_range.check_in..reservation.date_range.check_out).include?(date)
valid_reservations << reservation
end
end
return valid_reservations
end

end
end
16 changes: 16 additions & 0 deletions lib/reservation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module Hotel
class Reservation
attr_reader :date_range, :room_id, :cost_per_night
def initialize(room_id,check_in,check_out)
# @id = id
@date_range = DateRange.new(check_in,check_out)
@room_id = room_id
@cost_per_night = 200
end

def get_total
# get the total cost for a given reservation
return date_range.number_of_nights * @cost_per_night
end
end
end
42 changes: 42 additions & 0 deletions specs/block_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
require_relative 'spec_helper'

describe "Block class" do
before do
@block_test = Hotel::Block.new#("2017-11-13", "2017-11-15")
@hotel_test = Hotel::Hotel.new
end
describe "Initialize Block Class" do
it "Block.new is an instance of Hotel module" do
@block_test.must_be_instance_of Hotel::Block
end
it "Responds to date_range variable" do
@block_test.must_respond_to :date_range
@block_test.block_rooms.must_be_kind_of Array
end
it "Responds to block_rooms variable" do
@block_test.must_respond_to :block_rooms
end
it "Responds to discount variable" do
@block_test.must_respond_to :discount
end
it "Responds to hotel variable" do
@block_test.must_respond_to :hotel
end
end
describe "Create Block method" do
it "Returns an array" do
hotel_test2 = Hotel::Hotel.new
hotel_test2.reserve_room(2,"2017-11-15", "2017-11-18")
hotel_test2.reserve_room(3,"2017-11-15", "2017-11-18")
hotel_test2.reserve_room(1,"2017-11-15", "2017-11-18")
ap hotel_test2.view_available_rooms("2017-11-15", "2017-11-18")
@block_test.create_block(2,"2017-11-15", "2017-11-17").must_be_kind_of Array
# @block_test.block_rooms.length.must_equal 5
end
it "Contains 5 rooms" do
skip
@block_test.create_block([4,5,6,7,8],"2017-11-15", "2017-11-17")
@block_test.block_rooms.length.must_equal 5
end
end
end # end of Block class describe
46 changes: 46 additions & 0 deletions specs/date_range_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
require_relative 'spec_helper'

describe "DateRange Class" do
before do
@date_range_test = Hotel::DateRange.new("2017-11-12", "2017-11-15")
end
describe "Initialize DateRange Class" do
it "DateRange.new is an instance of Hotel module" do
@date_range_test.must_be_instance_of Hotel::DateRange
end
it "Responds to check_in variable" do
@date_range_test.must_respond_to :check_in
end
it "Responds to check_out variable" do
@date_range_test.must_respond_to :check_out
end
it "Responds to number_of_nights variable" do
@date_range_test.must_respond_to :number_of_nights
end
it "Equals number_of_nights" do
@date_range_test.number_of_nights.must_equal 3
end
it "raises an ArgumentError if check_in date has passed" do
proc {
Hotel::DateRange.new("2017-9-1", "2017-9-12")
}.must_raise ArgumentError
end
it "raises an ArgumentError if check_out date is before check_in date" do
proc {
Hotel::DateRange.new("2017-11-15", "2017-11-12")
}.must_raise ArgumentError
end
end # end of describe Initialize DateRange Class
describe "Overlap? method" do
it "Checks for overlapping dates" do
@date_range_test = Hotel::DateRange.new("2017-11-12", "2017-11-15")
@date_range_test.overlap?("2017-11-13", "2017-11-14").must_equal true
@date_range_test.overlap?("2017-11-16", "2017-11-17").must_equal false
end
end
describe "Return_date_range method" do
it "Returns to a string" do
@date_range_test.return_date_range.must_be_kind_of String
end
end # end of describe return_date_range method
end # end of describe DateRange class
62 changes: 62 additions & 0 deletions specs/hotel_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
require_relative 'spec_helper'
require 'awesome_print'

describe "Hotel" do
before do
@hotel_test = Hotel::Hotel.new
end
describe "Initialize Hotel Class" do
it "Hotel.new is an instance of Hotel module" do
@hotel_test.must_be_instance_of Hotel::Hotel
end
it "Responds to rooms variable" do
@hotel_test.must_respond_to :rooms
end
it "Rooms variable is an array" do
@hotel_test.rooms.must_be_kind_of Array
end
it "Rooms variable has 20 rooms" do
@hotel_test.rooms.length.must_equal 20
end
end

describe "Reserve_room method" do
it "Responds to Hotel::Reservation" do
@hotel_test.reserve_room(3,"2017-11-12", "2017-11-15")
@hotel_test.reservations[0].must_be_instance_of Hotel::Reservation
end
it "Raises an error if asked to reserve an unavailable room " do
@hotel_test.reserve_room(3,"2017-11-12", "2017-11-15")
proc {
@hotel_test.reserve_room(3,"2017-11-13", "2017-11-15")
}.must_raise ArgumentError
end
it "Raises an error if room does not exist" do
proc {
@hotel_test.reserve_room(23,"2017-11-13", "2017-11-15")
}.must_raise ArgumentError
end
end
describe "Access_reservations method" do
it "Responds to Hotel" do
@hotel_test.must_respond_to :reservations
end
it "Is an Array" do
@hotel_test.reserve_room(3,"2017-11-12", "2017-11-15")
@hotel_test.access_reservations("2017-11-12").must_be_kind_of Array
end
it "Has one element" do
@hotel_test.reserve_room(20,"2017-11-12", "2017-11-15")
@hotel_test.reserve_room(20,"2017-11-16", "2017-11-20")
skip
@hotel_test.access_reservations("2017-11-12").length.must_equal 1
end
end
describe "View_available_rooms method" do
it "Return a list of available rooms for a given date range" do
@hotel_test.reserve_room(19,"2017-11-15", "2017-11-18")
@hotel_test.view_available_rooms("2017-11-15", "2017-11-18").must_be_kind_of Array
@hotel_test.view_available_rooms("2017-11-15", "2017-11-18").wont_include 19
end
end
end
Loading