diff --git a/desktopDomain/src/main/kotlin/com/intelligentbackpack/desktopdomain/entities/implementations/AbstractSchoolSupply.kt b/desktopDomain/src/main/kotlin/com/intelligentbackpack/desktopdomain/entities/implementations/AbstractSchoolSupply.kt new file mode 100644 index 00000000..12c40c3e --- /dev/null +++ b/desktopDomain/src/main/kotlin/com/intelligentbackpack/desktopdomain/entities/implementations/AbstractSchoolSupply.kt @@ -0,0 +1,69 @@ +package com.intelligentbackpack.desktopdomain.entities.implementations + +import com.intelligentbackpack.desktopdomain.entities.MutableSchoolSupply +import com.intelligentbackpack.desktopdomain.entities.SchoolSupply +import com.intelligentbackpack.desktopdomain.entities.SchoolSupplyType +import com.intelligentbackpack.desktopdomain.entities.Subject +import com.intelligentbackpack.desktopdomain.exception.ReplaceException +import com.intelligentbackpack.desktopdomain.policies.ReplacePolicy +import kotlin.jvm.Throws + +/** + * Abstract implementation of a school supply. + * + * @property type The type of the school supply. + * @property rfidCode The RFID code of the school supply. + * @property subjects The subjects of the school supply. + * @property replacedBy The school supplies that replace this one. + * @property replace The school supplies that this one replaces. + */ +abstract class AbstractSchoolSupply : MutableSchoolSupply { + + /** + * The type of the school supply. + * @param type The type of the school supply. + * @param rfidCode The RFID code of the school supply. + * @param subjects The subjects of the school supply. + * @param replacedBy The school supplies that replace this one. + * @param replace The school supplies that this one replaces. + * @return The school supply with the given type. + */ + protected abstract fun copy( + type: SchoolSupplyType = this.type, + rfidCode: String = this.rfidCode, + subjects: Set = this.subjects, + replacedBy: Set = this.replacedBy, + replace: Set = this.replace, + ): MutableSchoolSupply + + /** + * Add the given school supply to the list of school supplies that replace this one. + * @param schoolSupply The school supply to add. + * @return The school supply with the given school supply added to the list of school supplies that replace this one. + * @throws ReplaceException If the school supply that replace doesn't respect the [ReplacePolicy]. + */ + @Throws(ReplaceException::class) + override fun addReplacedBy(schoolSupply: T): MutableSchoolSupply { + if (ReplacePolicy.isValid(setOf(schoolSupply), setOf(this))) + return this.copy(replacedBy = replacedBy + schoolSupply) + else + throw ReplaceException() + } + + /** + * Add the given school supply to the list of school supplies that this one replaces. + * @param schoolSupply The school supply to add. + * @return The school supply with the given school supply added to the list of school supplies that this one replaces. + * @throws ReplaceException If the school supply to replace doesn't respect the [ReplacePolicy]. + */ + @Throws(ReplaceException::class) + override fun addReplace(schoolSupply: T): MutableSchoolSupply { + if (ReplacePolicy.isValid(setOf(this), setOf(schoolSupply))) + return this.copy(replace = replace + schoolSupply) + else + throw ReplaceException() + } + + override fun addSubjects(subjects: Set) = + this.copy(subjects = this.subjects + subjects) +} diff --git a/desktopDomain/src/main/kotlin/com/intelligentbackpack/desktopdomain/entities/implementations/BookCopyImpl.kt b/desktopDomain/src/main/kotlin/com/intelligentbackpack/desktopdomain/entities/implementations/BookCopyImpl.kt new file mode 100644 index 00000000..c99e88f3 --- /dev/null +++ b/desktopDomain/src/main/kotlin/com/intelligentbackpack/desktopdomain/entities/implementations/BookCopyImpl.kt @@ -0,0 +1,71 @@ +package com.intelligentbackpack.desktopdomain.entities.implementations + +import com.intelligentbackpack.desktopdomain.entities.BookCopy +import com.intelligentbackpack.desktopdomain.entities.SchoolSupplyType +import com.intelligentbackpack.desktopdomain.entities.SchoolSupplyTypes +import com.intelligentbackpack.desktopdomain.entities.Subject + +/** + * Implementation of a book copy. + * + * @property rfidCode The RFID code of the book copy. + * @property subjects The subjects of the book copy. + * @property isbn The ISBN of the book copy. + * @property title The title of the book copy. + * @property authors The authors of the book copy. + * @property replacedBy The book copies that replace this one. + * @property replace The book copies that this one replaces. + * @property type is automatically set to [SchoolSupplyTypes.BOOK]. + */ +internal data class BookCopyImpl( + override val rfidCode: String, +) : AbstractSchoolSupply(), BookCopy { + + override lateinit var subjects: Set + private set + override lateinit var isbn: String + private set + override lateinit var title: String + private set + override lateinit var authors: List + private set + override var replacedBy: Set = setOf() + private set + override var replace: Set = setOf() + private set + + constructor( + rfidCode: String, + subjects: Set, + isbn: String, + title: String, + authors: List, + replacedBy: Set, + replace: Set + ) : this(rfidCode) { + this.subjects = subjects + this.isbn = isbn + this.title = title + this.authors = authors + this.replacedBy = replacedBy + this.replace = replace + } + + override val type: SchoolSupplyType = SchoolSupplyTypes.BOOK + override fun copy( + type: SchoolSupplyType, + rfidCode: String, + subjects: Set, + replacedBy: Set, + replace: Set + ): BookCopy = + BookCopyImpl( + rfidCode = rfidCode, + subjects = subjects, + isbn = isbn, + title = title, + authors = authors, + replacedBy = replacedBy, + replace = replace + ) +} diff --git a/desktopDomain/src/main/kotlin/com/intelligentbackpack/desktopdomain/entities/implementations/GeneralSchoolSupplyImpl.kt b/desktopDomain/src/main/kotlin/com/intelligentbackpack/desktopdomain/entities/implementations/GeneralSchoolSupplyImpl.kt new file mode 100644 index 00000000..7ac6eed3 --- /dev/null +++ b/desktopDomain/src/main/kotlin/com/intelligentbackpack/desktopdomain/entities/implementations/GeneralSchoolSupplyImpl.kt @@ -0,0 +1,61 @@ +package com.intelligentbackpack.desktopdomain.entities.implementations + +import com.intelligentbackpack.desktopdomain.entities.GeneralSchoolSupply +import com.intelligentbackpack.desktopdomain.entities.SchoolSupplyType +import com.intelligentbackpack.desktopdomain.entities.Subject + +/** + * Implementation of a general school supply. + * + * @property type The type of the school supply. + * @property rfidCode The RFID code of the school supply. + * @property description The description of the school supply. + * @property subjects The subjects of the school supply. + * @property replacedBy The school supplies that replace this one. + * @property replace The school supplies that this one replaces. + */ +internal data class GeneralSchoolSupplyImpl( + override val rfidCode: String +) : AbstractSchoolSupply(), GeneralSchoolSupply { + + constructor( + type: SchoolSupplyType, + rfidCode: String, + description: String, + subjects: Set, + replacedBy: Set, + replace: Set + ) : this(rfidCode) { + this.type = type + this.description = description + this.subjects = subjects + this.replacedBy = replacedBy + this.replace = replace + } + + override lateinit var type: SchoolSupplyType + private set + override lateinit var description: String + private set + override lateinit var subjects: Set + private set + override var replacedBy: Set = setOf() + private set + override var replace: Set = setOf() + private set + + override fun copy( + type: SchoolSupplyType, + rfidCode: String, + subjects: Set, + replacedBy: Set, + replace: Set + ): GeneralSchoolSupply = GeneralSchoolSupplyImpl( + type = type, + rfidCode = rfidCode, + description = description, + subjects = subjects, + replacedBy = replacedBy, + replace = replace + ) +}