1. Use Interface as a blueprint with methods and properties that need to be implemented by the concrete class.
  2. We use generic type in a class so that class will be able to use back all the methods and properties of that class on other types of object in the future.

Hello and welcome back, in this chapter we will continue to develop our bookkeeping application. In the previous program, I have hardcoded the name of three students to find out what is the total price of books that are related to Python and in this chapter, I will just simply include the student into a list which will be used to calculate the total amount later on.

First of all, let us create an interface which will be implemented by a concrete class. This interface has the method to include all the students in a MutableList, to calculate the total purchase amount of books, to print the total amount on the console and a total variable used to keep the total amount of books.

interface StudentGroup<T> {
    var total:Double
    fun includeStudentToGroup(student : T)
    fun calculateTotalAmount()
    fun printTotalAmount()
}

Next, we will create a reusable concrete generic class which will perform all those calculations.

class StdentGrouping : StudentGroup<Stdent1> {
    var student_group : MutableList<Stdent1> = mutableListOf<Stdent1>()
    override var total: Double = 0.0

    // calculate total amount of books
    override fun calculateTotalAmount() {
        for(student in student_group)
            total += student.total_purchase
    }

    override fun includeStudentToGroup(student : Stdent1) {
        student_group.add(student) //To add student to the student group
    }

    override fun printTotalAmount() {
       student_group.get(0).totalPurchaseStatement() // output Total Purchase From All Students:
       print("%.2f".format(total)) // output 926.10
    }
}

We will also create a new method under the Student superclass which will print the total amount statement before the student grouping class prints out the total amount.

open class Student(var amount : Double) {
    val total_purchase : Double
        get() {
            return amount
        }

    fun totalPurchaseStatement() {
        println("Total Purchase From All Students:")
    }
}

Finally we will change the code in the main program to print out the total amount.

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)
    val student_group = StdentGrouping()
    for(total_student in (1..3))
        student_group.includeStudentToGroup(Stdent1(FilterBook.total_purchase))
    student_group.calculateTotalAmount() // calculate the total amount of books
    //print("%.2f".format(jimmy.total_purchase + tommy.total_purchase + george.total_purchase)) // output 926.10
    student_group.printTotalAmount() // print total amount of books purchase by the student
}

As you can see, we no longer need to hard code the name of the student but will just pass the student into a student list within the student grouping class under the for loop in the main program.

Please follow and like us:
error0

Leave a Reply

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