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

Commit

Permalink
feat(desktop): add method to take and put supply from/into backpack
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreaBrighi committed May 12, 2023
1 parent 0baa811 commit fef820d
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ interface Desktop {
*/
val schoolSupplyTypes: Set<SchoolSupplyType>

/**
* The school supplies in the backpack.
*/
val schoolSuppliesInBackpack: Set<SchoolSupply>

/**
* Adds a school supply to the desktop.
*
Expand All @@ -31,19 +36,38 @@ interface Desktop {
@Throws(TypeException::class, DuplicateRFIDException::class)
fun addSchoolSupply(schoolSupply: SchoolSupply): Desktop

/**
* Adds a school supply in the backpack.
*
* @param schoolSupply The school supply to add.
* @return The desktop with the school supply added.
*/
fun putSchoolSupplyInBackpack(schoolSupply: SchoolSupply): Desktop

/**
* Extract a school supply from the backpack.
*
* @param schoolSupply The school supply to extract.
* @return The desktop with the school supply removed.
*/
fun takeSchoolSupplyFromBackpack(schoolSupply: SchoolSupply): Desktop

companion object {
/**
* Builds a desktop.
*
* @param schoolSupplies The school supplies of the desktop.
* @param schoolSuppliesInBackpack The school supplies in the backpack.
* @return The desktop built.
*/
fun create(
schoolSupplies: Set<SchoolSupply> = emptySet()
schoolSupplies: Set<SchoolSupply> = emptySet(),
schoolSuppliesInBackpack: Set<SchoolSupply> = emptySet()
): Desktop =
DesktopImpl(
schoolSupplies,
setOf(SchoolSupplyTypes.BOOK),
schoolSuppliesInBackpack
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ package com.intelligentbackpack.desktopdomain.entities.implementations
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.DuplicateRFIDException
import com.intelligentbackpack.desktopdomain.exception.SchoolSupplyNotFoundException
import com.intelligentbackpack.desktopdomain.exception.TypeException
import kotlin.jvm.Throws

/**
* Implementation of a desktop.
Expand All @@ -15,6 +18,7 @@ import com.intelligentbackpack.desktopdomain.exception.TypeException
internal data class DesktopImpl(
override val schoolSupplies: Set<SchoolSupply>,
override val schoolSupplyTypes: Set<SchoolSupplyType>,
override val schoolSuppliesInBackpack: Set<SchoolSupply> = emptySet()
) : Desktop {

/**
Expand All @@ -37,4 +41,47 @@ internal data class DesktopImpl(
schoolSupplyTypes = schoolSupplyTypes,
)
}

/**
* Put a school supply in the backpack.
*
* @param schoolSupply The school supply to put in the backpack.
* @return A new desktop with the school supply in the backpack.
* @throws SchoolSupplyNotFoundException If the school supply is not in the desktop.
* @throws AlreadyInBackpackException If the school supply is already in the backpack.
*/
@Throws(SchoolSupplyNotFoundException::class)
override fun putSchoolSupplyInBackpack(schoolSupply: SchoolSupply): Desktop {
if (!schoolSupplies.contains(schoolSupply))
throw SchoolSupplyNotFoundException(schoolSupply.rfidCode)
else {
if (schoolSuppliesInBackpack.contains(schoolSupply))
throw AlreadyInBackpackException(schoolSupply)
else
return DesktopImpl(
schoolSupplies = schoolSupplies,
schoolSupplyTypes = schoolSupplyTypes,
schoolSuppliesInBackpack = schoolSuppliesInBackpack + schoolSupply
)
}
}

/**
* Take a school supply from the backpack.
*
* @param schoolSupply The school supply to take.
* @return A new desktop with the school supply taken from the backpack.
* @throws SchoolSupplyNotFoundException If the school supply is not in the backpack.
*/
@Throws(SchoolSupplyNotFoundException::class)
override fun takeSchoolSupplyFromBackpack(schoolSupply: SchoolSupply): Desktop {
if (!schoolSuppliesInBackpack.contains(schoolSupply))
throw SchoolSupplyNotFoundException(schoolSupply.rfidCode)
else
return DesktopImpl(
schoolSupplies = schoolSupplies,
schoolSupplyTypes = schoolSupplyTypes,
schoolSuppliesInBackpack = schoolSuppliesInBackpack - schoolSupply
)
}
}

0 comments on commit fef820d

Please sign in to comment.