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

Commit

Permalink
refactor(desktop): in Desktop add field backpack implemented in Deskt…
Browse files Browse the repository at this point in the history
…opImpl
  • Loading branch information
AndreaBrighi committed May 16, 2023
1 parent f6474e8 commit 5bc93d7
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -25,10 +31,16 @@ interface Desktop {
*/
val schoolSuppliesInBackpack: Set<SchoolSupply>

/**
* 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.
Expand All @@ -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.
Expand Down Expand Up @@ -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<SchoolSupply> = emptySet(),
schoolSuppliesInBackpack: Set<SchoolSupply> = 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
)
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<SchoolSupply>,
schoolSupplyTypes: Set<SchoolSupplyType>,
schoolSuppliesInBackpack: Set<SchoolSupply> = emptySet(),
backpackAssociated: Boolean
backpack: Backpack? = null
) : Desktop {

override var schoolSupplies: Set<SchoolSupply> = schoolSupplies
Expand All @@ -29,7 +33,8 @@ internal class DesktopImpl(
private set
override var schoolSuppliesInBackpack: Set<SchoolSupply> = schoolSuppliesInBackpack
private set
override var backpackAssociated: Boolean = backpackAssociated

override var backpack: Backpack? = backpack
private set

/**
Expand Down Expand Up @@ -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
}
}
}

Expand All @@ -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
}

/**
Expand All @@ -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
}
}
}
Original file line number Diff line number Diff line change
@@ -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.")

0 comments on commit 5bc93d7

Please sign in to comment.