/***************************************************************** / In recitation today, we developed a class hierarchy for a / / file system. I've done a little bit of prettying it up and / / have created a compilable version, below. Most of the method / / method code is left out, but in a few places where I thought / / it helped to illustrate something, I put in a couple of lines. / / -- Alan Frank, 4 January 2001 / *****************************************************************/ import java.io.*; // for System.out.println import java.util.*; // for Date /** A file system; may or may not be hierarchical */ public class FileSystem { protected int capacity; protected int fileNameMaxLength; protected boolean readOnly; protected Device location; protected Directory rootDirectory; /* constructor & accessor methods are omitted from the listings of this and other classes. */ public void DeleteFile (File goner) {} File FindFile (String fileName) { return rootDirectory.FindFile (fileName); } public static File FindFileAnywhere (String fileName) { // loops through all FileSystems to find file return null; // so we can compile this } } // end class FileSystem /** A hierarchical file system; that means that directories can have subdirectories, ad infinitum */ public class HierarchicalFileSystem extends FileSystem { protected Directory currentDirectory; public void changeDirectory (String directoryName) { currentDirectory = (Directory) FindFile (directoryName); } public void changeDirectoryRelative (String subdirectoryName) {} public void changeDirectory (Directory destinationDirectory) { currentDirectory = destinationDirectory; } /* void AddFile (File newFile, Directory parentDirectory) This isn't implemented; instead, use parentDirectory.AddFile(newFile); CreateDirectory (String name) isn't implemented either; use parentDirectory.AddFile (new Directory (name, fileSystem)); */ } // end class HierarchicalFileSystem /** A file, but we can't do anything with it */ public abstract class File { protected String name; protected Date updatedDate; protected int size; private BunchOfBlocks blocks []; abstract public void Edit (); abstract public void Display (); static void Edit (String fileName) { FileSystem.FindFileAnywhere (fileName).Edit(); } /** This inner class allows us to keep together all the information about the blocks that make up this file. */ private class BunchOfBlocks { private int startingBlock; private int numberOfBlocks; BunchOfBlocks (int position, int sb, int nob) { startingBlock = sb; numberOfBlocks = nob; blocks [position] = this; } } } public interface Executable { public void Execute (); } public class Directory extends File { File fileList []; FileSystem fileSystem; public void Edit () { System.out.println ("I don't know how to edit a directory."); } public void Display () { // Run ("ls " + name); } public File FindFile (String name) { return null; } } // end class Directory public class TextFile extends File { public void Edit () { // Run ("emacs " + name); } public void Display () { // Run ("cat " + name); } } public class HTML extends TextFile implements Executable { public void Execute () { // put code here to call browser } } public class ShellScript extends TextFile implements Executable { public void Execute () { // Code here to run a shell script } } public class BinaryFile extends File { public void Edit () { // Put some code here to edit a binary file } public void Display () { // Put some code here to display a binary file } } public class JavaClassFile extends BinaryFile implements Executable { public HTML FindJavadoc () { // left as an exercise return null; // if not found } public void Display () { HTML javadoc; javadoc = FindJavadoc(); if (javadoc == null) super.Display (); else javadoc.Execute(); } public void Execute () { // Run ("java " + name); } } // end class JavaClassFile public class Device {} // dummy