import java.util.Arrays; import java.util.Objects; abstract class Person { static final String NONE = "<Κενό>"; protected String firstName = NONE; protected String lastName = NONE; Person(String firstName, String lastName) { if (Validation.isNameValid(firstName)) { this.firstName = firstName.trim(); } if (Validation.isNameValid(lastName)) { this.lastName = lastName.trim(); } } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = Validation.isNameValid(firstName) ? firstName.trim() : NONE; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = Validation.isNameValid(lastName) ? lastName.trim() : NONE; } @Override public String toString() { return ", firstName=" + firstName + ", lastName=" + lastName; } } class Student extends Person { private static int amCounter = 0; private final int am; private int age = -1; // 15-18 private ClassRoom classRoom; public Student(String firstName, String lastName, int age) { super(firstName, lastName); am = ++amCounter; if (Validation.isAgeValid(age)) { this.age = age; } } public int getAm() { return am; } public int getAge() { return age; } public void setAge(int age) { this.age = Validation.isAgeValid(age) ? age : -1; } public ClassRoom getClassRoom() { return classRoom; } void setClassRoom(ClassRoom classRoom) { this.classRoom = classRoom; } @Override public int hashCode() { int hash = 7; hash = 41 * hash + this.am; return hash; } @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } final Student other = (Student) obj; return this.am == other.am; } @Override public String toString() { return "Student{" + "am=" + am + super.toString() + ", age=" + age + ", classRoom=" + classRoom + '}'; } } class Teacher extends Person { private static final int MAX_LESSONS = 3; private String id = NONE; // ΑΔΤ private String[] lessons; private int index = 0; public Teacher(String id, String firstName, String lastName, String... lessons) { super(firstName, lastName); if (Validation.isIdValid(id)) { this.id = id; } if (lessons != null && lessons.length > 0) { // αντιγράφουμε μόνο τα 3 πρώτα μαθήματα int length = lessons.length > MAX_LESSONS ? MAX_LESSONS : lessons.length; this.lessons = Arrays.copyOfRange(lessons, 0, length); } else { this.lessons = new String[MAX_LESSONS]; } } public String getId() { return id; } public void setId(String id) { this.id = Validation.isIdValid(id) ? id : NONE; } public void addLesson(String lesson) { if (lesson != null && index < MAX_LESSONS) { lessons[index++] = lesson; } } public void removeLesson(String lesson) { if (lesson != null) { remove(contains(lessons, lesson)); } } @Override public int hashCode() { int hash = 3; hash = 59 * hash + Objects.hashCode(this.id); return hash; } @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } final Teacher other = (Teacher) obj; return Objects.equals(this.id, other.id); } @Override public String toString() { return "Teacher{" + "id=" + id + super.toString() + ", lessons=" + Arrays.toString(lessons) + '}'; } private int contains(String[] lessons, String les) { for (int i = 0; i < lessons.length; i++) { String lesson = lessons[i]; if (lesson.equalsIgnoreCase(les)) { return i; } } return -1; } private boolean remove(int indx) { if (indx < 0 || indx >= this.lessons.length) { return false; } String[] newLessons = new String[this.lessons.length - 1]; // αντιγραφή από το 0 μέχρι το students[index-1] System.arraycopy(this.lessons, 0, newLessons, 0, indx); // αντιγραφή από students[index+1] μέχρι students[students.length-1] System.arraycopy(this.lessons, indx + 1, newLessons, indx, this.lessons.length - indx - 1); this.lessons = newLessons; index--; return true; } } final class Validation { private static final int MAX_SIZE = 20; private static final int CLASSROOM_NAME_LENGTH = 2; private static final int ID_LENGTH = 8; public static final int DEFAULT_SIZE = 30; private Validation() { } public static boolean isNameValid(String name) { return name != null && !name.isBlank() && name.length() <= MAX_SIZE; } public static boolean isAgeValid(int inAge) { return inAge >= 15 && inAge <= 18; } public static boolean isSizeValid(int inSize) { return inSize >= 10 && inSize <= DEFAULT_SIZE; } public static boolean isClassRoomNameValid(String classRoom) { return classRoom != null && !classRoom.isBlank() && classRoom.length() == CLASSROOM_NAME_LENGTH && (classRoom.startsWith("Α") || classRoom.startsWith("Β") || classRoom.startsWith("Γ")) && classRoom.charAt(1) >= '1' && classRoom.charAt(1) <= '9'; } // ΑΔΤ: ΓΓ123456 public static boolean isIdValid(String id) { return id != null && !id.isBlank() && id.length() == ID_LENGTH && isCapitalLetter(id.charAt(0)) && isCapitalLetter(id.charAt(1)) && isNumber(id.charAt(2)) && isNumber(id.charAt(3)) && isNumber(id.charAt(4)) && isNumber(id.charAt(5)) && isNumber(id.charAt(6)) && isNumber(id.charAt(7)); } public static boolean isCapitalLetter(char c) { return c >= 'A' && c <= 'Ω'; } public static boolean isNumber(char c) { return c >= '0' && c <= '9'; } } class ClassRoom { private static final String NO_CLASSROOM = "--"; private final String name; private final int size; private Student[] students; private int index = 0; public ClassRoom(String name, int size) { this.name = Validation.isClassRoomNameValid(name) ? name : NO_CLASSROOM; this.size = Validation.isSizeValid(size) ? size : Validation.DEFAULT_SIZE; this.students = new Student[size]; } public ClassRoom(String name) { this(name, Validation.DEFAULT_SIZE); } public String getName() { return name; } public int getSize() { return size; } public void addStudent(Student student) { if (student != null && index < size) { students[index++] = student; student.setClassRoom(this); } } public void removeStudent(int am) { int indx = contains(students, am); if (indx != -1) { Student student = students[indx]; if (remove(indx)) { student.setClassRoom(null); } } } public void removeStudent(Student student) { if (student != null) { removeStudent(student.getAm()); student.setClassRoom(null); } } public void removeAllStudents() { for (Student student : students) { student.setClassRoom(null); } Arrays.fill(students, null); index = 0; } private int contains(Student[] students, int am) { for (int i = 0; i < students.length; i++) { Student student = students[i]; if (student.getAm() == am) { return i; } } return -1; } private boolean remove(int indx) { if (indx < 0 || indx >= this.students.length) return false; Student[] newStudents = new Student[this.students.length - 1]; // αντιγραφή από το 0 μέχρι το students[index-1] System.arraycopy(this.students, 0, newStudents, 0, indx); // αντιγραφή από students[index+1] μέχρι students[students.length-1] System.arraycopy(this.students, indx + 1, newStudents, indx, this.students.length - indx - 1); this.students = newStudents; index--; return true; } @Override public int hashCode() { int hash = 7; hash = 29 * hash + Objects.hashCode(this.name); return hash; } @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } final ClassRoom other = (ClassRoom) obj; return Objects.equals(this.name, other.name); } @Override public String toString() { return "ClassRoom{" + "name=" + name + ", size=" + size + ", numOfStudents=" + index + '}'; } } public class School { private static final ClassRoom[] classRooms = { new ClassRoom("Α1"), new ClassRoom("Α2", 28), new ClassRoom("Α3", 25), new ClassRoom("Β1"), new ClassRoom("Β2", 28), new ClassRoom("Β3", 25), new ClassRoom("Γ1"), new ClassRoom("Γ2", 26), new ClassRoom("Γ3", 22), }; public static void main(String[] args) { System.out.println("=== Create student ioannis ===="); Student ioannis = new Student("Γιάννης", "Αντεκοτούμπο", 16); System.out.println("AM: " + ioannis.getAm()); System.out.println("ClassRoom: " + ioannis.getClassRoom()); System.out.println("\n=== Add student ioannis to classroom 'B1' ===="); classRooms[3].addStudent(ioannis); System.out.println(ioannis.getClassRoom()); System.out.println(ioannis); System.out.println("\n=== Create student aliki and add to classroom 'B1' ===="); Student aliki = new Student("Αλίκη", "Βουγιουκλάκη", 17); classRooms[3].addStudent(aliki); System.out.println(aliki); System.out.println(classRooms[3]); System.out.println("\n=== Remove student ioannis from classroom 'B1' ===="); classRooms[3].removeStudent(ioannis.getAm()); System.out.println(ioannis); System.out.println(classRooms[3]); System.out.println("\n=== Create a teacher ===="); Teacher socrates = new Teacher("AB123456", "Σωκράτης", "Σωκράτης"); socrates.addLesson("Φιλοσοφία"); socrates.addLesson("Αρχαία Ελληνικά"); System.out.println(socrates); } }