import 'dart:io'; void main() { // Lists to store income and expense entries List> incomeList = []; List> expenseList = []; // Tax rate constant const double taxRate = 0.15; print('Welcome to the Budget Management Application!'); // Main loop for user interaction while (true) { print('\nPlease choose an option:'); print('1. Add income'); print('2. Add expense'); print('3. View summary'); print('4. Exit'); stdout.write('\nEnter your choice: '); String? choice = stdin.readLineSync(); if (choice == '1') { // Add income addIncome(incomeList); } else if (choice == '2') { // Add expense addExpense(expenseList); } else if (choice == '3') { // View summary viewSummary(incomeList, expenseList, taxRate); } else if (choice == '4') { // Exit program print('Exiting the application. Goodbye!'); break; } else { print('Invalid choice! Please enter a number between 1 and 4.'); } } } // Function to add income void addIncome(List> incomeList) { stdout.write('Enter income description: '); String? description = stdin.readLineSync(); if (description == null || description.isEmpty) { print('Invalid description!'); return; } double amount = getValidAmount('Enter income amount: '); if (amount > 0) { incomeList.add({ 'description': description, 'amount': amount }); print('Income added!'); } else { print('Income amount must be positive!'); } } // Function to add expense void addExpense(List> expenseList) { stdout.write('Enter expense description: '); String? description = stdin.readLineSync(); if (description == null || description.isEmpty) { print('Invalid description!'); return; } double amount = getValidAmount('Enter expense amount: '); if (amount > 0) { expenseList.add({ 'description': description, 'amount': amount }); print('Expense added!'); } else { print('Expense amount must be positive!'); } } // Function to get valid amount from user double getValidAmount(String prompt) { while (true) { stdout.write(prompt); String? amountInput = stdin.readLineSync(); try { double amount = double.parse(amountInput!); if (amount >= 0) { return amount; } else { print('Amount cannot be negative! Please enter a positive value.'); } } catch (e) { print('Invalid input! Please enter a valid number.'); } } } // Function to calculate total from a list double calculateTotal(List> list) { double total = 0; for (var item in list) { total += item['amount']; } return total; } // Function to view summary void viewSummary(List> incomeList, List> expenseList, double taxRate) { double totalIncome = calculateTotal(incomeList); double totalExpenses = calculateTotal(expenseList); double netProfit = totalIncome - totalExpenses; print('\nSummary:'); print('Total income: \$${totalIncome.toStringAsFixed(2)}'); print('Total expenses: \$${totalExpenses.toStringAsFixed(2)}'); if (netProfit >= 0) { print('Net profit: \$${netProfit.toStringAsFixed(2)}'); // Tax calculation using ternary operator (only if profitable) double tax = netProfit > 0 ? netProfit * taxRate : 0; if (tax > 0) { print('Tax to be paid: \$${tax.toStringAsFixed(2)}'); } } else { print('Net loss: \$${netProfit.abs().toStringAsFixed(2)}'); } // Display detailed lists if available if (incomeList.isNotEmpty) { print('\nIncome Details:'); for (var income in incomeList) { print('- ${income['description']}: \$${income['amount'].toStringAsFixed(2)}'); } } if (expenseList.isNotEmpty) { print('\nExpense Details:'); for (var expense in expenseList) { print('- ${expense['description']}: \$${expense['amount'].toStringAsFixed(2)}'); } } }