Create a Kotlin class to filter out unwanted programming books and calculate the total price of the Kotlin related book.

Hello and welcome back, in this Kotlin tutorial we are going to create a class in Kotlin which will then create a map which only contains the price of books that only have the word ‘Kotlin’ in their title. That map will then be used in the main Kotlin program to calculate the total amount we need to pay for those Kotlin related books.

First of all, let us create the simple book filter class.

class FilterBook(val book: Map<String, Double>) {
    var kotlin_book_list : Map<String, Double> = book.filter {
        it.key.contains("Kotlin")
    }
}

With the Kotlin class ready, we will write the main program.

fun main(args : Array<String>) {
    val filter_kotlin = FilterBook(book = mapOf("Kotlin" to 20.00, "Kotlin is really great" to 10.00, "Other Program" to 26.00))
    val kotlin_book_price = (filter_kotlin.kotlin_book_list)
    var total_price : Double = 0.00
    kotlin_book_price.forEach{
        total_price += it.value * 1.029 // 2.9 % sale taxes
    }
    print("%.2f".format(total_price)) // output 30.87
}

In the next chapter I will start a new desktop application project with Kotlin which will take a few weeks to complete, if you like the Kotlin topic and want to follow that next project then do subscribe to the Kotlin RSS feed on this website: https://kibiwebgeek.com/category/kotlin/feed/ . You can put the above link into any of your favorite rss readers to receive the future new post about Kotlin.

Please follow and like us:
error0

Leave a Reply

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