Class MaterialService

java.lang.Object
com.university.bookstore.service.MaterialService

public class MaterialService extends Object
Domain service that orchestrates business logic for material management. Demonstrates hexagonal architecture by coordinating between domain logic and infrastructure.

This service encapsulates business rules and coordinates between the domain layer and the repository layer, maintaining clean separation of concerns.

Since:
2024-09-15
Version:
3.0
Author:
Navid Mohaghegh
  • Constructor Details

    • MaterialService

      public MaterialService(MaterialRepository repository)
      Creates a new material service with the specified repository.
      Parameters:
      repository - the material repository to use
  • Method Details

    • addMaterial

      public void addMaterial(Material material)
      Adds a material to the system with business logic validation.
      Parameters:
      material - the material to add
      Throws:
      MaterialService.InvalidMaterialException - if the material is invalid
    • updateMaterial

      public void updateMaterial(Material material)
      Updates an existing material with business logic validation.
      Parameters:
      material - the material to update
      Throws:
      MaterialService.InvalidMaterialException - if the material is invalid
      MaterialService.MaterialNotFoundException - if the material doesn't exist
    • findMaterial

      public Material findMaterial(String id)
      Finds a material by its ID.
      Parameters:
      id - the material ID
      Returns:
      the material if found
      Throws:
      MaterialService.MaterialNotFoundException - if the material doesn't exist
    • findMaterialOptional

      public Optional<Material> findMaterialOptional(String id)
      Finds a material by its ID, returning Optional.
      Parameters:
      id - the material ID
      Returns:
      Optional containing the material if found
    • getAllMaterials

      public List<Material> getAllMaterials()
      Gets all materials in the system.
      Returns:
      list of all materials
    • removeMaterial

      public boolean removeMaterial(String id)
      Removes a material from the system.
      Parameters:
      id - the material ID to remove
      Returns:
      true if the material was removed, false if not found
    • materialExists

      public boolean materialExists(String id)
      Checks if a material exists in the system.
      Parameters:
      id - the material ID
      Returns:
      true if the material exists
    • getMaterialCount

      public long getMaterialCount()
      Gets the total number of materials in the system.
      Returns:
      the material count
    • clearAllMaterials

      public void clearAllMaterials()
      Clears all materials from the system.