import 'dart:io'; const double TAX_RATE = 0.15; void main() { // قوائم تخزين الدخل والمصروفات: كل عنصر هو Map حيث المفتاح = الوصف، القيمة = المبلغ List> incomeList = []; List> expenseList = []; print('Welcome to the Budget Management Application!\n'); bool running = true; while (running) { showMenu(); stdout.write('\nEnter your choice: '); String? input = stdin.readLineSync(); int? choice = input != null ? int.tryParse(input.trim()) : null; if (choice == null) { print('Invalid input. Please enter a number between 1 and 4.\n'); continue; } if (choice == 1) { // Add income stdout.write('Enter income description: '); String? desc = stdin.readLineSync(); if (desc == null || desc.trim().isEmpty) { print('Description cannot be empty.\n'); continue; } stdout.write('Enter income amount: '); String? amtStr = stdin.readLineSync(); double? amount = _parsePositiveDouble(amtStr); if (amount == null) { print('Invalid amount. Please enter a non-negative number.\n'); continue; } incomeList.add({desc.trim(): amount}); print('Income added!\n'); } else if (choice == 2) { // Add expense stdout.write('Enter expense description: '); String? desc = stdin.readLineSync(); if (desc == null || desc.trim().isEmpty) { print('Description cannot be empty.\n'); continue; } stdout.write('Enter expense amount: '); String? amtStr = stdin.readLineSync(); double? amount = _parsePositiveDouble(amtStr); if (amount == null) { print('Invalid amount. Please enter a non-negative number.\n'); continue; } expenseList.add({desc.trim(): amount}); print('Expense added!\n'); } else if (choice == 3) { // View summary double totalIncome = _sumAmounts(incomeList); double totalExpenses = _sumAmounts(expenseList); double net = totalIncome - totalExpenses; print('\nSummary:'); print('Total income: \$${_formatMoney(totalIncome)}'); print('Total expenses: \$${_formatMoney(totalExpenses)}'); if (net >= 0) { print('Net profit: \$${_formatMoney(net)}'); } else { print('Net loss: \$${_formatMoney(net.abs())}'); } // حساب الضريبة (15%) باستخدام ternary: تُحسب فقط لو كانت ربح double tax = net > 0 ? net * TAX_RATE : 0.0; if (tax > 0) { print('Tax to be paid (15%): \$${_formatMoney(tax)}'); } print(''); } else if (choice == 4) { print('Exiting the application.'); running = false; } else { print('Choice out of range. Please choose between 1 and 4.\n'); } } } void showMenu() { print('Please choose an option:'); print('1. Add income'); print('2. Add expense'); print('3. View summary'); print('4. Exit'); } /// يحسب مجموع المبالغ داخل قائمة من Map double _sumAmounts(List> list) { double total = 0.0; for (var map in list) { for (var value in map.values) { total += value; } } return total; } /// يحاول تحويل النص إلى double ويتحقق أنه غير سالب. /// يرجع null إذا فشل أو كان سالباً. double? _parsePositiveDouble(String? s) { if (s == null) return null; double? val = double.tryParse(s.trim()); if (val == null) return null; if (val < 0) return null; return val; } /// تنسيق مبالغ بعرض رقمين بعد الفاصلة (للطباعة) String _formatMoney(double v) { return v.toStringAsFixed(2); }