File: Mortgage.cpp

package info (click to toggle)
endless-sky 0.10.16-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 414,608 kB
  • sloc: cpp: 73,435; python: 893; xml: 666; sh: 271; makefile: 28
file content (216 lines) | stat: -rw-r--r-- 5,112 bytes parent folder | download
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
/* Mortgage.cpp
Copyright (c) 2014 by Michael Zahniser

Endless Sky is free software: you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later version.

Endless Sky is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with
this program. If not, see <https://www.gnu.org/licenses/>.
*/

#include "Mortgage.h"

#include "DataNode.h"
#include "DataWriter.h"

#include <algorithm>
#include <cmath>

using namespace std;

namespace {
	const int MORTGAGE_TERM = 365;
}



// Find out how much you can afford to borrow with the given annual revenue
// and the given credit score (which should be between 200 and 800).
int64_t Mortgage::Maximum(int64_t annualRevenue, int creditScore, double currentPayments)
{
	const double revenue = annualRevenue - MORTGAGE_TERM * currentPayments;
	if(revenue <= 0.)
		return 0;

	const double interest = (600 - creditScore / 2) * .00001;
	const double power = pow(1. + interest, MORTGAGE_TERM);
	const double multiplier = interest * MORTGAGE_TERM * power / (power - 1.);
	return static_cast<int64_t>(max(0., revenue / multiplier));
}



// Create a new mortgage of the given amount.
Mortgage::Mortgage(string type, int64_t principal, int creditScore, int term)
	: type(std::move(type)),
	principal(principal),
	interest((600 - creditScore / 2) * .00001),
	interestString("0." + to_string(600 - creditScore / 2) + "%"),
	term(term)
{
}



// Create a mortgage with a specific interest rate instead of using the player's
// credit score. Due to how the class is set up, the interest rate must currently
// be within the range [0, 1).
Mortgage::Mortgage(string type, int64_t principal, double interest, int term)
	: type(std::move(type)),
	principal(principal),
	interest(interest * .01),
	interestString("0." + to_string(static_cast<int>(1000. * interest)) + "%"),
	term(term)
{
}



// Construct and Load() at the same time.
Mortgage::Mortgage(const DataNode &node)
{
	Load(node);
}



// Load or save mortgage data.
void Mortgage::Load(const DataNode &node)
{
	if(node.Size() >= 2)
		type = node.Token(1);
	else
		type = "Mortgage";

	for(const DataNode &child : node)
	{
		const string &key = child.Token(0);
		bool hasValue = child.Size() >= 2;
		if(key == "principal" && hasValue)
			principal = child.Value(1);
		else if(key == "interest" && hasValue)
		{
			interest = child.Value(1);
			int f = 100000. * interest;
			interestString = "0." + to_string(f) + "%";
		}
		else if(key == "term" && hasValue)
			term = max(1., child.Value(1));
	}
}



void Mortgage::Save(DataWriter &out) const
{
	out.Write("mortgage", type);
	out.BeginChild();
	{
		out.Write("principal", principal);
		out.Write("interest", interest);
		out.Write("term", term);
	}
	out.EndChild();
}



// Make a mortgage payment. The return value is the amount paid.
int64_t Mortgage::MakePayment()
{
	int64_t payment = Payment();
	MissPayment();
	principal -= payment;
	--term;

	return payment;
}



void Mortgage::MissPayment()
{
	principal += lround(principal * interest);
}



// Pay down additional principal. Unlike a "real" mortgage, this reduces
// the minimum amount of your future payments, not the term of the mortgage.
// This returns the actual amount paid, which may be less if the total
// principal remaining is less than the given amount.
int64_t Mortgage::PayExtra(int64_t amount)
{
	amount = min(principal, amount);
	principal -= amount;
	return amount;
}



// The type is "Mortgage" if this is a mortgage you applied for from a bank,
// "Fine" if this is a fine imposed on you for illegal activities, and
// "Debt" if this is debt given to you by a mission.
const string &Mortgage::Type() const
{
	return type;
}



// Get the remaining mortgage principal.
int64_t Mortgage::Principal() const
{
	return principal;
}



// Get the interest rate. It is formatted as a string, because all that the
// program will ever do with this is display it.
const string &Mortgage::Interest() const
{
	return interestString;
}



// Get the remaining number of payments that must be made.
int Mortgage::Term() const
{
	return term;
}



// Check the amount of the next payment due (rounded to the nearest credit).
int64_t Mortgage::Payment() const
{
	if(!term)
		return principal;
	if(!interest)
		return lround(static_cast<double>(principal) / term);

	// Always require every payment to be at least 1 credit.
	double power = pow(1. + interest, term);
	return max<int64_t>(1, lround(principal * interest * power / (power - 1.)));
}



// Check the amount of the next payment due.
double Mortgage::PrecisePayment() const
{
	if(!term)
		return principal;
	if(!interest)
		return static_cast<double>(principal) / term;

	const double power = pow(1. + interest, term);
	return principal * interest * power / (power - 1.);
}