In: Computer Science
Use Python
general directions:
In this question you will be asked to create some classes: Store, GroceryStore, and ClothingStore.
Store has the following attributes and methods:
GroceryStore class is a child (subclass) of Store and has the following methods.
ClothingStore class is a another child (subclass) of Store and has the following methods.
Questions themselves:
class Store:
"""
Store class to copy some functions of a store
>>> walmart = Store()
>>> walmart.add_item("Bread", 5, 10, "Food")
Added 10 Bread at $5
>>> walmart.get_num_items()
10
>>> walmart.get_profit()
0
>>> walmart.sell_item("Bread", 3)
>>> walmart.get_profit()
15
>>> walmart.sell_all("Bread")
>>> walmart.get_profit()
50
>>> walmart
This store has
Bread - Price: 5 - In Stock: 0 - Category: Food
for a total of 0 items and a profit of $50
>>> costco = Store()
>>> costco.add_item("iPad", 200, 5, "Electronics")
Added 5 iPad at $200
>>> costco.sell_item("Hotdog", 1)
ITEM NOT IN INVENTORY
>>> costco.add_item("Hotdog", 3, 2, "Food")
Added 2 Hotdog at $3
>>> costco.get_item("Hotdog")
{'price': 3, 'stock': 2, 'category': 'Food'}
>>> costco.sell_item("Hotdog", 5)
NOT ENOUGH ITEMS IN INVENTORY
>>> costco.sell_all("iPad")
>>> costco.sell_all("Hotdog")
>>> costco.get_profit()
1006
>>> costco
This store has
iPad - Price: 200 - In Stock: 0 - Category: Electronics
Hotdog - Price: 3 - In Stock: 0 - Category: Food
for a total of 0 items and a profit of $1006
>>> Store.total_profit
1056
>>> trashcan = Store()
>>> trashcan.get_num_items()
0
>>> trashcan.get_item("trash")
ITEM NOT IN INVENTORY
>>> trashcan
This store has no items
"""
def __init__(self):
# Replace pass with your code
pass
def add_item(self, item, price, stock, category):
# Replace pass with your code
pass
def sell_item(self, item, num):
# Replace pass with your code
pass
def sell_all(self, item):
# Replace pass with your code
pass
def get_item(self, item):
# Replace pass with your code
pass
def get_num_items(self):
# Replace pass with your code
pass
def get_profit(self):
# Replace pass with your code
pass
def get_total_profit(self):
# Replace pass with your code
pass
def __repr__(self):
if not self.inventory:
return "This store has no items"
s = "This store has\n"
for item in self.inventory:
s += str(item) + \
" - Price: " + str(self.get_item(item)['price']) + \
" - In Stock: " + str(self.get_item(item)['stock']) + \
" - Category: " + str(self.get_item(item)['category']) + \
"\n"
s += "for a total of " + str(self.num_items) + " items " + \
"and a profit of $" + str(self.profit)
return s
class GroceryStore(Store):
"""
>>> mcd = GroceryStore()
>>> mcd.add_item("Happy Meal", 5, 20)
Added 20 Happy Meal at $5
>>> mcd.get_item("Happy Meal")
{'price': 5, 'stock': 20, 'category': 'Food'}
>>> mcd
This store has
Happy Meal - Price: 5 - In Stock: 20 - Category: Food
for a total of 20 items and a profit of $0
"""
def __init__(self):
# Replace pass with your code
pass
def add_item(self, item, price, stock):
# Replace pass with your code
pass
class ClothingStore(Store):
"""
>>> gucci = ClothingStore()
>>> gucci.add_item("Jacket", 1000, 2)
Added 2 Jacket at $1000
>>> gucci.get_item("Jacket")
{'price': 1000, 'stock': 2, 'category': 'Clothes'}
>>> gucci
This store has
Jacket - Price: 1000 - In Stock: 2 - Category: Clothes
for a total of 2 items and a profit of $0
"""
def __init__(self):
# Replace pass with your code
pass
def add_item(self, item, price, stock):
# Replace pass with your code
pass
This program contains a super class Store and its 2 sub classes GroceryStore and ClothigStore.
Store class:
Store class Code:
#Store class
class Store:
#class variable
total_profit=0
#constructor
def __init__(self):
#instance
variables
self.inventory={} #empty
dictionary
self.num_items=0
self.profit=0
#method
def add_item(self, item, price, stock,
category):
#when stock is less than
1 print
if(stock<1):
print('MUST ADD AT LEAST ONE (item)')
#when item is already
present in the inventory dictionary
elif item in
self.inventory:
#increment the stock part
self.inventory[item]['stock']=self.inventory[item]['stock']+stock
#print
print('Added',stock,'more',category)
#increment the instance variable num_items
self.num_items=self.num_items+stock
#when item is not
present in the dictionary add a new key-value pair
else:
self.inventory[item]={}
self.inventory[item]['price']=price
self.inventory[item]['stock']=stock
self.inventory[item]['category']=category
print('Added',stock,item,'at $'+str(price))
#increment num_items value
self.num_items=self.num_items+stock
#method for selling items
def sell_item(self, item, num):
#if the item is present
in the dictionary
if item in
self.inventory:
#and the no of stock to be sold is greater than the stock
present
if(self.inventory[item]['stock']
print('NOT ENOUGH ITEMS IN INVENTORY')
#if the requested stock is available
else:
#decrement stock and num_items value
self.inventory[item]['stock']=self.inventory[item]['stock']-num
self.num_items=self.num_items-num
#increase the profit by multiplying price with the num of items
sold
self.profit=self.profit+(self.inventory[item]['price']*num)
Store.total_profit=Store.total_profit+self.inventory[item]['price']*num
#if item is not present
in the dictionary
else:
print('ITEM NOT IN INVENTORY')
#method for selling all the stock of an
item
def sell_all(self, item):
#if the item is present
in dictionary
if item in
self.inventory:
#store the item's stock in a stock variable
stock=self.inventory[item]['stock']
#decrement num_items by stock
self.num_items=self.num_items-stock
#set item's stock value to 0
self.inventory[item]['stock']=0
#calculate the profit
self.profit=self.profit+(self.inventory[item]['price']*stock)
Store.total_profit=Store.total_profit+self.inventory[item]['price']*stock
#if item is not
present in the dictionary
else:
print('ITEM NOT IN INVENTORY')
#method to return the requested item
values
def get_item(self, item):
#if item is present in
the dictionary
if item in
self.inventory:
return self.inventory[item]
else:
print('ITEM NOT IN INVENTORY')
#method to return the num_items value
def get_num_items(self):
return
self.num_items
#method to return the profit value
def get_profit(self):
return self.profit
#method to return the total_profit
value
def get_total_profit(self):
return
Store.total_profit
#display function
def __repr__(self):
if not
self.inventory:
return "This store has no items"
s = "This store
has\n"
for item in
self.inventory:
s += str(item) + \
" - Price: " + str(self.get_item(item)['price']) + \
" - In Stock: " + str(self.get_item(item)['stock']) + \
" - Category: " + str(self.get_item(item)['category']) + \
"\n"
s += "for a total of " + str(self.num_items) + " items " + \
"and a profit of $" + str(self.profit)
return s
GroceryStore Class:
#class GroceryStore inherits Store class
class GroceryStore (Store):
#constructor
def __init__(self):
#call the super
class(Store class) constructor
super().__init__()
#declare instance
variable
self.category='Food'
#method for adding items
def add_item(self,item,price,stock):
#call super class method
add_item
super().add_item(item,price,stock,self.category)
ClothingStore class:
#class ClothingStore inherits Store class
class ClothingStore(Store):
#constructor
def __init__(self):
#call the super
class(Store class) constructor
super().__init__()
#declare instance
variable
self.category='Clothes'
#method for adding items
def add_item(self,item,price,stock):
#call super class method
add_item
super().add_item(item,price,stock,self.category)
Testing:
Code:
#For testing the 3 classes
walmart = Store()
walmart.add_item("Bread", 5, 10, "Food")
walmart.sell_item("Bread", 3)
walmart.sell_all("Bread")
print(walmart.get_profit())
print(walmart)
print()
print()
costco = Store()
costco.add_item("iPad", 200, 5, "Electronics")
costco.sell_item("Hotdog", 1)
costco.add_item("Hotdog", 3, 2, "Food")
print(costco.get_item("Hotdog"))
costco.sell_item("Hotdog", 5)
costco.sell_all("iPad")
costco.sell_all("Hotdog")
print(costco.get_profit())
print(costco)
print(Store.total_profit)
print()
print()
trashcan = Store()
print(trashcan.get_num_items())
trashcan.get_item("trash")
print(trashcan)
print()
print()
mcd = GroceryStore()
mcd.add_item("Happy Meal", 5, 20)
print(mcd.get_item("Happy Meal"))
print(mcd)
print()
print()
gucci = ClothingStore()
gucci.add_item("Jacket", 1000, 2)
print(gucci.get_item("Jacket"))
print(gucci)
Output: