Exercise: Converting and Creating a Measurement Unit

Enhance your Kotlin expertise by implementing extension functions for conversion between two classes and handling transformations of various data fields.

We'll cover the following

Problem statement

Implement two kinds of extension functions to facilitate smooth transformation between the User and UserJson classes. The User class represents our domain object, while UserJson represents the structure that is sent to the client.

Instructions

In the starting code, you’re provided with the basic structure of the UserEmailCentimeters, and UserJson classes. Your task is to implement the extension functions toJson() and toUser(), ensuring the specified conversion behavior:

  1. Copy username as it is.

  2. Convert email to/from String using the Email class.

  3. Transform registrationDate to/from String in ISO format ISO format refers to the international standard date and time notation (ISO 8601), representing dates and times in a specific format, such as 2022-02-15T12:30:45.utilizing parse and toString from LocalDateTime.

  4. Convert height to/from Int through the Centimeters class.

  5. Additionally, implement the cm extension property for Int to create a corresponding Centimeters object.

  6. Convert the User object to a UserJson object.

  7. Convert the UserJson object back to a User object.

The actual output when running the provided code should be as follows:

Get hands-on with 1200+ tech skills courses.