-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPerson.cpp
More file actions
264 lines (218 loc) · 9.05 KB
/
Copy pathPerson.cpp
File metadata and controls
264 lines (218 loc) · 9.05 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
// Dawid Bartosiak
#include "Person.h"
const std::string FixedInstallment = "fixed";
const std::string DescendingInstallment = "descending";
Person::Person(std::shared_ptr<Bank> aBank, unsigned int aIncome, unsigned int aLivingCost) {
if (aLivingCost <= 0){throw std::out_of_range("Living cost can't be negative or equal 0.");}
bank = std::move(aBank);
income = aIncome;
livingCost = aLivingCost;
creditworthiness = (income - livingCost) / 2;
}
int Person::get_income() { return income; }
int Person::get_living_cost() { return livingCost; }
void Person::set_income(const unsigned int& aIncome) {
income = aIncome;
}
void Person::set_living_cost(const unsigned int& aLivingCost) {
if (aLivingCost <= 0)
{
throw std::out_of_range("Living cost can't be negative or equal 0.");
}
else { livingCost = aLivingCost; }
}
void Person::get_loan(std::string loanType, const double& amount, unsigned int ratesAmount, unsigned int ratesInYear) {
if (amount <= 0 || ratesAmount <= 0 || ratesInYear <= 0)
{
throw std::out_of_range("Any of loan parameter cannot be negative.");
}
else {
if (loanType == FixedInstallment) {
FixedInstallmentLoan loan = bank->give_loan_fixed(getPerson(), amount, ratesAmount, ratesInYear, creditworthiness);
fixed.push_back(loan);
creditworthiness -= loan.get_interest();
}
else if (loanType == DescendingInstallment) {
DescendingInstallmentLoan loan = bank->give_loan_descending(getPerson(), amount, ratesAmount, ratesInYear, creditworthiness);
descending.push_back(loan);
creditworthiness -= loan.get_interest();
}
else throw std::out_of_range("Incorrect loan type.");
}
}
void Person::overpay(std::string loanType, unsigned int numberOfLoan, const double& amount) {
if (amount <= 0)
{
throw std::out_of_range("You can't overpay negative amount of money.");
}
else {
if (loanType == FixedInstallment) {
if (fixed.size() <= numberOfLoan) { throw std::out_of_range("Invalid vector subscript."); }
else {
bank->overpay_fixed(fixed.at(numberOfLoan), amount);
if (fixed.at(numberOfLoan).get_amount_left() <= 0 || fixed.at(numberOfLoan).get_rates_amount() == 0) {
creditworthiness += bank->get_fixed_loan_negative_creditworthiness(fixed.at(numberOfLoan));
fixed.erase(fixed.begin() + numberOfLoan);
}
}
}
else if (loanType == DescendingInstallment) {
if (descending.size() <= numberOfLoan) { throw std::out_of_range("Invalid vector subscript."); }
else {
bank->overpay_descending(descending.at(numberOfLoan), amount);
if (descending.at(numberOfLoan).get_amount_left() <= 0 || descending.at(numberOfLoan).get_rates_amount() == 0) {
creditworthiness += bank->get_descending_loan_negative_creditworthiness(descending.at(numberOfLoan));
descending.erase(descending.begin() + numberOfLoan);
}
}
}
else throw std::out_of_range("Incorrect loan type.");
}
}
double Person::get_interest_total() {
double sum_fixed = 0.0;
if (size(fixed) != 0) {
for (unsigned int i = 0; i <= size(fixed) - 1; ++i)
sum_fixed = sum_fixed + fixed.at(i).get_interest();
double sum_descending = 0.0;
if (size(descending) == 0) { return sum_fixed + sum_descending; }
for (unsigned int i = 0; i <= size(descending) - 1; ++i)
sum_fixed = sum_fixed + descending.at(i).get_interest();
return sum_fixed + sum_descending;
}
else {
double sum_descending = 0.0;
if (size(descending) == 0) { return sum_fixed + sum_descending; }
for (unsigned int i = 0; i <= size(descending) - 1; ++i)
sum_fixed = sum_fixed + descending.at(i).get_interest();
return sum_fixed + sum_descending;
}
}
double Person::get_loan_costs_total() {
double sum_fixed = 0.0;
double sum_descending = 0.0;
if (size(fixed) != 0) {
for (unsigned int i = 0; i <= size(fixed) - 1; ++i)
sum_fixed = sum_fixed + fixed.at(i).get_loan_costs_total();
if (size(descending) == 0) { return sum_fixed + sum_descending; }
for (unsigned int i = 0; i <= size(descending) - 1; ++i)
sum_fixed = sum_fixed + descending.at(i).get_loan_costs_total();
return sum_fixed + sum_descending;
}
else {
if (size(descending) == 0) { return sum_fixed + sum_descending; }
for (unsigned int i = 0; i <= size(descending) - 1; ++i)
sum_fixed = sum_fixed + descending.at(i).get_loan_costs_total();
return sum_fixed + sum_descending;
}
}
double Person::get_amount_total_left() {
double sum_fixed = 0.0;
double sum_descending = 0.0;
if (size(fixed) != 0) {
for (unsigned int i = 0; i <= size(fixed) - 1; ++i)
sum_fixed = sum_fixed + fixed.at(i).get_amount_left();
if (size(descending) == 0) { return sum_fixed + sum_descending; }
for (unsigned int i = 0; i <= size(descending) - 1; ++i)
sum_fixed = sum_fixed + descending.at(i).get_amount_left();
return sum_fixed + sum_descending;
}
else {
if (size(descending) == 0) { return sum_fixed + sum_descending; }
for (unsigned int i = 0; i <= size(descending) - 1; ++i)
sum_fixed = sum_fixed + descending.at(i).get_amount_left();
return sum_fixed + sum_descending;
}
}
double Person::get_creditworthiness() { return creditworthiness; }
double Person::get_interest_single(std::string loanType, unsigned int numberOfLoan) {
if (loanType == FixedInstallment) {
if (fixed.size() <= numberOfLoan) { throw std::out_of_range("Invalid vector subscript."); }
else { return fixed.at(numberOfLoan).get_interest(); }
}
else if (loanType == DescendingInstallment) {
if (fixed.size() <= numberOfLoan) { throw std::out_of_range("Invalid vector subscript."); }
else { return descending.at(numberOfLoan).get_interest(); }
}
else throw std::out_of_range("Incorrect loan type.");
}
int Person::get_rates_amount(std::string loanType, unsigned int numberOfLoan) {
if (loanType == FixedInstallment) {
if (fixed.size() <= numberOfLoan) { throw std::out_of_range("Invalid vector subscript."); }
else { return fixed.at(numberOfLoan).get_rates_amount(); }
}
else if (loanType == DescendingInstallment) {
if (fixed.size() <= numberOfLoan) { throw std::out_of_range("Invalid vector subscript."); }
else { return descending.at(numberOfLoan).get_rates_amount(); }
}
else throw std::out_of_range("Incorrect loan type.");
}
double Person::get_negative_creditworthines(std::string loanType, unsigned int numberOfLoan) {
if (loanType == FixedInstallment) {
if (fixed.size() <= numberOfLoan) { throw std::out_of_range("Invalid vector subscript."); }
else { return bank->get_fixed_loan_negative_creditworthiness(fixed.at(numberOfLoan)); }
}
else if (loanType == DescendingInstallment) {
if (fixed.size() <= numberOfLoan) { throw std::out_of_range("Invalid vector subscript."); }
else { return bank->get_descending_loan_negative_creditworthiness(descending.at(numberOfLoan)); }
}
else throw std::out_of_range("Incorrect loan type.");
}
double Person::get_loan_costs_single(std::string loanType, unsigned int numberOfLoan) {
if (loanType == FixedInstallment) {
if (fixed.size() <= numberOfLoan) { throw std::out_of_range("Invalid vector subscript."); }
else { return fixed.at(numberOfLoan).get_loan_costs_total(); }
}
else if (loanType == DescendingInstallment) {
if (fixed.size() <= numberOfLoan) { throw std::out_of_range("Invalid vector subscript."); }
else { return descending.at(numberOfLoan).get_loan_costs_total(); }
}
else throw std::out_of_range("Incorrect loan type.");
}
double Person::get_amount_single_left(std::string loanType, unsigned int numberOfLoan) {
if (loanType == FixedInstallment) {
if (fixed.size() <= numberOfLoan) { throw std::out_of_range("Invalid vector subscript."); }
else { return fixed.at(numberOfLoan).get_amount_left(); }
}
else if (loanType == DescendingInstallment) {
if (fixed.size() <= numberOfLoan) { throw std::out_of_range("Invalid vector subscript."); }
else { return descending.at(numberOfLoan).get_amount_left(); }
}
else throw std::out_of_range("Incorrect loan type.");
}
std::vector<DescendingInstallmentLoan>& Person::get_descending() { return descending; }
std::vector<FixedInstallmentLoan>& Person::get_fixed() { return fixed; }
unsigned int Person::get_longest_rates() {
unsigned int longestRates = 0;
unsigned int rates = 0;
if (size(fixed) != 0) {
for (unsigned int i = 0; i <= size(fixed) - 1; ++i) {
rates = fixed.at(i).get_rates_amount();
if (rates > longestRates) longestRates = rates;
}
if (size(descending) == 0) { return longestRates; }
for (unsigned int i = 0; i <= size(descending) - 1; ++i) {
rates = descending.at(i).get_rates_amount();
if (rates > longestRates) longestRates = rates;
}
return longestRates;
}
else {
if (size(descending) == 0) { return longestRates; }
for (unsigned int i = 0; i <= size(descending) - 1; ++i) {
rates = descending.at(i).get_rates_amount();
if (rates > longestRates) longestRates = rates;
}
return longestRates;
}
}
void Person::operator+(FixedInstallmentLoan loan) {
if (creditworthiness - loan.get_interest() < 0) { return; }
creditworthiness -= loan.get_interest();
fixed.push_back(loan);
}
void Person::operator+(DescendingInstallmentLoan loan) {
if (creditworthiness - loan.get_interest() < 0) { return; }
creditworthiness -= loan.get_interest();
descending.push_back(loan);
}