所以我必须为我的一个作业创建一个电影亭租赁系统,并且我必须将五部电影添加到数据库中。我已经正确地做到了这一点(我相信(,但是当我实现 removeMovie(( 函数时,它特别返回 null。我不确定我做错了什么。
这是我的代码:
import java.util.*;
public class Catalogue {
private Kiosk kiosk;
private List<Movie> moviesAvailable = new ArrayList<Movie>();
private List<Movie> moviesRented = new ArrayList<Movie>();
private List<Genre> genres = new ArrayList<>();
public Catalogue(Kiosk kiosk) {
this.kiosk = kiosk;
genres.add(new Genre ("SciFi"));
genres.add(new Genre("Drama"));
genres.add(new Genre("Crime"));
moviesAvailable.add(new Movie("Matrix", 1999, genres.get(0), 3));
moviesAvailable.add(new Movie("Titanic", 1997, genres.get(1), 4));
moviesAvailable.add(new Movie("The Silence of the Lambs", 1991, genres.get(2), 3));
moviesAvailable.add(new Movie("Jurassic Park", 1993, genres.get(0), 4));
moviesAvailable.add(new Movie("Terminator 2", 1991, genres.get(0), 3));
}
/**
*
*/
public void use() {
char choice;
while ((choice = readChoice()) != 'R') {
switch (choice){
case '1': dispMovies(); break;
case '2': dispAvail(); break;
case '3': dispGenres(); break;
case '4': dispMoviegen(); break;
case '5': dispMovieyear(); break;
case '6': rentMovie(); break;
case '7': returnMovie(); break;
}
}
}
private char readChoice() {
System.out.println("Welcome to the Catalogue! Please make a selection from the menu:");
System.out.println("1. Display all movies.");
System.out.println("2. Display all available movies.");
System.out.println("3. Display all genres.");
System.out.println("4. Display movies in a genre.");
System.out.println("5. Display all movies by year.");
System.out.println("6. Rent a movie.");
System.out.println("7. Return a movie.");
System.out.println("R. Return to previous menu.");
System.out.print("Enter a choice: ");
return In.nextChar();
}
private void dispMovies(){
}
private void dispAvail(){
}
private void dispGenres(){
}
private void dispMoviegen(){
}
private void dispMovieyear(){
}
private void rentMovie() {
}
private void returnMovie() {
}
private String readTitle() {
System.out.print("Enter the title of the movie: ");
return In.nextLine();
}
private int readYear() {
System.out.print("Enter the year: ");
return In.nextInt();
}
/**
*
*/
public void useAdmin() {
char choice;
while((choice = readChoiceadmin()) != 'R') {
switch (choice) {
case '1': listCustomers(); break;
case '2': addCustomer(); break;
case '3': removeCustomer(); break;
case '4': listMovies(); break;
case '5': addMovie(); break;
case '6': removeMovie(); break;
}
}
}
private char readChoiceadmin(){
System.out.println("Welcome to the administration menu:");
System.out.println("1. List all customers.");
System.out.println("2. Add a customer.");
System.out.println("3. Remove a customer.");
System.out.println("4. List all movies.");
System.out.println("5. Add a movie to the catalogue.");
System.out.println("6. Remove a movie from the catalogue.");
System.out.println("R. Return to the previous menu.");
System.out.print("Enter a choice: ");
return In.nextChar();
}
private void listCustomers(){
}
private void addCustomer(){
}
private void removeCustomer(){
}
private void listMovies(){
System.out.println("");
System.out.println("The Kiosk has the following movies:");
System.out.println(moviesAvailable);
System.out.println("");
}
private void addMovie(){
}
private void removeMovie(){
System.out.println("");
System.out.println("Removing a movie.");
String title = readTitle();
int year = readYear();
Movie movie = movie(title, year);
if(movie != null) {
moviesAvailable.remove(movie);
System.out.println(movie + " removed from catalogue.");
}
else
System.out.println("No such movie.");
}
private Movie movie(String title, int year) {
for (Movie movie : moviesAvailable)
if(movie.hasTitle(title) && movie.hasYear(year))
return movie;
return null;
}
Catalogue() {
//To change body of generated methods, choose Tools | Templates.
}
}
Movie movie = movie(title, year);
我真的不明白这一点。 它看起来像一个构造函数,但显然不是???
不管对我来说,它闻起来就像你正在创建一个新的电影对象,然后试图从你的收藏中删除该对象。 当然,它不在您的收藏中。 它可能是另一个具有完全相同属性的对象。 但这并不能使它成为存储在集合中的同一对象。
我建议你阅读Object.equals。 然后使用 forEach 循环访问您的集合。 大概是这样的
String title = readTitle();
int year = readYear();
Movie searchMovie = new Movie(title, year);
for (Movie movie : moviesAvailable) {
if (movie.equals(searchMovie) {
moviesAvailable.remove(movie)
}
}