diff --git a/desktopDomain/src/main/kotlin/com/intelligentbackpack/desktopdomain/entities/Book.kt b/desktopDomain/src/main/kotlin/com/intelligentbackpack/desktopdomain/entities/Book.kt new file mode 100644 index 00000000..8c27b74c --- /dev/null +++ b/desktopDomain/src/main/kotlin/com/intelligentbackpack/desktopdomain/entities/Book.kt @@ -0,0 +1,90 @@ +package com.intelligentbackpack.desktopdomain.entities + +import com.intelligentbackpack.desktopdomain.entities.implementations.BookImpl +import com.intelligentbackpack.desktopdomain.exception.ISBNException +import com.intelligentbackpack.desktopdomain.policies.ISBNPolicy + +interface Book { + /** + * The ISBN of the book. + */ + val isbn: String + + /** + * The title of the book. + */ + val title: String + + /** + * The authors of the book. + */ + val authors: Set + + companion object { + + /** + * Builds a book copy. + * + * @param block The builder block. + * @return The book copy built. + * @throws IllegalArgumentException If the book title is black or the authors are blanck. + * @throws ISBNException If the ISBN of the book is not valid. + * @throws IllegalStateException If not all the properties are initialized. + */ + inline fun build( + block: Builder.() -> Unit + ): Book = Builder().apply(block).build() + } + + class Builder { + + /** + * The ISBN of the book. + */ + lateinit var isbn: String + + /** + * The title of the book. + */ + lateinit var title: String + + /** + * The authors of the book. + */ + lateinit var authors: Set + + /** + * Builds the school supply. + * + * @return The school supply built. + * @throws IllegalStateException If not all properties are initialized. + * @throws IllegalArgumentException for any other reason. + * @throws ISBNException If the ISBN of the book is not valid. + */ + @Throws( + IllegalStateException::class, + ISBNException::class, + IllegalArgumentException::class + ) + fun build(): Book = + if (this::isbn.isInitialized && + this::title.isInitialized && + this::authors.isInitialized + ) + if (title.isNotBlank() && + authors.all { it.isNotBlank() } + ) + if (ISBNPolicy.isValid(isbn)) { + BookImpl( + isbn = isbn, + title = title, + authors = authors + ) + } else + throw ISBNException() + else + throw IllegalStateException("Not all properties are initialized") + else + throw IllegalStateException("Not all properties are initialized") + } +} diff --git a/desktopDomain/src/main/kotlin/com/intelligentbackpack/desktopdomain/entities/BookCopy.kt b/desktopDomain/src/main/kotlin/com/intelligentbackpack/desktopdomain/entities/BookCopy.kt index 6f5650cb..a29a0c2a 100644 --- a/desktopDomain/src/main/kotlin/com/intelligentbackpack/desktopdomain/entities/BookCopy.kt +++ b/desktopDomain/src/main/kotlin/com/intelligentbackpack/desktopdomain/entities/BookCopy.kt @@ -4,27 +4,13 @@ import com.intelligentbackpack.desktopdomain.entities.implementations.BookCopyIm import com.intelligentbackpack.desktopdomain.exception.ISBNException import com.intelligentbackpack.desktopdomain.exception.RFIDFormatException import com.intelligentbackpack.desktopdomain.exception.TypeException -import com.intelligentbackpack.desktopdomain.policies.ISBNPolicy import com.intelligentbackpack.desktopdomain.policies.RFIDPolicy /** * Interface for a book copy. */ interface BookCopy : SchoolSupply { - /** - * The ISBN of the book. - */ - val isbn: String - - /** - * The title of the book. - */ - val title: String - - /** - * The authors of the book. - */ - val authors: List + val book: Book companion object { @@ -34,7 +20,6 @@ interface BookCopy : SchoolSupply { * @param block The builder block. * @return The book copy built. * @throws IllegalArgumentException If the book copy is invalid - * ( the [isbn] doesn't match with the [ISBNPolicy], * the [rfidCode] doesn't match with the [RFIDPolicy], * @throws IllegalStateException If not all the properties are initialized. */ @@ -47,8 +32,6 @@ interface BookCopy : SchoolSupply { * Builder for a book copy. * The [type] is automatically set to [SchoolSupplyTypes.BOOK]. * - * @property checkSubjects The subjects that are available. - * */ class Builder { @@ -58,19 +41,9 @@ interface BookCopy : SchoolSupply { lateinit var rfidCode: String /** - * The ISBN of the book. - */ - lateinit var isbn: String - - /** - * The title of the book. + * The book of the copy. */ - lateinit var title: String - - /** - * The authors of the book. - */ - lateinit var authors: List + lateinit var book: Book /** * The type of the school supply. @@ -84,43 +57,27 @@ interface BookCopy : SchoolSupply { * @throws IllegalStateException If not all properties are initialized. * @throws TypeException If the type of the school supply is not valid. * @throws RFIDFormatException If the RFID code of the school supply is not valid. - * @throws IllegalArgumentException for any other reason. * @throws ISBNException If the ISBN of the book is not valid. */ @Throws( IllegalStateException::class, TypeException::class, - RFIDFormatException::class, - IllegalArgumentException::class + RFIDFormatException::class ) fun build(): BookCopy = if (this::rfidCode.isInitialized && - this::isbn.isInitialized && - this::title.isInitialized && - this::authors.isInitialized + this::book.isInitialized ) - if (title.isNotBlank() && - authors.isNotEmpty() && - authors.all { it.isNotBlank() } - ) - if (type == SchoolSupplyTypes.BOOK) - if (RFIDPolicy.isValid(rfidCode)) - - if (ISBNPolicy.isValid(isbn)) { - BookCopyImpl( - rfidCode = rfidCode, - isbn = isbn, - title = title, - authors = authors - ) - } else - throw ISBNException() - else - throw RFIDFormatException() + if (type == SchoolSupplyTypes.BOOK) + if (RFIDPolicy.isValid(rfidCode)) + BookCopyImpl( + rfidCode = rfidCode, + book = book + ) else - throw TypeException(type) + throw RFIDFormatException() else - throw IllegalStateException("Not all properties are initialized") + throw TypeException(type) else throw IllegalStateException("Not all properties are initialized") } 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 index 2a8145ec..5558833c 100644 --- a/desktopDomain/src/main/kotlin/com/intelligentbackpack/desktopdomain/entities/implementations/BookCopyImpl.kt +++ b/desktopDomain/src/main/kotlin/com/intelligentbackpack/desktopdomain/entities/implementations/BookCopyImpl.kt @@ -1,5 +1,6 @@ package com.intelligentbackpack.desktopdomain.entities.implementations +import com.intelligentbackpack.desktopdomain.entities.Book import com.intelligentbackpack.desktopdomain.entities.BookCopy import com.intelligentbackpack.desktopdomain.entities.SchoolSupplyType import com.intelligentbackpack.desktopdomain.entities.SchoolSupplyTypes @@ -8,31 +9,21 @@ import com.intelligentbackpack.desktopdomain.entities.SchoolSupplyTypes * Implementation of a book copy. * * @property rfidCode The RFID code 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 book The book of the copy. * @property type is automatically set to [SchoolSupplyTypes.BOOK]. */ internal data class BookCopyImpl( override val rfidCode: String, ) : BookCopy { - override lateinit var isbn: String - private set - override lateinit var title: String - private set - override lateinit var authors: List + override lateinit var book: Book private set constructor( rfidCode: String, - isbn: String, - title: String, - authors: List, + book: Book ) : this(rfidCode) { - this.isbn = isbn - this.title = title - this.authors = authors + this.book = book } override val type: SchoolSupplyType = SchoolSupplyTypes.BOOK diff --git a/desktopDomain/src/main/kotlin/com/intelligentbackpack/desktopdomain/entities/implementations/BookImpl.kt b/desktopDomain/src/main/kotlin/com/intelligentbackpack/desktopdomain/entities/implementations/BookImpl.kt new file mode 100644 index 00000000..aed62432 --- /dev/null +++ b/desktopDomain/src/main/kotlin/com/intelligentbackpack/desktopdomain/entities/implementations/BookImpl.kt @@ -0,0 +1,18 @@ +package com.intelligentbackpack.desktopdomain.entities.implementations + +import com.intelligentbackpack.desktopdomain.entities.Book + +data class BookImpl( + override val isbn: String +) : Book { + override lateinit var title: String + private set + + override lateinit var authors: Set + private set + + constructor(isbn: String, title: String, authors: Set) : this(isbn) { + this.title = title + this.authors = authors + } +}