Solution: Person details display

This is the expected solution for this exercise:

fun formatPersonDisplay( name: String? = null, surname: String? = null, age: Int? = null, ): String { var result = "" if (name != null) { result += name } if (surname != null) { result += " $surname" } if (age != null) { result += " ($age)" } return result.trim() }

This exercise can also be solved using functions that are presented in the Functional Kotlin book (the next book I recommend you reading after Kotlin Essentials). This is a solution using listOfNotNull and joinToString functions:

fun formatPersonDisplay( name: String? = null, surname: String? = null, age: Int? = null, ): String = listOfNotNull( name, surname, if (age != null) "($age)" else null // or age?.let { "($it)" } ).joinToString(separator = " ")

This is a solution using buildString function (it allows constructing a string by appending it with next parts inside lambda expression block):

fun formatPersonDisplay( name: String? = null, surname: String? = null, age: Int? = null, ): String = buildString { if (name != null) append("$name") if (surname != null) append(" $surname") if (age != null) append(" ($age)") }.trim()

Example solution in playground

import org.junit.Test import kotlin.test.assertEquals fun formatPersonDisplay( name: String? = null, surname: String? = null, age: Int? = null, ): String { var result = "" if (name != null) { result += name } if (surname != null) { result += " $surname" } if (age != null) { result += " ($age)" } return result.trim() } fun main() { println(formatPersonDisplay("John", "Smith", 42)) // John Smith (42) println(formatPersonDisplay("Alex", "Simonson")) // Alex Simonson println(formatPersonDisplay("Peter", age = 25)) // Peter (25) println(formatPersonDisplay(surname="Johnson", age=18)) // Johnson (18) } class PersonDisplayTest { @Test fun testFormatPersonDisplay() { val name = "John" val surname = "Smith" val age = 42 val expected = "John Smith (42)" assertEquals(expected, formatPersonDisplay(name, surname, age)) } @Test fun testFormatPersonDisplayWithoutAge() { val name = "Alex" val surname = "Simonson" val expected = "Alex Simonson" assertEquals(expected, formatPersonDisplay(name, surname)) } @Test fun testFormatPersonDisplayWithoutSurname() { val name = "Peter" val age = 25 val expected = "Peter (25)" assertEquals(expected, formatPersonDisplay(name = name, age = age)) } @Test fun testFormatPersonDisplayWithoutName() { val surname = "Johnson" val age = 18 val expected = "Johnson (18)" assertEquals(expected, formatPersonDisplay(surname = surname, age = age)) } @Test fun testFormatPersonDisplayWithoutNameAndSurname() { val age = 18 val expected = "(18)" assertEquals(expected, formatPersonDisplay(age = age)) } @Test fun testFormatPersonDisplayWithoutParameters() { val expected = "" assertEquals(expected, formatPersonDisplay()) } @Test fun testFormatPersonDisplayWithNullName() { val name: String? = null val surname = "Smith" val age = 42 val expected = "Smith (42)" assertEquals(expected, formatPersonDisplay(name, surname, age)) } @Test fun testFormatPersonDisplayWithNullSurname() { val name = "John" val surname: String? = null val age = 42 val expected = "John (42)" assertEquals(expected, formatPersonDisplay(name, surname, age)) } }