package tutor; import java.io.*; import java.util.*; /** * This class represents a type of command which can be entered by * the user from a console for the {@linkplain tutor.PracticeLoop} * program. These types need to be instantiated and available from * the {@linkplain tutor.CommandTypePool} class. * */ public class CommandType extends Object { //-------------------------------------------- /** a short form of the command (eg "h" for help or "q" for quit) */ private String key; //-------------------------------------------- /** the full name of the command */ private String name; //-------------------------------------------- /** a short help message for the command */ private String helpMessage; //-------------------------------------------- /** a short help message for the command */ private String longHelpMessage; //-------------------------------------------- /** alternate ways to invoke the command */ private String[] commandStrings; //-------------------------------------------- public CommandType() { this.key = new String("?"); this.name = new String("undefined"); this.helpMessage = new String(""); this.longHelpMessage = new String(""); } //-------------------------------------------- public CommandType( String sName, String sKey, String sHelpMessage, String[] aaCommandStrings) { this.name = sName.trim(); this.key = sKey.trim(); this.helpMessage = sHelpMessage.trim(); this.commandStrings = aaCommandStrings; } //-------------------------------------------- public CommandType( String sName, String sKey, String sHelpMessage) { this.name = sName.trim(); this.key = sKey.trim(); this.helpMessage = sHelpMessage.trim(); } //-------------------------------------------- public String getKey() { return this.key; } //-------------------------------------------- public String getName() { return this.name; } //-------------------------------------------- /** return the short help message */ public String getHelpMessage() { return this.helpMessage; } //-------------------------------------------- /** return the long help message for a command */ public String getLongHelpMessage() { return this.longHelpMessage; } //-------------------------------------------- /** sets the long help message for a command */ public void setLongHelpMessage(String sLongHelpMessage) { this.longHelpMessage = sLongHelpMessage; } //-------------------------------------------- /** returns a "summary" of how to use the command */ public String printHelpMessage() { StringBuffer sReturn = new StringBuffer(); sReturn.append(" " + this.key + " - " + this.helpMessage + " ("); for (int ii = 0; ii < this.commandStrings.length; ii++) { if (ii != 0) { sReturn.append(", "); } sReturn.append(this.commandStrings[ii]); } sReturn.append(") \n"); return sReturn.toString(); } //-------------------------------------------- public boolean isLegalCommand(String sCommand) { if (this.key.equals(sCommand)) { return true; } for (int ii = 0; ii < this.commandStrings.length; ii++) { if (this.commandStrings[ii].equals(sCommand)) { return true; } } return false; } //-- m: isLegalCommand //-------------------------------------------- public String toString() { StringBuffer sReturn = new StringBuffer(); sReturn.append("Command: " + this.name + "\n"); sReturn.append(this.key + " " + this.helpMessage + " ("); for (int ii = 0; ii < this.commandStrings.length; ii++) { sReturn.append(this.commandStrings[ii] + "|"); } sReturn.append(") \n"); return sReturn.toString(); } //-- m: toString //-------------------------------------------- //-------------------------------------------- //-------------------------------------------- public static void main(String[] args) throws Exception { StringBuffer sbUserMessage = new StringBuffer(""); sbUserMessage.append("usage: java CommandType name key helpmessage \n"); if (args.length < 3) { System.out.println(sbUserMessage); System.exit(-1); } String sName = args[0]; String sKey = args[1]; String sHelpMessage = args[2]; String[] aaCommandStrings = {"?", "?"}; CommandType ccTest = new CommandType(sName, sKey, sHelpMessage, aaCommandStrings); System.out.println("method: toString()"); System.out.println(ccTest.toString()); System.out.println("method: printHelpMessage()"); System.out.println(ccTest.printHelpMessage()); } //-- main() } //-- CommandType class