import 'dart:io'; void main() { print('Welcome to the Budget Management Application!'); List> incomes = []; List> expenses = []; while (true) { print('\nMenu:'); print('1. Add Income'); print('2. Add Expense'); print('3. View Summary (Total income, Total expenses, Net)'); print('4. Exit'); stdout.write('Choose an option (1-4): '); String? choice = stdin.readLineSync(); if (choice == null) { print('Invalid input. Try again.'); continue; } if (choice == '1') { stdout.write('Enter income description: '); String? desc = stdin.readLineSync() ?? ''; double? amount = _readPositiveDouble('Enter income amount: '); if (amount == null) { print('Cancelled adding income.'); continue; } incomes.add({'description': desc, 'amount': amount}); print('Income added: $desc — \$${amount.toStringAsFixed(2)}'); } else if (choice == '2') { stdout.write('Enter expense description: '); String? desc = stdin.readLineSync() ?? ''; double? amount = _readPositiveDouble('Enter expense amount: '); if (amount == null) { print('Cancelled adding expense.'); continue; } expenses.add({'description': desc, 'amount': amount}); print('Expense added: $desc — \$${amount.toStringAsFixed(2)}'); } else if (choice == '3') { double totalIncome = incomes.fold( 0.0, (s, e) => s + (e['amount'] as double), ); double totalExpenses = expenses.fold( 0.0, (s, e) => s + (e['amount'] as double), ); double net = totalIncome - totalExpenses; print('\n--- Budget Summary ---'); print('Total Income: \$${totalIncome.toStringAsFixed(2)}'); print('Total Expenses: \$${totalExpenses.toStringAsFixed(2)}'); if (net >= 0) { print('Your net profit is \$${net.toStringAsFixed(2)}'); } else { print('Your net loss is \$${(net.abs()).toStringAsFixed(2)}'); } double tax = net > 0 ? net * 0.15 : 0.0; if (net > 0) { print('Tax to pay (15% of profit): \$${tax.toStringAsFixed(2)}'); } else { print('No tax due (no profit).'); } print('-----------------------\n'); } else if (choice == '4') { print('Exiting Budget Manager. Goodbye!'); break; } else { print('Invalid choice — pick 1-4.'); } } } double? _readPositiveDouble(String prompt) { while (true) { stdout.write(prompt); String? input = stdin.readLineSync(); if (input == null || input.trim().isEmpty) return null; double? value = double.tryParse(input); if (value == null) { print('Please enter a numeric value.'); continue; } if (value < 0) { print('Amount cannot be negative. Try again.'); continue; } return value; } }