123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- // acctabc.cpp -- bank account class methods
- #include <iostream>
- #include "acctabc.h"
- using std::cout;
- using std::ios_base;
- using std::endl;
- using std::string;
- // Abstract Base Class
- AcctABC::AcctABC(const string & s, long an, double bal)
- {
- fullName = s;
- acctNum = an;
- balance = bal;
- }
- void AcctABC::Deposit(double amt)
- {
- if (amt < 0)
- cout << "Negative deposit not allowed; "
- << "deposit is cancelled.\n";
- else
- balance += amt;
- }
- void AcctABC::Withdraw(double amt)
- {
- balance -= amt;
- }
- // protected methods for formatting
- AcctABC::Formatting AcctABC::SetFormat() const
- {
- // set up ###.## format
- Formatting f;
- f.flag =
- cout.setf(ios_base::fixed, ios_base::floatfield);
- f.pr = cout.precision(2);
- return f;
- }
- void AcctABC::Restore(Formatting & f) const
- {
- cout.setf(f.flag, ios_base::floatfield);
- cout.precision(f.pr);
- }
- // Brass methods
- void Brass::Withdraw(double amt)
- {
- if (amt < 0)
- cout << "Withdrawal amount must be positive; "
- << "withdrawal canceled.\n";
- else if (amt <= Balance())
- AcctABC::Withdraw(amt);
- else
- cout << "Withdrawal amount of $" << amt
- << " exceeds your balance.\n"
- << "Withdrawal canceled.\n";
- }
- void Brass::ViewAcct() const
- {
-
- Formatting f = SetFormat();
- cout << "Brass Client: " << FullName() << endl;
- cout << "Account Number: " << AcctNum() << endl;
- cout << "Balance: $" << Balance() << endl;
- Restore(f);
- }
- // BrassPlus Methods
- BrassPlus::BrassPlus(const string & s, long an, double bal,
- double ml, double r) : AcctABC(s, an, bal)
- {
- maxLoan = ml;
- owesBank = 0.0;
- rate = r;
- }
- BrassPlus::BrassPlus(const Brass & ba, double ml, double r)
- : AcctABC(ba) // uses implicit copy constructor
- {
- maxLoan = ml;
- owesBank = 0.0;
- rate = r;
- }
- void BrassPlus::ViewAcct() const
- {
- Formatting f = SetFormat();
- cout << "BrassPlus Client: " << FullName() << endl;
- cout << "Account Number: " << AcctNum() << endl;
- cout << "Balance: $" << Balance() << endl;
- cout << "Maximum loan: $" << maxLoan << endl;
- cout << "Owed to bank: $" << owesBank << endl;
- cout.precision(3);
- cout << "Loan Rate: " << 100 * rate << "%\n";
- Restore(f);
- }
- void BrassPlus::Withdraw(double amt)
- {
- Formatting f = SetFormat();
- double bal = Balance();
- if (amt <= bal)
- AcctABC::Withdraw(amt);
- else if ( amt <= bal + maxLoan - owesBank)
- {
- double advance = amt - bal;
- owesBank += advance * (1.0 + rate);
- cout << "Bank advance: $" << advance << endl;
- cout << "Finance charge: $" << advance * rate << endl;
- Deposit(advance);
- AcctABC::Withdraw(amt);
- }
- else
- cout << "Credit limit exceeded. Transaction cancelled.\n";
- Restore(f);
- }
|