Last active
December 5, 2025 14:30
-
-
Save florestankorp/bd8bb31eda05845be19bcd56672f07d5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Book: | |
| """A book of a library""" | |
| def __init__(self, title, author, borrowed=False): | |
| self.title = title | |
| self.author = author | |
| self.is_borrowed = borrowed | |
| def __str__(self): | |
| return f"Title: {self.title}, Author: {self.author}, Borrowed: {'Yes' if self.is_borrowed else 'No'}" | |
| def __repr__(self): | |
| return f"'{self.title}'" | |
| class Library: | |
| """Library as list of books""" | |
| def __int__(self): | |
| self.books = [] | |
| def add_book(self, book): | |
| self.books.append(book) | |
| def search_by_title(self, title): | |
| found_books = [] | |
| for book in self.books: | |
| if book.title == title: | |
| found_books.append(book) | |
| return found_books | |
| def search_by_author(self, author): | |
| found_author = [] | |
| for book in self.books: | |
| if book.author == author: | |
| found_author.append(book) | |
| return found_author | |
| def list_available_books(self): | |
| available_books = [] | |
| for book in self.books: | |
| if not book.is_borrowed: | |
| available_books.append(book) | |
| return available_books | |
| def borrow_book(self, title): | |
| for book in self.books: | |
| if book.title == title: | |
| if not book.is_borrowed: | |
| return book | |
| else: | |
| print(f"Error: This book has been borrowed") | |
| return None | |
| print(f"Error: No book with this title exists") | |
| # return None | |
| def return_book(self, book): | |
| if book.is_borrowed: | |
| book.is_borrowed = False | |
| else: | |
| print("Error: This book was not borrowed.") | |
| def remove_book(self, title): | |
| for book in self.books: | |
| if book.title == title: | |
| self.books.remove(book) | |
| # def sort_books_by_title(self): | |
| # self.books.sort(key = self.books[] book: book.title) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
this still needs to be implemented