Skip to content

Commit

Permalink
HMA-2551: Change calculation from canEarnFinalBonus to `finalBonusS… (
Browse files Browse the repository at this point in the history
#9)

* HMA-2551: Change calculation from `canEarnFinalBonus` to `finalBonusStatus`.

- Now return a string of 3 possible status earned/possibleToEarn/cannotEarn.

* Address comment
- change string return type to enum
  • Loading branch information
ngoulongkam authored Apr 6, 2020
1 parent 8ab70d5 commit f946697
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ This will returns an object of type `FinalBonusCalculatorResponse`.
* `totalProjectedSavingsIncludingBonuses: Double`
* `totalProjectedSavings: Double`
* `totalProjectedBonuses: Double`
* `canEarnFinalBonus: Boolean`
* `finalBonusStatus: FinalBonusStatus`

## Validation

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package uk.gov.hmrc.helptosavecalculator

import uk.gov.hmrc.helptosavecalculator.models.FinalBonusInput
import uk.gov.hmrc.helptosavecalculator.models.FinalBonusStatus
import uk.gov.hmrc.helptosavecalculator.utils.monthsSince

internal class FinalBonusTermCalculation {
Expand Down Expand Up @@ -53,6 +54,18 @@ internal class FinalBonusTermCalculation {
it > totalProjectedSavings
} ?: totalProjectedSavings

fun finalBonusStatus(
input: FinalBonusInput,
monthsLeftInScheme: Int,
additionalSavingsThisMonth: Double
): FinalBonusStatus {
val highestPossibleBalance = input.currentBalance + additionalSavingsThisMonth + (monthsLeftInScheme * 50)
return if (input.secondTermBonusEstimate > 0.0) FinalBonusStatus.EARNED else {
if (highestPossibleBalance > input.balanceMustBeMoreThanForBonus)
FinalBonusStatus.POSSIBLE_TO_EARN else FinalBonusStatus.CANNOT_EARN
}
}

fun calculateMonthsLeftInScheme(input: FinalBonusInput): Int {
val thisMonthEndDate = input.thisMonthEndDate.convertToDateTime()
val secondTermEndDate = input.secondTermEndDate.convertToDateTime()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ object FinalBonusTermCalculator {
fun runFinalBonusCalculator(input: FinalBonusInput): FinalBonusCalculatorResponse = calculateFinalBonus(input)

private fun calculateFinalBonus(input: FinalBonusInput): FinalBonusCalculatorResponse {
var canEarnFinalBonus = true
validateUserInput(input.regularPayment)

val monthLeftInScheme = calculation.calculateMonthsLeftInScheme(input)
Expand All @@ -45,16 +44,13 @@ object FinalBonusTermCalculator {
val totalProjectedSavingsIncludingBonuses = calculation.calculateTotalProjectedSavingsIncludeBonuses(
totalProjectedSavings,
totalProjectedBonuses)

if (totalProjectedBonuses == 0.0) {
canEarnFinalBonus = false
}
val finalBonusStatus = calculation.finalBonusStatus(input, monthLeftInScheme, additionalSavingsThisMonth)

return FinalBonusCalculatorResponse(
totalProjectedSavingsIncludingBonuses,
totalProjectedSavings,
totalProjectedBonuses,
canEarnFinalBonus
finalBonusStatus
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,5 @@ data class FinalBonusCalculatorResponse(
val totalProjectedSavingsIncludingBonuses: Double,
val totalProjectedSavings: Double,
val totalProjectedBonuses: Double,
val canEarnFinalBonus: Boolean
val finalBonusStatus: FinalBonusStatus
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright 2020 HM Revenue & Customs
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package uk.gov.hmrc.helptosavecalculator.models

enum class FinalBonusStatus {
EARNED, POSSIBLE_TO_EARN, CANNOT_EARN
}
46 changes: 46 additions & 0 deletions src/commonTest/kotlin/uk/gov/hmrc/FinalBonusTermCalculationTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import kotlin.test.Test
import kotlin.test.assertEquals
import uk.gov.hmrc.helptosavecalculator.FinalBonusTermCalculation
import uk.gov.hmrc.helptosavecalculator.models.FinalBonusInput
import uk.gov.hmrc.helptosavecalculator.models.FinalBonusStatus
import uk.gov.hmrc.helptosavecalculator.models.YearMonthDayInput

class FinalBonusTermCalculationTest {
Expand Down Expand Up @@ -153,4 +154,49 @@ class FinalBonusTermCalculationTest {

assertEquals(11, result)
}

@Test
fun `GIVEN customer already earned final bonus WHEN finalBonusStatus called THEN return earned`() {
val input = FinalBonusInput(10.0,
0.0,
25.0,
YearMonthDayInput(2023, 3),
YearMonthDayInput(2024, 2, 28),
10.0,
5.0)
val calculation = FinalBonusTermCalculation()
val result = calculation.finalBonusStatus(input, 23, 0.0)

assertEquals(FinalBonusStatus.EARNED, result)
}

@Test
fun `GIVEN customer can possibly earn a final bonus WHEN finalBonusStatus called THEN return possibleToEarn`() {
val input = FinalBonusInput(10.0,
0.0,
25.0,
YearMonthDayInput(2023, 3),
YearMonthDayInput(2024, 2, 28),
10.0,
0.0)
val calculation = FinalBonusTermCalculation()
val result = calculation.finalBonusStatus(input, 23, 0.0)

assertEquals(FinalBonusStatus.POSSIBLE_TO_EARN, result)
}

@Test
fun `GIVEN customer can not earn final bonus WHEN finalBonusStatus called THEN return cannotEarn`() {
val input = FinalBonusInput(50.0,
0.0,
0.0,
YearMonthDayInput(2023, 3),
YearMonthDayInput(2024, 2, 28),
1200.0,
0.0)
val calculation = FinalBonusTermCalculation()
val result = calculation.finalBonusStatus(input, 22, 50.0)

assertEquals(FinalBonusStatus.CANNOT_EARN, result)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import kotlin.test.assertFailsWith
import uk.gov.hmrc.helptosavecalculator.FinalBonusTermCalculator.runFinalBonusCalculator
import uk.gov.hmrc.helptosavecalculator.exceptions.InvalidRegularPaymentException
import uk.gov.hmrc.helptosavecalculator.models.FinalBonusInput
import uk.gov.hmrc.helptosavecalculator.models.FinalBonusStatus
import uk.gov.hmrc.helptosavecalculator.models.YearMonthDayInput

class FinalBonusTermCalculatorTest {
Expand Down Expand Up @@ -67,7 +68,7 @@ class FinalBonusTermCalculatorTest {
assertEquals(36.0, calculator.totalProjectedSavingsIncludingBonuses)
assertEquals(24.0, calculator.totalProjectedSavings)
assertEquals(12.0, calculator.totalProjectedBonuses)
assertEquals(true, calculator.canEarnFinalBonus)
assertEquals(FinalBonusStatus.POSSIBLE_TO_EARN, calculator.finalBonusStatus)
}

@Test
Expand All @@ -84,7 +85,7 @@ class FinalBonusTermCalculatorTest {
assertEquals(900.0, calculator.totalProjectedSavingsIncludingBonuses)
assertEquals(600.0, calculator.totalProjectedSavings)
assertEquals(300.0, calculator.totalProjectedBonuses)
assertEquals(true, calculator.canEarnFinalBonus)
assertEquals(FinalBonusStatus.POSSIBLE_TO_EARN, calculator.finalBonusStatus)
}

@Test
Expand All @@ -101,7 +102,7 @@ class FinalBonusTermCalculatorTest {
assertEquals(1800.0, calculator.totalProjectedSavingsIncludingBonuses)
assertEquals(1200.0, calculator.totalProjectedSavings)
assertEquals(600.0, calculator.totalProjectedBonuses)
assertEquals(true, calculator.canEarnFinalBonus)
assertEquals(FinalBonusStatus.POSSIBLE_TO_EARN, calculator.finalBonusStatus)
}

@Test
Expand All @@ -118,7 +119,7 @@ class FinalBonusTermCalculatorTest {
assertEquals(900.0, calculator.totalProjectedSavingsIncludingBonuses)
assertEquals(600.0, calculator.totalProjectedSavings)
assertEquals(300.0, calculator.totalProjectedBonuses)
assertEquals(true, calculator.canEarnFinalBonus)
assertEquals(FinalBonusStatus.EARNED, calculator.finalBonusStatus)
}

@Test
Expand All @@ -135,6 +136,6 @@ class FinalBonusTermCalculatorTest {
assertEquals(25.0, calculator.totalProjectedSavingsIncludingBonuses)
assertEquals(25.0, calculator.totalProjectedSavings)
assertEquals(0.0, calculator.totalProjectedBonuses)
assertEquals(false, calculator.canEarnFinalBonus)
assertEquals(FinalBonusStatus.CANNOT_EARN, calculator.finalBonusStatus)
}
}

0 comments on commit f946697

Please sign in to comment.