forked from AdaGold/hotel
-
Notifications
You must be signed in to change notification settings - Fork 40
Sara Nilsen - Space #40
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
Open
sarabrandao21
wants to merge
12
commits into
Ada-C13:master
Choose a base branch
from
sarabrandao21: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
12 commits
Select commit
Hold shift + click to select a range
07392ce
hotel reservation cost
sarabrandao21 3449e7d
new class room
sarabrandao21 5b0b0e3
hotel_controller instance of rooms
sarabrandao21 8d3fdb2
added date_range to reservation class
sarabrandao21 94f9a05
updated tests in date range, 0-length and negative length
sarabrandao21 174d051
date_range overlap tests
sarabrandao21 deca557
new tests for room_test related to instances
sarabrandao21 8a205c0
hotel controller test updates
sarabrandao21 59a2f61
if rooms all reserved, no available rooms message is raised
sarabrandao21 87491a9
include_date_range? method
sarabrandao21 8181556
final changes in the hotel controller for reserving a block
sarabrandao21 3345183
details about refactors.txt
sarabrandao21 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
Binary file not shown.
This file contains hidden or 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,16 @@ | ||
| { | ||
| // Use IntelliSense to learn about possible attributes. | ||
| // Hover to view descriptions of existing attributes. | ||
| // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 | ||
| "version": "0.2.0", | ||
| "configurations": [ | ||
| { | ||
| "name": "Listen for rdebug-ide", | ||
| "type": "Ruby", | ||
| "request": "attach", | ||
| "remoteHost": "127.0.0.1", | ||
| "remotePort": "1234", | ||
| "remoteWorkspaceRoot": "${workspaceRoot}" | ||
| } | ||
| ] | ||
| } |
This file contains hidden or 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,31 @@ | ||
| require_relative 'hotel_controller' | ||
| require 'date' | ||
|
|
||
| module Hotel | ||
| class DateRange | ||
| attr_accessor :start_date, :end_date | ||
|
|
||
| def initialize(start_date, end_date) | ||
| @start_date = start_date | ||
| @end_date = end_date | ||
| raise ArgumentError.new("date range is incorrect") if @start_date > @end_date || @start_date == @end_date | ||
| end | ||
|
|
||
| def overlap?(second_date_range) | ||
| if second_date_range.start_date >= @end_date || second_date_range.end_date <= @start_date | ||
| return false | ||
| else | ||
| return true | ||
| end | ||
| end | ||
|
|
||
| def include_date_range?(date_range) | ||
| date_range.start_date.between?(@start_date, @end_date) || date_range.end_date.between?(@start_date, @end_date) | ||
|
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. Nice clear logic here! |
||
| end | ||
|
|
||
| def nights | ||
| return (@end_date - @start_date).to_i | ||
| end | ||
|
|
||
| end | ||
| end | ||
This file contains hidden or 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,59 @@ | ||
| require_relative 'reservation' | ||
| require_relative 'room' | ||
| require_relative 'date_range' | ||
|
|
||
| module Hotel | ||
| class HotelController | ||
| attr_reader :rooms, :get_available_rooms, :access_reservations | ||
| attr_accessor :reserve_room | ||
| def initialize | ||
| @rooms = Array.new(20) { |i| Room.new(i + 1) } | ||
| @hotel_reservations = [] | ||
|
|
||
| end | ||
|
|
||
| def reserve_room(room_to_reserve, date_range, hotel_block = :no) | ||
| if !room_to_reserve.check_availability(date_range) | ||
| raise ArgumentError.new("This room is not available") | ||
| else | ||
| new_reservation = Reservation.new(date_range, room_to_reserve, hotel_block) | ||
| room_to_reserve.add_reservation(new_reservation) | ||
| @hotel_reservations << new_reservation | ||
| return new_reservation | ||
| end | ||
| end | ||
|
|
||
| def get_available_rooms(date_range) | ||
| rooms_available = [] | ||
| @rooms.each do |room| | ||
| if room.check_availability(date_range) | ||
| rooms_available << room | ||
| end | ||
| end | ||
| return rooms_available | ||
| end | ||
|
|
||
| def access_reservations(date_range) | ||
| list_reservations = [] | ||
|
|
||
| @hotel_reservations.each do |r| | ||
| if r.date_range.include_date_range?(date_range) | ||
| list_reservations << r | ||
| end | ||
| end | ||
| return list_reservations | ||
| end | ||
|
|
||
| def reserve_block(date_range, rooms_to_reserve) | ||
| raise ArgumentError if !(3..5).include?(rooms_to_reserve.length) | ||
| rooms_to_reserve.each do |room| | ||
| begin | ||
| self.reserve_room(room, date_range, :yes) | ||
| rescue => exception | ||
| puts "Unable to reserve block: #{exception}" | ||
| end | ||
| end | ||
| end | ||
|
|
||
| end | ||
| end |
This file contains hidden or 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,29 @@ | ||
| require 'date' | ||
| require_relative 'date_range' | ||
|
|
||
| module Hotel | ||
| class Reservation | ||
| attr_reader :date_range, :room, :price, :cost, :hotel_block | ||
| def initialize(date_range, room, price = 200, hotel_block = :no) | ||
| @date_range = date_range | ||
| @room = room | ||
| @price = price | ||
| @hotel_block = hotel_block | ||
|
|
||
| if @hotel_block == :yes | ||
| @price = 180 | ||
| end | ||
|
|
||
| valid_block = [:yes, :no] | ||
| raise ArgumentError if !valid_block.include?(@hotel_block) | ||
| end | ||
|
|
||
| def cost | ||
| total_cost = @date_range.nights * @price | ||
| return total_cost | ||
| end | ||
|
|
||
| end | ||
| end | ||
|
|
||
|
|
This file contains hidden or 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,29 @@ | ||
| require_relative 'reservation' | ||
| require_relative 'date_range' | ||
|
|
||
| module Hotel | ||
| class Room | ||
| attr_reader :id, :reservations | ||
|
|
||
| def initialize(id, reservations = []) | ||
| @id = id | ||
| @reservations = [] | ||
| end | ||
|
|
||
| def check_availability(date_range) # is available? #giving a date range to room and check if room is available for that day | ||
| @reservations.each do |r| | ||
| if r.date_range.overlap?(date_range) | ||
| return false | ||
| end | ||
| end | ||
| return true | ||
| end | ||
|
|
||
| def add_reservation(reservation) | ||
| @reservations << reservation | ||
| end | ||
| end | ||
| end | ||
|
|
||
|
|
||
|
|
This file contains hidden or 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,4 @@ | ||
| One thing i would do differently would be to create a class for hotel blocks. | ||
| I found myself lost multiple times for not having a specific space for creating the blocks. | ||
| I would want to find a better method name for access_reservations, could not come up with a more clear name that | ||
| indicate the function. |
This file contains hidden or 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,136 @@ | ||
| require_relative "test_helper" | ||
|
|
||
| describe Hotel::DateRange do | ||
| describe "constructor" do | ||
| before do | ||
| start_date = Date.new(2017, 01, 01) | ||
| end_date = start_date + 3 | ||
|
|
||
| @range = Hotel::DateRange.new(start_date, end_date) | ||
| end | ||
|
|
||
| it "Can be initialized with two dates" do | ||
| start_date = Date.new(2017, 01, 01) | ||
| end_date = start_date + 3 | ||
| expect(@range.start_date).must_equal start_date | ||
| expect(@range.end_date).must_equal end_date | ||
| end | ||
|
|
||
| it "is an an error for negative-lenght ranges" do | ||
| start_date = Date.today | ||
| end_date = start_date - 3 | ||
| expect { Hotel::DateRange.new(start_date, end_date) }.must_raise ArgumentError | ||
| end | ||
|
|
||
| it "is an error to create a 0-length range" do | ||
| start_date = Date.today | ||
| end_date = @start_date | ||
| expect { Hotel::DateRange.new(start_date, end_date) }.must_raise ArgumentError | ||
| end | ||
| end | ||
|
|
||
| describe "overlap?" do | ||
| before do | ||
| start_date = Date.new(2017, 01, 01) | ||
| end_date = start_date + 10 | ||
|
|
||
| @range = Hotel::DateRange.new(start_date, end_date) | ||
| end | ||
|
|
||
| it "returns true for the same range" do | ||
| start_date = @range.start_date | ||
| end_date = @range.end_date | ||
| test_range = Hotel::DateRange.new(start_date, end_date) | ||
|
|
||
| expect(@range.overlap?(test_range)).must_equal true | ||
| end | ||
|
|
||
| it "returns true for a contained range" do | ||
| start_date = Date.new(2017, 01, 03) | ||
| end_date = start_date + 3 | ||
| second_range = Hotel::DateRange.new(start_date, end_date) | ||
| expect(@range.overlap?(second_range)).must_equal true | ||
|
|
||
| end | ||
|
|
||
| it "returns true for a range that overlaps in front" do | ||
| start_date = Date.new(2016, 12, 27) | ||
| end_date = @range.start_date + 1 #end of the first range should be available for second_range night | ||
| second_range = Hotel::DateRange.new(start_date, end_date) | ||
| expect(@range.overlap?(second_range)).must_equal true | ||
| end | ||
|
|
||
| it "returns true for a range that overlaps in the back" do | ||
| start_date = @range.start_date | ||
| end_date = start_date + 4 | ||
| second_range = Hotel::DateRange.new(start_date, end_date) | ||
| expect(@range.overlap?(second_range)).must_equal true | ||
| end | ||
|
|
||
| it "returns true for a containing range" do | ||
| start_date = @range.start_date - 4 | ||
| end_date = @range.end_date + 3 | ||
| second_range = Hotel::DateRange.new(start_date, end_date) | ||
| expect(@range.overlap?(second_range)).must_equal true | ||
| end | ||
|
|
||
| it "returns false for a range starting on the end_date date" do | ||
| start_date = @range.end_date | ||
| end_date = start_date + 3 | ||
| second_range = Hotel::DateRange.new(start_date, end_date) | ||
| expect(@range.overlap?(second_range)).must_equal false | ||
| end | ||
|
|
||
| it "returns false for a range ending on the start_date date" do | ||
| start_date = Date.new(2016, 12, 27) | ||
| end_date = @range.start_date | ||
| second_range = Hotel::DateRange.new(start_date, end_date) | ||
| expect(@range.overlap?(second_range)).must_equal false | ||
| end | ||
|
|
||
| it "returns false for a range completely before" do | ||
| start_date = Date.new(2016, 12, 20) | ||
| end_date = start_date + 5 | ||
| second_range = Hotel::DateRange.new(start_date, end_date) | ||
| expect(@range.overlap?(second_range)).must_equal false | ||
| end | ||
|
|
||
| it "returns false for a date completely after" do | ||
| start_date = Date.new(2017, 02, 03) | ||
| end_date = start_date + 5 | ||
| second_range = Hotel::DateRange.new(start_date, end_date) | ||
| expect(@range.overlap?(second_range)).must_equal false | ||
| end | ||
| end | ||
|
|
||
| describe "nights" do | ||
| it "returns the correct number of nights" do | ||
| start_date = Date.new(2017, 02, 03) | ||
| end_date = start_date + 3 | ||
| range = Hotel::DateRange.new(start_date, end_date) | ||
| expect(range.nights).must_equal 3 | ||
|
|
||
| end | ||
| end | ||
| describe "include_date_range?" do | ||
| before do | ||
| start_date = Date.new(2017, 01, 01) | ||
| end_date = start_date + 3 | ||
| @range = Hotel::DateRange.new(start_date, end_date) | ||
| end | ||
| it "is included in the range" do | ||
| second_start_date = Date.new(2017, 01, 01) | ||
| second_end_date = second_start_date + 1 | ||
| second_range= Hotel::DateRange.new(second_start_date, second_end_date) | ||
| expect(@range.include_date_range?(second_range)).must_equal true | ||
| end | ||
| it "is not included in the range" do | ||
| start_date = Date.new(2016, 12, 27) | ||
| end_date = start_date + 2 | ||
| second_range = Hotel::DateRange.new(start_date, end_date) | ||
| expect(@range.include_date_range?(second_range)).must_equal false | ||
| 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.
This creates what's called a circular load, think about your code like a tree. Some things are going to be closer to the root, some things are going to be the ends of branches. This file is the end of a branch!