160 likes | 212 Views
Creating menus in Java. Mimi Opkins CECS 174. Menus. Menus work well for console applications. The menu can be contained within a do-while loop and the do-while loop should be contained within main(). The menu itself is coded as a switch-case statement. Menu-Driven Application.
E N D
Creating menus in Java Mimi Opkins CECS 174
Menus • Menus work well for console applications. • The menu can be contained within a do-while loop and the do-while loop should be contained within main(). • The menu itself is coded as a switch-case statement.
Menu-Driven Application Suppose you want to design an application that will create and maintain a list of students. The student information consists of the student's identification number, first and last name. The menu selections presented to the user are: • Enter a student into the list. • Delete a student from the list. • Locate and display a student. • Clear the list of all students. • Display all students in id. number order. • Exit the program.
Flowcharting Menu Application • Prior to writing any code, create a flowchart of the top-level flow of control for your program. • Once the top-level flowchart is completed, top-down design can be used to break down each task into subtasks. • Lower level flowcharts can be created for each sub-level design.
Declare and initialize variables for the menu option, student and list Display the purpose of the program Display the menu and prompt the user for a menu option menu option = 1 Prompt for student info. Insert student into list menu option = 2 Prompt for id. number Remove student from list
menu option = 3 Prompt for id. number Retrieve student from list menu option = 4 Display student Clear list menu option = 5 Display list menu option = 6 Display program terminating Exit program Invalid menu option Display invalid option message
import java.util.Scanner; public class StudentListTest { public static void main(String args[]) { // Declaration and initialization of variables student s; list l = new list(); //Display the program’s purpose System.out.println("This program will allow the user to create and insert"); System.out.println( "a student into a list, retrieve a student from the list"); System.out.println("for display and delete a student from the list. The"); System.out.println("user can also clear the list and display all students"); System.out.println("in the list.");
do { // Display the menu and get the menu option int menuOption=0; System.out.println("\n Student List Menu Options"); System.out.println("1. Enter a student into the list."); System.out.println("2. Delete a student from the list."); System.out.println("3. Locate and display a student."); System.out.println("4. Clear the list of all students."); System.out.println("5. Display all students in id. number order."); System.out.println("6. Exit the program."); System.out.println(“What do you want to do?”); menuOption = keyboard.nextInt(); keyboard.nextLine();
switch (menuOption) { case 1: // Insert a student into the list // Prompt the user for the student's information s = create_student(); // Insert the student into the list l.insert(s); break; case 2: // Remove the student from the list // Prompt the user for the student's // identification number s = get_id_number(); // Remove the student from the list l.remove(s); break; case 3: // Locate a student in the list // Prompt the user for the student's // identification number s = get_id_number(); // Retrieve the student from the list l.retrieve(s); // Display the student information System.out.printf("%s", s); break;
case 4: // Clear the list l.clearList(); break; case 5: // Display all students in the list System.out.printf("%s", l); break; case 6: // Prepare to exit the program System.out.println("Program terminating"); break; default: // Invalid menu option System.out.println("Invalid menu option. Please re-enter."); break; } // end switch statement } while (menuOption != 6); // end do-while loop } // end main() } // end class StudentListTest
Coding Steps • When writing your code, code one case statement at a time and comment or stub the remaining statements until you are ready to code them. • If you are writing code to create and maintain a list of students, code the basic switch-case statement and stub all methods except the methods to display the purpose of the program, display the menu options and prompt the user for a menu choice.
switch (menuOption) { case 1: // Insert a student into the list System.out.println(“Menu Option 1 Chosen”); break; case 2: // Remove the student from the list System.out.println(“Menu Option 2 Chosen”); break; case 3: // Locate a student in the list System.out.println(“Menu Option 3 Chosen”); break; case 4: // Clear the list System.out.println(“Menu Option 4 Chosen”); break; case 5: // Display all students in the list System.out.println(“Menu Option 5Chosen”); break; case 6: // Prepare to exit the program System.out.println("Program terminating“); break; default: // Invalid menu option System.out.println("Invalid menu option. Please re-enter.“); break; } // end switch statement } while (menuOption != 6); // end do-while loop } // end main()
Sample output: This program will allow the user to create and insert a student into a list, retrieve a student from the list for display and delete a student from the list. The user can also clear the list and display all students in the list. Student List Menu Options 1. Enter a student into the list. 2. Delete a student from the list. 3. Locate and display a student. 4. Clear the list of all students. 5. Display all students in id. number order. 6. Exit the program. Enter the menu option: 3 Menu option 3 chosen
Student List Menu Options 1. Enter a student into the list. 2. Delete a student from the list. 3. Locate and display a student. 4. Clear the list of all students. 5. Display all students in id. number order. 6. Exit the program. Enter the menu option: 8 Invalid menu option. Please re-enter. Student List Menu Options 1. Enter a student into the list. 2. Delete a student from the list. 3. Locate and display a student. 4. Clear the list of all students. 5. Display all students in id. number order. 6. Exit the program. Enter the menu option: -2 Invalid integer - please re-enter: 6 Program terminating
Stub Testing • By commenting out or stubbing statements, you can thoroughly test each case statement and method call. • When a case statement is thoroughly tested, you can proceed to coding the next case statement.
Coding/Testing Steps • For example, once the do-while loop is thoroughly tested using valid and invalid menu options, the next step might be to write the code for the first case statement. • Code the method or methods to prompt the user for information about the student, then insert the student into the list. • No other code would be written until the first case statement is thoroughly tested. • Once case statement 1 is completely tested, the code should be saved (in case you need to back up to it at a later date) and the code for another case statement should be written and tested.