
package web2;
public class Movie {
private String title;
private String director;
private String actor;
private int rating;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDirector() {
return director;
}
public void setDirector(String director) {
this.director = director;
}
public String getActor() {
return actor;
}
public void setActor(String actor) {
this.actor = actor;
}
public int getRating() {
return rating;
}
public void setRating(int rating) {
this.rating = rating;
}
}
package web2;
import java.util.ArrayList;
import java.util.List;
public class MovieService {
public List<Movie> getMovieList() {
List<Movie> movieList = new ArrayList<Movie>();
Movie goneWithTheWind = new Movie();
goneWithTheWind.setTitle("Gone with the Wind");
goneWithTheWind.setDirector("Victor Fleming");
goneWithTheWind.setActor("Vivien Leigh");
goneWithTheWind.setRating(10);
movieList.add(goneWithTheWind);
Movie backToTheFuture = new Movie();
backToTheFuture.setTitle("Back To The Future");
backToTheFuture.setDirector("Robert Zemeckis");
backToTheFuture.setActor("Michael J Fox");
backToTheFuture.setRating(10);
movieList.add(backToTheFuture);
Movie starWars = new Movie();
starWars.setTitle("Star Wars");
starWars.setDirector("George Lucas");
starWars.setActor("Harrison Ford");
starWars.setRating(10);
movieList.add(starWars);
return movieList;
}
public Movie getMovie(String title) {
List<Movie> movieList = getMovieList();
for (Movie movie : movieList) {
if (title.equals(movie.getTitle())) {
return movie;
}
}
return null;
}
}
| getMovieList() | This class returns a list of Movie objects. |
| getMovie(String title) | This class returns a Move that has the same name as the passed argument. |
In this tutorial, all of the data is hard coded into the class for the purpose of simplifying the tutorial. If you were to implement a Web application in production, the data would come from databases, EJBs, or other server-side components.
