Zybooks 11.12 LAB*: Program: Online shopping cart (continued)
Python 3 is the code needed and this is in Zybooks
Existing Code
# Type code for classes here
class ItemToPurchase:
def __init__(self, item_name="none", item_price=0,
item_quantity=0):
self.item_name = item_name
self.item_price = item_price
self.item_quantity = item_quantity
# def __mul__(self):
# print_item_cost = (self.item_quantity * self.item_price)
# return '{} {} @ ${} = ${}' .format(self_item_name,
self.item_quantity, self.item_price, print_item_cost)
def print_item_cost(self):
self.print_cost = (self.item_quantity * self.item_price)
print(('{} {} @ ${} = ${}') .format(self.item_name,
self.item_quantity, self.item_price, self.print_cost))
def main():
print('Item 1')
name = input('Enter the item name:\n')
price = int(input('Enter the item price:\n'))
quantity = int(input('Enter the item quantity:\n\n'))
item1 = ItemToPurchase(name, price, quantity)
print('Item 2')
name = input('Enter the item name:\n')
price = int(input('Enter the item price:\n'))
quantity = int(input('Enter the item quantity:\n\n'))
item2 = ItemToPurchase(name, price, quantity)
print('TOTAL COST')
item1.print_item_cost()
item2.print_item_cost()
print('\nTotal: $%s' % (item1.print_cost + item2.print_cost))
if __name__ == "__main__":
main()
Zybooks 11.12 LAB*: Program: Online shopping cart (continued)
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. Note: Some can be method stubs (empty
methods) initially, to be completed in later steps.
Parameterized constructor which takes the customer name and date as parameters (2 pts)
Attributes
customer_name (string) - Initialized in default constructor to "none"
current_date (string) - Initialized in default constructor to "January 1, 2016"
cart_items (list)
Methods
add_item()
Adds an item to cart_items list. Has parameter ItemToPurchase. Does not return anything.
remove_item()
Removes item from cart_items list. Has a string (an item's name) parameter. Does not return anything.
If item name cannot be found, output this message: Item not found in cart. Nothing removed.
modify_item()
Modifies an item's description, price, and/or quantity. Has parameter ItemToPurchase. Does not return anything.
If item can be found (by name) in cart, check if parameter has default values for description, price, and quantity. If not, modify
item in cart.
If item cannot be found (by name) in cart, output this message: Item not found in cart. Nothing modified.
get_num_items_in_cart() (2 pts)
Returns quantity of all items in cart. Has no parameters.
get_cost_of_cart() (2 pts)
Determines and returns the total cost of items in cart. Has no parameters.
print_total()
Outputs total of objects in cart.
If 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
(3) In 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 customer's name:
John Doe
Enter today's date:
February 1, 2016
Customer name: John Doe
Today's date: February 1, 2016 (
4) 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 the 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. (3 pts)
Ex:
MENU
a - Add item to cart
r - Remove item from cart
c - Change item quantity
i - Output items' descriptions
o - Output shopping cart
q - Quit
Choose an option:
(5) Implement Output shopping cart menu option. (3 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
(6) Implement Output item's description menu option. (2 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
(7) Implement Add item to cart menu option. (3 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
(8) Implement remove item menu option. (4 pts)
Ex:
REMOVE ITEM FROM CART
Enter name of item to remove:
Chocolate Chips
(9) Implement Change item quantity menu option. Hint: Make new ItemToPurchase object before using ModifyItem() method. (5 pts)
Ex:
CHANGE ITEM QUANTITY
Enter the item name:
Nike Romaleos
Enter the new quantity:
3
Code that I have which has errors: What am I missing?
# Type code for classes here class ItemToPurchase: def __init__(self, item_name="none", item_price=0, item_quantity=0,item_description="none"): self.item_name = item_name self.item_price = item_price self.item_quantity = item_quantity # item_description attribute to store item's description, initialize it to "none" self.item_description = item_description # def __mul__(self): # print_item_cost = (self.item_quantity * self.item_price) # return '{} {} @ ${} = ${}' .format(self_item_name, self.item_quantity, self.item_price, print_item_cost) def print_item_cost(self): self.print_cost = (self.item_quantity * self.item_price) print(('{} {} @ ${} = ${}') .format(self.item_name, self.item_quantity, self.item_price, self.print_cost)) # prints item name followed by its description def print_item_description(self): print(self.item_name,end=": ") print(self.item_description) class ShoppingCart: def __init__(self, customer_name = 'none', current_date = 'January 1, 2016', cart_items = []): self.customer_name = customer_name self.current_date = current_date self.cart_items = cart_items def add_item(self, string): print('\nADD ITEM TO CART', end='\n') item_name = str(input('Enter the item name:')) item_description = str(input('\nEnter the item description:')) item_price = int(input('\nEnter the item price:')) item_quantity = int(input('\nEnter the item quantity:\n')) self.cart_items.append(ItemToPurchase(item_name, item_price, item_quantity, item_description)) def remove_item(self): print('REMOVE ITEM FROM CART', end='\n') string = str(input('Enter name of item to remove:')) i = 0 for item in self.cart_items: if(item.item_name == string): del self.cart_items[i] i += 1 flag=True break else: flag=False if(flag==False): print('\nItem not found in cart. Nothing removed.') def modify_item(self): print('\nCHANGE ITEM QUANTITY', end='\n') name = str(input('Enter the item name:')) for item in self.cart_items: if(item.item_name == name): quantity = int(input('Enter the new quantity:')) item.item_quantity = quantity flag=True break else: flag=False if(flag==False): print('Item not found in cart. Nothing modified') def get_num_items_in_cart(self): num_items=0 for item in self.cart_items: num_items= num_items+item.item_quantity return num_items def get_cost_of_cart(self): total_cost = 0 cost = 0 for item in self.cart_items: cost = (item.item_quantity * item.item_price) total_cost += cost return total_cost def print_total(): total_cost = get_cost_of_cart() if (total_cost == 0): print('SHOPPING CART IS EMPTY') else: output_cart() def print_descriptions(self): print('\nOUTPUT ITEMS\' DESCRIPTIONS') print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.current_date),end='\n') print('\nItem Descriptions', end='\n') for item in self.cart_items: print('{}: {}'.format(item.item_name, item.item_description), end='\n') def output_cart(self): new=ShoppingCart() print('OUTPUT SHOPPING CART', end='\n') print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.current_date),end='\n') print('Number of Items:', new.get_num_items_in_cart(), end='\n\n') tc = 0 for item in self.cart_items: print('{} {} @ ${} = ${}'.format(item.item_name, item.item_quantity, item.item_price, (item.item_quantity * item.item_price)), end='\n') tc += (item.item_quantity * item.item_price) print('\nTotal: ${}'.format(tc), end='\n') def print_menu(ShoppingCart): customer_Cart = newCart string='' menu = ('\nMENU\n' 'a - Add item to cart\n' 'r - Remove item from cart\n' 'c - Change item quantity\n' 'i - Output items\' descriptions\n' 'o - Output shopping cart\n' 'q - Quit\n') command = '' while(command != 'q'): string='' print(menu, end='\n') command = input('Choose an option:\n') while(command != 'a' and command != 'o' and command != 'i' and command != 'r' and command != 'c' and command != 'q'): command = input('Choose an option:\n') if(command == 'a'): customer_Cart.add_item(string) if(command == 'o'): customer_Cart.output_cart() if(command == 'i'): customer_Cart.print_descriptions() if(command == 'r'): customer_Cart.remove_item() if(command == 'c'): customer_Cart.modify_item() customer_name = str(input('Enter customer\'s name:')) current_date = str(input('\nEnter today\'s date:\n')) print('\nCustomer name:', customer_name, end='\n') print('Today\'s date:', current_date, end='\n') newCart = ShoppingCart(customer_name, current_date) print_menu(newCart)
Screenshot
Program
# Create a class ItemToPurchase
class ItemToPurchase:
#constructor
def __init__(self, item_name="none",
item_price=0, item_quantity=0,item_description="none"):
self.item_name =
item_name
self.item_price =
item_price
self.item_quantity =
item_quantity
# item_description
attribute to store item's description, initialize it to
"none"
self.item_description =
item_description
#Print cost of an item
def print_item_cost(self):
self.print_cost =
(self.item_quantity * self.item_price)
print(('{} {} @ ${} =
${}') .format(self.item_name,self.item_quantity, self.item_price,
self.print_cost))
# prints item name followed by its
description
def print_item_description(self):
print(self.item_name,end=": ")
print(self.item_description)
#Create a class ShoppingCart
class ShoppingCart:
#Constructor
def __init__(self, customer_name = 'none',
current_date = 'January 1, 2016', cart_items = []):
self.customer_name =
customer_name
self.current_date =
current_date
self.cart_items =
cart_items
#Add an item into cart
def add_item(self, string):
print('\nADD ITEM TO
CART', end='\n')
item_name =
str(input('Enter the item name:'))
item_description =
str(input('\nEnter the item description:'))
item_price =
float(input('\nEnter the item price:'))
item_quantity =
int(input('\nEnter the item quantity:\n'))
self.cart_items.append(ItemToPurchase(item_name, item_price,
item_quantity, item_description))
#Remove an item from the cart
def remove_item(self):
print('REMOVE ITEM FROM
CART', end='\n')
string =
str(input('Enter name of item to remove:'))
i = 0
for item in
self.cart_items:
if(item.item_name.capitalize() ==
string.capitalize()):
del self.cart_items[i]
i += 1
flag=True
break
else:
flag=False
if(flag==False):
print('\nItem not found in cart. Nothing removed.')
#modify the cart
def modify_item(self):
print('\nCHANGE ITEM
QUANTITY', end='\n')
name = str(input('Enter
the item name:'))
for item in
self.cart_items:
if(item.item_name.capitalize() == name.capitalize()):
quantity = int(input('Enter the new quantity:'))
item.item_quantity = quantity
flag=True
break
else:
flag=False
if(flag==False):
print('Item not found in cart. Nothing modified')
#Get number of items in the cart
def get_num_items_in_cart(self):
num_items=0
for item in
self.cart_items:
num_items= num_items+item.item_quantity
return num_items
#Get total cost of the cart items
def get_cost_of_cart(self):
total_cost = 0
cost = 0
for item in
self.cart_items:
cost = (item.item_quantity * item.item_price)
total_cost += cost
return total_cost
#Print total cart details
def print_total():
total_cost =
get_cost_of_cart()
if (total_cost ==
0):
print('SHOPPING CART IS EMPTY')
else:
output_cart()
#Get details of items in the cart
def print_descriptions(self):
if(len(self.cart_items)==0):
print('SHOPPING CART IS EMPTY')
else:
print('\nOUTPUT ITEMS\' DESCRIPTIONS')
print('{}\'s Shopping Cart - {}'.format(self.customer_name,
self.current_date),end='\n')
print('\nItem Descriptions', end='\n')
for item in self.cart_items:
item.print_item_description()
#Out put cart
def output_cart(self):
if(len(self.cart_items)==0):
print('SHOPPING CART IS EMPTY')
else:
new=ShoppingCart()
print('OUTPUT SHOPPING CART', end='\n')
print('{}\'s Shopping Cart - {}'.format(self.customer_name,
self.current_date),end='\n')
print('Number of Items:', new.get_num_items_in_cart(),
end='\n\n')
tc = 0
for item in self.cart_items:
item.print_item_cost()
tc += (item.item_quantity * item.item_price)
print('\nTotal: ${}'.format(tc), end='\n')
#Method to print user menu
def print_menu(ShoppingCart):
customer_Cart = newCart
string=''
menu = ('\nMENU\n'
'a - Add item to cart\n'
'r - Remove item from cart\n'
'c - Change item quantity\n'
'i - Output items\' descriptions\n'
'o - Output shopping cart\n'
'q - Quit\n')
command = ''
while(command != 'q'):
string=''
print(menu,
end='\n')
command = input('Choose
an option:\n')
#error check
while(command != 'a' and
command != 'o' and command != 'i' and command != 'r'
and command != 'c' and command != 'q'):
command = input('Choose an option:\n')
if(command ==
'a'):
customer_Cart.add_item(string)
if(command ==
'o'):
customer_Cart.output_cart()
if(command ==
'i'):
customer_Cart.print_descriptions()
if(command ==
'r'):
customer_Cart.remove_item()
if(command ==
'c'):
customer_Cart.modify_item()
#Prompt for customer name and purchase date
customer_name = str(input('Enter customer\'s name:'))
current_date = str(input('\nEnter today\'s date:\n'))
print('\nCustomer name:', customer_name, end='\n')
print('Today\'s date:', current_date, end='\n')
newCart = ShoppingCart(customer_name, current_date)
print_menu(newCart)
------------------------------------------------------------------
Output
Enter customer's name:Tiffany Kerg
Enter today's date:
July, 05,2019
Customer name: Tiffany Kerg
Today's date: July, 05,2019
MENU
a - Add item to cart
r - Remove item from cart
c - Change item quantity
i - Output items' descriptions
o - Output shopping cart
q - Quit
Choose an option:
o
SHOPPING CART IS EMPTY
MENU
a - Add item to cart
r - Remove item from cart
c - Change item quantity
i - Output items' descriptions
o - Output shopping cart
q - Quit
Choose an option:
i
SHOPPING CART IS EMPTY
MENU
a - Add item to cart
r - Remove item from cart
c - Change item quantity
i - Output items' descriptions
o - Output shopping cart
q - Quit
Choose an option:
a
ADD ITEM TO CART
Enter the item name:Chocolate
Enter the item description:Strawberry filled
Enter the item price:4.5
Enter the item quantity:
5
MENU
a - Add item to cart
r - Remove item from cart
c - Change item quantity
i - Output items' descriptions
o - Output shopping cart
q - Quit
Choose an option:
a
ADD ITEM TO CART
Enter the item name:Buiscuit
Enter the item description:Cocco filled
Enter the item price:6.45
Enter the item quantity:
10
MENU
a - Add item to cart
r - Remove item from cart
c - Change item quantity
i - Output items' descriptions
o - Output shopping cart
q - Quit
Choose an option:
o
OUTPUT SHOPPING CART
Tiffany Kerg's Shopping Cart - July, 05,2019
Number of Items: 15
Chocolate 5 @ $4.5 = $22.5
Buiscuit 10 @ $6.45 = $64.5
Total: $87.0
MENU
a - Add item to cart
r - Remove item from cart
c - Change item quantity
i - Output items' descriptions
o - Output shopping cart
q - Quit
Choose an option:
i
OUTPUT ITEMS' DESCRIPTIONS
Tiffany Kerg's Shopping Cart - July, 05,2019
Item Descriptions
Chocolate: Strawberry filled
Buiscuit: Cocco filled
MENU
a - Add item to cart
r - Remove item from cart
c - Change item quantity
i - Output items' descriptions
o - Output shopping cart
q - Quit
Choose an option:
c
CHANGE ITEM QUANTITY
Enter the item name:chocolate
Enter the new quantity:10
MENU
a - Add item to cart
r - Remove item from cart
c - Change item quantity
i - Output items' descriptions
o - Output shopping cart
q - Quit
Choose an option:
o
OUTPUT SHOPPING CART
Tiffany Kerg's Shopping Cart - July, 05,2019
Number of Items: 20
Chocolate 10 @ $4.5 = $45.0
Buiscuit 10 @ $6.45 = $64.5
Total: $109.5
MENU
a - Add item to cart
r - Remove item from cart
c - Change item quantity
i - Output items' descriptions
o - Output shopping cart
q - Quit
Choose an option:
i
OUTPUT ITEMS' DESCRIPTIONS
Tiffany Kerg's Shopping Cart - July, 05,2019
Item Descriptions
Chocolate: Strawberry filled
Buiscuit: Cocco filled
MENU
a - Add item to cart
r - Remove item from cart
c - Change item quantity
i - Output items' descriptions
o - Output shopping cart
q - Quit
Choose an option:
r
REMOVE ITEM FROM CART
Enter name of item to remove:buiscuit
MENU
a - Add item to cart
r - Remove item from cart
c - Change item quantity
i - Output items' descriptions
o - Output shopping cart
q - Quit
Choose an option:
o
OUTPUT SHOPPING CART
Tiffany Kerg's Shopping Cart - July, 05,2019
Number of Items: 10
Buiscuit 10 @ $6.45 = $64.5
Total: $64.5
MENU
a - Add item to cart
r - Remove item from cart
c - Change item quantity
i - Output items' descriptions
o - Output shopping cart
q - Quit
Choose an option:
i
OUTPUT ITEMS' DESCRIPTIONS
Tiffany Kerg's Shopping Cart - July, 05,2019
Item Descriptions
Buiscuit: Cocco filled
MENU
a - Add item to cart
r - Remove item from cart
c - Change item quantity
i - Output items' descriptions
o - Output shopping cart
q - Quit
Choose an option:
q
-----------------------------------------------------
Note:-
What error are you getting.
I changed the display part of the shoppng cart and add conditions and avoid case sensitivities.
If you have any doubts, let me know
Zybooks 11.12 LAB*: Program: Online shopping cart (continued) Python 3 is the code needed and this...
Hi, can someone offer input on how to address these 4 remain parts the zybook python questions? 4: Tests that get_num_items_in_cart() returns 6 (ShoppingCart) Your output ADD ITEM TO CART Enter the item name: Traceback (most recent call last): File "zyLabsUnitTestRunner.py", line 10, in <module> passed = test_passed(test_passed_output_file) File "/home/runner/local/submission/unit_test_student_code/zyLabsUnitTest.py", line 8, in test_passed cart.add_item(item1) File "/home/runner/local/submission/unit_test_student_code/main.py", line 30, in add_item item_name = str(input('Enter the item name:\n')) EOFError: EOF when reading a line 5: Test that get_cost_of_cart() returns 10 (ShoppingCart)...
11.12 LAB*: Program: Online shopping cart (continued)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 constructorImplement 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. Note: Some can be method stubs...
7.11 LAB: Online shopping cart - Part 2 This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program). (1) Extend the ItemToPurchase namedtuple to contain a new attribute. (2 pts) item_description (string) - Set to "none" in the construct_item() function Implement the following function with an ItemToPurchase as a parameter. print_item_description() - Prints item_name and item_description attribute for an ItemToPurchase namedtuple. Has an ItemToPurchase parameter. Ex. of print_item_description() output: Bottled Water: Deer Park, 12 oz....
Can someone please help me with this Python code? Thank you in advance, Zybooks keeps giving me errors. Thanks in advance!! 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...
11.11 LAB: Warm up: Online shopping cart (1) Build the ItemToPurchase class with the following specifications: Attributes (6 pts) item_name (string) item_price (float) item_quantity (int) Default constructor (2 pt) Initializes item's name = "none", item's price = 0, item's quantity = 0 Method print_item_cost() Ex. of print_item_cost() output: Bottled Water 10 @ $1 = $10 (2) In the main section of your code, prompt the user for two items and create two objects of the ItemToPurchase class. (4 pts) Ex:...
8.7 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 fieldsstring itemDescription - Initialized in default constructor to "none"Parameterized...
4.18 Ch 7 Program: Online shopping cart (continued) (C++) This program extends the earlier "Online shopping cart" program. (solution from previous lab assignment is provided in Canvas). (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() -...
Ch 7 Program: Online shopping cart (continued) (Java)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 fieldsstring itemDescription - Initialized in default constructor to "none"Parameterized constructor to assign item name, item description, item price, and item quantity (default values of 0). (1 pt)Public member methodssetDescription() mutator & getDescription() accessor (2 pts)printItemCost() - Outputs the item name followed by the quantity, price, and subtotalprintItemDescription() - Outputs the...
Python3 programming help needed LAB*: Program: Online shopping cart (continued) Need the below code edited to run properly for the lab.. class ItemPurchase: def __init__(self, nameitem='none', item_prc=0, item_quntity=0, item_descrp = 'none'): self.nameitem = nameitem self.item_prc = item_prc self.item_quntity = item_quntity self.item_descrp = item_descrp def print_itemvaluecost(self): string = '{} {} @ ${} = ${}'.format(self.nameitem, self.item_quntity, self.item_prc(self.item_quntity * self.item_prc)) valuecost = self.item_quntity * self.item_prc return string, valuecost def print_itemdescription(self): string...
Online shopping cart (continued) (Java) Hello, I need help with Java to figure this out. In my Shopping Cart Manager Class (Bottom Code), I get "Resource leak: 'sc' is never closed." I have tried multiple things and cannot figure it out. Thank you. Online shopping cart (continued) (Java) Hello, I need help with Java to figure this out. In my Shopping Cart Manager Class (Bottom Code), I get "Resource leak: 'sc' is never closed." I have tried multiple things and...