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

Dikla Rikovitch Hotel Octos #26

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
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
51 changes: 51 additions & 0 deletions design-activity.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
1. What classes does each implementation include? Are the lists the same?
answer: Each implementation include three classes: CartEntry, ShoppingCart, Order.
in implementation A the total price is calculated only in order class and in implementation B the total price is calculated for each class in separate

2. Write down a sentence to describe each class.
answer:
Implementation A:
class CartEntry: Store the unit price and quantity
class ShoppingCart: Store an array of entries
class Order: Store an instant of ShoppingCart and calculate order (price * quantity) after adding Tax
Implementation B:
class CartEntry: Store the unit price and quantity and calculate (price * quantity)
class ShoppingCart: Store an array of entries and calculate prices for all the entries
class Order: Adding Tax for what was already calculate in ShoppingCart class

3. How do the classes relate to each other? It might be helpful to draw a diagram on a whiteboard or piece of paper.
answer: class CartEntry store the unit price and quantity, class ShoppingCart store array of entries and class Order store ShoppingCart object and add Tax

4. What data does each class store? How (if at all) does this differ between the two implementations?
answer:
Implementation A:
class CartEntry: Store the unit price and quantity
class ShoppingCart: Store an array of entries
class Order: Store an instant of ShoppingCart and calculate order (price * quantity) after adding Tax
Implementation B:
class CartEntry: Store the unit price and quantity and calculate (price * quantity)
class ShoppingCart: Store an array of entries and calculate prices for all the entries
class Order: Adding Tax for what was already calculate in ShoppingCart class

5. What methods does each class have? How (if at all) does this differ between the two implementations?
answer:
Implementation A:
class CartEntry: initialize
class ShoppingCart: initialize
class Order: initialize, total_price
Implementation B:
class CartEntry: initialize, price
class ShoppingCart: initialize, price
class Order: initialize, total_price

6. 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?
- Does total_price directly manipulate the instance variables of other classes?

7. If we decide items are cheaper if bought in bulk, how would this change the code? Which implementation is easier to modify?
answer: We will need to use an if statement and it will be easier to modify implementation A

8. Which implementation better adheres to the single responsibility principle?
answer: Implementation B is better

9. Bonus question once you've read Metz ch. 3: Which implementation is more loosely coupled?
37 changes: 37 additions & 0 deletions lib/management.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
module Hotel
class Management

# PRICE_PER_NIGHT = 200
attr_reader :rooms, :price_per_night

def initialize
@rooms = []
(1..20).each do |num|
@rooms << Hotel::Room.new(num)
end
@reservations = []

end

def reserve_room(room, check_in, check_out)
reservation = Hotel::Reservation.new(room, check_in, check_out)
@reservations << reservation
return reservation
end

def get_reservations_for_date(date)
reservations_for_date = []
@reservations.each do |reservation|
if reservation.dates.include?(date) #check_in == date
reservations_for_date << reservation
end
end
return reservations_for_date
end





end # end of class
end # end of module
25 changes: 25 additions & 0 deletions lib/period.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require 'pry'
module Hotel
class Period

attr_reader :check_in, :check_out

def initialize(check_in, check_out)
@check_in = check_in
@check_out = check_out
end

def is_valid?

Choose a reason for hiding this comment

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

Methods that end in a ? should return true or false

(@check_out != nil && @check_in != nil) && !(@check_out <= @check_in)
end

def num_of_nights
return check_out - check_in
end

def include?(date)
return date >= check_in && date < check_out
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 @@
module Hotel
class Reservation

attr_reader :room, :dates, :total_cost
def initialize(room, check_in, check_out)
@room = room
@dates = Hotel::Period.new(check_in, check_out)
if [email protected]_valid? || [email protected]_available(@dates)
raise ArgumentError.new("This dates are not availabe")
end
@room.mark_dates_as_reserved(@dates)
@total_cost = @dates.num_of_nights * @room.price_per_night
end


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

PRICE_PER_NIGHT = 200
attr_reader :room_num, :price_per_night

def initialize(num)
@room_num = num
@price_per_night = PRICE_PER_NIGHT
@reserved_dates = []
end



def mark_dates_as_reserved(reservation_period)
(reservation_period.check_in...reservation_period.check_out).each do |date|
@reserved_dates << date
end
end

def is_reserved_for_date(date)
if date != nil
@reserved_dates.include?(date)
else
raise ArgumentError.new("Invalid date")
end
end

def is_available(query_period)
(query_period.check_in...query_period.check_out).each do |date|
if is_reserved_for_date(date)
return false
end
end
return true
end
end
end
54 changes: 54 additions & 0 deletions specs/management_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
require_relative 'spec_helper'

describe 'Management class' do
describe 'initialize' do
it "can be created" do
hotel_management = Hotel::Management.new
hotel_management.must_be_instance_of Hotel::Management
hotel_management.rooms.must_be_kind_of Array
reservations = Hotel::Management.new
reservations.must_be_instance_of Hotel::Management
end
it "can access the list of all of the rooms in the hotel" do
hotel = Hotel::Management.new
hotel.rooms.length.must_equal 20
end

it "there should be 20 total rooms" do
hotel = Hotel::Management.new
hotel.rooms.count.must_equal 20
end

