Загрузка данных


class DataValidationException extends RuntimeException {
    public DataValidationException(String message) {
        super(message);
    }
}

abstract class Learner implements Comparable<Learner> {
    String surname;
    String name;
    int[] grades;

    public Learner(String surname, String name, int[] grades) {
        setSurname(surname);
        setName(name);
        setGrades(grades);
    }

    public void setSurname(String surname) {
        if (surname == null || surname.trim().isEmpty()) {
            throw new DataValidationException("Фамилия не может быть пустой.");
        }
        if (!Character.isUpperCase(surname.charAt(0))) {
            throw new DataValidationException("Фамилия должна начинаться с большой буквы: " + surname);
        }
        this.surname = surname;
    }

    public void setName(String name) {
        if (name == null || name.trim().isEmpty()) {
            throw new DataValidationException("Имя не может быть пустым.");
        }
        if (!Character.isUpperCase(name.charAt(0))) {
            throw new DataValidationException("Имя должно начинаться с большой буквы: " + name);
        }
        this.name = name;
    }

    public void setGrades(int[] grades) {
        if (grades == null || grades.length == 0) {
            throw new DataValidationException("Список оценок не может быть пустым.");
        }
        for (int grade : grades) {
            if (grade < 2 || grade > 5) {
                throw new DataValidationException("Оценка должна быть в диапазоне от 2 до 5. Передано: " + grade);
            }
        }
        this.grades = grades;
    }

    public double getAverageRating() {
        if (grades.length == 0) return 0.0;
        double sum = 0;
        for (int grade : grades) {
            sum += grade;
        }
        return sum / grades.length;
    }

    public abstract boolean isEligibleForSpecialScholarship();

    public int compareTo(Learner other) {
        int surnameComparison = this.surname.compareTo(other.surname);
        if (surnameComparison != 0) {
            return surnameComparison;
        }
        return this.name.compareTo(other.name);
    }
}

class Schoolchild extends Learner {
    int schoolNumber;

    public Schoolchild(String surname, String name, int[] grades, int schoolNumber) {
        super(surname, name, grades);
        setSchoolNumber(schoolNumber);
    }

    public void setSchoolNumber(int schoolNumber) {
        if (schoolNumber <= 0) {
            throw new DataValidationException("Номер школы должен быть положительным.");
        }
        this.schoolNumber = schoolNumber;
    }

    public boolean isEligibleForSpecialScholarship() {
        return getAverageRating() >= 4.8;
    }

    public String toString() {
        return surname + " " + name + " | Школа №" + schoolNumber + " | Средний балл: " + getAverageRating();
    }
}

class Student extends Learner {
    int course;

    public Student(String surname, String name, int[] grades, int course) {
        super(surname, name, grades);
        setCourse(course);
    }

    public void setCourse(int course) {
        if (course < 1 || course > 6) {
            throw new DataValidationException("Курс должен быть в диапазоне от 1 до 6.");
        }
        this.course = course;
    }

    public boolean isEligibleForSpecialScholarship() {
        return getAverageRating() >= 4.7;
    }

    public String toString() {
        return surname + " " + name + " | Средний балл: " + getAverageRating();
    }
}

public class Main {
    public static void main(String[] args) {
        Schoolchild[] schoolchildren = {
            new Schoolchild("Смирнов", "Алексей", new int[]{5, 5, 5, 4}, 10),
            new Schoolchild("Артемьева", "Мария", new int[]{5, 5, 5, 5}, 5),
            new Schoolchild("Васильев", "Дмитрий", new int[]{4, 3, 4, 4}, 10),
            new Schoolchild("Иванова", "Елена", new int[]{5, 5, 4, 5}, 3)
        };

        Student[] students = {
            new Student("Николаев", "Артем", new int[]{5, 4, 5, 5}, 2),
            new Student("Алексеева", "Ольга", new int[]{5, 5, 5, 5}, 3),
            new Student("Борисов", "Илья", new int[]{3, 4, 3, 4}, 1),
            new Student("Григорьева", "София", new int[]{5, 4, 5, 4}, 4)
        };

        System.out.println("=== 1. Специальная стипендия ===");
        Learner[] special = new Learner[schoolchildren.length + students.length];
        int count = 0;
        for (Schoolchild s : schoolchildren) {
            if (s.isEligibleForSpecialScholarship()) special[count++] = s;
        }
        for (Student st : students) {
            if (st.isEligibleForSpecialScholarship()) special[count++] = st;
        }

        for (int i = 0; i < count - 1; i++) {
            for (int j = 0; j < count - 1 - i; j++) {
                if (special[j].compareTo(special[j + 1]) > 0) {
                    Learner temp = special[j];
                    special[j] = special[j + 1];
                    special[j + 1] = temp;
                }
            }
        }
        for (int i = 0; i < count; i++) {
            System.out.println(special[i]);
        }
        System.out.println();

        System.out.println("=== 2. Лучшие по успеваемости ===");
        Schoolchild bestSchoolchild = schoolchildren[0];
        for (int i = 1; i < schoolchildren.length; i++) {
            if (schoolchildren[i].getAverageRating() > bestSchoolchild.getAverageRating()) {
                bestSchoolchild = schoolchildren[i];
            }
        }

        Student bestStudent = students[0];
        for (int i = 1; i < students.length; i++) {
            if (students[i].getAverageRating() > bestStudent.getAverageRating()) {
                bestStudent = students[i];
            }
        }
        System.out.println(bestSchoolchild);
        System.out.println(bestStudent);
        System.out.println();

        System.out.println("=== 3. Школьники по успеваемости и номеру школы ===");
        for (int i = 0; i < schoolchildren.length - 1; i++) {
            for (int j = 0; j < schoolchildren.length - 1 - i; j++) {
                boolean swap = false;
                double r1 = schoolchildren[j].getAverageRating();
                double r2 = schoolchildren[j + 1].getAverageRating();
                if (r1 < r2) {
                    swap = true;
                } else if (r1 == r2) {
                    if (schoolchildren[j].schoolNumber > schoolchildren[j + 1].schoolNumber) {
                        swap = true;
                    }
                }
                if (swap) {
                    Schoolchild t = schoolchildren[j];
                    schoolchildren[j] = schoolchildren[j + 1];
                    schoolchildren[j + 1] = t;
                }
            }
        }
        for (Schoolchild s : schoolchildren) {
            System.out.println(s);
        }
        System.out.println();

        System.out.println("=== 4. Студенты по успеваемости ===");
        for (int i = 0; i < students.length - 1; i++) {
            for (int j = 0; j < students.length - 1 - i; j++) {
                if (students[j].getAverageRating() < students[j + 1].getAverageRating()) {
                    Student t = students[j];
                    students[j] = students[j + 1];
                    students[j + 1] = t;
                }
            }
        }
        for (Student st : students) {
            System.out.println(st);
        }
    }
}