Defining Shared Code
Doubloons4Gold’s core logic will be defined in the commonMain directory. This will allow you to write the parts of your application that are specific to your problem – and not the platform – once, including the exchange rate, prompts shown to the user, and input requests. You can then use these implementations on all platforms without modifications. (In fact, if you accidentally tried to use a platform-specific API in this directory, IntelliJ would respond with an error.)
Get started by making a new file in src/commonMain/kotlin called Converter.kt. Create a convertCurrency function take care of your currency conversion:
Listing 24.2 Converting currencies (commonMain/kotlin/Converter.kt)
val pricePerDoubloon ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access