/* Directory.java 1.0 */ /* by Sharon Tuttle */ /* last modified: 2-18-03 */ class Directory { /*-------------------------- fields ----------------------------*/ /* the FIRST element in the directory */ DirectoryElement first; /* how many are IN the directory instance right now? */ int count; /*------------------------------ constructors --------------------------------*/ /* create an empty Directory object */ Directory() { this.first = null; this.count = 0; } /* create a Directory containing just a single entry */ Directory(CityEntry entry) { this.first = new DirectoryElement(entry); this.count = 1; } /*---------------------------------- accessors ------------------------------------*/ DirectoryElement getFirst() { return this.first; } int getCount() { return this.count; } /*----------------------------------------------------- overridden methods --------------------------------------------------------*/ public String toString() { if (this.count == 0) { return "Directory[]"; } else { return "Directory[\n" + printAllInDir(this.first) + "]"; } } /*--------------------------------------- other methods ----------------------------------------*/ /*------------------------------------------------------------- printAllInDir() Purpose: a helper method for toString, to help it in depicting all of the CityEntry's in a Directory. (deliberately made private --- for "local" class use only!) ----------------------------------------------------------------*/ private String printAllInDir(DirectoryElement curr) { if (curr != null) { return curr.dirEntry + "\n" + printAllInDir(curr.nextInDirectory); } else { return ""; } } /*------------------------------------------------------------ addToBeginning Purpose: make CityEntry entry the first element in the calling Directory instance. ---------------------------------------------------------------*/ void addToBeginning(CityEntry entry) { DirectoryElement newDirElem = new DirectoryElement(entry); /* new element is in front of CURRENT first element */ newDirElem.setNextInDirectory(this.first); /* new element should now be FIRST in directory */ this.first = newDirElem; /* and, of course, count in directory must be increased! */ this.count = this.count + 1; } }