I'd like a loop that will increment it so that when I add a document to a Directory, it'll also add that Document to the parent of the Directory, and add one to its parent, till it reaches the root Directory, then it stops.
Then I somehow need to add the names of each of these Documents to an ArrayList, or an array, -preferably an ArrayList, and return all the names with the Number of Documents.
However, since there could be at most 100 Documents in a Directory, how do I do it without having to create 100 arrays, 98 or which won't be used if I do and will return Null Pointer Exceptions?
/** * */ package Assignment3; /** * @author pradcoc *This is directory. It uses most of the parent methods, but has a toString() method *and since Object already has one, it's silly to make Document abstract for that. *It can add Documents and sets itself as the parent of those Documents. */ public class Directory extends Document { private int numOfDocs = 0; public Directory(String name, Directory parent) { super(name, parent); } // this is supposed to add a Document to a Directory, set that Directory as // the parent of the Document, unless the Document is the root directory, // and also add up from 0 the number of Documents it has in it. And I need // a way to return the names of the Documents in it too. public void add(Document doc) { // if the Document is a Directory if (doc instanceof Directory) { // if it's not the root directory if(doc.getParent() != null) { doc.setParent(this); numOfDocs = numOfDocs + 1; // adds one to this Directory's number of Documents doc.getParent().numOfDocs = doc.getParent().numOfDocs + 1; // should also add // one to parent Directory's number of Documents as well. Not quite working nicely. } // if it's the root directory else { doc.setParent(this); // sets this directory, the root directory, as the parent numOfDocs = numOfDocs + 1; // adds 1 to the number of Documents this Directory has } } else { if (doc.getParent().getParent() != null) { doc.setParent(this); doc.getParent().numOfDocs = doc.getParent().numOfDocs + 1; // doc.getParent().numOfDocs = doc.getParent().numOfDocs + 1; } else { doc.setParent(this); doc.getParent().numOfDocs = doc.getParent().numOfDocs + 1; } } } public int returnNumberOfDocs() { return(numOfDocs); } // my test program can set a parent, but it can't tell if a directory is a parent of another directory or file. }
I need a method called toString() that'll return the names of all the Documents in each Directory and the number of Documents in each Directory.
I know I could create an array of size numberOfDocumentsInDirectory and get it to return the names that way, but how do I do that for potentially 100 Documents in each Directory, all of which, I suppose, could be Directories?