// budget.dart 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? choice = stdin.readLineSync(); if (choice == null) continue; switch (choice.trim()) { case '1': _addIncome(incomeList); break; case '2': _addExpense(expenseList); break; case '3': _viewSummary(incomeList, expenseList); break; case '4': print('\nExiting the application. Goodbye!'); return; default: print('Invalid choice. Please enter 1, 2, 3, or 4.'); } } } void _addIncome(List> incomeList) { stdout.write('Enter income description: '); String? desc = stdin.readLineSync(); if (desc == null || desc.trim().isEmpty) { print('Description cannot be empty. Income not added.'); return; } stdout.write('Enter income amount: '); String? amtStr = stdin.readLineSync(); double? amount = _parsePositiveDouble(amtStr); if (amount == null) { print('Invalid amount. Income not added.'); return; } incomeList.add({'description': desc.trim(), 'amount': amount}); print('Income added!'); } void _addExpense(List> expenseList) { stdout.write('Enter expense description: '); String? desc = stdin.readLineSync(); if (desc == null || desc.trim().isEmpty) { print('Description cannot be empty. Expense not added.'); return; } stdout.write('Enter expense amount: '); String? amtStr = stdin.readLineSync(); double? amount = _parsePositiveDouble(amtStr); if (amount == null) { print('Invalid amount. Expense not added.'); return; } expenseList.add({'description': desc.trim(), 'amount': amount}); print('Expense added!'); } double? _parsePositiveDouble(String? input) { if (input == null) return null; double? val = double.tryParse(input.trim()); if (val == null) return null; if (val < 0) return null; return val; } void _viewSummary(List> incomeList, List> expenseList) { double totalIncome = incomeList.fold(0.0, (sum, item) => sum + (item['amount'] as double)); double totalExpenses = expenseList.fold(0.0, (sum, item) => sum + (item['amount'] as double)); 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)}'); double tax = net > 0 ? net * 0.15 : 0.0; if (tax > 0) { print('Tax to be paid (15%): \$${tax.toStringAsFixed(2)}'); } else { print('No tax to be paid.'); } } else { print('Net loss: \$${(-net).toStringAsFixed(2)}'); print('No tax (business made a loss).'); } }