Question

In: Computer Science

Create a Python 3 program that acts as a grocery store shopping cart. 1. Create a...

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.

Solutions

Expert Solution

'''

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:


Related Solutions

Python This week you will write a program in Python that mimics an online shopping cart...
Python This week you will write a program in Python that mimics an online shopping cart . You will use all the programming techniques we have learned thus far this semester, including if statements, loops, lists and functions. It should include all of the following: The basics - Add items to a cart until the user says "done" and then print out a list of items in the cart and a total price. - Ensure the user types in numbers...
In python 3 please: This program extends the earlier "Online shopping cart" program. (Start working from...
In python 3 please: This program extends the earlier "Online shopping cart" program. (Start working from your Program 7). (1) Build the ShoppingCart class with the following data attributes and related methods. Note: Some can be method stubs (empty methods) initially, to be completed in later steps.(3 pt) ● Parameterized constructor which takes the customer name and date as parameters ● Attributes ○ customer_name (string) ○ current_date (string) ○ cart_items (list) ● Methods ○ add_item() ■ Adds an item to...
Python This week you will write a program in Pyhton that mimics an online shopping cart...
Python This week you will write a program in Pyhton that mimics an online shopping cart . You will use all the programming techniques we have learned thus far this semester, including if statements, loops, lists and functions. It should include all of the following: The basics - Add items to a cart until the user says "done" and then print out a list of items in the cart and a total price. - Ensure the user types in numbers...
A customer comes into a grocery store and buys 8 items. Write a PYTHON program that...
A customer comes into a grocery store and buys 8 items. Write a PYTHON program that asks for the name of the item and the price of each item, and then displays these on the screen. Refer to the print() and input() statements covered in the module. Do NOT use loops as it will be covered in later modules. Apples 2.10 Hamburger 3.25 Milk 3.49 Sugar 1.99 Bread 1.76 Deli Turkey 7.99 Pickles 3.42 Butter 2.79
Python: Write a program to simulate the purchases in a grocery store. A customer may buy...
Python: Write a program to simulate the purchases in a grocery store. A customer may buy any number of items. So, you should use a while loop. Your program should read an item first and then read the price until you enter a terminal value (‘done’) to end the loop. You should add all the prices inside the loop. Add then a sales tax of 8.75% to the total price. Print a receipt to indicate all the details of the...
This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program). (1)...
This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program). (1) Extend the ItemToPurchase class to contain a new attribute. (2 pts) item_description (string) - Set to "none" in default constructor Implement the following method for the ItemToPurchase class. print_item_description() - Prints item_description attribute for an ItemToPurchase object. Has an ItemToPurchase parameter. Ex. of print_item_description() output: Bottled Water: Deer Park, 12 oz. (2) Build the ShoppingCart class with the following data attributes and related methods....
This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program). (1)...
This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program). (1) Extend the ItemToPurchase class to contain a new attribute. (2 pts) item_description (string) - Set to "none" in default constructor Implement the following method for the ItemToPurchase class. print_item_description() - Prints item_description attribute for an ItemToPurchase object. Has an ItemToPurchase parameter. Ex. of print_item_description() output: Bottled Water: Deer Park, 12 oz. (2) Build the ShoppingCart class with the following data attributes and related methods....
7.7 Ch 7 Program: Online shopping cart (continued) (C) This program extends the earlier "Online shopping...
7.7 Ch 7 Program: Online shopping cart (continued) (C) This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program). (1) Extend the ItemToPurchase struct to contain a new data member. (2 pt) char itemDescription[ ] - set to "none" in MakeItemBlank() Implement the following related functions for the ItemToPurchase struct. PrintItemDescription() Has an ItemToPurchase parameter. Ex. of PrintItemDescription() output: Bottled Water: Deer Park, 12 oz. (2) Create three new files: ShoppingCart.h - struct definition and...
C++.This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program). (1)...
C++.This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program). (1) Extend the ItemToPurchase class per the following specifications: Parameterized constructor to assign item name, item description, item price, and item quantity (default values of 0). (1 pt) Public member functions SetDescription() mutator & GetDescription() accessor (2 pts) PrintItemCost() - Outputs the item name followed by the quantity, price, and subtotal PrintItemDescription() - Outputs the item name and description Private data members string itemDescription -...
Create a ShoppingCart class in java that simulates the operation of a shopping cart. The ShoppingCart...
Create a ShoppingCart class in java that simulates the operation of a shopping cart. The ShoppingCart instance should contain a BagInterface implementation that will serve to hold the Items that will be added to the cart. Use the implementation of the Item object provided in Item.java. Note that the price is stored as the number of cents so it can be represented as an int (e.g., an Item worth $19.99 would have price = 1999). Your shopping cart should support...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT