Answer to Question #192540 in Python for Andu

Question #192540

Task 2

Create an abstract class named Ticket that represents the ticket to access a museum. The class has a constructor that takes in input a number representing the base value of the ticket

Moreover, the class has an abstract method named "calcTicketCost(int)".

In addition, create two classes, respectively named

NormalTicket and Student Ticket that inherit from the class Ticket and implement the missing abstract method calcTicketCost(int). The total cost of the ticket depends on the base ticket value and the number of visitors (normally, number of visitors x base value).

For the normal ticket the following discounts are applied: 1 to 3 visitors, no discount 4 to 7 visitors, 10% 8 to 10 visitors, 15% .More than 10, 20%

For the student ticket there is a flat discount of 40% regardless of the number of visitors.


1
Expert's answer
2021-05-13T05:39:13-0400
#from abc import ABC, abstractmethod
#from random import normalvariate

#Abstract class Ticket
class Ticket(ABC):
    
    #Constructor which assigns base price of a ticket
    def __init__(self, basePrice):
        self.basePrice = basePrice

    #Abstract method to calculate ticket price
    @abstractmethod
    def calcTicketCost(self, n):
        pass

#NormalTicket class that inherits from Ticket class
class NormalTicket(Ticket):
    
    #Implementation of abstract method from Ticket class
    def calcTicketCost(self, n):
        if n < 4:
            return n*self.basePrice
            
        elif n < 8:
            return n*self.basePrice*0.9
        
        elif n < 11:
            return n*self.basePrice*0.85
        
        else:
            return n*self.basePrice*0.8

#StudentTicket class that inherits from Ticket class
class StudentTicket(Ticket):
    
    #Implementation of abstract method from Ticket class
    def calcTicketCost(self, n):
        return n*self.basePrice*0.6


#Main method
if __name__ == "__main__":
    
    #Object of NormalTcket class
    nt = NormalTicket(10)
    print(nt.calcTicketCost(10))

    #Object of StudentTicket class
    st = StudentTicket(20)
    print(st.calcTicketCost(1))

We first import the "abc" module for using abstract class in python. Then we create an abstract class Ticket that has a constructor to assign base ticket price. It also contains an abstract method calcTicketCost to calculate cost of ticket for n number of people. Now we define two classes NormalTicket and StudentTicket that inherits from abstract class Ticket. We define the abstract method of Ticket class, calcTicketCost in both these classes.

In main method, we test the objects of both classes by initializing them with constructors and calling calcTicketCost method.


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS