Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
aa7f322
setting up the enviroment
SuelyBarreto Mar 1, 2020
44f1e61
desing scaffolding
SuelyBarreto Mar 1, 2020
49edc05
guard and minicov installed
SuelyBarreto Mar 1, 2020
ed52713
minor fixes after first look at tests and classes
SuelyBarreto Mar 1, 2020
15f25f7
completed test and class for date range
SuelyBarreto Mar 1, 2020
235cb9a
Add coverage directory to .gitignore file
SuelyBarreto Mar 2, 2020
94f6957
added range must be kind of HotelDateRange to the describe constructor
SuelyBarreto Mar 2, 2020
c0f50ca
date_range instance_of?
SuelyBarreto Mar 3, 2020
d6a757e
reservation_test and reservation for wave 1
SuelyBarreto Mar 3, 2020
76cd3ac
starting hotel_controller in wave 1
SuelyBarreto Mar 3, 2020
bc4ae6d
added comments and user stories for guidance
SuelyBarreto Mar 3, 2020
dd3bf92
available_rooms and room_available
SuelyBarreto Mar 3, 2020
1a046ff
fixed errors after available_rooms (start_day, end_day, room)
SuelyBarreto Mar 3, 2020
c2d2717
fixing comments
SuelyBarreto Mar 4, 2020
3c75bc4
adding tests to hotel controller tests
SuelyBarreto Mar 4, 2020
78579ec
fixing errors
SuelyBarreto Mar 4, 2020
8aa44f1
returns the reservation when you ask for a date in the middle
SuelyBarreto Mar 4, 2020
269d56d
more tests
SuelyBarreto Mar 5, 2020
7f32c5d
end of hotel controller tests
SuelyBarreto Mar 5, 2020
8072925
Block class and block_test
SuelyBarreto Mar 5, 2020
8d2def6
Wave 3 basics, test outline
SuelyBarreto Mar 6, 2020
5b59d71
block_rooms_valid?
SuelyBarreto Mar 7, 2020
5565391
create block tests and code
SuelyBarreto Mar 7, 2020
e36c6ff
reserve_from_block tests and code
SuelyBarreto Mar 7, 2020
f12aae3
rooms_valid?
SuelyBarreto Mar 7, 2020
41ed098
block_rooms_available?
SuelyBarreto Mar 7, 2020
10001c1
is_room_unblocked?
SuelyBarreto Mar 7, 2020
89d697b
available_rooms_in(block) test
SuelyBarreto Mar 7, 2020
5359065
wave 3 code completed, all test passed
SuelyBarreto Mar 7, 2020
65f1024
fixing comments and indentation for date range tests and code
SuelyBarreto Mar 7, 2020
1cfc58a
fixing comments and indentation for reservation tests and codes
SuelyBarreto Mar 7, 2020
cb368bd
fixing comments and indentation for hotel controller code
SuelyBarreto Mar 7, 2020
f2f14f3
fixing comments and indentation for hotel controller tests
SuelyBarreto Mar 7, 2020
07ae984
fixed comments and indentation for block tests and code
SuelyBarreto Mar 7, 2020
26acb36
it initializes block test
SuelyBarreto Mar 7, 2020
324d8d7
CLI (optional)
SuelyBarreto Mar 8, 2020
6f8836d
spacing and indentation
SuelyBarreto Mar 8, 2020
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 @@ -48,3 +48,4 @@ build-iPhoneSimulator/

# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
.rvmrc
coverage
49 changes: 49 additions & 0 deletions design-scaffolding-notes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Hotel Design Scaffolding

## How to Read This Scaffolding to Create a Second Draft

This scaffolding is one solution that answers some of the initial questions about how project files can be laid out.

