creating menus in java mimi opkins cecs 174. menus menus work well for console applications. the...

16
CREATING MENUS IN JAVA Mimi Opkins CECS 174

Upload: allen-benson

Post on 20-Jan-2016

236 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 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

CREATING MENUS IN JAVAMimi Opkins

CECS 174

Page 2: 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

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.

04/21/23

2

Page 3: 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

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.

04/21/23

3

Page 4: 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

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.

04/21/23

4

Page 5: 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

04/21/23 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

Page 6: 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

04/21/23 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

Page 7: 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

import java.util.Scanner;public class StudentListTest{

public static void main(String args[]){

// Declaration and initialization of variablesstudent s;list l = new list();

//Display the program’s purposeSystem.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.");

04/21/23

7

Page 8: 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 {// Display the menu and get the menu optionint 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();

04/21/23

8

Page 9: 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

04/21/23 9

switch (menuOption) {case 1: // Insert a student into the list

// Prompt the user for the student's informations = create_student();// Insert the student into the listl.insert(s);break;

case 2: // Remove the student from the list// Prompt the user for the student's // identification numbers = get_id_number();// Remove the student from the listl.remove(s);break;

case 3: // Locate a student in the list// Prompt the user for the student's// identification numbers = get_id_number();// Retrieve the student from the listl.retrieve(s);// Display the student informationSystem.out.printf("%s", s);break;

Page 10: 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

04/21/23 10

case 4: // Clear the listl.clearList();break;

case 5: // Display all students in the listSystem.out.printf("%s", l);break;

case 6: // Prepare to exit the programSystem.out.println("Program terminating");break;

default: // Invalid menu optionSystem.out.println("Invalid menu option. Please re-enter.");break;

} // end switch statement} while (menuOption != 6); // end do-while loop

} // end main()} // end class StudentListTest

Page 11: 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

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.

04/21/23

11

Page 12: 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

04/21/23 12switch (menuOption) {

case 1: // Insert a student into the listSystem.out.println(“Menu Option 1 Chosen”);break;

case 2: // Remove the student from the listSystem.out.println(“Menu Option 2 Chosen”);break;

case 3: // Locate a student in the listSystem.out.println(“Menu Option 3 Chosen”);break;

case 4: // Clear the listSystem.out.println(“Menu Option 4 Chosen”);break;

case 5: // Display all students in the listSystem.out.println(“Menu Option 5 Chosen”);break;

case 6: // Prepare to exit the programSystem.out.println("Program terminating“);break;

default: // Invalid menu optionSystem.out.println("Invalid menu option. Please

re-enter.“);break;

} // end switch statement} while (menuOption != 6); // end do-while loop

} // end main()

Page 13: 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

04/21/23 13

Sample output:

This program will allow the user to create and inserta student into a list, retrieve a student from the listfor 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 Options1. 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

Page 14: 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

04/21/23 14

Student List Menu Options1. 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: 8Invalid menu option. Please re-enter.

Student List Menu Options1. 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: -2Invalid integer - please re-enter: 6

Program terminating

Page 15: 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

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.

04/21/23

15

Page 16: 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

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.

04/21/23

16