import java.util.HashMap;

    // Illustrate the HashMap class by using one to create a glossary.
    // The contents of the glossary are read from a file as pairs of lines.
class GlossaryManager {
    public static final String defaultFile = "glossary.txt";

    public GlossaryManager(){
        this(defaultFile);
    }

        // Set up the glossary from the given file.
    public GlossaryManager(String glossaryFile){
        SimpleInput file = new SimpleInput(glossaryFile);
        HashMap gloss = getGlossary();
        String word = file.nextLine();
        while(!word.equalsIgnoreCase(endMarker)){
            String definition = file.nextLine();
            if(gloss.put(word,definition) != null){
                System.out.println(word+" is defined more than once.");
            }
            word = file.nextLine();
        }
    }

        // Interact with the user to look up words.
    public void interact(){
        System.out.println("There are: "+getGlossary().size()+
                           " words in the glossary.");

        SimpleInput keyboard = new SimpleInput();
        System.out.println("Input words to be looked up. Use "+
                           endMarker+" to finish.");
        String word = keyboard.nextLine();
        while(!word.equalsIgnoreCase(endMarker)){
            System.out.println(word+": "+definition(word));
            word = keyboard.nextLine();
        }
    }

        // Always return a non-null string, even if it is an error message.
    public String definition(String word){
        HashMap gloss = getGlossary();
        String definition = (String) gloss.get(word);
        if(definition != null){
            return definition;
        }
        else{
            return "I have no definition for "+word;
        }
    }

    private HashMap getGlossary(){
        return glossary;
    }

    private HashMap glossary = new HashMap();
    private final String endMarker = "end";
}
