import java.util.*; public class MovieStore { private HashMap movieList; private HashMap> actorList; private HashMap> typeList; public MovieStore() { movieList = new HashMap(); actorList = new HashMap>(); typeList = new HashMap>(); } public HashMap getMovieList() { return movieList; } public HashMap> getActorList() { return actorList; } public HashMap> getTypeList() { return typeList; } public String toString() { return ("MovieStore with " + movieList.size() + " movies."); } //This method adds a movie to the movieStore. public void addMovie(Movie aMovie) { //Add to the movieList movieList.put(aMovie.getTitle(), aMovie); //If there is no category matching this movie's type, make a new category if (!typeList.containsKey(aMovie.getType())) typeList.put(aMovie.getType(), new ArrayList()); //Add the movie to the proper category. typeList.get(aMovie.getType()).add(aMovie); //Now add all of the actors for (String anActor: aMovie.getActors()) { //If there is no actor yet matching this actor, make a new actor key if (!actorList.containsKey(anActor)) actorList.put(anActor, new ArrayList()); //Add the movie for this actor actorList.get(anActor).add(aMovie); } } //This private method removes a movie from the movie store private void removeMovie(Movie aMovie) { //Remove from the movieList movieList.remove(aMovie.getTitle()); //Remove from the type list vector. If last one, remove the type. typeList.get(aMovie.getType()).remove(aMovie); if (typeList.get(aMovie.getType()).isEmpty()) typeList.remove(aMovie.getType()); //Now Remove from the actors list. If actor has no more, remove him. for(String anActor: aMovie.getActors()) { actorList.get(anActor).remove(aMovie); if (actorList.get(anActor).isEmpty()) actorList.remove(anActor); } } //This method removes a movie (given its title) from the movie store public void removeMovieWithTitle(String aTitle) { if (movieList.get(aTitle) == null) System.out.println("No movie with that title"); else removeMovie(movieList.get(aTitle)); } //This method lists all movie titles that are in the store public void listMovies() { for (String s: movieList.keySet()) System.out.println(s); } //This method lists all movies that star the given actor public void listMoviesWithActor(String anActor) { for (Movie m: actorList.get(anActor)) System.out.println(m); } //This method lists all movies that have the given type public void listMoviesOfType(String aType) { for (Movie m: typeList.get(aType)) System.out.println(m); } }