10 chapter 101 using menus and validating input programming logic and design, second edition,...

31
Chapter 10 1 10 Using Menus and Validating Input Programming Logic and Design, Second Edition, Comprehensive 10

Upload: rudolph-hodges

Post on 18-Jan-2018

217 views

Category:

Documents


0 download

DESCRIPTION

10 Chapter 103 Objectives After studying Chapter 10, you should be able to: Use a case structure to manage a menu Create a program that uses a multilevel menu Validate input Understand types of data validation

TRANSCRIPT

Page 1: 10 Chapter 101 Using Menus and Validating Input Programming Logic and Design, Second Edition, Comprehensive 10

Chapter 10 1

10

Using Menus and Validating Input

Programming Logic and Design, Second Edition, Comprehensive

10

Page 2: 10 Chapter 101 Using Menus and Validating Input Programming Logic and Design, Second Edition, Comprehensive 10

Chapter 10 2

10

Objectives

After studying Chapter 10, you should be able to:

• Understand the need for interactive, menu-driven programs

• Create a program that uses a single-level menu

• Code modules as black boxes

• Improve menu programs

Page 3: 10 Chapter 101 Using Menus and Validating Input Programming Logic and Design, Second Edition, Comprehensive 10

Chapter 10 3

10

Objectives

After studying Chapter 10, you should be able to:

• Use a case structure to manage a menu

• Create a program that uses a multilevel menu

• Validate input

• Understand types of data validation

Page 4: 10 Chapter 101 Using Menus and Validating Input Programming Logic and Design, Second Edition, Comprehensive 10

Chapter 10 4

10

Using Interactive Programs

• Programs for which all the data are gathered prior to running use batch processing

• Programs that depend on user input while they are running use interactive processing

• Interactive computer programs are often called real-time applications, because they run while a transaction is taking place, not at some later time

• You can refer to interactive processing as online processing, because the user’s data or requests are gathered during the execution of the program

Page 5: 10 Chapter 101 Using Menus and Validating Input Programming Logic and Design, Second Edition, Comprehensive 10

Chapter 10 5

10

Using Interactive Programs• A batch processing system can be off-line; that is, you can collect

data such as time cards or purchase information well ahead of the actual computer processing of the paychecks or bills

• A menu program is a common type of interactive program in which the user sees a number of options on the screen and can select any one of them

Page 6: 10 Chapter 101 Using Menus and Validating Input Programming Logic and Design, Second Edition, Comprehensive 10

Chapter 10 6

10

Using a Single-Level Menu

The program in Figure 10-1 uses a single-level menu; that is, the user makes a selection from only one menu before using the program for its ultimate purpose—arithmetic practice

Page 7: 10 Chapter 101 Using Menus and Validating Input Programming Logic and Design, Second Edition, Comprehensive 10

Chapter 10 7

10Mainline Logic for

Arithmetic Drill Menu Program

Page 8: 10 Chapter 101 Using Menus and Validating Input Programming Logic and Design, Second Edition, Comprehensive 10

Chapter 10 8

10

Using a Single-Level Menu

• The startUp() module in the arithmetic drill program defines variables and opens files

Page 9: 10 Chapter 101 Using Menus and Validating Input Programming Logic and Design, Second Edition, Comprehensive 10

Chapter 10 9

10

Using a Single-Level Menu

• When the looping() module ends, control passes to the main program

Page 10: 10 Chapter 101 Using Menus and Validating Input Programming Logic and Design, Second Edition, Comprehensive 10

Chapter 10 10

10

Coding Modules as Black Boxes

• Programmers often refer to modules such as addition() and subtraction() as existing within a black box meaning that the module statements are “invisible” to the rest of the program

• When programmers develop systems containing many modules, they often code “empty” black box procedures called stubs

• Most programming languages provide you with built-in modules or functions, which are subroutines that automatically provide a mathematical value such as a square root, absolute value, or random number

Page 11: 10 Chapter 101 Using Menus and Validating Input Programming Logic and Design, Second Edition, Comprehensive 10

Chapter 10 11

10

Coding Modules as Black Boxes

• You can make many additional improvements to any of the addition() modules shown in Figures 10-7, 10-9, and 10-10

Page 12: 10 Chapter 101 Using Menus and Validating Input Programming Logic and Design, Second Edition, Comprehensive 10

Chapter 10 12

10Making Improvements

to a Menu Program• The

programmer can assist the user by displaying a message when the selected response is not one of the allowable menu options, as shown in Figure 10-11

Page 13: 10 Chapter 101 Using Menus and Validating Input Programming Logic and Design, Second Edition, Comprehensive 10

Chapter 10 13

10Using the Case

Structure to Manage a Menu

• The arithmetic drill program contains just three valid user options: numeric entries that represent Addition, Subtraction, or Quit

• All menu-driven programs should be user-friendly, meaning that they should make it easy for the user to make desired choices

• Programmers often overlook the fact that computers recognize uppercase letters as being different from their lowercase counterparts

Page 14: 10 Chapter 101 Using Menus and Validating Input Programming Logic and Design, Second Edition, Comprehensive 10

Chapter 10 14

10Menu Program

Using the Case Structure

Page 15: 10 Chapter 101 Using Menus and Validating Input Programming Logic and Design, Second Edition, Comprehensive 10

Chapter 10 15

10Menu Program Using

the Case Structurewith Multiple Allowed Responses

Page 16: 10 Chapter 101 Using Menus and Validating Input Programming Logic and Design, Second Edition, Comprehensive 10

Chapter 10 16

10

Using Multilevel Menus

• When you need to present the user with a large number of options, you invite several potential problems:– Not all the options will appear on the screen, and the

