Book Details

Book Details

Create a class Book with the following private member variables

  • book_name
  • book_price 
  • author_name

Include appropriate getter and setter methods. The getter method format should be like:

get_book_name(), get_book_price() and get_author_name() for the private instance variables;book_name,book_price and author_name respectively.

The setter method for book_price should be: set_book_price(self,new_price)

Get the book details as shown in the sample input and store these values into the variables; book_name, book_price and author_name respectively. 

Create an object ‘book_obj’ for the book class using constructor by passing book name, price and author name.  Then get the new price from the user and store it into a variable with the name ‘new_price’.  Change the book price by calling the appropriate setter method and print the output as shown in the sample output using the getter methods. 

Note:

  • The book price should be a float value.  So, necessary conversion is required.
  • Use the same attribute names, class name, object name and method names as given in the question description.

Sample input and output:

Enter the book name: Python Programming

Enter the price: 500

Enter the author name: Einstein

Book details before price renewal:

Book Name: Python Programming

Book Price: 500.0

Author Name: Einstein 

Enter the new price: 1000 

Book details after price renewal:

Book Name: Python Programming

Book Price: 1000.0

Author Name: Einstein

Code :-

class Book:
    def __init__(self,book_name,book_price,author_name):
        self.__book_name=book_name
        self.__book_price=book_price
        self.__author_name=author_name
    def get_book_name(self):
        return self.__book_name
    def get_book_price(self):
        return self.__book_price
    def get_author_name(self):
        return self.__author_name
    def set_book_price(self,new_price):
        self.__book_price=new_price

book_name=input("Enter the book name: ")
book_price=float(input("Enter the price: "))
author_name=input("Enter the author name: ")

book_obj=Book(book_name,book_price,author_name)

print("\nBook details before price renewal:"+"\nBook Name: "+book_obj.get_book_name()+"\nBook Price: "+str(book_obj.get_book_price())+"\nAuthor Name: "+book_obj.get_author_name())

new_price=float(input("\nEnter the new price: "))

book_obj.set_book_price(new_price)

print("\nBook details after price renewal:")
print("Book Name: "+book_obj.get_book_name()+"\n"+"Book Price: "+str(book_obj.get_book_price())+"\n"+"Author Name: "+book_obj.get_author_name())

Leave a Comment

Your email address will not be published. Required fields are marked *