From 5bc93d7582b0cfcf72b4c096503c4a0c0748b4db Mon Sep 17 00:00:00 2001 From: Andrea Brighi Date: Tue, 16 May 2023 19:32:27 +0200 Subject: [PATCH] refactor(desktop): in Desktop add field backpack implemented in DesktopImpl --- .../desktopdomain/entities/Desktop.kt | 34 ++++++++++----- .../entities/implementations/DesktopImpl.kt | 43 ++++++++++++------- .../BackpackNotAssociatedException.kt | 6 +++ 3 files changed, 58 insertions(+), 25 deletions(-) create mode 100644 desktopDomain/src/main/kotlin/com/intelligentbackpack/desktopdomain/exception/BackpackNotAssociatedException.kt diff --git a/desktopDomain/src/main/kotlin/com/intelligentbackpack/desktopdomain/entities/Desktop.kt b/desktopDomain/src/main/kotlin/com/intelligentbackpack/desktopdomain/entities/Desktop.kt index cb6af89e..d443c217 100644 --- a/desktopDomain/src/main/kotlin/com/intelligentbackpack/desktopdomain/entities/Desktop.kt +++ b/desktopDomain/src/main/kotlin/com/intelligentbackpack/desktopdomain/entities/Desktop.kt @@ -3,8 +3,14 @@ package com.intelligentbackpack.desktopdomain.entities import com.intelligentbackpack.desktopdomain.entities.implementations.DesktopImpl import com.intelligentbackpack.desktopdomain.exception.BackpackAlreadyAssociatedException import com.intelligentbackpack.desktopdomain.exception.DuplicateRFIDException +import com.intelligentbackpack.desktopdomain.exception.BackpackNotAssociatedException import com.intelligentbackpack.desktopdomain.exception.TypeException +/** + * Type alias for backpack. + */ +typealias Backpack = String + /** * Interface for the desktop of the user. */ @@ -25,10 +31,16 @@ interface Desktop { */ val schoolSuppliesInBackpack: Set + /** + * The backpack of the user. + */ + val backpack: Backpack? + /** * True if the backpack is connected for the user, false otherwise. */ - val backpackAssociated: Boolean + val isBackpackAssociated: Boolean + get() = backpack != null /** * Adds a school supply to the desktop. @@ -48,7 +60,7 @@ interface Desktop { * @throws BackpackAlreadyAssociatedException If a backpack is already connected. */ @Throws(BackpackAlreadyAssociatedException::class) - fun associateBackpack() + fun associateBackpack(backpack: Backpack) /** * Adds a school supply in the backpack. @@ -90,19 +102,21 @@ interface Desktop { * * @param schoolSupplies The school supplies of the desktop. * @param schoolSuppliesInBackpack The school supplies in the backpack. - * @param backpackAssociated true if the backpack is associated for the user, false otherwise. * @return The desktop built. */ fun create( schoolSupplies: Set = emptySet(), schoolSuppliesInBackpack: Set = emptySet(), - backpackAssociated: Boolean = false + backpack: Backpack? = null ): Desktop = - DesktopImpl( - schoolSupplies, - setOf(SchoolSupplyTypes.BOOK), - schoolSuppliesInBackpack, - backpackAssociated - ) + if (backpack == null && schoolSuppliesInBackpack.isNotEmpty()) + throw BackpackNotAssociatedException() + else + DesktopImpl( + schoolSupplies, + setOf(SchoolSupplyTypes.BOOK), + schoolSuppliesInBackpack, + backpack + ) } } diff --git a/desktopDomain/src/main/kotlin/com/intelligentbackpack/desktopdomain/entities/implementations/DesktopImpl.kt b/desktopDomain/src/main/kotlin/com/intelligentbackpack/desktopdomain/entities/implementations/DesktopImpl.kt index dd44ab65..26cd9c24 100644 --- a/desktopDomain/src/main/kotlin/com/intelligentbackpack/desktopdomain/entities/implementations/DesktopImpl.kt +++ b/desktopDomain/src/main/kotlin/com/intelligentbackpack/desktopdomain/entities/implementations/DesktopImpl.kt @@ -1,10 +1,12 @@ package com.intelligentbackpack.desktopdomain.entities.implementations +import com.intelligentbackpack.desktopdomain.entities.Backpack import com.intelligentbackpack.desktopdomain.entities.Desktop import com.intelligentbackpack.desktopdomain.entities.SchoolSupply import com.intelligentbackpack.desktopdomain.entities.SchoolSupplyType import com.intelligentbackpack.desktopdomain.exception.AlreadyInBackpackException import com.intelligentbackpack.desktopdomain.exception.BackpackAlreadyAssociatedException +import com.intelligentbackpack.desktopdomain.exception.BackpackNotAssociatedException import com.intelligentbackpack.desktopdomain.exception.DuplicateRFIDException import com.intelligentbackpack.desktopdomain.exception.SchoolSupplyNotFoundException import com.intelligentbackpack.desktopdomain.exception.TypeException @@ -15,12 +17,14 @@ import kotlin.jvm.Throws * * @property schoolSupplies The school supplies of the desktop. * @property schoolSupplyTypes The types of the school supplies of the desktop. + * @property schoolSuppliesInBackpack The school supplies in the backpack. + * @property backpack The backpack of the user. */ internal class DesktopImpl( schoolSupplies: Set, schoolSupplyTypes: Set, schoolSuppliesInBackpack: Set = emptySet(), - backpackAssociated: Boolean + backpack: Backpack? = null ) : Desktop { override var schoolSupplies: Set = schoolSupplies @@ -29,7 +33,8 @@ internal class DesktopImpl( private set override var schoolSuppliesInBackpack: Set = schoolSuppliesInBackpack private set - override var backpackAssociated: Boolean = backpackAssociated + + override var backpack: Backpack? = backpack private set /** @@ -58,13 +63,17 @@ internal class DesktopImpl( */ @Throws(SchoolSupplyNotFoundException::class) override fun putSchoolSupplyInBackpack(schoolSupply: SchoolSupply) { - if (!schoolSupplies.contains(schoolSupply)) - throw SchoolSupplyNotFoundException(schoolSupply.rfidCode) + if (!isBackpackAssociated) + throw BackpackNotAssociatedException() else { - if (schoolSuppliesInBackpack.contains(schoolSupply)) - throw AlreadyInBackpackException(schoolSupply) - else - schoolSuppliesInBackpack = schoolSuppliesInBackpack + schoolSupply + if (!schoolSupplies.contains(schoolSupply)) + throw SchoolSupplyNotFoundException(schoolSupply.rfidCode) + else { + if (schoolSuppliesInBackpack.contains(schoolSupply)) + throw AlreadyInBackpackException(schoolSupply) + else + schoolSuppliesInBackpack = schoolSuppliesInBackpack + schoolSupply + } } } @@ -73,11 +82,11 @@ internal class DesktopImpl( * * @throws BackpackAlreadyAssociatedException If a backpack is already connected. */ - override fun associateBackpack() { - if (backpackAssociated) + override fun associateBackpack(backpack: Backpack) { + if (isBackpackAssociated) throw BackpackAlreadyAssociatedException() else - backpackAssociated = true + this.backpack = backpack } /** @@ -88,9 +97,13 @@ internal class DesktopImpl( */ @Throws(SchoolSupplyNotFoundException::class) override fun takeSchoolSupplyFromBackpack(schoolSupply: SchoolSupply) { - if (!schoolSuppliesInBackpack.contains(schoolSupply)) - throw SchoolSupplyNotFoundException(schoolSupply.rfidCode) - else - schoolSuppliesInBackpack = schoolSuppliesInBackpack - schoolSupply + if (!isBackpackAssociated) + throw BackpackNotAssociatedException() + else { + if (!schoolSuppliesInBackpack.contains(schoolSupply)) + throw SchoolSupplyNotFoundException(schoolSupply.rfidCode) + else + schoolSuppliesInBackpack = schoolSuppliesInBackpack - schoolSupply + } } } diff --git a/desktopDomain/src/main/kotlin/com/intelligentbackpack/desktopdomain/exception/BackpackNotAssociatedException.kt b/desktopDomain/src/main/kotlin/com/intelligentbackpack/desktopdomain/exception/BackpackNotAssociatedException.kt new file mode 100644 index 00000000..442ced79 --- /dev/null +++ b/desktopDomain/src/main/kotlin/com/intelligentbackpack/desktopdomain/exception/BackpackNotAssociatedException.kt @@ -0,0 +1,6 @@ +package com.intelligentbackpack.desktopdomain.exception + +/** + * Exception thrown when a backpack is not associated, but is used. + */ +class BackpackNotAssociatedException : IllegalStateException("The backpack is not associated.")