# In this file create unit tests such that there is 100% coverage class ShoppingCart: """ This class manages items in a shopping cart and provides various operations like adding items, calculating totals, and applying discounts. """ def __init__(self): """Initialize an empty shopping cart.""" self.items = [] def add_item(self, name, price, quantity=1): """ Add an item to the cart. Args: name (str): Name of the item price (float): Price per unit quantity (int): Number of items to add Raises: ValueError: If price is negative or quantity is not positive """ if price < 0: raise ValueError("Price cannot be negative") if quantity <= 0: raise ValueError("Quantity must be positive") self.items.append({"name": name, "price": price, "quantity": quantity}) def remove_item(self, name): """ Remove the first occurrence of an item by name. Args: name (str): Name of the item to remove Returns: bool: True if item was removed, False if not found """ for i, item in enumerate(self.items): if item["name"] == name: self.items.pop(i) return True return False def get_total(self): """ Calculate the total price of all items in the cart. Returns: float: Total price """ total = 0 for item in self.items: total += item["price"] * item["quantity"] return round(total, 2) def get_item_count(self): """ Get the total number of items in the cart. Returns: int: Total quantity of all items """ return sum(item["quantity"] for item in self.items) def is_empty(self): """ Check if the cart is empty. Returns: bool: True if cart is empty, False otherwise """ return len(self.items) == 0 def apply_discount(self, percentage): """ Apply a percentage discount to all items in the cart. Args: percentage (float): Discount percentage (0-100) Raises: ValueError: If percentage is not between 0 and 100 """ if percentage < 0 or percentage > 100: raise ValueError("Discount percentage must be between 0 and 100") discount_multiplier = 1 - (percentage / 100) for item in self.items: item["price"] = round(item["price"] * discount_multiplier, 2) def clear_cart(self): """Remove all items from the cart.""" self.items = [] def get_items(self): """ Get a copy of all items in the cart. Returns: list: List of item dictionaries """ return self.items.copy()