Question: Its in python Starting with your solution to the previous task, extend the MyLibrary class by implementing the following methods: The find_available_book (self, code) method
Its in python



Starting with your solution to the previous task, extend the MyLibrary class by implementing the following methods: The find_available_book (self, code) method which takes a book code as a parameter and searches through the collection of books based on a given code. If the book is found and it is available, the method should return the Book object, and none otherwise. The borrow_book(self, code, borrower_name) method which takes a book code and a name of the borrower as parameters. The method should search for the book in the books list. If the book is available, the method should issue the book to the borrower by calling the borrow_book () method on the Book and print a message as in the example below. If the book is NOT available, the method should print "ERROR: could not issue the book: 'XXX'" where XXX indicates the book code. Note: You can assume that the Book class is given. Keep a copy of your solution to this task because you will be extending it step by step in subsequent tasks. For example: Test Result = library MyLibrary( 'MyLibrary', 'simple_books.txt') print(library.find_available_book( 'Q$12.02.003')) print(library.find_available_book ('003')) 5 books loaded. The Dune Chronicles, QS12.02.003 (Available) None library MyLibrary( 'MyLibrary', 'simple_books.txt') library.borrow_book ( 'QS12.02.003', 'Michael') library.borrow_book ('003', 'Michael') 5 books loaded. 'The Dune Chronicles' is issued to Michael success ERROR: could not issue the book: '003'. 1. class Book: 2, def __init__(self, code , title, status True): 3 self.__code code 4 self.__title title 5 self.__status status 6 def get_book_title(self): 7 return self.__title 8 def get_book_code(self): 9 return self.__code 10 def is_available(self): 11, if self.__status != False: 12 return True 13 else: 14 return false def borrow_book(self): 16 self.__status False 17 def return_book(self): 18 self.__status True def __str__(self): 20 if self.__status False: 21 return "{}, {} (On Loan)". format(self.__title, self.__code) 22 else: 23 return "{}, {} (Available)". format(self.__title, self.__code) OOONO WNPO OVOU WNA 15 Precheck Check name 1 yclass MyLibrary: 2 def __init__(self, name filename "'): 3 self.__name = 4 self.__books_list [] 5 if not(name or filename): 6 return 7 try: 8 f open(filename, 'r') 9 except FileNotFoundError: 10 print("ERROR: The file '{}' does not exist.".format(filename)) 11 else: 12 for line in f.read().split(' '): 13 code, title line.split(',') 14 self.__books_list.append(Book(code, title)) 15 f.close() print("{} books loaded.".format(len(self.__books_list))) 17 def show_all_books(self): 18 for book in self.__books_list: 19 print(book) 20 21 22 Starting with your solution to the previous task, extend the MyLibrary class by implementing the following methods: The find_on_loan_book (self, code) method which takes a book code as a parameter and and searches through the collection of books based on the parameter code. If the book is found and it is on loan, the method should return the Book object, and none otherwise. The return_book(self, code) method which takes a book code as a parameter and searches through the collection of books based on the parameter code. If the book is on loan, the method should return the book and print a message as in the example below. If the book is not "on loan" or cannot be found, the method should print "ERROR: could not return the book: XXX" where XXX indicates the book code. Note: You can assume that the Book class is given. Keep a copy of your solution to this task because you will be extending it step by step in subsequent tasks. For example: Test Result = library MyLibrary( 'MyLibrary', 'simple_books.txt') 5 books loaded. library.borrow_book ('QS12.02.003', 'Michael') 'The Dune Chronicles' is issued to Michael success library.return_book ('QS12.02.003') 'QS12.02.003' is returned successfully. print(library.find_available_book( 'QS12.02.003')) The Dune Chronicles, QS12.02.003 Available) = library MyLibrary( 'MyLibrary', 'simple_books.txt') library.return_book ('QS12.02.003') library.return_book ('003') 5 books loaded. ERROR: could not return the book: 'QS12.02.003'. ERROR: could not return the book: '003
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
