Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
0306f64
Creating Hotel Modulo with classes(DataRange, FrontDesk,Reservation a…
veralizeth Mar 3, 2020
6ec5568
Adding tests for the classes (FrontDesk and Reservation) * TODO: Writ…
veralizeth Mar 3, 2020
ea0b574
Adding Initialize test, Negative length, Incorrect date format and ov…
veralizeth Mar 3, 2020
65a70fa
Including Self.validate_date in data_range to validate data format
veralizeth Mar 3, 2020
5c77079
Method to generate the rooms, 1 to 20 numbers of room
veralizeth Mar 3, 2020
b001173
Initializing Room class
veralizeth Mar 3, 2020
8dfa860
Creating new class client to keep the client information
veralizeth Mar 3, 2020
f14c6d8
Self private validate date, to validate the format date,start date no…
veralizeth Mar 5, 2020
52f72df
Update rooms method to return the @rooms array
veralizeth Mar 5, 2020
d16c528
Clean bug in line 14 and 15 calling a wrong instance variable.(Fixed)…
veralizeth Mar 5, 2020
2f9c593
overlap? refractored, deleting extra logic to validate overlaps dates
veralizeth Mar 6, 2020
feac28b
total_reservations, reservations_by_date,cost_by_reservation created
veralizeth Mar 6, 2020
9ab9811
reservation intance variables(data_range and id). total_cost method
veralizeth Mar 6, 2020
46606d9
Reservations_by_date and cost_by_reservation tests
veralizeth Mar 6, 2020
53f0d55
total cost test
veralizeth Mar 6, 2020
1003ece
get_available_rooms and make_resrvation methods
veralizeth Mar 6, 2020
e5f0003
Return - Raise syntax error fixed
veralizeth Mar 9, 2020
513447d
Typos fixed date_range, make_reservation update for averlap cases, re…
veralizeth Mar 9, 2020
54ee1ed
Updating incorrect format test
veralizeth Mar 9, 2020
6ef1bab
Test for not available rooms, test for reservations by room and date_…
veralizeth Mar 9, 2020
18338dd
Refactoring and comments
veralizeth Mar 9, 2020
5537aee
Comments and code organizing
veralizeth Mar 9, 2020
4bc07a8
Cleaning comments and puts
veralizeth Mar 9, 2020
6754ecb
Debbuger Json
veralizeth Mar 9, 2020
93145f2
Overlap? refactoring and comments
veralizeth Mar 9, 2020
c103e39
Client class not used
veralizeth Mar 9, 2020
aa930c5
refactoring get_available_rooms, reservations_by_date_room and refact…
veralizeth Mar 9, 2020
c53640c
Refactoring geretate_id and comments
veralizeth Mar 9, 2020
d7bf771
Cost method deleted
veralizeth Mar 9, 2020
4135156
total cost change must_equal, List of reservations for a specified ro…
veralizeth Mar 9, 2020
b01c327
raises an error for incorrect dates format code organizing
veralizeth Mar 9, 2020
d5dfde7
Answer question
veralizeth Mar 9, 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
12 changes: 6 additions & 6 deletions .github/PULL_REQUEST_TEMPLATE
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ Congratulations! You're submitting your assignment. Please reflect on the assign
## Reflection
Question | Answer
:------------- | :-------------
What was a design challenge that you encountered on this project? |
What was a design decision you made that changed over time over the project? |
What was a concept you gained clarity on, or a learning that you'd like to share? |
What is an example of a _nominal test_ that you wrote for this assignment? What makes it a nominal case? |
What is an example of an _edge case test_ that you wrote for this assignment? What makes it an edge case? |
How do you feel you did in writing pseudocode first, then writing the tests and then the code? |
What was a design challenge that you encountered on this project? | Try to think of each class and have each one follow the principle of single responsibility. It was very difficult to think if it was too big, or if I was giving responsibilities that were not of the class or the method.
What was a design decision you made that changed over time over the project? | I thought of creating a class that would be called client but then I didn't use it. Also at the beginning I didn't have a room class and then I decided to include it.
What was a concept you gained clarity on, or a learning that you'd like to share? | Interact with classes and understand what an instance looks like.
What is an example of a _nominal test_ that you wrote for this assignment? What makes it a nominal case? | Test out the list of rooms. It is nominal because It was the basic-minimun thing my hotel structure should show.
What is an example of an _edge case test_ that you wrote for this assignment? What makes it an edge case? | Test out all the possible overlaps. Especially to test when an end_date reservation was the same as the start_date of a new reservation.
How do you feel you did in writing pseudocode first, then writing the tests and then the code? | It was easy at the beginning, drawing the classes and their responsibility, was pretty useful. For the last methods to write the tests at first it became complicated, to think about what results I wanted and how to write a real test, that really test what I want or what I expect.
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
.vscode
47 changes: 47 additions & 0 deletions lib/date_range.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
require 'date'

module Hotel
class DateRange
attr_accessor :start_date, :end_date

def initialize(start_date, end_date)
self.class.validate_date?(start_date,end_date)
@start_date = start_date
@end_date = end_date
end

# An input range given will not be part of any other date_range
# It returns true if there is an overlap or false if there is not.
# input_range = Hotel::DataRange instance.
def overlap?(input_range)
a = self.start_date >= input_range.end_date
b = self.end_date <= input_range.start_date
return a || b ? false : true
end

def nights
nights_number = (end_date - start_date).to_i
return nights_number
end

private
# validate_date? validates that start and end date have valid format.
# start date cannot be after the end date
# Exception raised when an invalid date range is provided
def self.validate_date?(start_date, end_date)

date_format = '%Y-%m-%d'
if !DateTime.strptime(start_date.to_s, date_format) || !DateTime.strptime(end_date.to_s, date_format)
raise ArgumentError
end

if start_date > end_date
raise ArgumentError.new("The end time is before the start time.")
end

if start_date - end_date == 0
raise ArgumentError.new("Start date and end date are the same.")
end
end
end
end
90 changes: 90 additions & 0 deletions lib/front_desk.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@

module Hotel
class FrontDesk
attr_reader :rooms, :date_range, :reservations

def initialize
@rooms = generate_rooms
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Good use of a helper method.

@reservations = []
end

def generate_rooms
hotel_rooms = Array.new
(1..20).each do |room_id|
hotel_rooms << Room.new(room_id)
end
return hotel_rooms
end

def room_list
return @rooms
end

def total_reservations
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Consider whether this reader method is necessary. Would .reservations suffice?

return @reservations
end

# list of rooms that are not reserved for a given date range,
# so that I can see all available rooms for that day.
# It retuns an arrary with rooms with not reservations with a given date.
def get_available_rooms(date_range)
unavailable_rooms = []
# Send the room to unavailable rooms if room has a reservation for a specific data_range.
@reservations.each do |reservation|
if reservation.daterange.overlap?(date_range) == true
unavailable_rooms << reservation.room
end
end
# Array substrating total_rooms minus unavailable rooms.
return @rooms - unavailable_rooms
end

# The list of reservations for a specified room and a given date range.
# room = room_i d.
# date_range = data_range instance.
# It returns an array of reservation.
def reservations_by_room_date (room_number, date_range)
# reservations_by_room contains all the reservations match
# based on the room and a given date range.
reservations_by_room = @reservations.select do |reservation|
reservation.room.room_id == room_number &&
reservation.daterange.overlap?(date_range)
end
return reservations_by_room
end

# It returns an array with the list of reservations for a specific date.
# date = date instance.
def reservations_by_date(date)
total_by_date = @reservations.select do |reservation|
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Good use of an enumerable method.

reservation.daterange.start_date == date
end
return total_by_date
end

# Get the total cost for a given reservation
# reservation_number = reservartion_id
# It returns nil if reservation id not found.
def cost_by_reservation(reservation_number)
@reservations.each do |reservation|
if reservation.id == reservation_number
return reservation.total_cost
end
end
return nil
end

# Make a reservation of a room for a given date range,
# and that room will not be part of any other reservation overlapping that date range
# an exception raised if I try to reserve a room during a date range when all rooms are reserved.
def make_resevation(date_range)
available_rooms = get_available_rooms(date_range)
if available_rooms.length < 1
raise ArgumentError
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Consider whether a custom exception would be appropriate here.

end
new_reservation = Hotel::Reservation.new(date_range,available_rooms[0])
@reservations << new_reservation
return new_reservation
end
end
end
25 changes: 25 additions & 0 deletions lib/reservation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

require 'securerandom'

module Hotel

class Reservation

attr_reader :daterange, :room, :id

def initialize(daterange, room)
@daterange = daterange
@room = room
@id = generate_id
end

# It retuns a secureRandom ID per each reservation made.
def generate_id
return SecureRandom.hex(5)
end

def total_cost
return daterange.nights * @room.cost
end
end
end
12 changes: 12 additions & 0 deletions lib/room.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

module Hotel
class Room
attr_reader :room_id, :capacity, :cost

def initialize(room_id, capacity: 2, cost: 200)
@room_id = room_id
@capacity = capacity
@cost = cost
end
end
end
122 changes: 122 additions & 0 deletions test/data_range_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
require_relative "test_helper"
require 'date'

# The start day cannnot be in the past
describe Hotel::DateRange do


describe "Initilize" do
it "Can be initialized with two dates" do

start_date = Date.new(2017, 01, 01)
end_date = start_date + 3

@range = Hotel::DateRange.new(start_date, end_date)

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.new(2017, 01, 01)
end_date = Date.new(2015, 01, 01)

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.new(2017, 01, 01)
end_date = Date.new(2017, 01, 01)

expect{
Hotel::DateRange.new(start_date, end_date)
}.must_raise ArgumentError

end

it "raises an error for incorrect dates format" do
start_date = ""
end_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 + 3
@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 range that overlaps in front." do
start_date = Date.new(2017, 01, 02)
end_date = start_date + 4
test_range = Hotel::DateRange.new(start_date, end_date)

expect(@range.overlap?(test_range)).must_equal true
end


it "returns true for a range that overlaps in the back." do
start_date = Date.new(2016, 12, 30)
end_date = start_date + 3
test_range = Hotel::DateRange.new(start_date, end_date)

expect(@range.overlap?(test_range)).must_equal true
end

it "returns false for a range ending on the start_date date." do
start_date = Date.new(2016, 12, 30)
end_date = Date.new(2017, 01, 01)
test_range = Hotel::DateRange.new(start_date, end_date)

expect(@range.overlap?(test_range)).must_equal false
end

it "returns false for a range starting on the end_date date." do
start_date = Date.new(2017, 01, 04)
end_date = start_date + 7
test_range = Hotel::DateRange.new(start_date, end_date)

expect(@range.overlap?(test_range)).must_equal false
end

it "returns true for a contained range." do
start_date = Date.new(2017, 01, 02)
end_date = start_date + 1
test_range = Hotel::DateRange.new(start_date, end_date)

expect(@range.overlap?(test_range)).must_equal true
end
end

describe "nights" do
it "returns the correct number of nights" do
start_date = Date.new(2017, 01, 01)
end_date = Date.new(2017, 01, 02)

@range = Hotel::DateRange.new(start_date, end_date)

expect(@range.nights).must_be_kind_of Integer
expect(@range.nights).must_equal 1

end
end

end
Loading