Skip to content

Commit 7900f3f

Browse files
committed
1 parent c27d9c1 commit 7900f3f

File tree

3 files changed

+60
-20
lines changed

3 files changed

+60
-20
lines changed
Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,58 @@
11
# [Financing a purchase](https://www.codewars.com/kata/financing-a-purchase "https://www.codewars.com/kata/59c68ea2aeb2843e18000109")
22

3-
The description is rather long but it tries to explain what a financing plan is.
3+
The description is rather long, but it tries to explain what a financing plan is.
44

5-
The fixed monthly payment for a fixed rate mortgage is the amount paid by the borrower every month that ensures
6-
that the loan is paid off in full with interest at the end of its term.
5+
The fixed monthly payment for a fixed rate mortgage is the amount paid by the borrower every month that ensures
6+
that the loan is paid off in full, with interest at the end of its term.
77

8-
The monthly payment formula is based on the annuity formula.
8+
The monthly payment formula is based on the annuity formula.
99
The monthly payment `c` depends upon:
1010

11-
- `rate` - the monthly interest rate is expressed as a decimal, not a percentage.
12-
The monthly rate is simply the **given** yearly percentage rate divided by 100 and then by 12.
11+
- `rate` - the monthly interest rate is expressed as a decimal, not a percentage.
12+
The monthly rate is simply the **given** yearly percentage rate divided by 100 and then by 12.
1313

1414
- `term` - the number of monthly payments, called the loan's `term`.
1515
- `principal` - the amount borrowed, known as the loan's principal (or `balance`).
1616

1717
First we have to determine `c`.
1818

19-
We have: `c = n /d` with `n = r LICENSE build.gradle.kts docs gradle gradle.properties gradlew gradlew.bat kata settings.gradle.kts balance` and `d = 1 - (1 + r)**(-term)` where `**` is the `power` function (you can look at the reference below).
19+
We have: `c = n /d` with `n = r LICENSE build.gradle.kts docs gradle gradle.properties gradlew gradlew.bat kata settings.gradle.kts balance`
20+
and `d = 1 - (1 + r)**(-term)` where `**` is the `power` function (you can look at the reference below).
2021

2122
The payment `c` is composed of two parts. The first part pays the interest (let us call it `int`)
22-
due for the balance of the given month, the second part repays the balance (let us call this part `princ`) hence for the following month we get a `new balance = old balance - princ` with `c = int + princ`.
23+
due for the balance of the given month, the second part repays the balance (let us call this part `princ`) hence for the following month we
24+
get a `new balance = old balance - princ` with `c = int + princ`.
2325

24-
Loans are structured so that the amount of principal returned to the borrower starts out small and increases with each mortgage payment.
25-
While the mortgage payments in the first years consist primarily of interest payments, the payments in the final years consist primarily of principal repayment.
26+
Loans are structured so that the amount of principal returned to the borrower starts out small and increases with each mortgage payment.
27+
While the mortgage payments in the first years consist primarily of interest payments, the payments in the final years consist primarily of
28+
principal repayment.
2629

27-
A mortgage's amortization schedule provides a detailed look at precisely what portion of each mortgage payment is dedicated to each component.
30+
A mortgage's amortization schedule provides a detailed look at precisely what portion of each mortgage payment is dedicated to each
31+
component.
2832

2933
In an example of a $100,000, 30-year mortgage with a rate of 6 percents the amortization schedule consists of 360 monthly payments.
3034
The partial amortization schedule below shows with 2 decimal floats
3135
the balance between principal and interest payments.
3236

33-
--|num_payment|c |princ |int |Balance |
34-
--|-----------|-----------|-----------|-----------|-----------|
35-
--|1 |599.55 |99.55 |500.00 |99900.45 |
36-
--|... |599.55 |... |... |... |
37-
--|12 |599.55 |105.16 |494.39 |98,771.99 |
38-
--|... |599.55 |... |... |... |
39-
--|360 |599.55 |596.57 |2.98 |0.00 |
37+
| -- | num_payment | c | princ | int | Balance |
38+
|----|-------------|--------|--------|--------|-----------|
39+
| -- | 1 | 599.55 | 99.55 | 500.00 | 99900.45 |
40+
| -- | ... | 599.55 | ... | ... | ... |
41+
| -- | 12 | 599.55 | 105.16 | 494.39 | 98,771.99 |
42+
| -- | ... | 599.55 | ... | ... | ... |
43+
| -- | 360 | 599.55 | 596.57 | 2.98 | 0.00 |
4044

4145
#### Task:
42-
Given parameters
46+
47+
Given parameters
48+
4349
```
4450
rate: annual rate as percent (don't forgent to divide by 100*12)
4551
bal: original balance (borrowed amount)
4652
term: number of monthly payments
4753
num_payment: rank of considered month (from 1 to term)
4854
```
55+
4956
the function `amort` will return a formatted string (for example):
5057

5158
`"num_payment %d c %.0f princ %.0f int %.0f balance %.0f" (with arguments num_payment, c, princ, int, balance`)
@@ -54,6 +61,7 @@ the function `amort` will return a formatted string (for example):
5461
return a list with num-payment, c, princ, int, balance each rounded.
5562

5663
#### Examples:
64+
5765
```
5866
amort(6, 100000, 360, 1) ->
5967
"num_payment 1 c 600 princ 100 int 500 balance 99900"
@@ -62,5 +70,7 @@ amort(6, 100000, 360, 12) ->
6270
"num_payment 12 c 600 princ 105 int 494 balance 98772"
6371
6472
```
73+
6574
#### Ref
66-
<https://en.wikipedia.org/wiki/Mortgage_calculator>
75+
76+
<https://en.wikipedia.org/wiki/Mortgage_calculator>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
interface Finance {
2+
static String amort(double rate, double bal, int term, int numPayment) {
3+
rate /= 1200;
4+
double c = rate * bal / (1 - Math.pow(1 + rate, -term));
5+
double principal = (c - bal * rate) * Math.pow(1 + rate, numPayment - 1);
6+
return String.format("num_payment %d c %.0f princ %.0f int %.0f balance %.0f",
7+
numPayment,
8+
c,
9+
principal,
10+
c - principal,
11+
bal * (1 - (Math.pow(1 + rate, numPayment) - 1) / (Math.pow(1 + rate, term) - 1)));
12+
}
13+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import static org.junit.jupiter.api.Assertions.assertEquals;
2+
3+
import org.junit.jupiter.params.ParameterizedTest;
4+
import org.junit.jupiter.params.provider.CsvSource;
5+
6+
class FinanceTest {
7+
@ParameterizedTest
8+
@CsvSource(textBlock = """
9+
7.4, 10215, 24, 20, num_payment 20 c 459 princ 445 int 14 balance 1809
10+
7.9, 107090, 48, 41, num_payment 41 c 2609 princ 2476 int 133 balance 17794
11+
6.8, 105097, 36, 4, num_payment 4 c 3235 princ 2685 int 550 balance 94447
12+
3.8, 48603, 24, 10, num_payment 10 c 2106 princ 2009 int 98 balance 28799
13+
""")
14+
void sample(double rate, int bal, int term, int numPayments, String expected) {
15+
assertEquals(expected, Finance.amort(rate, bal, term, numPayments));
16+
}
17+
}

0 commit comments

Comments
 (0)