Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
228 changes: 228 additions & 0 deletions Roadmap/26 - SOLID SRP/javascript/pedamoci.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
// SRP MAL HECHO
class WrongSRP {
sumar(num1, num2) {
console.log(num1 + num2)
}

dateNow() {
const date = new Date
console.log(date.toDateString())
}
}

const wrongSRP = new WrongSRP
wrongSRP.sumar(45, 321)
wrongSRP.dateNow()

// SRP BIEN HECHO
class SumarSRP {
sumar(num1, num2) {
console.log(num1 + num2)
}
}

class DateNowSRP {
dateNow() {
const date = new Date
console.log(date.toDateString())
}
}

const sumarSRP = new SumarSRP
sumarSRP.sumar(56, 987)

const dateNowSRP = new DateNowSRP
dateNowSRP.dateNow()

// ------------------------------------------------ DIFICULTAD EXTRA ------------------------------------------------
class ManagementLibrary {
constructor() {
this.books = new Map()
this.users = new Map()
this.loans = []
}

addBook(title, writer, numberCopies) {
if (this.books.has(title)) console.warn('El libro que desea agregar ya está en la biblioteca')
else {
this.books.set(title, {writer: writer, copies: numberCopies})
console.log('El libro se ha agregado al catálogo de la biblioteca')
}
}

addUser(idUser, name, number) {
if (this.users.has(idUser)) console.warn('El usuario ya está registrado en la biblioteca')
else {
this.users.set(idUser, {name: name, number: number})
console.log('El usuario se ha registrado correctamente en la biblioteca')
}
}

loanBook(title, idUser) {
if (!this.books.has(title)) return console.warn('El libro que desea no está en esta biblioteca')
if (!this.users.has(idUser)) return console.warn('El usuario no esta registrado en esta biblioteca')
const book = this.books.get(title)
if (book.copies > 0) {
this.loans.push([idUser, title])
book.copies--
console.log('Recuerda devolver el libro cuando lo termines, gracias!')
} else {
console.warn('No quedan existencias del libro que desea\nPregunte nuevamente en otro momento o elija otro')
}
}

returnBook(title, idUser) {
if (this.books.has(title) && this.users.has(idUser)) {
let booksLoanedUser = this.loans.filter((loan) => loan[0] === idUser) // devuelde los libros retirados por el usuario
if (booksLoanedUser.length > 0 && booksLoanedUser.map((loan) => loan[1] === title)) { // verifica que haya libros retirado por el usuario y que coincida con el titulo ingresado
let index = this.loans.findIndex((loan) => {return loan[0] === idUser && loan[1] === title}) // devuelve el index del array donde esta el libro retirado y el id del usuario
this.loans.splice(index, 1)
const book = this.books.get(title)
book.copies++
console.log('Has devuelto el libro!! Gracias!!')
} else console.warn('Id o titulo del libro incorrectos')
}
else {
console.error('Datos incorrectos, vuelve a chequear que escribiste bien el nombre del libro o tu id')
}
}
}

const library = new ManagementLibrary()

//Creación de libros
console.log('----------------- CREACION DE LIBROS -----------------')
library.addBook('Cien años de soledad', 'Gabriel García Márquez', 5); library.addBook('1984', 'George Orwell', 7); library.addBook('Orgullo y Prejuicio', 'Jane Austen', 3); library.addBook('El Principito', 'Antoine de Saint-Exupéry', 10); library.addBook('Don Quijote de la Mancha', 'Miguel de Cervantes', 2); library.addBook('Crimen y Castigo', 'Fiódor Dostoievski', 4); library.addBook('Moby Dick', 'Herman Melville', 6); library.addBook('Ensayo sobre la ceguera', 'José Saramago', 8)

//Creación de usuarios
console.log('\n----------------- CREACION DE USUARIOS -----------------')
library.addUser(10001, 'Martina López', 987654321); library.addUser(10002, 'Javier Ríos', 912345678); library.addUser(10003, 'Sofía Giménez', 900112233); library.addUser(10004, 'Andrés Castro', 945612378); library.addUser(10005, 'Valeria Torres', 998877665); library.addUser(10006, 'Ricardo Paz', 965432101); library.addUser(10007, 'Elena Ruiz', 955544433); library.addUser(10008, 'Felipe Díaz', 933322211); library.addUser(10009, 'Carla Méndez', 977788899); library.addUser(10010, 'Pablo Núñez', 921234567); library.addUser(10011, 'Luisa Vargas', 987123456); library.addUser(10012, 'Diego Soto', 911223344); library.addUser(10013, 'Catalina Vega', 950505050); library.addUser(10014, 'Hugo Ferrer', 930303030); library.addUser(10015, 'Micaela Gil', 970707070)

// Creación de prestamos
console.log('\n----------------- CREACION DE PRESTAMOS -----------------')
// AGOTANDO EXISTENCIAS: 'Don Quijote de la Mancha' y 'Orgullo y Prejuicio'
library.loanBook('Don Quijote de la Mancha', 10001); library.loanBook('Don Quijote de la Mancha', 10002)
library.loanBook('Orgullo y Prejuicio', 10003); library.loanBook('Orgullo y Prejuicio', 10004); library.loanBook('Orgullo y Prejuicio', 10005)

// PETICIONES EXITOSAS
library.loanBook('1984', 10006); library.loanBook('Cien años de soledad', 10007); library.loanBook('Ensayo sobre la ceguera', 10008); library.loanBook('Moby Dick', 10009); library.loanBook('Crimen y Castigo', 10010)

// PETICIONES FALLIDAS
console.log('\n----------------- CREACION DE PRESTAMOS ERRONEOS -----------------')
//Libros agotados
library.loanBook('Don Quijote de la Mancha', 10011); library.loanBook('Orgullo y Prejuicio', 10012)
//Libros inexistentes
library.loanBook('El Código Da Vinci', 10013); library.loanBook('Frieren', 10014)

// Creación de devoluciones
// Devoluciones exitosas
console.log('\n----------------- CREACION DE DEVOLUCIONES -----------------')
library.returnBook('Don Quijote de la Mancha', 10001); library.returnBook('Orgullo y Prejuicio', 10005); library.returnBook('1984', 10006); library.returnBook('Cien años de soledad', 10007); library.returnBook('Moby Dick', 10009)

// Devolución con ID de usuario incorrecto (posiblemente fallida)
console.log('\n----------------- CREACION DE DEVOLUCIONES ERRONEAS -----------------')
library.returnBook('Crimen y Castigo', 32465)



// ---------------------------------------------------------------- REFACTORIZACION ----------------------------------------------------------------
class AddBook {
constructor() {
this.books = new Map()
}

addBook(title, writer, numberCopies) {
if (this.books.has(title)) console.warn('El libro que desea agregar ya está en la biblioteca')
else {
this.books.set(title, {writer: writer, copies: numberCopies})
console.log('El libro se ha agregado al catálogo de la biblioteca')
}
}
}

class AddUser {
constructor() {
this.users = new Map()
}

addUser(idUser, name, number) {
if (this.users.has(idUser)) console.warn('El usuario ya está registrado en la biblioteca')
else {
this.users.set(idUser, {name: name, number: number})
console.log('El usuario se ha registrado correctamente en la biblioteca')
}
}
}

class Loans {
constructor() {
this.loans = []
}

loanBook(title, idUser) {
if (!inventory.books.has(title)) return console.warn('El libro que desea no está en esta biblioteca')
if (!clients.users.has(idUser)) return console.warn('El usuario no esta registrado en esta biblioteca')
const book = inventory.books.get(title)
if (book.copies > 0) {
this.loans.push([idUser, title])
book.copies--
console.log('Recuerda devolver el libro cuando lo termines, gracias!')
}
else {
console.warn('No quedan existencias del libro que desea\nPregunte nuevamente en otro momento o elija otro')
}
}

returnBook(title, idUser) {
if (inventory.books.has(title) && clients.users.has(idUser)) {
let booksLoanedUser = this.loans.filter((loan) => loan[0] === idUser) // devuelde los libros retirados por el usuario
if (booksLoanedUser.length > 0 && booksLoanedUser.map((loan) => loan[1] === title)) { // verifica que haya libros retirado por el usuario y que coincida con el titulo ingresado
let index = this.loans.findIndex((loan) => {return loan[0] === idUser && loan[1] === title}) // devuelve el index del array donde esta el libro retirado y el id del usuario
this.loans.splice(index, 1)
const book = inventory.books.get(title)
book.copies++
console.log('Has devuelto el libro!! Gracias!!')
} else console.warn('Id o titulo del libro incorrectos')
}
else {
console.error('Datos incorrectos, vuelve a chequear que escribiste bien el nombre del libro o tu id')
}
}
}

