Title: Chapter 8 Data Validation
1Chapter 8 - Data Validation
2Data Validation Tests
- Numeric
- Alphabetic
- Reasonableness (limit or range)
- Consistency check
- Existing Code
- Sequence Check
- Completeness Check
- Data Check
- Subscript Check
- Defensive Programming
3Numeric Test
- Ensures that a numeric field contains numeric
data - . Blanks are not numeric
4Reasonableness Check
- A limit or range check
- Ensures that a number is within expected limits
does not exceed an upper or lower extreme
5Consistency check
- Verifies that the values in two or more fields
are consistent - Example
- Individuals credit rating and the amount of
credit a bank is willing to extend
6Existing Code Check
- Each code should be explicitly checked just
because you check one code doesnt mean the other
is right. - Example of bad code
- If sex-in Male Add 1 to Number-of-Men
- Else
- Add 1 to Number-of-Women
7IF statement
- Has a large number of options
- One of the most difficult statements to master
- An IF requires a CONDITION to be evaluated TRUE
or FALSE
8Four Types of Conditions
- Relational
- Class
- Sign
- Condition-Name
9Relational Conditon
- Most Common Condition
- Compares the quantities on both sides of the
relational operator to be True or False - The data types being compared must be of the same
type both numeric or both nonnumeric
10Figure 8.1 The Relational Condition
IS NOT GREATER THAN IS NOT IS NOT
LESS THAN identifier-1 IS NOT
TO literal-2 expression-1 IS NOT
expression-2 IS NOT GREATER THAN OR
EQUAL TO IS NOT IS NOT LESS THAN OR
EQUAL TO IS NOT
(a) Syntax
05 NUMERIC-FIELD PIC 9(5).05
ALPHANUMERIC-FIELD PIC X(5).. .
.IF NUMERIC-FIELD 10 . . . (valid entry)IF
NUMERIC-FIELD 10 . . . (invalid entry)IF
ALPHANUMERIC-FIELD 10 (invalid entry)IF
ALPHANUMERIC-FIELD 10 (valid entry)
(b) Examples
11Class Test
- Ensures that a field contains numeric or
alphabetic data in accordance with its PIC clause - Numeric fields 0 to 9 and a sign ( -)
- Alphabetic A to Z and/or blanks
- Alphanumeric letters numbers special
characters
12Figure 8.2 The Class Test
NUMERIC ALPHABETIC IF identifier
IS NOT ALPHABETIC-UPPER ALPHABETIC-LOWER
(a) Syntax
05 NUMERIC-FIELD PIC 9(5).05
ALPHABETIC-FIELD PIC X(5).. .
.IF NUMERIC-FIELD IS NUMERIC PERFORM
D0-ARITHMETIC-CALCULATIONSEND-IF. IF
NUMERIC-FIELD IS NOT NUMERIC DISPLAY
ERROR - NUMERIC FIELD CONTAINS INVALID
DATAEND-IF. IF ALPHABETIC-FIELD IS
ALPHABETIC DISPLAY ALPHABETIC FIELD
CONTAINS UPPER AND/OR LOWER CASE
LETTERSEND-IF. IF ALPHABETIC-FIELD IS
NOTALPHABETIC DISPLAY ERROR - ALPHABETIC
FIELD CONTAINS NON-ALPHABETIC DATAEND-IF.
(b) Examples
13Sign Test
- Determines whether a numeric field is positive
negative or zero. - You could use a relational test to check for the
sign of a number
14Figure 8.3 The SignTest
identifier POSITIVE IF arithmetic
expression IS NOT NEGATIVE ZERO
(a) Syntax
IF NET-PAY IS NOT POSITIVE PERFORM
TOO-MUCH-TAXESEND-IF. IF CHECK-BALANCE IS
NEGATIVE PERFORM OVERDRAWNEND-IF.
(b) Examples
15Condition-Name Test
- A special way of writing a relational condition
that makes it easier to read - 88-level entry in Data Division simplifies coding
in Procedure Division
16Figure 8.4 Condition Names (88-level entries)
(a) Syntax
05 YEAR-CODE PIC 9. 88 FRESHMAN VALUE
1. 88 SOPHOMORE VALUE 2. 88
JUNIOR VALUE 3. 88 SENIOR VALUE 4.
88 GRAD-STUDENT VALUES ARE 5 THRU 8. 88
UNDER-CLASSMAN VALUES ARE 1 2 88
UPPER-CLASSMAN VALUES ARE 3 4. 88
VALID-CODES VALUES ARE 1 THRU 8. IF FRESHMAN
PERFORM WELCOME-NEW-STUDENTSEND-IF IF
VALID-CODES PERFORM PROCESS-STUDENT-RECORDEL
SE DISPLAY INCOMING YEAR CODE IS IN
ERROREND-IF.
(b) Examples
17Compound Test
- Using two or more tests at a time with the AND or
OR operators - AND implies all conditions must be TRUE
- OR implies one condition must be TRUE
18Figure 8.5 Compound Conditions
(a) A B AND C D
(b) A B OR C D
19Hierarchy of Operations
- Which takes precedence in a nested IF
- Arithmetic expressions
- Relational operators
- NOT condition
- AND (from left to right)
- OR (from left to right)
20Implied Conditions
- The simple conditions with a compound condition
often have the same subject as the statement - If salary 30000 and salary
- If salary 30000 and
- If department MIS or ACC
21Nested IFs
- Results when either statement 1 or statement 2
is itself another IF statement two or more Ifs
in one sentence
22Figure 8.6 Nested IF Statements
(a) Flowchart
23Figure 8.6 Nested IF Statements
IF A B IF A C MOVE A TO BIG
ELSE MOVE C TO BIG
END-IFELSE IF C B MOVE C TO
BIG ELSE MOVE B TO BIG
END-IFEND-IF.
(b) COBOL Statements
24Figure 8.7 Nested IF Statements/II
IF A B IF C D ADD 1 TO X
END-IFELSE ADD 1 TO YEND-IF
(a) Flowchart
IF A B IF C D ADD 1 TO X
ELSE NEXT SENTENCEELSE ADD 1 TO Y.
(b) NEXT SENTENCE(COBOL-74)
(c) Scope Terminators(COBOL-85)
25Accept Statement
- Used to obtain the day date and/or time of
execution
26Figure 8.8 The ACCEPT Statement
WORKING-STORAGE SECTION. 01 EMPLOYEE-RECORD.
. . 05 EMP-DATE-OF-BIRTH.
10 EMP-BIRTH-MONTH PIC 99. 10
EMP-BIRTH-YEAR PIC 99. 01 EMPLOYEE-AGE PIC
99V99. 01 DATE-WORK-AREA. 05
TODAYS-YEAR PIC 99. 05 TODAYS-MONTH PIC
99. 05 TODAYS-DAY PIC 99. .
.PROCEDURE DIVISION.. . ACCEPT
DATE-WORK-AREA FROM DATE.. .
COMPUTE EMPLOYEE-AGE TODAYS-YEAR -
EMP-BIRTH-YEAR (TODAYS-MONTH -
EMP-BIRTH-MONTH) / 12.
27Figure 8.9 The Stand-Alone Edit Program
28Figure 8.10 Transaction Files and Error Reports
1 Invalid date
123459BAKER ROBERT G930431F05006700006640
25X987651BROWN PETER
G930112M1000353000352000N999999JONES TOM
J931209E3500450000460005Y987655BROWNING
PAULA J93102400700240000252500Y999777ELSINOR
TERRY R921126F0500168000159005N655443F
ITZPATRICK DAN T930532L070100000098700
0C987654SMITH PAUL G921213M03005100005
00502Y PINNOCK
1931012F10003424OO331000NX93477BUTLER JOHN
H930631C0000423000419075N354679KERBEL
NORMAN X930331E1000340000324300Y2648405CLARK
JANE S921101F0700561500551200N8464
40 SAM 921231XI500182300169
802N233432BEINHORN CATHY
B921122M0200123400113402Y556564HUMMER
MARGO R920815C0800234500123403y677844MCDONAL
D JAMES 930123C0500423500402300N8862
22VOGEL JANICE D930518F1200634500612
302Y008632TOWER DARREN
R930429L0900700200689300N
2 Inconsistent mileage
3 Nonnumeric mileage
4 Invalid car type
(a) Transaction File
29Figure 8.10 (continued)
ERROR REPORT AS OF 07/03/98 CONTRACT LAST
NAME ERROR MESSAGE FIELD CONTENTS 123459
BAKER INVALID DAY 04/31 123459
BAKER INSURANCE CODE MUST BE Y OR N X
987651 BROWN MILES DRIVEN UNREASONABLY
LOW DAYS 10 MILES 000010 999999
JONES MONTH MUST BE BETWEEN 1 AND 12 13
999999 JONES MILEAGE IN LESS THAN
MILEAGE OUT IN 004500 OUT 004600 999999
JONES NON-NUMERIC MILEAGE RATE O5
987655 BROWNING CAR TYPE MUST BE E
C M F OR L 0 987655 BROWNING DATE
HAS NOT YET OCCURRED 10/24/98 987655
BROWNING NON-NUMERIC MILES IN OO2400 655443
FITZPATRICK INVALID DAY 05/32 655443
FITZPATRICK INSURANCE CODE MUST BE Y OR
N C PINNOCK NON-NUMERIC
CONTRACT NUMBER
PINNOCK MISSING FIRST NAME
PINNOCK NON ALPHABETIC INITIAL 1
PINNOCK DATE YAS NOT YET
OCCURRED 10/12/98
PINNOCK NON-NUMERIC MILES OUT OO3310 X93477
BUTLER NON-NUMERIC CONTRACT
NUMBER X93477 X93477 BUTLER INVALID
DAY 06/31 X93477 BUTLER DAYS RENTED
MUST BE ZERO 00 X93477
BUTLER MILEAGE RATE OUT OF RANGE 75 84644O
NON-NUMERIC CONTRACT NUMBER 84644O
84644O MISSING LAST NAME 84644O CAR
TYPE MUST BE E C M F OR L X
84644O DAYS RENTED MUST BE NUMERIC I5
84644O MILES DRIVEN UNREASONABLY LOW DAYS I5
MILES 000125
(b) Error Report
30Figure 8.10 (continued)
999777ELSINOR TERRY R921126F050016800015
9005N987654SMITH PAUL
G921213M0300510000500502Y354679KERBEL
NORMAN X930331E1000340000324300Y2648405CLARK
JANE S921101F0700561500551200N2334
32BEINHORN CATHY B921122M0200123400113
402Y556564HUMMER MARGO
R920815C0800234500123403Y677844MCDONALD
JAMES 930123C0500423500402300N886222VOGEL
JANICE D930518F1200634500612302Y0086
32TOWER DARREN R930429L0900700200689
300N
(c) Valid Transaction File
31Figure 8.11 Pseudeocode
Open filesGet todays dateWrite error
headingsDO WHILE data remains Move YES to
valid record switch Validate contract
number Validate last name first name and
initial Validate car type Validate month day
and non-future date Validate days
rented Validate mileage in mileage out and
computed miles Validate mileage rate Validate
insurance code IF valid record switch YES
Write valid record ELSE Write blank line in
error report ENDIF Read next recordENDDOClose
filesStop run
32Figure 8.12 Hierarchy Chart for Validation Program
33Figure 8.14 Limitations of COBOL-74
IF VALID-RECORD-SW Y IF SEX M ADD 1
TO MALE COUNTER END-IF IF INCOME
50000 ADD 1 TO HIGH-INCOME-COUNTER
END-IFEND-IF.
(b) COBOL-85
IF VALID-RECORD-SW Y PERFORM
DO-MORE-TESTS. DO-MORE-TESTS. IF SEX M
ADD 1 TO MALE-COUNTER. IF INCOME 50000
ADD 1 TO HIGH-INCOME-COUNTER.
(c) COBOL-74
(a) Flowchart