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 that creates a class which represents a Student Grade. The class includes...
Write a Python program that creates a class which represents a Student Grade. The class includes a function that reads a text file called Course_Score.txt. A sample of the file is provided below. Each row in the file corresponds to the Last Name, First Name, ClassWork score (100), Mid-Term score (100), and Final-Exam score (100). Also include the the following functions to process the content read from the file. a. getData(): This method reads the data from a file and...
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 language which will do the following: Write a program to prompt...
Write a program in Python language which will do the following: Write a program to prompt the user to enter a company name, city, state and zip code. Create a dictionary with the data. Output the dictionary. Then remove the city from the dictionary and output again.
Write a python program to read from a file the names and grades of a class...
Write a python program to read from a file the names and grades of a class of students to calculate the class average, the maximum, and the minimum grades. The program should then write the names and grades on a new file identifying the students who passed and the students who failed. The program should consist of the following functions: a) Develop a gradesInput() function that reads data from a file and stores it and returns it as a dictionary....
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...
In Python, write a program that computes machine epsilon (macheps) in: Single precision (32 bits) and...
In Python, write a program that computes machine epsilon (macheps) in: Single precision (32 bits) and Double precision (64 bits)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT