import 'dart:io'; void main() { // Welcome message print('Welcome to the Budget Management Application!'); // Data storage: Lists of maps for incomes and expenses List> incomes = []; List> expenses = []; bool running = true; while (running) { print('\nMenu Options:'); print('1. Add Income'); print('2. Add Expense'); print('3. View Summary'); print('4. Exit'); stdout.write('Choose an option (1-4): '); String? input = stdin.readLineSync(); int? choice = int.tryParse(input ?? ''); if (choice == null || choice < 1 || choice > 4) { print('Invalid choice. Please enter a number between 1 and 4.'); continue; } // Use if/else for menu options if (choice == 1) { // Add Income stdout.write('Enter income description: '); String? description = stdin.readLineSync(); if (description == null || description.trim().isEmpty) { print('Description cannot be empty.'); continue; } stdout.write('Enter income amount: '); String? amountInput = stdin.readLineSync(); double? amount = double.tryParse(amountInput ?? ''); if (amount == null || amount <= 0) { print('Invalid amount. Please enter a positive number.'); } else { incomes.add({'description': description, 'amount': amount}); print('Income added successfully.'); } } else if (choice == 2) { // Add Expense stdout.write('Enter expense description: '); String? description = stdin.readLineSync(); if (description == null || description.trim().isEmpty) { print('Description cannot be empty.'); continue; } stdout.write('Enter expense amount: '); String? amountInput = stdin.readLineSync(); double? amount = double.tryParse(amountInput ?? ''); if (amount == null || amount <= 0) { print('Invalid amount. Please enter a positive number.'); } else { expenses.add({'description': description, 'amount': amount}); print('Expense added successfully.'); } } else if (choice == 3) { // View Summary double totalIncome = incomes.fold(0.0, (sum, item) => sum + item['amount']); double totalExpenses = expenses.fold(0.0, (sum, item) => sum + item['amount']); double net = totalIncome - totalExpenses; print('\nBudget Summary:'); print('Total Income: \$${totalIncome.toStringAsFixed(2)}'); print('Total Expenses: \$${totalExpenses.toStringAsFixed(2)}'); if (net >= 0) { print('Net Profit: \$${net.toStringAsFixed(2)}'); // Tax calculation using ternary operator double tax = (net > 0) ? net * 0.15 : 0.0; if (tax > 0) { print('Tax to be paid (15% of profit): \$${tax.toStringAsFixed(2)}'); } } else { print('Net Loss: \$${(-net).toStringAsFixed(2)}'); } // Optional: List details if (incomes.isNotEmpty) { print('\nIncome Details:'); for (var income in incomes) { print('- ${income['description']}: \$${income['amount'].toStringAsFixed(2)}'); } } if (expenses.isNotEmpty) { print('\nExpense Details:'); for (var expense in expenses) { print('- ${expense['description']}: \$${expense['amount'].toStringAsFixed(2)}'); } } } else if (choice == 4) { // Exit running = false; print('Exiting the Budget Management Application. Goodbye!'); } } }