Skip to content

Commit 0ef8c91

Browse files
committed
Corrección Roadmap 26 + Nuevo ejercicio 27
1 parent 1cb09d2 commit 0ef8c91

File tree

3 files changed

+179
-3
lines changed

3 files changed

+179
-3
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
2828
## Corrección y próximo ejercicio
2929

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

@@ -60,7 +60,8 @@
6060
|23|[SINGLETON](./Roadmap/23%20-%20SINGLETON/ejercicio.md)|[📝](./Roadmap/23%20-%20SINGLETON/python/mouredev.py)|[▶️](https://youtu.be/cOIcFo_w9hA)|[👥](./Roadmap/23%20-%20SINGLETON/)
6161
|24|[DECORADORES](./Roadmap/24%20-%20DECORADORES/ejercicio.md)|[📝](./Roadmap/24%20-%20DECORADORES/python/mouredev.py)|[▶️](https://youtu.be/jxJOjg7gPG4)|[👥](./Roadmap/24%20-%20DECORADORES/)
6262
|25|[LOGS](./Roadmap/25%20-%20LOGS/ejercicio.md)|[📝](./Roadmap/25%20-%20LOGS/python/mouredev.py)|[▶️](https://youtu.be/y2O6L1r_skc)|[👥](./Roadmap/25%20-%20LOGS/)
63-
|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/)
63+
|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)
64+
|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/)
6465

6566
## Instrucciones
6667

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
"""
2+
Ejercicio
3+
"""
4+
5+
# Incorrecto
6+
7+
8+
class User:
9+
10+
def __init__(self, name, email) -> None:
11+
self.name = name
12+
self.email = email
13+
14+
def save_to_database(self):
15+
pass
16+
17+
def send_email(self):
18+
pass
19+
20+
# Correcto
21+
22+
23+
class User:
24+
25+
def __init__(self, name, email) -> None:
26+
self.name = name
27+
self.email = email
28+
29+
30+
class UserService:
31+
32+
def save_to_database(self, user):
33+
pass
34+
35+
36+
class EmailService:
37+
38+
def send_email(self, email, message):
39+
pass
40+
41+
42+
"""
43+
Extra
44+
"""
45+
46+
# Incorrecto
47+
48+
49+
class Library:
50+
51+
def __init__(self) -> None:
52+
self.books = []
53+
self.users = []
54+
self.loans = []
55+
56+
def add_book(self, title, author, copies):
57+
self.books.append({"title": title, "author": author, "copies": copies})
58+
59+
def add_user(self, name, id, email):
60+
self.users.append({"name": name, "id": id, "email": email})
61+
62+
def loan_book(self, user_id, book_title):
63+
for book in self.books:
64+
if book["title"] == book_title and book["copies"] > 0:
65+
book["copies"] -= 1
66+
self.loans.append(
67+
{"user_id": user_id, "book_title": book_title})
68+
return True
69+
return False
70+
71+
def return_book(self, user_id, book_title):
72+
for loan in self.loans:
73+
if loan["user_id"] == user_id and loan["book_title"] == book_title:
74+
self.loans.remove(loan)
75+
for book in self.books:
76+
if book["title"] == book_title:
77+
book["copies"] += 1
78+
return True
79+
return False
80+
81+
# Correcto
82+
83+
84+
class Book:
85+
86+
def __init__(self, title, author, copies):
87+
self.title = title
88+
self.author = author
89+
self.copies = copies
90+
91+
92+
class User:
93+
94+
def __init__(self, name, id, email):
95+
self.name = name
96+
self.id = id
97+
self.email = email
98+
99+
100+
class Loan:
101+
102+
def __init__(self):
103+
self.loans = []
104+
105+
def loan_book(self, user, book):
106+
if book.copies > 0:
107+
book.copies -= 1
108+
self.loans.append(
109+
{"user_id": user.id, "book_title": book.title})
110+
return True
111+
return False
112+
113+
def return_book(self, user, book):
114+
for loan in self.loans:
115+
if loan["user_id"] == user.id and loan["book_title"] == book.title:
116+
self.loans.remove(loan)
117+
book.copies += 1
118+
return True
119+
return False
120+
121+
122+
class Library:
123+
124+
def __init__(self) -> None:
125+
self.books = []
126+
self.users = []
127+
self.loans_service = Loan()
128+
129+
def add_book(self, book):
130+
self.books.append(book)
131+
132+
def add_user(self, user):
133+
self.users.append(user)
134+
135+
def loan_book(self, user_id, book_title):
136+
user = next((u for u in self.users if u.id == user_id), None)
137+
book = next((b for b in self.books if b.title == book_title), None)
138+
if user and book:
139+
return self.loans_service.loan_book(user, book)
140+
return False
141+
142+
def return_book(self, user_id, book_title):
143+
user = next((u for u in self.users if u.id == user_id), None)
144+
book = next((b for b in self.books if b.title == book_title), None)
145+
if user and book:
146+
return self.loans_service.return_book(user, book)
147+
return False
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# #27 SOLID: PRINCIPIO ABIERTO-CERRADO (OCP)
2+
> #### Dificultad: Media | Publicación: 01/07/24 | Corrección: 08/07/24
3+
4+
## Ejercicio
5+
6+
```
7+
/*
8+
* EJERCICIO:
9+
* Explora el "Principio SOLID Abierto-Cerrado (Open-Close Principle, OCP)"
10+
* y crea un ejemplo simple donde se muestre su funcionamiento
11+
* de forma correcta e incorrecta.
12+
*
13+
* DIFICULTAD EXTRA (opcional):
14+
* Desarrolla una calculadora que necesita realizar diversas operaciones matemáticas.
15+
* Requisitos:
16+
* - Debes diseñar un sistema que permita agregar nuevas operaciones utilizando el OCP.
17+
* Instrucciones:
18+
* 1. Implementa las operaciones de suma, resta, multiplicación y división.
19+
* 2. Comprueba que el sistema funciona.
20+
* 3. Agrega una quinta operación para calcular potencias.
21+
* 4. Comprueba que se cumple el OCP.
22+
*/
23+
```
24+
#### Tienes toda la información extendida sobre el roadmap de retos de programación en **[retosdeprogramacion.com/roadmap](https://retosdeprogramacion.com/roadmap)**.
25+
26+
Sigue las **[instrucciones](../../README.md)**, consulta las correcciones y aporta la tuya propia utilizando el lenguaje de programación que quieras.
27+
28+
> 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 commit comments

Comments
 (0)