Skip to content

Commit

Permalink
Corrección Roadmap 26 + Nuevo ejercicio 27
Browse files Browse the repository at this point in the history
  • Loading branch information
mouredev committed Jul 1, 2024
1 parent 1cb09d2 commit 0ef8c91
Show file tree
Hide file tree
Showing 3 changed files with 179 additions and 3 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
## Corrección y próximo ejercicio

> #### Lunes 1 de julio de 2024 a las 20:00 (hora España) desde **[Twitch](https://twitch.tv/mouredev)**
> #### Consulta el **[horario](https://discord.gg/CPKcDD9d?event=1252321976027054111)** por país y crea un **[recordatorio](https://discord.gg/CPKcDD9d?event=1252321976027054111)**
> #### Lunes 8 de julio de 2024 a las 20:00 (hora España) desde **[Twitch](https://twitch.tv/mouredev)**
> #### Consulta el **[horario](https://discord.gg/4azkvPUJ?event=1254974320136949871)** por país y crea un **[recordatorio](https://discord.gg/4azkvPUJ?event=1254974320136949871)**
## Roadmap

Expand Down Expand Up @@ -60,7 +60,8 @@
|23|[SINGLETON](./Roadmap/23%20-%20SINGLETON/ejercicio.md)|[📝](./Roadmap/23%20-%20SINGLETON/python/mouredev.py)|[▶️](https://youtu.be/cOIcFo_w9hA)|[👥](./Roadmap/23%20-%20SINGLETON/)
|24|[DECORADORES](./Roadmap/24%20-%20DECORADORES/ejercicio.md)|[📝](./Roadmap/24%20-%20DECORADORES/python/mouredev.py)|[▶️](https://youtu.be/jxJOjg7gPG4)|[👥](./Roadmap/24%20-%20DECORADORES/)
|25|[LOGS](./Roadmap/25%20-%20LOGS/ejercicio.md)|[📝](./Roadmap/25%20-%20LOGS/python/mouredev.py)|[▶️](https://youtu.be/y2O6L1r_skc)|[👥](./Roadmap/25%20-%20LOGS/)
|26|[SOLID: PRINCIPIO DE RESPONSABILIDAD ÚNICA](./Roadmap/26%20-%20SOLID%20SRP/ejercicio.md)|[🗓️ 01/07/24](https://discord.gg/CPKcDD9d?event=1252321976027054111)||[👥](./Roadmap/26%20-%20SOLID%20SRP/)
|26|[SOLID: PRINCIPIO DE RESPONSABILIDAD ÚNICA](./Roadmap/26%20-%20SOLID%20SRP/ejercicio.md)|[📝](./Roadmap/26%20-%20SOLID%20SRP/python/mouredev.py)||[👥](./Roadmap/26%20-%20SOLID%20SRP)
|27|[SOLID: PRINCIPIO ABIERTO-CERRADO](./Roadmap/27%20-%20SOLID%20OCP/ejercicio.md)|[🗓️ 08/07/24](https://discord.gg/4azkvPUJ?event=1254974320136949871)||[👥](./Roadmap/27%20-%20SOLID%20OCP/)

## Instrucciones

Expand Down
147 changes: 147 additions & 0 deletions Roadmap/26 - SOLID SRP/python/mouredev.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
"""
Ejercicio
"""

# Incorrecto


class User:

def __init__(self, name, email) -> None:
self.name = name
self.email = email

def save_to_database(self):
pass

def send_email(self):
pass

# Correcto


class User:

def __init__(self, name, email) -> None:
self.name = name
self.email = email


class UserService:

def save_to_database(self, user):
pass


class EmailService:

def send_email(self, email, message):
pass


"""
Extra
"""

# Incorrecto


class Library:

def __init__(self) -> None:
self.books = []
self.users = []
self.loans = []

def add_book(self, title, author, copies):
self.books.append({"title": title, "author": author, "copies": copies})

def add_user(self, name, id, email):
self.users.append({"name": name, "id": id, "email": email})

def loan_book(self, user_id, book_title):
for book in self.books:
if book["title"] == book_title and book["copies"] > 0:
book["copies"] -= 1
self.loans.append(
{"user_id": user_id, "book_title": book_title})
return True
return False

def return_book(self, user_id, book_title):
for loan in self.loans:
if loan["user_id"] == user_id and loan["book_title"] == book_title:
self.loans.remove(loan)
for book in self.books:
if book["title"] == book_title:
book["copies"] += 1
return True
return False

# Correcto


class Book:

def __init__(self, title, author, copies):
self.title = title
self.author = author
self.copies = copies


class User:

def __init__(self, name, id, email):
self.name = name
self.id = id
self.email = email


class Loan:

def __init__(self):
self.loans = []

def loan_book(self, user, book):
if book.copies > 0:
book.copies -= 1
self.loans.append(
{"user_id": user.id, "book_title": book.title})
return True
return False

def return_book(self, user, book):
for loan in self.loans:
if loan["user_id"] == user.id and loan["book_title"] == book.title:
self.loans.remove(loan)
book.copies += 1
return True
return False


class Library:

def __init__(self) -> None:
self.books = []
self.users = []
self.loans_service = Loan()

def add_book(self, book):
self.books.append(book)

def add_user(self, user):
self.users.append(user)

def loan_book(self, user_id, book_title):
user = next((u for u in self.users if u.id == user_id), None)
book = next((b for b in self.books if b.title == book_title), None)
if user and book:
return self.loans_service.loan_book(user, book)
return False

def return_book(self, user_id, book_title):
user = next((u for u in self.users if u.id == user_id), None)
book = next((b for b in self.books if b.title == book_title), None)
if user and book:
return self.loans_service.return_book(user, book)
return False
28 changes: 28 additions & 0 deletions Roadmap/27 - SOLID OCP/ejercicio.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# #27 SOLID: PRINCIPIO ABIERTO-CERRADO (OCP)
> #### Dificultad: Media | Publicación: 01/07/24 | Corrección: 08/07/24
## Ejercicio

```
/*
* EJERCICIO:
* Explora el "Principio SOLID Abierto-Cerrado (Open-Close Principle, OCP)"
* y crea un ejemplo simple donde se muestre su funcionamiento
* de forma correcta e incorrecta.
*
* DIFICULTAD EXTRA (opcional):
* Desarrolla una calculadora que necesita realizar diversas operaciones matemáticas.
* Requisitos:
* - Debes diseñar un sistema que permita agregar nuevas operaciones utilizando el OCP.
* Instrucciones:
* 1. Implementa las operaciones de suma, resta, multiplicación y división.
* 2. Comprueba que el sistema funciona.
* 3. Agrega una quinta operación para calcular potencias.
* 4. Comprueba que se cumple el OCP.
*/
```
#### Tienes toda la información extendida sobre el roadmap de retos de programación en **[retosdeprogramacion.com/roadmap](https://retosdeprogramacion.com/roadmap)**.

Sigue las **[instrucciones](../../README.md)**, consulta las correcciones y aporta la tuya propia utilizando el lenguaje de programación que quieras.

> Recuerda que cada semana se publica un nuevo ejercicio y se corrige el de la semana anterior en directo desde **[Twitch](https://twitch.tv/mouredev)**. Tienes el horario en la sección "eventos" del servidor de **[Discord](https://discord.gg/mouredev)**.

0 comments on commit 0ef8c91

Please sign in to comment.