import java.io.*;

/**
 * A class the calculates the size of a directory and
 * its subdirectories
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0
 */
public class DirectorySizeCalculator
{
    private boolean     verbose;
    private File        top;
    public  int         numberOfDirectories, numberOfOtherFiles;
    public  long        totalSize;


    /**
     * Explicit Value Constructor
     *
     * @param path    The path to the starting directory
     * @param verbose true to report on progress along the way
     */
     public DirectorySizeCalculator(String path, boolean verbose)
     {
			this.verbose = verbose;
			numberOfDirectories = 0;
			numberOfOtherFiles  = 0;
			totalSize           = 0L;

			top = new File(path);
    }


    /**
     * Get the number of directories
     *
     * @return The number of directories
     */
    	public int getNumberOfDirectories()
    	{
			return numberOfDirectories;
    	}


    /**
     * Get the number of files (not including directories)
     *
     * @return The number of files
     */
    	public int getNumberOfOtherFiles()
    	{
			return numberOfOtherFiles;
    	}


    /**
     * Get the total size (in bytes)
     *
     * @return The total size
     */
    	public long getTotalSize()
    	{
			return totalSize;
    	}


    /**
     * Perform the search
     */
    	public void search()
    	{
			search(top);
    	}


    /**
     * Recursively search the given directory and its children
     *
     * @param directory   The directory to search
     */
    	private void search(File directory)
    	{
			File[]       contents;
			int          i;


			contents = directory.listFiles();
			for (i=0; i < contents.length; i++)
			{
	    		if (contents[i].isDirectory())
	    		{
					numberOfDirectories++;
					if (verbose) 
						System.out.println("Entering "+
									contents[i].getName()+
									"...");
					search(contents[i]);
	    		}
	    		else
	    		{
					if (verbose) 
						System.out.println(contents[i].getName());
					numberOfOtherFiles++;
					totalSize += contents[i].length();
	   		} // end else
		} // end for
    } // end search

}// end DirectorySizeCalculator