Use [this view of our branch on GitHub.com](https://github.com/AdaGold/hotel/tree/design-scaffolding) to explore the files that exist on this repo.
- What classes exist?
- Why? What are they named, what do they represent, and what state and behavior do they have?
- What tests exist?
- What parts of this design inspires you, and you want to steal?
- What parts of this design are you unsure about, and need to consider again later?
- What parts of this design do you think you can do without?

Spend **no more than 1 hour** answering those questions and adjusting your project's first draft design. After one hour, get started; don't forget that a useful skill for the programmer is the ability to get started, and adjust in small ways often.

### What it includes

- Three class stubs, `HotelController`, `Reservation` and `DateRange`
- Stubs for public methods of each class from waves 1 and 2, as described in the user stories
- "Interface" tests for each class method that invoke it with the right parameters and verify the return type
- Full test stubs for the `DateRange` class

### What it does not include

- Opinions about how classes should interact or data should be stored
- Opinions about whether there should be a `Room` class, or whether it should know about `Reservation`s
- Private helper methods to keep code organized

Students should feel free to modify any code as they see fit, including changing method signatures, adding new classes and methods, reordering things, not looking at the `DateRange` tests because they want to give it a shot on their own, etc.

## How to use this code

Design scaffolding code lives on the `design-scaffolding` branch.

You can use this code either as inspiration, or as a starting point. If using it as an inspiration, it follows our standard project layout, with product code under `lib/` and tests under `test/`.

If you choose to use the code on this branch as a starting point, you can either:

1. Copy and paste each file from this project into your existing `hotel` folder
2. Or start your project anew with the following steps:

```
$ git clone <paste forked repo URL>
$ cd hotel
$ git merge origin/design-scaffolding
```

- Note: You can try to merge in the design scaffolding after you've started, but you'll probably end up with merge conflicts. See an instructor if you're not able to resolve them yourself.
25 changes: 25 additions & 0 deletions lib/block.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module Hotel

class Block
# Generator
attr_reader :range, :rooms, :rate

# Constructor
def initialize(start_date, end_date, rooms, rate)
@range = Hotel::DateRange.new(start_date, end_date)
raise ArgumentError, "Rooms must be an array" unless rooms.instance_of?(Array)
raise ArgumentError, "No more than 5 rooms per block" if rooms.size > 5
raise ArgumentError, "At least one room per block" if rooms.size == 0
@rooms = rooms
raise ArgumentError, "Rate must be a number" unless rate.is_a?(Numeric)
raise ArgumentError, "Rate must be a positive number" if rate < 0
@rate = rate
end

# Calculate cost
def cost
return @rate * @range.nights * @rooms.size
end
end

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

class DateRange
# Generator
attr_accessor :start_date, :end_date

# Constructor
def initialize(start_date, end_date)
# Raise an error when an invalid date range is provided
unless start_date.instance_of?(Date) && end_date.instance_of?(Date)
raise ArgumentError, "Parameters must be of class Date"
end
if start_date >= end_date
raise ArgumentError, "Start date must be before end date"
end
@start_date = start_date
@end_date = end_date
end

# Check range overlap
def overlap?(other)
unless other.instance_of?(Hotel::DateRange)
raise ArgumentError, "Parameters must be of class Hotel::DateRange"
end

return other.start_date < @end_date && other.end_date > @start_date
end

# Check if a date is included in the date range
def include?(date)
unless date.instance_of?(Date)
raise ArgumentError, "Parameters must be of class Date"
end

return date >= @start_date && date < @end_date
end

# Calculate number of nights
# Checkout day not to be charged
def nights
return (@end_date - @start_date).to_i
end

end

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

class HotelController
# Generator
# Access list of all rooms in hotel
attr_reader :rooms, :reservations, :blocks

# Constructor
# Hotel has 20 rooms, 1 through 20
def initialize
@rooms = (1..20).to_a
@reservations = []
@blocks = []
end

# Reserve a room for a given date range
# Determine which room to reserve given start and end dates
def reserve_room(start_date, end_date)
room = available_rooms(start_date, end_date).first
raise ArgumentError, "No Vacancy" if room == nil
reservation = Hotel::Reservation.new(start_date, end_date, room)
@reservations << reservation
return reservation
end

# Access list of reservations for a specific date to track reservations by date
def reservations_by_date(date)
return @reservations.select do |reservation|
reservation.range.include?(date)
end
end

# Returns list of reservations for specific room and given date range
def reservations_by_room(start_date, end_date, room)
range = Hotel::DateRange.new(start_date, end_date)
return @reservations.select do |reservation| # which reservations have an overlap with range passed
reservation.range.overlap?(range) && reservation.room == room
end
end

# Returns specific room available for date range
def is_room_available?(start_date, end_date, room)
return reservations_by_room(start_date, end_date, room).size == 0
end

# Returns list of rooms available for date range
# Checks if the room is in a block. Respects room blocks as well as individual reservations
def available_rooms(start_date, end_date)
no_reservation = @rooms.select { |room| is_room_available?(start_date, end_date, room) }
# Cannot reserve specific room, for specific date, if room already in a block for that specific date
no_blocks = no_reservation.select { |room| is_room_unblocked?(start_date, end_date, room) }
return no_blocks
end

# Wave 3

# Create a block for given date range, set of rooms and rate
def create_block(start_date, end_date, rooms, rate)
raise ArgumentError, "Invalid room" unless rooms_valid?(rooms)
# Exception raised if try to create a block and at least one of the rooms is unavailable for given date range
raise ArgumentError, "Rooms not available" unless block_rooms_available?(start_date, end_date, rooms)
block = Hotel::Block.new(start_date, end_date, rooms, rate)
@blocks << block
return block
end

# Create a reservation from a block
# Reserve a specific room from a hotel block
def reserve_from_block(block, room)
raise ArgumentError, "Not a block" unless block.instance_of?(Hotel::Block)
raise ArgumentError, "Room not in block" unless block.rooms.include?(room)
# Only reserves a room from block for the full duration of the block
# When a room is reserved from a block of rooms, the reservation dates will always match the date range of the block
start_date = block.range.start_date
end_date = block.range.end_date
raise ArgumentError, "No Vacancy" unless is_room_available?(start_date, end_date, room)
reservation = Hotel::Reservation.new(start_date, end_date, room)
# Reservation made from a hotel block adds to the list of reservations for that date
@reservations << reservation
return reservation
end

# Check if rooms are valid
def rooms_valid?(rooms)
return false unless rooms.instance_of?(Array)
return false if rooms.size == 0
rooms.each do |room|
return false unless @rooms.include?(room)
end
return true
end

# Check if rooms for a block are available in date range
# Cannot create another hotel block for specific date and room if room already in another block
def block_rooms_available?(start_date, end_date, rooms)
rooms.each do |room|
return false unless is_room_available?(start_date, end_date, room)
return false unless is_room_unblocked?(start_date, end_date, room)
end
return true
end

# Check if a room is not blocked in a specific date range
def is_room_unblocked?(start_date, end_date, room)
return blocks_by_room(start_date, end_date, room).size == 0
end

# Returns a list of blocks for that room in the date range
def blocks_by_room(start_date, end_date, room)
range = Hotel::DateRange.new(start_date, end_date)
return @blocks.select do |block| # which blocks have an overlap with the range passed
block.range.overlap?(range) && block.rooms.include?(room)
end
end

# Checks whether a given block has any rooms available
def available_rooms_in(block)
return block.rooms.select { |room| is_room_available?(block.range.start_date, block.range.end_date, room)}
end

end # class HotelController

end # module Hotel
21 changes: 21 additions & 0 deletions lib/reservation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module Hotel

class Reservation
# Generator
attr_reader :range, :room
ROOM_COST = 200

# Constructor
def initialize(start_date, end_date, room)
@range = Hotel::DateRange.new(start_date, end_date)
@room = room
end

# Get the total cost for a given reservation
# Rooms Identical and Cost $200/night
def cost
return ROOM_COST * @range.nights
end
end

end
Loading