Skip to content

Commit

Permalink
chapter13 ABC
Browse files Browse the repository at this point in the history
  • Loading branch information
hzchenxiaobin committed May 7, 2023
1 parent 16bd4c5 commit 7407a4e
Show file tree
Hide file tree
Showing 4 changed files with 195 additions and 0 deletions.
Binary file added chapter13/acctabc/a.out
Binary file not shown.
82 changes: 82 additions & 0 deletions chapter13/acctabc/acctabc.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#include <iostream>
#include "acctabc.h"

using namespace std;

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 is not allowed!" << endl;
else
balance += amt;
}

void AcctABC::Withdraw(double amt)
{
balance -= amt;
}

void Brass::Withdraw(double amt)
{
if (amt < 0)
cout << "Withdraw amount must be positive." << endl;
else if (amt <= Balance())
AcctABC::Withdraw(amt);
else
cout << "Withdraw amount exceeded your balance!" << endl;
}

void Brass::ViewAcct() const
{
cout << "Client: " << FullName() << endl;
cout << "Account number: " << AcctNum() << endl;
cout << "Balance: $" << Balance() << endl;
}

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 &br, double ml, double r) : AcctABC(br)
{
maxLoan = ml;
rate = r;
}

void BrassPlus::ViewAcct() const
{
cout << "Client: " << FullName() << endl;
cout << "Account number: " << AcctNum() << endl;
cout << "Balance: $" << Balance() << endl;
cout << "Maxium load: $" << maxLoan << endl;
cout << "Loan Rate: " << rate << endl;
cout << "Owed to bank: $" << owesBank << endl;
}

void BrassPlus::Withdraw(double amt)
{
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";
}
55 changes: 55 additions & 0 deletions chapter13/acctabc/acctabc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#ifndef __ACCTABC_H__
#define __ACCTABC_H__

#include <iostream>
#include <cstring>

using namespace std;

class AcctABC
{
private:
string fullName;
long acctNum;
double balance;

protected:
const string &FullName() const { return fullName; };
long AcctNum() const { return acctNum; };

public:
AcctABC(const string &s = "Nullbody", long an = -1, double bal = 0.0);
void Deposit(double amt);
virtual void Withdraw(double amt) = 0;
double Balance() const { return balance; };
virtual void ViewAcct() const = 0;
virtual ~AcctABC(){};
};

class Brass : public AcctABC
{
public:
Brass(const string &s = "Nullbody", long an = -1, double bal = 0.0) : AcctABC(s, an, bal){};
virtual void Withdraw(double amt);
virtual void ViewAcct() const;
virtual ~Brass(){};
};

class BrassPlus : public AcctABC
{
private:
double maxLoan;
double rate;
double owesBank;

public:
BrassPlus(const string &s = "Nullbody", long an = -1, double bal = 0.0, double ml = 500, double r = 0.11125);
BrassPlus(const Brass &br, double ml = 500, double r = 0.11125);
virtual void ViewAcct() const;
virtual void Withdraw(double amt);
void ResetMax(double m) { maxLoan = m; };
void ResetRate(double r) { rate = r; };
void ResetOwes() { owesBank = 0; };
};

#endif
58 changes: 58 additions & 0 deletions chapter13/acctabc/usebrass3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include <iostream>
#include "acctabc.h"

using namespace std;

const int CLIENTS = 3;

int main()
{
AcctABC *p_clients[CLIENTS];

string temp;
long tempnum;
double tempbal;
char kind;
for (int i = 0; i < CLIENTS; i++)
{
cout << "Enter the client's name: ";
getline(cin, temp);
cout << "Enter client's account number: ";
cin >> tempnum;
cout << "Enter opening balance: $";
cin >> tempbal;
cout << "Enter 1 for Brass Account or 2 for BrassPlus Account: ";
while (cin >> kind && (kind != '1' && kind != '2'))
cout << "Enter either 1 or 2: ";
if (kind == '1')
p_clients[i] = new Brass(temp, tempnum, tempbal);
else
{
double tmax, trate;
cout << "Enter the overdraft limit: $";
cin >> tmax;
cout << "Enter the rate: ";
cin >> trate;
p_clients[i] = new BrassPlus(temp, tempnum, tempbal, tmax, trate);
}
while (cin.get() != '\n')
continue;
}

cout << endl;

for (int i = 0; i < CLIENTS; i++)
{
p_clients[i]->ViewAcct();
cout << endl;
}

for (int i = 0; i < CLIENTS; i++)
{
delete p_clients[i];
}

cout << "Done.\n";

return 0;
}

0 comments on commit 7407a4e

Please sign in to comment.