Credit Processing Program

Consider the following problem statement:

Create a credit-processing program capable of storing at most 100 fixed-length records for a company that can have up to 100 customers. Each record should consist of an account number that acts as the record key, a last name, a first name and a balance. The program should be able to update an account, insert a new account, delete an account and insert all the account records into a formatted text file for printing.

The next few sections create this credit-processing program. Figure 14.11 illustrates opening a random-access file, defining the record format using an object of class ClientData (Figs. 14.914.10) and writing data to the disk in binary format. This program initializes all 100 records of the file credit.dat with empty objects, using function write. Each empty object contains the account number 0, emptylast and first name strings and the balance 0.0. Each record is initialized with the space in which the account data will be stored.


 1   // Fig. 14.9: ClientData.h
 2   // Class ClientData definition used in Fig. 14.11–Fig. 14.14.
 3   #ifndef CLIENTDATA_H
 4   #define CLIENTDATA_H
 5
 6   #include <string>
 7
 8   class ClientData
 9   {
10   public:
11      // default ClientData constructor
12      ClientData( int = 0, const std::string & = "",
13         const std::string & = "", double = 0.0 );
14
15      // accessor functions for accountNumber
16      void setAccountNumber( int );
17      int getAccountNumber() const;
18
19      // accessor functions for lastName
20      void setLastName( const std::string & );
21      std::string getLastName() const;
22
23      // accessor functions for firstName
24      void setFirstName( const std::string & );
25      std::string getFirstName() const;
26
27      // accessor functions for balance
28      void setBalance( double );
29      double getBalance() const;
30   private:
31      int accountNumber;
32      char lastName[ 15 ];
33      char firstName[ 10 ];
34      double balance;
35   }; // end class ClientData
36
37   #endif


Fig. 14.9. ClientData class header.


 1   // Fig. 14.10: ClientData.cpp
 2   // Class ClientData stores customer's credit information.
 3   #include <string>
 4   #include "ClientData.h"
 5   using namespace std;
 6
 7   // default ClientData constructor
 8   ClientData::ClientData( int accountNumberValue, const string &lastName,
 9      const string &firstName, double balanceValue )
10      : accountNumber( accountNumberValue ), balance( balanceValue )
11   {
12      setLastName( lastNameValue );
13      setFirstName( firstNameValue );
14   } // end ClientData constructor
15
16   // get account-number value
17   int ClientData::getAccountNumber() const
18   {
19      return accountNumber;
20   } // end function getAccountNumber
21
22   // set account-number value
23   void ClientData::setAccountNumber( int accountNumberValue )
24   {
25      accountNumber = accountNumberValue; // should validate
26   } // end function setAccountNumber
27
28   // get last-name value
29   string ClientData::getLastName() const
30   {
31      return lastName;
32   } // end function getLastName
33
34   // set last-name value
35   void ClientData::setLastName( const string &lastNameString )
36   {
37      // copy at most 15 characters from string to lastName
38      int length = lastNameString.size();
39      length = ( length < 15 ? length : 14 );
40      lastNameString.copy( lastName, length );
41      lastName[ length ] = ''; // append null character to lastName
42   } // end function setLastName
43
44   // get first-name value
45   string ClientData::getFirstName() const
46   {
47      return firstName;
48   } // end function getFirstName
49
50   // set first-name value
51   void ClientData::setFirstName( const string &firstNameString )
52   {
53      // copy at most 10 characters from string to firstName
54      int length = firstNameString.size();
55      length = ( length < 10 ? length : 9 );
56      firstNameString.copy( firstName, length );
57      firstName[ length ] = ''; // append null character to firstName
58   } // end function setFirstName
59
60   // get balance value
61   double ClientData::getBalance() const
62   {
63      return balance;
64   } // end function getBalance
65
66   // set balance value
67   void ClientData::setBalance( double balanceValue )
68   {
69      balance = balanceValue;
70   } // end function setBalance


Fig. 14.10. ClientData class represents a customer’s credit information.

Objects of class string do not have uniform size, rather they use dynamically allocated memory to accommodate strings of various lengths. We must maintain fixed-length records, so class ClientData stores the client’s first and last name in fixed-length char arrays (declared in Fig. 14.9, lines 32–33). Member functions setLastName (Fig. 14.10, lines 35–42) and setFirstName (Fig. 14.10, lines 51–58) each copy the characters of a string object into the corresponding char array. Consider function setLastName. Line 38 invokes string member function size to get the length of lastNameString. Line 39 ensures that length is fewer than 15 characters, then line 40 copies length characters from lastNameString into the char array lastName using string member function copy. Member function setFirstName performs the same steps for the first name.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.116.43