Exercise: Person details display
Your task is to implement formatPersonDisplay
function: It should have String
result type and the following parameters:
name
of type String?
and default value null
.surname
of type String?
and default value null
.age
of type Int?
and default value null
.
Beware! Parameter types should include ?
, so those should be String?
and Int?
instead of String
and Int
. This is because we want to allow passing null
as a parameter value. This will be explained in the chapter Nullability.
Function should return a string in the following format: "{name} {surname} ({age})"
. If any of the parameters is null
, it should be omitted from the result. If all parameters are null
, it should return an empty string.
Here are some examples of how the function should work:
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)
This problem can either be solved in the below playground or you can clone kotlin-exercises project and solve it locally. In the project, you can find code template for this exercise in essentials/functions/PersonDisplay.kt. You can find there example usage and unit tests.
In this project, example usage and unit tests are commented not to prevent other files from compilation. To uncomment them, select commented lines and use command + /
on Mac (Ctrl + /
on Windows)
Hint: You can use trim
function on a string to remove leading and trailing whitespace characters.
Once you are done with the exercise, you can check your solution here.
import org.junit.Test
import kotlin.test.assertEquals
// TODO
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.