Untitled
unknown
plain_text
a year ago
3.5 kB
11
Indexable
class Book:
def __init__(self, title, author, description, series=None):
self.title = title
self.author = author
self.description = description
self.series = series # Reference to a Series object if the book is part of a series
def __str__(self):
return "Book"
class PhysicalBook(Book):
def __init__(self, title, author, description, isbn, series=None):
super().__init__(title, author, description, series)
self.isbn = isbn # ISBN is specific to physical books
def __str__(self):
return "PBook"
class Ebook(Book):
def __init__(self, title, author, description, series=None):
super().__init__(title, author, description, series)
def __str__(self):
return "EBook"
class Series:
def __init__(self, title, author, description):
self.title = title
self.author = author
self.description = description
self.books = [] # List to store the books in the series
def add_book(self, book):
self.books.append(book)
def __str__(self):
print("Series")
class BookOrganizer:
def __init__(self):
self.books = [] # List of all books (both physical and ebooks)
self.series_list = [] # List of all seriesuh
def add_book(self, book):
self.books.append(book)
def add_series(self, series):
self.series_list.append(series)
def search_by_title(self, title):
return [book for book in self.books if book.title.lower() == title.lower()]
def search_by_author(self, author):
return [book for book in self.books if book.author.lower() == author.lower()]
def search_by_isbn(self, isbn):
return [book for book in self.books if isinstance(book, PhysicalBook) and book.isbn == isbn]
def delete_by_isbn(self, isbn):
self._deleteList(self.books, isbn)
for series in self.series_list:
self._deleteList(series.books, isbn)
def _deleteList(self, books, isbn):
idxToRemove = []
for idx in range(len(books)):
book = books[idx]
if isinstance(book, PhysicalBook) and book.isbn == isbn:
idxToRemove.append(idx)
for idx in idxToRemove:
del inputList[idx]
def get_num_ebooks(self):
return len([book for book in self.books if isinstance(book, Ebook)])
def list_books_in_series(self, series_title):
for series in self.series_list:
if series.title.lower() == series_title.lower():
return series.books
return None
# Create Series
harry_potter_series = Series("Harry Potter", "J.K. Rowling", "A young wizard's journey")
# Create Books
book1 = PhysicalBook("Harry Potter and the Philosopher's Stone", "J.K. Rowling", "First book of Harry Potter", "978-3-16-148410-0", harry_potter_series)
book2 = Ebook("Harry Potter and the Chamber of Secrets", "J.K. Rowling", "Second book of Harry Potter", harry_potter_series)
# Add books to series
harry_potter_series.add_book(book1)
harry_potter_series.add_book(book2)
# Add to Catalog
catalog = BookOrganizer()
catalog.add_book(book1)
catalog.add_book(book2)
catalog.add_series(harry_potter_series)
# Search in catalog
print(catalog.search_by_title("Harry Potter and the Philosopher's Stone"))
print(catalog.list_books_in_series("Harry Potter"))
for book in catalog.search_by_title("Harry Potter and the Philosopher's Stone"):
print(book)
Editor is loading...
Leave a Comment