Menu
Coddy logo textTech
flag Ar iconالعربيةdown icon

معالجة الأخطاء

جزء من قسم المنطق وتدفق التنفيذ في رحلة C# على Coddy. الدرس 45 من 66.

challenge icon

التحدي

سهل

لنُحسّن نظامنا بمعالجة قوية للأخطاء للتعامل مع المشكلات المحتملة بسلاسة. أنشئ فئة باسم ErrorHandler باستخدام هذه الأساليب:

  1. ValidateInput(int[][] scoreGrid, int studentIndex, int assignmentIndex): تتحقق من فهارس الإدخال وتُرجع رسالة الخطأ المناسبة أو سلسلة فارغة إذا كانت صالحة.
    • تُرجع "Invalid student index" إذا كان فهرس الطالب خارج الحدود.
    • تُرجع "Invalid assignment index" إذا كان فهرس الواجب خارج الحدود.
    • تُرجع سلسلة فارغة "" إذا كان كلا الفهرسين صالحًا.
  2. SafeGetScore(int[][] scoreGrid, int studentIndex, int assignmentIndex): تسترجع الدرجة بأمان مع معالجة الأخطاء.
    • تُرجع الدرجة إذا كان كلا الفهرسين صالحًا.
    • تُرجع -999 إذا كان أي فهرس غير صالح.
  3. ProcessBatchUpdate(int[][] scoreGrid, int[][] updates): تعالج تحديثات دفعية، حيث يكون كل تحديث هو [studentIndex, assignmentIndex, score].
    • تُرجع مصفوفة string[] من رسائل الخطأ لكل تحديث فاشل.
    • يمكن أن يفشل كل تحديث بإحدى رسائل الخطأ المحددة التالية:
      • "Invalid update format"، إذا لم يحتوِ التحديث على 3 قيم بالضبط.
      • "Invalid student index": إذا كان فهرس الطالب خارج الحدود.
      • "Invalid assignment index": إذا كان فهرس الواجب خارج الحدود.
      • "Invalid score value": إذا كانت الدرجة خارج النطاق الصالح (0-100).
    • نسّق كل خطأ كما يلي: "Error at index X: [specific error message]"
    • أرجِع مصفوفة فارغة إذا نجحت جميع التحديثات.
تلميح: لجمع الأخطاء ديناميكيًا قبل إرجاعها على هيئة string[]، استخدم List<string> errors = new List<string>();. تعمل List<string> مثل مصفوفة قابلة لتغيير الحجم، حيث يمكنك استدعاء errors.Add(...) لإضافة عناصر. في النهاية، حوّلها باستخدام errors.ToArray(). إن استيراد using System.Collections.Generic; المطلوب لذلك مُضمَّن بالفعل في أعلى الملف.

جرّب بنفسك

using System; // لا تحذف هذا السطر
using System.Collections.Generic;

public class ErrorHandler
{
    public static string ValidateInput(int[][] scoreGrid, int studentIndex, int assignmentIndex)
    {
        // اكتب الكود هنا
        
    }
    
    public static int SafeGetScore(int[][] scoreGrid, int studentIndex, int assignmentIndex)
    {
        // اكتب الكود هنا
        
    }
    
    public static string[] ProcessBatchUpdate(int[][] scoreGrid, int[][] updates)
    {
        // اكتب الكود هنا
        
    }
}

// مطلوب للاختبار - لا تعدّل
public class DataCollector
{
    public static int[][] CreateScoreGrid(int students, int assignments)
    {
        int[][] scoreGrid = new int[students][];
        for (int i = 0; i < students; i++)
        {
            scoreGrid[i] = new int[assignments];
        }
        return scoreGrid;
    }
    
    public static bool ValidateScore(int score)
    {
        return score >= 0 && score <= 100;
    }
    
    public static int[][] PopulateWithDefaultValues(int[][] scoreGrid)
    {
        for (int i = 0; i < scoreGrid.Length; i++)
        {
            for (int j = 0; j < scoreGrid[i].Length; j++)
            {
                scoreGrid[i][j] = -1;
            }
        }
        return scoreGrid;
    }
}

public class DataEntry
{
    public static int SetStudentScore(int[][] scoreGrid, int studentIndex, int assignmentIndex, int score)
    {
        // تحقق من الفهارس الخارجة عن الحدود
        if (studentIndex < 0 || studentIndex >= scoreGrid.Length || 
            assignmentIndex < 0 || assignmentIndex >= scoreGrid[studentIndex].Length)
        {
            return -1;
        }
        
        // تحقق من صحة الدرجة
        if (!DataCollector.ValidateScore(score))
        {
            return -2;
        }
        
        // عيّن الدرجة
        scoreGrid[studentIndex][assignmentIndex] = score;
        return 0;
    }
    
    public static int UpdateAllScores(int[][] scoreGrid, int[] studentIndices, int assignmentIndex, int score)
    {
        int successCount = 0;
        
        for (int i = 0; i < studentIndices.Length; i++)
        {
            int result = SetStudentScore(scoreGrid, studentIndices[i], assignmentIndex, score);
            if (result == 0)
            {
                successCount++;
            }
        }
        
        return successCount;
    }
}

public class DataAnalyzer
{
    public static double CalculateStudentAverage(int[][] scoreGrid, int studentIndex)
    {
        // Check if student index is valid
        if (studentIndex < 0 || studentIndex >= scoreGrid.Length)
        {
            return -1;
        }
        
        int sum = 0;
        int count = 0;
        
        // احسب مجموع الدرجات الصالحة
        for (int j = 0; j < scoreGrid[studentIndex].Length; j++)
        {
            int score = scoreGrid[studentIndex][j];
            if (score != -1)  // تجاهل الواجبات غير المُقيَّمة
            {
                sum += score;
                count++;
            }
        }
        
        // أرجع المتوسط أو 0 إذا لم تكن هناك درجات صالحة
        return count > 0 ? (double)sum / count : 0;
    }
    
    public static double CalculateAssignmentAverage(int[][] scoreGrid, int assignmentIndex)
    {
        // تحقق مما إذا كان هناك أي طلاب
        if (scoreGrid.Length == 0)
        {
            return -1;
        }
        
        // Check if assignment index is valid
        if (assignmentIndex < 0 || assignmentIndex >= scoreGrid[0].Length)
        {
            return -1;
        }
        
        int sum = 0;
        int count = 0;
        
        // احسب مجموع الدرجات الصالحة للواجب
        for (int i = 0; i < scoreGrid.Length; i++)
        {
            if (assignmentIndex < scoreGrid[i].Length)
            {
                int score = scoreGrid[i][assignmentIndex];
                if (score != -1)  // تجاهل الواجبات غير المُقيَّمة
                {
                    sum += score;
                    count++;
                }
            }
        }
        
        // أرجع المتوسط أو 0 إذا لم تكن هناك درجات صالحة
        return count > 0 ? (double)sum / count : 0;
    }
    
    public static int[] FindHighestScore(int[][] scoreGrid)
    {
        int highestStudentIndex = 0;
        int highestAssignmentIndex = 0;
        int highestScore = -1;
        
        // ابحث عن أعلى درجة
        for (int i = 0; i < scoreGrid.Length; i++)
        {
            for (int j = 0; j < scoreGrid[i].Length; j++)
            {
                int currentScore = scoreGrid[i][j];
                if (currentScore > highestScore)
                {
                    highestScore = currentScore;
                    highestStudentIndex = i;
                    highestAssignmentIndex = j;
                }
            }
        }
        
        return new int[] { highestStudentIndex, highestAssignmentIndex, highestScore };
    }
}

جميع دروس المنطق وتدفق التنفيذ

تدرّب بنفسك: مترجم C# عبر الإنترنت