from abc import ABC, abstractmethod
class LibraryItem(ABC): def init(self, title, author, publication_date, available=True): self._title = title self._author = author self._publication_date = publication_date self._available = available
@property
def title(self):
return self._title
@property
def author(self):
return self._author
@property
def publication_date(self):
return self._publication_date
@property
def available(self):
return self._available
@abstractmethod
def checkout(self):
pass
@abstractmethod
def return_item(self):
pass
class Book(LibraryItem): def init(self, title, author, publication_date, genre, available=True): super().init(title, author, publication_date, available) self._genre = genre
@property
def genre(self):
return self._genre
def checkout(self):
if self._available:
self._available = False
print(f"The book '{self.title}' has been checked out.")
else:
print(f"The book '{self.title}' is not available.")
def return_item(self):
if not self._available:
self._available = True
print(f"The book '{self.title}' has been returned.")
else:
print(f"The book '{self.title}' was not checked out.")
class DVD(LibraryItem): def init(self, title, author, publication_date, runtime, available=True): super().init(title, author, publication_date, available) self._runtime = runtime
@property
def runtime(self):
return self._runtime
def checkout(self):
if self._available:
self._available = False
print(f"The DVD '{self.title}' has been checked out.")
else:
print(f"The DVD '{self.title}' is not available.")
def return_item(self):
if not self._available:
self._available = True
print(f"The DVD '{self.title}' has been returned.")
else:
print(f"The DVD '{self.title}' was not checked out.")
class LibraryCatalog: def init(self): self._items = []
def add_item(self, item):
self._items.append(item)
def find_item(self, search_term):
results = [
item for item in self._items
if search_term.lower() in item.title.lower() or search_term.lower() in item.author.lower()
]
if results:
for item in results:
status = "Available" if item.available else "Checked out"
print(f"Title: {item.title}, Author: {item.author}, Status: {status}")
else:
print("No items found.")
def display_catalog(self):
for item in self._items:
status = "Available" if item.available else "Checked out"
print(f"Title: {item.title}, Author: {item.author}, Status: {status}")
catalog = LibraryCatalog()
book1 = Book("The Great Gatsby", "F. Scott Fitzgerald", "1925", "Fiction") book2 = Book("1984", "George Orwell", "1949", "Dystopian") dvd1 = DVD("Inception", "Christopher Nolan", "2010", "148 minutes")
catalog.add_item(book1) catalog.add_item(book2) catalog.add_item(dvd1)
catalog.display_catalog() print("\nSearching for '1984':") catalog.find_item("1984") print("\nChecking out '1984':") book2.checkout() print("\nReturning '1984':") book2.return_item() print("\nCatalog after updates:") catalog.display_catalog()