Question

In: Computer Science

In this question you will be asked to create some classes: Store, GroceryStore, and ClothingStore.

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:

  • total_profit - this is a CLASS ATTRIBUTE that keeps track of the profit made in each instance of Store. (A class attribute is an attribute shared by all the instances of the class)
  • __init__ - this is a constructor that is used to create an instance of a Store object. This constructor will do three things:
    • initialize an instance variable inventory to be an empty dictionary. This dictionary will have 'items' as keys with a dictionary for values. This inner dictionary will have key value pairs for 'price' (the price of an item), 'stock' (the number of items), and 'category' (the type of item).
    • initialize an instance variable num_items. This will count the total number of items in the current Store.
    • initialize an instance variable profit. This will keep track of the total profit made in the current Store.
  • add_item() - this will take in an item name, the item price, the number of items to be added, and the category of items.
    • Add the specified item to the inventory by assigning the item key a dictionary of its values. Look at bullet point 1 in __init__ for details.
      • Ex. If I called add_item("Bread", 5, 10, "Food"). Inventory would look like {'Bread': {'price': 5, 'stock': 10, 'category': 'Food'}}
      • It would then PRINT 'Added 10 Bread at $5'
    • Increase num_items by the stock of the item we added.
    • If the specified item ALREADY EXISTS in our inventory, only add to its stock and ignore all other changes.
      • If I called add_item("Bread", 20, 5, "Not food"), only increase stock and num_items and PRINT "Added 5 more Breads"
    • If the 'stock' is less than one, PRINT "MUST ADD AT LEAST ONE (item)"
  • sell_item() - this will take in an item name and the number of items to be sold.
    • Decrease the stock of the item and num_items by the number of items sold.
    • Increase the profit of the store and the profit of all stores.
    • If the item does not exist in the inventory, PRINT "ITEM NOT IN INVENTORY"
    • If we try to sell more of the item than the store has, PRINT "NOT ENOUGH ITEMS IN INVENTORY"
  • sell_all() - this will take in an item name and sell all of the items that the store has.
    • If the item does not exist in the inventory, PRINT "ITEM NOT IN INVENTORY"
  • get_item() - will return the dictionary value of the item
    • If the item does not exist in the inventory, PRINT "ITEM NOT IN INVENTORY"
  • get_num_items() - get the number of items in the store
  • get_profit() - get the profit of the store
  • get_total_profit() - get the profit of all stores
  • __repr__ - output of the store when it is printed. Do not change this function.

GroceryStore class is a child (subclass) of Store and has the following methods.

  • __init__ - constructor to create an instance of a GroceryStore.
    • Since GroceryStore is inherited from Store, what line should you have? Hint here.# this link just refers to the super function
    • https://www.programiz.com/python-programming/methods/built-in/super
    • Set an instance variable category to be 'Food'. You will use this when you call add_item()
  • add_item() - will take in the item name, item price, and the stock of the item.
    • Think about how we can use the add_item() we have already defined in a parent class. Make sure to not duplicate code in this method

ClothingStore class is a another child (subclass) of Store and has the following methods.

  • __init__ - constructor to create an instance of a ClothingStore.
    • Since ClothingStore is inherited from Store, what line should you have?
    • Set an instance variable category to be 'Clothes'. You will use this when you call add_item()
  • add_item() - will take in the item name, item price, and the stock of the item.
    • Think about how we can use the add_item() we have already defined in a parent class. Make sure to not duplicate code in this method

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

Solutions

Expert Solution

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
                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:


Related Solutions

The Python Question is as follows: You have been asked by a manager of a store...
The Python Question is as follows: You have been asked by a manager of a store to identify the items most commonly bought together (the manager would like to place these items in close proximity). You are given a file, receipts.txt , that contains the receipts of the last 1000 transactions in the following format, where each line of the file is a single receipt: eggs, bread, milk, lettuce cheese, milk, apples bread, milk bread, cheese, milk Write a function...
In this question, use your new GUI classes to create a small application. You have 2...
In this question, use your new GUI classes to create a small application. You have 2 options: Write any small application of your choice, as long as it follows the rules that follow. Write the small UnitConversion application, which will be described for you. Option 1: Write your own application You can write any small application that you like, as long as: It uses your GUI classes, including: at least one checkbox, at least one group of 3 radio buttons,...
1. For the following two question, you are asked to create drug cards. It needs to...
1. For the following two question, you are asked to create drug cards. It needs to include the following information: •Class •Route of Administration •Mechanism of Action •Onset of Medication •Any additional relevant information about the medication Question 1: Create a drug card for Salbutamol (Ventolin). Question 2: Create a drug card for Ipratropium Bromide (Atrovent).
C++ Assignment 1: Make two classes practice For each of the two classes you will create...
C++ Assignment 1: Make two classes practice For each of the two classes you will create for this assignment, create an *.h and *.cpp file that contains the class definition and the member functions. You will also have a main file 'main.cpp' that obviously contains the main() function, and will instantiate the objects and test them. Class #1 Ideas for class one are WebSite, Shoes, Beer, Book, Song, Movie, TVShow, Computer, Bike, VideoGame, Car, etc Take your chosen class from...
For this assignment, you will create a hierarchy of five classes to describe various elements of...
For this assignment, you will create a hierarchy of five classes to describe various elements of a school setting. The classes you will write are: Person, Student,Teacher, HighSchoolStudent, and School. Detailed below are the requirements for the variables and methods of each class. You may need to add a few additional variables and/or methods; figuring out what is needed is part of your task with this assignment. Person Variables: String firstName - Holds the person's first name String lastName -...
Create a C++ code for the mastermind game using classes(private classes and public classes). Using this...
Create a C++ code for the mastermind game using classes(private classes and public classes). Using this uml as a reference.
You are an IT manager for a small business. You are being asked to create a...
You are an IT manager for a small business. You are being asked to create a network security plan both for internal IT workers and for the company in general. You are to put together a network security plan that addresses each of the following components : Explain the use of virtual private networks (VPNs) and their security benefits and drawbacks. Create a standard procedure for adding new users to a network. Create a summary of a network plan, including...
I am trying to create a PICO question research question. Can you please give me some...
I am trying to create a PICO question research question. Can you please give me some examples that i can use? At least 3 PICO questions and their research question and what do you want to find out about the issue. Thank you
QUESTION 4 You are preparing some sweet potato pie for your annual Thanksgiving feast. The store...
QUESTION 4 You are preparing some sweet potato pie for your annual Thanksgiving feast. The store sells sweet potatoes in packages of 6. From prior experience you have found that the package will contain 0 spoiled sweet potatoes about 65% of the time, 1 spoiled sweet potato 25% of the time and 2 spoiled sweet potatoes the rest of the time. Conduct a simulation to estimate the number of packages of sweet potatoes you need to purchase to have three...
A quality survey asked recent customers of their experience at a local department store. One question...
A quality survey asked recent customers of their experience at a local department store. One question asked for the customers rating on their service using categorical responses of average, outstanding, and exceptional. Another question asked for the applicant’s education level with categorical responses of Some HS, HS Grad, Some College, and College Grad. The sample data below are for 700 customers who recently visited the department store. Education Quality Rating Some HS HS Grad Some College College Grad Average 55...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT