import 'dart:io'; void main() { print("=== Welcome to the Budget Management System ==="); // Lists to store income and expenses List> incomeList = []; List> expenseList = []; bool running = true; while (running) { print("\nChoose an option:"); print("1) Add Income"); print("2) Add Expense"); print("3) View Summary"); print("4) Exit"); stdout.write("Your choice: "); String? choice = stdin.readLineSync(); if (choice == null) continue; // Control flow using if/else if (choice == "1") { _addEntry(isIncome: true, list: incomeList); } else if (choice == "2") { _addEntry(isIncome: false, list: expenseList); } else if (choice == "3") { _viewSummary(incomeList, expenseList); } else if (choice == "4") { print("Exiting program... Goodbye!"); running = false; } else { print("Invalid option. Try again."); } } } // Function to add income or expense void _addEntry({ required bool isIncome, required List> list, }) { stdout.write("Enter description: "); String? description = stdin.readLineSync(); if (description == null || description.trim().isEmpty) { print("Description cannot be empty."); return; } stdout.write("Enter amount: "); String? amountInput = stdin.readLineSync(); double? amount = double.tryParse(amountInput ?? ""); // Input validation if (amount == null || amount < 0) { print("Invalid amount. Must be a positive number."); return; } list.add({description: amount}); print("${isIncome ? "Income" : "Expense"} added successfully."); } // Function to calculate and display summary void _viewSummary( List> incomeList, List> expenseList, ) { double totalIncome = 0; double totalExpenses = 0; // Calculate totals for (var entry in incomeList) { totalIncome += entry.values.first; } for (var entry in expenseList) { totalExpenses += entry.values.first; } 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)}"); // Optional tax calculation using ternary operator double tax = net > 0 ? net * 0.15 : 0; print("Tax (15%): \$${tax.toStringAsFixed(2)}"); print("Profit after tax: \$${(net - tax).toStringAsFixed(2)}"); } else if (net < 0) { print("Your net loss is \$${net.abs().toStringAsFixed(2)}"); print("No tax applied due to loss."); } else { print("You broke even. No profit, no loss."); } }