const inventory = new AddBook()
const clients = new AddUser()
const loans = new Loans()

//Creación de libros
console.log('----------------- CREACION DE LIBROS -----------------')
inventory.addBook('Cien años de soledad', 'Gabriel García Márquez', 5); inventory.addBook('1984', 'George Orwell', 7); inventory.addBook('Orgullo y Prejuicio', 'Jane Austen', 3); inventory.addBook('El Principito', 'Antoine de Saint-Exupéry', 10); inventory.addBook('Don Quijote de la Mancha', 'Miguel de Cervantes', 2); inventory.addBook('Crimen y Castigo', 'Fiódor Dostoievski', 4); inventory.addBook('Moby Dick', 'Herman Melville', 6); inventory.addBook('Ensayo sobre la ceguera', 'José Saramago', 8)

//Creación de usuarios
console.log('\n----------------- CREACION DE USUARIOS -----------------')
clients.addUser(10001, 'Martina López', 987654321); clients.addUser(10002, 'Javier Ríos', 912345678); clients.addUser(10003, 'Sofía Giménez', 900112233); clients.addUser(10004, 'Andrés Castro', 945612378); clients.addUser(10005, 'Valeria Torres', 998877665); clients.addUser(10006, 'Ricardo Paz', 965432101); clients.addUser(10007, 'Elena Ruiz', 955544433); clients.addUser(10008, 'Felipe Díaz', 933322211); clients.addUser(10009, 'Carla Méndez', 977788899); clients.addUser(10010, 'Pablo Núñez', 921234567); clients.addUser(10011, 'Luisa Vargas', 987123456); clients.addUser(10012, 'Diego Soto', 911223344); clients.addUser(10013, 'Catalina Vega', 950505050); clients.addUser(10014, 'Hugo Ferrer', 930303030); clients.addUser(10015, 'Micaela Gil', 970707070)

// Creación de prestamos
console.log('\n----------------- CREACION DE PRESTAMOS -----------------')
// AGOTANDO EXISTENCIAS: 'Don Quijote de la Mancha' y 'Orgullo y Prejuicio'
loans.loanBook('Don Quijote de la Mancha', 10001); loans.loanBook('Don Quijote de la Mancha', 10002)
loans.loanBook('Orgullo y Prejuicio', 10003); loans.loanBook('Orgullo y Prejuicio', 10004); loans.loanBook('Orgullo y Prejuicio', 10005)

// PETICIONES EXITOSAS
loans.loanBook('1984', 10006); loans.loanBook('Cien años de soledad', 10007); loans.loanBook('Ensayo sobre la ceguera', 10008); loans.loanBook('Moby Dick', 10009); loans.loanBook('Crimen y Castigo', 10010)

// PETICIONES FALLIDAS
console.log('\n----------------- CREACION DE PRESTAMOS ERRONEOS -----------------')
//Libros agotados
loans.loanBook('Don Quijote de la Mancha', 10011); loans.loanBook('Orgullo y Prejuicio', 10012)
//Libros inexistentes
loans.loanBook('El Código Da Vinci', 10013); loans.loanBook('Frieren', 10014)

// Creación de devoluciones
// Devoluciones exitosas
console.log('\n----------------- CREACION DE DEVOLUCIONES -----------------')
loans.returnBook('Don Quijote de la Mancha', 10001); loans.returnBook('Orgullo y Prejuicio', 10005); loans.returnBook('1984', 10006); loans.returnBook('Cien años de soledad', 10007); loans.returnBook('Moby Dick', 10009)

