import 'dart:io'; // Lists to store income and expense entries List> incomeList = []; List> expenseList = []; // Main function to run the application void main() { print('Welcome to the Budget Management Application!'); // Loop for continuous user interaction while (true) { displayMenu(); // Read user choice String? choiceInput = stdin.readLineSync(); int? choice = int.tryParse(choiceInput ?? ''); if (choice == null) { print('Invalid input. Please enter a number from 1 to 4.'); continue; } switch (choice) { case 1: addIncome(); break; case 2: addExpense(); break; case 3: viewSummary(); break; case 4: print('Exiting the application. Goodbye!'); return; // Exit the main function and the program default: print('Invalid choice. Please select an option from 1 to 4.'); } print(''); // Newline for separation } } // Display menu options void displayMenu() { print('Please choose an option:'); print('1. Add income'); print('2. Add expense'); print('3. View summary'); print('4. Exit'); stdout.write('Enter your choice: '); } // Helper function to read a positive double value double? readPositiveDouble(String prompt) { double? value; while (value == null) { stdout.write(prompt); String? input = stdin.readLineSync(); value = double.tryParse(input ?? ''); if (value == null || value <= 0) { print('Invalid amount. Please enter a positive number.'); value = null; // Reset to loop again } } return value; } // 1. Add income void addIncome() { stdout.write('Enter income description: '); String? description = stdin.readLineSync(); if (description == null || description.trim().isEmpty) { print('Description cannot be empty.'); return; } double? amount = readPositiveDouble('Enter income amount: '); if (amount != null) { incomeList.add({'description': description, 'amount': amount}); print('Income added!'); } } // 2. Add expense void addExpense() { stdout.write('Enter expense description: '); String? description = stdin.readLineSync(); if (description == null || description.trim().isEmpty) { print('Description cannot be empty.'); return; } double? amount = readPositiveDouble('Enter expense amount: '); if (amount != null) { expenseList.add({'description': description, 'amount': amount}); print('Expense added!'); } } // 3. View summary void viewSummary() { // Arithmetic Operations double totalIncome = incomeList.fold(0.0, (sum, item) => sum + item['amount']); double totalExpenses = expenseList.fold(0.0, (sum, item) => sum + item['amount']); double netProfit = totalIncome - totalExpenses; // Tax Calculation (Optional) double taxRate = 0.15; // 15% double taxToBePaid = 0.0; // Use Ternary Operator to calculate tax only if there is a net profit taxToBePaid = (netProfit > 0) ? netProfit * taxRate : 0.0; // Use String Interpolation to display the summary print('Summary:'); print('Total income: \$${totalIncome.toStringAsFixed(2)}'); print('Total expenses: \$${totalExpenses.toStringAsFixed(2)}'); // Display Net Profit/Loss if (netProfit >= 0) { print('Net profit: \$${netProfit.toStringAsFixed(2)}'); // Display custom message based on profit String profitMessage = (netProfit >= 5000) ? 'Your net profit is great! (\$${netProfit.toStringAsFixed(2)})' : 'Your net profit is \$${netProfit.toStringAsFixed(2)}'; print(profitMessage); // Display tax if there is a profit if (taxToBePaid > 0) { print('Tax to be paid: \$${taxToBePaid.toStringAsFixed(2)}'); } } else { // Display Net Loss double netLoss = netProfit.abs(); print('Net loss: \$${netLoss.toStringAsFixed(2)}'); // Display custom message based on loss String lossMessage = (netLoss >= 200) ? 'Your net loss is high! (\$${netLoss.toStringAsFixed(2)})' : 'Your net loss is \$${netLoss.toStringAsFixed(2)}'; print(lossMessage); } }