gpt4 book ai didi

java - 我已将一些内容添加到 ArrayList 中,但仍然无法使用 for 循环和函数

转载 作者:太空宇宙 更新时间:2023-11-04 10:29:45 25 4
gpt4 key购买 nike

因此,我必须为我的一项作业创建一个电影亭租赁系统,并且我必须将五部电影添加到数据库中。我已经正确地做到了这一点(我相信),但是当我实现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(标题,年份);

我不太明白这个。它看起来像一个构造函数,但显然不是???

不管怎样,我觉得你正在创建一个新的电影对象,然后尝试从你的集合中删除该对象。当然,它不在您的 Collection 中。它可能是具有完全相同属性的另一个对象。但这并不意味着它与存储在集合中的对象相同。

我建议您阅读 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)
}
}

关于java - 我已将一些内容添加到 ArrayList 中,但仍然无法使用 for 循环和函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50146513/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com