0

I have made this code and now I want to add from the class 'Product' the price together. So I have 2 products: Computer and Nintendo and I want to add the price together, can I make a definition for this so that from product 3 and 4 it will also add up? I hope my question makes sense, I'm a beginner in programming.

class Customer:
    def __init__(self, ID, name, address):
        self.ID = ID
        self.name = name
        self.address = address
    

    def customer_information(self):
        print('ID: '+ self.ID + ', Name: ' + self.name + ', Address: '+ self.address)

class Product:
    def __init__(self, product_name, product_ID, price):
        self.product_name = product_name
        self.product_ID = product_ID
        self.price = price

    def product_information(self):
        print(self.product_name+', '+self.product_ID + ', €'+str(self.price))

class Order:
    def __init__(self):
        self.customer = []
        self.product = []

    def add1(self, product):
        self.product.append(product)

    def customer_data(self, customer):
        self.customer.append(customer)
    

    def show(self):
        for c in self.customer:
            c.customer_information()
        print('This order contains:')
        for p in self.product:
            p.product_information()

customer1 = Customer('542541', 'Name', 'Rotterdam')
customer2 = Customer('445412', 'Name', 'Schiedam')
        
product1 = Product('Computer', '34456', 200.00)
product2 = Product('Nintendo', '12345', 14.99)
product3 = Product('Camera', '51254', 50.00)
product4 = Product('Go-pro', '51251', 215.00)


myOrder = Order()
myOrder.customer_data(customer1)
myOrder.add1(product1)
myOrder.add1(product2)


myOrder1 = Order()
myOrder1.customer_data(customer2)
myOrder1.add1(product3)
myOrder1.add1(product4)

myOrder.show()
myOrder1.show()
D Kramer
  • 21
  • 3
  • Sorry but no, your question doesn't make sense. When you say "a definition", what *kind* of definition are you talking about? A function definition? A class definition? I also don't understand what you want this unspecified definition to *do*. What is its input, and what's the expected output or result? – Aran-Fey Mar 09 '19 at 19:39
  • Possible duplicate of [Sum / Average an attribute of a list of objects in Python](https://stackoverflow.com/questions/10879867/sum-average-an-attribute-of-a-list-of-objects-in-python) – stovfl Mar 09 '19 at 19:44

2 Answers2

0

Yes, you can create another variable in class order like-

    def __init__(self):
        self.customer = []
        self.product = []
        self.total = 0

and add every product's price to the total whenever a product is added to the list-

    def add1(self, product):
        self.product.append(product) 
        self.total += product.price
0

Seems like you want to get the sum of all the product prices, or order totals. Both are the same result, but you have two classes that contain the same information so you can calculate the sums by either Product or Order:

productsum = product1.price + product2.price + product3.price + product4.price
ordersum = sum([p.price for p in myOrder.product]) + sum([p.price for p in myOrder1.product])


print(productsum) # 479.99
print(ordersum)   # 479.99

Either way you'll get the same answer, just choose how you want to implement it.