// Budget Management CLI - Question 2 // Author: Esraa Mohamed Abduljaleel // ID: 251001040 // Simple Dart console app to manage incomes and expenses. import 'dart:io'; void main() { List> incomeList = []; List> expenseList = []; print('Welcome to Budget Management CLI.'); while (true) { print('\nMenu:'); print('1) Add income'); print('2) Add expense'); print('3) View summary (total income, total expenses, net)'); print('4) Exit'); stdout.write('Enter choice (1-4): '); String? input = stdin.readLineSync(); if (input == null) continue; int? choice = int.tryParse(input); if (choice == null) { print('Invalid input. Enter a number 1-4.'); continue; } if (choice == 4) { print('Exiting Budget CLI. Goodbye!'); break; } else if (choice == 1) { stdout.write('Enter income description: '); String? desc = stdin.readLineSync() ?? ''; double amount = _readPositiveAmount(); incomeList.add({'description': desc, 'amount': amount}); print('Income added.'); } else if (choice == 2) { stdout.write('Enter expense description: '); String? desc = stdin.readLineSync() ?? ''; double amount = _readPositiveAmount(); expenseList.add({'description': desc, 'amount': amount}); print('Expense added.'); } else if (choice == 3) { double totalIncome = incomeList.fold(0.0, (prev, e) => prev + (e['amount'] as double)); double totalExpenses = expenseList.fold(0.0, (prev, e) => prev + (e['amount'] as double)); double net = totalIncome - totalExpenses; print('\n--- Summary ---'); print('Total income: \$${totalIncome.toStringAsFixed(2)}'); print('Total expenses: \$${totalExpenses.toStringAsFixed(2)}'); if (net >= 0) { print('Your net profit is \$${net.toStringAsFixed(2)}'); double tax = net > 0 ? net * 0.15 : 0.0; if (tax > 0) { print('Tax (15% of profit): \$${tax.toStringAsFixed(2)}'); } } else { print('Your net loss is \$${(-net).toStringAsFixed(2)}'); } print('----------------\n'); } else { print('Choice out of range. Please pick 1-4.'); } } } double _readPositiveAmount() { while (true) { stdout.write('Enter amount (positive number): '); String? amtStr = stdin.readLineSync(); double? amt = double.tryParse(amtStr ?? ''); if (amt == null || amt.isNaN || amt <= 0) { print('Invalid amount. Please enter a positive number.'); continue; } return amt; } }