[Solved] default and parameterized constructor along with setter and getters with Python

Create a class Stall, with following private attributes,

Attribute Data Type
__namestr
__detailstr

Use the __init__() method for initializing the Attributes. Use the following getter and setter methods in Stall class.

Getter and Setter method names:
set_name()
get_name()
set_detail()
get_detail()

Include default and parameterized constructors.

If the user choice is ‘1’, then use a parameterized constructor and print the details by overriding the __str__() method.

If the user choice is ‘2’, then use setters and getters for initializing and accessing the attributes and print the details in Main.

Note: All the print statements should be in ‘Main.py’

Note: Strictly adhere to the Object-Oriented specifications given in the problem statement.
All class names, attribute names, and method names should be the same as specified in the problem statement.
 

Refer to sample input and output:

[All Texts in bold corresponds to the input and rest are output]
Sample Input/Output 1:

Enter book name

Book

Enter book details

All latest books are available under this category

Menu

1.Use Construtor to initialize

2.Use setters and getters

2
Initializing and printing using default Constructor and Setters and Getters
Details of the stall category:
Book Name : Book
Detail : All latest books are available under this category

Sample Input-Output 2:
Enter book name
Book
Enter book details
All latest books are available under this category
Menu
1.Use Construtor to initialize
2.Use setters and getters
1
Initializing and printing using parameterized Constructor and Str method

Details of the stall category:

Book Name : Book

Detail : All latest books are available under this category

Solution

class Stall:
    #fill your code
    def __init__(self, name, detail):
        self.__name = name
        self.__detail = detail
        
    def display(self):
       #fill your code
        return "Details of the stall category:\n" +"Book Name : " + self.__name + "\nDetail : " + self.__detail
    
    def setBookName(self,a):
        #fill your code
        self.__name = a
    
    def setDetails(self,b):
        #fill your code
        self.__detail = b
        
    def getBookName(self):
        #fill your code
        return self.__name
        
    def getDetails(self):
        #fill your code
        return self.__detail
    
#Main
x=raw_input("Enter book name\n")
y=raw_input("Enter book details")
n=input("\nMenu\n1.Use Construtor to initialize\n2.Use setters and getters\n")
#fill your code
stall = Stall(x, y)
if n == 1:
    print("Initializing and printing using parameterized Constructor and Str method")
    print(stall.display())
elif n == 2:
    print("Initializing and printing using default Constructor and Setters and Getters")
    stall.setBookName(x)
    stall.setDetails(y)

    print(stall.display())
else:
    print("Invalid choice")

Happy Learning – If you require any further information, feel free to contact me.

Share your love
Saurav Hathi

Saurav Hathi

I'm currently studying Bachelor of Computer Science at Lovely Professional University in Punjab.

📌 Nodejs and Android 😎
📌 Java

Articles: 444

Leave a Reply

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