import java.util.*;

class Database {
    Set <Entry> entries = new HashSet<Entry>();
    Map <String, Set<Entry>> categories = 
	new HashMap<String, Set<Entry>> ();

    void add(Entry e) {
	entries.add(e);
	
	String c = e.getCategory();
	Set<Entry> category = categories.get(c);
	if (category == null) {
	    categories.put(c, category = new HashSet<Entry>());
	}
	category.add(e);
	
    }

    List <Entry> lookup (String key, String attribute) {
	List <Entry> result = new ArrayList<Entry>();
	for (Entry e : entries) {
	    if (e.compare(key, attribute)) {
		result.add(e);
	    }
	} 
	return result;
    }

    List <Entry> lookup (String c, String key, String attribute) {
	List <Entry> result = new ArrayList<Entry>();
	if (categories.containsKey(c)) {
	    for (Entry e : categories.get(c)) {
		if (e.compare(key, attribute)) {
		    result.add(e);
		}
	    } 
	}
	return result;
    }

    boolean remove (Entry e) {
	boolean found = entries.remove(e);
	if (found) {
	    Set<Entry> category = categories.get(e.getCategory());
	    category.remove(e);
	}
	return found;

    }

    void print () {
	for (Map.Entry<String, Set<Entry>> categoryEntry : categories.entrySet()) { 
	    String name = categoryEntry.getKey();
	    Set<Entry> category = categoryEntry.getValue();
	    printCategory(category, name);
	}
    }

    void printCategory (Set<Entry> category, String name) {
	System.out.println("--------------------------------------------------");
	System.out.println(name);
	for (Entry e : category) { 
	    e.print(false);
	}
    }

    void print (String categoryName) {
	Set<Entry> category = categories.get(categoryName);
	if (category != null) { 
	    printCategory(category, categoryName);
	}
	else 
	    System.out.println("Category " + categoryName + " does not exist");
    }

    public static void main (String [] arg) { 
	UI ui = new UI();
	ui.commandLoop(new Scanner(System.in));
    }
}

