Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This program extends the earlier \"Online shopping cart\" program. (Consider fir

ID: 3813088 • Letter: T

Question

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:

(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)

Methodsadd_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:


Ex. of print_descriptions() output:


(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.


(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:


(5) Implement Output shopping cart menu option. (3 pts)

Ex:


(6) Implement Output item's description menu option. (2 pts)

Ex.


(7) Implement Add item to cart menu option. (3 pts)

Ex:


(8) Implement remove item menu option. (4 pts)

Ex:


(9) Implement Change item quantity menu option. Hint: Make new ItemToPurchase object before using ModifyItem() method. (5 pts)

Ex:

My current code is

class ItemToPurchase:
def __init__(self,name = "none",price = 0,quantity = 0):
self.item_name = name
self.item_price = price
self.item_quantity = quantity
def print_item_cost(self):
print (self.item_name, self.item_quantity,"@ $",self.item_price, " = $",self.item_quantity*self.item_price)

print ("Item 1")
n1 = str(input("Enter the item name:"))
p1 = float(input("Enter the item price:"))
q1 = float(input("Enter the item quantity"))
print ("Item 2")
n2 = str(input("Enter the item name:"))
p2 = float(input("Enter the item price:"))
q2 = float(input("Enter the item quantity"))
i1 = ItemToPurchase(n1,p1,q1)
i2 = ItemToPurchase(n2,p2,q2)
print ("TOTAL COST")
i1.print_item_cost()
i2.print_item_cost()
print ("Total:$",((i1.item_price*i1.item_quantity)+(i2.item_price*i2.item_quantity)))

Explanation / Answer

# 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 self.item_description = item_description def print_item_cost(self): string = '{} {} @ ${} = ${}'.format(self.item_name, self.item_quantity, self.item_price, (self.item_quantity * self.item_price)) cost = self.item_quantity * self.item_price return string, cost def print_item_description(self): string = '{}: {}'.format(self.item_name, self.item_description) print(string, end=' ') return string 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(' ADD ITEM TO CART', end=' ') item_name = str(input('Enter the item name: ')) item_description = str(input(' Enter the item description: ')) item_price = int(input(' Enter the item price: ')) item_quantity = int(input(' Enter the item quantity: ')) self.cart_items.append(ItemToPurchase(item_name, item_price, item_quantity, item_description)) def remove_item(self): print(' REMOVE ITEM FROM CART', end=' ') 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 def modify_item(self): print(' CHANGE ITEM QUANTITY', end=' ') name = str(input('Enter the item name: ')) quantity = int(input('Enter the new quantity: ')) for item in self.cart_items: if(item.item_name == name): item.item_quantity = quantity def get_num_items_in_cart(self): num_items = len(self.cart_items) 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(' OUTPUT ITEMS' DESCRIPTIONS') print('{}'s Shopping Cart - {}'.format(self.customer_name, self.current_date),end=' ') print(' Item Descriptions', end=' ') for item in self.cart_items: print('{}: {}'.format(item.item_name, item.item_description), end=' ') def output_cart(self): print(' OUTPUT SHOPPING CART', end=' ') print('{}'s Shopping Cart - {}'.format(self.customer_name, self.current_date),end=' ') print('Number of Items:', len(self.cart_items), end=' ') 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=' ') tc += (item.item_quantity * item.item_price) print(' Total: ${}'.format(tc), end=' ') def print_menu(ShoppingCart): customer_Cart = newCart menu = (' 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 ') command = '' while(command != 'q'): string='' print(menu, end=' ') command = input('Choose an option: ') while(command != 'a' and command != 'o' and command != 'i' and command != 'r' and command != 'c' and command != 'q'): command = input('Choose an option: ') 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() if __name__ == "__main__": # Type main section of code here customer_name = str(input('Enter customer's name: ')) current_date = str(input(' Enter today's date: ')) print('Customer name:', customer_name, end=' ') print('Today's date:', current_date, end=' ') newCart = ShoppingCart(customer_name, current_date) print_menu(newCart)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote