Skip to content

Commit

Permalink
feature #818 Implement loan calculator.
Browse files Browse the repository at this point in the history
  • Loading branch information
toastkidjp committed Dec 27, 2021
1 parent 0eebb02 commit b4cd58c
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions loan/src/main/java/jp/toastkid/loan/Calculator.kt
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
}

}

0 comments on commit b4cd58c

Please sign in to comment.