From f1af80eac357d91515c3abf5d138062c12b7288c Mon Sep 17 00:00:00 2001 From: Andrea Brighi Date: Sat, 27 May 2023 02:18:19 +0200 Subject: [PATCH] feat(school): create Interface Person, Student and Professor --- .../schooldomain/entities/person/Person.kt | 14 +++ .../schooldomain/entities/person/Professor.kt | 97 +++++++++++++++++++ .../schooldomain/entities/person/Student.kt | 51 ++++++++++ 3 files changed, 162 insertions(+) create mode 100644 schoolDomain/src/main/kotlin/com/intelligentbackpack/schooldomain/entities/person/Person.kt create mode 100644 schoolDomain/src/main/kotlin/com/intelligentbackpack/schooldomain/entities/person/Professor.kt create mode 100644 schoolDomain/src/main/kotlin/com/intelligentbackpack/schooldomain/entities/person/Student.kt diff --git a/schoolDomain/src/main/kotlin/com/intelligentbackpack/schooldomain/entities/person/Person.kt b/schoolDomain/src/main/kotlin/com/intelligentbackpack/schooldomain/entities/person/Person.kt new file mode 100644 index 00000000..21d358d3 --- /dev/null +++ b/schoolDomain/src/main/kotlin/com/intelligentbackpack/schooldomain/entities/person/Person.kt @@ -0,0 +1,14 @@ +package com.intelligentbackpack.schooldomain.entities.person + +/** + * A person. + * + * @property name the name of the person + * @property surname the surname of the person + * @property email the email of the person + */ +interface Person { + val name: String + val surname: String + val email: String +} diff --git a/schoolDomain/src/main/kotlin/com/intelligentbackpack/schooldomain/entities/person/Professor.kt b/schoolDomain/src/main/kotlin/com/intelligentbackpack/schooldomain/entities/person/Professor.kt new file mode 100644 index 00000000..3d21008f --- /dev/null +++ b/schoolDomain/src/main/kotlin/com/intelligentbackpack/schooldomain/entities/person/Professor.kt @@ -0,0 +1,97 @@ +package com.intelligentbackpack.schooldomain.entities.person + +import com.intelligentbackpack.schooldomain.entities.Subject +import com.intelligentbackpack.schooldomain.entities.Class + +/** + * A professor. + * + * @property name the name of the professor + * @property surname the surname of the professor + * @property email the email of the professor + * @property professorClasses the classes the professor teaches + * @property professorSubjects the subjects the professor teaches + * @property professorSubjectsInClasses the subjects the professor teaches in each class + */ +interface Professor : Person { + val subjects: Set + val professorSubjectsInClasses: Map> + val professorClasses: Set + get() = professorSubjectsInClasses.keys + + val professorSubjects: Set + get() = professorSubjectsInClasses.values.flatten().toSet() + + /** + * Adds the professor to a class. + * If the professor is already in the class, the subjects are added to the subjects the professor teaches in the class. + * + * @param professorClass the class to add the professor to + * @param subjects the subjects the professor teaches in the class + * @throws IllegalArgumentException if the subjects are empty + */ + fun addProfessorToClass(professorClass: Class, subjects: Set) + + companion object { + + private data class ProfessorImpl( + override val email: String, + override val name: String, + override val surname: String, + ) : Professor { + override val subjects: Set + get() = professorSubjectsInClasses.values.flatten().toSet() + override var professorSubjectsInClasses: Map> = mapOf() + private set + + override fun addProfessorToClass(professorClass: Class, subjects: Set) { + if (subjects.isEmpty()) { + throw IllegalArgumentException("subjects cannot be empty") + } else { + professorSubjectsInClasses = if (professorSubjectsInClasses.containsKey(professorClass)) { + val oldSubjects = professorSubjectsInClasses[professorClass]!! + professorSubjectsInClasses + (professorClass to oldSubjects + subjects) + } else { + professorSubjectsInClasses + (professorClass to subjects) + } + } + } + } + + /** + * Creates a professor. + * + * @param email the email of the professor + * @param name the name of the professor + * @param surname the surname of the professor + * @param professorClasses the classes the professor teaches + * @return the created professor + * @throws IllegalArgumentException if the email is blank + * @throws IllegalArgumentException if the name is blank + * @throws IllegalArgumentException if the surname is blank + * @throws IllegalArgumentException if the professorClasses are empty + */ + fun create( + email: String, + name: String, + surname: String, + professorClasses: Map>, + ): Professor { + if (email.isBlank()) { + throw IllegalArgumentException("email cannot be blank") + } else if (name.isBlank()) { + throw IllegalArgumentException("name cannot be blank") + } else if (surname.isBlank()) { + throw IllegalArgumentException("surname cannot be blank") + } else { + if (professorClasses.any { it.value.isEmpty() }) { + throw IllegalArgumentException("professorClasses cannot be empty") + } else { + return ProfessorImpl(email, name, surname).apply { + professorClasses.forEach { addProfessorToClass(it.key, it.value) } + } + } + } + } + } +} diff --git a/schoolDomain/src/main/kotlin/com/intelligentbackpack/schooldomain/entities/person/Student.kt b/schoolDomain/src/main/kotlin/com/intelligentbackpack/schooldomain/entities/person/Student.kt new file mode 100644 index 00000000..8faf900b --- /dev/null +++ b/schoolDomain/src/main/kotlin/com/intelligentbackpack/schooldomain/entities/person/Student.kt @@ -0,0 +1,51 @@ +package com.intelligentbackpack.schooldomain.entities.person + +import com.intelligentbackpack.schooldomain.entities.Class + +/** + * A student. + * + * @property email the email of the student + * @property name the name of the student + * @property surname the surname of the student + * @property studentClass the class the student attends + */ +interface Student : Person { + val studentClass: Class + + companion object { + private data class StudentImpl( + override val email: String, + override val name: String, + override val surname: String, + override val studentClass: Class, + ) : Student + + /** + * Creates a student. + * + * @param email the email of the student + * @param name the name of the student + * @param surname the surname of the student + * @param studentClass the class the student attends + * @throws IllegalArgumentException if the email or name or surname is blank + * + */ + fun create( + email: String, + name: String, + surname: String, + studentClass: Class, + ): Student { + if (email.isBlank()) { + throw IllegalArgumentException("email cannot be blank") + } else if (name.isBlank()) { + throw IllegalArgumentException("name cannot be blank") + } else if (surname.isBlank()) { + throw IllegalArgumentException("surname cannot be blank") + } else { + return StudentImpl(email, name, surname, studentClass) + } + } + } +}