Programming 3
______________________________________________________________________________________________________________________________________________________________________________________________________________________
Page 2
______________________________________________________________________________________________________________________________________________________________________________________________________________________
Lesson 8
Lab 8 – Using Repetition for User EXIT and Input Validation
Objectives:
· Understand how to use the Repetition/Loop Structure for Input Validation
· Use the while repetition structure to allow user to re-enter bad input or choose to exit
· Understand how to use string or numeric comparison depending on what is being tested
· Translate pseudocode to flowcharts using Flowgorithm
· Use named constants as provided input
· Use comments to document test cases
· Use the Decision Structure for user requested program exit and fee determination
· Use the Repetition/Loop structure for validation and ending program at user request (Sentinel Value)
· Use the AND/OR operator in your decision structure
Lab Task Checklist
· It is expected you have read the required reading in this Lesson before starting the lab.
· Review Course Resources/Rules of Engagement (ROE)
· Review Course Resources/Flowgorithm Guidance/Menu Guidance
· Use Flowgorithm to create an algorithm meeting customer requirement
· Naming Convention: “ lastname-repetition.fprg ”
· Submit files to Blackboard.
Instructions:
1. Review the customer requirements:
Customer Requirements: The customer needs an application that provides the total cost of concert tickets to include sales tax. The customer will provide their name, phone number, and how many adult and child tickets they want. The program will do the required calculation and display the name, phone number, #number of tickets, sales tax total, subtotal, and total cost.
· Adult tickets are $36.75/each and child tickets are $25.50/each.
· Tax rate is 7% (0.07 for calculations purposes). Total = subtotal + total tax + fee
· It needs to display the transaction with a title of the Concert (This is provided by the program)
· There is an additional fee (not Taxed) based on how many total tickets
· If less than or equal to 5 total tickets, then fee is $1 (1.00) per person
· If more than 5 total tickets, then fee is (.50) per person.
· The fee is not Taxed but is part of the total cost.
· The algorithm will calculate the total cost = subtotal + sales TAX total + fee
· Fee has been added to the display requirements
· The only dates that are available for attendance are: Mar, Apr, and May. Use the while loop to validate input and allow user to re-enter input or choose to exit if there is an error for date (while structure).
· There must be at least one adult ticket purchased before any processing is done. Use the while loop to validate input and allow user to re-enter input or choose to exit if there is an error for adults (while structure).
· Use the Decision Structure to determine if 999 entered for exit
· Date Error. Sentinel value for exit from validation and program should be “999” (date != “999”) (decision structure)
· Adult Error. Sentinel value for exit from validation and program should be 999 (adult != 999)(decision structure)
· Use the While loop to allow user to finalize exit or to start again. Sentinel value for exit should be “NO”.
2. Review the IPO chart and example pseudocode found below.
3. Open Flowgorithm and save with the required naming conventions in the “Lab Task Checklist” and remember to enter your Program Attributes.
4. Translate the below pseudocode to flowcharts using Flowgorithm.
a. Reserve Memory Locations (Declare) - Use as short and meaningful names
I. Variables - Declare variables close to where they will be used
1. Use the correct naming conventions (found in ROE)
II. Named constants - Declare named constants at the start of the program
1. Use the correct naming conventions (found in ROE)
2. Use named constants for:
a. cost of adult tickets
b. cost of child tickets
c. TAX rate
d. the two different fees
e. ATTEND number test
b. Use prompts to describe required input before asking for input
c. You will need to provide the title of your own concert, as well as your lastname for programmer credits and end of program message
d. The output must clearly delineate what each value is.
I. name and phone number with the details of total cost
II. Detailed breakout of Total cost to include: # of adult tickets, # of child tickets, subtotal, sales Tax total, fee, and total cost.
6. Document your test cases using the comment block of flowgorithm
INPUT, Processing and OUTPUT:
|
Input |
Processing |
Output |
|
Constants: Sales tax = 0.07 Price of adult tickets = 36.75 Price of child tickets = 25.50 Maximum fee = 1.00 Minimum fee = .50 Attend Limit = 5
Variables: Name Phone Number # adult tickets # child tickets Date
|
Assign price of adult, child tickets and sales tax rate Assign fee rate for minimum and maximum Assign number for attend test Get # adult tickets, # child tickets Test for Adults Get Date Test for Date Get Name, Phone Number Calculate subtotal, sales tax, fee and total cost · subtotal = (#adult * 36.75) + (#child * 25.50) · total sales tax = subtotal * 0.07 · total fees if less than or equal to 5 attending · fee = #total tickets * 1.00 · total fees if more than 5 attending · fee=#total tickets * .50 · total cost = subtotal + sales tax + fee
Validate and allow re-entry or exit · #adult · Date · Allow user to finalize exit Sentinel Values – Validation – 999 Sentinel Value – exit or continue - NO |
Title of Concert error message for adult and date # adult tickets, # child tickets and result of subtotal, sales tax total, total cost. name and phone number with an additional sentence saying the details of total cost
Programmer Credits |
|
Test Data |
|
|
|
|
T1:Adults 0, child 2 Allows re-entry T1a: 999 allows exit T1b: Any key allows final exit T1c: NO allows new transaction
T2: Adults 2, child 0. Wrong Date. Allows re-entry T2a: 999 allows exit T2b: Any key allows final exit T2c: NO allows new transaction
T3: adults 2 , child 2. Subtotal = 124.50. Tax = 8.715 , fee = 4,total cost = 137.215 Test 5 or less NO to continue T4:adults 3, child 2. Subtotal = 161.25, Tax=11.2875, fee = 5 for Total Cost = 177.5375. Attending=5 Any key to exit T5: adults 3 child 3 subtotal=186.75 tax = 13.0725 fee= 3 total cost = 202.8225T4: adults 3, child 3 Correct date Subtotal = 186.75. Tax = 13.0725 , fee = 3 total cost = 202.8225 (more than 5) |
|
|
|
|
|
Pseudocode Example (Gaddis Pseudocode):
**DISCLAIMER: This is only one of many ways that can be used to meet the customer requirements above.
** Reminder: Flowgorithm concatenation symbol is an ampersand (&) not a comma (,)
Function Main
// T1:Adults 0, child 2 Exits at adult entry
// T2: Adults 2, child 0. Wrong Date. exits at date entry
// T3: adults 2 , child 2. Subtotal = 124.5.5. Tax =8.715 , fee = 4,total cost = 137.215 Test 5 or less
// T4: adults 3, child 2. Subtotal = 161.25, Tax= 11.2875, fee = 5 for Total Cost = 177.5375. Attending=5
// T5: adults 3 child 3 Subtotal = 186.75, Tax=13.0725, fee = 3 for Total Cost = 202.8225
Declare Real TAX, ADULTcost, CHILDcost, MINfee, MAXfee
Set TAX = 0.07
Set ADULTcost = 36.75
Set CHILDcost = 25.50
Set MINfee = 0.50
Set MAXfee = 1.00
Declare Integer ATTEND
Set ATTEND = 5
Display "This program calculates total cost of concert tickets"
Display "Some CONCERT Title"
Declare String exit
Set exit = "NO"
While exit == "NO"
Display "How Many Adults. You need at least one."
Declare Integer adultTickets
Input adultTickets
While adultTickets <= 0
Display "You must have at least one adult ticket. Please input at least one adult or 999 to exit."
Input adultTickets
End While
If adultTickets != 999 Then
Display "You must choose one of the allowed dates which are Mar, Apr or May"
Display "Please enter the date you wish to attend"
Declare String date
Input date
While date != "Mar" AND date != "Apr" AND date != "May" AND date != "999"
Display "Dates available are Mar, Apr or May. Please input correct date or 999 to exit."
Input date
End While
If date != "999" Then
Display "Child Tickets Needed"
Declare Integer childTickets
Input childTickets
Display "Name"
Declare String name
Input name
Display "Phone Number"
Declare String phone
Input phone
Declare Real subtotal, salesTax, totalCost, fee
Declare Integer totalTickets
Set totalTickets = childTickets + adultTickets
If totalTickets <= ATTEND Then
Set fee = totalTickets * MAXfee
Else
Set fee = totalTickets * MINfee
End If
Set subtotal = adultTickets * ADULTcost + childTickets * CHILDcost
Set salesTax = subtotal * TAX
Set totalCost = subtotal + salesTax + fee
Display "Some CONCERT Title"
Display "Thank you ", name, " at ", phone, ". Details of your total cost ", totalCost, " can be found below:"
Display "Adult Tickets: ", adultTickets
Display "Child Tickets: ", childTickets
Display "The date you selected was ", date
Display "Subtotal is: $ ", subtotal, ", Tax is: $", salesTax, ", Additional Fee is $", fee, " for a total cost of: $ ", totalCost
End If
End If
Display "Do you want to Really want to Exit? Enter NO to continue, otherwise enter any key"
Input exit
End While
// "Your last name and message for end of program"
Display "Thank you for using this program. This program was designed by lastname"
Graphic of Pseudocode Example (Gaddis Pseudocode):
AY2022