home *** CD-ROM | disk | FTP | other *** search
- //BUDGET3.CPP - Budget program with real classes for
- // the first time. Now we transform the
- // C structs into classes. This is starting
- // to look like something.
-
- #include <iostream.h>
-
- //the maximum number of accounts one can have
- const int maxAccounts = 10;
-
- //Checking - this describes checking accounts
- class Checking //Note 1
- {
- public:
- Checking() //Note 2
- {
- accountNumber = 0;
- balance = 0.0F;
- }
- void init() //Note 3
- {
- cout << "Enter account number:";
- cin >> accountNumber;
- }
-
- //access functions //Note 4
- int accountNo()
- {
- return accountNumber;
- }
- float acntBalance()
- {
- return balance;
- }
-
- //transaction functions //Note 5
- void deposit(float amount)
- {
- balance += amount;
- }
- void withdrawal(float amount);
-
- //display function for displaying self on æcoutÆ
- void display() //Note 6
- {
- cout << "Account " << accountNumber
- << " = " << balance
- << "\n";
- }
-
- protected: //Note 7
- unsigned accountNumber;
- float balance;
- };
- //withdrawal - this member function is too big to
- // be inlined
- void Checking::withdrawal(float amount)
- {
- if (balance < amount )
- {
- cout << "Insufficient funds: balance " << balance
- << ", check " << amount
- << "\n";
- }
- else
- {
- balance -= amount;
-
- //if balance falls too low, charge service fee
- if (balance < 500.00F)
- {
- balance -= 0.20F;
- }
- }
- }
-
- //Savings - you can probably figure this one out
- class Savings
- {
- public:
- Savings()
- {
- accountNumber = 0;
- balance = 0.0F;
- noWithdrawals = 0;
- }
- void init()
- {
- cout << "Enter account number:";
- cin >> accountNumber;
- }
-
- //access functions
- int accountNo()
- {
- return accountNumber;
- }
- float acntBalance()
- {
- return balance;
- }
-
- //transaction functions
- void deposit(float amount)
- {
- balance += amount;
- }
- void withdrawal(float amount);
-
- //display function - display self to cout
- void display()
- {
- cout << "Account " << accountNumber
- << " = " << balance
- << " (no. withdrawals = " << noWithdrawals
- << ")\n";
- }
-
- protected:
- unsigned accountNumber;
- float balance;
- int noWithdrawals;
- };
- void Savings::withdrawal(float amount)
- {
- if (balance < amount)
- {
- cout << "Insufficient funds: balance " << balance
- << ", withdrawal " << amount
- << "\n";
- }
- else
- {
- if (++noWithdrawals > 1)
- {
- balance -= 5.00F;
- }
- balance -= amount;
- }
- }
-
- //prototype declarations
- void process(Checking &checking);
- void process(Savings &savings);
-
- //checking and savings account objects
- Checking chkAcnts[maxAccounts]; //Note 8
- Savings svgAcnts[maxAccounts];
-
- //main - accumulate the initial input and output totals
- int main()
- {
- /*loop until someone enters an æXÆ or æxÆ*/
- int noChkAccounts = 0; //count the number of accounts
- int noSvgAccounts = 0;
- char accountType; //S or C
-
- unsigned keepLooping = 1;
- while (keepLooping)
- {
- cout << "Enter S for Savings, "
- "C for Checking, X for exit\n";
- cin >> accountType;
-
- switch (accountType)
- {
- case 'c':
- case 'C':
- if (noChkAccounts < maxAccounts)
- {
- chkAcnts[noChkAccounts].init(); //Note 9
- process(chkAcnts[noChkAccounts]);
- noChkAccounts++;
- }
- else
- {
- cout << "No more room for checking accounts\n";
- }
- break;
-
- case 's':
- case 'S':
- if (noSvgAccounts < maxAccounts)
- {
- svgAcnts[noSvgAccounts].init();
- process(svgAcnts[noSvgAccounts]);
- noSvgAccounts++;
- }
- else
- {
- cout << "No more room for savings accounts\n";
- }
- break;
-
- case 'x':
- case 'X':
- keepLooping = 0;
- break;
-
- default:
- cout << "I didn't get that.\n";
- }
- }
-
- //now present totals
- float chkTotal = 0.0F; //total of all checking accounts
- cout << "Checking accounts:\n";
- int i;
- for (i = 0; i < noChkAccounts; i++)
- {
- chkAcnts[i].display(); //Note 10
- chkTotal += chkAcnts[i].acntBalance();
- }
- float svgTotal = 0.0F; //total of all savings accounts
- cout << "Savings accounts:\n";
- for (i = 0; i < noSvgAccounts; i++)
- {
- svgAcnts[i].display();
- svgTotal += svgAcnts[i].acntBalance();
- }
-
- float total = chkTotal + svgTotal;
- cout << "Total for checking accounts = " << chkTotal << "\n";
- cout << "Total for savings accounts = " << svgTotal << "\n";
- cout << "Total worth = " << total << "\n";
- return 0;
- }
-
- //process(Checking) - input the data for a checking account*/
- void process(Checking &checking)
- {
- cout << "Enter positive number for deposit,\n"
- "negative for check, 0 to terminate";
-
- float transaction;
- do
- {
- cout << ":";
- cin >> transaction;
-
- //deposit
- if (transaction > 0.0F)
- {
- checking.deposit(transaction);
- }
-
- //withdrawal
- if (transaction < 0.0F)
- {
- checking.withdrawal(-transaction);
- }
- } while (transaction != 0.0F);
- }
- //process(Savings) - input the data for a savings account
- void process(Savings &savings)
- {
- cout << "Enter positive number for deposit,\n"
- "negative for withdrawal, 0 to terminate";
-
- float transaction;
- do
- {
- cout << ":";
- cin >> transaction;
-
- //deposit
- if (transaction > 0.0F)
- {
- savings.deposit(transaction);
- }
-
- //withdrawal
- if (transaction < 0.0F)
- {
- savings.withdrawal(-transaction);
- }
- } while (transaction != 0.0F);
- }
-