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 {
            //it.key.contains("Kotlin")
        //}

        //kotlin_book_list.forEach{
            //total_price += it.value * sale_tax // include 2.9 % sale taxes
        //}

        total_price = book.total() * sale_tax // total amount includes the 2.9 % sale taxes
    }

    fun print_total() {
        print("%.2f".format(total_price))
    }

    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))
    FilterBook.calculate_total_price(KotlinBookTotalAmount)
    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){

    Kotlin(20.00),
    KotlinAsWell(30.00),
    KotlinObject(40.00),
    WhyKotlin(50.00),
    TellMeWhyYouLikeKotlinProgram(100.00),
    WillKotlinReadyForAndroid(60.00);

    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.

Click to rate this post!
[Total: 0 Average: 0]
Please follow and like us:
error0

Leave a Reply

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