What's new

Help Test bot

Sendlove

𝒜 𝓢𝓱𝓲𝓷𝓲𝓷𝓰 𝓑𝓮𝓪𝓬𝓸𝓷, 𝓔𝓶𝓹𝓪𝓽𝓱
Elite
define a class named book with properties (title, author, and genre_), implement a method borrow () that marks the book as borrowed (change the attribute) and a method return _() that marks it as returned. create two books borrowing and returning one of them
 
Sure! Here's an example of the implementation of the Book class in Python:

Python:
class Book:
    def __init__(self, title, author, genre):
        self.title = title
        self.author = author
        self.genre = genre
        self.borrowed = False

    def borrow(self):
        if self.borrowed:
            print("This book is already borrowed.")
        else:
            self.borrowed = True
            print(f"The book '{self.title}' has been marked as borrowed.")

    def return_book(self):
        if not self.borrowed:
            print("This book is not currently borrowed.")
        else:
            self.borrowed = False
            print(f"The book '{self.title}' has been marked as returned.")


# Creating two book objects
book1 = Book("The Great Gatsby", "F. Scott Fitzgerald", "Fiction")
book2 = Book("To Kill a Mockingbird", "Harper Lee", "Classic")

# Borrowing and returning books
book1.borrow()
book1.return_book()

book2.borrow()

In this example, the Book class has a constructor (__init__) that accepts title, author, and genre as parameters and initializes the borrowed attribute as False.

The borrow() method checks if the book is already borrowed or not. If it's already borrowed, it prints a message stating that the book is already borrowed. Otherwise, it marks the book as borrowed by setting the borrowed attribute to True and displays a message confirming the action.

The return_book() method checks if the book is currently borrowed or not. If it's not borrowed, it prints a message stating that the book is not currently borrowed. Otherwise, it marks the book as returned by setting the borrowed attribute to False and displays a message confirming the action.

Finally, we create two book objects (book1 and book2) and demonstrate how to borrow and return books using the borrow() and return_book() methods.
 

Similar threads

Back
Top