import 'dart:io'; void main() { List> incomeList = []; List> expenseList = []; bool isRunning = true; print('Welcome to the Budget Management Application!'); while (isRunning) { print('\nPlease choose an option:\n'); print('1. Add income'); print('2. Add expense'); print('3. View summary'); print('4. Exit'); stdout.write('\nEnter your choice: '); String choice = stdin.readLineSync()?.trim() ?? ''; if (choice == '1') { addIncome(incomeList); } else if (choice == '2') { addExpense(expenseList); } else if (choice == '3') { viewSummary(incomeList, expenseList); } else if (choice == '4') { print('Exiting the application. Goodbye!'); isRunning = false; } else { print('Invalid choice. Please try again.'); } } } void addIncome(List> incomeList) { stdout.write('Enter income description: '); String description = stdin.readLineSync()?.trim() ?? ''; double amount = readValidAmount('Enter income amount: '); incomeList.add({ 'description': description, 'amount': amount, }); print('Income added!'); } void addExpense(List> expenseList) { stdout.write('Enter expense description: '); String description = stdin.readLineSync()?.trim() ?? ''; double amount = readValidAmount('Enter expense amount: '); expenseList.add({ 'description': description, 'amount': amount, }); print('Expense added!'); } double readValidAmount(String message) { while (true) { stdout.write(message); String input = stdin.readLineSync()?.trim() ?? ''; double? amount = double.tryParse(input); if (amount != null && amount >= 0) { return amount; } else { print('Invalid amount. Please enter a non-negative number.'); } } } void viewSummary( List> incomeList, List> expenseList, ) { double totalIncome = 0; double totalExpenses = 0; for (int i = 0; i < incomeList.length; i++) { totalIncome += incomeList[i]['amount']; } for (int i = 0; i < expenseList.length; i++) { totalExpenses += expenseList[i]['amount']; } double netResult = totalIncome - totalExpenses; double tax = netResult > 0 ? netResult * 0.15 : 0; print('\nSummary:'); print('Total income: \$${formatAmount(totalIncome)}'); print('Total expenses: \$${formatAmount(totalExpenses)}'); if (netResult > 0) { print('Net profit: \$${formatAmount(netResult)}'); print('Tax to be paid: \$${formatAmount(tax)}'); } else if (netResult < 0) { print('Net loss: \$${formatAmount(netResult.abs())}'); } else { print('Net profit: \$0'); } } String formatAmount(double amount) { if (amount == amount.roundToDouble()) { return amount.toStringAsFixed(0); } else { return amount.toStringAsFixed(2); } }