import java.io.*;

public class Office implements Serializable
{
	// Declare object arrays to hold owners, appointments and pets.
	// These have class scope.
	Owner[] owners;
	Appt[] appts;
	static ObjectIO io = new ObjectIO();

	public static void main (String[] args)
	{
		Office test = new Office();	// instantiate the Office class
		test.addOwners();		// create owners and pets
		test.makeAppts();		// schedule appointments
		test.showOwners();		// print owners with their pets
		test.showAppts();		// print appointments
		test.writeData();		//call method to write save arrays
		System.exit(0);
	}

	// this adds owner and pet data to the arrays. In the "real world",
	// this data would be read from a file or database.
	private void addOwners()
	{
		owners = new Owner[3];
		Pet[] pets;

		// create owner Joe Shmo with pets Rufus and Fifi
		pets = new Pet[2];
		pets[0] = new Pet ("Rufus", "rottweiler", 'M');
		pets[1] = new Pet ("Fifi", "toy poodle", 'F');
		owners[0] = new Owner ("Joe Shmo", "123 Elm", pets);

		// create owner Sally Sanchez with pets Oliver, Cole and Angel
		pets = new Pet[3];
		pets[0] = new Pet ("Oliver", "cat", 'M');
		pets[1] = new Pet ("Cole", "lab", 'M');
		pets[2] = new Pet ("Angel", "cockatoo", 'F');
		owners[1] = new Owner ("Sally Sanchez", "245 Oak", pets);

		// create owner Mona Lisa with pet Sammy
		pets = new Pet[1];
		pets[0] = new Pet ("Sammy", "rat", 'M');
		owners[2] = new Owner ("Mona Lisa", "987 Pine", pets);
	}

	// schedule appointments
	private void makeAppts()
	{
		appts = new Appt[4];
		appts[0] = new Appt ("4/10/04", "3:30 PM", 3, owners[0].getPet("Rufus"));
		appts[1] = new Appt ("4/15/04", "10:00 AM", 2, owners[0].getPet("Fifi"));
		appts[2] = new Appt ("4/15/04", "10:00 AM", 3, owners[2].getPet("Sammy"));
		appts[3] = new Appt ("4/16/04", "1:00 PM", 2, owners[1].getPet("Brutus")); //no such pet
	}

	// print a report of owner names and their pet names and breeds
	private void showOwners()
	{
		Pet[] pets;

		System.out.println ("OWNERS AND PETS:\n");		// header
		for (int i = 0; i < owners.length; i++)			// loop through owners array
		{
			System.out.println (owners[i].getName());	// print owner name
			pets = owners[i].getPets();			// get the owner's pets

			for (int j = 0; j < pets.length; j++)		// loop through the pet array
			{
				System.out.println ("\t" 		// print pet name and breed
				+ pets[j].getName() + ", "
				+ pets[j].getBreed());
			}
			System.out.println ();				// print a space
		}
	}

	// print a summary of appointments
	private void showAppts()
	{
		System.out.println ("APPOINTMENTS:\n");			// header
		for (int i = 0; i < appts.length; i++)			// loop through the appointments array
		{
			System.out.println (appts[i].toString());	// print appointment
		}
		System.out.println ("\n");
	}

	private void writeData()
	{
		io.writeFile(owners, "owners.txt");
		io.writeFile(appts, "appts.txt");
	}
}
