Credit Inquiry Program

Figure 14.7 enables a credit manager to display the account information for those customers with zero balances (i.e., customers who do not owe the company any money), credit (negative) balances (i.e., customers to whom the company owes money), and debit (positive) balances (i.e., customers who owe the company money for goods and services received in the past). The program displays a menu and allows the credit manager to enter one of three options to obtain credit information. Option 1 produces a list of accounts with zero balances. Option 2 produces a list of accounts with credit balances. Option 3 produces a list of accounts with debit balances. Option 4 terminates program execution. Entering an invalid option displays the prompt to enter another choice. Lines 64–65 enable the program to read from the beginning of the file after end-of-file has been read.


 1   // Fig. 14.7: Fig14_07.cpp
 2   // Credit inquiry program.
 3   #include <iostream>
 4   #include <fstream>  
 5   #include <iomanip>
 6   #include <string>
 7   #include <cstdlib>
 8   using namespace std;
 9
10   enum RequestType { ZERO_BALANCE = 1, CREDIT_BALANCE, DEBIT_BALANCE, END };
11   int getRequest();
12   bool shouldDisplay( int, double );
13   void outputLine( int, const string &, double );
14
15   int main()
16   {
17      // ifstream constructor opens the file          
18      ifstream inClientFile( "clients.txt", ios::in );
19
20      // exit program if ifstream could not open file
21      if ( !inClientFile )
22      {
23         cerr << "File could not be opened" << endl;
24         exit( EXIT_FAILURE );
25      } // end if
26
27      int account; // the account number
28      string name; // the account owner's name
29      double balance; // the account balance
30
31      // get user's request (e.g., zero, credit or debit balance)
32      int request = getRequest();
33
34      // process user's request
35      while ( request != END )
36      {
37         switch ( request )
38         {
39            case ZERO_BALANCE:
40               cout << " Accounts with zero balances: ";
41               break;
42            case CREDIT_BALANCE:
43               cout << " Accounts with credit balances: ";
44               break;
45            case DEBIT_BALANCE:
46               cout << " Accounts with debit balances: ";
47               break;
48         } // end switch
49
50         // read account, name and balance from file
51         inClientFile >> account >> name >> balance;
52
53         // display file contents (until eof)
54         while ( !inClientFile.eof() )
55         {
56            // display record
57            if ( shouldDisplay( request, balance ) )
58               outputLine( account, name, balance );
59
60            // read account, name and balance from file
61            inClientFile >> account >> name >> balance;
62         } // end inner while
63
64         inClientFile.clear(); // reset eof for next input          
65         inClientFile.seekg( 0 ); // reposition to beginning of file
66         request = getRequest(); // get additional request from user
67      } // end outer while
68
69      cout << "End of run." << endl;
70   } // end main
71
72   // obtain request from user
73   int getRequest()
74   {
75      int request; // request from user
76
77      // display request options
78      cout << " Enter request" << endl
79         << " 1 - List accounts with zero balances" << endl
80         << " 2 - List accounts with credit balances" << endl
81         << " 3 - List accounts with debit balances" << endl
82         << " 4 - End of run" << fixed << showpoint;
83
84      do // input user request
85      {
86         cout << " ? ";
87         cin >> request;
88      } while ( request < ZERO_BALANCE && request > END );
89
90      return request;
91   } // end function getRequest
92
93   // determine whether to display given record
94   bool shouldDisplay( int type, double balance )
95   {
96      // determine whether to display zero balances
97      if ( type == ZERO_BALANCE && balance == 0 )
98         return true;
99
100     // determine whether to display credit balances
101     if ( type == CREDIT_BALANCE && balance < 0 )
102        return true;
103
104     // determine whether to display debit balances
105     if ( type == DEBIT_BALANCE && balance > 0 )
106        return true;
107
108     return false;
109  } // end function shouldDisplay
110
111  // display single record from file
112  void outputLine( int account, const string &name, double balance )
113  {
114     cout << left << setw( 10 ) << account << setw( 13 ) << name
115        << setw( 7 ) << setprecision( 2 ) << right << balance << endl;
116  } // end function outputLine


Enter request
 1 - List accounts with zero balances
 2 - List accounts with credit balances
 3 - List accounts with debit balances
 4 - End of run
? 1

Accounts with zero balances:
300       White           0.00

Enter request
 1 - List accounts with zero balances
 2 - List accounts with credit balances
 3 - List accounts with debit balances
 4 - End of run
? 2

Accounts with credit balances:
400       Stone         -42.16

Enter request
 1 - List accounts with zero balances
 2 - List accounts with credit balances
 3 - List accounts with debit balances
 4 - End of run
? 3

Accounts with debit balances:
100       Jones          24.98
200       Doe           345.67
500       Rich          224.62

Enter request
 1 - List accounts with zero balances
 2 - List accounts with credit balances
 3 - List accounts with debit balances
 4 - End of run
? 4
End of run.


Fig. 14.7. Credit inquiry program.

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

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