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

Time - Haben #38

Open
wants to merge 23 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
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.
28 changes: 28 additions & 0 deletions lib/date_range.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module Hotel
class DateRange
attr_accessor :start_date, :end_date

def initialize(start_date, end_date)
# if start date is on or after end Date, raise an exception
if start_date >= end_date
raise ArgumentError, " Invalid Date Range."

end

@start_date = start_date
@end_date = end_date
end
# check if date selected overlaps with the date_range
def overlap?(new_start_date, new_end_date)

Choose a reason for hiding this comment

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

Nice use of a boolean expression to return true or false. You don't even need a conditional :)

return(start_date <= new_end_date && end_date >= new_start_date)
end
# check if new_date is between start_date and end_date
def include?(new_date)
return start_date <= new_date && end_date > new_date
end

def nights
return end_date - start_date
end
end
end
73 changes: 73 additions & 0 deletions lib/hotel_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
module Hotel
class HotelController
# Wave 1
attr_reader :reservations, :rooms

# I can access the list of all of the rooms in the hotel
def initialize
@reservations = []
@rooms = [] #create a list of rooms

room_number = 1
20.times do # 20 rooms from requirements
#20 times... do...
@rooms << room_number
room_number = room_number + 1
end
end
# access the list of reservations for a specified room and a given date range
def list_reservations_for_room(room, start_date, end_date)
list = [] # returns an array of reservations that falls within the date range
@reservations.each do |reservation|
if room == reservation.room_number
if reservation.date_range.overlap?(start_date, end_date)
list << reservation
end
end
end
return list
end

def reserve_room(start_date, end_date)
# start_date and end_date should be instances of class Date
# looks through room numbers and get reservations for the date in question until we find one with no reservations
available = find_available_rooms(start_date, end_date)

# check for available rooms
if available.length > 0
newly_created_reservation = Reservation.new(start_date, end_date, available[0])
reservations << newly_created_reservation
return newly_created_reservation
else
raise ArgumentError, "No available rooms."
end
end
# access the list of reservations for a specific date,
# so that it can track reservations by date
def show_reservations_for_date(date)
# go through reservations and add the ones whose range include the given date
list = []
@reservations.each do |reservation|
if reservation.date_range.include?(date)
list << reservation
end
end
return list
end

# Wave 2
def find_available_rooms(start_date, end_date)
# start_date and end_date should be instances of class Date
available_rooms = []
# iterate through the rooms to find available room
@rooms.each do |room|
reservations = list_reservations_for_room(room, start_date, end_date)

Choose a reason for hiding this comment

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

Interesting design choice to use list_reservations_for_room here. This works! However, you don't necessarily need to know all the reservations for that room, just the ones that overlap. Consider if there's a way you could refactor this to make it more efficient.


if(reservations.length() == 0) # rooms that are empty
available_rooms << room
end
end
return available_rooms
end
end
end
19 changes: 19 additions & 0 deletions lib/reservation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module Hotel
class Reservation
attr_reader :date_range, :start_date, :end_date, :room_number

# initizlize
def initialize(start_date, end_date, room_number)
@date_range = DateRange.new(start_date, end_date)
@room_number = room_number
@start_date = start_date
@end_date = end_date

end

# get the total cost for a given reservation
def cost
return @date_range.nights * 200
end
end
end
Loading