import 'dart:io'; void main() { print('Welcome to Budget Manager CLI!\n'); List> incomeList = []; List> expenseList = []; bool running = true; while (running) { print('\nMenu:'); print('1. Add Income'); print('2. Add Expense'); print('3. View Summary'); print('4. Exit'); stdout.write('Choose an option (1-4): '); String choice = (stdin.readLineSync() ?? '').trim(); if (choice == '1') { _addEntry(incomeList, isIncome: true); } else if (choice == '2') { _addEntry(expenseList, isIncome: false); } else if (choice == '3') { _viewSummary(incomeList, expenseList); } else if (choice == '4') { running = false; print('Exiting Budget Manager. Goodbye!'); } else { print('Invalid choice. Enter 1, 2, 3, or 4.'); } } } void _addEntry(List> list, {required bool isIncome}) { String type = isIncome ? 'income' : 'expense'; stdout.write('Enter $type description: '); String desc = (stdin.readLineSync() ?? '').trim(); if (desc.isEmpty) { print('Description cannot be empty. Aborting.'); return; } stdout.write('Enter amount for "$desc": '); String amtRaw = (stdin.readLineSync() ?? '').trim(); double? amount = double.tryParse(amtRaw); if (amount == null) { print('Invalid amount. Please enter a numeric value.'); return; } if (amount < 0) { print('Amount cannot be negative.'); return; } list.add({'description': desc, 'amount': amount}); print('${isIncome ? "Income" : "Expense"} added: "$desc" — \$${amount.toStringAsFixed(2)}'); } void _viewSummary(List> incomes, List> expenses) { double totalIncome = 0.0; double totalExpenses = 0.0; for (var entry in incomes) { var amt = entry['amount']; if (amt is num) totalIncome += amt.toDouble(); } for (var entry in expenses) { var amt = entry['amount']; if (amt is num) totalExpenses += amt.toDouble(); } 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)}'); double tax = net > 0 ? net * 0.15 : 0.0; print(tax > 0 ? 'Tax to pay (15%): \$${tax.toStringAsFixed(2)}' : 'No tax to be paid.'); } else { print('Your net loss is \$${(net.abs()).toStringAsFixed(2)}'); print('No tax (losses do not incur tax).'); } if (incomes.isNotEmpty) { print('\nIncome entries:'); for (var e in incomes) { print('- ${e['description']}: \$${(e['amount'] as num).toStringAsFixed(2)}'); } } else { print('\nNo income entries.'); } if (expenses.isNotEmpty) { print('\nExpense entries:'); for (var e in expenses) { print('- ${e['description']}: \$${(e['amount'] as num).toStringAsFixed(2)}'); } } else { print('\nNo expense entries.'); } print('-----------------------'); }