
class Queviures:

    def __init__(self, capital):
        self.capital = capital
        self.magatzem = {}

    def __getitem__(self, producte):
        return self.magatzem.get(producte, 0)

    def __len__(self):
        return len(self.magatzem)
    
    def comprar(self, producte, n, preu):
        cost = preu * n
        if self.capital >= cost:
            self.capital = self.capital - cost
            self.magatzem[producte] = self[producte] + n

    def vendre(self, producte, preu):
        if producte in self.magatzem:
            self.capital += preu * self.magatzem[producte]
            del self.magatzem[producte]

    def nombre_productes(self):
        return sum(self.magatzem.values())

