In: Computer Science
* Python *
* Python Programming *
Part 1: Product Class
Write a class named Product that holds data about an item in a retail store. The class should store the following data in attributes: product id, item description, units in inventory, and price. Write the __init__ method to require all four attributes. Also write a __str__ method for debugging output.
Once you have written the class, write a main() function that creates three Product objects and stores the following data in them. Use the __str__ method to confirm the data is stored properly.
ID
Description
Quantity Price
1
Jacket
12
59.95
2 Designer
Jeans
40
34.95
3
Shirt
20
24.95
Submit your python file (.py) to Sakai
Part 2: Inventory Module
Create an Inventory module by moving the Product Class definition to another file called inventory.py. Add three __doc__ strings: one that describe the inventory module (include your name and the date here), one that describes the Product class, and one that describes the Product class __init__ method.
Rename your main() function to test_inventory.py
Add an import at the top of the file to read the Product class
definition.
Add these print statements to test the __doc__ string:
Print(inventory.__doc__)
Print(Product.__doc__)
Print(Product.__init__..__doc__)
Test the program to be sure it still works. NOTE: If using IDLE, you will have to explicitly save the inventory.py file after making changes.
Part1
product.py
class Product(object):
def __init__(self, product_id, item_desc, units, price):
self.product_id = product_id
self.item_desc = item_desc
self.units = units
self.price = price
def __str__(self):
return 'Product ID: %s, Itesm: %s, Units: %d, price: %.2f' %(self.product_id, self.item_desc, self.units, self.price)
def main():
p1 = Product('1', 'Jacket', 12, 59.95)
p2 = Product('2', 'Designer Jeans', 40, 34.95)
p3 = Product(3, 'Shirt', 20, 24.95)
print(p1)
print(p2)
print(p3)
main()
# OUT
part 2 NOTE: update your name in below place holder
inventory.py
""" Name: <PLACE YOUR NAME> DAte: 18-09-2019 """ class Product(object): """ prodcut class representataion of product that have id, name units and price """ def __init__(self, product_id, item_desc, units, price): """ constructor of product class :param product_id: id of the product :param item_desc: item detail :param units: no of units :param price: price per unit """ self.product_id = product_id self.item_desc = item_desc self.units = units self.price = price def __str__(self): return 'Product ID: %s, Itesm: %s, Units: %d, price: %.2f' % ( self.product_id, self.item_desc, self.units, self.price)
test_inventory.py
import inventory # importing inventory Product = inventory.Product def main(): # printing doc print(inventory.__doc__) print(Product.__doc__) print(Product.__init__.__doc__) # creating and printing product p1 = Product('1', 'Jacket', 12, 59.95) p2 = Product('2', 'Designer Jeans', 40, 34.95) p3 = Product(3, 'Shirt', 20, 24.95) print(p1) print(p2) print(p3) main()
# OUT