import 'dart:io'; void main() { // Data storage requirement: lists of map entries (description -> amount) final List> incomeList = []; final 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: '); final String choiceInput = stdin.readLineSync() ?? ''; final int? choice = int.tryParse(choiceInput); if (choice == null) { print('Invalid input. Please enter a number (1-4).'); continue; } if (choice == 1) { stdout.write('Enter income description: '); final String desc = (stdin.readLineSync() ?? '').trim(); double amount; while (true) { stdout.write('Enter income amount: '); final String amountInput = stdin.readLineSync() ?? ''; final double? parsed = double.tryParse(amountInput); // Input validation: no negative amounts (logical operators) if (parsed != null && parsed >= 0) { amount = parsed; break; } else { print('Invalid amount. Please enter a non-negative number.'); } } incomeList.add({desc.isEmpty ? 'Income' : desc: amount}); print('Income added!'); } else if (choice == 2) { stdout.write('Enter expense description: '); final String desc = (stdin.readLineSync() ?? '').trim(); double amount; while (true) { stdout.write('Enter expense amount: '); final String amountInput = stdin.readLineSync() ?? ''; final double? parsed = double.tryParse(amountInput); if (parsed != null && parsed >= 0) { amount = parsed; break; } else { print('Invalid amount. Please enter a non-negative number.'); } } expenseList.add({desc.isEmpty ? 'Expense' : desc: amount}); print('Expense added!'); } else if (choice == 3) { // Arithmetic operations: totals + net final double totalIncome = incomeList.fold( 0.0, (sum, entry) => sum + entry.values.first, ); final double totalExpenses = expenseList.fold( 0.0, (sum, entry) => sum + entry.values.first, ); final double net = totalIncome - totalExpenses; print('\nSummary:'); print('Total income: \$${totalIncome.toStringAsFixed(2)}'); print('Total expenses: \$${totalExpenses.toStringAsFixed(2)}'); if (net >= 0) { print('Your net profit is \$${net.toStringAsFixed(2)}'); } else { print('Your net loss is \$${net.abs().toStringAsFixed(2)}'); } // Optional tax calculation: 15% of net profit only if profitable (ternary operator) final double tax = net > 0 ? net * 0.15 : 0.0; if (tax > 0) { print('Tax to be paid (15%): \$${tax.toStringAsFixed(2)}'); } } else if (choice == 4) { print('Exiting the application. Goodbye!'); break; } else { print('Invalid choice. Please enter a number from 1 to 4.'); } } }