Class MaterialBundle
java.lang.Object
com.university.bookstore.composite.MaterialBundle
- All Implemented Interfaces:
MaterialComponent
Composite component in the Composite pattern.
Represents a bundle of materials that can contain other components (both leaves and other bundles).
This class implements the MaterialComponent interface and can contain other MaterialComponent objects, enabling recursive composition and unified treatment of individual materials and bundles.
- Since:
- 2024-09-15
- Version:
- 3.0
- Author:
- Navid Mohaghegh
-
Constructor Summary
ConstructorsConstructorDescriptionMaterialBundle(String bundleName, double bundleDiscount) Creates a new material bundle with the specified name and discount. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddComponent(MaterialComponent component) Adds a component to this bundle.booleancontainsMaterial(Material material) Checks if this bundle contains the specified material.booleanChecks if this bundle contains any materials of the specified type.booleandoubleGets the bundle discount rate.Gets the bundle name.intGets the number of components in this bundle.Gets all components in this bundle.Gets a description of this component.doubleGets the discounted price of this component.doubleGets the discount rate applied to this component.intGets the total number of items in this component.Gets all materials contained in this component.Gets all materials of the specified type in this bundle.doublegetPrice()Gets the total price of this component.getTitle()Gets the title of this component.doubleCalculates the total savings from the bundle discount.inthashCode()booleanisLeaf()Checks if this component is a leaf (individual material).booleanremoveComponent(MaterialComponent component) Removes a component from this bundle.toString()Methods inherited from interface com.university.bookstore.composite.MaterialComponent
isComposite
-
Constructor Details
-
MaterialBundle
Creates a new material bundle with the specified name and discount.- Parameters:
bundleName- the name of the bundlebundleDiscount- the discount rate (0.0 to 1.0)- Throws:
IllegalArgumentException- if bundleName is null or empty, or discount is invalid
-
-
Method Details
-
addComponent
Adds a component to this bundle.- Parameters:
component- the component to add- Throws:
IllegalArgumentException- if component is null
-
removeComponent
Removes a component from this bundle.- Parameters:
component- the component to remove- Returns:
- true if the component was removed, false if not found
-
getComponents
Gets all components in this bundle.- Returns:
- list of components (defensive copy)
-
getComponentCount
public int getComponentCount()Gets the number of components in this bundle.- Returns:
- the component count
-
getTitle
Description copied from interface:MaterialComponentGets the title of this component. For individual materials, this is the material title. For bundles, this is the bundle name.- Specified by:
getTitlein interfaceMaterialComponent- Returns:
- the component title
-
getPrice
public double getPrice()Description copied from interface:MaterialComponentGets the total price of this component. For individual materials, this is the material price. For bundles, this is the sum of all contained materials.- Specified by:
getPricein interfaceMaterialComponent- Returns:
- the total price
-
getDiscountedPrice
public double getDiscountedPrice()Description copied from interface:MaterialComponentGets the discounted price of this component. For individual materials, this applies the material's discount. For bundles, this applies the bundle discount to the total price.- Specified by:
getDiscountedPricein interfaceMaterialComponent- Returns:
- the discounted price
-
getDescription
Description copied from interface:MaterialComponentGets a description of this component. Provides detailed information about the component and its contents.- Specified by:
getDescriptionin interfaceMaterialComponent- Returns:
- the component description
-
getMaterials
Description copied from interface:MaterialComponentGets all materials contained in this component. For individual materials, returns a list containing only itself. For bundles, returns all materials in the bundle (recursively).- Specified by:
getMaterialsin interfaceMaterialComponent- Returns:
- list of all contained materials
-
getItemCount
public int getItemCount()Description copied from interface:MaterialComponentGets the total number of items in this component. For individual materials, returns 1. For bundles, returns the sum of all contained items.- Specified by:
getItemCountin interfaceMaterialComponent- Returns:
- the total item count
-
getDiscountRate
public double getDiscountRate()Description copied from interface:MaterialComponentGets the discount rate applied to this component.- Specified by:
getDiscountRatein interfaceMaterialComponent- Returns:
- the discount rate (0.0 to 1.0)
-
isLeaf
public boolean isLeaf()Description copied from interface:MaterialComponentChecks if this component is a leaf (individual material).- Specified by:
isLeafin interfaceMaterialComponent- Returns:
- true if this is a leaf component
-
getBundleName
-
getBundleDiscount
public double getBundleDiscount()Gets the bundle discount rate.- Returns:
- the discount rate
-
getTotalSavings
public double getTotalSavings()Calculates the total savings from the bundle discount.- Returns:
- the total savings amount
-
containsType
Checks if this bundle contains any materials of the specified type.- Parameters:
type- the material type to check for- Returns:
- true if the bundle contains materials of the specified type
-
getMaterialsByType
Gets all materials of the specified type in this bundle.- Parameters:
type- the material type to filter by- Returns:
- list of materials of the specified type
-
containsMaterial
Checks if this bundle contains the specified material.- Parameters:
material- the material to check for- Returns:
- true if the bundle contains the material
-
equals
-
hashCode
-
toString
-