
/* Simulation of console-I/O program ListDemo,
   using ConsoleApplet as a basis.  See the file
   ConsoleApplet.java for more information.
   
   David Eck
   eck@hws.edu
   
*/

public class ListDemoConsole extends ConsoleApplet {


   protected String getTitle() {
      return "Sample program \"ListDemo\"";
   }


   protected void program() {

      StringList list;    // The list of strings.
      
      String usersItem;   // An item typed in by the user.
      
      String[] elements;  // The elements in the list, obtained by
                          //    calling list.getElements().  This
                          //    is used to display the list contents
                          //    to the user.
      
      boolean done;       // This will be set to true then the user
                          //   wants to exit from the program.
      
      done = false;
      
      list = new StringList();  // Start with an empty list.
      
      while (done == false) {
      
         // Get and display the elements that are currently in the list.
      
         elements = list.getElements();
         if (elements.length == 0)
            console.putln("\n\nThere are no elements in the list.");
         else {
            console.putln("\n\nElements of the list:");
            for (int i = 0; i < elements.length; i++)
               console.putln("   " + elements[i]);
         }
      
         // Display a menu of available operations, and get the
         // user's choice.
      
         console.putln("\n\nChoose an operation on the list:");
         console.putln("    1.  Add an item.");
         console.putln("    2.  Delete an item.");
         console.putln("    3.  Find an item.");
         console.putln("    4.  Exit from this program.");
         console.put("Enter the number of your choice:  ");
         int menuChoice = console.getlnInt();
         
         // Carry out the operation selected by the user.  For
         // items 1 to 3, get a string from the user and call
         // the appropriate method from the list.
         
         switch (menuChoice) {
            case 1:  // Insert an item.
               console.put("\nEnter the item to be added:  ");
               usersItem = console.getln().trim().toLowerCase();
               list.insert(usersItem);
               console.putln("OK");
               break;
            case 2:  // Delete an item.
               console.put("\nEnter the item to be deleted:  ");
               usersItem = console.getln().trim().toLowerCase();
               if ( list.delete(usersItem) )
                  console.putln("OK");
               else
                  console.putln("That item was not found in the list.");
               break;
            case 3:  // Check whether an item occurs in the list.
               console.put("\nEnter an item to find:  ");
               usersItem = console.getln().trim().toLowerCase();
               if ( list.find(usersItem) )
                  console.putln("Yes, that item is in the list.");
               else
                  console.putln("No, that item is not in the list.");
               break;
            case 4:  // Exit from this program.
               done = true;
               break;
            default:
               console.putln("Illegal choice.");
               break;
         } // end switch
         
      
      } // end while
      
      console.putln("\n\nExiting program.");

   } // end program()


}  // end class ListDemoConsole
