# Root

## Overview

The root of this codebase appears to represent a small library management application organized as a clean layered system. The core business capability is to manage books, members, and loans: catalog items are created and stored, members are registered, and borrowing/returning flows keep inventory state in sync with loan state.

From the child documentation, this area is structured around a straightforward flow:
- `model` defines the domain objects
- `repository` stores them in memory and provides lookup/query access
- `service` orchestrates application behavior and business rules
- `ui` presents the console interface and delegates actions to services
- `util` provides shared console input helpers

This appears to be a deliberately lightweight architecture, optimized for readability and simple interactive use rather than persistence, networking, or advanced infrastructure.

## Sub-module Guide

### `model`

The `model` package defines the primary domain concepts: `Book`, `Member`, and `Loan`.

These types are the shared language of the system. `Book` holds catalog metadata and availability state, `Member` represents the borrower, and `Loan` ties the two together while tracking borrow and return dates. The key relationship is that `Loan` coordinates with `Book` to flip availability when a return occurs.

In other words, `model` owns the data shape and the smallest unit of business state, but it does not manage storage or user interaction.

### `repository`

The `repository` package provides in-memory storage for `Book` and `Member` objects.

It sits directly on top of the model layer and gives the rest of the system a simple API for adding, finding, listing, and filtering records. `BookRepository` adds a few extra queries such as available-only listings and case-insensitive title search, while `MemberRepository` stays minimal.

These repositories are the persistence boundary for the application as documented here, but they are temporary and process-local. They do not own business rules; they simply preserve and expose collections of model objects.

### `service`

The `service` package is the coordination layer.

`LibraryService` acts as a façade for catalog and registration tasks, creating books and members, generating IDs, and delegating reads to repositories. `LoanService` handles the workflow around borrowing and returning books. It validates inputs against repositories, mutates book availability, and stores active loan records in memory.

This layer is where the system’s behavior becomes visible as application logic rather than raw data access. The services connect repository state with model transitions and prepare results that the UI can display.

### `ui`

The `ui` package contains the console entry point, centered on `ConsoleUI`.

This module is the user-facing shell of the application. It presents a menu, collects input, and calls the service layer for every meaningful action. It does not create or validate domain objects itself; instead, it translates user intent into service calls and prints the results.

The UI depends on the service layer for all business operations and on model `toString()` implementations for readable console output.

### `util`

The `util` package contains shared helpers for console input, currently represented by `InputUtils`.

This utility class standardizes line and integer reading, trims text, handles invalid numeric input, and closes the shared scanner at shutdown. It exists to keep input handling consistent across the UI and to avoid repeating parsing logic.

`util` does not participate in business flow directly, but it supports the UI boundary and helps keep the interactive experience predictable.

## Key Patterns and Architecture

### Layered flow

The codebase follows a classic top-down layering pattern:

- `ui` handles presentation and interaction
- `service` handles orchestration and business rules
- `repository` handles storage access
- `model` holds domain state
- `util` supports cross-cutting console concerns

This separation keeps responsibilities narrow. The UI can remain procedural, services can focus on use cases, and repositories can stay simple and easy to reason about.

### Domain objects are mutable and shared by reference

This appears to be an intentional design choice in the model and service layers. `Book` exposes mutable availability, and `LoanService` changes that flag directly when borrowing or returning a book. Because repositories store the domain objects themselves, updates made in the service layer are immediately reflected in repository-backed views.

That means the system relies on shared object identity rather than copying or event propagation.

### In-memory state with explicit orchestration

The repositories and loan tracking are in memory only. There is no persistence layer visible in the indexed documentation, so application state lives for the duration of the process.

The upside is simplicity. The tradeoff is that restarting the application resets catalog, member, and active loan state unless another layer outside the documented modules persists them.

### Console-first application flow

The UI and util modules are built around a terminal workflow. `InputUtils` normalizes prompts and parsing, while `ConsoleUI` focuses on menu dispatch and display. The services return simple values and messages that can be printed directly, which keeps the interactive path low-friction.

### System interaction overview

```mermaid
flowchart TD
RootModule["Root module"] --> ModelModule["model"]
RootModule --> RepositoryModule["repository"]
RootModule --> ServiceModule["service"]
RootModule --> UiModule["ui"]
RootModule --> UtilModule["util"]
ModelModule --> RepositoryModule
RepositoryModule --> ServiceModule
ServiceModule --> UiModule
UtilModule --> UiModule
```

## Dependencies and Integration

### Internal dependencies

The module relationships described in the child documentation suggest the following dependency direction:

- `service` depends on `model` and `repository`
- `ui` depends on `service`, `model`, and `util`
- `repository` depends on `model`
- `util` depends only on the Java standard library
- `model` depends only on standard library types such as `LocalDate`

This is a one-way dependency chain from the data model upward to the UI.

### External dependencies

The documented code relies mainly on the Java standard library:

- collection types such as `List`, `ArrayList`, and `Optional`
- `java.time.LocalDate` for loan dates
- `java.util.UUID` for generating short identifiers in services
- `java.util.Scanner` for console input

There is no evidence in the provided documentation of database, web, or messaging dependencies.

### Integration within the wider system

This root area appears to form the application core for a console-based library system. The service layer is the main integration point for higher-level callers, and the UI is the likely entry point for end users.

If additional modules exist outside the indexed child pages, they would likely compose these packages rather than replace them, because the documented code already provides a full vertical slice from domain object to terminal interaction.

## Notes for Developers

- `Loan` is the key bridge between book inventory and member activity. If you change borrowing rules, review both `LoanService` and `Loan` together.
- `Book` availability is mutable and is used as the source of truth for whether a book can be borrowed. Any alternate borrow path should keep that flag in sync.
- Repository methods return snapshots or filtered results, but they still expose the underlying model objects. Mutating those objects affects the shared in-memory state.
- `LoanService` stores loans locally rather than in a repository. If you need loan history or persistence, that is the first place the architecture would need to expand.
- The UI expects service methods to return printable values or messages. If service contracts change, console output will need to be updated accordingly.
- `InputUtils` is intentionally shared and stateful through a single `Scanner`. Be careful with lifecycle management if you add more interactive entry points.

Overall, this codebase appears to be optimized for clarity and directness. Each module has a narrow job, and the system works by passing domain objects through a simple, well-defined stack from terminal input to in-memory storage and back to console output.