Your Mission
Nexo manages the Código con Criterio digital library and needs to model its catalog and loan tracking by separating responsibilities between two collaborating classes: Libro (owner of available and total copies) and Biblioteca (owner of the Libro instance collection and active loans).
Implement class Libro, class Biblioteca, and solve(input), which receives:
libros: array of 0 to 50 objects with id (unique integer from 1 to 10000), titulo (non-empty string of 1 to 80 characters), and copias (integer from 0 to 20).
operaciones: array of 0 to 100 objects with tipo ("PRESTAR" or "DEVOLVER"), libroId (integer from 1 to 10000, may not exist), and usuarioId (integer from 1 to 10000).
class Libro Requirements
- Public properties
id and titulo; private fields #copiasDisponibles and #copiasTotales.
constructor(id, titulo, copias): initializes id, titulo, and both private fields with copias.
prestar(): if #copiasDisponibles > 0, decrements by 1 and returns true; otherwise returns false.
devolver(): if #copiasDisponibles < #copiasTotales, increments by 1 and returns true; otherwise returns false.
consultarDisponibles() and consultarTotales(): return the corresponding private values.
class Biblioteca Requirements
- Private fields
#libros (array of Libro instances created with new Libro(...)) and #prestamos (array of active loans { libroId, usuarioId }).
constructor(libros): creates the Libro instances in original order and initializes #prestamos as empty.
prestar(libroId, usuarioId): checks in this exact order:
- If the book does not exist in
#libros, return "LIBRO_NO_ENCONTRADO".
- If an active loan already exists for the same
libroId and usuarioId, return "PRESTAMO_DUPLICADO".
- If
libro.prestar() returns false (no copies left), return "SIN_COPIAS".
- Otherwise, record
{ libroId, usuarioId } in #prestamos and return "OK".
devolver(libroId, usuarioId): checks in this exact order:
- If the book does not exist in
#libros, return "LIBRO_NO_ENCONTRADO".
- If no active loan exists for that
libroId and usuarioId, return "PRESTAMO_NO_ENCONTRADO".
- Otherwise, remove that active loan (preserving the order of remaining loans), call
libro.devolver(), and return "OK".
consultarLibros(): returns a new array of { id, titulo, copiasTotales, copiasDisponibles } objects in catalog order.
consultarPrestamos(): returns a new array of new { libroId, usuarioId } objects in registration order of still-active loans.
Iterate through operaciones in order, call prestar or devolver on the Biblioteca instance, and record in historial objects with { tipo, libroId, usuarioId, resultado, copiasDisponibles }, where copiasDisponibles is the book's available stock after the operation or null if the book does not exist. Do not mutate input.
Return Value
Return an object with exactly five properties:
catalogoFinal: result of biblioteca.consultarLibros().
prestamosActivos: result of biblioteca.consultarPrestamos().
operacionesExitosas: count of operations with resultado === "OK".
operacionesRechazadas: count of operations with any other status code.
historial: array detailing each processed operation.
Example 1
Input
{
"libros": [
{
"id": 1,
"copias": 1,
"titulo": "Eloquent JavaScript"
},
{
"id": 2,
"copias": 2,
"titulo": "Clean Code"
}
],
"operaciones": [
{
"tipo": "PRESTAR",
"libroId": 1,
"usuarioId": 100
},
{
"tipo": "PRESTAR",
"libroId": 1,
"usuarioId": 100
},
{
"tipo": "PRESTAR",
"libroId": 1,
"usuarioId": 200
},
{
"tipo": "DEVOLVER",
"libroId": 1,
"usuarioId": 100
}
]
}Expected result
{
"historial": [
{
"tipo": "PRESTAR",
"libroId": 1,
"resultado": "OK",
"usuarioId": 100,
"copiasDisponibles": 0
},
{
"tipo": "PRESTAR",
"libroId": 1,
"resultado": "PRESTAMO_DUPLICADO",
"usuarioId": 100,
"copiasDisponibles": 0
},
{
"tipo": "PRESTAR",
"libroId": 1,
"resultado": "SIN_COPIAS",
"usuarioId": 200,
"copiasDisponibles": 0
},
{
"tipo": "DEVOLVER",
"libroId": 1,
"resultado": "OK",
"usuarioId": 100,
"copiasDisponibles": 1
}
],
"catalogoFinal": [
{
"id": 1,
"titulo": "Eloquent JavaScript",
"copiasTotales": 1,
"copiasDisponibles": 1
},
{
"id": 2,
"titulo": "Clean Code",
"copiasTotales": 2,
"copiasDisponibles": 2
}
],
"prestamosActivos": [],
"operacionesExitosas": 2,
"operacionesRechazadas": 2
}Example 2
Input
{
"libros": [
{
"id": 10,
"copias": 2,
"titulo": "Refactoring"
}
],
"operaciones": [
{
"tipo": "PRESTAR",
"libroId": 10,
"usuarioId": 1
},
{
"tipo": "PRESTAR",
"libroId": 10,
"usuarioId": 2
},
{
"tipo": "DEVOLVER",
"libroId": 99,
"usuarioId": 1
},
{
"tipo": "DEVOLVER",
"libroId": 10,
"usuarioId": 3
}
]
}Expected result
{
"historial": [
{
"tipo": "PRESTAR",
"libroId": 10,
"resultado": "OK",
"usuarioId": 1,
"copiasDisponibles": 1
},
{
"tipo": "PRESTAR",
"libroId": 10,
"resultado": "OK",
"usuarioId": 2,
"copiasDisponibles": 0
},
{
"tipo": "DEVOLVER",
"libroId": 99,
"resultado": "LIBRO_NO_ENCONTRADO",
"usuarioId": 1,
"copiasDisponibles": null
},
{
"tipo": "DEVOLVER",
"libroId": 10,
"resultado": "PRESTAMO_NO_ENCONTRADO",
"usuarioId": 3,
"copiasDisponibles": 0
}
],
"catalogoFinal": [
{
"id": 10,
"titulo": "Refactoring",
"copiasTotales": 2,
"copiasDisponibles": 0
}
],
"prestamosActivos": [
{
"libroId": 10,
"usuarioId": 1
},
{
"libroId": 10,
"usuarioId": 2
}
],
"operacionesExitosas": 2,
"operacionesRechazadas": 2
}