简体   繁体   中英

Creating a relationship between two ArrayList objects

I have two ArrayLists , moviesAvailable and moviesRented , in a class called Catalogue and I have another ArrayList called currentlyRented in a class called Customer . I want the moviesRented and currentlyRented to be associated so that one Customer can have many movies rented.

I have the code so that when a customer rents a movie, that movie is removed from the moviesAvailable class and addeto the moviesRented class. I want each customer to have a currentlyRented list.

The Catalogue class:

private Kiosk kiosk;
private List<Movie> moviesAvailable = new ArrayList<>();
private List<Movie> moviesRented =  new ArrayList<>();
private List<Genre> genres =  new ArrayList<>();

public void rentMovie(int id, String title) {
    Movie mov = movie(title);
    if(mov.hasType(title))
        moviesRented.add(mov);
        moviesAvailable.removeIf(movie -> movie.hasType(title));
        System.out.println("Movie rented.\n");
}
public Movie movie(String title) {
    for(Movie movie : moviesAvailable)
        if(movie.hasType(title))
            return movie;
    return null;
}
public void returnMovie(int id, String title) {
    Movie mov = movie(title);
        moviesRented.removeIf(movie -> movie.hasType(title));
        moviesAvailable.add(mov);
        System.out.println(title + " has been returned.");
}

The Customer Class:

private int ID;
private String name;
private int balance;
private List<Movie> currentlyRented = new ArrayList<>();
private List<Movie> rentingHistory = new ArrayList<>();

public Customer(int ID, String name, int balance) {
    this.ID = ID;
    this.name = name;
    this.balance = balance;
}
public boolean hasType(int id) {
    return id == (this.ID);
}
public void movie(int id, String title) {
    Movie movie = movie(title);
        currentlyRented.add(movie);
        rentingHistory.add(movie);
}
public void returnMovie(int id, String title) {
    Movie mov = movie(title);
    if(mov.hasType(title))
        currentlyRented.removeIf(movie -> movie.hasType(title));
}
public Movie movie(String title) {
    for(Movie movie : currentlyRented)
        if(movie.hasType(title))
            return movie;
    return null;
}
public void rents() {
    System.out.println(name + " has the following movies: " );
    System.out.println("Movies currently rented by " + name + ":");
}

When I run the full program, what happens is if I enter a customer id , and the title of the movie, it rents the movie. When I rent another one using a different id , once again it rents. However when I enter a customer id to return a movie, it lists the movies of both customers.

You are duplicating the behavior of renting/returning a movie in both Customer and Catalog . Choose one, then manage all lists contents from there. Here is an example implementation where Customer performs the operations:

class Movie {

    String title;

    public Movie(String title) {
        this.title = title;
    }
    // equals, hashCode, toString
}

class Catalog {

    public static final List<Movie> moviesAvailable = new ArrayList<>();
    public static final List<Movie> moviesRented = new ArrayList<>();
}

class Customer {

    private List<Movie> currentlyRented = new ArrayList<>();
    private List<Movie> rentingHistory = new ArrayList<>();

    public void rentMovie(String title) {
        Movie movie = new Movie(title);
        if (Catalog.moviesAvailable.remove(movie)) {
            Catalog.moviesRented.add(movie);
            currentlyRented.add(movie);
            System.out.println("Movie " + movie + " was rented to " + this);
        } else {
            System.out.println("Movie " + movie + " is not available");
        }
    }

    public void returnMovie(String title) {
        Movie movie = new Movie(title);
        if (currentlyRented.remove(movie)) {
            rentingHistory.add(movie);
            Catalog.moviesRented.remove(movie);
            Catalog.moviesAvailable.add(movie);
            System.out.println("Movie " + movie + " was returned by " + this);
        } else {
            System.out.println("Movie " + movie + " is not being rented by " + this);
        }
    }
}

Since there is only one Catalog , either make it a singleton or use the static members approach as demonstrated above (which is simpler). If you are careful, all lists will be correctly synchronized. However, this approach allows the catalog lists to be modified outside of the customer's methods. Then even though currentlyRented contains a movie, Catalog.moviesRented might not. There are other designs that can eliminate this issue, but this is out of scope for this question.

I think it would be better design to have a Rental class too, that would hold a Movie and a Customer, (or a list of customers if each movie could be rented by more than one customer). Then your Catalogue class could have a list of Rentals for all Customers, and a list of Movies (all of them, rented or not). Like this, you could iterate through all the Rentals of your Catalogue class and return only the ones applicable to a specific Customer ID etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM