Question: Python 3.6 IDE and above Question 3a. Create a class that satisfy the following specifications. Class name ShoppingCart Class attribute server url: str Instance attributes


Python 3.6 IDE and above
Question 3a. Create a class that satisfy the following specifications. Class name ShoppingCart Class attribute server url: str Instance attributes account id: str cart: dict Parameter account id: str Detailed information Assign the str "128.123.123.0" to the class attribute, server_url. The value of the instance attribute, account_id, should be assigned with the parameter of the same name. The instance attribute, cart, is an empty dictionary when a Shopping Cart object is instantiated. The keys in the dictionary are the product names, of str types, while the values, of int type, mark the quantity of the products in the cart. Question 3b Write an instance method for the Shopping Cart class that satisfies the following specifications. Method name add item_to_cart Parameter 1. product_name: 2. quantity: int Return value Detailed description The function should check if the item exists in the keys of the cart attribute and update the number of the items in the cart accordingly. str That is, if the item does not exist in the cart, it should create a key-value pair for the car. Otherwise, it should add the quantity in the parameter to the existing value. You may assume that the quantity in the parameter will be more than 0. Question 3c Write an instance method for the Shopping Cart class that satisfies the following specifications Method name remove_item_to_cart Parameter 1. product_name: str 2. quantity: int Return value Detailed description The function should update the cart attribute with the correct number of product. If the updated quantity is 0, the key value pair should be removed from the cart. You can assume that the product is available for removal and the quantity to be removed is less than or equal to the quantity in the cart Question 3e Write a class method for the Shopping Cart class that satisfies the following specifications. Method name get_url Parameter Return value 1. A str Detailed description This method returns the class attribute server_url Question 3f Write an instance method for the Shopping Cart class that satisfies the following specifications. empty cart Method name Parameter Return value Detailed description This method empties the instance attribute, cart Question 3g Write an instance method for the Shopping Cart class that satisfies the following specifications. Method name count items Parameter Return value 1. int Detailed description This method returns the total number of items based on the existing items in the cart. If there is nothing in the cart, the return value should be 0. An example of how your code should work given the following parameters. newCart = ShoppingCart ('1234567') newCart.add_item_to_cart ('fruit juice', 2) newCart.add_item_to_cart ('tissue box', 4) newCart.add_item_to_cart ('ice cream', 3) # newCart.cart is now t'fruit juice': 2, 'tissue box': 4, 'ice cream': 3} newCart.remove_item_from_cart('tissue box',1) newCart.remove_item_from_cart ('fruit juice',2) # newCart.cart is now {'tissue box': 3, 'ice cream': 3} newCart.count_items() # returns 6 print (newCart.get_url()) # prints 128.123.123.0 newCart.empty_cart() newCart.count_items() # returns 0
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
