-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCD.cpp
More file actions
66 lines (58 loc) · 2.06 KB
/
CD.cpp
File metadata and controls
66 lines (58 loc) · 2.06 KB
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
// CDAccount.cpp
#include "CD.h"
#include <stdexcept>
#include <iostream>
#include <cmath> // For pow function
// CDAccount class methods
CD::CD(int account_number, double balance, int termMonths)
: Account(account_number, balance), termMonths(termMonths) {
interestRate = calculateInterestRate();
maturityReached = false;
time_t currentTime = time(0);
tm* timeStruct = localtime(¤tTime);
timeStruct->tm_mon += termMonths;
mktime(timeStruct); // Normalize the tm structure
time_agreement = mktime(timeStruct);
}
static const double CD::calculateInterestRate() const {
if (termMonths == 3) return CD_3_MON; // 2.5% for 3 months
else if (termMonths == 6) return CD_6_MON; // 3% for 6 months
else if (termMonths == 12) return CD_12_MON; // 5% for 12 months
return 0.0;
}
bool CD::isMaturityDatePassed() const {
if(difftime(time(0), time_agreement) > 0 && !maturityReached) {
balance += calculateInterest();
maturityReached = true;
}
return difftime(time(0), time_agreement) > 0;
}
double Cd::calculateInterest() {
double years = static_cast<double>(termMonths) / 12.0;
double interest = balance * pow(1 + interestRate, years) - balance;
return interest;
}
void CD::withdraw(double amount) {
if (!isMaturityDatePassed()) {
throw runtime_error("Cannot withdraw: Maturity date has not passed yet.");
}
if (amount > balance) {
throw runtime_error("Insufficient funds for withdrawal.");
}
balance -= amount;
}
void CD::transfer(double amount, Account* recipient) {
if (!isMaturityDatePassed()) {
throw runtime_error("Cannot transfer: Maturity date has not passed yet.");
}
if (amount > balance) {
throw runtime_error("Insufficient funds for transfer.");
}
balance -= amount;
toAccount.balance += amount;
}
void CD::printAccount() const {
cout << "Term: " << termMonths << " months | Interest Rate: "
<< interestRate * 100 << "% | Maturity Date: "
<< put_time(localtime(&time_agreement), "%c") << endl;
}