Press enter to see results or esc to cancel.

Lesson 27: Class collaboration in Kotlin Language

Share this

Normally a problem solved with the methodology of object oriented programming does not involve a single class, but there are many classes that interact and communicate.

We will pose problems by separating activities into two classes.

Problem 1

A bank has 3 clients that can make deposits and withdrawals. Also, the bank requires that at the end of the day you calculate the amount of money deposited.

The first thing we do is to identify the classes:
We can identify the class Client and the class Bank.

Then we must define the properties and methods of each class:

        first name
        to deposit
        to print

        3 Client (3 objects of the Client class)
        Total deposits

Project119 – Main.kt

class client (var name: String, var amount: Float) {

    fun deposit (amount: Float) {
        this.amount += amount

    fun extract (amount: Float) {
        this.amount -= amount

    fun print () {
        println ("$name has deposited the sum of $amount")

class Bank {
    val client1: client = client ("Juan", 0f)
    var client2: client = client ("Ana", 0f)
    var client3: client = client ("Luis", 0f)

    fun operate () {
        client1.deposit (100f)
        client2.deposit (150f)
        client3.deposit (200f)
        client3.extract (150f)

    fun depositsTotal () {
        val total = client1.amount + client2.amount + client3.amount
        println ("Total bank money is: $total")
        client1.print ()
        client2.print ()
        client3.print ()

fun main (parameter: Array <String>) {
    val bank1 = Bank ()
    bank1.operate ()
    bank1.depositsTotal ()

First, we make the declaration of the class Client, the main constructor arrives the name of the client and the initial amount deposited (the properties we are defining them in the own constructor):

Class Client (var name: String, var amount: Float) {

The method that increases the property amount is:

    Fun deposit (amount: Float) {
        This.amount+= amount

And the method that reduces the property amount of the client is:

    Fun extract (amount: Float) {
        This.amount -= amount

To display the customer data we have the method:

    fun print () {
        Println ("$ name has deposited the sum of $ amount")

The second class of our problem is the Bank. This class defines three properties of the Client class (the Client class collaborates with the Bank class):

Class Bank {
    Val client1: Client = Client ("Juan", 0f)
    Var client2: client = Customer ("Ana", 0f)
    Var client3: Client = Customer ("Luis", 0f)

The operating method performs a series of deposits and withdrawals from customers:

    Fun operate () {
        client1.deposit (100f)
        client2.deposit (150f)
        client3.deposit (200f)
        Client3.extractor (150f)

The method that shows how much money has deposited the bank is solved by accessing the property amount of each client:

    Fun depositsTotal () {
        Val total = client1.amount+ client2.amount+ client3.amount
        Println ("Total bank money is: $total")
        Client1.print ()
        Client2.print ()
        Client3.print ()

In the main function of our program we proceed to create an object of class Bank and call the two methods:

Fun main (parameter: Array <String>) {
    Val bank1 = Bank ()
    Bank1.operate ()
    Bank1.depositosTotals ()

Problem 2

Propose a program that allows you to play a dice. The rules of the game are:
three dice are thrown, if all three come out with the same value displaying a message that “wins” but “lost”.

The first thing we do is identify the classes:

We can identify the Given class and the GameData class.

Then the properties and methods of each class:

        to print

Dice game
        3 Given (3 objects of the Given class)
        to play

Project120 – Home.kt

class given (var value: Int) {

    fun throwin () {
        value = ((Math.random () * 6) + 1) .toInt ()

    fun print () {
        println ("Value of data: $value")

class gameData {
    val given1 = given (1)
    val given2 = given (1)
    val given3 = given (1)

    fun play () {
        if (given1.value == given2.value && given2.value == given3.value)
            println ("Won")
            print ("Lost")

fun main (parameter: Array <String>) {
    val game1 = gameData () ()

The Given class defines a throw method that stores a random number in the value property between 1 and 6. It also calls the print method to display it:

Class Given (var value: Int) {

    Fun throwin () {
        Value = ((Math.random () * 6) + 1) .toInt ()
        to print()

The GameData class defines three properties of the given class:

Class GameData {
    Val given1 = Given (1)
    Val given2 = Given (1)
    Val given3 = Given (1)

In the play method of the GameData class we proceed to ask each die to be pulled and check if the three values are the same:

    play () {
        Given1.size ()
        Given2.size ()
        Given3.size ()
        If (given1.value == given2.value && given2.value == given3.value)
            Println ("Won")
            Print ("Lost")

In the main function of our program we create an object of the class GameData:

Fun main (parameter: Array <String>) {
    Val game1 = GameData () ()

Proposed problem

  • Arrange a Club class and another class Partner.
    The Partner class must have the following properties: name and seniority in the club (in years).
    To the constructor of the partner class, make it arrive the name and its seniority.
    The Club class must have as properties 3 objects of class Partner.
    Define a method in the Club class to print the name of the oldest member in the club.



class partner (val name: String, val seniority: Int) {
    fun print () {
        println ("$name has an antiquity of $seniority")

class club {
    val partner1 = partner ("Juan", 22)
    val partner2 = partner ("Ana", 34)
    val partner3 = partner ("Carlos", 1)

    fun seniorityAge () {
        partner1.print ()
        partner2.print ()
        partner3.print ()
        print ("Senior partner:")
        when {
            partner1.seniority > partner2.seniority && partner1.seniority> partner3.seniority -> print (
            partner2.seniority> partner3.seniority -> print (
            else -> print (

fun main (parameter: Array <String>) {
    val club1 = club ()
    club1.seniorityAge ()


How useful was this Lesson?

Click on a star to rate it!

Average rating / 5. Vote count:

Be the first to rate this post.!

As you found this post useful...

Follow us on social media!

We are sorry that this lesson was not useful for you!

Let us improve this lesson!

Tell us how we can improve this lesson?

Share this