Created
May 22, 2025 06:58
-
-
Save untainsYD/08f3f846fd32dd987507be4635370734 to your computer and use it in GitHub Desktop.
Laboratory 5, Task 3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package lab5.archive; | |
import java.io.Serializable; | |
import java.util.Arrays; | |
import java.util.Objects; | |
/** | |
* Клас для представлення академічної групи студентів. | |
* Реалізує Serializable для можливості збереження в архіві. | |
*/ | |
public class AcademicGroup implements Serializable { | |
private static final long serialVersionUID = 1L; | |
private String groupName; | |
private String specialty; | |
private int year; | |
private Student[] students; | |
private int studentCount; | |
/** | |
* Конструктор за замовчуванням | |
*/ | |
public AcademicGroup() { | |
this.students = new Student[0]; | |
this.studentCount = 0; | |
} | |
/** | |
* Конструктор з параметрами | |
* @param groupName назва групи | |
* @param specialty спеціальність | |
* @param year рік навчання | |
* @param maxStudents максимальна кількість студентів | |
*/ | |
public AcademicGroup(String groupName, String specialty, int year, int maxStudents) { | |
this.groupName = groupName; | |
this.specialty = specialty; | |
this.year = year; | |
this.students = new Student[maxStudents]; | |
this.studentCount = 0; | |
} | |
/** | |
* Конструктор з масивом студентів | |
* @param groupName назва групи | |
* @param specialty спеціальність | |
* @param year рік навчання | |
* @param students масив студентів | |
*/ | |
public AcademicGroup(String groupName, String specialty, int year, Student[] students) { | |
this.groupName = groupName; | |
this.specialty = specialty; | |
this.year = year; | |
this.students = students.clone(); | |
this.studentCount = students.length; | |
} | |
/** | |
* Додає студента до групи | |
* @param student студент для додавання | |
* @return true, якщо студента додано успішно | |
*/ | |
public boolean addStudent(Student student) { | |
if (studentCount < students.length) { | |
students[studentCount] = student; | |
studentCount++; | |
return true; | |
} | |
return false; // Група повна | |
} | |
/** | |
* Видаляє студента за ідентифікатором | |
* @param studentId ідентифікатор студента | |
* @return true, якщо студента видалено успішно | |
*/ | |
public boolean removeStudent(int studentId) { | |
for (int i = 0; i < studentCount; i++) { | |
if (students[i].getStudentId() == studentId) { | |
// Зсуваємо елементи | |
for (int j = i; j < studentCount - 1; j++) { | |
students[j] = students[j + 1]; | |
} | |
students[studentCount - 1] = null; | |
studentCount--; | |
return true; | |
} | |
} | |
return false; // Студента не знайдено | |
} | |
/** | |
* Знаходить студента за ідентифікатором | |
* @param studentId ідентифікатор студента | |
* @return студент або null, якщо не знайдено | |
*/ | |
public Student findStudent(int studentId) { | |
for (int i = 0; i < studentCount; i++) { | |
if (students[i].getStudentId() == studentId) { | |
return students[i]; | |
} | |
} | |
return null; | |
} | |
/** | |
* Обчислює середній бал групи | |
* @return середній бал групи | |
*/ | |
public double getGroupAverageGrade() { | |
if (studentCount == 0) return 0.0; | |
double sum = 0.0; | |
for (int i = 0; i < studentCount; i++) { | |
sum += students[i].getAverageGrade(); | |
} | |
return sum / studentCount; | |
} | |
/** | |
* Повертає масив активних студентів (без null елементів) | |
* @return масив студентів | |
*/ | |
public Student[] getActiveStudents() { | |
return Arrays.copyOf(students, studentCount); | |
} | |
// Геттери та сеттери | |
public String getGroupName() { | |
return groupName; | |
} | |
public void setGroupName(String groupName) { | |
this.groupName = groupName; | |
} | |
public String getSpecialty() { | |
return specialty; | |
} | |
public void setSpecialty(String specialty) { | |
this.specialty = specialty; | |
} | |
public int getYear() { | |
return year; | |
} | |
public void setYear(int year) { | |
this.year = year; | |
} | |
public Student[] getStudents() { | |
return students.clone(); | |
} | |
public void setStudents(Student[] students) { | |
this.students = students.clone(); | |
this.studentCount = students.length; | |
} | |
public int getStudentCount() { | |
return studentCount; | |
} | |
@Override | |
public String toString() { | |
return String.format("AcademicGroup{name='%s', specialty='%s', year=%d, students=%d, avgGrade=%.2f}", | |
groupName, specialty, year, studentCount, getGroupAverageGrade()); | |
} | |
@Override | |
public boolean equals(Object o) { | |
if (this == o) return true; | |
if (o == null || getClass() != o.getClass()) return false; | |
AcademicGroup that = (AcademicGroup) o; | |
return year == that.year && | |
studentCount == that.studentCount && | |
Objects.equals(groupName, that.groupName) && | |
Objects.equals(specialty, that.specialty) && | |
Arrays.equals(getActiveStudents(), that.getActiveStudents()); | |
} | |
@Override | |
public int hashCode() { | |
int result = Objects.hash(groupName, specialty, year, studentCount); | |
result = 31 * result + Arrays.hashCode(getActiveStudents()); | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment