File tree Expand file tree Collapse file tree 1 file changed +36
-2
lines changed
Expand file tree Collapse file tree 1 file changed +36
-2
lines changed Original file line number Diff line number Diff line change @@ -23,5 +23,39 @@ def print_product(self):
2323 """
2424 print '%d\t %s\t %.02f each' % (self .pid , self .qty , self .price )
2525
26- p = Product (1.4 , 123 , 5 )
27- p .print_product ()
26+ class Inventory :
27+
28+ def __init__ (self ):
29+ """
30+ Initializes the class instance.
31+ """
32+ self .products = [] # list to hold all products
33+
34+ def add (self , product ):
35+ """
36+ Adds a passed Product to the list of products.
37+ """
38+ self .products .append (product )
39+
40+ def print_inventory (self ):
41+ """
42+ Prints the current inventory, and the total value
43+ of products.
44+ """
45+ value = 0
46+ for product in self .products :
47+ print '%d\t %s\t %.02f each' % (product .pid , product .qty , product .price )
48+ value += (product .price * product .qty )
49+ print '\n Total value: %.02f' % value
50+
51+ if __name__ == '__main__' :
52+ p1 = Product (1.4 , 123 , 5 )
53+ p2 = Product (1 , 3432 , 100 )
54+ p3 = Product (100.4 , 2342 , 99 )
55+
56+
57+ i = Inventory ()
58+ i .add (p1 )
59+ i .add (p2 )
60+ i .add (p3 )
61+ i .print_inventory ()
You can’t perform that action at this time.
0 commit comments