@@ -149,4 +149,131 @@ def return_book(self, title: str, name: str):
149
149
150
150
my_library .return_book ("The Hobbit" , "Alice" )
151
151
my_library .print_books ()
152
- my_library .print_users ()
152
+ my_library .print_users ()
153
+
154
+
155
+ class BooksManeger :
156
+ def __init__ (self ):
157
+ self .books = []
158
+
159
+ def add_book (self , title : str , author : str , copies : int ):
160
+ self .books .append (
161
+ {"title" : title , "author" : author , "copies" : copies }
162
+ )
163
+ logging .info (f"Book { title } added successfully" )
164
+
165
+ def print_books (self ):
166
+ for book in self .books :
167
+ print (f"\t Title: { book ['title' ]} , Author: { book ['author' ]} , Copies: { book ['copies' ]} " )
168
+
169
+ def lend_book (self , title : str ):
170
+ for book in self .books :
171
+ if book ["title" ] == title :
172
+ if book ["copies" ] > 0 :
173
+ book ["copies" ] -= 1
174
+ logging .info (f"Book { title } borrowed" )
175
+ else :
176
+ logging .info (f"Book { title } is not available" )
177
+ break
178
+ else :
179
+ logging .warning (f"Book { title } not found" )
180
+
181
+ def return_book (self , title : str ):
182
+ for book in self .books :
183
+ if book ["title" ] == title :
184
+ book ["copies" ] += 1
185
+ logging .info (f"Book { title } returned" )
186
+ break
187
+ else :
188
+ logging .warning (f"Book { title } not found" )
189
+
190
+
191
+ class UsersManager :
192
+ def __init__ (self ):
193
+ self .users = []
194
+
195
+ def add_user (self , name : str , id : int , email : str ):
196
+ self .users .append (
197
+ {"name" : name , "id" : id , "email" : email , "books" : []}
198
+ )
199
+ logging .info (f"User { name } added successfully" )
200
+
201
+ def print_users (self ):
202
+ for user in self .users :
203
+ print (f"\t Name: { user ['name' ]} , ID: { user ['id' ]} , Email: { user ['email' ]} , Books: { user ['books' ]} " )
204
+
205
+ def lend_book (self , title : str , name : str ):
206
+ for user in self .users :
207
+ if user ["name" ] == name :
208
+ user ["books" ].append ({"title" : title })
209
+ logging .info (f"Book { title } added to user { name } " )
210
+ break
211
+ else :
212
+ logging .warning (f"User { name } not found" )
213
+
214
+ def return_book (self , title : str , name : str ):
215
+ for user in self .users :
216
+ if user ["name" ] == name :
217
+ for book in user ["books" ]:
218
+ if book ["title" ] == title :
219
+ user ["books" ].remove (book )
220
+ logging .info (f"Book { title } removed from user { name } " )
221
+ break
222
+ break
223
+ else :
224
+ logging .warning (f"User { name } not found" )
225
+
226
+ class Library_v2 :
227
+ def __init__ (self ):
228
+ self .books_m = BooksManeger ()
229
+ self .users_m = UsersManager ()
230
+
231
+ def add_user (self , name : str , id : int , email : str ):
232
+ self .users_m .add_user (name , id , email )
233
+
234
+ def add_book (self , title : str , author : str , copies : int ):
235
+ self .books_m .add_book (title , author , copies )
236
+
237
+ def print_books (self ):
238
+ self .books_m .print_books ()
239
+
240
+ def print_users (self ):
241
+ self .users_m .print_users ()
242
+
243
+ def lend_book (self , title : str , name : str ):
244
+ self .books_m .lend_book (title )
245
+ self .users_m .lend_book (title , name )
246
+
247
+ def return_book (self , title : str , name : str ):
248
+ self .books_m .return_book (title )
249
+ self .users_m .return_book (title , name )
250
+
251
+ print ("\n " * 3 )
252
+ print ("### Testing: Dificultad extra\n " )
253
+ # Testing the classes
254
+
255
+ my_library_v2 = Library_v2 ()
256
+ my_library_v2 .add_user ("Alice" , 1 , "alice@dev" )
257
+ my_library_v2 .add_user ("Bob" , 2 , "bob@dev" )
258
+
259
+ my_library_v2 .add_book ("The Hobbit" , "J.R.R. Tolkien" , 5 )
260
+ my_library_v2 .add_book ("The Lord of the Rings" , "J.R.R. Tolkien" , 3 )
261
+
262
+ print ("\n Books in the library:" )
263
+ my_library_v2 .print_books ()
264
+
265
+ my_library_v2 .lend_book ("The Hobbit" , "Alice" )
266
+
267
+ print ("\n Books in the library:" )
268
+ my_library_v2 .print_books ()
269
+
270
+ print ("\n Users in the library:" )
271
+ my_library_v2 .print_users ()
272
+
273
+ my_library_v2 .return_book ("The Hobbit" , "Alice" )
274
+
275
+ print ("\n Books in the library:" )
276
+ my_library_v2 .print_books ()
277
+
278
+ print ("\n Users in the library:" )
279
+ my_library_v2 .print_users ()
0 commit comments