forked from AdaGold/hotel
-
Notifications
You must be signed in to change notification settings - Fork 45
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
diklaaharoni
wants to merge
7
commits into
Ada-C9:master
Choose a base branch
from
diklaaharoni:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9fadd8e
created a Rakefile, spec helper, specs and classes
diklaaharoni 752fda9
i wrote the tests for room class and i pass
diklaaharoni e9dd289
I wrote the tests for reservation and I pass
diklaaharoni 39c7da7
I wrote the tests for get reservation for date and I pass
diklaaharoni d850d6b
I add a test for reserve_room in reservation class to raise and error…
diklaaharoni 18c7ed4
I changed the is_valid? method to return true or false and I also mad…
diklaaharoni 1f93b8b
added design-activity.md file
diklaaharoni File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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? | ||
(@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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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