|
| 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 |
0 commit comments