System of bonuses

Report a typo

Our new customer asked us to write a system of bonuses for his store customers with the possibility of using various bonus-calculation strategies.

To implement the project, we've created:
1. The BonusCalculator interface, which contains the following function:

  • calculateBonus(spend: Double): Double – it calculates the number of bonus points awarded depending on the amount spent.

2. Two classes that implement the BonusCalculator interface:

  • FlatRateBonusCalculator – it gives a fixed number of bonus points for every $100 spent.
  • PercentageBonusCalculator – it gives bonus points as a percentage of the amount spent.

You need to create the Customer class, which uses class delegation to calculate bonuses using BonusCalculator.

Write a program in Kotlin
interface BonusCalculator {
fun calculateBonus(spend: Double): Double
}

class FlatRateBonusCalculator(private val rate: Double) : BonusCalculator {
override fun calculateBonus(spend: Double): Double {
return (spend / 100.0).toInt() * rate
}
}

class PercentageBonusCalculator(private val percentage: Double) : BonusCalculator {
override fun calculateBonus(spend: Double): Double {
return spend * percentage / 100.0
}
}

class Customer(val name: String, bonusCalculator: BonusCalculator) // make your code here
___

Create a free account to access the full topic