import 'dart:io'; void main() { print('=== Budget Management Application ==='); List> incomes = []; List> expenses = []; while (true) { print('\nChoose an option:'); print('1) Add Income'); print('2) Add Expense'); print('3) Show Summary'); print('4) Exit'); stdout.write('> '); String? choice = stdin.readLineSync(); if (choice == null) continue; if (choice == '4') { print('Exiting... Goodbye '); break; } if (choice == '1') { stdout.write('Enter income description: '); String? desc = stdin.readLineSync(); stdout.write('Enter amount: '); String? amountStr = stdin.readLineSync(); double? amount = double.tryParse(amountStr ?? ''); if (desc == null || desc.trim().isEmpty || amount == null || amount < 0) { print('Invalid input. Please enter a valid description and positive amount.'); continue; } incomes.add({'description': desc.trim(), 'amount': amount}); print('Income added.'); } else if (choice == '2') { stdout.write('Enter expense description: '); String? desc = stdin.readLineSync(); stdout.write('Enter amount: '); String? amountStr = stdin.readLineSync(); double? amount = double.tryParse(amountStr ?? ''); if (desc == null || desc.trim().isEmpty || amount == null || amount < 0) { print('Invalid input. Please enter a valid description and positive amount.'); continue; } expenses.add({'description': desc.trim(), 'amount': amount}); print('Expense added.'); } else if (choice == '3') { double totalIncome = incomes.fold(0.0, (p, e) => p + (e['amount'] as double)); double totalExpense = expenses.fold(0.0, (p, e) => p + (e['amount'] as double)); double net = totalIncome - totalExpense; print('\n--- Summary ---'); print('Total Income: \$${totalIncome.toStringAsFixed(2)}'); print('Total Expenses: \$${totalExpense.toStringAsFixed(2)}'); if (net >= 0) { print('Net Profit: \$${net.toStringAsFixed(2)}'); } else { print('Net Loss: \$${(net.abs()).toStringAsFixed(2)}'); } double tax = net > 0 ? net * 0.15 : 0.0; print(net > 0 ? 'Tax to pay (15%): \$${tax.toStringAsFixed(2)}' : 'No tax (no profit).'); if (incomes.isNotEmpty) { print('\nIncomes:'); for (var i = 0; i < incomes.length; i++) { var it = incomes[i]; print('${i + 1}. ${it['description']} - \$${(it['amount'] as double).toStringAsFixed(2)}'); } } if (expenses.isNotEmpty) { print('\nExpenses:'); for (var i = 0; i < expenses.length; i++) { var it = expenses[i]; print('${i + 1}. ${it['description']} - \$${(it['amount'] as double).toStringAsFixed(2)}'); } } print('----------------'); } else { print('Invalid option. Please try again.'); } } }