Skip to content
This repository has been archived by the owner on Oct 23, 2023. It is now read-only.

Commit

Permalink
refactor(desktop): Add interface Book and its implementation BookImpl
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreaBrighi committed May 11, 2023
1 parent c26e9b0 commit ff38d6c
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 70 deletions.
Original file line number Diff line number Diff line change
@@ -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<String>

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<String>

/**
* 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")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>
val book: Book

companion object {

Expand All @@ -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.
*/
Expand All @@ -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 {

Expand All @@ -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<String>
lateinit var book: Book

/**
* The type of the school supply.
Expand All @@ -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")
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<String>
override lateinit var book: Book
private set

constructor(
rfidCode: String,
isbn: String,
title: String,
authors: List<String>,
book: Book
) : this(rfidCode) {
this.isbn = isbn
this.title = title
this.authors = authors
this.book = book
}

override val type: SchoolSupplyType = SchoolSupplyTypes.BOOK
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String>
private set

constructor(isbn: String, title: String, authors: Set<String>) : this(isbn) {
this.title = title
this.authors = authors
}
}

0 comments on commit ff38d6c

Please sign in to comment.