Virtual Credits Wallet with class and Private Field #saldo
Implement with Nexo the CuentaProtegida class featuring true encapsulation via #saldo, validated deposit and withdrawal methods, and an operation history.
Nexo is building the Código con Criterio virtual credits wallet and needs to protect each account's balance so it can only be modified through domain rules validated inside a class with true encapsulation.
Implement the CuentaProtegida class and the solve(input) function, which receives an object with:
titular: non-empty string of 1 to 40 characters.
saldoInicial: integer from 0 to 100000.
operaciones: array of 0 to 100 objects, each with:
tipo: strictly "DEPOSITAR" or "RETIRAR".
monto: integer from 0 to 100000.
class CuentaProtegida Requirements
Properties and constructor:
Public property titular and private field #saldo.
constructor(titular, saldoInicial) initializing this.titular = titular and this.#saldo = saldoInicial.
Instance methods:
depositar(monto): accepts only monto > 0. If valid, adds monto to #saldo and returns true; if monto === 0, keeps #saldo unchanged and returns false.
retirar(monto): accepts only monto > 0 and monto <= this.#saldo (a withdrawal strictly equal to the current balance is valid and leaves #saldo at 0). If valid, subtracts monto from #saldo and returns true; if monto === 0 or monto > this.#saldo, keeps #saldo unchanged and returns false.
consultarSaldo(): returns the current value of this.#saldo without modifying it.
Coordination in solve(input)
Create an instance using new CuentaProtegida(titular, saldoInicial).
Iterate through operaciones sequentially with for...of, call depositar(op.monto) or retirar(op.monto) accordingly, and record in historial a new object for each operation with strictly:
tipo: op.tipo
monto: op.monto
exitosa: boolean returned by the method
saldoPosterior: integer returned by consultarSaldo() immediately after the operation
Count how many operations succeeded (operacionesExitosas) and how many were rejected (operacionesRechazadas). Do not mutate input or its objects.
Return Value
Return an object with exactly these six properties:
titular: original string.
saldoInicial: original integer.
saldoFinal: integer obtained via cuenta.consultarSaldo() at the end.
operacionesExitosas: integer.
operacionesRechazadas: integer.
historial: array of { tipo, monto, exitosa, saldoPosterior } objects in original order.