// Devolución con ID de usuario incorrecto (posiblemente fallida)
console.log('\n----------------- CREACION DE DEVOLUCIONES ERRONEAS -----------------')
loans.returnBook('Crimen y Castigo', 32465)
150 changes: 150 additions & 0 deletions Roadmap/27 - SOLID OCP/javascript/pedamoci.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
// OCP MAL HECHO
class DateNow {
getDateNow(info) {
const date = new Date
switch (info) {
case 'day':
console.log(`El día es: ${date.getDay()}`)
break;
case 'month':
console.log(`El mes es: ${date.getMonth()}`)
break;
default:
break;
}
}
}
console.log('------------- OCP MAL HECHO -------------')
const dateNow = new DateNow
dateNow.getDateNow('day')
dateNow.getDateNow('month')

// Si necesito agregar una función tengo que modificar el switch

class DateNow2 {
getDateNow(info) {
const date = new Date
switch (info) {
case 'day':
console.log(`El día es: ${date.getDay()}`)
break;
case 'month':
console.log(`El mes es: ${date.getMonth()}`)
break;
case 'year':
console.log(`El año es: ${date.getFullYear()}`)
break;
default:
break;
}
}
}

const dateNow2 = new DateNow2
dateNow2.getDateNow('day')
dateNow2.getDateNow('month')
dateNow2.getDateNow('year')

// OCP BIEN HECHO
class DateNow3 {
constructor(getDate) {
this.getDate = getDate
}

getDateNow() {
return this.getDate.getDateNow()
}
}

class GetDateNow {
getDateNow() {}
}

class GetDay extends GetDateNow{
getDateNow() {
const date = new Date
return date.getDay()
}
}

class GetMonth extends GetDateNow{
getDateNow() {
const date = new Date
return date.getMonth()
}
}

console.log('\n------------- OCP BIEN HECHO -------------')
const getDay = new DateNow3(new GetDay())
console.log(`El día es: ${getDay.getDateNow()}`)
const getMonth = new DateNow3(new GetMonth())
console.log(`El mes es: ${getMonth.getDateNow()}`)

// Si quiero agregar una función
class GetYear extends GetDateNow {
getDateNow() {
const date = new Date
return date.getFullYear()
}
}

const getYear = new DateNow3(new GetYear())
console.log(`El año es: ${getYear.getDateNow()}`)

// -------------------------------------------------- EJERCICIO EXTRA --------------------------------------------------
class Calculator {
constructor(calculation) {
this.calculation = calculation
}

operation(num1, num2) {
return this.calculation.operation(num1, num2)
}
}

class Operations {
operation() {}
}

class Addition extends Operations{
operation(num1, num2) {
return num1 + num2
}
}

class Subtraction extends Operations{
operation(num1, num2) {
return num1 - num2
}
}

class Division extends Operations{
operation(num1, num2) {
if (num2 === 0) return 'No se puede dividir por cero'
return num1 / num2
}
}

// Comprobación de que el sistema funciona
console.log('\n--------------- COMPROBACION DE FUNCIONAMIENTO ---------------')
const addition = new Calculator(new Addition())
console.log(`Suma: ${addition.operation(55, 32)}`)

const subtraction = new Calculator(new Subtraction())
console.log(`Resta: ${subtraction.operation(55, 32)}`)

const division = new Calculator(new Division())
console.log(`Divición: ${division.operation(55, 5)}`)

class Exponentiation extends Operations{
operation(num1, num2) {
return num1 ** num2
}
}

console.log('\n--------------- COMPROBACION DE FUNCIONAMIENTO FINAL ---------------')
const exponentiation = new Calculator(new Exponentiation())
console.log(`Suma: ${addition.operation(5465, 3245)}`)
console.log(`Resta: ${subtraction.operation(35480, 54982)}`)
console.log(`Divición: ${division.operation(8464, 154)}`)
console.log(`Exponenciación: ${exponentiation.operation(55, 4)}`)
Loading