import java.io.* ; import java.util.* ; public class FilesInfo { // these variables are static to make it easier to work // with them in the static methods of this class static PrintWriter outPW = null ; static Vector listOfFiles = new Vector() ; // command line arguments are outfilename and any number // of pathnames (call the list of pathnames P) // first builds a list of FileInfoObj objects, one for // each pathname in P that exists and can be read // then accepts a sequence of commands from standard input // and processes as described in homework writeup public static void main(String[] args) { // usage: FilesInfo outfilename file1 file2 .... if (args.length < 2) { System.out.println("At least two " + "arguments required") ; System.exit(-1) ; } // PUT SOMETHING HERE: // replace this assignment with one that makes // outPW write onto outputfilename outPW = new PrintWriter( new OutputStreamWriter(System.out)) ; for (int i = 1 ; i < args.length ; i++) { FileInfoObj tempObj = buildObj(args[i]) ; if (tempObj == null) { System.out.println(args[i] + " not found or not readable") ; } else { System.out.println("Adding " + args[i] + " to list of files") ; listOfFiles.addElement(tempObj) ; } } longPrompt() ; boolean more = true ; while (more) { more = processCmd() ; } outPW.close() ; } // if pathName exists and can be read, returns a new // FileInfoObj for it; otherwise returns null static FileInfoObj buildObj(String pathName) { FileInfoObj o = null ; // PUT SOMETHING HERE: // assign appropriate value to o // *HERE* is where you should decide what type // of file you have and call an appropriate // constructor (for a subclass of FileInfoObj) return o ; } // prompts for and processes one command // returns false if no more input (command was 'Q'), // true otherwise static boolean processCmd() { System.out.println("Enter option: " + "(S, L, X, C, or Q)") ; String cmd = Input.readString() ; if (cmd.length() != 1) { System.out.println("Invalid option") ; longPrompt() ; return true ; } char cmdChar = cmd.charAt(0) ; switch (cmdChar) { case 'S' : System.out.println("printing sizes") ; outPW.println("**Sizes**") ; applyCmdToAll('S') ; outPW.println("**done**") ; outPW.println() ; return true ; case 'L' : System.out.println("printing line counts") ; outPW.println("**Line counts**") ; applyCmdToAll('L') ; outPW.println("**done**") ; outPW.println() ; return true ; case 'X' : System.out.println("printing line counts " + "excluding whitespace, comments") ; outPW.println("**Line counts " + "excluding whitespace, comments**") ; applyCmdToAll('X') ; outPW.println("**done**") ; outPW.println() ; return true ; case 'C' : System.out.println("printing contents") ; outPW.println("**Contents**") ; applyCmdToAll('C') ; outPW.println("**done**") ; outPW.println() ; return true ; case 'Q' : outPW.println("**End of user input**") ; return false ; default : System.out.println("Invalid option") ; longPrompt() ; return true ; } } // applies one of the commands (S, L, X, C) to every file static void applyCmdToAll(char cmdChar) { Enumeration e = listOfFiles.elements() ; while (e.hasMoreElements()) { FileInfoObj o = (FileInfoObj) e.nextElement() ; switch (cmdChar) { case 'S' : o.prSize(outPW) ; break ; case 'L' : o.prLineCount(outPW) ; break ; case 'X' : o.prLineCountXWSC(outPW) ; break ; case 'C' : o.prContents(outPW) ; break ; } } } // writes long form of prompt to standard output static void longPrompt() { System.out.println("Options are:") ; System.out.println(" S to get file sizes") ; System.out.println(" L to count lines in files") ; System.out.println(" X to count lines in files " + "excluding whitespace, comments") ; System.out.println(" C to get file contents") ; System.out.println(" Q to end") ; } } // PUT SOMETHING HERE: // add to or modify this code to get the desired results from // the main program // you will probably want to fill in the blanks of this class // and then define some subclasses class FileInfoObj { // PUT SOMETHING HERE: // add instance variables as needed // PUT SOMETHING HERE: // define appropriate constructor(s) void prSize(PrintWriter pw) { // PUT SOMETHING HERE: // write to pw: filename and size (in bytes) // for this object } void prLineCount(PrintWriter pw) { // PUT SOMETHING HERE: // write to pw: filename and line count (if possible) // for this object // see homework writeup for details } void prLineCountXWSC(PrintWriter pw) { // PUT SOMETHING HERE: // write to pw: filename and line count excluding // whitespace and comments (if possible) for this // object // see homework writeup for details } void prContents(PrintWriter pw) { // PUT SOMETHING HERE: // write to pw: file contents (if possible) for this // object // see homework writeup for details } } // PUT SOMETHING HERE?: // define additional classes as needed