import 'dart:io'; // 1. تعريف كلاس للعملية المالية (Transaction) class Transaction { String description; double amount; bool isIncome; // true للدخل، false للمصروف Transaction(this.description, this.amount, this.isIncome); } // 2. كلاس لإدارة الميزانية (BudgetManager) يحتوي على المنطق البرمجي class BudgetManager { List transactions = []; // إضافة عملية ماليّة void addTransaction(String desc, double amt, bool income) { transactions.add(Transaction(desc, amt, income)); print("✅ تم الحفظ بنجاح."); } // حساب المجموع باستخدام الدوال المتقدمة (fold) بدلاً من for loop العادية double getTotalIncome() { return transactions .where((t) => t.isIncome) .fold(0, (sum, t) => sum + t.amount); } double getTotalExpenses() { return transactions .where((t) => !t.isIncome) .fold(0, (sum, t) => sum + t.amount); } // عرض التقرير المالي void printReport() { double income = getTotalIncome(); double expenses = getTotalExpenses(); double balance = income - expenses; double tax = balance > 0 ? balance * 0.15 : 0; print("\n========= التقرير المالي ========="); print("إجمالي الدخل: \$${income.toStringAsFixed(2)}"); print("إجمالي المصاريف: \$${expenses.toStringAsFixed(2)}"); print("الرصيد الصافي: \$${balance.toStringAsFixed(2)}"); if (balance > 0) { print("الضريبة (15%): \$${tax.toStringAsFixed(2)}"); print("الصافي النهائي: \$${(balance - tax).toStringAsFixed(2)}"); } print("================================="); } } void main() { BudgetManager myBudget = BudgetManager(); bool running = true; print("🚀 نظام إدارة الميزانية الذكي (نسخة OOP)"); while (running) { print("\n1. دخل | 2. مصروف | 3. تقرير | 4. خروج"); stdout.write("اختر العملية: "); String? choice = stdin.readLineSync(); if (choice == '4') { running = false; continue; } if (choice == '3') { myBudget.printReport(); continue; } // إدخال البيانات للخيارات 1 و 2 stdout.write("الوصف: "); String desc = stdin.readLineSync() ?? "بدون عنوان"; stdout.write("المبلغ: "); double? amt = double.tryParse(stdin.readLineSync() ?? ""); if (amt == null || amt <= 0) { print("⚠️ خطأ: مبلغ غير صحيح!"); continue; } if (choice == '1') { myBudget.addTransaction(desc, amt, true); } else if (choice == '2') { myBudget.addTransaction(desc, amt, false); } else { print("❌ خيار غير معروف."); } } }