Menu
Coddy logo textTech

Hata Yönetimi

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ı sorunlarla zarif bir şekilde başa çıkacak sağlam hata işleme özellikleriyle geliştirelim. ErrorHandler adlı bir sınıf oluşturun ve şu yöntemlere sahip olmasını sağlayın:

  1. ValidateInput(int[][] scoreGrid, int studentIndex, int assignmentIndex): Girdi indekslerini doğrular ve uygun bir hata mesajı veya geçerliyse boş bir dize döndürür.
    • Öğrenci indeksi sınırların dışındaysa "Invalid student index" döndürür.
    • Ödev indeksi sınırların dışındaysa "Invalid assignment index" döndürür.
    • Her iki indeks de geçerliyse boş bir dize "" döndürür.
  2. SafeGetScore(int[][] scoreGrid, int studentIndex, int assignmentIndex): Hata işleme kullanarak bir puanı güvenli şekilde 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 olan her güncelleme için hata mesajlarından oluşan bir string[] dizisi döndürür.
    • Her güncelleme şu özel hata mesajlarından biriyle başarısız olabilir:
      • Güncelleme tam olarak 3 değer içermiyorsa "Invalid update format".
      • Öğrenci indeksi sınırların dışındaysa "Invalid student index".
      • Ödev indeksi sınırların dışındaysa "Invalid assignment index".
      • Puan geçerli aralığın (0-100) dışındaysa "Invalid score value".
    • Her hatayı şu biçimde biçimlendirin: "Error at index X: [specific error message]"
    • Tüm güncellemeler başarılıysa boş bir dizi döndürün.
İpucu: Hataları string[] olarak döndürmeden önce dinamik şekilde toplamak için List<string> errors = new List<string>(); kullanın. List<string>, öğeleri eklemek için errors.Add(...) çağrısı yapabileceğiniz, yeniden boyutlandırılabilir bir dizi gibi çalışır. Sonunda errors.ToArray() ile dönüştürün. Bunun için gereken using System.Collections.Generic; içe aktarması dosyanın en üstüne zaten eklenmiştir.

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)
    {
        // Kodunuzu buraya yazın
        
    }
    
    public static int SafeGetScore(int[][] scoreGrid, int studentIndex, int assignmentIndex)
    {
        // Kodunuzu buraya yazın
        
    }
    
    public static string[] ProcessBatchUpdate(int[][] scoreGrid, int[][] updates)
    {
        // Kodunuzu buraya yazın
        
    }
}

// Test için gerekli - değiştirmeyin
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 et
        if (studentIndex < 0 || studentIndex >= scoreGrid.Length || 
            assignmentIndex < 0 || assignmentIndex >= scoreGrid[studentIndex].Length)
        {
            return -1;
        }
        
        // Skoru doğrula
        if (!DataCollector.ValidateScore(score))
        {
            return -2;
        }
        
        // Skoru ayarla
        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;
        
        // Geçerli skorların toplamını hesapla
        for (int j = 0; j < scoreGrid[studentIndex].Length; j++)
        {
            int score = scoreGrid[studentIndex][j];
            if (score != -1)  // Notlandırılmamış ödevleri yoksay
            {
                sum += score;
                count++;
            }
        }
        
        // Ortalamayı döndür veya geçerli skor yoksa 0
        return count > 0 ? (double)sum / count : 0;
    }
    
    public static double CalculateAssignmentAverage(int[][] scoreGrid, int assignmentIndex)
    {
        // Öğrenci olup olmadığını kontrol et
        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;
        
        // Ödev için geçerli skorların toplamını hesapla
        for (int i = 0; i < scoreGrid.Length; i++)
        {
            if (assignmentIndex < scoreGrid[i].Length)
            {
                int score = scoreGrid[i][assignmentIndex];
                if (score != -1)  // Notlandırılmamış ödevleri yoksay
                {
                    sum += score;
                    count++;
                }
            }
        }
        
        // Ortalamayı döndür veya geçerli skor yoksa 0
        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 skoru ara
        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

Kendi başına pratik yap: Online C# derleyicisi