Question

In: Computer Science

Write a TicketMachine class in PYTHON PROGRAM, which models a ticket machine that issues flatfare tickets....

Write a TicketMachine class in PYTHON PROGRAM, which models a ticket machine that issues flatfare tickets. The price of a ticket is specified via the constructor (you can use price variable).

Your TicketMachine class will have the following methods:

(a) insertMoney, which receives an amount of money from a customer and updates customer’s balance (you can use balance variable).

(b) getPrice, which return and prints the price of a ticket.

(c) getBalance, which returns and prints the amount of money already inserted for the ticket

(d) printTicket, which prints a ticket, update the total collected by the machine and reduce the balance to zero.

The ticket will be printed if the customer’s balance is greater than or equal to ticket price. You also need to define a variable e.g., total, which keeps track of the total amount of money collected by the machine.

Your program should be able to check if the ticket price and the amount entered by the customer is valid. For example, ticket price and the amount entered by the user cannot be negative.

Solutions

Expert Solution

Python code:

class TicketMachine:
    # Static variable to keep track of the total amount
    # of money collected by the machine
    total = 0

    # Constructor which takes ticket price
    def __init__(self, price):
        # To keep track of customer's balance
        self.balance = 0
        # Ticket price
        self.price = 0
        # Validate ticket price
        if price >= 0:
            self.price = price

    # (a) Function which receives an amount of money from a customer
    # and updates customer’s balance
    def insertMoney(self, amount):
        # Validate amount entered by the customer
        if amount > 0:
            self.balance += amount

    # (b) Function which returns and prints the price of a ticket
    def getPrice(self):
        return self.price

    # (c) Function which returns and prints the amount of money
    # already inserted for the ticket
    def getBalance(self):
        return self.balance

    # prints a ticket, update the total collected by
    # the machine and reduce the balance to zero.
    # The ticket will be printed if the customer’s
    # balance is greater than or equal to ticket price.
    def printTicket(self):
        # If balance is not sufficient, do not print the ticket
        if self.balance < self.price:
            print("Insufficient balance\n")
            return

        # Print the ticket
        print("*********TICKET*********")
        print("Ticket price: ${:.2f}".format(self.price))
        print("Ticket qty: 1\n")
        # Update the total collected by the machine
        TicketMachine.total += self.price
        # Reduce the balance to zero
        self.balance = 0


# Create a TicketMachine with ticket price set to $12.00
ticket = TicketMachine(12)
# Customer A inserts $15.00 to the machine
ticket.insertMoney(15)
ticket.printTicket()
# Customer B inserts $11.00 to the machine
ticket.insertMoney(11)
ticket.printTicket()
# Customer B inserts $1.00 more to the machine
ticket.insertMoney(1)
ticket.printTicket()
# Print the total money collected by the TicketMachine
print("Total amount collected: ${:.2f}".format(TicketMachine.total))

Output:

Kindly rate the answer and for any help just drop a comment


Related Solutions

Write a python program using the following requirements: Create a class called Sentence which has a...
Write a python program using the following requirements: Create a class called Sentence which has a constructor that takes a sentence string as input. The default value for the constructor should be an empty string The sentence must be a private attribute in the class contains the following class methods: get_all_words — Returns all the words in the sentence as a list get_word — Returns only the word at a particular index in the sentence Arguments: index set_word — Changes...
Write a program IN PYTHON of the JUPYTER NOOTBOOK Write a Python program that gets a...
Write a program IN PYTHON of the JUPYTER NOOTBOOK Write a Python program that gets a numeric grade (on a scale of 0-100) from the user and convert it to a letter grade based on the following table. A: 90% - 100% B 80% - 89% C 70% - 79% D 60% - 69% F <60% The program should be written so that if the user entered either a non-numeric input or a numeric input out of the 0-100 range,...
Python Explain Code #Python program class TreeNode:
Python Explain Code   #Python program class TreeNode:    def __init__(self, key):        self.key = key        self.left = None        self.right = Nonedef findMaxDifference(root, diff=float('-inf')):    if root is None:        return float('inf'), diff    leftVal, diff = findMaxDifference(root.left, diff)    rightVal, diff = findMaxDifference(root.right, diff)    currentDiff = root.key - min(leftVal, rightVal)    diff = max(diff, currentDiff)     return min(min(leftVal, rightVal), root.key), diff root = TreeNode(6)root.left = TreeNode(3)root.right = TreeNode(8)root.right.left = TreeNode(2)root.right.right = TreeNode(4)root.right.left.left = TreeNode(1)root.right.left.right = TreeNode(7)print(findMaxDifference(root)[1])
I want to write this program in java. Write a simple airline ticket reservation program in...
I want to write this program in java. Write a simple airline ticket reservation program in java.The program should display a menu with the following options: reserve a ticket, cancel a reservation, check whether a ticket is reserved for a particular person, and display the passengers. The information is maintained on an alphabetized linked list of names. In a simpler version of the program, assume that tickets are reserved for only one flight. In a fuller version, place no limit...
Program must be in Python Write a program in Python whose inputs are three integers, and...
Program must be in Python Write a program in Python whose inputs are three integers, and whose output is the smallest of the three values. Input is 7 15 3
Calculating Delivery Cost Program in Python write a program in Python that will ask a user...
Calculating Delivery Cost Program in Python write a program in Python that will ask a user to enter the purchase total, the number of the items that need to be delivered and delivery day. Then the system displays the cost of delivery along with the total cost. Purchase total > $150 Yes Number of the items (N) N<=5 N>=6 Delivery day Same Day Next Day Same Day Next Day Delivery charges ($) 8 N * 1.50 N * 2.50 N...
Write a design algorithm and a python program which asks the user for the length of...
Write a design algorithm and a python program which asks the user for the length of the three sides of two triangles. It should compute the perimeter of the two triangles and display it. It should also display which triangle has the greater perimeter. If both have the same perimeter it should display that the perimeters are the same. Formula: Perimeter of triangleA = (side1A + side2A +side3A) See the 2 sample outputs. Enter side1 of TriangleA: 2 Enter side2...
Write a Python program which uses a function to calculate the perimeter of a rectangle. a...
Write a Python program which uses a function to calculate the perimeter of a rectangle. a function named volume to calculate the volume of a cylinder volume = 3.14 x radius x radius x height .b function named volume to calculate the volume of a cuboid volume = Length x width x ht Write a Python Program to calculate the sum of all odd numbers for 2 to 20 using a for loop. 4. Write statements that assign random integers...
Python class Select a free ebook and download the plain text file (utf8). Write a program...
Python class Select a free ebook and download the plain text file (utf8). Write a program that does the following: Read in your ebook (any book, any language) Break the book into words (look at .split() for strings) Make a dictionary of all words and how often they occur: for instance if the word'the' happened 2000 time and the word'a' happened 1940 times word_dict= {'the' : 2000, 'a' :1940} Print the dictionary of word frequencies to a file (freqs.txt) with...
In python write a program which prints the longest substring of numbers which occur in ascending...
In python write a program which prints the longest substring of numbers which occur in ascending order s=342153476757561235
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT