// Philip Peng
// Nov 20, 2007
// For Mr. Benum
//
// The "NameList" class.
// Stores an array of names.

public class NameList
{
    protected final int MAX;
    protected String list [];
    protected int size;

    // Constructor.
    public NameList (int maxSize)
    {
    	MAX = maxSize;
    	list = new String [MAX];
    	size = 0;
    } // NameList constructor

    // Find occurance of name from a list via sequential search
    protected int find (String name) {
    	for (int i = 0; i < size; i++) {
    		if (name.equalsIgnoreCase(list[i])) {
    			return i;
    		}
    	}
    	return -1; // If not found
    } // find method
    
    protected static String toProperCase (String name) {
    	return Character.toString(Character.toUpperCase(name.charAt(0)))
    		+ name.substring(1).toLowerCase();
    } // to proper case method
    
    // Shift over all items left one space
    protected void shiftLeft(int start) {
    	// Overwrite each slot by content of slot on right side, then nulls the previously last slot
    	for (int i = start; i < (size-1); i++) {
			list[i] = list[i+1];
		}
		list[size-1] = null; // For cleanliness ; )
    }
    
    // Add a name to end of list.
    public void insert (String name)
    {
    	if (name.length()==0) {
    		System.out.println("  Error: No name inputted");
    	} else {
	    	if (size == MAX) {
	    		System.out.println("  Error: NameList is full! Try deleting some names");
	    	} else {
		    	if (find(name) == -1) {
		    		list[size] = toProperCase(name); // Add to end since size = last index + 1 ; )
		    		size++;
		    		System.out.println("  Success: Name \"" + toProperCase(name) + "\" inserted");
		    	} else {
		    		System.out.println("  Error: Name \"" + toProperCase(name) + "\" is already on list");
		    	}
	    	}
    	}
    } // insert method

    // Delete a name from list.
    public void delete (String name)
    {
    	int buffer = find(name); // Don't need to call more than once
    	if (buffer != -1) {
    		shiftLeft(buffer); // Will overwrite target slot
    		size--;
    		System.out.println("  Success: Name \"" + toProperCase(name) + "\" deleted");
    	} else {
    		System.out.println("  Error: Name \"" + toProperCase(name) + "\" could not be found");
    	}
    } // delete method

    // Output list of names.
    public void printList ()
    {
    	if (size == 0) {
    		System.out.println("  The NameList is empty!");
    	} else {
	    	System.out.println("  NameList:");
	    	for (int i = 0; i < size; i++) {
	    		System.out.println("  - " + list[i]);
	    	}
    	}
    } // printList method
    
} // NameList class
