Using Python 3.4.3 For this assignment you are to create a Merchandise data clas
ID: 3663144 • Letter: U
Question
Using Python 3.4.3
For this assignment you are to create a Merchandise data class and, in a separate file, an executable program named program101.py to test it. Refer to the UML diagram below to create the Merchandise class. See page 462-463 regarding UML diagrams.
__item
__quantity
__cost
__init__(item,quantity,cost)
set_item(item)
set_quantity(quantity)
set_cost(cost)
get_item()
get_quantity()
get_cost()
get_inventory_value()
__str__()
NOTE: the get_inventory_value() method calculates and prints the value of the merchandise item in currency format.
NOTE: the __str()__ method (see page 437-440) returns a string showing all 3 attributes and the inventory value (quantity * cost).
In the program101.py main function:
create a Merchandise instance for 10 hammers costing $14.95 each.
create another Merchandise object for 6 necklaces costing $799.99 each.
run the __str__ methods of both objects.
prompt the user for a new quantity for the hammer object, then change this attribute with the setter method.
prompt the user for a new cost for the necklace object, then change this attribute with the setter method.
verify the changes by running the __str__ methods again.
display the inventory value for each object as well.
SAMPLE OUTPUT
hammer, 10 @ $14.95
necklace, 6 @ $799.99
Enter new quantity for hardware 12
Enter new cost for jewelry 659
hammer, 12 @ $14.95
Inventory value: $179.40
necklace, 6 @ $659.00
Inventory value: $3,954.00
__item
__quantity
__cost
__init__(item,quantity,cost)
set_item(item)
set_quantity(quantity)
set_cost(cost)
get_item()
get_quantity()
get_cost()
get_inventory_value()
__str__()
Explanation / Answer
##program101.py
class MerchandiseProg:
def __init__(self11,item,qty,cost):
self11.__item = item
self11.__qty = qty
self11.__cost = cost
def set_item(self11,item):
self11.__item = item
def set_quantity(self11,qty):
self11.__qty = qty
def set_cost(self11,cost):
self11.__cost = cost
def get_item(self11):
return self11.__item
def get_quantity(self11):
return self11.__qty
def get_cost(self11):
return self11.__cost
def __str__(self11):
s = self11.__item + ' ' + str(self11.__qty)
s += ' @ ' + str(self11.__cost)
return s
### import a merchandiseProg module
import merchandiseProg
def main():
storevalmerchandise = merchandiseProg.MerchandiseProg('hammer',10,14.95)
print(storevalmerchandise)
storevalmerchandise.set_item('necklaces')
storevalmerchandise.set_quantity(6)
storevalmerchandise.set_cost(799.99)
print(storevalmerchandise)
item2 = input('Enter a new quantity for the hardware ')
storevalmerchandise.set_item('hammer')
storevalmerchandise.set_quantity(item2)
storevalmerchandise.set_cost(14.95)
print(storevalmerchandise)
necklace2 = input('Enter the new cost for a necklaces ')
storevalmerchandise.set_item('necklace')
storevalmerchandise.set_quantity(necklace2)
storevalmerchandise.set_cost(659.00)
print(storevalmerchandise)
print(storevalmerchandise + format(storevalmerchandise.get_cost(), ',.2f'), sep='')
main()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.