------------------------------------------------------------ -- Program to update a file of bank account balances -- when a withdrawal is made. -- -- Uses a direct file to contain the balance records. -- -- Adapted from Skansholm pp 527-8. -- ------------------------------------------------------------ with TEXT_IO, DIRECT_IO; use TEXT_IO, DIRECT_IO; procedure make_transaction is type account_record is record number : POSITIVE; balance : FLOAT; end record; package account_io is new DIRECT_IO (account_record); use account_io; package int_io is new INTEGER_IO(INTEGER); use int_io; accfile : account_io.FILE_TYPE; -- account records acc : account_record; -- working storage target : POSITIVE; -- account no sought amount : FLOAT; -- withdrawal amount found : BOOLEAN; -- right record found? first : account_io.count; -- for binary chop last : account_io.count; -- for binary chop mid : account_io.count; -- for binary chop begin OPEN (accfile, INOUT_FILE, "accounts.dat"); PUT ("Enter account number : "); GET (target); SKIP_LINE; -- use binary chop to find the record in the file found := false; first := 1; last := SIZE (accfile); while (not found) and (first <= last) loop mid := (first + last) / 2; READ (accfile, acc, mid); if target = acc.number then found := true; elsif target < acc.number then last := mid - 1; else first := mid + 1; end if; end loop; if found then PUT ("Balance is "); PUT (acc.balance, EXP=>0,FORE=>7,AFT=>2); NEW_LINE; PUT ("Enter amount to withdraw: "); GET (amount); SKIP_LINE; if amount < balance then acc.balance := acc.balance - amount; WRITE (accfile, acc, mid); PUT ("Resulting balance = "); PUT (acc.balance, EXP=>0,FORE=>7,AFT=>2); NEW_LINE; else PUT_LINE ("Not enough funds available"); end if; else PUT_LINE ("No such account found"); end if; CLOSE (accfile); end make_transaction;