# 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

- ## Nested Class Summary

  Nested Classes
  Modifier and Type
  Class
  Description
  `static class`
  `MaterialService.InvalidMaterialException`
  Exception thrown when a material is invalid according to business rules.
  `static class`
  `MaterialService.MaterialNotFoundException`
  Exception thrown when a requested material is not found.

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `MaterialService(MaterialRepository repository)`
  Creates a new material service with the specified repository.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `void`
  `addMaterial(Material material)`
  Adds a material to the system with business logic validation.
  `void`
  `clearAllMaterials()`
  Clears all materials from the system.
  `Material`
  `findMaterial(String id)`
  Finds a material by its ID.
  `Optional<Material>`
  `findMaterialOptional(String id)`
  Finds a material by its ID, returning Optional.
  `List<Material>`
  `getAllMaterials()`
  Gets all materials in the system.
  `long`
  `getMaterialCount()`
  Gets the total number of materials in the system.
  `boolean`
  `materialExists(String id)`
  Checks if a material exists in the system.
  `boolean`
  `removeMaterial(String id)`
  Removes a material from the system.
  `void`
  `updateMaterial(Material material)`
  Updates an existing material with business logic validation.

  ### Methods inherited from class java.lang.Object

  `equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## 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.