Intro to Computer Science II (Pascal Example from Fall 1992)
Program 1
Phase 1 - Design
Problem Statement
A program, including separate procedure and types modules, is needed that
is capable of reading sequential data from a text file, and storing
the data as a keyed file of records. The user will be able the select
options that will add or delete records. The records will consist
of fields for a "dataless" keyfield, Book Title, Author, Price, and Quantity.
The user will also be able to make changes to the Price and Quantity fields.
Extensive error checking will also be implemented where feasible.
Input
The input to the program will be a text file containing the data to be used
for the generation of a file of records. Additional input will be from
the user's keyboard (in general use) to be used in the selection of various
menu options.
Output
The output will be a file of records that consists of fields corresponding
to the data in the text file used for input. Also, a menu option
will allow the user to display the contents of all records in the record
file.
Data Structures
KeyString=Packed Array 1..4 OF CHAR {4 characters used for key field}
LongString=Varying 1..50 OF CHAR {String used for book titles}
ShortString=Varying 1..15 OF CHAR {String used for book authors}
BookRec=RECORD {Define a book record}
Key : KeyString; {Field for the dataless key}
Title : LongString; {Field for the title of the book}
Author : ShortString; {Field for the author of the book}
Price : REAL; {Field for the price of the book (in $)}
Qty : INTEGER; {Field for quantity of book in stock}
END; {of BookRec}
Procedures
ExtractText( var InputFile : Text ; {Text file used to read data}
var RecFile : File ); {File of records to store data}
{This procedure reads data from a text file and stores it in a file of records}
Algorithm:
1. Reset InputFile for reading.
2 Reset RecFile for writing records (start from an empty file).
3. Loop thru all the data in InputFile using WHILE NOT(EOF(InputFile)).
4. Read data from InputFile.
5. Store data into additional record of RecFile.
6. Continue loop.
PromptUser( UserEntry : INTEGER );
{Displays a menu and returns an integer to the main program}
Algorithm:
1. Display a menu and a request for user input.
2. Get user input.
3. Return user input to main program.
CheckKey( RecFile : File ; {File of Records}
KeyToFind : KeyString ; {Key to find in RecFile}
var Exists : Boolean ); {Whether the key exists in RecFile}
{Checks whether a given key exists in the File of Records}
Algorithm: Attempt to locate KeyToFind in the RecFiles,
and set Exists to TRUE or FALSE determined by whether
KeyToFind was found. Additional details
to be determined at implementation (Phase 2).
AddRec( var RecFile : File ); {File of records}
{Add a record to the RecFile}
Algorithm:
1. Get user input for all new fields. Store in local variable NewRec.
2. Call CheckKey to determine whether NewRec's key exists in RecFile.
2. If it does, display an error message.
3. If it does not, add NewRec to RecFile.
DeleteRec(var RecFile : File ) {File of Records}
{Remove an old record from RecFile}
Algorithm:
1. Get user input for a local variable called OldRecKey.
2. Call CheckKey to determine whether OldRecKey exists in RecFile.
3. If it does, delete the record for OldRecKey from RecFile.
(An 'Are you sure (Y/N)' prompt may be provided to the user in part 3)
4. If it doesn't, display an error message.
ChangeQty(var RecFile : File)
{Change the quantity by an integer provided by the user}
Algorithm:
1. Get user input for a local variable called KeyToChg.
2. Call CheckKey to determine whether KeyToChg exists in RecFile.
3. If it does, prompt the user for the change and verify that the change
will result in a nonnegative integer for Qty field. If the change
is acceptable, effect needed change.
4. If ChkKey doesn't exist, display an error message.
ChangePrice(var RecFile : File)
{Change the price to an new value provided by the user}
Algorithm:
1. Get user input for a local KeyString-type variable called KeyToChg.
2. Call CheckKey to determine whether KeyToChg exists in RecFile.
3. If it does, prompt the user for the new value and if the input is
a nonnegative integer when multiplied by 100 (Pos.Integer No. of cents)
effect file Update.
4. If either CheckKey doesn't exist or the 'if' in part 3 has a FALSE
condition, display an error message.
ListAll(var RecFile : File);
{Display total book inventory of all records in RecFile}
Algorithm:
1. Initialize needed counter(s).
2. Open RecFile for seqential read of the records.
3. Loop thru all records using WHILE NOT(EOF(RecFile)).
4. Increment counter(s) and display any needed items (like headings).
5. Read the record sequentially and display in needed format.
6. Continue to perform loop.
7. Display any ending messages and return to main program.
