Advertisement
Radoslav_03

2.3_zad.py

Sep 17th, 2023 (edited)
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.95 KB | None | 0 0
  1. class Book:
  2.     def __init__(self, book_id, title, author, genre, year, copies):
  3.         self.book_id = book_id
  4.         self.title = title
  5.         self.author = author
  6.         self.genre = genre
  7.         self.year = year
  8.         self.copies = copies
  9.  
  10.     def display_info(self):
  11.         print(f"Book ID: {self.book_id}")
  12.         print(f"Title: {self.title}")
  13.         print(f"Author: {self.author}")
  14.         print(f"Genre: {self.genre}")
  15.         print(f"Year: {self.year}")
  16.  
  17.  
  18. def add_books(books_list, book):
  19.     books_list.append(book)
  20.  
  21. def sort_by_oldest_year(books_list):
  22.     sorted_books = sorted(books_list, key=lambda book: book.year, reverse = True)
  23.     for book in sorted_books:
  24.         book.display_info()
  25.  
  26. def serach_by_author_title(books_list, author, title):
  27.     found = False
  28.  
  29.     for book in books_list:
  30.         if book.author == author and book.title == title:
  31.             found = True
  32.             book.display_info()
  33.             break
  34.  
  35.         if not found:
  36.             print("Book not found!!!")
  37.  
  38. def filter_by_genre(books_list, genre):
  39.     for book in books_list:
  40.         if book.genre == genre:
  41.             book.display_info()
  42.  
  43. def check_copies(books_list, author, title):
  44.     for book in books_list:
  45.         if book.author == author and book.title == title:
  46.             if book.copies > 0:
  47.                 print(f"{book.copies} left.")
  48.             else:
  49.                 print("No copies left.")
  50.  
  51. books_list = []
  52.  
  53. books = int(input())
  54.  
  55. for _ in range(books):
  56.     book_id = int(input())
  57.     title = input()
  58.     author = input()
  59.     genre = input()
  60.     year = int(input())
  61.     copies = int(input())
  62.  
  63. book = Book(book_id, title, author, genre, year, copies)
  64. books_list.append(book)
  65.  
  66. while True:
  67.    
  68.     print("1. Добавяне на книга")
  69.     print("2. Сортиране по година")
  70.     print("3. Търсене на книга по заглавие и автор")
  71.     print("4. Филтриране по жанр")
  72.     print("5. Проверка на копия")
  73.     print("6. Изход")
  74.  
  75.     choice = int(input())
  76.  
  77.     match choice:
  78.         case 1:
  79.             book_id = int(input())
  80.             title = input()
  81.             author = input()
  82.             genre = input()
  83.             year = int(input())
  84.             copies = int(input())
  85.  
  86.             book_to_add = Book(book_id, title, author, genre, year, copies)
  87.             books_list.append(book_to_add)
  88.             print(books_list)
  89.  
  90.         case 2:
  91.             sort_by_oldest_year(books_list)
  92.         case 3:
  93.             author = input()
  94.             title = input()
  95.             serach_by_author_title(books_list, author, title)
  96.         case 4:
  97.             genre = input()
  98.             filter_by_genre(books_list, genre)
  99.         case 5:
  100.             author = input()
  101.             title = input()
  102.             check_copies(books_list, author, title)
  103.         case 6:
  104.             break
  105.         case _:
  106.             print("Wrong option!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement