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.

ScalabeginnercliAI generated

Command-Line Unit Converter

Build a unit converter for the terminal in Scala: type conversions like 5 km to mi, 100 F to C or 2 l to cup, and get the answer right away, in an interactive loop or directly from the command line. It supports length, weight, temperature and volume, recognizes common unit names and abbreviations, and refuses conversions that make no sense, like kilograms to liters, with a clear message. It's a good first Scala project: you'll learn setting up a project with sbt, case classes and enums to model units, pattern matching, pure functions, parsing input with regular expressions, handling errors with Either instead of exceptions, and testing with ScalaTest.

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

sbt · ScalaTest

Project roadmap

  1. 01

    Set up the project

    ~0.5h

    Create a project with sbt new scala/scala3.g8, add ScalaTest to build.sbt, and write a main method that prints a welcome message. Done when: sbt run prints the message, and sbt test runs (with no tests yet).

  2. 02

    Model units and write conversions

    ~1.5h

    Model the categories as an enum and units as case objects (or an enum) with a category, names and aliases (km, kilometer and kilometers), and a factor to the base unit for length, weight and volume. Temperature needs formulas, so use pattern matching on the (from, to) pair through Celsius. Write convert(value, from, to): Either[String, Double], returning Left when the categories differ or a temperature is below absolute zero. Done when: 5 km to mi gives 3.107, and kg to l returns a clear error.

  3. 03

    Parse the input

    ~1h

    Write a parser using a regular expression for input like 5 km to mi or 5km in mi (with optional spaces and decimals, and negative numbers for temperatures), looking up units by any of their names, ignoring case, and returning Either with a helpful error (suggesting similar unit names when one isn't found). Done when: parser tests pass for valid and invalid inputs.

  4. 04

    Build the interactive loop

    ~1h

    When run without arguments, loop reading lines with StdIn.readLine (stopping on quit or the end of input), and print the result or the error for each, with sensible rounding (for example 3 decimals, dropping trailing zeros). When run with arguments, convert once and exit with code 0 or 1. Add a help command listing units by category. Done when: both the interactive mode and single conversions work.

  5. 05

    Handle errors clearly

    ~0.5h

    Make sure every problem (bad numbers, unknown units, mixed categories and impossible temperatures) produces a clear message and never a stack trace, and that the loop continues after an error. Done when: typing nonsense never crashes the program.

  6. 06

    Write unit tests

    ~1.5h

    Write ScalaTest tests for each category's conversions (including round trips that return the original value), temperature edge cases (-40 is the same in C and F, and absolute zero), unit aliases, mixed categories, and the parser. Done when: sbt test passes.

Ready to build this?

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

~6h · 6 steps

Tech stack

sbtScalaTest