home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C++ for Dummies (3rd Edition)
/
C_FD.iso
/
BUDGETS
/
BUDGET4.CPP
< prev
next >
Wrap
C/C++ Source or Header
|
1996-09-15
|
8KB
|
368 lines
//BUDGET4.CPP - Budget program with expanded constructors,
// new (and improved) heap management, and
// static members. The Checking and Savings
// classes should have a destructor to remove
// the object from the list, but the destructors
// arenÆt included to save space.
#include <iostream.h>
#include <stdlib.h>
//Checking - this describes checking accounts
class Checking
{
protected:
Checking(Checking &c)
{
cout << "No creating funds\n";
}
public: //Note 1
Checking(unsigned accNo, float initialBalance = 0.0F);
//access functions
int accountNo()
{
return accountNumber;
}
float acntBalance()
{
return balance;
}
static Checking *first()
{
return pFirst;
}
Checking *next()
{
return pNext;
}
static int noAccounts()
{
return count;
}
//transaction functions
void deposit(float amount)
{
balance += amount;
}
void withdrawal(float amount);
//display function for displaying self on æcoutÆ
void display()
{
cout << "Account " << accountNumber
<< " = " << balance
<< "\n";
}
protected:
//keep accounts in a linked list so thereÆs no limit
static Checking *pFirst; //Note 2
Checking *pNext;
static int count; //number of accounts
unsigned accountNumber;
float balance;
};
//allocate space for statics
Checking *Checking::pFirst = 0; //Note 3
int Checking::count = 0;
//define constructor
Checking::Checking(unsigned accNo, float initialBalance)
{
accountNumber = accNo;
balance = initialBalance;
//add this to end of list and count it
count++;
Checking *pC;
if (pFirst == 0) //Note 4
{
pFirst = this; //empty list; make it first
}
else //list not empty; look for last
{
for (pC = pFirst; pC->pNext; pC = pC->pNext)
{ //do nothing (weÆre just looking...
} //...for the last element in the list)
pC->pNext = this; //tack us onto end
}
pNext = 0; //weÆre always last
}
//withdrawal - now the withdrawal function
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
{
protected:
Savings(Savings &s)
{
cout << "No creating funds\n";
}
public:
Savings(unsigned accNo, float initialBalance = 0.0F);
//access functions
int accountNo()
{
return accountNumber;
}
float acntBalance()
{
return balance;
}
static Savings *first()
{
return pFirst;
}
Savings *next()
{
return pNext;
}
static int noAccounts()
{
return count;
}
//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:
//keep savings accounts in linked list as well
static Savings *pFirst;
Savings *pNext;
static int count; //number of accounts
unsigned accountNumber;
float balance;
int noWithdrawals;
};
//allocate space for statics
Savings *Savings::pFirst = 0;
int Savings::count = 0;
//define constructor
Savings::Savings(unsigned accNo, float initialBalance)
{
accountNumber = accNo;
balance = initialBalance;
noWithdrawals = 0;
//add this to end of list and count it
count++;
Savings* pS;
if (pFirst == 0)
{
pFirst = this; //empty list; make it first
}
else //list not empty; look for last
{
for (pS = pFirst; pS->pNext; pS = pS->pNext)
{
}
pS->pNext = this; //make last point to us
}
pNext = 0; //and we point to nothing
}
//withdrawal - perform a Savings withdrawal
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
unsigned getAccntNo();
void process(Checking &checking);
void process(Savings &savings);
void outOfMemory();
//main - accumulate the initial input and output totals
int main()
{
/*loop until someone enters an æXÆ or æxÆ*/
Checking *pC;
Savings *pS;
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': //Note 5
pC = new Checking(getAccntNo());
if (pC == 0)
{
outOfMemory();
}
process(*pC);
break;
case 's':
case 'S': //Note 5
pS = new Savings(getAccntNo());
if (pS == 0)
{
outOfMemory();
}
process(*pS);
break;
case 'x':
case 'X':
keepLooping = 0;
break;
default:
cout << "I didn't get that.\n";
}
}
//now present totals
float chkTotal = 0.0F;
float svgTotal = 0.0F;
cout << "Checking accounts:\n"; //Note 6
for (pC = Checking::first(); pC; pC = pC->next())
{
pC->display();
chkTotal += pC->acntBalance();
}
cout << "Savings accounts:\n"; //Note 6
for (pS = Savings::first(); pS; pS = pS->next())
{
pS->display();
svgTotal += pS->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;
}
//getAccntNo - return the account number entered
unsigned getAccntNo()
{
unsigned accntNo;
cout << "Enter account number:";
cin >> accntNo;
return accntNo;
}
//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)
{
s