Implement the processUserInformation function according to the following rules:
If the input user is null, return "Missing user information".
If name from user is null, throw an IllegalArgumentException.
If age from user is null, treat it as 0.
If email from user is null or contains a blank email address, return "Missing email".
Otherwise, return information about user in the following format: "User {name} is {age} years old, email: {email}".
Starting code:
fun processUserInformation(user: User?): String {
return ""
}
data class EmailAddress(val email: String?)
data class User(
val name: String?,
val age: Int?,
val email: EmailAddress?
)
Example usage:
println(processUserInformation(null))
// Missing user information
val user1 = User(
"John",
30,
EmailAddress("[email protected]")
)
println(processUserInformation(user1))
// User John is 30 years old, email: [email protected]
val user2 = User(
"Alice",
null,
EmailAddress("[email protected]")
)
println(processUserInformation(user2))
// User Alice is 0 years old, email: [email protected]
val user3 = User(
"Bob",
25,
EmailAddress("") // or EmailAddress(null) or null
)
println(processUserInformation(user3))
// Missing email
val user6 = User(
null,
40,
EmailAddress("[email protected]")
)
println(processUserInformation(user6))
// IllegalArgumentException
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/nullability/UserInformation.kt. You can find there starting code, example usage and unit tests.
Once you are done with the exercise, you can check your solution here.
Playground
import org.junit.Assert.assertThrows
import org.junit.Test
import kotlin.test.assertEquals
fun processUserInformation(user: User?): String {
return ""
}
data class EmailAddress(val email: String?)
data class User(
val name: String?,
val age: Int?,
val email: EmailAddress?
)
fun main() {
println(processUserInformation(null))
// Missing user information
val user1 = User(
"John",
30,
EmailAddress("[email protected]")
)
println(processUserInformation(user1))
// User John is 30 years old, email: [email protected]
val user2 = User(
"Alice",
null,
EmailAddress("[email protected]")
)
println(processUserInformation(user2))
// User Alice is 0 years old, email: [email protected]
val user3 = User(
"Bob",
25,
EmailAddress("") // or EmailAddress(null) or null
)
println(processUserInformation(user3))
// Missing email
val user6 = User(
null,
40,
EmailAddress("[email protected]")
)
println(processUserInformation(user6))
// IllegalArgumentException
}
class StudentInformationTest {
@Test
fun `valid user information`() {
val user = User("John Doe", 30, EmailAddress("[email protected]"))
val result = processUserInformation(user)
val expected = "User John Doe is 30 years old, email: [email protected]"
assertEquals(expected, result)
}
@Test
fun `null user information`() {
val result = processUserInformation(null)
val expected = "Missing user information"
assertEquals(expected, result)
}
@Test
fun `user with missing age should have age 0`() {
val user = User("Alice", null, EmailAddress("[email protected]"))
val result = processUserInformation(user)
val expected = "User Alice is 0 years old, email: [email protected]"
assertEquals(expected, result)
}
@Test
fun `user with blank email`() {
val user = User("Bob", 25, EmailAddress(""))
val result = processUserInformation(user)
val expected = "Missing email"
assertEquals(expected, result)
}
@Test
fun `user with null email address`() {
val user = User("Bob", 25, EmailAddress(null))
val result = processUserInformation(user)
val expected = "Missing email"
assertEquals(expected, result)
}
@Test
fun `user with null email information`() {
val user = User("Bob", 25, null)
val result = processUserInformation(user)
val expected = "Missing email"
assertEquals(expected, result)
}
@Test
fun `user with missing name should throw exception`() {
val user = User(null, 40, EmailAddress("[email protected]"))
assertThrows(IllegalArgumentException::class.java) {
processUserInformation(user)
}
}
}
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.