Untitled
unknown
java
2 years ago
2.3 kB
7
Indexable
import java.util.*;
public class bluebox {
// ArrayLists for different genres of movies
private static ArrayList<Movie> action = new ArrayList<>();
private static ArrayList<Movie> comedy = new ArrayList<>();
// ... other genre lists ...
// HashMap to store customer receipts (customer name/ID mapped to list of rented movies)
private static HashMap<String, List<Movie>> customerReceipts = new HashMap<>();
// HashSet to ensure unique movie titles
private static HashSet<String> uniqueMovieTitles = new HashSet<>();
// Array to store average ratings for each genre
private static double[] genreRatings = new double[5]; // 0: Action, 1: Comedy, ...
public static void main(String[] args) {
// ... existing setup ...
// Example of adding movies and ensuring uniqueness
addMovie(new ActionMovie("Transformers", 8, true));
// ... add other movies ...
// Calculate and store average ratings for each genre
calculateGenreRatings();
// ... existing user input handling ...
}
// Method to add a movie to the appropriate genre list and check for uniqueness
private static void addMovie(Movie movie) {
// Check if the movie title is already present
if (uniqueMovieTitles.add(movie.getName())) {
// Add movie based on genre
switch (movie.getGenre().toLowerCase()) {
case "action": action.add(movie); break;
// ... other cases ...
}
} else {
// Notify if movie already exists
System.out.println("Movie already exists: " + movie.getName());
}
}
// Method to calculate average ratings for each genre
private static void calculateGenreRatings() {
genreRatings[0] = calculateAverageRating(action);
genreRatings[1] = calculateAverageRating(comedy);
// ... calculations for other genres ...
}
// Utility method to calculate the average rating of a list of movies
private static double calculateAverageRating(ArrayList<Movie> movies) {
double total = 0;
for (Movie movie : movies) {
total += movie.getRating();
}
return movies.size() > 0 ? total / movies.size() : 0;
}
}
Editor is loading...
Leave a Comment