-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Corrección Roadmap 26 + Nuevo ejercicio 27
- Loading branch information
Showing
3 changed files
with
179 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)**. |