Home > Software engineering >  How do I modify the quantity of an item in a list?
How do I modify the quantity of an item in a list?

Time:08-12

I have to write a code that modifies the quantity of an item in a shopping cart. Initially, I have a class names ItemToPurhcase that takes in the item's name, quantity, price and description. Under ShoppingCart class, function modify_item() modifies an item's quantity. Has a parameter of type ItemToPurchase. Does not return anything. If item can be found (by name) in cart, modify item in cart. If item cannot be found (by name) in cart, output this message: Item not found in cart. Nothing modified. My code so far:

class ItemToPurchase:
    def __init__(self, name="none", price=0, quantity=0, descrip='none'):
        self.name = name
        self.quantity = quantity
        self.price = price
        self.descrip = descrip

    def print_item_cost():
        print(self.name   ' '   str(self.quantity)   '@ $'   str(self.price)   '= $'   str(self.price * self.quantity))

    def print_item_description(ItemToPurchase):
        print(self.name   ':'   str(self.descrip)

class ShoppingCart:
    def __init__(self, customer_name='none', current_date='January 1, 2016', cart_items):
        self.customer_name = customer_name
        self.currend_date = current_date
        self.cart_items = []

    def add_item(self, ItemToPurchase):
        self.cart_items.append(ItemToPurchase)
        
    def remove_item(self, item_name):
        while item_name in self.cart_items:
            del self.cart_items[item_name]
        if item_name not in self.cart_items:
            print('Item not found in cart. Nothing removed')

    def modify_item(self, ItemToPurchase):
        if name in self.cart_items:

I need help completing the function.

CodePudding user response:

I would make your code look like this:

def modify_item(self, item: ItemToPurchase):
    item_names = [item.name for item in self.cart_items]
    if item.name in item_names:
        spot = item_names.index(item.name)
        self.cart_items[spot] = item
    else:
        return "Item not found in cart. Nothing modified."

The item parameter takes an instance of the ItemToPurchase Class, and then the function uses list comprehension to find the names of all of the items in the cart. Then it checks if the name of the item to be modified is already in the cart before it finds what spot in the list that item is at and then changes the item. Otherwise, it returns "Item not found in cart. Nothing modified."

CodePudding user response:

The shopping cart doesn't contain names, it contains ItemToPurchase. So you can't just check if the name is in the list, you have to check whether any of the items in the list has a .name attribute that matches.

The modify_item function needs to take the new quantity as a parameter. Then it can loop through the cart items and update the quantity.

def remove_item(self, item_name):
    old_len = len(self.cart_items)
    self.cart_items = [item for item in self.cart_items if item.name != item_name]
    if len(self.cart_items) == old_len:
        print('Item not found in cart. Nothing removed')

def modify_item(self, item_name, new_quantity):
    for item in enumerate(cart_items):
        if item.name == item_name:
            item.quantity = new_quantity
            break
    else:
        print("Item not found in cart. Nothing modified")

CodePudding user response:

I also edited the rest of your code to get rid of the rest of the bugs:

class ItemToPurchase:
    def __init__(self, name="none", price=0, quantity=0):
        self.name = name
        self.quantity = quantity
        self.price = 0
        self.description = 'none'

    def set_description(self, descrip):
        self.description = descrip

    def print_item_description(self):
        print(self.name   ':'   str(self.description))

    def print_item_cost(self):
        print(self.name   ' '   str(self.quantity)   '@ $'   str(self.price)   '= $'   str(self.price * self.quantity))


class ShoppingCart:
    def __init__(self, cart_items: list, customer_name='none', current_date='January 1, 2016'):
        self.customer_name = customer_name
        self.current_date = current_date
        self.cart_items = cart_items

    def add_item(self, new_item):
        self.cart_items.append(new_item)

    def remove_item(self, item_name):
        item_names = [item.name for item in self.cart_items]
        if item_name in item_names:
            spot = item_names.index(item_name)
            del self.cart_items[spot]
        if item_name not in self.cart_items:
            print('Item not found in cart. Nothing removed')

    def modify_item(self, item: ItemToPurchase):
        item_names = [item.name for item in self.cart_items]
        if item.name in item_names:
            spot = item_names.index(item.name)
            self.cart_items[spot] = item
        else:
            return "Item not found in cart. Nothing modified."
  • Related