-
Notifications
You must be signed in to change notification settings - Fork 2
GreenBOX Project _STHOMAS #1
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?
Changes from all commits
c35757f
712ed68
aadb210
519749f
f062626
651442d
bf943a9
06db929
d6d83ef
e4528e1
8bb4291
2a143a8
f58479f
4908fae
a665208
d2275d4
704b131
7bbb903
e3167e4
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 |
|---|---|---|
|
|
@@ -48,3 +48,4 @@ build-iPhoneSimulator/ | |
|
|
||
| # unless supporting rvm < 1.11.0 or doing something fancy, ignore this: | ||
| .rvmrc | ||
| coverage | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,46 @@ | ||
| require 'time' | ||
| require_relative 'movie_reserver' | ||
|
|
||
| module GreenBox | ||
| # When a movie is being rented you need to understand when that rental occurs. To represent that interval of time, you will create a `DateRange` class. | ||
| class DateRange | ||
| attr_reader :start_date , :end_date, :nights | ||
|
|
||
| def initialize(start_date, end_date) | ||
| @start_date = start_date | ||
| @end_date = end_date | ||
|
|
||
| if start_date > end_date | ||
| raise ArgumentError.new "The end date is before the start date" | ||
| end | ||
| end | ||
|
|
||
| # `contains(date)` - This method returns true if the date occurs on or after the start date and before the end date. | ||
| def contains(date) | ||
| if (date >= @start_date) && (date < @end_date) | ||
| return true | ||
| else | ||
| date < @start_date || date >= @end_date | ||
| return false | ||
| end | ||
| end | ||
|
|
||
|
|
||
| # `overlaps(other_date_range)` - This method takes another date range as a parameter and returns `true` if the date ranges overlap. | ||
| def overlaps(other_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. 👍 |
||
| if contains(other_date_range.start_date) || contains(other_date_range.end_date) | ||
| true | ||
| elsif other_date_range.contains(@start_date) && other_date_range.contains(@end_date) | ||
| true | ||
| else | ||
| false | ||
| end | ||
| end | ||
|
|
||
| # `nights` - This method will return the number of nights included in the given `DateRange. | ||
| def nights | ||
| count = (@end_date - @start_date)/(60*60*24) | ||
| return count | ||
| end | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,28 @@ | ||
| require 'csv' | ||
|
|
||
| module GreenBox | ||
| #A 'movie class' represents a film in our system database | ||
| class Movie | ||
| attr_reader :id, :title, :publisher, :actors | ||
|
|
||
| def initialize(id,title,publisher, actors:[]) | ||
| @id = id | ||
| @title = title | ||
| @publisher = publisher | ||
| @actors = actors | ||
| end | ||
|
|
||
| movie = GreenBox::Movie.new(4,'Happiness','Fox',actors:['Denzel Washington', 'LL Cool J']) | ||
|
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. You can take this line out |
||
|
|
||
| #This method will return true if the given actor does appear in the movie. | ||
|
|
||
| def stars_actor(actor_name) | ||
| actors.each do |actor| | ||
| if actor_name == actor | ||
| return true | ||
| end | ||
| end | ||
| return false | ||
| end | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,121 @@ | ||
| require 'csv' | ||
| require 'pry' | ||
| require_relative 'date_range' | ||
| require_relative 'movie' | ||
| require_relative 'rental' | ||
|
|
||
|
|
||
| module GreenBox | ||
| class MovieReserver | ||
| attr_accessor :movies, :rentals, :date_range | ||
| # :reserver, :date_range | ||
|
|
||
| def initialize | ||
| @movies = MovieReserver.load_movies | ||
| @rentals = [] | ||
| @date_range = GreenBox::DateRange.new(Time.parse('2018-08-08'),Time.parse('2018-08-09')) | ||
|
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 |
||
| end | ||
|
|
||
| # `self.load_movies`: This method will open the csv file `movies.csv` and read in the movies and return an array of the given movies. Note the actors are separated by the `:` character. You will need to break up that field. | ||
| def self.load_movies | ||
| showtime_movies = [] | ||
|
|
||
| CSV.read('data/movies.csv', headers: false).each do |line| | ||
| movie_id = line[0].to_i | ||
| title = line[1] | ||
| publisher = line[2] | ||
| all_actors = line[3] | ||
| actors_names = all_actors.split(':') | ||
| actors = {actors: actors_names} | ||
|
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. You could also make this: actors = actors_namesAnd just use an array instead of a has containing one array. |
||
|
|
||
| showtime_movies << GreenBox::Movie.new(movie_id, title, publisher, actors) | ||
| end | ||
| return showtime_movies | ||
| end | ||
|
|
||
|
|
||
| # `available_movies(date_range)`: This method will take a `DateRange` instance and return a list of | ||
| # movies available (not rented yet) in that range. | ||
| def available_movies(date_range) | ||
|
|
||
| # @movies = available_movies | ||
| # If @rentals = [] - is empty, | ||
| if @rentals.empty? | ||
| # then all the movies are available | ||
| return @movies | ||
| else | ||
| # @movies is a MovieReserver object that contains movies. | ||
| available_movies = [] | ||
| @movies.each do |movie| | ||
| if @rentals.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. This part is not quite right.
You can look up the |
||
| available_movies << movie | ||
| end | ||
| end | ||
| return @rentals | ||
| end | ||
| end | ||
| #PSEUDOCODE | ||
| # If there is a movie in rentals | ||
| ##### Want to check what movies are in rentals | ||
| # that fall into that date date_range | ||
| # and remove that movie only | ||
| # will need to use a data structure to only use the ID that are in the date_range | ||
| #use data structure to store those and match | ||
| # them with the ones in the movie list | ||
| # | ||
|
|
||
| # if @rentals.length > 0 | ||
| # @rentals.each do |movies| | ||
| # date_range | ||
| # if @movies.include(date_range) | ||
| # available_movies = @movies.delete(date_range) | ||
| # end | ||
|
|
||
| # @rentals.each do |movie| | ||
| # movie.id | ||
| # if @movies.include(movie.id) | ||
| # available_movies = @movies.delete(movie.id) | ||
| # end | ||
|
|
||
| # end | ||
|
|
||
| # end | ||
|
|
||
| # * `rent_movie(movie_title, date_range, customer_name)`: This method will attempt to reserve a movie with the given title for the given date range. If the movie is not available in that range the method should raise a `StandardError`. | ||
|
|
||
| def rent_movie(movie_title, date_range, customer_name) | ||
| @movies.each do |movie| | ||
|
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. As your test shows, this isn't working because it always rents a movie as long as the movie is in the inventory. If I can make a suggestion:
|
||
| if movie.title == movie_title | ||
| @rentals << movie | ||
| return @rentals | ||
| end | ||
| end | ||
| # If we finish the loop and nothing is found, raise an error here. | ||
| raise ArgumentError.new("No movies found!") | ||
| end | ||
| end | ||
|
|
||
| end | ||
|
|
||
|
|
||
|
|
||
| # available_movies = @rentals.map do |movie| | ||
| # if @rentals.date_range.contains(movie.id) | ||
| # return available_movies | ||
| # else | ||
| # @rentals << movie | ||
| # end | ||
| # end | ||
| # movies not rented yet = showtime_movies | ||
| # movies rented = rentals | ||
| # | ||
| # available_movies << GreenBox::Rental.new(movie,date_range) | ||
| # | ||
| # available_movies = MovieReserver.load_movies | ||
| # | ||
| # @rentals = available_movies.each do |date_range| | ||
| # if @rentals.date_range <= 0 | ||
| # @rentals << movie | ||
| # end | ||
| # end | ||
| # return available_movies | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,28 @@ | ||
| require_relative 'date_range' | ||
| require 'time' | ||
| require_relative 'movie' | ||
|
|
||
|
|
||
| module GreenBox | ||
| # A `Rental` represents a movie rental in the system. | ||
| class Rental | ||
| attr_accessor :movie, :date_range, :customer | ||
|
|
||
| def initialize(movie, date_range, customer) | ||
| if date_range.nights <= 0 | ||
| raise ArgumentError.new "This is an illegal date range" | ||
| end | ||
| @movie = movie | ||
| @date_range = date_range | ||
| # * customer: The name of the customer making the rental | ||
| @customer = customer | ||
| end | ||
|
|
||
| # * `cost`: This method will return the cost of the rental. A movie rental will cost $3.0 per night. The customer is **not** charged for the day the movie is checked in. So a movie checked out on August 8th and checked in August 10th would cost $3.00 * 2 days = $6.00 | ||
| def cost | ||
|
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. 👍 |
||
| rent = @date_range.nights | ||
| cost = rent * 3.0 | ||
| return cost.round(2) | ||
| end | ||
| end | ||
| end | ||
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.
You don't need this 2nd conditional since if it's not inside you can return false.