In: Computer Science
JAVA programming language
Modify the following code. Make changes so that Movies can be sorted by title
-----------------------------------------------------------------
package Model; |
import java.time.Duration; |
import java.time.LocalDate; |
import java.util.ArrayList; |
import java.util.List; |
// TODO - Modify the movie class so that Java knows how to compare two Movies for sorting by title |
public class Movie extends DataStoreObj |
{ |
private String title; |
private String description; |
private LocalDate releaseDate; |
private Duration runningTime; |
private Rating rating; |
private List<Genre> genres = new ArrayList<>(); |
private List<Actor> actors = new ArrayList<>(); |
// TODO: Add list of reviews |
private List<Review> reviews = new ArrayList<>(); |
public Movie(String title, String description, Rating rating, String releaseDate, int runningTimeMinutes) { |
this(null, title, description, rating, releaseDate, runningTimeMinutes); |
} |
public Movie(Long id, String title, String description, Rating rating, String releaseDate, int runningTimeMinutes) { |
super(id); |
this.title = title; |
this.description = description; |
this.rating = rating; |
this.releaseDate = LocalDate.parse(releaseDate); |
// https://stackoverflow.com/a/41800301/673393 |
this.runningTime = Duration.ofMinutes(runningTimeMinutes); |
} |
public Duration getRunningTime() { |
return runningTime; |
} |
public String getTitle() { |
return title; |
} |
public void addGenre(Genre genre) { |
genres.add(genre); |
} |
public void addActor(long actorId) { |
Actor foundActor = Datastore.getActorById(actorId); |
actors.add(foundActor); |
} |
public List<Actor> getActors() { |
return actors; |
} |
public void addReview(Review review) { |
// TODO: Write code to add the review to the list |
reviews.add(review); |
} |
public String toString() { |
int runningTimeMinutes = (int) this.getRunningTime().getSeconds() / 60; |
return String.format("%s (%s, %s) %s {%s min}", this.title, this.rating, this.releaseDate.getYear(), genres, runningTimeMinutes); |
} |
// TODO - You'll need a new method here to tell Java how to compare two movies by title |
public List<Review> getReviews() { |
return reviews; |
} |
}
Here is the completed code for this problem. Made Movie class implement Comparable interface, and implemented compareTo method that is needed for sorting Movie objects by title in alphabetical order. No other change has been made. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
// Movie.java (updated)
import java.time.Duration;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
//made Movie class implement Comparable interface
public class Movie extends DataStoreObj implements Comparable<Movie> {
private String title;
private String description;
private LocalDate releaseDate;
private Duration runningTime;
private Rating rating;
private List<Genre> genres = new ArrayList<>();
private List<Actor> actors = new ArrayList<>();
// TODO: Add list of reviews
private List<Review> reviews = new ArrayList<>();
public Movie(String title, String description, Rating rating, String releaseDate, int runningTimeMinutes) {
this(null, title, description, rating, releaseDate, runningTimeMinutes);
}
public Movie(Long id, String title, String description, Rating rating, String releaseDate, int runningTimeMinutes) {
super(id);
this.title = title;
this.description = description;
this.rating = rating;
this.releaseDate = LocalDate.parse(releaseDate);
// https://stackoverflow.com/a/41800301/673393
this.runningTime = Duration.ofMinutes(runningTimeMinutes);
}
public Duration getRunningTime() {
return runningTime;
}
public String getTitle() {
return title;
}
public void addGenre(Genre genre) {
genres.add(genre);
}
public void addActor(long actorId) {
Actor foundActor = Datastore.getActorById(actorId);
actors.add(foundActor);
}
public List<Actor> getActors() {
return actors;
}
public void addReview(Review review) {
// TODO: Write code to add the review to the list
reviews.add(review);
}
public String toString() {
int runningTimeMinutes = (int) this.getRunningTime().getSeconds() / 60;
return String.format("%s (%s, %s) %s {%s min}", this.title, this.rating, this.releaseDate.getYear(), genres, runningTimeMinutes);
}
public List<Review> getReviews() {
return reviews;
}
//method that returns an integer value after comparing titles of this movie and another. returns a negative value
//if this movie should come before other, 0 if both have same titles, a positive value if other should come first
@Override
public int compareTo(Movie other) {
return this.title.compareTo(other.title);
}
}