-
Notifications
You must be signed in to change notification settings - Fork 297
Time - Hannah #42
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?
Time - Hannah #42
Changes from all commits
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 |
|---|---|---|
| @@ -1,9 +1,9 @@ | ||
| require 'rake/testtask' | ||
| require "rake/testtask" | ||
|
|
||
| Rake::TestTask.new do |t| | ||
| t.libs = ["lib"] | ||
| t.warning = true | ||
| t.test_files = FileList['test/*_test.rb'] | ||
| t.test_files = FileList["test/*_test.rb"] | ||
| end | ||
|
|
||
| task default: :test |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| class DateRange | ||
| attr_reader :start_date, :end_date | ||
|
|
||
| def initialize(start_date_str, end_date_str) # give it Date.parse(year-month) | ||
| @start_date = Date.parse(start_date_str) | ||
| @end_date = Date.parse(end_date_str) | ||
| end | ||
|
|
||
| def with_in_range?(date) | ||
| return date >= start_date && date < end_date | ||
| end | ||
|
|
||
| def duration | ||
| # to get duration subtract the two dates | ||
| return end_date - start_date | ||
| end | ||
|
|
||
| def self.overlap?(date_range1, date_range2) | ||
| if date_range1.end_date <= date_range2.start_date || date_range1.start_date >= date_range2.end_date | ||
| return false | ||
| else | ||
| return true | ||
| end | ||
| # if date_range1.start_date <= date_range2.start_date && date_range1.end_date >= date_range2.end_date | ||
| # return true | ||
| # elsif date_range1.start_date < date_range2.end_date && date_range1.start_date >= date_range2.start_date | ||
| # return true | ||
| # elsif date_range1.end_date >date_range2.start_date && date_range1.end_date <= date_range2.end_date | ||
| # return true | ||
| # else | ||
| # return false | ||
| # end | ||
|
|
||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| # creating reservation class | ||
| require_relative "system.rb" | ||
|
|
||
| module Hotel | ||
| class Reservation | ||
| attr_reader :date_range, :room_num, :cost_per_night | ||
|
|
||
| def initialize(date_range, room_num) | ||
| @date_range = date_range | ||
| @room_num = room_num | ||
| @cost_per_night = 200 | ||
| end | ||
|
|
||
| def get_cost | ||
| return date_range.duration * cost_per_night | ||
| end | ||
|
|
||
| # to get the total cost, need the duration (as input wont be able to dirctly get duration but will get res id) | ||
| # go throught the reservation array to get one one rervation which matches the id | ||
| # get that reservation_id to get the duration | ||
| # after getting duration, calculate cost | ||
|
|
||
| # found_reservation = @reservations.find do |reservation| | ||
|
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. Remember to remove unused code before submitting. |
||
| # reservation.reservation_id == reservation_id | ||
| # end | ||
|
|
||
| # duration = found_reservation.date_range.duration() | ||
|
|
||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| require "date" | ||
| require "pry" | ||
| require_relative "room.rb" | ||
| require_relative "reservation.rb" | ||
| require_relative "date_range.rb" | ||
|
|
||
| module Hotel | ||
| class System | ||
| attr_reader :reservations, :rooms | ||
|
|
||
| def initialize(number_of_rooms) | ||
| @reservations = [] | ||
| @rooms = (1..number_of_rooms).to_a | ||
| end | ||
|
|
||
| def create_reservation(start_date, end_date) | ||
| date_range = DateRange.new(start_date, end_date) | ||
| room = find_avail_room(date_range) | ||
|
|
||
| if !room | ||
| raise "there is no available room" | ||
| end | ||
| reservation = Reservation.new(date_range, room) | ||
| reservations << reservation | ||
| end | ||
|
|
||
| #helper method | ||
| def find_avail_room(date_range) | ||
| rooms.each do |room| | ||
| room_reservation = reservations.select { |reservation| reservation.room_num == room } | ||
| if !room_reservation.any? { |reservation| DateRange.overlap?(reservation.date_range, 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. Good use of an enumerable method. |
||
| return room | ||
| end | ||
| end | ||
| return | ||
| end | ||
|
|
||
| def get_reservation(room_num, given_date_range) | ||
| room_reservations = reservations.select { |res| res.room_num = room_num } | ||
| return room_reservations.select { |res| DateRange.overlap?(res.date_range, given_date_range) } | ||
| end | ||
|
|
||
| def search_by_date(date) | ||
| return reservations.select { |reservation| reservation.date_range.with_in_range?(date) } | ||
| end | ||
| end | ||
| end | ||
|
|
||
| # 1) Write pseudocode | ||
| # 2) Write test(s) | ||
| # 3) Write code | ||
| # 4) Refactor | ||
| # 5) Commit (15 of them) | ||
|
|
||
| # WAVE 1 | ||
| # write the functionality for the system to track valid reservations | ||
| # Your job is to only build the classes that store information and handle business logic | ||
|
|
||
| # I can access the list of all of the rooms in the hotel (done) | ||
| # I access the list of reservations for a specified room and a given date range (change the date using parse method)(done) | ||
|
|
||
| # I can access the list of reservations for a specific date, so that I can track reservations by date | ||
|
|
||
| # I can get the total cost for a given reservation | ||
|
|
||
| # I want exception raised when an invalid date range is provided, so that I can't make a reservation for an invalid date range | ||
|
|
||
| # Detail | ||
|
|
||
| # When reserving a room, the user provides only the start and end dates | ||
| # the library should determine which room to use for the reservation | ||
| # For this wave, any room can be reserved at any time, and you don't need to check whether reservations conflict with each other (this will come in wave 2!) | ||
|
|
||
| # WAVE 2 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| # writting a test for duration | ||
| require_relative "test_helper" | ||
|
|
||
| describe "date range class" do | ||
| it "calculates duration" do | ||
| # Arrange | ||
| date_range_instance = DateRange.new("2020-02-27", "2020-03-02") | ||
| # Act | ||
|
|
||
| duration = date_range_instance.duration() | ||
|
|
||
| # Assert | ||
| expect (duration).must_equal 4 | ||
| end | ||
| describe "overlap" do | ||
|
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. These tests are very comprehensive. Nice work! Consider naming them with the particular test case they test for. See the example below from the design scaffold. 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 "case 1 " do | ||
| #Arrange | ||
| date_one = DateRange.new("2020-03-27", "2020-04-02") | ||
| date_two = DateRange.new("2020-04-04", "2020-04-06") | ||
| #Act | ||
| result = DateRange.overlap?(date_one, date_two) | ||
| #Assert | ||
| expect(result).must_equal(false) | ||
| end | ||
| it "case 2" do | ||
| #Arrange | ||
| date_one = DateRange.new("2020-03-27", "2020-04-02") | ||
| date_two = DateRange.new("2020-04-02", "2020-04-03") | ||
| #Act | ||
| result = DateRange.overlap?(date_one, date_two) | ||
| #Assert | ||
| expect(result).must_equal(false) | ||
| end | ||
| it "case 3" do | ||
| #Arrange | ||
| date_one = DateRange.new("2020-03-27", "2020-04-02") | ||
| date_two = DateRange.new("2020-04-01", "2020-04-27") | ||
| #Act | ||
| result = DateRange.overlap?(date_one, date_two) | ||
| #Assert | ||
| expect(result).must_equal(true) | ||
| end | ||
| it "case 4" do | ||
| #Arrange | ||
| date_one = DateRange.new("2020-03-27", "2020-04-02") | ||
| date_two = DateRange.new("2020-03-27", "2020-04-02") | ||
| #Act | ||
| result = DateRange.overlap?(date_one, date_two) | ||
| #Assert | ||
| expect(result).must_equal(true) | ||
| end | ||
| it "case 5" do | ||
| #Arrange | ||
| date_one = DateRange.new("2020-03-27", "2020-04-02") | ||
| date_two = DateRange.new("2020-03-27", "2020-04-05") | ||
| #Act | ||
| result = DateRange.overlap?(date_one, date_two) | ||
| #Assert | ||
| expect(result).must_equal(true) | ||
| end | ||
| it "case 6" do | ||
| #Arrange | ||
| date_one = DateRange.new("2020-03-17", "2020-04-05") | ||
| date_two = DateRange.new("2020-03-06", "2020-04-05") | ||
| #Act | ||
| result = DateRange.overlap?(date_one, date_two) | ||
| #Assert | ||
| expect(result).must_equal(true) | ||
| end | ||
| it "case 7" do | ||
| #Arrange | ||
| date_one = DateRange.new("2020-04-03", "2020-04-07") | ||
| date_two = DateRange.new("2020-04-04", "2020-04-05") | ||
| #Act | ||
| result = DateRange.overlap?(date_one, date_two) | ||
| #Assert | ||
| expect(result).must_equal(true) | ||
| end | ||
| it "case 8" do | ||
| #Arrange | ||
| date_one = DateRange.new("2020-04-03", "2020-04-05") | ||
| date_two = DateRange.new("2020-03-31", "2020-04-07") | ||
| #Act | ||
| result = DateRange.overlap?(date_one, date_two) | ||
| #Assert | ||
| expect(result).must_equal(true) | ||
| end | ||
| it "case 9" do | ||
| #Arrange | ||
| date_one = DateRange.new("2020-03-17", "2020-04-05") | ||
| date_two = DateRange.new("2020-03-06", "2020-03-17") | ||
| #Act | ||
| result = DateRange.overlap?(date_one, date_two) | ||
| #Assert | ||
| expect(result).must_equal(false) | ||
| end | ||
| it "case 10" do | ||
| #Arrange | ||
| date_one = DateRange.new("2020-03-17", "2020-04-05") | ||
| date_two = DateRange.new("2020-03-06", "2020-03-19") | ||
| #Act | ||
| result = DateRange.overlap?(date_one, date_two) | ||
| #Assert | ||
| expect(result).must_equal(true) | ||
| end | ||
| it "case 11" do | ||
| #Arrange | ||
| date_one = DateRange.new("2020-05-05", "2020-05-07") | ||
| date_two = DateRange.new("2020-03-06", "2020-03-17") | ||
| #Act | ||
| result = DateRange.overlap?(date_one, date_two) | ||
| #Assert | ||
| expect(result).must_equal(false) | ||
| end | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| require "simplecov" | ||
| SimpleCov.start | ||
|
|
||
| require_relative "test_helper" | ||
|
|
||
| describe "Reservation class" do | ||
| it "calculate cost" do | ||
| #Arrange | ||
| date_range = DateRange.new("02-10-2020", "02-12-2020") | ||
| reservation = Hotel::Reservation.new(date_range, 8) | ||
|
|
||
| #Act | ||
| cost = reservation.get_cost | ||
|
|
||
| #Assert | ||
| expect(cost).must_equal(date_range.duration * reservation.cost_per_night) | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| require "simplecov" | ||
| SimpleCov.start |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| require "date" | ||
|
|
||
| require "simplecov" | ||
| SimpleCov.start | ||
|
|
||
| require_relative "test_helper" | ||
|
|
||
| describe "System class" do | ||
| it "gets lists of rooms" do | ||
|
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. To increase readability and ensure you are fully testing your methods, group the tests for a single method together using a describe block. |
||
| #Arrange | ||
| system = Hotel::System.new(20) | ||
|
|
||
| #Act | ||
| rooms = system.rooms | ||
|
|
||
| #Assert | ||
| expect(rooms).must_equal (1..20).to_a | ||
| end | ||
|
|
||
| it "create reservation" do | ||
| #Arrange | ||
| system = Hotel::System.new(20) | ||
| #Act | ||
| system.create_reservation("01/01/2020", "01/10/2020") | ||
| #Assert | ||
| expect(system.reservations.length).must_equal 1 | ||
| end | ||
|
|
||
| it "gets list of reservation and given a date" do | ||
| #Arrange | ||
| date = Date.parse ("02/02/2020") | ||
| system = Hotel::System.new(20) | ||
| system.create_reservation("03/12/2020", "03/12/2020") | ||
| system.create_reservation("02/02/2020", "02/04/2020") | ||
|
|
||
| #Act | ||
| res_array = system.search_by_date(date) | ||
|
|
||
| #Assert | ||
| expect(res_array.length).must_equal 1 | ||
| end | ||
|
|
||
| it "raises exception, if no available rooms" do | ||
|
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 is a clever way to test this edge case! |
||
| #Arrange | ||
| system = Hotel::System.new(1) | ||
| system.create_reservation("03/03/2010", "03/04/2020") | ||
| #Act | ||
| expect{system.create_reservation("03/03/2010", "03/04/2020")}.must_raise Exception | ||
| #Assert | ||
| end | ||
|
|
||
|
|
||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,15 @@ | ||
| # Add simplecov | ||
| require "simplecov" | ||
| SimpleCov.start do | ||
| add_filter "test/" # Tests should not be checked for coverage. | ||
| end | ||
|
|
||
| require "minitest" | ||
| require "minitest/autorun" | ||
| require "minitest/reporters" | ||
|
|
||
| Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new | ||
|
|
||
| # require_relative your lib files here! | ||
| require_relative "../lib/date_range.rb" | ||
| require_relative "../lib/reservation.rb" | ||
| require_relative "../lib/system.rb" | ||
| require_relative "../lib/room.rb" |
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.
Nice work refactoring this code. You should delete the commented code. This would be a great moment to commit!
git commit -m "refactored DateRange.overlap?.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.
Consider making this in an instance method such that it would be called like this
date_range1.overlap?(date_range2)