import 'dart:io'; void main() { print("Welcome to the Budget Management Application!"); // Data Storage using lists of maps (as required) List> incomeList = []; List> expenseList = []; while (true) { print("\nPlease choose an option:"); print("1. Add income"); print("2. Add expense"); print("3. View summary"); print("4. Exit"); stdout.write("\nEnter your choice: "); String? input = stdin.readLineSync(); int? choice = int.tryParse(input ?? ""); if (choice == null) { print("Invalid input. Please enter a number from 1 to 4."); continue; } if (choice == 1) { // Add income stdout.write("Enter income description: "); String desc = (stdin.readLineSync() ?? "").trim(); double amount = _readPositiveAmount("Enter income amount: "); incomeList.add({desc: amount}); print("Income added!"); } else if (choice == 2) { // Add expense stdout.write("Enter expense description: "); String desc = (stdin.readLineSync() ?? "").trim(); double amount = _readPositiveAmount("Enter expense amount: "); expenseList.add({desc: amount}); print("Expense added!"); } else if (choice == 3) { // View summary double totalIncome = _sumList(incomeList); double totalExpenses = _sumList(expenseList); double net = totalIncome - totalExpenses; print("\nSummary:"); print("Total income: \$${totalIncome.toStringAsFixed(2)}"); print("Total expenses: \$${totalExpenses.toStringAsFixed(2)}"); if (net >= 0) { print("Net profit: \$${net.toStringAsFixed(2)}"); // Optional Tax (15%) using ternary operator (as required) double tax = net > 0 ? net * 0.15 : 0.0; if (tax > 0) { print("Tax to be paid (15%): \$${tax.toStringAsFixed(2)}"); } } else { print("Net loss: \$${net.abs().toStringAsFixed(2)}"); print("No tax because there is a loss."); } } else if (choice == 4) { print("Exiting the application. Goodbye!"); break; } else { print("Invalid choice. Please enter a number from 1 to 4."); } } } double _readPositiveAmount(String message) { while (true) { stdout.write(message); String? input = stdin.readLineSync(); double? amount = double.tryParse(input ?? ""); // input validation: valid number and not negative if (amount == null) { print("Invalid number. Try again."); continue; } if (amount < 0) { print("Amount cannot be negative. Try again."); continue; } return amount; } } double _sumList(List> list) { double total = 0.0; for (var entry in list) { for (var amount in entry.values) { total += amount; } } return total; }