
Patrick B. answered 03/27/21
Math and computer tutor/teacher
I would recommend creating a class...
class Order:
def __init__(self, id, qty, name, price):
self.__orderID = id
self.__quantity = qty
self.__itemName = name
self.__price = price
def GetName(self):
return self.__itemName
def GetPrice(self):
return self.__price
def GetQty(self):
return self.__quantity
def GetID(self):
return self.__orderID
def GetSubtotal(self):
return float(self.__quantity) * float(self.__price)
def Serialize(self):
outbuff = str(self.__orderID) + " "
outbuff = outbuff + str(self.__quantity) + " "
outbuff = outbuff + str(self.__itemName) + " "
outbuff = outbuff + str(self.__price)
return(outbuff)
############################################################
#creates the objects
N=5
orderRec1 = Order(0,1,"pizza",34.2)
orderRec2 = Order(1,1,"chips",2.30)
orderRec3 = Order(2,2,"soda",2)
orderRec4 = Order(3,3,"burger",10.2)
orderRec5 = Order(4,1,"tart",6.43)
#prints them out
print(orderRec1.Serialize())
print(orderRec2.Serialize())
print(orderRec3.Serialize())
print(orderRec4.Serialize())
print(orderRec5.Serialize())
#stores them in the array list
orders = [orderRec1,orderRec2,orderRec3,orderRec4,orderRec5]
grandTotal = 0
for order in orders:
curSubtotal = order.GetSubtotal()
grandTotal = float(grandTotal)+ float(curSubtotal)
print("the grand total is " + str(grandTotal))
print(" the average is " + str(float(grandTotal)/int(N)))