我已经在我的ArrayList中添加了东西,但仍然不能用于循环和函数

时间:2018-05-03 03:30:58

标签: java arraylist netbeans null

所以我必须为我的一个作业创建一个电影亭租赁系统,我必须在数据库中添加五部电影。我已经做得很好(我相信)但是当我实现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.
}

}

1 个答案:

答案 0 :(得分:0)

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)
    }
}