Ejemplo n.º 1
0
    def purchase(self, customername, itemname, qty, phone_number=None):
        ## Find or create new customer
        customer_idx = self.find_customer_in_store(customername)
        if customer_idx == -1:
            customer = Customer(customername, phone_number)
            self.customers.append(customer)
        else:
            customer = self.customers[customer_idx]

        ## Find the item they want to buy
        idx = self.find_item_in_store(itemname)


        if  idx != -1:         # check if the itemname is already in stock, increment the quantity of the itemname
            self.items[idx].purchase(qty)
            transaction = Transaction(customer, self.items[idx], qty)
            customer.add_dollars_spent(self.items[idx].price * qty)
            self.transactions.append(transaction)
        else:         # not if stock, create the itemname object and add it to our stock.
            raise ValueError("Trying to buy an itemname that is not in our store!")