import 'dart:io'; // Data Storage List> incomeList = []; List> expenseList = []; const double taxRate = 0.15; // 15% tax rate void main() { print("Welcome to the Budget Management Application!"); menuLoop(); print("Exiting the application. Goodbye!"); } // Control Flow & Loops: Main application loop void menuLoop() { String? choice; do { displayMenu(); stdout.write("Enter your choice: "); choice = stdin.readLineSync(); // Control Flow: Use if/else statements (via switch for clarity) to handle menu options switch (choice) { case '1': addEntry(incomeList, "income"); break; case '2': addEntry(expenseList, "expense"); break; case '3': viewSummary(); break; case '4': // Exit handled by do-while loop condition break; default: print("Invalid choice. Please enter a number between 1 and 4."); } print(""); // Separator line for visual clarity } while (choice != '4'); } // Displaying the menu void displayMenu() { print("\nPlease choose an option:"); print("1. Add income"); print("2. Add expense"); print("3. View summary"); print("4. Exit"); } // Income and Expenses Management: Add income or expense entry void addEntry(List> list, String type) { stdout.write("Enter $type description: "); String description = stdin.readLineSync() ?? 'No Description'; double amount = getValidAmount(type); // Storing entry as Map list.add({description: amount}); print("${type[0].toUpperCase()}${type.substring(1)} added!"); } // Input Validation: Ensure non-negative numeric values double getValidAmount(String type) { double? amount; bool isValid = false; while (!isValid) { stdout.write("Enter $type amount: "); String? input = stdin.readLineSync(); amount = double.tryParse(input ?? ''); // Input Validation: Use logical operators to validate inputs (e.g., != null && >= 0) if (amount != null && amount >= 0) { isValid = true; } else { print("Invalid numeric value. Please enter a non-negative number."); } } return amount!; } // View net profit or loss and tax summary void viewSummary() { // Arithmetic Operations: Calculate totals double totalIncome = calculateTotal(incomeList); double totalExpenses = calculateTotal(expenseList); double netProfitOrLoss = totalIncome - totalExpenses; print("\nSummary:"); print("Total income: \$${totalIncome.toStringAsFixed(2)}"); print("Total expenses: \$${totalExpenses.toStringAsFixed(2)}"); // String Interpolation & Operations: Clearly show amounts and messages String statusMessage = (netProfitOrLoss >= 0) ? "Net profit" : "Net loss"; print("$statusMessage: \$${netProfitOrLoss.abs().toStringAsFixed(2)}"); // Exercise: Tax Calculation // Use the ternary operator to ensure the tax is only calculated if the business is profitable. double taxDue = (netProfitOrLoss > 0) ? netProfitOrLoss * taxRate : 0.0; // Display the tax amount to be paid if there is a profit if (netProfitOrLoss > 0) { print("Tax to be paid: \$${taxDue.toStringAsFixed(2)}"); } } // Helper function for Arithmetic Operations: Calculate total sum of a list double calculateTotal(List> list) { double total = 0.0; for (var entry in list) { // Summing the amounts (values) from the maps total += entry.values.first; } return total; }