
// DirBase class
// =============

import java.io.*;

public class DirBase

/*	methods include :
		size
		close
		add
		contains
		getEntry
		find
		indexPrint
		mainPrint
*/
{
	static Index index;

	// constructor method ..

	public DirBase(String dbName)
	{
		index = new Index();
		index.open(dbName+".ind");
		try	{
			index.load();
		}
		catch (IOException e)
		    {
		    //	if we can't load the index, not much point in
		    //	continuing ..
			System.exit(0);
		}
		index.print();
		DirEntry.open(dbName+".dat");
	} // end of constructor

	public int size()
	// return how many records are currently in the system
	{
		return index.size();
	}

	public void close()
	// close the database down ..
	// delegates to the Index and DirEntry classes ..
	{
		try	{
			index.save();
			index.close();
			DirEntry.close();
		}
		catch (IOException e)
		    {
			System.exit(0);
		}
	} // end of method "close"

	public void add(String sname, String fname, int number)
	// adds a directory entry triple to the system ..
	{
		// create a new "dirEntry"
		DirEntry rec = new DirEntry(fname,sname,number);

		// find out where the end of the main file is 
		int nextRecSlot = index.size();

		try	{
			// write the new entry into the main file
			rec.write(nextRecSlot);
		}
		catch (IOException e)
		    {
		    // not doing anything intelligent here!
		}

		// create a new index element for the entry
		IndexElem ie = new IndexElem(sname,nextRecSlot);

		// add it to the index
		index.addElement(ie);

	} // end of method "add"

	public int contains (String surname)
	// given a key surname, return it's position in the index
	// if it is not present, return -1
	{
		int recNum = -1;
		if (index.contains(surname))
		{
			recNum = index.indexOf(surname);
		}
		return recNum;
	} // end of method "contains"

	public DirEntry getEntry(int recNum)
	// given a position in the index, return the record indicated by
	// that index element
	{
		int  mainRecNum, position;
		DirEntry rec = new DirEntry();

		// get the relevant index element
		IndexElem ie =  index.elementAt(recNum);

		// extract the main record number from the index element
		mainRecNum = ie.getNum();

		try	{
			// read the directory entry
			rec.read(mainRecNum);
		}
		catch (IOException e)
		{
			// make it into a "null" entry
			rec.clear();
		}
		return rec;
	} // end of method "getEntry"

	public DirEntry find (String surname)
	// this method combines "contains" and "getEntry" for
	// the convenience of the client.
	// given a key "surname", this returns the matching directory entry.
	// If not found, returns a "null" entry.
	{
		int recNum = contains(surname);
		if (recNum == -1)
		{
			DirEntry empty = new DirEntry();
			empty.clear();
			return empty;
		}
		return (getEntry(recNum));
	} // end of method "find"
	
	public void indexPrint()
	// debugging method ..
	{
		index.print();
	}

	public void mainPrint()
	// debugging method ..
	// sequentially read and display the contents of the main file,
	// one record at a time.
	{
		int i = 0;
		DirEntry rec = new DirEntry();
		boolean goon = true;

		while (goon)
		{
			try	{
				rec.read(i);
			}
			catch (IOException e)
			    {
				// we expect "end-of-file" to occur, so
				// stop the loop when it does.
				goon = false;
			};
			if (goon) System.out.println(i+" "+rec);
			i++;
		}
	} // end of method "mainPrint"

} // end of class DirBase

