// PetSounds.java
// by Scot Drysdale on 11/25/04
// modified to use Scanner and generics by Scot Drysdale on 5/21/08

// Demonstrates the use of a Map to associate animals with the sounds
// that they make.

import java.util.*;

public class PetSounds
{
	public static void main(String args[])
	{
		// a map to play with, initially empty
		Map<String,String> animalMap = new HashMap<String,String>(); 	  
		char command = ' ';                 // a command
		String animal;                      // an animal's name
		String sound;					              // Sound that the animal makes
		Scanner input = new Scanner(System.in);

		while (command != 'q') 
		{
			System.out.print("Command (q, p, g, r, P, ?): ");
			command = input.nextLine().charAt(0);;

			switch (command) 
			{
			case 'q':                   // Quit
				System.out.println("Bye");
				break;

			case 'p':                   // put
				System.out.print("Enter animal: ");
				animal = input.nextLine();
				System.out.print("Enter sound that the animal makes: ");
				sound = input.nextLine();
				animalMap.put(animal, sound);
				break;

			case 'g':                   // get
				System.out.print("Enter animal: ");
				animal = input.nextLine();
				Object obj = animalMap.get(animal);
				if(obj == null)
					System.out.println("I do not know the sound that " + animal + " makes.");
				else
					System.out.println(animal + " says " + obj);
				break;

			case 'r':                   // remove
				System.out.print("Enter animal to remove: ");
				animal = input.nextLine();
				animalMap.remove(animal);
				break;

			case 'P':                   // print
				if (animalMap.isEmpty())
					System.out.println("The map is empty");
				else
				{
					System.out.println("The animals and their sounds are:");

					Set<String> animalNames = animalMap.keySet();
					Iterator<String> iter = animalNames.iterator();

					while(iter.hasNext())
					{
						animal = iter.next();
						System.out.println(animal + " says " + animalMap.get(animal));
					}
				}
				break;

			case '?':                   // Print all the commands
				System.out.println("Commands are\n  q: quit\n  p: put\n  g: get\n  " +
				"r: remove\n  P: print\n  ");
				break;

			default:
				System.out.println("Huh?");
			}
		}
	}
}
