Question: Create a class called Item. Item will have 3 attributes: price ( a float containing the value of the item ) , qunatity ( a

Create a class called Item. Item will have 3 attributes: price (a float containing the value of the
item), qunatity (a non-negative integer specifying how many of that item are present), and
description(a string that will describe what the item is).
Create a class called Cart. Cart will have 2 attributes: basket (a list of Items that are currently in
the cart) and total (a float that represents the total price of all items in the cart combined).
Write a function add_to_cart that takes a Cart object and an Item object and adds the Item to the Cart.
(For instance add 3 kiwis to my cart). This function should be aware if the Item decription is already
present in the cart then just increase the quantity of the Item in the cart by the Quantity of the
passed Item.
Write a function remove_from_cart that takes a Car object, an Item.description and an amount. If the item
is present in the cart remove that many of said Item. If the amount is bigger than the quanitity of
that item in the cart then jsut remove all quantity of that item, do not have a negative quantity. If
The item does not exist in the carat tell the user that that item does not exist in the cart.
Write a funciton print_cart that uses fstrings to print out the contents of the cart.basket. There should
be three columns printed: Description, Quntity, Price. Center the columns and subsequent rows within
20 blank spaces each. For each item display the descirption, quantity, and price*quantity in line
with the columns. After the table, output a the total price of all Items in the cart.
Create an instance of the Class cart and add the following items to it in this order:
5 Apple for $0.79 each
1 Milk for $3.62
1 Dozen Eggs for $2.66
21lb Bacon for $7.48 each
1 Apple for $0.79 each
Remove one 1lb Bacon from the Cart.
Display the contents of the cart using print_cart

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!