-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #818 Implement loan calculator.
- Loading branch information
1 parent
0eebb02
commit b4cd58c
Showing
1 changed file
with
34 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Copyright (c) 2021 toastkidjp. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompany this distribution. | ||
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html. | ||
*/ | ||
|
||
package jp.toastkid.loan | ||
|
||
import kotlin.math.max | ||
import kotlin.math.pow | ||
|
||
class Calculator { | ||
|
||
operator fun invoke( | ||
amount: Int, | ||
term: Int, | ||
interestRate: Double, | ||
downPayment: Int, | ||
managementFee: Int, | ||
renovationReserves: Int | ||
): Int { | ||
val paymentCount = (term * 12).toDouble() | ||
val convertedRate = interestRate / 100.0 | ||
val poweredMonthlyInterestRate = (1 + convertedRate / 12).pow(paymentCount) | ||
|
||
val numerator = ((amount - downPayment) * convertedRate) / 12 * poweredMonthlyInterestRate | ||
val denominator = poweredMonthlyInterestRate - 1 | ||
|
||
return (max(numerator / denominator, 0.0)).toInt() + managementFee + renovationReserves | ||
} | ||
|
||
} |