Exercise: Conversion and measurement unit creation
Your task is to implement two kinds of extension functions. First, you need functions to transform between User
and UserJson
classes. User
represents our domain object, while UserJson
represents the structure that is sent to the client. The conversion should be done according to the following rules:
username
should be copied as is,email
should be converted to/fromString
in theEmail
class,registrationDate
should be converted to/fromString
in ISO format. You can useparse
andtoString
fronLocalDateTime
for that,height
should be converted to/fromInt
inCentimeters
class.
Then implement cm
extension property for Int
that will create a Centimeters
object.
Once your solution is ready, the following code should work:
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/extensions/User.kt. You can find there example usage and unit tests.
Once you are done with the exercise, you can check your solution here.