Help us measure site usage?

With your permission, we use self-hosted analytics to understand page usage. It is optional and does not affect the Service. Learn more.

KotlinbeginnercliAI generated

Command-Line Unit & Currency Converter

Build a command-line converter in Kotlin for units and currencies. You run it like convert 10 km mi or convert 100 EUR USD and it prints the result. It handles length, weight and temperature, plus currencies using exchange rates stored in a local JSON file that you can update yourself, and it refuses conversions that make no sense, such as kilograms to miles. It's a good first Kotlin project: you'll learn functions, data classes, enums and maps, when expressions, handling invalid input with nullable types and exceptions, reading a JSON file, parsing command-line arguments, building with Gradle, and testing.

Estimate
~7h
Steps
6
Completed by
0
Proposed by
codeseed.app

kotlinx-cli · Gradle

Project roadmap

  1. 01

    Set up the project

    ~1h

    Create a Kotlin project with gradle init (application type) and add kotlinx-cli for argument parsing (or Clikt, a popular and actively maintained alternative) and kotlinx.serialization for JSON. Make the main function print its arguments. Done when: ./gradlew run --args="10 km mi" prints the arguments.

  2. 02

    Define the units

    ~1h

    Create an enum Category (LENGTH, WEIGHT, TEMPERATURE) and a data class Unit (symbol, name, category, and factor to the base unit: meters or kilograms). Put all units in a map by symbol, accepting upper and lower case. Done when: looking up km, MI and kg returns the right units, and unknown symbols return null.

  3. 03

    Convert units

    ~1.5h

    Write convert(value, from, to): check both units are in the same category (otherwise throw an error naming both), convert length and weight through the base unit, and temperatures with a when expression over the formulas for Celsius, Fahrenheit and Kelvin, rejecting temperatures below absolute zero. Done when: 10 km to mi gives 6.21, and 100 C to F gives 212.

  4. 04

    Add currencies

    ~1.5h

    Create rates.json with a base currency, a date and rates for at least 6 currencies. Load it with kotlinx.serialization into a data class, and convert through the base currency, rounding to 2 decimals with BigDecimal. Report a clear error when the file is missing or broken, and show the rates' date with every currency result. Done when: 100 EUR USD uses the file's rates and shows their date.

  5. 05

    Build the command line

    ~1h

    Parse the value (as a Double, with a clear error for text), the two units, and options --precision and --list (showing all units and currencies by category). Print results like 10 km = 6.21 mi. Exit with a non-zero code on errors, printing the message to stderr. Done when: valid commands print clean results, and invalid ones print helpful errors.

  6. 06

    Test it

    ~1h

    Write unit tests with kotlin.test (JUnit 5): conversions in every category, round trips (converting and back gives the original), temperatures including negatives, mixed categories throwing an error, currency conversion with a test rates file, and parsing invalid values. Done when: ./gradlew test passes.

Ready to build this?

Get a GitHub repo and start building. Your AI reviewer checks each step as you go.

~7h · 6 steps

Tech stack

kotlinx-cliGradle