article banner

Exercise: User Information Processor

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("john@example.com") ) println(processUserInformation(user1)) // User John is 30 years old, email: john@example.com val user2 = User( "Alice", null, EmailAddress("alice@example.com") ) println(processUserInformation(user2)) // User Alice is 0 years old, email: alice@example.com val user3 = User( "Bob", 25, EmailAddress("") // or EmailAddress(null) or null ) println(processUserInformation(user3)) // Missing email val user6 = User( null, 40, EmailAddress("jake@example.com") ) 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("john@example.com") ) println(processUserInformation(user1)) // User John is 30 years old, email: john@example.com val user2 = User( "Alice", null, EmailAddress("alice@example.com") ) println(processUserInformation(user2)) // User Alice is 0 years old, email: alice@example.com val user3 = User( "Bob", 25, EmailAddress("") // or EmailAddress(null) or null ) println(processUserInformation(user3)) // Missing email val user6 = User( null, 40, EmailAddress("jake@example.com") ) println(processUserInformation(user6)) // IllegalArgumentException } class StudentInformationTest { @Test fun `valid user information`() { val user = User("John Doe", 30, EmailAddress("john@example.com")) val result = processUserInformation(user) val expected = "User John Doe is 30 years old, email: john@example.com" 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("alice@example.com")) val result = processUserInformation(user) val expected = "User Alice is 0 years old, email: alice@example.com" 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@example.com")) assertThrows(IllegalArgumentException::class.java) { processUserInformation(user) } } }