![](/img/trans.png)
[英]My arraylist size is a zero even though I've added items to the it
[英]I've added things into my ArrayList but still not working for loop, and function
因此,我必须为我的一项任务创建一个电影售票亭出租系统,并且必须向数据库中添加五部电影。 我已经正确地做到了(我相信),但是当我实现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)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.