This commit is contained in:
@@ -24,10 +24,7 @@ jobs:
|
||||
distribution: 'temurin'
|
||||
cache: maven
|
||||
- name: Build with Maven
|
||||
run: mvn -B clean package
|
||||
- run: |
|
||||
echo "💡 The ${{ gitea.repository }} repository has been cloned to the runner."
|
||||
echo "🖥️ The workflow is now ready to test your code on the runner."
|
||||
run: mvn -B package
|
||||
- name: List files in the repository
|
||||
run: |
|
||||
ls ${{ gitea.workspace }}
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
package dev.darktale
|
||||
|
||||
import dev.darktale.model.Rate
|
||||
import java.math.BigDecimal
|
||||
import java.time.LocalDate
|
||||
|
||||
class AmountByRateCalculator {
|
||||
private val leapYearDuration = BigDecimal.valueOf(366L)
|
||||
private val nonLeapYearDuration = BigDecimal.valueOf(365L)
|
||||
fun caluculate(amount: BigDecimal, rate: Rate, dateFrom: LocalDate, dateTo: LocalDate): BigDecimal {
|
||||
var result = amount
|
||||
var curRate = rate.rateOnDate(dateFrom)
|
||||
while (curRate != null && curRate.since < dateTo) {
|
||||
val curDateFrom = max(curRate.since, dateFrom)
|
||||
val curDateTo = min(dateTo, curRate.till)
|
||||
result *= (BigDecimal.ONE + (curRate.rate!!.setScale(20) *
|
||||
(curDateTo.dayOfYear - curDateFrom.dayOfYear + 1).toBigDecimal() /
|
||||
(if (curDateFrom.isLeapYear) leapYearDuration else nonLeapYearDuration)))
|
||||
curRate = rate.rateOnDate(curDateTo.plusDays(1))?.let {
|
||||
if (it === curRate) null else it
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
private fun max(localDate1: LocalDate, localDate2: LocalDate?) =
|
||||
if (localDate2 == null || localDate1 >= localDate2) localDate1 else localDate2
|
||||
|
||||
private fun min(localDate1: LocalDate, localDate2: LocalDate?) =
|
||||
if (localDate2 == null || localDate1 <= localDate2) localDate1 else localDate2
|
||||
@@ -0,0 +1,32 @@
|
||||
package dev.darktale.model
|
||||
|
||||
import java.math.BigDecimal
|
||||
import java.time.LocalDate
|
||||
import java.util.*
|
||||
|
||||
sealed class Rate {
|
||||
open val rate: BigDecimal? = null
|
||||
abstract val since: LocalDate
|
||||
open val till: LocalDate? = null
|
||||
abstract fun rateOnDate(date: LocalDate): Rate?
|
||||
}
|
||||
|
||||
class SimpleRate(
|
||||
override val rate: BigDecimal,
|
||||
override val since: LocalDate,
|
||||
override val till: LocalDate? = null,
|
||||
) : Rate() {
|
||||
override fun rateOnDate(date: LocalDate) =
|
||||
if (date >= since && (till == null || date <= till)) this else null
|
||||
}
|
||||
|
||||
class PeriodicRate(
|
||||
private val rates: TreeMap<LocalDate, Rate>
|
||||
) : Rate() {
|
||||
override val since: LocalDate
|
||||
get() = rates.firstKey()
|
||||
override fun rateOnDate(date: LocalDate) =
|
||||
rates.floorEntry(date)?.value?.let {
|
||||
if (it.till == null || it.till!! >= date) it else null
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package dev.darktale
|
||||
|
||||
import dev.darktale.model.Rate
|
||||
import dev.darktale.model.SimpleRate
|
||||
import org.junit.jupiter.api.extension.ExtensionContext
|
||||
import org.junit.jupiter.params.ParameterizedTest
|
||||
import org.junit.jupiter.params.provider.Arguments
|
||||
import org.junit.jupiter.params.provider.ArgumentsProvider
|
||||
import org.junit.jupiter.params.provider.ArgumentsSource
|
||||
import java.math.BigDecimal
|
||||
import java.time.LocalDate
|
||||
import java.util.stream.Stream
|
||||
import kotlin.math.abs
|
||||
import kotlin.test.DefaultAsserter.assertTrue
|
||||
|
||||
class AmountByRateCalculatorTest {
|
||||
val amountByRateCalculator = AmountByRateCalculator()
|
||||
|
||||
@ParameterizedTest
|
||||
@ArgumentsSource(RateProvider::class)
|
||||
fun testRateCalculator(
|
||||
rate: Rate,
|
||||
sourceAmount: BigDecimal,
|
||||
dateFrom: LocalDate,
|
||||
dateTo: LocalDate,
|
||||
expectedAmount: BigDecimal
|
||||
) {
|
||||
val result = amountByRateCalculator.caluculate(sourceAmount, rate, dateFrom, dateTo)
|
||||
assertTrue(
|
||||
"expected amount was $expectedAmount ± 0.0001, but actual is $result",
|
||||
abs((expectedAmount - result).toDouble()) <= 0.0001,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
class RateProvider : ArgumentsProvider {
|
||||
override fun provideArguments(context: ExtensionContext): Stream<out Arguments> {
|
||||
val startOfYear = LocalDate.of(2025, 1, 1)
|
||||
val endOfYear = LocalDate.of(2025, 12, 31)
|
||||
val middleOfYear = startOfYear.plusDays((endOfYear.dayOfYear / 2).toLong())
|
||||
val simpleRate = SimpleRate(BigDecimal.valueOf(0.1), startOfYear, endOfYear)
|
||||
return Stream.of(
|
||||
Arguments.of(simpleRate, BigDecimal.valueOf(1000L), startOfYear, endOfYear, BigDecimal.valueOf(1100L)),
|
||||
Arguments.of(simpleRate, BigDecimal.valueOf(1000L), startOfYear, middleOfYear, BigDecimal.valueOf(1050.137)),
|
||||
)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user