/**
 * Netflix: a class to model a Netflix distribution center in a
 * NetFlix-type movie-rental system.
 *
 * @author CS117 3a (Carleton College, Winter 2006)
 * @version 1.00 (designed in class 10 February 2006, written 15 February 2006)
 */
class Netflix {

    private Movie[] movies;   // the center's movies (rented and unrented)
    private int capacity;     // the size of the movies[] array.
    private int numMovies;    // the number of actual movies in that array.

    /**
     * Construct a new Netflix object, with a default capacity of 100
     * movies.
     */
    public Netflix() {
	capacity = 100;
	movies = new Movie[capacity];
	numMovies = 0;
    }

    /**
     * Add a new movie to movies[].
     */
    public void addMovie(Movie movie) {
	// If we're completely out of space in the array movies[], we
	// need to make more room in order to add in this new movie.
	// To do so, we'll create a new array with twice as much space
	// and copy over all of the movies from the old array.
	if (numMovies == capacity) {
	    Movie[] newMovies = new Movie[capacity*2];
	    for (int i = 0; i < capacity; i++) {
		newMovies[i] = movies[i];
	    }
	    movies = newMovies;
	    capacity = capacity * 2;
	}
	 
	// Now movies[] has space left in it, so we can just shove the
	// new one into the array.
	movies[numMovies] = movie;
	numMovies++;
    }

    /**
     * Returns a flag indicating whether there is an unrented copy of
     * a particular title.
     */
    private boolean available(String title) {
	for (int i = 0; i < numMovies; i++) {
	    if (title.equals(movies[i].getTitle())) {
		return true;
	    }
	}
	return false;
    }

    /**
     * Returns the best-available movie given a queue of preferences,
     * or null if none is available.
     */
    public Movie fulfillRequest(String[] queue) {
	for (int i = 0; i < queue.length; i++) {
	    for (int j = 0; j < numMovies; j++) {
		if (queue[i].equals(movies[j].getTitle()) 
		    && movies[j].isAvailable()) {
		    return movies[j];
		}
	    }
	}
	return null;
    }

    /**
     * Converts the center into a string.
     */
    public String toString() {
	String str = "";
	for (int i = 0; i < numMovies; i++) {
	    str += "(" + movies[i].getTitle()
		+ "," + movies[i].isAvailable() + ")\n";
	}
	return str;
    }

}
