In: Computer Science
Please solve using jupyter notebook .
10.10- (Invoice Class) Create a class called Invoice that a hardware store might use to represent an invoice for an item sold at the store. An Invoice should include four pieces of information as data attributes—a part number (a string), a part description (a string), a quantity of the item being purchased (an int) and a price per item (a Decimal). Your class should have an __init__ method that initializes the four data attributes. Provide a property for each data attribute. The quantity and price per item should each be non-negative—use validation in the properties for these data attributes to ensure that they remain valid. Provide a calculate_invoice method that returns the invoice amount (that is, multiplies the quantity by the price per item). Demonstrate class Invoice’s capabilities.
Program Code [Python]
# Invoice class
class Invoice:
# __init__ method
def __init__(self, nu, description, quantity, price_per_item):
# All Required data attributes
self.__part_number = nu
self.__part_description = description
self.__quantity = quantity
self.__price_per_item = price_per_item
# Providing property of each data attribute
@property
def part_number(self):
return self.__part_number
@part_number.setter
def part_number(self, num):
self.__part_number = num
@property
def part_description(self):
return self.__part_description
@part_description.setter
def part_description(self, description):
self.__part_description = description
@property
def quantity(self):
return self.__quantity
@quantity.setter
def quantity(self, quantity):
# Validating that quantity must be non-negative
if quantity >= 0:
self.__quantity = quantity
@property
def price_per_item(self):
return self.__price_per_item
@price_per_item.setter
def price_per_item(self, price_per_item):
if price_per_item >= 0:
self.__price_per_item = price_per_item
# calculate_invoice method
def calculate_invoice(self):
return self.__quantity * self.__price_per_item
# testing Invoice class
invoice = Invoice(1, 'Sports Shirts and Shorts', 5, 100)
print('Invoice')
print('Part Number:', invoice.part_number)
print('Part Description:', invoice.part_description)
print('Quantity:', invoice.quantity)
print('Price Per Item: $', invoice.price_per_item)
print('Total: $', invoice.calculate_invoice())
Sample Output:-
----------------------------------------------------------------------
COMMENT DOWN FOR ANY QUERIES!!!
HIT A THUMBS UP IF YOU DO LIKE IT!!!