Menu
Coddy logo textTech

Error Handling

Coddy'nin C# Journey'sinin Mantık & Akış bölümünün bir parçası — ders 45 / 66.

challenge icon

Görev

Kolay

Sistemimizi olası sorunları zarif bir şekilde ele almak için güçlü hata yönetimi ile geliştirelim. ErrorHandler adında bir sınıf oluşturun ve şu yöntemlere sahip olsun:

  1. ValidateInput(int[][] scoreGrid, int studentIndex, int assignmentIndex): Girdi indekslerini doğrular ve geçerliyse uygun bir hata mesajı veya boş bir dize döndürür.
    • Öğrenci indeksi sınırların dışındaysa "Invalid student index" döndürür.
    • Atama indeksi sınırların dışındaysa "Invalid assignment index" döndürür.
    • Her iki indeks de geçerliyse boş dize "" döndürür.
  2. SafeGetScore(int[][] scoreGrid, int studentIndex, int assignmentIndex): Hata yönetimi ile güvenli bir şekilde bir puanı alır.
    • Her iki indeks de geçerliyse puanı döndürür.
    • Herhangi bir indeks geçersizse -999 döndürür.
  3. ProcessBatchUpdate(int[][] scoreGrid, int[][] updates): Her güncellemenin [studentIndex, assignmentIndex, score] olduğu toplu güncellemeleri işler.
    • Başarısız her güncelleme için hata mesajları içeren bir string[] dizisi döndürür.
    • Her güncelleme şu özel hata mesajlarından biriyle başarısız olabilir:
      • "Invalid update format" — eğer güncelleme tam olarak 3 değer içermiyorsa.
      • "Invalid student index" — eğer öğrenci indeksi sınırların dışındaysa.
      • "Invalid assignment index" — eğer atama indeksi sınırların dışındaysa.
      • "Invalid score value" — eğer puan geçerli aralığın (0–100) dışındaysa.
    • Her hatayı şu formatta oluşturun: "Error at index X: [specific error message]"
    • Tüm güncellemeler başarılıysa boş bir dizi döndürün.
İpucu: string[] olarak döndürmeden önce hataları dinamik olarak toplamak için List<string> errors = new List<string>(); kullanın — List<string>, errors.Add(...) çağırarak öğe ekleyebileceğiniz yeniden boyutlandırılabilir bir dizi gibi çalışır. Sonunda errors.ToArray() ile dönüştürün. Bunun için gerekli using System.Collections.Generic; içe aktarması dosyanın üstünde zaten mevcut.

Kendin dene

using System; // Bu satırı silmeyin
using System.Collections.Generic;

public class ErrorHandler
{
    public static string ValidateInput(int[][] scoreGrid, int studentIndex, int assignmentIndex)
    {
        // Kodu buraya yazın
        
    }
    
    public static int SafeGetScore(int[][] scoreGrid, int studentIndex, int assignmentIndex)
    {
        // Kodu buraya yazın
        
    }
    
    public static string[] ProcessBatchUpdate(int[][] scoreGrid, int[][] updates)
    {
        // Kodu buraya yazın
        
    }
}

// Required for testing - do not modify
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)
    {
        // Sınır dışı indeksleri kontrol edin
        if (studentIndex < 0 || studentIndex >= scoreGrid.Length || 
            assignmentIndex < 0 || assignmentIndex >= scoreGrid[studentIndex].Length)
        {
            return -1;
        }
        
        // Puanı doğrulayın
        if (!DataCollector.ValidateScore(score))
        {
            return -2;
        }
        
        // Puanı ayarlayın
        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)
    {
        // Öğrenci indeksinin geçerli olup olmadığını kontrol edin
        if (studentIndex < 0 || studentIndex >= scoreGrid.Length)
        {
            return -1;
        }
        
        int sum = 0;
        int count = 0;
        
        // Geçerli puanların toplamını hesaplayın
        for (int j = 0; j < scoreGrid[studentIndex].Length; j++)
        {
            int score = scoreGrid[studentIndex][j];
            if (score != -1)  // Puanlanmamış ödevleri yoksayın
            {
                sum += score;
                count++;
            }
        }
        
        // Geçerli puan yoksa ortalama veya 0 döndürün
        return count > 0 ? (double)sum / count : 0;
    }
    
    public static double CalculateAssignmentAverage(int[][] scoreGrid, int assignmentIndex)
    {
        // Herhangi bir öğrenci var mı kontrol edin
        if (scoreGrid.Length == 0)
        {
            return -1;
        }
        
        // Ödev indeksinin geçerli olup olmadığını kontrol edin
        if (assignmentIndex < 0 || assignmentIndex >= scoreGrid[0].Length)
        {
            return -1;
        }
        
        int sum = 0;
        int count = 0;
        
        // Ödev için geçerli puanların toplamını hesaplayın
        for (int i = 0; i < scoreGrid.Length; i++)
        {
            if (assignmentIndex < scoreGrid[i].Length)
            {
                int score = scoreGrid[i][assignmentIndex];
                if (score != -1)  // Puanlanmamış ödevleri yoksayın
                {
                    sum += score;
                    count++;
                }
            }
        }
        
        // Geçerli puan yoksa ortalama veya 0 döndürün
        return count > 0 ? (double)sum / count : 0;
    }
    
    public static int[] FindHighestScore(int[][] scoreGrid)
    {
        int highestStudentIndex = 0;
        int highestAssignmentIndex = 0;
        int highestScore = -1;
        
        // En yüksek puanı arayın
        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 };
    }
}

Mantık & Akış bölümündeki tüm dersler