import 'dart:io'; void main() { List> incomeList = []; List> expenseList = []; print('Welcome to the Budget Management Application!'); 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? choiceInput = stdin.readLineSync(); if (choiceInput == null) { print('Invalid input. Please try again.'); continue; } int? choice = int.tryParse(choiceInput); if (choice == null) { print('Please enter a valid number.'); continue; } if (choice == 4) { print('Exiting the application. Goodbye!'); break; } if (choice == 1) { _addIncome(incomeList); } else if (choice == 2) { _addExpense(expenseList); } else if (choice == 3) { _viewSummary(incomeList, expenseList); } else { print('Invalid choice. Please try again.'); } } } void _addIncome(List> incomeList) { stdout.write('Enter income description: '); String? description = stdin.readLineSync(); if (description == null || description.trim().isEmpty) { print('Description cannot be empty.'); return; } double? amount = _readPositiveAmount('Enter income amount: '); if (amount == null) { return; } incomeList.add({description.trim(): amount}); print('Income added!'); } void _addExpense(List> expenseList) { stdout.write('Enter expense description: '); String? description = stdin.readLineSync(); if (description == null || description.trim().isEmpty) { print('Description cannot be empty.'); return; } double? amount = _readPositiveAmount('Enter expense amount: '); if (amount == null) { return; } expenseList.add({description.trim(): amount}); print('Expense added!'); } double? _readPositiveAmount(String prompt) { while (true) { stdout.write(prompt); String? input = stdin.readLineSync(); if (input == null) { print('Invalid input. Please try again.'); continue; } double? value = double.tryParse(input); if (value == null) { print('Please enter a valid number.'); continue; } if (value < 0) { print('Amount cannot be negative. Please try again.'); continue; } return value; } } void _viewSummary( List> incomeList, List> expenseList, ) { double totalIncome = 0; for (var entry in incomeList) { entry.forEach((_, amount) { totalIncome += amount; }); } double totalExpenses = 0; for (var entry in expenseList) { entry.forEach((_, amount) { totalExpenses += amount; }); } 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)}'); } else if (net < 0) { print('Net loss: \$${(-net).toStringAsFixed(2)}'); } else { print('You are breaking even (no profit, no loss).'); } double tax = net > 0 ? net * 0.15 : 0; if (tax > 0) { print('Tax to be paid: \$${tax.toStringAsFixed(2)}'); } else { print('No tax due (no profit).'); } }