Question

In: Computer Science

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 the cart_items list. Has parameter ItemToPurchase. Does not return anything.

○ get_cost_of_cart()

■ Determines and returns the total cost of items in the cart. Has no parameters.

○ print_total()

  • Outputs total of objects in the cart.

  • If the cart is empty, output this message:

SHOPPING CART IS EMPTY

○ print_descriptions()

■ Outputs each item's description.

Ex. of print_total() output:

John Doe's Shopping Cart - February 1, 2016
Number of Items: 8
Nike Romaleos 2 @ $189 = $378
Chocolate Chips 5 @ $3 = $15
Powerbeats 2 Headphones 1 @ $128 = $128

Total: $521

Ex. of print_descriptions() output:

John Doe's Shopping Cart - February 1, 2016

Item Descriptions
Nike Romaleos: Volt color, Weightlifting shoes Chocolate Chips: Semi-sweet
Powerbeats 2 Headphones: Bluetooth headphones

1

(2) In the main section of your code, prompt the user for a customer's name and today's date. Output the name and date. Create an object of type ShoppingCart. (1 pt)

Ex.

Enter the customer's name:
John Doe
Enter today's date:
February 1, 2016
Customer name: John Doe
Today's date: February 1, 2016

(3) Implement the print_menu() function. print_menu() has a ShoppingCart parameter and outputs a menu of options to manipulate the shopping cart. Each option is represented by a single character. Build and output the menu within the function.

If an invalid character is entered, continue to prompt for a valid choice. Hint: Implement Quit before implementing other options. Call print_menu() in the main() function. Continue to execute the menu until the user enters q to Quit. (2 pts)
-Hint: this is the same type of menu that we have done for a few different labs now ;-)
Ex:
MENU
a - Add item to cart
i - Output items' descriptions
o - Output shopping cart
q - Quit
Choose the option:

(4) Implement Output shopping cart menu option. (1 pts)

Ex:

OUTPUT SHOPPING CART
John Doe's Shopping Cart - February 1, 2016
Number of Items: 8
Nike Romaleos 2 @ $189 = $378
Chocolate Chips 5 @ $3 = $15
Powerbeats 2 Headphones 1 @ $128 = $128

Total: $521

(5) Implement Output item's description menu option. (1 pts) Ex.

OUTPUT ITEMS' DESCRIPTIONS
John Doe's Shopping Cart - February 1, 2016

Item Descriptions
Nike Romaleos: Volt color, Weightlifting shoes Chocolate Chips: Semi-sweet
Powerbeats 2 Headphones: Bluetooth headphones

2

(6) Implement Add item to cart menu option. (2 pts)

Ex:

ADD ITEM TO CART
Enter the item name:
Nike Romaleos
Enter the item description:
Volt color, Weightlifting shoes
Enter the item price:
189
Enter the item quantity:
2

Solutions

Expert Solution

Code to Copy:

#class item to store the details of an item
class Item:
   #constructor
   def __init__(self, name, description, price, quantity):
       #initialize all the values
       self.name = name
       self.description = description
       self.price = price
       self.quantity = quantity
class ShoppingCart:
   #constructor
   def __init__(self, name, data):
       #initialize name and date to given values
       self.name = name
       self.date = date
       #set the items to empty list
       self.items = []
       #set the number of items to 0
       self.num_items = 0
   #method to add an item to shopping cart
   def add_item(self, itemToPurchase):
       #append the item to the list
       self.items.append(itemToPurchase)
       #add the item quantity to number of items
       self.num_items += itemToPurchase.quantity
   #method to print the descriptions
   def print_descriptions(self):
       #if there are no items in the shopping cart
       if(len(self.items) == 0):
           print("SHOPPING CART IS EMPTY")
       else:
           print("Item Descrptions")
           print(self.name + "'s Shopping Cart - " + self.date)
           #iterate over the items
           for i in self.items:
               #print item name and description
               print(i.name + ": " + i.description)
   #method to print the details of items
   def print_total(self):
       #if there are no items in shopping cart
       if(len(self.items) == 0):
           print("SHOPPING CART IS EMPTY")
       else:
           print(self.name + "'s Shopping Cart - " + self.date)
           print("Number of items: " + str(self.num_items))
           total = 0
           #iterate over the items
           for i in self.items:
               #print the details of items
               print(i.name + " " + str(i.quantity) + " @ $" + str(i.quantity * i.price))
               #calculate the price and add it to total
               total += (i.quantity * i.price)
           #print total price
           print("total : $" + str(total))
#method to print the menu
def print_menu():
   print("Main")
   print("a - Add item to cart")
   print("i - Output items descriptions")
   print("o - Output shopping cart")
   print("q. quit")
if __name__ == "__main__":
   #prompt the user for name
   name = input("Enter customer name:\n")
   #prompt the user for date
   date = input("Enter today's date:\n")
   #Print name and date
   print("Customer name: " + name)
   print("Today's date: " + date)
   #create shopping cart object
   s = ShoppingCart(name, date)
   while True:
       #print the menu
       print_menu()
       #prompt the user to enter an option
       ch = input("Enter your choice: ")
       if(ch == 'a'):
           #prompt the user for item details
           i_name = input("Enter the item name:\n")
           description = input("Enter the item description:\n")
           price = float(input("Enter the item price:\n"))
           quantity = int(input("Enter the item quantity:\n"))
           #create object for item
           item = Item(i_name, description, price, quantity)
           #call the method to add the item ot shopping cart
           s.add_item(item)
       elif(ch == 'i'):
           #call the method to print the descriptions
           s.print_descriptions()
       elif(ch == 'o'):
           #call the method to print the details
           s.print_total()
       elif(ch == 'q'):
           print("Thank you for using the shopping cart")
           #break the loop
           break
       else:
           print("Invalid choice, please try again")
          
Code Images:

Output Images:

Follow the Code images for indentation.


Related Solutions

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....
IN C++ 7.22 LAB*: Program: Online shopping cart (Part 2) This program extends the earlier "Online...
IN C++ 7.22 LAB*: Program: Online shopping cart (Part 2) 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...
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 -...
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...
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...
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...
This week you will write a program that mimics an online shopping cart . You will...
This week you will write a program 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 for quantities -...
9.25 LAB*: Program: Online shopping cart (Part 2) Note: Creating multiple Scanner objects for the same...
9.25 LAB*: Program: Online shopping cart (Part 2) Note: Creating multiple Scanner objects for the same input stream yields unexpected behavior. Thus, good practice is to use a single Scanner object for reading input from System.in. That Scanner object can be passed as an argument to any methods that read input. This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program). (1) Extend the ItemToPurchase class per the following specifications: Private fields string itemDescription -...
Solve please in python b) Create a program that shows a series of numbers that start...
Solve please in python b) Create a program that shows a series of numbers that start at a and increase from 5 to 5 until reaching b, where a and b are two numbers captured by the user and assumes that a is always less than b. Note that a and b are not necessarily multiples of 5, and that you must display all numbers that are less than or equal to b. c) Create a program that displays n...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT