Итак, мне нужно создать систему проката киосков для одного из моих заданий, и мне нужно добавить пять фильмов в базу данных. Я сделал это правильно (я полагаю), но когда я реализую функцию removeMovie (), она возвращает ноль, особенно. Я не уверен, что делаю не так.
Вот мой код:
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.
}
}