import 'dart:io'; // --- 1. Transaction Class --- class Transaction { final String description; final double amount; // Positive for income, negative for expense final DateTime date; Transaction(this.description, double value) : amount = value, date = DateTime.now(); bool get isIncome => amount > 0; bool get isExpense => amount < 0; @override String toString() { String type = isIncome ? 'دخل' : 'مصروف'; double absAmount = amount.abs(); return '$type: $description (${absAmount.toStringAsFixed(2)})'; } } // --- 2. Budget Manager Class --- class BudgetManager { final List _transactions = []; static const double taxRate = 0.15; void addTransaction(String description, double amount) { if (amount == 0) { print('خطأ: المبلغ لا يمكن أن يكون صفراً.'); return; } _transactions.add(Transaction(description, amount)); print('تم إضافة المعاملة بنجاح.'); } double get totalIncome => _transactions .where((t) => t.isIncome) .fold(0.0, (sum, t) => sum + t.amount); double get totalExpense => _transactions .where((t) => t.isExpense) .fold(0.0, (sum, t) => sum + t.amount); double get netProfit => totalIncome + totalExpense; // totalExpense is already negative double get taxAmount { double profit = netProfit; return profit > 0 ? profit * taxRate : 0.0; } double get finalBalance => netProfit - taxAmount; void displaySummary() { print('\n--- ملخص الميزانية ---'); print('إجمالي الدخل: ${totalIncome.toStringAsFixed(2)}'); print('إجمالي المصروفات: ${totalExpense.abs().toStringAsFixed(2)}'); print('----------------------------------------'); double profit = netProfit; if (profit >= 0) { print('صافي الربح قبل الضريبة: ${profit.toStringAsFixed(2)}'); print('ضريبة الدخل (${(taxRate * 100).toStringAsFixed(0)}%): ${taxAmount.toStringAsFixed(2)}'); print('صافي الربح بعد الضريبة: ${finalBalance.toStringAsFixed(2)}'); print('تهانينا! رصيدك النهائي هو ${finalBalance.toStringAsFixed(2)}'); } else { print('صافي الخسارة: ${profit.abs().toStringAsFixed(2)}'); print('للأسف، صافي خسارتك هو ${profit.abs().toStringAsFixed(2)}'); } print('\n--- سجل المعاملات ---'); if (_transactions.isEmpty) { print('لا يوجد سجلات معاملات.'); } else { _transactions.forEach((t) => print('- ${t.toString()}')); } } } // --- 3. Main Function and User Interface --- void main() { final manager = BudgetManager(); print('مرحباً بك في تطبيق إدارة الميزانية (Budget Management Application)'); while (true) { print('\nاختر خيارك:'); print('1. إضافة دخل'); print('2. إضافة مصروف'); print('3. عرض الملخص'); print('4. خروج'); stdout.write('أدخل رقم الخيار: '); String? choice = stdin.readLineSync(); if (choice == null) continue; switch (choice.trim()) { case '1': _addTransactionUI(manager, isIncome: true); break; case '2': _addTransactionUI(manager, isIncome: false); break; case '3': manager.displaySummary(); break; case '4': print('شكراً لاستخدامك التطبيق. إلى اللقاء!'); return; default: print('خيار غير صحيح. يرجى اختيار رقم من 1 إلى 4.'); } print('----------------------------------------'); } } void _addTransactionUI(BudgetManager manager, {required bool isIncome}) { String type = isIncome ? 'الدخل' : 'المصروف'; stdout.write('أدخل وصف $type: '); String? description = stdin.readLineSync()?.trim(); if (description == null || description.isEmpty) { print('الوصف لا يمكن أن يكون فارغاً.'); return; } stdout.write('أدخل مبلغ $type (يجب أن يكون موجباً): '); String? amountInput = stdin.readLineSync(); try { double amount = double.parse(amountInput ?? ''); // Input validation: amount must be positive if (amount <= 0) { print('خطأ: يجب أن يكون المبلغ موجباً.'); return; } // Convert expense amount to negative for internal storage double finalAmount = isIncome ? amount : -amount; manager.addTransaction(description, finalAmount); } catch (e) { print('خطأ: إدخال غير صالح للمبلغ. يرجى إدخال رقم.'); } }