
   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 File        top;
      public  int         numberOfDirectories, numberOfOtherFiles;
      public  long        totalSize;
   
   
    /**
     * Explicit Value Constructor
     *
     * @param path    The path to the starting directory
     */
       public DirectorySizeCalculator(String path)
      {
         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;
      
        // Get an array of all File objects in this directory
         contents = directory.listFiles();
      
        // Process each of the File objects
         for (int i=0; i < contents.length; i++)
         {
           // Handle the easy case
         
            if (contents[i].isFile()) 
            {
               numberOfOtherFiles++;
               totalSize = totalSize + contents[i].length();
            }
         
           // Refine the hard cases
         
           else 
			  {
			      numberOfDirectories++;
					search (contents[i]);
			  }

         
         }
      }
   
   
   
   
   }
