In: Computer Science
Create a Python 3 program that acts as a grocery store shopping cart. 1. Create a class named GroceryItem. This class should contain: - an __init__ method that initializes the item’s name and price - a get_name method that returns the item’s name - a get_price method that returns the item’s price - a __str__ method that returns a string representation of that GroceryItem - an unimplemented method is_perishable Save this class in GroceryItem.py. 2. Create a subclass Perishable that inherits from GroceryItem. This class should contain: - an __ init__ method that passes a name and price to GroceryItem - an implementation of is_perishable that returns True Save this class in Perishable.py, or you may add it right into GroceryItem.py. 3. Create a subclass NonPerishable that inherits from GroceryItem. This class should contain: - an __ init__ method that passes a name and price to GroceryItem - an implementation of is_perishable that returns False Save this class in NonPerishable.py, or you may add it right into GroceryItem.py. 4. Create a main program that continuously shows the user a menu for manipulating GroceryItems into a list. Your menu should allow the user to: - add an item to their cart - show all items in the cart - checkout and exit Each menu choice should be handled in a separate method to do the work, such as a method to create_grocery_item, print_cart, and checkout, making your main pretty simple. Show the user a menu continuously until they choose to checkout. create_grocery_item should do the job of prompting the user for the name and price of a grocery item. Also ask the user if this item is perishable or not, then create the appropriate Perishable or NonPerishable object and append it to a list of GroceryItems. print_cart should print all the elements in the GroceryItem list. Since the cart will be a mixed bag of GroceryItem types, this will use polymorphism! checkout should output some kind of store receipt to the user, then exit the program. It should show all the items in the cart with their price, sales tax, then the total bill amount. Separate the items in the receipt by perishable vs non-perishable.
'''
Python version : 3.6
Python program to create a program that acts as a grocery store shopping cart.
'''
# This module provides the infrastructure for defining abstract base classes (ABCs)
import abc
#abstract class GroceryItem
class GroceryItem():
__metaclass__ = abc.ABCMeta
# constructor
def __init__(self,name,price):
self.name = name
self.price = price
# function that returns the name of the item
def get_name(self):
return self.name
# function that returns the price of the item
def get_price(self):
return self.price
# function that returns the string representation of the item
def __str__(self):
return ('%-20s%.2f' %(self.name,self.price))
# abstract method
@abc.abstractmethod
def is_perishable(self):
return
# class Perishable
class Perishable(GroceryItem):
# constructor
def __init__(self,name,price):
super(Perishable,self).__init__(name,price) # call the GroceryItem constructor
# returns that the item is perishable
def is_perishable(self):
return True
# class Non-Perishable
class NonPerishable(GroceryItem):
#constructor
def __init__(self,name,price):
super(NonPerishable,self).__init__(name,price) # call the GroceryItem constructor
# returns that the item is not perishable
def is_perishable(self):
return False
# function to display the menu and get the input of menu choice from the user
def menu():
print("1. Add an item to the cart")
print("2. Show items in the cart")
print("3. Checkout and exit")
choice = int(input('Enter your choice(1-3) : '))
# validate the choice
while choice < 1 or choice > 3:
print('Invalid choice')
choice = int(input('Enter your choice(1-3) : '))
return choice
# function to create and return the grocery item
def create_grocery_item():
name = input('Enter name of the item : ')
price = float(input('Enter price of the item : '))
perishable = input('Is the item perishable (y/n) ? ')
if perishable.lower() == "y":
item = Perishable(name,price)
else:
item = NonPerishable(name,price)
print("")
return item
# function to print the items in the cart
def print_cart(cart):
if len(cart) == 0:
print("Empty cart")
else:
print('%-20s%s' %('Item','Price'))
for item in cart:
print(item)
print("")
# function to print the Bill Receipt
def checkout(cart):
if len(cart) == 0:
print("Empty cart")
else:
print("%15s" %("Receipt"))
sales_tax_percent = 5
total_price = 0
num_perishable = 0
num_non_perishable = 0
for item in cart:
total_price += item.get_price()
if item.is_perishable():
num_perishable += 1
else:
num_non_perishable += 1
sales_tax = ((float(sales_tax_percent))*total_price)/100
bill_amount = total_price + sales_tax
if(num_perishable > 0):
print("%10s" %('Perishable'))
print('%-20s%s' %('Item','Price'))
for item in cart:
if item.is_perishable():
print(item)
print("")
if(num_non_perishable > 0):
print("%10s" %('Non-Perishable'))
print('%-20s%s' %('Item','Price'))
for item in cart:
if not item.is_perishable():
print(item)
print("")
print("%15s : %.2f" %("Total Price",total_price))
print("%15s : %.2f" %("Sales Tax",sales_tax))
print("%15s : %.2f" %("Bill Amount",bill_amount))
# main program to simulate the operations in the shopping cart
def main():
cart = []
choice = menu()
# loop continues till the user exits
while choice != 3:
if choice == 1:
item = create_grocery_item()
cart.append(item)
elif choice == 2:
print_cart(cart)
choice = menu()
checkout(cart)
# call the main function
main()
#end of program
Code Screenshot:
Output: