import java.io.*;

// CS100 Fall 2000 Assignment 3.
public class Program3 {

	// Initialize Text object in to read from standard input.
		static TokenReader in = new TokenReader(System.in);
		
	// Display every person in the database.
	static void showPeople() {
		int n = Person.numberOfPeople();
		for (int j=0; j < n; j++){
		    Person p = Person.intToPerson(j);
			System.out.println(p);
		}
	}
       
	// Return the person with name s in the database.
	public static Person stringToPerson(String s) {
		int n = Person.numberOfPeople();
		int j=0;

		// Search until the person is found.
		while (j < n && !s.equals(Person.intToPerson(j).name()))
			j++;

		// Return the person if j is valid.
		if (j<n) 
			return Person.intToPerson(j);
		else 
			return null;
	}
	
	// Return true if a Person named s already exists, false otherwise.
	public static boolean personExists(String s) {
		int n = Person.numberOfPeople();
		for (int j=0; j < n; j++){
		    Person p = Person.intToPerson(j);
			if (s.equals(p.name())) return true;
		}
		return false;
	}
	
	public static void printError(String msg)
	{
		System.out.println("ERROR: " + msg);

	}
	
	// Read one name, n1. 
	// If there is not already an existing person named n1,
	// create a new person named n1 of the given sex; 
	// otherwise print an appropriate error message.
	public static void createPerson(boolean sex)
	{
		String p = in.readString();
		if (personExists(p)) 
			printError("There already is a person named " + p);
		else
			new Person(p, sex);
	}
	
	// Read two names, n1 and n2.
	// If n1 and n2 are people who have been created,
	// and n1 has no more than one friend, 
	// and n1 is not n2, 
	// record or delete (depending on addFriend) 
	// a friendship between n1 and n2; 
	// otherwise, print an appropriate error message.
	public static void addOrDeleteFriend(boolean addFriend)
	{
		String p = in.readString();
		String q = in.readString();
				
		if (personExists(p) && personExists(q))
		{
			Person p_person = stringToPerson(p);
			Person q_person = stringToPerson(q);
					
			if (addFriend)
			{
				int returnCode = p_person.newFriend(q_person);
				if (returnCode == 1) 
					printError("You can't be your own friend");
				else if (returnCode == 2) 
					printError(p + " already considers " + q + " a friend");
				else if (returnCode ==3) 
					printError(p + " already has two friends");
			}
			else
			{
				if (! p_person.removeFriend(q_person))
					printError(p + " does not consider " + q + " a friend");

			}
		} else 
		{
			if (!personExists(p))
				printError("No such person as " + p);
			if (!personExists(q))
				printError("No such person as " + q);
		}
	}
	
	// Prompt with "> ", input string, and return it.
	public static String promptAndGetCommand() 
	{
		System.out.print("> ");
		System.out.flush();
		return in.readString();
	}
		
	public static void main(String args[]) 
	{
		// Read first command.
		   String s = promptAndGetCommand();

		// Process each command until "q".
		   while ( !s.equals("q") )
		   {        
			   if ( s.equals("b")) 
					createPerson(true);
			   else if ( s.equals("g")) 
					createPerson(false);
			   else if (s.equals("f"))
					addOrDeleteFriend(true);
			   else if (s.equals("u"))
					addOrDeleteFriend(false);
			   else if ( s.equals("?"))
			   		{
			   			String name = in.readString();
			   			if (personExists(name))
				 			System.out.println(stringToPerson(name));   
				 		else 
				 			printError("No such person as " + name);
                 	}
			   else if ( s.equals("p"))
					showPeople();                   
			
		 	   s = promptAndGetCommand();
		}
	}
}