-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoans.cpp
More file actions
120 lines (97 loc) · 4.21 KB
/
Copy pathLoans.cpp
File metadata and controls
120 lines (97 loc) · 4.21 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
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
112
113
114
115
116
117
118
119
120
// Dawid Bartosiak
#include <iostream>
#include <cmath>
#include "Loans.h"
FixedInstallmentLoan::FixedInstallmentLoan(Authorisation, const double& aAmount, const double& aInterestRate, int aRatesAmount, int aRatesInYear, const double& creditworthiness) {
interest = aAmount * aInterestRate / (aRatesInYear * (1 - pow((aRatesInYear/(aRatesInYear + aInterestRate)),aRatesAmount)));
if (interest > creditworthiness) throw std::out_of_range("Your creditworthiness is too low. Try more rates or lower loan.");
interestRate = aInterestRate;
ratesAmount = aRatesAmount;
ratesInYear = aRatesInYear;
loanCosts = interest * ratesAmount;
amount = interest * ratesAmount;
negativeCreditworthiness = interest;
}
void FixedInstallmentLoan::overpay(Authorisation, const double& aAmountOfOverpay) {
amount = amount - aAmountOfOverpay;
ratesAmount = ratesAmount - int(aAmountOfOverpay / interest);
interest = amount / ratesAmount;
}
void FixedInstallmentLoan::set_interest_rates(Authorisation, const double& aInterestRate) {
double tempAmount = interest* (ratesInYear * (1 - pow((ratesInYear / (ratesInYear + interestRate)), ratesAmount)) / interestRate);
interest = tempAmount * aInterestRate / (ratesInYear * (1 - pow((ratesInYear / (ratesInYear + aInterestRate)), ratesAmount)));
interestRate = aInterestRate;
tempAmount = amount;
amount = interest * ratesAmount;
loanCosts = loanCosts - tempAmount + amount;
}
DescendingInstallmentLoan::DescendingInstallmentLoan(Authorisation, const double& aAmount, const double& aInterestRate, int aRatesAmount, int aRatesInYear, const double& creditworthiness) {
interest = (double(aAmount / aRatesAmount)) * (1 + aRatesAmount * aInterestRate / aRatesInYear);
if (interest > creditworthiness) throw std::out_of_range("Your creditworthiness is too low. Try more rates or lower loan.");
interestRate = aInterestRate;
ratesAmount = aRatesAmount;
ratesAmountTotal = ratesAmount;
ratesInYear = aRatesInYear;
loanCosts = 0.0;
for (int i = 1; i <= ratesAmount; i++) {
loanCosts += (double(aAmount / aRatesAmount))* (1 + (aRatesAmount-i+1) * aInterestRate / aRatesInYear);
}
amount = loanCosts;
negativeCreditworthiness = interest;
}
void DescendingInstallmentLoan::overpay(Authorisation, const double& aAmountOfOverpay) {
double overpay = aAmountOfOverpay;
double nettoAmount = interest * ratesAmount / (1 + (ratesAmount)*interestRate / ratesInYear);
while (overpay >= interest && ratesAmount > 0) {
overpay -= interest;
interest = (nettoAmount / ratesAmountTotal)* (1 + (ratesAmount) * interestRate / ratesInYear);
ratesAmount--;
}
amount -= aAmountOfOverpay - overpay;
}
void DescendingInstallmentLoan::set_interest_rates(Authorisation, const double& aInterestRate) {
double nettoAmount = interest * ratesAmount / (1 + (ratesAmount)*interestRate / ratesInYear);
interestRate = aInterestRate;
double tempAmount = 0.0;
for (int i = 1; i <= ratesAmount; i++) {
tempAmount += (nettoAmount / ratesAmount) * (1 + (ratesAmount - i + 1) * interestRate / ratesInYear);
}
interest = (nettoAmount / ratesAmount) * (1 + ratesAmount * interestRate / ratesInYear);
loanCosts = loanCosts + tempAmount - amount;
amount = tempAmount;
}
bool Loan::operator>(FixedInstallmentLoan loan){
if (loan.get_loan_costs_total() < loanCosts) { return true; }
return false;
}
bool Loan::operator<(FixedInstallmentLoan loan){
if (loan.get_loan_costs_total() > loanCosts) { return true; }
return false;
}
bool Loan::operator>(DescendingInstallmentLoan loan){
if (loan.get_loan_costs_total() < loanCosts) { return true; }
return false;
}
bool Loan::operator<(DescendingInstallmentLoan loan){
if (loan.get_loan_costs_total() > loanCosts) { return true; }
return false;
}
double Loan::get_negative_creditworthiness(Authorisation) const noexcept {
return negativeCreditworthiness;
}
double Loan::get_interest() const noexcept {
return interest;
}
double Loan::get_loan_costs_total() const noexcept {
return loanCosts;
}
double Loan::get_amount_left() const noexcept {
return amount;
}
int Loan::get_rates_amount() const noexcept {
return ratesAmount;
}
std::ostream& operator<<(std::ostream& os, Loan& loan) noexcept {
os << "Loan with total cost: " << loan.loanCosts << " and current installment: " << loan.interest << std::endl;
return os;
}