In: Computer Science
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) (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 |
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
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.