14.11 Case Study: A Transaction-Processing Program
We now present a substantial transaction-processing program (Fig. 14.13) using a random-access file to achieve instant-access processing. The program maintains a bank’s account information. It updates existing accounts, adds new accounts, deletes accounts and stores a formatted listing of all current accounts in a text file. We assume that the program of Fig. 14.10 has been executed to create the file credit.dat and that the program of Fig. 14.11 has been executed to insert the initial data. Line 25 opens the credit.dat file by creating an fstream object for both reading and writing in binary format.
The program has five options (Option 5 is for terminating the program). Option 1 calls function createTextFile to store a formatted list of all the account information in a text file called print.txt that may be printed. Function createTextFile (lines 76–109) takes an fstream object as an argument to be used to input data from the credit.dat file. Function createTextFile invokes istream member function read (lines 95–96) and uses the sequential-file-access techniques of Fig. 14.12 to input data from credit.dat. Function outputLine, discussed in Section 14.10, outputs the data to file print.txt. Note that function createTextFile uses istream member function seekg (line 91) to ensure that the file-position pointer is at the beginning of the file before reading the file’s contents. After choosing Option 1, the print.txt file contains
Account Last Name First Name Balance
29 Brown Nancy -24.54
33 Dunn Stacey 314.33
37 Barker Doug 0.00
88 Smith Dave 258.34
96 Stone Sam 34.98
Option 2 calls updateRecord (lines 112–148) to update an account. This function updates only an existing record, so the function first determines whether the specified record is empty—we use function getAccount (lines 235–245) to read from the user the number of the record to update. Line 121 reads data into object client, using istream member function read. Then line 124 compares the value returned by getAccountNumber of the client object to zero to determine whether the record contains information. If this value is zero, lines 145–146 print an error message indicating that the record is empty. If the record contains information, line 125 displays the record, using function outputLine, line 130 inputs the transaction amount and lines 133–142 calculate the new balance and rewrite the record to the file. A typical execution for Option 2 is
Enter account to update (1 - 100): 37
37 Barker Doug 0.00
Enter charge (+) or payment (-): +87.99
37 Barker Doug 87.99
Option 3 calls function newRecord (lines 151–192) to add a new account to the file. If the user enters an account number for an existing account, newRecord displays an error message indicating that the account exists (lines 189–190). This function adds a new account in the same manner as the program of Fig. 14.11. A typical execution for Option 3 is
Enter new account number (1 - 100): 22
Enter lastname, firstname, balance
? Johnston Sarah 247.45
Option 4 calls function deleteRecord (lines 195–223) to delete a record from the file. Line 197 prompts the user to enter the account number. Only an existing record may be deleted, so, if the specified account is empty, line 221 displays an error message. If the account exists, lines 209–216 reinitialize that account by writing an empty record (blank-Client) to the file. Line 218 displays a message to inform the user that the record has been deleted. A typical execution for Option 4 is
Enter account to delete (1 - 100): 29
Account #29 deleted.