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...
Java Program to write a Fraction class that models a mathematical fraction. The fraction class needs...
Java Program to write a Fraction class that models a mathematical fraction. The fraction class needs to support simple arithmetic functions including taking the sum, difference, and the product of the division of the two. Do not include a main() method in the Fraction class. The Fraction class will implement the following class methods: Fraction add (Fraction f1, Fraction f2); // f1 + f2 and returns a new Fraction Fraction sub (Fraction f1, Fraction f2); // f1 - f2 and...
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])
You will write a program using Python that simulates an Automatic Teller Machine (ATM). For this...
You will write a program using Python that simulates an Automatic Teller Machine (ATM). For this program, your code can have user defined functions (but not required), the program must not call on any external functions or modules to handle any of the input, computational, and output requirements. Requirements: There is a customer with the following credential and can only access the system if the Access Code is correct: • Name: Peter Parker, Access Code: 2222 • When the program...
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...
The second assignment involves writing a Python program to compute the price of a theater ticket....
The second assignment involves writing a Python program to compute the price of a theater ticket. Your program should prompt the user for the patron's age and whether the movie is 3D. Children and seniors should receive a discounted price. There should be a surcharge for movies that are 3D. You should decide on the age cutoffs for children and seniors and the prices for the three different age groups. You should also decide on the amount of the surcharge...
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...
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT