// Import dart:io to allow reading input from the console import 'dart:io'; void main() { // Constant for tax rate const double TAX_RATE = 0.15; // Lists to store income and expense entries List> incomeList = []; List> expenseList = []; // Welcome message print('Welcome to the Budget Management Application!'); // Infinite loop to continuously show menu until user exits while (true) { // Display menu options print('\n--- Menu ---'); print('1. Add Income'); print('2. Add Expense'); print('3. View Summary'); print('4. Exit'); // Prompt user to enter choice stdout.write('Enter your choice (1-4): '); String? choice = stdin.readLineSync(); if (choice == null) continue; // Skip if input is null // Handle user's choice using switch switch (choice) { // Option 1: Add income case '1': stdout.write('Enter income description: '); String? desc = stdin.readLineSync(); stdout.write('Enter income amount: '); String? amtStr = stdin.readLineSync(); double? amt = double.tryParse(amtStr ?? ''); // Validate input if (desc == null || desc.trim().isEmpty) { print('Invalid description. Operation cancelled.'); } else if (amt == null || amt < 0) { print('Invalid amount. Please enter a non-negative number.'); } else { incomeList.add({desc.trim(): amt}); print('Income added: ${desc.trim()} - \$${amt.toStringAsFixed(2)}'); } break; // Option 2: Add expense case '2': stdout.write('Enter expense description: '); String? edesc = stdin.readLineSync(); stdout.write('Enter expense amount: '); String? eamtStr = stdin.readLineSync(); double? eamt = double.tryParse(eamtStr ?? ''); // Validate input if (edesc == null || edesc.trim().isEmpty) { print('Invalid description. Operation cancelled.'); } else if (eamt == null || eamt < 0) { print('Invalid amount. Please enter a non-negative number.'); } else { expenseList.add({edesc.trim(): eamt}); print('Expense added: ${edesc.trim()} - \$${eamt.toStringAsFixed(2)}'); } break; // Option 3: View summary (total income, expenses, net profit/loss) case '3': double totalIncome = 0.0; for (var item in incomeList) { totalIncome += item.values.first; } double totalExpenses = 0.0; for (var item in expenseList) { totalExpenses += item.values.first; } double net = totalIncome - totalExpenses; print('\n--- Summary ---'); print('Total Income: \$${totalIncome.toStringAsFixed(2)}'); print('Total Expenses: \$${totalExpenses.toStringAsFixed(2)}'); // Display net profit/loss and tax if applicable if (net > 0) { print('Your net profit is \$${net.toStringAsFixed(2)}'); double tax = net * TAX_RATE; // 15% tax print('Tax to pay (15%): \$${tax.toStringAsFixed(2)}'); } else if (net < 0) { print('Your net loss is \$${(net.abs()).toStringAsFixed(2)}'); } else { print('You broke even (no profit, no loss).'); } break; // Option 4: Exit the program case '4': print('Exiting program. Goodbye!'); return; // Handle invalid choices default: print('Invalid choice. Please select 1-4.'); } } }