In this chapter, we will continue to develop the software which will calculate the total amount of all the students in a class that need to pay for all the Kotlin related books. In the previous chapter, we have passed in the map object into the function of the FilterBook singleton object and then calculate the total amount each student needs to pay for the Kotlin related books. In this chapter, we will create a new enum class object which will only contain the Kotlin related books and their price which then will get passed into the same function to calculate the total amount each student needs to pay for those Kotlin related books.

The first step is to modify the singleton class function and it’s content like this.

object FilterBook {

    var total_price : Double = 0.00
    var sale_tax : Double = 0.00

    fun calculate_total_price(book: KotlinBookTotalAmount.Companion) {

        //var kotlin_book_list : Map<String, Double> = book.filter {

            //total_price += it.value * sale_tax // include 2.9 % sale taxes

        total_price = * sale_tax // total amount includes the 2.9 % sale taxes

    fun print_total() {

    var total_purchase : Double
         get() {
             return total_price

    set(value) {
        sale_tax = value.toDouble()

The second step is to modify the main Kotlin program.

fun main(args : Array<String>) {

    FilterBook.sale_tax = 1.029
    //FilterBook.calculate_total_price(book = mapOf("Kotlin" to 20.00, "Kotlin is really great" to 10.00, "Other Program" to 26.00, "Why Kotlin is really great?" to 40.00))
    val jimmy = Stdent1(FilterBook.total_purchase)
    val tommy = Stdent1(FilterBook.total_purchase)
    val george = Stdent1(FilterBook.total_purchase)
    print("%.2f".format(jimmy.total_purchase + tommy.total_purchase + george.total_purchase)) // output 926.10

Finally, we will create the enum class and it’s companion object’s function to calculate the total amount each student needs to pay for the Kotlin related books.

enum class KotlinBookTotalAmount (val price : Double){


    companion object {
        fun total() : Double {
            var total_amount : Double = 0.00
            for (book in KotlinBookTotalAmount.values()) {
                total_amount += book.price
            return total_amount


If you miss the original total book amount program then please don’t hesitate to read them under the Kotlin category of this website.

Please follow and like us:

Leave a Reply

Your email address will not be published. Required fields are marked *