JAVA Can someone explain how I would write the public static List <Film>findFilm
ID: 3602140 • Letter: J
Question
JAVA
Can someone explain how I would write the
public static List <Film>findFilmByTitle (String title) ? Im having a hard time understanding it.
public class DataSource final static String FILM_FILE final static String ACTORS_FILE final static String FILM-ACTORS-LINK-FILE = "film-actors, csV". "films.csv" "actors.csv" public static void init String realPath ServletUtils.getProjectInputFilesPath); FilmReader aReader -new FilmReader); List films aReader.readFilmFile(realPath, FILM_FILE) FilmCatalog filmInventory FilmCatalog.getInstance); filmInventory.addAll (films); ActorReader actorReader new ActorReader (); List actors actorReader. readActorFile(realPath, ACTORS_FILE) ActorInventory actorInventory-ActorInventory.getInstance); actorInventory.addAll (actors); FilmActorReader fiLmActorReader new FilmActorReader O; List pairs- filmActorReader.readFilmActorFile(realPath, FILM_ACTORS_LINK_FILE); FilctorBuilder builder = new FilmActorBuilder (); builder.build(filmInventory, actorInventory, pairs); public static List findFi lmByTitle (String title) f // You will write this method using the Strategy pattern... return foundFilms;Explanation / Answer
Solution:
In order to find films matching the title which is passed, we need to have the film list as well. But since it is not passed to the method i assume it is accessible from the method as well. You will find below two code snippets, if your code supports Java 8 syntax you can go with the lambda expression, or can choose for the generic one. It is also assumed that the 'title' is compared to an attribute called title from Film class
public static List<Film> findFilmByTitle (String title){
List<Film> foundFilms = new ArrayList<Film>();
for(Film film : this.films){
if(title.equals(film.getTitle())){
foundFilms.add(film);
}
}
return foundFilms;
//Using Lambda expressions and Stream class
// return this.films.stream().filter(film -> title.equals(film.getTitle())).collect(Collectors.toList());
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.