Class BundleService
java.lang.Object
com.university.bookstore.composite.BundleService
Service for managing material bundles using the Composite pattern.
Provides operations for creating, managing, and analyzing material bundles.
This service demonstrates the Composite pattern in action by providing a high-level interface for working with both individual materials and bundles.
- Since:
- 2024-09-15
- Version:
- 3.0
- Author:
- Navid Mohaghegh
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classStatistics class for bundle analysis. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddBundleToBundle(String parentBundleName, String childBundleName) Adds a bundle to another bundle (nested bundles).voidaddComponentToBundle(String bundleName, MaterialComponent component) Adds a component to the specified bundle.voidaddToBundle(String bundleName, Material material) Adds a material to the specified bundle.doublecalculateBundleSavings(String bundleName) Calculates the total savings for a bundle.doubleCalculates the total savings across all bundles.voidClears all bundles.createBundle(String name, double discount) Creates a new material bundle with the specified name and discount.Gets all bundles.Gets a bundle by name.intGets the number of bundles.Gets all bundle names.getBundlesByDiscount(double minDiscount) Gets bundles with discounts greater than or equal to the specified threshold.Gets bundles that contain materials of the specified type.getBundlesByValueRange(double minValue, double maxValue) Gets bundles with values within the specified range.Gets bundle statistics.doubleGets the total value of all bundles.doubleGets the total discounted value of all bundles.booleanisEmpty()Checks if any bundles exist.booleanremoveBundle(String name) Removes a bundle by name.booleanremoveFromBundle(String bundleName, Material material) Removes a material from the specified bundle.toString()
-
Constructor Details
-
BundleService
public BundleService()Creates a new bundle service.
-
-
Method Details
-
createBundle
Creates a new material bundle with the specified name and discount.- Parameters:
name- the bundle namediscount- the discount rate (0.0 to 1.0)- Returns:
- the created bundle
- Throws:
IllegalArgumentException- if name is null or empty
-
addToBundle
Adds a material to the specified bundle.- Parameters:
bundleName- the name of the bundlematerial- the material to add- Throws:
IllegalArgumentException- if bundle not found or material is null
-
addBundleToBundle
Adds a bundle to another bundle (nested bundles).- Parameters:
parentBundleName- the name of the parent bundlechildBundleName- the name of the child bundle to add- Throws:
IllegalArgumentException- if either bundle is not found
-
addComponentToBundle
Adds a component to the specified bundle.- Parameters:
bundleName- the name of the bundlecomponent- the component to add- Throws:
IllegalArgumentException- if bundle not found or component is null
-
removeFromBundle
-
getBundle
Gets a bundle by name.- Parameters:
name- the bundle name- Returns:
- Optional containing the bundle if found
-
getAllBundles
-
getBundleNames
-
calculateBundleSavings
Calculates the total savings for a bundle.- Parameters:
bundleName- the name of the bundle- Returns:
- the total savings amount
- Throws:
IllegalArgumentException- if bundle not found
-
calculateTotalSavings
public double calculateTotalSavings()Calculates the total savings across all bundles.- Returns:
- the total savings amount
-
getTotalBundleValue
public double getTotalBundleValue()Gets the total value of all bundles.- Returns:
- the total value
-
getTotalDiscountedBundleValue
public double getTotalDiscountedBundleValue()Gets the total discounted value of all bundles.- Returns:
- the total discounted value
-
getBundlesByMaterialType
Gets bundles that contain materials of the specified type.- Parameters:
type- the material type to filter by- Returns:
- list of bundles containing the specified type
-
getBundlesByDiscount
Gets bundles with discounts greater than or equal to the specified threshold.- Parameters:
minDiscount- the minimum discount rate- Returns:
- list of bundles meeting the criteria
-
getBundlesByValueRange
Gets bundles with values within the specified range.- Parameters:
minValue- the minimum valuemaxValue- the maximum value- Returns:
- list of bundles within the value range
-
removeBundle
Removes a bundle by name.- Parameters:
name- the bundle name- Returns:
- true if the bundle was removed, false if not found
-
clearAllBundles
public void clearAllBundles()Clears all bundles. -
getBundleCount
public int getBundleCount()Gets the number of bundles.- Returns:
- the bundle count
-
isEmpty
public boolean isEmpty()Checks if any bundles exist.- Returns:
- true if no bundles exist
-
getBundleStats
Gets bundle statistics.- Returns:
- bundle statistics
-
toString
-