Thuta Learning
ရှာဖွေရန်
ProjectsProgrammingbeginner

Student Manager Project - Part 3: Exceptions & Final Report

စိတ်လျှော့ပါ။ ဒီခန်းကို စာအုပ်လိုမဟုတ်ဘဲ စကားပြောသလိုပဲ၊ နားလည်လွယ်အောင် ရှင်းပါမယ်။

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • Student Manager Project - Part 3: Exceptions & Final Report ကို လက်တွေ့ project ထဲမှာ အသုံးချတတ်မယ်
  • ကိုယ်တိုင် code ရေးပြီး run ကြည့်တတ်မယ်
  • Project တစ်ခုလုံးကို အဆင့်လိုက် ဆောက်တတ်မယ်

ခဏလေး ဒီလိုပဲ စဉ်းစားကြည့်

Project ရဲ့ နောက်ဆုံး Part ဖြစ်ပါတယ်။ Exception handling ကို အသုံးပြုပြီး invalid data (ဥပမာ score က 0-100 range ပြင်ပ) ရောက်လာရင် program crash မဖြစ်ဘဲ graceful ဖြစ်အောင် handle လုပ်ပါမယ်။ Custom exception class InvalidScoreException ကို Exception ကနေ extend လုပ်ပြီးဆောက်ပါမယ်။ Comparator interface ကိုသုံးပြီး student list ကို average score အလိုက် sort လုပ်တာလည်း ဒီ Part ထဲမှာ ထည့်ပါမယ်။ နောက်ဆုံးမှာ student data အားလုံးကို summary table အနေနဲ့ report ထုတ်ပြီး project ပြီးဆုံးပါလိမ့်မယ်။

လက်တွေ့ ဆောက်ကြည့်မယ်

InvalidScoreException class ကို Exception ကနေ extend လုပ်ပြီး constructor ထဲမှာ super(message) ကို ခေါ်ပါ။ StudentManager class ထဲမှာ addScoreSafe(Student s, String subject, int score) method ရေးပြီး score က 0-100 range ပြင်ပဆို InvalidScoreException throw လုပ်ပါ - method signature မှာ throws InvalidScoreException ထည့်ဖို့ မမေ့ပါနဲ့။ printReport() method ထဲမှာ students.sort() နဲ့ Comparator.comparingDouble(Student::calculateAverage).reversed() ကိုသုံးပြီး average အများဆုံးက အပေါ်ဆုံးလာအောင် sort လုပ်ပါ၊ ပြီးရင် System.out.printf() နဲ့ formatted table print ထုတ်ပါ။ main method ထဲမှာ try-catch block နဲ့ addScoreSafe() ကို ခေါ်ပြီး invalid score တစ်ခု ကြိုးစားထည့်ကာ error message catch လုပ်တာကို စမ်းကြည့်ပါ။

Code နမူနာ

java
import java.util.ArrayList;
import java.util.Comparator;

class InvalidScoreException extends Exception {
    public InvalidScoreException(String message) {
        super(message);
    }
}

class StudentManager {
    private ArrayList<Student> students = new ArrayList<>();

    public void addStudent(Student s) {
        students.add(s);
    }

    public void addScoreSafe(Student s, String subject, int score) throws InvalidScoreException {
        if (score < 0 || score > 100) {
            throw new InvalidScoreException(subject + " score must be 0-100, got: " + score);
        }
        s.addScore(subject, score);
    }

    public void printReport() {
        students.sort(Comparator.comparingDouble(Student::calculateAverage).reversed());

        System.out.println("---- Final Report ----");
        for (Student s : students) {
            System.out.printf("%-10s Avg: %.1f Grade: %s%n",
                    s.getName(), s.calculateAverage(), s.getGrade());
        }
    }
}

public class StudentApp {
    public static void main(String[] args) {
        StudentManager manager = new StudentManager();

        Student s1 = new Student("Aung Aung", 101);
        Student s2 = new Student("Su Su", 102);
        manager.addStudent(s1);
        manager.addStudent(s2);

        try {
            manager.addScoreSafe(s1, "Math", 95);
            manager.addScoreSafe(s2, "Math", 120); // invalid
        } catch (InvalidScoreException e) {
            System.out.println("Error: " + e.getMessage());
        }

        manager.printReport();
    }
}
You should see
Console မှာ Error: Math score must be 0-100, got: 120 ဆိုတဲ့ error message ပြီးရင် student list ကို average descending order နဲ့ Final Report table အနေနဲ့ print ထုတ်ပေးပါလိမ့်မယ်။

၅ မိနစ် စမ်းကြည့်

InvalidScoreException အပြင် student name ကို empty string ထည့်ရင် throw လုပ်မယ့် InvalidNameException custom exception ကို ထပ်ဆောက်ပြီး 5 minutes အတွင်း integrate လုပ်ကြည့်ပါ။

သတိလေးတစ်ချက်

Custom exception တွေက checked exception (Exception ကနေ extend) ဖြစ်ရင် caller side မှာ try-catch ဒါမှမဟုတ် throws declaration လိုအပ်ကြောင်း သတိထားပါ။

ဒီနေရာမှာ လူအများမှားတတ်တယ်

  • throws InvalidScoreException ကို method signature မှာ ထည့်ဖို့ မေ့သွားပြီး compile error ဖြစ်ခြင်း
  • try block ထဲမှာ addScoreSafe() ခေါ်တာအပါအဝင် code အားလုံးကို ထည့်မထားလို့ catch block အလုပ်မလုပ်ခြင်း

အခု ကိုယ်တိုင် စမ်းကြည့်

InvalidScoreException အပြင် student name ကို empty string ထည့်ရင် throw လုပ်မယ့် InvalidNameException custom exception ကို ထပ်ဆောက်ပြီး 5 minutes အတွင်း integrate လုပ်ကြည့်ပါ။

You'll know it worked when: Console မှာ Error: Math score must be 0-100, got: 120 ဆိုတဲ့ error message ပြီးရင် student list ကို average descending order နဲ့ Final Report table အနေနဲ့ print ထုတ်ပေးပါလိမ့်မယ်။

Student Manager Project - Part 3: Exceptions & Final Report | Thuta Learning