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:
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))
}
}
Marcin Moskala is a highly experienced developer and Kotlin instructor as the founder of Kt. Academy, an official JetBrains partner specializing in Kotlin training, Google Developers Expert, known for his significant contributions to the Kotlin community. Moskala is the author of several widely recognized books, including "Effective Kotlin," "Kotlin Coroutines," "Functional Kotlin," "Advanced Kotlin," "Kotlin Essentials," and "Android Development with Kotlin."
Beyond his literary achievements, Moskala is the author of the largest Medium publication dedicated to Kotlin. As a respected speaker, he has been invited to share his insights at numerous programming conferences, including events such as Droidcon and the prestigious Kotlin Conf, the premier conference dedicated to the Kotlin programming language.