import 'dart:io'; // A custom class to represent an income or expense transaction class Transaction { String description; double amount; bool isIncome; // true for income, false for expense Transaction(this.description, this.amount, this.isIncome); } // Data Structure: List to store all transactions (Requirement 2) List transactions = []; // Helper function to read a positive double from the user double? readPositiveAmount(String prompt) { stdout.write(prompt); final input = stdin.readLineSync(); try { final amount = double.parse(input ?? ''); // 4. Input Validation: Ensure amounts are positive (Requirement 4) if (amount > 0) { return amount; } else { print('❌ Amount must be a positive number.'); return null; } } catch (_) { print('❌ Invalid input. Please enter a valid number.'); return null; } } // Main function (Requirement 1) void main() { print('Welcome to the Budget Management Application!'); // 3. User Interaction: Loop for continuous interaction (Requirement 3, Control Flow) while (true) { print('\nPlease choose an option:'); print(' 1. Add Income'); print(' 2. Add Expense'); print(' 3. View Summary'); print(' 4. Exit'); stdout.write('Your choice: '); final choice = stdin.readLineSync(); // 5. Control Flow: Use if/else to handle menu options (Requirement 5) if (choice == '1') { // Add Income (Requirement 3) stdout.write('Enter income description: '); final description = stdin.readLineSync() ?? 'Misc Income'; final amount = readPositiveAmount('Enter income amount: '); if (amount != null) { transactions.add(Transaction(description, amount, true)); print('✅ Income added!'); } } else if (choice == '2') { // Add Expense (Requirement 3) stdout.write('Enter expense description: '); final description = stdin.readLineSync() ?? 'Misc Expense'; final amount = readPositiveAmount('Enter expense amount: '); if (amount != null) { transactions.add(Transaction(description, amount, false)); print('✅ Expense added!'); } } else if (choice == '3') { // View Summary (Requirement 3) viewSummary(); } else if (choice == '4') { print('Exiting the application. Goodbye!'); break; // Exit the loop (Requirement 5) } else { print('❌ Invalid choice. Please select an option from 1 to 4.'); } } } void viewSummary() { // 7. Arithmetic Operations: Calculate totals (Requirement 7) double totalIncome = 0; double totalExpenses = 0; for (final t in transactions) { if (t.isIncome) { totalIncome += t.amount; } else { totalExpenses += t.amount; } } final netProfit = totalIncome - totalExpenses; // 8. Exercise: Tax Calculation (Optional but included) // Use the ternary operator to calculate tax (Requirement 8) final taxRate = 0.15; final taxAmount = netProfit > 0 ? netProfit * taxRate : 0.0; print('\n--- Summary ---'); // 6. String Interpolation: Use to display summary (Requirement 6) print('Total Income: \$${totalIncome.toStringAsFixed(2)}'); print('Total Expenses: \$${totalExpenses.toStringAsFixed(2)}'); // 6. String Interpolation: Display net profit or loss message (Requirement 6) if (netProfit > 0) { print('Net Profit: \$${netProfit.toStringAsFixed(2)}'); print('Your net profit is \$${netProfit.toStringAsFixed(2)}'); // Requirement example print('Tax to be paid: \$${taxAmount.toStringAsFixed(2)} (15% of profit)'); // Requirement 8 } else if (netProfit < 0) { // Math.abs to display loss as a positive amount print('Net Loss: \$${(netProfit * -1).toStringAsFixed(2)}'); print('Your net loss is \$${(netProfit * -1).toStringAsFixed(2)}'); // Requirement example } else { print('Net Balance: \$0.00'); } print('-----------------\n'); }