movie code
import java.util.*;
class MovieSystem {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Input number of movies
int n = sc.nextInt();
sc.nextLine(); // Consume the remaining newline
List<Movie> movies = new ArrayList<>();
// Input movie details
for (int i = 0; i < n; i++) {
int movieID = sc.nextInt();
sc.nextLine();
String movieName = sc.nextLine();
double rating = sc.nextDouble();
int actorCount = sc.nextInt();
sc.nextLine(); // Consume the remaining newline
Set<String> actors = new HashSet<>();
for (int j = 0; j < actorCount; j++) {
String actor = sc.nextLine();
actors.add(actor);
}
movies.add(new Movie(movieID, movieName, rating, actors));
}
// Input set of actors
int actorSetSize = sc.nextInt();
sc.nextLine();
Set<String> actorSet = new HashSet<>();
for (int i = 0; i < actorSetSize; i++) {
String actor = sc.nextLine();
actorSet.add(actor);
}
// Compute and print average rating
double avgRating = calculateAverageRating(movies, actorSet);
if (avgRating > 0) {
System.out.printf("Average Rating: %.2f\n", avgRating);
} else {
System.out.println("No movies found for the given actors.");
}
// Print movies involving the actors
Set<String> involvedMovies = getMoviesByActors(movies, actorSet);
if (!involvedMovies.isEmpty()) {
System.out.println("Movies:");
for (String movie : involvedMovies) {
System.out.println(movie);
}
} else {
System.out.println("No movies found for the given actors.");
}
sc.close();
}
// Method to calculate average rating of movies involving the given actors
public static double calculateAverageRating(List<Movie> movies, Set<String> actorSet) {
double totalRating = 0;
int count = 0;
for (Movie movie : movies) {
if (!Collections.disjoint(movie.getActors(), actorSet)) {
totalRating += movie.getRating();
count++;
}
}
return count > 0 ? totalRating / count : 0;
}
// Method to retrieve movies involving the given actors
public static Set<String> getMoviesByActors(List<Movie> movies, Set<String> actorSet) {
Set<String> result = new TreeSet<>();
for (Movie movie : movies) {
if (!Collections.disjoint(movie.getActors(), actorSet)) {
result.add(movie.getMovieName());
}
}
return result;
}
}
// Movie Class with Getters and Setters
class Movie {
private int movieID;
private String movieName;
private double rating;
private Set<String> actors;
public Movie(int movieID, String movieName, double rating, Set<String> actors) {
this.movieID = movieID;
this.movieName = movieName;
this.rating = rating;
this.actors = actors;
}
// Getters
public int getMovieID() {
return movieID;
}
public String getMovieName() {
return movieName;
}
public double getRating() {
return rating;
}
public Set<String> getActors() {
return actors;
}
// Setters
public void setMovieID(int movieID) {
this.movieID = movieID;
}
public void setMovieName(String movieName) {
this.movieName = movieName;
}
public void setRating(double rating) {
this.rating = rating;
}
public void setActors(Set<String> actors) {
this.actors = actors;
}
}