SEQUENTIAL FILE PROCESSING ALGORITHM
Scenario: Two or more files with common fields in each
Example: Bank account processing
Input
Files:
Withdrawals
Account number (numeric
max length 10)
Date (numeric mmddyyyy format)
Amount of withdrawal
(numeric max length less than $1 million)
Deposits
Account number (numeric
max length 10)
Date (numeric mmddyyyy format)
Amount of deposit
(numeric max length less than $1 million)
AccountBalance
Account number (numeric
max length 10)
Name (alphanumeric max
length 20)
Balance (numeric max
length less than $1 million)
New Account Balance – want a new
file for backtracking/backup purposes!
Account number (numeric
max length 10)
Name (alphanumeric max
length 20)
Balance (numeric max
length less than $1 million)
Overdrawn Flag
Withdrawal
and deposit files are ordered by date.
Account
Balance is sorted ascending by account number.
Assumptions:
Each
account number in the account balance file is unique.
Each
withdrawal or deposit amount account number is found in the account balance
file.
Solution:
All
of the files need to be ordered by the same field - account number for this
example, so be sure to sort withdrawal and deposit files by account number
first in the same order as the account balance file is ordered (i.e. ascending
for this problem). See psuedocode on next page…
QUESTION: What if account balance goes negative at any
point in time??!!
ALGORITHM:
Sort
deposit file ascending by account number
Sort
withdrawal file ascending by account number (then by amount?!)
Sample file info: Bal Dep With NewBal 1 1 1 1 2 1 1 2 3 1 3 3 4 3 3 4 5 4 3 5 4 5 5
Read
balance file first record
Read
deposit record@
Read
withdrawal record#
Perform
until end of file
Save account number from balance
file
Move balance-in to new-balance
@
Perform until end of file or dep account# is different than saved account#
Newbalance
= newbalance + deposit-in
Read deposit record
End-perform
#
Perform until end of file or withdraw
account# is different than saved account#
***Check that you have
enough funds to withdraw the current amount
Compute check balance = newbalance – withdrawal amount
If check balance > 0
then
Newbalance = newbalance –
withdrawal-amount
Else
OVERDRAWN!
*** consider
putting withdrawals in ascending order?
End-if
Read withdrawal record
End-perform
Write account
number (and name) with new balance (flag for overdrawn problem?!)
Read balance file next record
End-perform
Close
files
Stop