user might not realize that additional options are available

– The screen is too crowded to be visually pleasing when you try to force all the options to fit on the screen

– Users become confused and frustrated when you present them with too many choices

Page 17: 10 Chapter 101 Using Menus and Validating Input Programming Logic and Design, Second Edition, Comprehensive 10

Chapter 10 17

10

Using Multilevel Menus

• When you have many menu options to present, using a multilevel menu might be more effective than using a single-level menu

• With a multilevel menu, the selection of a menu option leads to another menu where the user can make further, more refined selections

• You refer to a menu that controls whether or not the program will continue as the main menu of a program

• Alternatively, the user can choose to continue the program, selecting an Addition, Subtraction, Multiplication, or Division arithmetic drill

Page 18: 10 Chapter 101 Using Menus and Validating Input Programming Logic and Design, Second Edition, Comprehensive 10

Chapter 10 18

10

Using Multilevel Menus

• No matter which drill the user chooses you can display a second menu like the one shown in Figure 10-18

• A second-level menu is a submenu

Page 19: 10 Chapter 101 Using Menus and Validating Input Programming Logic and Design, Second Edition, Comprehensive 10

Chapter 10 19

10Third Menu for

Arithmetic Drill Program

Page 20: 10 Chapter 101 Using Menus and Validating Input Programming Logic and Design, Second Edition, Comprehensive 10

Chapter 10 20

10

Using Multilevel Menus

• You would not need to learn any new techniques to create as many levels of menus as the application warrants

• The module that controls each new level can:1. Display a menu2. Accept a response3. While the user does not select the quit option for the

specific menu level, perform another module based on the selection (or inform the user of an error)

4. Display the menu and accept a response again

Page 21: 10 Chapter 101 Using Menus and Validating Input Programming Logic and Design, Second Edition, Comprehensive 10

Chapter 10 21

10

Validating Input

• The programs you write will be improved if you employ defensive programming, which means trying to prepare for all possible errors before they occur

• You can circumvent potential problems caused by a user’s invalid data entries by validating the user’s input

• Validating input involves checking the user’s responses to ensure they fall within acceptable bounds

Page 22: 10 Chapter 101 Using Menus and Validating Input Programming Logic and Design, Second Edition, Comprehensive 10

Chapter 10 22

10

Validating Input

• Alternatively, you can force the invalid data to a default value

• Forcing a field to a value means you override incorrect data by setting the field to a specific value

• New programmers often make the following two kinds of mistakes when validating data:– They use incorrect logic to check for valid responses

when there is more than one possible correct entry– They fail to account for the user making multiple invalid

entries

Page 23: 10 Chapter 101 Using Menus and Validating Input Programming Logic and Design, Second Edition, Comprehensive 10

Chapter 10 23

10

Validating Input

Page 24: 10 Chapter 101 Using Menus and Validating Input Programming Logic and Design, Second Edition, Comprehensive 10

Chapter 10 24

10Best Method for

Validating User Response

Page 25: 10 Chapter 101 Using Menus and Validating Input Programming Logic and Design, Second Edition, Comprehensive 10

Chapter 10 25

10Understanding

Types of Data Validation

• Some of the techniques you want to master include validating:

– Data type

– Range

– Reasonableness and consistency of data

– Presence of data

Page 26: 10 Chapter 101 Using Menus and Validating Input Programming Logic and Design, Second Edition, Comprehensive 10

Chapter 10 26

10

Validating a Data Type

• Some programming languages allow you to check data to make sure they are the correct type

Page 27: 10 Chapter 101 Using Menus and Validating Input Programming Logic and Design, Second Edition, Comprehensive 10

Chapter 10 27

10

Validating a Data Range

• Sometimes a user response or other data must fall within a range of values

Page 28: 10 Chapter 101 Using Menus and Validating Input Programming Logic and Design, Second Edition, Comprehensive 10

Chapter 10 28

10Validating Reasonableness

and Consistency of Data

• Data items can be the correct type and within range, but still be incorrect

• You have experienced this phenomenon yourself if anyone has ever misspelled your name or over billed you

• There are many data items that you cannot check for reasonableness; it is just as reasonable that your name is Catherine as it is that your name is Katherine or Kathryn

Page 29: 10 Chapter 101 Using Menus and Validating Input Programming Logic and Design, Second Edition, Comprehensive 10

Chapter 10 29

10

Validating Presence of Data

• Sometimes data are missing from a file, either on purpose or by accident

• A job applicant might fail to submit an entry for the salaryAtPreviousJob field or a client has no entry for the emailAddress field

• Good defensive programs try to foresee all possible inconsistencies and errors

Page 30: 10 Chapter 101 Using Menus and Validating Input Programming Logic and Design, Second Edition, Comprehensive 10

Chapter 10 30

10

Summary

• Programs for which all the data are gathered prior to running use batch processing

• When you create a single-level menu, the user makes a selection from only one menu before using the program for its ultimate purpose

• When you code a module as a black box, the module statements are invisible to the rest of the program

• A programmer can improve a menu program and assist the user by displaying a message when the selected response is not one of the allowable menu options

Page 31: 10 Chapter 101 Using Menus and Validating Input Programming Logic and Design, Second Edition, Comprehensive 10

Chapter 10 31

10

Summary

• You can use the case structure to make decisions when you need to test a single variable against several possible values

• When a program requires more options than can easily fit in one menu, you can use a multilevel menu

• You can circumvent potential problems caused by a user’s invalid data entries by validating the user’s input

• Some of the techniques you want to master include: validating data type, range, reasonableness and consistency of data, and presence of data