Python Forum
Want a solution to this - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Want a solution to this (/thread-1930.html)

Pages: 1 2


RE: Want a solution to this - Div - Mar-12-2018

This is what i tried, it should help
class ShoppingCart:
    items = {}
    totals = []
    def __init__(self, total=0):
        self.total = total
        self.items = {}

    def add_item(self, item_name, quantity, price):
        self.item_name = item_name
        self.quantity = quantity
        self.price = price
        self.total += price * quantity
        self.items.update({item_name : quantity})

    def remove_item(self, item_name, quantity, price):
        self.item_name = item_name
        self.quantity = quantity
        self.price = price
        if item_name in self.items:
            if quantity < self.items[item_name] and quantity > 0:
                self.items[item_name] -= quantity
                self.total -= price * quantity
        elif quantity >= self.items[item_name]:
            self.total -= price * self.items[item_name]
            del self.items[item_name]


    def checkout(self, cash_paid):
        self.cash_paid = cash_paid
        if cash_paid >= self.total:
            return cash_paid - self.total
        return "Cash paid not enough"



class Shop(ShoppingCart):
    def __init__(self):
        self.quantity = 100

    def remove_item(self):
        self.quantity -= 1