Download presentation
Presentation is loading. Please wait.
Published byDerek Roberts Modified over 8 years ago
1
Programming Logic and Design Fourth Edition, Comprehensive Chapter 10 Using Menus and Validating Input
2
Programming Logic and Design, Fourth Edition, Comprehensive2 Objectives 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 Use a case structure to manage a menu
3
Programming Logic and Design, Fourth Edition, Comprehensive3 Objectives (continued) Create a program that uses a multilevel menu Validate input Understand types of data validation
4
Programming Logic and Design, Fourth Edition, Comprehensive4 Using Interactive Programs Two kinds of programs, based on how they get data: –Batch processing: data are gathered prior to running –Interactive processing: depends on user input Batch processing typically reads a file of transactions and processes them Offline: transactions gathered prior to the run Real-time applications (or online processing) run while the transaction is taking place
5
Programming Logic and Design, Fourth Edition, Comprehensive5 Using Interactive Programs (continued) Menu program: interactive program that displays a set of options to the user Console application: requires the user to enter a choice using the keyboard Graphical user interface (GUI) application: user selects option using mouse or other pointing device
6
Programming Logic and Design, Fourth Edition, Comprehensive6 Using Interactive Programs (continued) Menus should always have a “quit” choice
7
Programming Logic and Design, Fourth Edition, Comprehensive7 Using a Single-Level Menu Single-level menu: selection from only one menu Mainline logic must check user’s input each time to determine if the program should end Use a variable to hold user’s response to the menu
8
Programming Logic and Design, Fourth Edition, Comprehensive8 Using a Single-Level Menu (continued) Developing the application: mainline logic
9
Programming Logic and Design, Fourth Edition, Comprehensive9 Using a Single-Level Menu (continued) Developing the application: startup () module
10
Programming Logic and Design, Fourth Edition, Comprehensive10 Using a Single-Level Menu (continued) Developing the application: displayMenu() module
11
Programming Logic and Design, Fourth Edition, Comprehensive11 Using a Single-Level Menu (continued) Developing the application: looping() module
12
Programming Logic and Design, Fourth Edition, Comprehensive12 Using a Single-Level Menu (continued) Developing the application: –cleanup() module
13
Programming Logic and Design, Fourth Edition, Comprehensive13 Coding Modules as Black Boxes Contents of program modules should not affect the main structure of the program in any way Black box modules: module statements are encapsulated in a container, making them invisible from the rest of the program Concentrate on the mainline logic first and what the modules will do, not how they will do it Stub: empty black box procedure
14
Programming Logic and Design, Fourth Edition, Comprehensive14 Coding Modules as Black Boxes (continued) Developing the application:
15
Programming Logic and Design, Fourth Edition, Comprehensive15 Coding Modules as Black Boxes (continued) Developing the application: –Improvement: using arrays
16
Programming Logic and Design, Fourth Edition, Comprehensive16 Coding Modules as Black Boxes (continued) Developing the application:
17
Programming Logic and Design, Fourth Edition, Comprehensive17 Coding Modules as Black Boxes (continued) Improvement: using built-in functions
18
Programming Logic and Design, Fourth Edition, Comprehensive18 Making Improvements to a Menu Program Improvement: trapping bad user input
19
Programming Logic and Design, Fourth Edition, Comprehensive19 Making Improvements to a Menu Program (continued) Improvement: tracking user errors
20
Programming Logic and Design, Fourth Edition, Comprehensive20 Making Improvements to a Menu Program (continued) The complete program:
21
Programming Logic and Design, Fourth Edition, Comprehensive21 Making Improvements to a Menu Program (continued) The complete program (continued):
22
Programming Logic and Design, Fourth Edition, Comprehensive22 Using the Case Structure to Manage a Menu Case structure: –Tests a single variable against several possible values –Useful for handling menu choices –Use default option to handle bad user input User-friendly: program should make it easy for the user to make desired choices
23
Programming Logic and Design, Fourth Edition, Comprehensive23 Using the Case Structure to Manage a Menu (continued) Developing the application:
24
Programming Logic and Design, Fourth Edition, Comprehensive24 Using the Case Structure to Manage a Menu (continued) Using a case structure for the input choice:
25
Programming Logic and Design, Fourth Edition, Comprehensive25 Using the Case Structure to Manage a Menu (continued) Handling variations of user input:
26
Programming Logic and Design, Fourth Edition, Comprehensive26 Using Multilevel Menus Multilevel menu: selection of one option leads to another menu with further, refined selections Multilevel menus eliminate problems caused by: –Too many options to fit on the display at one time –Screen is too crowded –User confusion caused by too many choices Submenu: a second-level (or later) menu
27
Programming Logic and Design, Fourth Edition, Comprehensive27 Using Multilevel Menus (continued)
28
Programming Logic and Design, Fourth Edition, Comprehensive28 Using Multilevel Menus (continued) Developing the application: mainline logic
29
Programming Logic and Design, Fourth Edition, Comprehensive29 Using Multilevel Menus (continued) Developing the application: startup() module
30
Programming Logic and Design, Fourth Edition, Comprehensive30 Using Multilevel Menus (continued) Developing the application: displayMenu() module
31
Programming Logic and Design, Fourth Edition, Comprehensive31 Using Multilevel Menus (continued) Developing the application: looping() module
32
Programming Logic and Design, Fourth Edition, Comprehensive32 Using Multilevel Menus (continued) Developing the application: addition() module
33
Programming Logic and Design, Fourth Edition, Comprehensive33 Using Multilevel Menus (continued) Developing the application: –d isplayDifficultyMenu() module
34
Programming Logic and Design, Fourth Edition, Comprehensive34 Using Multilevel Menus (continued) Techniques are the same for all levels of menus: –Display the menu –Accept a response –Perform a module based on the selection –Display the menu again
35
Programming Logic and Design, Fourth Edition, Comprehensive35 Validating Input Defensive programming: trying to prepare for all possible errors before they occur Incorrect user entries are the most common error Validating input: –Check user responses to ensure they are within acceptable bounds –Does not eliminate all program errors Common programming errors: –Incorrect logic to check for valid responses –Failing to account for multiple invalid entries
36
Programming Logic and Design, Fourth Edition, Comprehensive36 Validating Input (continued) Invalid use of OR logic: Improved logic:
37
Programming Logic and Design, Fourth Edition, Comprehensive37 Validating Input (continued) Best logic:
38
Programming Logic and Design, Fourth Edition, Comprehensive38 Understanding Types of Data Validation Need to handle all aspects of validation, including: –Data type –Range –Reasonableness and consistency of data –Presence of data
39
Programming Logic and Design, Fourth Edition, Comprehensive39 Validating a Data Type Some languages provide built-in functions for checking data type Can accept user input as a string and then attempt to use conversion methods to get correct data type –If conversion fails, input data was the wrong type
40
Programming Logic and Design, Fourth Edition, Comprehensive40 Validating a Data Range Similar technique to validating an exact value
41
Programming Logic and Design, Fourth Edition, Comprehensive41 Validating Reasonableness and Consistency of Data Data input can be the correct type and within range, but still be incorrect Check for obvious logical relationships, such as: –Payment date cannot occur before purchase date –Zip code falls within the state –Birth date produces a reasonable age
42
Programming Logic and Design, Fourth Edition, Comprehensive42 Validating Presence of Data Must handle the possibility of missing data Some programming languages provide built-in functions to test for blank or null values
43
Programming Logic and Design, Fourth Edition, Comprehensive43 Summary Batch programs process data collected earlier Interactive programs collect user data in real time Single-level menu allows user to select a choice of actions Black-box module is independent of the rest of the program Must handle invalid user selections from menu
44
Programming Logic and Design, Fourth Edition, Comprehensive44 Summary (continued) Case structure handles multiple menu options Multilevel menu manages large number of options Validate all user input Validate for –Correct data type –Data within acceptable ranges –Data is reasonable and consistent –Data is present
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.