Answer these class Book: def init(self, title:str, author:str, isbn:str) -> None:…
- User
Answer these
class Book: def init(self, title:str, author:str, isbn:str) -> None: self.title = title self.author = author self.isbn = isbn self.checked_out = False
def __str__(self) -> str: status = "Unavailable" if self.checked_out else "Available" return f"{self.title} by {self.author} (ISBN: {self.isbn}) - {status}"class Library: def init(self) -> None: self.books = []
def add_book(self, book:Book) -> str: self.books.append(book) return f"Book '{book.title}' added to the library." def remove_book(self, isbn:str) -> str: for book in self.books: if book.isbn == isbn: self.books.remove(book) return f"Book '{book.title}' removed from the library." return "Book not found." def check_out_book(self, isbn:str) -> str: for book in self.books: if book.isbn == isbn and not book.checked_out: book.checked_out = True return f"You have checked out '{book.title}'." elif book.isbn == isbn: return f"'{book.title}' is already checked out." return "Book not found." def return_book(self, isbn:str) -> str: for book in self.books: if book.isbn == isbn and book.checked_out: book.checked_out = False return f"You have returned '{book.title}'." return "Book not found or not checked out." def search_books(self, query:str) -> list[Book]: results = [book for book in self.books if query.lower() in book.title.lower() or query.lower() in book.author.lower()] return results if results else "No books found." def list_books(self) -> list[str]: return [str(book) for book in self.books] if self.books else "No books available."Example usage:
library = Library() library.add_book(Book("1984", "George Orwell", "1234567890")) library.add_book(Book("Brave New World", "Aldous Huxley", "0987654321")) print(library.list_books()) print(library.check_out_book("1234567890")) print(library.list_books()) print(library.return_book("1234567890")) What is the above code written to do? Search an external database for a books written by a given author Manage a list of books and allow a user to search this list by title Update an online API with new books Check if a Python Library is installed How is the above code an example of the Single Responsibility Principle? The Book class handles every interaction and the Library class is in charge of the list The Book class is in charge of only information relating to the status of a single book The Library class has many methods
- Assistant
The provided code implements a simple library management system in Python, which facilitates the adding, removing, checking out, returning, and searching for books. Here's a breakdown of the functionalities:
-
Class Definitions:
- Book Class: Represents a single book with attributes for title, author, ISBN, and availability status (checked out or not).
- Library Class: Manages a collection of objects, offering methods to add, remove, check out, return, and search for books.
-