-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrass.cpp
111 lines (100 loc) · 2.45 KB
/
brass.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// brass.cpp -- bank account class methods
#include <iostream>
#include <cstring>
#include "brass.h"
using std::cout;
using std::ios_base;
using std::endl;
// Brass methods
Brass::Brass (const char *s, long an, double bal)
{
std::strncpy (fullName, s, MAX - 1);
fullName[MAX - 1] = '\0';
acctNum = an;
balance = bal;
}
void Brass::Deposit (double amt)
{
if (amt < 0) {
cout << "Negative deposit not allowed: "
<< "deposit is cancelled.\n";
} else {
balance += amt;
}
}
void Brass::Withdraw (double amt)
{
if (amt < 0) {
cout << "Withdrawal amount must be positive: "
<< "withdrawal cancelled.\n";
} else if (amt <= balance) {
balance -= amt;
} else {
cout << "Withdrawal amount of $" << amt
<< " exceeds your balance.\n"
<< "Withdrawal canceled.\n";
}
}
double Brass::Balance()const
{
return balance;
}
void Brass::ViewAcct()const
{
ios_base::fmtflags initialState =
cout.setf(ios_base::fixed, ios_base::floatfield);
cout.setf (ios_base::showpoint);
cout.precision(2);
cout << "Client: " << fullName << endl;
cout << "Account Number: " << acctNum << endl;
cout << "Balance: $" << balance << endl;
cout.setf(initialState); // restore original format
}
// BrassPlus Methods
BrassPlus::BrassPlus(const char *s, long an, double bal,
double ml, double r) : Brass (s, an, bal)
{
maxLoan = ml;
ownsBank = 0.0;
rate = r;
}
BrassPlus::BrassPlus(const Brass & ba, double ml, double r)
: Brass (ba) // uses implicit copy cosntructor
{
maxLoan = ml;
ownsBank = 0.0;
rate = r;
}
void BrassPlus::ViewAcct()const
{
ios_base::fmtflags initialState =
cout.setf(ios_base::fixed, ios_base::floatfield);
cout.setf(ios_base::showpoint);
cout.precision (2);
Brass::ViewAcct();
cout << "Maximum load: $" << maxLoan << endl;
cout << "Owed to bank: $" << ownsBank << endl;
cout << "Load Rate: " << 100 * rate << "%\n";
cout.setf(initialState);
}
void BrassPlus::Withdraw (double amt)
{
ios_base::fmtflags initialState =
cout.setf(ios_base::fixed, ios_base::floatfield);
cout.setf(ios_base::showpoint);
cout.precision(2);
double bal = Balance();
if (amt <= bal) {
Brass::Withdraw(amt);
} else if (amt <= bal + maxLoan - ownsBank) {
double advance = amt - bal;
ownsBank += advance * (1.0 * rate);
cout << "Bank advance: $" << advance << endl;
cout << "Finance charge: $" << advance * rate << endl;
Deposit(advance);
Brass::Withdraw(amt);
} else {
cout << "Credit limit exceeded. Transaction cancelled.\n";
}
cout.setf(initialState);
}