import 'dart:io'; // 3. Data Storage: Use Lists to store income and expenses List> incomeList = []; List> expenseList = []; // Function to display the main menu void displayMenu() { print('\nPlease choose an option:'); print('1. Add income'); print('2. Add expense'); print('3. View summary'); print('4. Exit'); } // Function to read and validate monetary amounts double? readAmount(String type) { stdout.write('Enter $type amount: '); String? input = stdin.readLineSync(); try { double amount = double.parse(input!); // 4. Input Validation: Check for a positive number if (amount > 0) { return amount; } else { print('Invalid amount. Please enter a positive number.'); return null; } } catch (e) { print('Invalid input. Please enter a valid number.'); return null; } } // 2. Income and Expenses Management: 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 = readAmount('income'); if (amount != null) { // 3. Data Storage: Store income as a Map incomeList.add({'description': description, 'amount': amount}); print('Income added!'); } } // 2. Income and Expenses Management: 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 = readAmount('expense'); if (amount != null) { // 3. Data Storage: Store expense as a Map expenseList.add({'description': description, 'amount': amount}); print('Expense added!'); } } // 2. Income and Expenses Management: View summary void viewSummary() { // 7. Arithmetic Operations: Calculate totals double totalIncome = 0; for (var income in incomeList) { totalIncome += income['amount'] as double; } double totalExpenses = 0; for (var expense in expenseList) { totalExpenses += expense['amount'] as double; } double netProfit = totalIncome - totalExpenses; // Calculate net profit/loss print('\nSummary:'); // 6. String Interpolation: Display totals print('Total Income: \$${totalIncome.toStringAsFixed(2)}'); print('Total Expenses: \$${totalExpenses.toStringAsFixed(2)}'); if (netProfit > 0) { print('Net Profit: \$${netProfit.toStringAsFixed(2)}'); // 8. Tax Calculation (Optional): Use the ternary operator // Calculate 15% tax only if netProfit is greater than 0 double tax = netProfit > 0 ? netProfit * 0.15 : 0; print('Tax to be paid: \$${tax.toStringAsFixed(2)}'); } else if (netProfit < 0) { // Loss print('Net Loss: \$${(netProfit.abs()).toStringAsFixed(2)}'); } else { print('Net Balance: \$0.00'); } } // 1. Main Method: Program entry point void main() { print('Welcome to the Budget Management Application CLI!'); // 5. Control Flow & Loops: Use a loop for continuous interaction while (true) { displayMenu(); stdout.write('Enter your choice: '); String? choice = stdin.readLineSync(); // 5. Use if/else statements to handle menu options. if (choice == '4') { print('Exiting the application. Goodbye!'); break; } else if (choice == '1') { addIncome(); } else if (choice == '2') { addExpense(); } else if (choice == '3') { viewSummary(); } else { print('Invalid choice. Please select an option from 1 to 4.'); } } }