Product Model
TheProductoPOJO represents a product entity in the e-commerce system. It encapsulates product information including identification, name, and price with built-in validation through value objects.
Model Structure
Fields
Unique identifier for the product.
Product name or title.
Price value object with built-in validation to ensure price integrity.
Price Validation
The
PrecioVO value object enforces price integrity through constructor validation. Any attempt to create an invalid price will throw an IllegalArgumentException.Validation Rules
The price must satisfy ALL of the following conditions:| Rule | Requirement | Exception Message |
|---|---|---|
| Not Null | Price value cannot be null | ”El precio no puede ser nulo” |
| Non-Negative | Price must be >= 0 | ”El precio no puede ser negativo” |
Valid Price Examples
Invalid Price Examples
Creating a Product
Updating a Price
ThePrecioVO provides an actualizarPrecio() method that creates a new price value object:
Price value objects are immutable. The
actualizarPrecio() method returns a new PrecioVO instance rather than modifying the existing one. This ensures price history and audit trails remain consistent.Complete Product Example
Price Display and Formatting
ThePrecioVO class overrides toString() to return the string representation of the price value:
For currency formatting with symbols ($, €, etc.) and locale-specific formatting, implement formatting logic in your presentation layer or API response serializer.
Business Rules
Free Products
Products with a price of0.0 are considered free:
Price Updates
Equality and Comparison
TwoPrecioVO objects are equal if their values are equal:
API Integration Examples
GET /productos/
Response
POST /productos
Request
Response (201 Created)
PUT /productos/
Request
Error Handling
Invalid Price Errors
400 Bad Request - Null Price
400 Bad Request - Negative Price
Related Models
- User Model - User entity with password validation
Source Code Reference
ProductoPOJO: src/main/java/com/example/demo/producto/domain/models/ProductoPOJO.java:11PrecioVO: src/main/java/com/example/demo/producto/domain/vo/PrecioVO.java:6