/*   
   Project 10-2   
   
   A help program that uses a disk file 
   to store help information. 
*/   
 
import java.io.*; 
 
/* The Help class opens a help file, 
   searches for an topic, and then displays 
   the information associated with that topic. 
   Notice that it handles all I/O exceptions 
   itself, avoiding the need for calling  
   code to do so. */  
class Help { 
  String helpfile; // name of help file 
 
  Help(String fname) { 
    helpfile = fname; 
  } 
 
  // Display help on a topic. 
  boolean helpon(String what) {  
    FileReader fr; 
    BufferedReader helpRdr; 
    int ch; 
    String topic, info; 
 
    try { 
      fr = new FileReader(helpfile); 
      helpRdr = new BufferedReader(fr); 
    } 
    catch(FileNotFoundException exc) { 
      System.out.println("Help file not found."); 
      return false; 
    } 
    catch(IOException exc) { 
      System.out.println("Cannot open file."); 
      return false; 
    } 
 
    try { 
      do { 
        // read characters until a # is found 
        ch = helpRdr.read(); 
 
        // now, see if topics match 
        if(ch == '#') { 
          topic = helpRdr.readLine(); 
          if(what.compareTo(topic) == 0) { // found topic 
            do { 
              info = helpRdr.readLine(); 
              if(info != null) System.out.println(info); 
            } while((info != null) && 
                    (info.compareTo("") != 0)); 
            return true; 
          } 
        } 
      } while(ch != -1); 
    } 
    catch(IOException exc) { 
      System.out.println("File error."); 
      try { 
        helpRdr.close(); 
      }  
      catch(IOException exc2) { 
        System.out.println("Error closing file."); 
      } 
      return false; 
    } 
    try { 
      helpRdr.close(); 
    }  
    catch(IOException exc) { 
      System.out.println("Error closing file."); 
    } 
    return false; // topic not found 
  }  
  
  // Get a Help topic. 
  String getSelection() {  
    String topic = ""; 
 
    BufferedReader br = new BufferedReader( 
              new InputStreamReader(System.in)); 
 
    System.out.print("Enter topic: ");   
    try {   
      topic = br.readLine(); 
    } 
    catch(IOException exc) { 
      System.out.println("Error reading console."); 
    } 
    return topic; 
  } 
}  
  
// Demonstrate the file-based Help system. 
class FileHelp {   
  public static void main(String args[]) {   
    Help hlpobj = new Help("helpfile.txt");  
    String topic; 
 
    System.out.println("Try the help system. " + 
                       "Enter 'stop' to end.");  
    do {  
      topic = hlpobj.getSelection();   
 
      if(!hlpobj.helpon(topic)) 
        System.out.println("Topic not found.\n"); 
 
    } while(topic.compareTo("stop") != 0); 
  }  
}


