This repository has been archived by the owner on Oct 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(school): create Interface Person, Student and Professor
- Loading branch information
1 parent
c506b24
commit f1af80e
Showing
3 changed files
with
162 additions
and
0 deletions.
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
schoolDomain/src/main/kotlin/com/intelligentbackpack/schooldomain/entities/person/Person.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
97 changes: 97 additions & 0 deletions
97
...lDomain/src/main/kotlin/com/intelligentbackpack/schooldomain/entities/person/Professor.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Subject> | ||
val professorSubjectsInClasses: Map<Class, Set<Subject>> | ||
val professorClasses: Set<Class> | ||
get() = professorSubjectsInClasses.keys | ||
|
||
val professorSubjects: Set<Subject> | ||
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<Subject>) | ||
|
||
companion object { | ||
|
||
private data class ProfessorImpl( | ||
override val email: String, | ||
override val name: String, | ||
override val surname: String, | ||
) : Professor { | ||
override val subjects: Set<Subject> | ||
get() = professorSubjectsInClasses.values.flatten().toSet() | ||
override var professorSubjectsInClasses: Map<Class, Set<Subject>> = mapOf() | ||
private set | ||
|
||
override fun addProfessorToClass(professorClass: Class, subjects: Set<Subject>) { | ||
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<Class, Set<Subject>>, | ||
): 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) } | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
schoolDomain/src/main/kotlin/com/intelligentbackpack/schooldomain/entities/person/Student.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
} | ||
} | ||
} |