-
Notifications
You must be signed in to change notification settings - Fork 40
Space - Cathy #21
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
base: master
Are you sure you want to change the base?
Space - Cathy #21
Changes from all commits
94cb11e
0b568c4
f3ed103
6cd1e66
8f69ba5
7f9a473
d6abc6e
1d16a53
255b4a8
2e1e6b5
c35d83e
326034f
205f236
543b708
17d6682
9026b3e
81831db
1c4c686
f5d36d4
f074a79
4baafe9
83984e0
a3bdc8f
b4ae83e
e008545
86d90f6
9667db6
eb2d44d
d85a9d3
e8b9edb
c997c2c
8a8177c
a427012
36551ca
c65c308
1b63121
ee8f691
2f46e39
9929095
d1a89b5
bd2940f
434be2e
b67560e
8df9b3d
ff0ac77
b017ab5
def524f
24a9b36
5047dc8
9f18e56
8c0c0cb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -48,3 +48,4 @@ build-iPhoneSimulator/ | |
|
|
||
| # unless supporting rvm < 1.11.0 or doing something fancy, ignore this: | ||
| .rvmrc | ||
| coverage | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| require_relative 'room' | ||
| require_relative 'date_range' | ||
| require_relative 'reservation' | ||
|
|
||
|
|
||
| module Hotel | ||
| class Block < Reservation | ||
| attr_reader :block | ||
|
|
||
| def initialize(date_range, room_id, cost = 200.00, block = 0) | ||
|
|
||
| super(date_range, room_id, cost) | ||
| @block = block | ||
|
|
||
| end | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,52 @@ | ||||||||
| require 'date' | ||||||||
|
|
||||||||
| module Hotel | ||||||||
| class DateRange | ||||||||
|
|
||||||||
| attr_accessor :start_date, :end_date | ||||||||
|
|
||||||||
| # your library code should assume that it's receiving Date objects to start | ||||||||
| def initialize(start_date, end_date) | ||||||||
| raise ArgumentError.new("Please pass in Date class instances.") if !(start_date.is_a? Date) || !(end_date.is_a? Date) | ||||||||
| raise ArgumentError.new("Live in the present! Dates should not be prior to today.") if start_date < Date.today | ||||||||
| raise ArgumentError.new("End date should not be earlier than or the same day as start date.") if end_date <= start_date | ||||||||
|
|
||||||||
| @start_date = start_date | ||||||||
| @end_date = end_date | ||||||||
| end | ||||||||
|
|
||||||||
| def count_nights | ||||||||
| nights = (@end_date - @start_date).to_i | ||||||||
| return nights | ||||||||
|
Comment on lines
+19
to
+20
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You don't need to assign to a variable before returning:
Suggested change
|
||||||||
| end | ||||||||
|
|
||||||||
| def include_date(date) | ||||||||
| return true if date >= @start_date && date < @end_date | ||||||||
| return false | ||||||||
|
Comment on lines
+24
to
+25
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can directly return the boolean condition:
Suggested change
|
||||||||
| end | ||||||||
|
|
||||||||
| def overlapping(other_range) | ||||||||
| if other_range.start_date < @end_date && @start_date < other_range.end_date | ||||||||
| return true | ||||||||
| else | ||||||||
| return false | ||||||||
| end | ||||||||
| end | ||||||||
|
|
||||||||
| def exactly_matching(other_range) | ||||||||
| if @start_date == other_range.start_date && @end_date == other_range.end_date | ||||||||
| return true | ||||||||
| else | ||||||||
| return false | ||||||||
| end | ||||||||
| end | ||||||||
|
|
||||||||
| def including(other_range) | ||||||||
| if other_range.start_date >= @start_date && other_range.end_date <= @end_date | ||||||||
| return true | ||||||||
| else | ||||||||
| return false | ||||||||
| end | ||||||||
| end | ||||||||
| end | ||||||||
| end | ||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
|
|
||
| class NoAvailabilityError < StandardError | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Custom exception class! 🎉 |
||
| end | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,28 @@ | ||||||
| require_relative 'date_range' | ||||||
|
|
||||||
| module Hotel | ||||||
| class Reservation | ||||||
| attr_reader :id, :start_date, :end_date, :date_range, :room_id, :block | ||||||
| attr_accessor :cost | ||||||
|
|
||||||
| @@next_id = 1 | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Generally we advise against using a class variable. In this case it's not too bad but it would be preferable to have this stored in the |
||||||
|
|
||||||
| def initialize(date_range, room_id, cost = 200.00) | ||||||
| @date_range = date_range | ||||||
| @start_date = date_range.start_date | ||||||
| @end_date = date_range.end_date | ||||||
|
|
||||||
| @room_id = room_id | ||||||
| @cost = cost | ||||||
|
|
||||||
| @id = @@next_id | ||||||
| @@next_id += 1 | ||||||
| @block = -1 | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like you're using Generally speaking you should use
Suggested change
|
||||||
| end | ||||||
|
|
||||||
| def get_total_price | ||||||
| total_price = date_range.count_nights * cost | ||||||
| return total_price | ||||||
| end | ||||||
| end | ||||||
| end | ||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,44 @@ | ||||||
| require_relative 'reservation' | ||||||
|
|
||||||
| module Hotel | ||||||
| class Room | ||||||
| attr_reader :room_id,:bookings | ||||||
| attr_accessor :cost | ||||||
|
|
||||||
| def initialize(room_id, cost = 200.00, bookings = nil) | ||||||
| raise ArgumentError if !room_id.is_a? Integer | ||||||
| raise ArgumentError if room_id < 1 | ||||||
| @room_id = room_id | ||||||
| @cost = cost | ||||||
| @bookings = bookings || [] | ||||||
| end | ||||||
|
|
||||||
| def add_booking_to_room(new_reservation) | ||||||
| @bookings << new_reservation | ||||||
| end | ||||||
|
|
||||||
| def get_price(reservation) | ||||||
| price = reservation.date_range.count_nights * cost | ||||||
| return price | ||||||
| end | ||||||
|
|
||||||
| def is_available(given_range) | ||||||
| return true if bookings.empty? | ||||||
|
|
||||||
| bookings.each do |item| | ||||||
| if item.block >= 0 #item.class == Hotel::Block | ||||||
| return false if item.date_range.overlapping(given_range) && item.date_range.exactly_matching(given_range)== false #it is not an exact match #method | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of comparing to
Suggested change
|
||||||
| elsif item.block < 0 #item.class == Hotel::Reservation | ||||||
| return false if item.date_range.overlapping(given_range) | ||||||
| end | ||||||
| end | ||||||
|
|
||||||
| return true | ||||||
| end | ||||||
|
|
||||||
| def change_rate(new_rate) | ||||||
| raise ArgumentError if !new_rate.is_a?(Numeric) || new_rate.to_f < 0 | ||||||
| @cost = new_rate.to_f | ||||||
| end | ||||||
| end | ||||||
| end | ||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,174 @@ | ||||||
| require_relative 'room' | ||||||
| require_relative 'date_range' | ||||||
| require_relative 'reservation' | ||||||
| require_relative 'block' | ||||||
| require_relative 'no_availability_error' | ||||||
|
|
||||||
| module Hotel | ||||||
| class SystemCoordinator | ||||||
| attr_reader :rooms | ||||||
|
|
||||||
| @@next_block = 1 | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This definitely doesn't need to be a class variable since there will only be one |
||||||
|
|
||||||
| def initialize(room_quantity = 20) | ||||||
| @rooms = [] # Array.new(quantity){|i| Hotel::Room.new(i+1)} | ||||||
| build_rooms(room_quantity) | ||||||
| end | ||||||
|
|
||||||
| def build_rooms(room_quantity) | ||||||
| room_quantity.times do | ||||||
| @rooms << Hotel::Room.new(@rooms.length + 1) | ||||||
| end | ||||||
| end | ||||||
|
|
||||||
| def list_rooms | ||||||
| return rooms | ||||||
| end | ||||||
|
Comment on lines
+24
to
+26
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You don't need both a |
||||||
|
|
||||||
|
|
||||||
| def find_reservations_by_date(date) | ||||||
| reservations_by_date = [] | ||||||
|
|
||||||
| rooms.each do |room| | ||||||
| room.bookings.each do |reservation| | ||||||
| reservations_by_date << reservation if reservation.date_range.include_date(date) | ||||||
| end | ||||||
| end | ||||||
|
|
||||||
| return reservations_by_date | ||||||
| end | ||||||
|
|
||||||
|
|
||||||
| def find_reservations_room_date(room_id, date_range) | ||||||
| selected_room = find_room(room_id) | ||||||
| reservations_room_date = selected_room.bookings.reject{|reservation|false == reservation.date_range.overlapping(date_range)} | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be simplified using
Suggested change
|
||||||
| return reservations_room_date | ||||||
| end | ||||||
|
|
||||||
|
|
||||||
| def find_reservations_range(given_range) | ||||||
| reservations_range = [] | ||||||
|
|
||||||
| rooms.each do |room| | ||||||
| room.bookings.each do |reservation| | ||||||
| reservations_range << reservation if reservation.date_range.overlapping(given_range) | ||||||
| end | ||||||
| end | ||||||
|
|
||||||
| return reservations_range | ||||||
| end | ||||||
|
|
||||||
|
|
||||||
| def find_available_rooms(given_range) | ||||||
| available_rooms = [] | ||||||
|
|
||||||
| rooms.each do |room| | ||||||
| if room.bookings.empty? | ||||||
| available_rooms << room | ||||||
| else | ||||||
| status = true | ||||||
| room.bookings.each do |item| | ||||||
| if item.block >= 0 #item.class == Hotel::Block | ||||||
| status = false if item.date_range.overlapping(given_range) && false == item.date_range.exactly_matching(given_range) #it is not an exact match #method | ||||||
| elsif item.block < 0 #item.class == Hotel::Reservation | ||||||
| status = false if item.date_range.overlapping(given_range) | ||||||
| end | ||||||
| end | ||||||
| available_rooms << room if true == status | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You don't need to compare to
Suggested change
|
||||||
| end | ||||||
| end | ||||||
|
|
||||||
| raise NoAvailabilityError.new "No Availability. Please try another date range." if available_rooms == [] | ||||||
| return available_rooms | ||||||
| end | ||||||
|
|
||||||
|
|
||||||
| def make_reservation(start_date, end_date) | ||||||
| range_created = Hotel::DateRange.new(start_date,end_date) | ||||||
| available_rooms = find_available_rooms(range_created) | ||||||
|
|
||||||
| chosen_room = available_rooms.shift | ||||||
| new_reservation = Hotel::Reservation.new(range_created, chosen_room.room_id) | ||||||
|
|
||||||
| chosen_room.add_booking_to_room(new_reservation) | ||||||
|
|
||||||
| return new_reservation | ||||||
| end | ||||||
|
|
||||||
|
|
||||||
| def find_block_rooms(given_range) | ||||||
| available_rooms = rooms.reject do |room| | ||||||
| room.bookings.any? do |reservation| #returns true if there are overlapping reservations | ||||||
| reservation.date_range.overlapping(given_range) == true | ||||||
| end | ||||||
| end | ||||||
| return available_rooms | ||||||
| end | ||||||
|
|
||||||
|
|
||||||
| def make_specific_block(date_range, roomid_array, cost) | ||||||
| room_array = roomid_array.map{|id| find_room(id)} | ||||||
| raise ArgumentError.new "Maximum 5 rooms for a hotel block." if room_array.length > 5 || room_array.length == 0 | ||||||
| rooms_in_block = [] | ||||||
|
|
||||||
| room_array.each do |room| | ||||||
| if room.is_available(date_range) == false | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be clarified using an
Suggested change
|
||||||
| raise NoAvailabilityError.new "Room#{room.room_id} is not available for this given date range." | ||||||
| end | ||||||
| end | ||||||
|
|
||||||
| room_array.each do |room| | ||||||
| room_block = Hotel::Block.new(date_range,room.room_id, cost, @@next_block) | ||||||
| room.add_booking_to_room(room_block) | ||||||
| rooms_in_block << room_block | ||||||
| end | ||||||
|
|
||||||
| @@next_block += 1 | ||||||
|
|
||||||
| return rooms_in_block | ||||||
| end | ||||||
|
|
||||||
|
|
||||||
| def make_block(date_range, room_quantity, cost) | ||||||
| raise ArgumentError.new "Maximum 5 rooms for a hotel block." if room_quantity > 5 | ||||||
| available_rooms = find_block_rooms(date_range) | ||||||
| raise NoAvailabilityError.new "No availability. Please try another date range." if available_rooms.length < room_quantity | ||||||
|
|
||||||
| rooms_in_block = [] | ||||||
|
|
||||||
| room_quantity.times do |i| | ||||||
| chosen_room = available_rooms[i] | ||||||
| room_id = chosen_room.room_id | ||||||
| room_block = Hotel::Block.new(date_range,room_id, cost, @@next_block) | ||||||
| chosen_room.add_booking_to_room(room_block) | ||||||
| rooms_in_block << room_block | ||||||
| end | ||||||
|
|
||||||
| @@next_block += 1 | ||||||
| return rooms_in_block | ||||||
| end | ||||||
|
|
||||||
|
|
||||||
| def find_room(given_room_id) | ||||||
| room_found = rooms.find{|room|room.room_id == given_room_id} | ||||||
| return room_found | ||||||
| end | ||||||
|
|
||||||
|
|
||||||
| def check_block_availability(block_id) | ||||||
| rooms_in_block = [] | ||||||
| blocks = [] | ||||||
| rooms.each do |room| | ||||||
| room.bookings.each do |item| | ||||||
| if item.block > 0 && item.block == block_id | ||||||
| blocks << item | ||||||
| rooms_in_block << room | ||||||
| end | ||||||
| end | ||||||
| end | ||||||
|
|
||||||
| date_range = blocks[0].date_range | ||||||
| rooms_in_block.any?{|room|room.is_available(date_range)} #returns true or false | ||||||
| end | ||||||
| end | ||||||
| end | ||||||
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.
It's helpful to include the bad arguments in message of the
ArgumentError: