šŸ”¢ Project: Command-Line Calculator

šŸ“š Lesson 1: Your First Dart App ā± 20-25 min šŸ“Š Beginner
šŸŽÆ Objective

Create a program that performs arithmetic operations based on command-line arguments with proper error handling and formatted output.

Requirements

  1. Accept three command-line arguments: first number, operator (+, -, *, /), second number
  2. Parse the numbers from strings to double for decimal support
  3. Use a switch statement to handle different operators
  4. Handle division by zero with a clear error message
  5. Show usage instructions if wrong number of arguments provided
  6. Format the result to 2 decimal places
  7. Validate that the operator is one of the supported ones

Expected Output

Terminal
$ dart run calculator.dart 10 + 5
Result: 10.00 + 5.00 = 15.00

$ dart run calculator.dart 10.5 / 3
Result: 10.50 / 3.00 = 3.50

$ dart run calculator.dart 10 / 0
Error: Division by zero is not allowed.

$ dart run calculator.dart 10 x 5
Error: Invalid operator "x". Supported operators: +, -, *, /

$ dart run calculator.dart
Usage: dart run calculator.dart <number1> <operator> <number2>
Example: dart run calculator.dart 10 + 5
Supported operators: + (addition), - (subtraction), * (multiplication), / (division)

Step-by-Step Hints

šŸ’” Hint 1: Argument Validation

Check arguments.length != 3 first. If it's not exactly 3, show usage and exit.

šŸ’” Hint 2: Safe Number Parsing

Use double.tryParse(arguments[0]) and double.tryParse(arguments[2]). Check if either returns null before proceeding.

šŸ’” Hint 3: Switch Statement

Use switch (arguments[1]) with cases for '+', '-', '*', '/'. Add a default case for invalid operators.

Challenge: Extend the Project

  1. Add support for more operations (%, sqrt, pow)
  2. Allow chaining multiple operations (e.g., 10 + 5 * 2)
  3. Add a history feature that remembers previous calculations
  4. Support different number formats (hexadecimal, binary)