-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
mouredev.py
147 lines (102 loc) · 3.26 KB
/
mouredev.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
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