Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 CREATING MENUS IN JAVA Mimi Opkins CECS 174

2 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. 12/12/2015 2

3 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: 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. 12/12/2015 3

4 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. 12/12/2015 4

5 5 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 menu option = 2 Prompt for student info. Insert student into list Prompt for id. number Remove student from list

6 12/12/2015 6 menu option = 3 menu option = 4 menu option = 5 menu option = 6 Invalid menu option Prompt for id. number Retrieve student from list Clear list Display list Display program terminating Exit program Display invalid option message Display student

7 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."); 12/12/2015 7

8 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(); 12/12/2015 8

9 9 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;

10 12/12/2015 10 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

11 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. 12/12/2015 11

12 12/12/2015 12 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 5 Chosen”); 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()

13 12/12/2015 13 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

14 12/12/2015 14 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

15 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. 12/12/2015 15

16 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. 12/12/2015 16


Download ppt "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."

Similar presentations


Ads by Google