lundi 24 septembre 2018

writing formula as a function and apply for each values

I have some MembershipTypes in my applications as follows:

sealed class MembershipType(val label: String) {
    abstract val interestOptions: List<BigDecimal>
    abstract val price: BigDecimal

    object GOLD : MembershipType("Gold") {
        override val price: BigDecimal = BigDecimal(1)
        override val interestOptions: List<BigDecimal> = listOf(BigDecimal(100), BigDecimal(200), BigDecimal(300))
    }

    object SILVER : MembershipType("Silver") {
        override val price: BigDecimal = BigDecimal(0.8)
        override val interestOptions: List<BigDecimal> = listOf(BigDecimal(300), BigDecimal(400), BigDecimal(500))
    }
}

Their properties are one price, and list of interestOptions.

I want to calculate all value (V) / interest(I) calculation combinations and return a list of Quotes List<Quote> using the following formula:

(R^max/R) × V × (1 - I/V) × 0.0015 × P
where 1 <= R <= R^max is the risk score with R^max = 3500, V > 0 is the value, I is the interest, 
and P is price. 

There are other types of MembershipType objects, however the GOLD and SILVER types have a set of pre-defined values so I define them in a QuoteMaker class:

class QuoteMaker {

    private val membershipToValue: MutableMap<MembershipType, List<BigDecimal>> = mutableMapOf()

    init {
        membershipToValue.put(MembershipType.GOLD, listOf(BigDecimal(2500), BigDecimal(5000)))
        membershipToValue.put(MembershipType.SILVER, listOf(BigDecimal(1000), BigDecimal(2000)))
    }


    fun generateQuotes(request1: Request) {

        val risk = request1.riskRate
        val membershipChosen = request1.membershipChosen   //list of MemberShipType selected

        generateMembershipQuote(riskRate, membershipChosen)

    }

    private fun generateMembershipQuote(riskRate: Int, membershipChosen: List<MembershipType>) : List<Quote> {
            //return membershipChosen.stream()
            //.forEach {  }...
    }

}

I receive the R(risk) and the selected list of Membership types in a Request object.

In the generateMembershipQuote(), I need to iterate through all the membership selected and then iterate over the values in membershipToValue for that type and calculate all the combinations for value/interest and return a result.

I want to create the formula as a function and then apply for each value/interest and return a result for that MembershipType. How could I implement this?

A Quote should have the info about the total price, the value and interest used to calculate the total and for which membership

data class Quote(
    val risk: Int, //r
    val value: BigDecimal, //v
    val interest: BigDecimal, //i
    val rate: BigDecimal, //0.0015
    val price: BigDecimal //p

) {

fun sum(): BigDecimal {
   //sum total
}

}

Aucun commentaire:

Enregistrer un commentaire