package JavaLinkedListDemo;import java.util.* ; // for LinkedListpublic class JavaLinkedListDemo { /** the basic data structure for this application */ private LinkedList<Professor> profs ; /** default constructor */ public JavaLinkedListDemo() { profs = new LinkedList<Professor>() ; // instantiate data structure while ( showMenu() ) // keep calling menu until it returns false { } // empty bracakets on separate line to make them more noticeable } /** application entry point * @param args command line parameters */ public static void main( String[] args ) { JavaLinkedListDemo theApp = new JavaLinkedListDemo() ; } /** show menu, get user response, and execute appropriate function or return 0 * @return <code>true</code> if program should continue, <code>false</code> if user * wishes to quit */ boolean showMenu() { System.out.println( "\n=========" ) ; // menu title System.out.println( "MAIN MENU" ) ; System.out.println( "=========" ) ; System.out.println( " A [A]dd an element to the list" ) ; System.out.println( " N show [N]umber of elements in list" ) ; System.out.println( " P [P]rint all elements in list" ) ; System.out.println( " X e[X]it program" ) ; // required variables String strMenuEntry ; // entire line entered by user char menuChoice ; // first character of line entered by user // get user response while ( true ) { System.out.print( "\nYour choice: " ) ; // prompt for user response strMenuEntry = jmhUtilities.ReadLine() ; // get user response strMenuEntry = strMenuEntry.toUpperCase() ; // convert entry to uppercase menuChoice = strMenuEntry.charAt( 0 ) ; // get first character switch ( menuChoice ) { // take appropriate action case 'A' : // add an element to the list return handleAddToList() ; case 'P' : // print all elements in list return handlePrintList() ; case 'N' : // show number of elements in list System.out.println( " The list contains " + profs.size() + " element" + ( profs.size() == 1 ? "" : "s" ) + ".\n" ) ; return true ; case 'X' : // exit program return false ; default : // unrecognized entry System.out.println( " Please enter A, N, P, or X." ) ; } } } /////////////////// //// menu functions /** helper function for method <code>handleAddToList()</code> * @param strPrompt prompt to present to user * @return response entered by user */ String handleAddToListAux( String strPrompt ) // the item desired, such as "last name", to display to user { String str = "" ; // user entry in response to prompt // prompt for and get user entry until it is non-blank while ( str.trim() == "" ) { // prompt for input System.out.print( " Professor's " + strPrompt + " (or \"quit\"): " ) ; str = jmhUtilities.ReadLine() ; // read user entry from keyboard if ( str.trim() == "" ) { // check for blank entry System.out.println( " Please enter the professor's " + strPrompt + " and then press Enter." ) ; } } return str ; // return string entered by user } /** handler for choice A: add a professor to the list * @return <code>true</code> if user enters a valid response, <code>false</code> * otherwise<br/> * N.B. the current version or this method always returns <code>true</code> */ boolean handleAddToList() { Professor prof ; // pointer to new Professor to add to list String strLastName ; // user-entered last name for Professor to add to list String strFirstName ; // user-entered first name for Professor to add to list String strEmailAddress ; // user-entered e-mail address for Professor to add to list String strSpecialty ; // user-entered specialty for Professor to add to list // get last name for new Professor strLastName = handleAddToListAux( "last name" ) ; // get new prof's last name if ( strLastName.toLowerCase() == "quit" ) { // quit if user requests System.out.println() ; return true ; } // get first name for new Professor strFirstName = handleAddToListAux( "first name" ) ; // get new prof's first name if ( strFirstName.toLowerCase() == "quit" ) { // quit if user requests System.out.println() ; return true ; } // get e-mail address for new Professor strEmailAddress = handleAddToListAux( "e-mail address" ) ; // get new prof's e-mail if ( strEmailAddress.toLowerCase() == "quit" ) { // quit if user requests System.out.println() ; return true ; } // get specialty for new Professor strSpecialty = handleAddToListAux( "specialty" ) ; // get new prof's specialty if ( strSpecialty.toLowerCase() == "quit" ) { // quit if user requests System.out.println() ; return true ; } // instantiate new Professor object with user-entered data // note that the new operator returns a pointer prof = new Professor( strLastName, strFirstName, strEmailAddress, strSpecialty ) ; // add new Professor to end of list // note that the push_back method requires an object, so we must dereference the pointer profs.addLast( prof ) ; return true ; // signal success and allow main menu to be presented again } /** handler for choice P: add a professor to the list * @return <code>true</code> if successful, <code>false</code> otherwise<br/> * N.B. the current version or this method always returns <code>true</code> */ boolean handlePrintList() { // handle empty list if ( profs.size() == 0 ) { System.out.println( " The list contains " + profs.size() + " elements.\n" ) ; // handle non-empty list } else { ListIterator<Professor> profIterator ; // iterator for Professor list int k = 1 ; // counter used to number list items for ( profIterator = profs.listIterator() ; // cursor *before* first element profIterator.hasNext() ; // cursor *after* last element k++ ) { // increment counter System.out.println( " " // constant indentation + ( k < 100 ? " " : "" ) // additional indentation to align numbers + ( k < 10 ? " " : "" ) + k + ". " + profIterator.next().toString() ) ; // String representation of next Professor } // in LinkedList } return true ; // signal success and allow main menu to be presented again }}