|
| 1 | + |
| 2 | + |
| 3 | +''' |
| 4 | +/* |
| 5 | + * EJERCICIO: |
| 6 | + * Explora el "Principio SOLID de Responsabilidad Única (Single Responsibility |
| 7 | + * Principle, SRP)" y crea un ejemplo simple donde se muestre su funcionamiento |
| 8 | + * de forma correcta e incorrecta. |
| 9 | +
|
| 10 | +
|
| 11 | +''' |
| 12 | +''' |
| 13 | + * DIFICULTAD EXTRA (opcional): |
| 14 | + * Desarrolla un sistema de gestión para una biblioteca. El sistema necesita |
| 15 | + * manejar diferentes aspectos como el registro de libros, la gestión de usuarios |
| 16 | + * y el procesamiento de préstamos de libros. |
| 17 | + * Requisitos: |
| 18 | + * 1. Registrar libros: El sistema debe permitir agregar nuevos libros con |
| 19 | + * información básica como título, autor y número de copias disponibles. |
| 20 | + * 2. Registrar usuarios: El sistema debe permitir agregar nuevos usuarios con |
| 21 | + * información básica como nombre, número de identificación y correo electrónico. |
| 22 | + * 3. Procesar préstamos de libros: El sistema debe permitir a los usuarios |
| 23 | + * tomar prestados y devolver libros. |
| 24 | + * Instrucciones: |
| 25 | + * 1. Diseña una clase que no cumple el SRP: Crea una clase Library que maneje |
| 26 | + * los tres aspectos mencionados anteriormente (registro de libros, registro de |
| 27 | + * usuarios y procesamiento de préstamos). |
| 28 | + * 2. Refactoriza el código: Separa las responsabilidades en diferentes clases |
| 29 | + * siguiendo el Principio de Responsabilidad Única. |
| 30 | +
|
| 31 | +''' |
| 32 | + |
| 33 | +# Incorrecto |
| 34 | + |
| 35 | +class Library: |
| 36 | + |
| 37 | + books = [] |
| 38 | + users = [] |
| 39 | + loans = [] |
| 40 | + |
| 41 | + def new_book(self, author, title, n_copies): |
| 42 | + self.books.append({"author": author, |
| 43 | + "title": title, |
| 44 | + "n_copies": n_copies}) |
| 45 | + |
| 46 | + def new_user(self, name, id, email): |
| 47 | + self.users.append({"name": name, |
| 48 | + "id": id, |
| 49 | + "email": email}) |
| 50 | + |
| 51 | + def loan_book(self, user_id, book_title): |
| 52 | + for book in self.books: |
| 53 | + if book["title"] == book_title and book["n_copies"] > 0: |
| 54 | + self.loans.append({"user_id": user_id, |
| 55 | + "title_book": book_title}) |
| 56 | + book["n_copies"] -=1 |
| 57 | + |
| 58 | + |
| 59 | + def return_book(self, user_id, book_title): |
| 60 | + for loan in self.loans: |
| 61 | + if loan["user_id"] == user_id and loan["title_book"] == book_title: |
| 62 | + self.loans.remove(loan) |
| 63 | + for books in self.books: |
| 64 | + if books["title"] == book_title: |
| 65 | + books["n_copies"] += 1 |
| 66 | + |
| 67 | + |
| 68 | + |
| 69 | + |
| 70 | +my_library = Library() |
| 71 | +my_library.new_book("Cervantes", "El quijote", 10) |
| 72 | +my_library.new_book("Rowling", "Harry Potter", 10) |
| 73 | +my_library.new_book("Tolkien", "El señor de los anillos", 20) |
| 74 | + |
| 75 | +my_library. new_user( "Brais", 105, "[email protected]") |
| 76 | +my_library. new_user( "Borja", 120, "[email protected]") |
| 77 | + |
| 78 | +print(my_library.books) |
| 79 | +print(my_library.users) |
| 80 | + |
| 81 | +my_library.loan_book(120, "Harry Potter") |
| 82 | +print(my_library.loans) |
| 83 | +print(my_library.books) |
| 84 | + |
| 85 | +my_library.return_book(120, "Harry Potter") |
| 86 | +print(my_library.books) |
| 87 | + |
| 88 | + |
| 89 | + |
| 90 | + |
| 91 | +# Correcto |
| 92 | +class Book: |
| 93 | + def __init__(self, title, author, n_copies): |
| 94 | + self.title = title |
| 95 | + self.author = author |
| 96 | + self.n_copies = n_copies |
| 97 | + |
| 98 | + |
| 99 | +class User: |
| 100 | + def __init__(self, name, id, email): |
| 101 | + self.name = name |
| 102 | + self.id = id |
| 103 | + self.email = email |
| 104 | + |
| 105 | +class BookLoanProcesing: |
| 106 | + |
| 107 | + def __init__(self): |
| 108 | + self.loans = [] |
| 109 | + |
| 110 | + def book_borrowing(self, book, user): |
| 111 | + if book.n_copies > 0: |
| 112 | + book.n_copies -= 1 |
| 113 | + self.loans.append({"user_id": user.id, |
| 114 | + "title_book": book.title }) |
| 115 | + else: |
| 116 | + print("No hay libros suficientes") |
| 117 | + |
| 118 | + def book_returning(self, user, book): |
| 119 | + for loan in self.loans: |
| 120 | + if loan["user_id"] == user.id and loan["title_book"] == book.title: |
| 121 | + book.n_copies += 1 |
| 122 | + self.loans.remove(loan) |
| 123 | + |
| 124 | + |
| 125 | +class Library(): |
| 126 | + def __init__(self) -> None: |
| 127 | + self.books = [] |
| 128 | + self.users = [] |
| 129 | + self.loans = BookLoanProcesing() |
| 130 | + |
| 131 | + def new_book(self, book): |
| 132 | + self.books.append(book) |
| 133 | + |
| 134 | + def new_user(self, user): |
| 135 | + self.users.append(user) |
| 136 | + |
| 137 | + def new_loan_book(self, user_id, title_book): |
| 138 | + user = next((user for user in self.users if user.id == user_id), None) |
| 139 | + book = next((book for book in self.books if book.title == title_book), None) |
| 140 | + if user and book: |
| 141 | + return self.loans.book_borrowing(user, book) |
| 142 | + |
| 143 | + def return_book(self, user_id, title_book): |
| 144 | + user = next((user for user in self.user if user_id == user.id),None) |
| 145 | + book = next((book for book in self.books if book.title == title_book), None) |
| 146 | + if user and book: |
| 147 | + return self.loans.book_returning(user, book) |
| 148 | + |
| 149 | + |
| 150 | + |
0 commit comments