/**
 * Dictionary implementation using an unsorted array.
 *
 * Limitations:  the constructor takes an argument that specifies
 *               the maximum number of entries that can be stored in the 
 *               dictionary.  Inserting additional entries into the dictionary
 *               results in a runtime exception.
 *
 * David Liben-Nowell (CS127, Carleton College)
 * Winter 2006
 *
 */
public class DictionaryUnsortedArray implements Dictionary {

    private DictionaryEntry[] dict; // dict[i] will store the ith entry
                                    // sorted by order of insertion.

    private int dictSize;           // current number of stored word/def pairs 

    private int maxDictSize;        // maximum number of pairs that
				    // can be stored.

    /**
     * Constructor -- the maxSize parameter sets the maximum number of
     * entries that can be stored in the dictionary.
     */
    public DictionaryUnsortedArray(int maxDictSize) {
	this.maxDictSize = maxDictSize;
	dictSize = 0;
	dict = new DictionaryEntry[maxDictSize];
    }

    /**
     * Insert a word and its definition into the dictionary.  Simply
     * puts the new word into the first unused slot of dict[].
     */
    public void insert(String word, String definition) {
	if (dictSize >= maxDictSize) {
	    throw new RuntimeException("Cannot insert into full dictionary");
	}

	dict[dictSize] = new DictionaryEntry(word,definition);
	dictSize++;
    }

    /**
     * Return a definition for a word that is in the dictionary.
     * Throws an exception if the word is not in fact in the dictionary.
     */
    public String define(String word) throws NotInDictionaryException {
	int i;
	for (i=0; i<dictSize; i++) {
	    if (word.compareTo(dict[i].getWord()) == 0) {
		return dict[i].getDefinition();
	    }
	}
	throw new NotInDictionaryException(word + "is not in dictionary");
    }

}