end
describe 'reserve_room' do
it 'return reservation' do
room = Hotel::Room.new(1)
check_in = Date.new(2018, 03, 01)
check_out = Date.new(2018, 03, 03)
reservation = Hotel::Management.new.reserve_room(room, check_in, check_out)
reservation.dates.check_in.must_equal check_in
reservation.dates.check_out.must_equal check_out
end

it 'raise an error if room is already reserved' do
room = Hotel::Room.new(1)
check_in = Date.new(2018, 03, 01)
check_out = Date.new(2018, 03, 03)
reservation = Hotel::Management.new.reserve_room(room, check_in, check_out)
proc{ Hotel::Management.new.reserve_room(room, check_in, check_out)}.must_raise ArgumentError
end
end

describe 'get_reservations_for_date' do
it 'return an array of reservations for date' do
management = Hotel::Management.new
room = Hotel::Room.new(1)
date = Date.new(2018, 03, 02)
check_in = Date.new(2018, 03, 01)
check_out = Date.new(2018, 03, 03)
management.reserve_room(room, check_in, check_out)
management.get_reservations_for_date(date).must_be_kind_of Array
management.get_reservations_for_date(date).length.must_equal 1
end
end
end
69 changes: 69 additions & 0 deletions specs/period_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
require_relative 'spec_helper'

describe 'Period class' do

describe 'initialize' do
it 'Can initialize check in and check out' do
check_in = Date.new(2018, 03, 01)
check_out = Date.new(2017, 03, 03)
period = Hotel::Period.new(check_in, check_out)
period.check_in.must_equal check_in
period.check_out.must_equal check_out
period.must_be_instance_of Hotel::Period
end

it "is an instance of Date" do
check_in = Date.new(2018, 03, 01)
check_out = Date.new(2017, 03, 03)
check_in.must_be_kind_of Date
check_out.must_be_kind_of Date
end
end

describe 'num_of_nights' do
it "accurately calculates the number of night" do
check_in = Date.new(2018, 03, 16)
check_out = Date.new(2018, 03, 19)
period = Hotel::Period.new(check_in, check_out)
period.num_of_nights.must_equal 3
end
end

describe "is_valid?" do
it 'cannot have negative number of days' do
check_in = Date.new(2018, 03, 01)
check_out = Date.new(2018, 02, 25)
Hotel::Period.new(check_in, check_out).is_valid?.must_equal false
end

it 'cannot have 0 days' do
check_in = Date.new(2018, 03, 01)
check_out = Date.new(2018, 03, 01)
Hotel::Period.new(check_in, check_out).is_valid?.must_equal false
end

it 'valid dates' do
check_in = Date.new(2018, 03, 01)
check_out = Date.new(2018, 03, 03)
Hotel::Period.new(check_in, check_out).is_valid?.must_equal true
end
end

describe 'include?' do
it "returns true if the date is between check_in and check_out" do
check_in = Date.new(2018, 03, 01)
check_out = Date.new(2018, 03, 04)
period = Hotel::Period.new(check_in, check_out)
date = Date.new(2018, 03, 03)
period.include?(date).must_equal true
end

it "returns false if the date is not between check_in and check_out" do
check_in = Date.new(2018, 03, 01)
check_out = Date.new(2018, 03, 04)
period = Hotel::Period.new(check_in, check_out)
date = Date.new(2018, 03, 05)
period.include?(date).must_equal false
end
end
end
42 changes: 42 additions & 0 deletions specs/reservation_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
require_relative 'spec_helper'

describe 'Reservation class' do

describe 'initialize' do
it 'can be created' do
room = Hotel::Room.new(1)
check_in = Date.new(2018,03,01)
check_out = Date.new(2018,03,03)
dates = Hotel::Period.new(check_in, check_out)
reservation = Hotel::Reservation.new(room, check_in, check_out)
room.must_be_instance_of Hotel::Room
dates.must_be_instance_of Hotel::Period
end
it 'can calculate the total cost' do
reservation = Hotel::Reservation.new(Hotel::Room.new(1), Date.new(2018,03,01), Date.new(2018,03,03))
room = Hotel::Room.new(1)
dates = Hotel::Period.new(Date.new(2018,03,01), Date.new(2018,03,03))
room.mark_dates_as_reserved(dates)
price_per_night = room.price_per_night
total_cost = dates.num_of_nights * price_per_night
reservation.total_cost.must_equal 400
end
it 'raise an error if the date range is not availabe' do
room = Hotel::Room.new(1)
dates = Hotel::Period.new(Date.new(2018,03,01), Date.new(2018,03,03))
reservation = Hotel::Reservation.new(room, Date.new(2018,03,01), Date.new(2018,03,03))
room.mark_dates_as_reserved(dates)
proc{ Hotel::Reservation.new(room, Date.new(2018,03,01), Date.new(2018,03,02))}.must_raise ArgumentError

# new_dates = Hotel::Period.new(Date.new(2018,03,01), Date.new(2018,03,02))
end

it 'raise an error if the date is in_valid' do
room = Hotel::Room.new(1)
dates = Hotel::Period.new(Date.new(2018,03,01), Date.new(2018,03,01))
reservation = Hotel::Reservation.new(room, Date.new(2018,03,01), Date.new(2018,03,03))
room.mark_dates_as_reserved(dates)
proc{ Hotel::Reservation.new(room, Date.new(2018,03,01), Date.new(2018,03,02))}.must_raise ArgumentError
end
end
end
Loading