import java.util.Scanner;

public class Manager
{
    public final int MAX_ELEMENTS = 10;    
    private Book [] bookList;
    private int lastElement;
    
    public Manager ()
    {
	bookList = new Book[MAX_ELEMENTS];
	int i;
	for (i = 0; i < MAX_ELEMENTS; i++)
	{
	    bookList[i] = null;
	}
	lastElement = -1;
    }

    public void display()
    {
	int i;
	System.out.println("Displaying list");
	if (lastElement <= -1)
	    System.out.println("\tList is empty");
	for (i = 0; i <= lastElement; i++)
	{
	    System.out.println("\tTitle No. " + (i+1) + ": "+ bookList[i].getName());
	}
    }

    public void displayRecursive ()
    {
	if (lastElement <= -1)
	{
	    System.out.println("\tList is empty");
	    return;
	}
	else
	{
	    final int FIRST = 0;
	    System.out.println("Displaying list");
	    doRecursiveDisplay(FIRST);
	    return;
	}
    }

    private void doRecursiveDisplay (int current)
    {
        if (current <= lastElement)
	{
	    System.out.println("\tTitle No. " + (current+1) + ": "+ bookList[current].getName());
	    current++;
	    doRecursiveDisplay(current);
	}
	return;
    }

    public void add ()
    {
	String newName;
	Scanner in;
	if ((lastElement+1) < MAX_ELEMENTS)
	{
	    System.out.print("Enter a title for the book: ");
	    in = new Scanner (System.in);
	    newName = in.nextLine ();
	    lastElement++;
	    bookList[lastElement] = new Book(newName);
	}
	else
	{
	    System.out.print("Cannot add new element: ");
	    System.out.println("List already has " + MAX_ELEMENTS + " elements.");
	}
    }
    
    public void eraseList ()
    {
	lastElement = -1;
    }

    public void remove ()
    {
	if (lastElement > -1)
        {
	    bookList[lastElement] = null;
	    lastElement--;
	    System.out.println("Last element removed from list.");
	}
	else
	    System.out.println("List is already empty: Nothing to remove");
    }
}

