Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from abc import ABC, abstractmethod
- from typing import List, Dict, Tuple, Set
- class LibraryItem(ABC):
- def __init__(self, title: str, id: int):
- self._title = title
- self._id = id
- @abstractmethod
- def get_details(self) -> str:
- pass
- @abstractmethod
- def borrow(self) -> None:
- pass
- @abstractmethod
- def return_item(self) -> None:
- pass
- class Book(LibraryItem):
- def __init__(self, title: str, id: int, author: str):
- super().__init__(title, id)
- self._author = author
- self._is_borrowed = False
- def get_details(self) -> str:
- return f"Book Title: {self._title}, ID: {self._id}, Author: {self._author}"
- def borrow(self) -> None:
- if not self._is_borrowed:
- self._is_borrowed = True
- else:
- print("This book is already borrowed.")
- def return_item(self) -> None:
- if self._is_borrowed:
- self._is_borrowed = False
- else:
- print("This book was not borrowed.")
- class Magazine(LibraryItem):
- def __init__(self, title: str, id: int, issue_number: int):
- super().__init__(title, id)
- self._issue_number = issue_number
- self._is_borrowed = False
- def get_details(self) -> str:
- return f"Magazine Title: {self._title}, ID: {self._id}, Issue Number: {self._issue_number}"
- def borrow(self) -> None:
- if not self._is_borrowed:
- self._is_borrowed = True
- else:
- print("This magazine is already borrowed.")
- def return_item(self) -> None:
- if self._is_borrowed:
- self._is_borrowed = False
- else:
- print("This magazine was not borrowed.")
- def display_item_details(items: List[LibraryItem]) -> None:
- for item in items:
- print(item.get_details())
- class Library:
- def __init__(self):
- self.items: List[LibraryItem] = []
- self.borrowed_items: Dict[int, LibraryItem] = {}
- self.available_ids: Set[int] = set()
- def add_item(self, item: LibraryItem) -> None:
- self.items.append(item)
- self.available_ids.add(item._id)
- def borrow_item(self, item_id: int) -> None:
- for item in self.items:
- if item._id == item_id:
- item.borrow()
- self.borrowed_items[item_id] = item
- self.available_ids.discard(item_id)
- return
- print("Item not found.")
- def return_item(self, item_id: int) -> None:
- if item_id in self.borrowed_items:
- item = self.borrowed_items[item_id]
- item.return_item()
- del self.borrowed_items[item_id]
- self.available_ids.add(item_id)
- else:
- print("Item was not borrowed.")
- def display_all_items(self) -> None:
- display_item_details(self.items)
- def main():
- library = Library()
- book1 = Book("1984", 1, "George Orwell")
- magazine1 = Magazine("National Geographic", 2, 2023)
- library.add_item(book1)
- library.add_item(magazine1)
- print("All Library Items:")
- library.display_all_items()
- print("\nBorrowing Item ID 1 (1984):")
- library.borrow_item(1)
- print("\nBorrowing Item ID 2 (National Geographic):")
- library.borrow_item(2)
- print("\nAll Library Items After Borrowing:")
- library.display_all_items()
- print("\nReturning Item ID 1 (1984):")
- library.return_item(1)
- print("\nFinal Library Items:")
- library.display_all_items()
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement