# (DD10) Business Logic — LibraryService.addBook() [6 LOC]

| Field | Value |
|-------|-------|
| Fully Qualified Name | `service.LibraryService` |
| Layer | Service |
| Module | `service` (Package: `service`) |

## 1. Role

### LibraryService.addBook()

This method performs the core business operation of registering a new book record into the library domain. It takes the book’s descriptive attributes — title, author, and ISBN — and converts them into a persisted `Book` domain object with a newly generated internal identifier. The method acts as a service-layer orchestration point: it does not validate user input, apply business rules beyond ID generation, or perform lookup logic; instead, it coordinates object creation and delegates storage to the repository layer. In architectural terms, it follows a simple create-and-persist pattern, where the service constructs the entity and hands it off to the repository for insertion. This method is part of the shared library management workflow and is typically used by presentation-layer entry points such as the console UI when a user adds a new book. There are no conditional branches, loops, or alternate processing paths in this method.

## 2. Processing Pattern (Detailed Business Logic)

```mermaid
flowchart TD
    START["addBook(title, author, isbn)"]
    GEN_ID["Generate book ID using UUID.randomUUID()
Prefix with 'B' and take first 6 uppercase characters"]
    CREATE_BOOK["Create Book(id, title, author, isbn)"]
    REPO_ADD["Call bookRepo.add(book)"]
    RETURN_BOOK["Return created Book"]
    END_NODE["End"]

    START --> GEN_ID
    GEN_ID --> CREATE_BOOK
    CREATE_BOOK --> REPO_ADD
    REPO_ADD --> RETURN_BOOK
    RETURN_BOOK --> END_NODE
```

## 3. Parameter Analysis

| No | Parameter Name | Type | Business Description |
|----|---------------|------|---------------------|
| 1 | `title` | `String` | The book title supplied by the user for catalog registration. It represents the display name of the book and is copied directly into the newly created `Book` object without transformation in this method. |
| 2 | `author` | `String` | The author name associated with the book being registered. It is stored as part of the book’s bibliographic metadata and is passed through unchanged into the domain entity. |
| 3 | `isbn` | `String` | The ISBN or book identifier used for library cataloging. It represents the external publication identifier and becomes part of the persisted `Book` record. |

**Instance fields / external state read by this method:**
- `bookRepo` — repository dependency used to persist the new `Book`.
- `UUID.randomUUID()` — external runtime source used to generate the internal book identifier.

## 4. CRUD Operations / Called Services

| CRUD | SC / CBS | SC Code | Entity / DB | Operation Description |
|------|----------|---------|-------------|-----------------------|
| C | `add` | - | `Book` / book repository storage | Persists the newly created book record into the repository. |

## 5. Dependency Trace

### Pre-computed evidence from code analysis graph:

No screen/batch entry points found within 8 hops. Direct callers found: 2 methods.
Terminal operations from this method: -

This method is called from the console UI and from a simple main entry point used for sample execution. The method itself terminates in a repository create operation after constructing the `Book` entity.

| # | Caller (Screen/Batch) | Call Chain (Full Path to this Method) | Terminal (SC / CRUD / Entity) |
|---|----------------------|--------------------------------------|-------------------------------|
| 1 | Main class: `Main` | `Main.main` -> `LibraryService.addBook` | `add [C] Book repository storage` |
| 2 | UI class: `ConsoleUI` | `ConsoleUI.addBook` -> `LibraryService.addBook` | `add [C] Book repository storage` |

## 6. Per-Branch Detail Blocks

**Block 1** — [SEQUENTIAL] `(method entry)` (L18)

> Accepts book registration input and begins the create flow.

| # | Type | Code |
|---|------|------|
| 1 | SET | `String id = "B" + UUID.randomUUID().toString().substring(0, 6).toUpperCase();` // Generate internal book ID |

**Block 2** — [SEQUENTIAL] `(create domain object)` (L20)

> Builds the `Book` aggregate from the generated identifier and supplied bibliographic data.

| # | Type | Code |
|---|------|------|
| 1 | SET | `Book book = new Book(id, title, author, isbn);` // Create new book entity |

**Block 3** — [SEQUENTIAL] `(persist book)` (L21)

> Delegates the create operation to the repository layer.

| # | Type | Code |
|---|------|------|
| 1 | CALL | `bookRepo.add(book);` // Store the new book in the repository |

**Block 4** — [SEQUENTIAL] `(return created book)` (L22)

> Returns the newly created domain object to the caller for immediate display or further use.

| # | Type | Code |
|---|------|------|
| 1 | RETURN | `return book;` |

## 7. Glossary

| Term | Type | Business Meaning |
|------|------|------------------|
| `LibraryService` | Class | Service-layer component that coordinates library business operations such as registering books and members. |
| `bookRepo` | Field | Repository dependency responsible for storing and retrieving `Book` records. |
| `Book` | Domain object | Library catalog entry representing a book and its bibliographic information. |
| `title` | Field | Book title used for catalog display and identification. |
| `author` | Field | Author name associated with the book. |
| `isbn` | Field | International Standard Book Number used to identify the publication. |
| `UUID` | Technical term | Universally Unique Identifier generator used here to create a unique internal book ID. |
| `book repository storage` | Technical term | In-memory or repository-backed persistence area where book records are added. |
| `Main` | Entry point | Sample application entry point that demonstrates service usage. |
| `ConsoleUI` | UI class | Console-based user interface that collects book details and calls the service. |
