1. You operate several BurgerStands distributed throughout town. Define a class named BurgerStand that has a member variable for the burger stand's id number and member variable for how many burgers that stand sold that day.
a) create a constructor that allows user of the class to initialize values for id number and for how many burgers sold that day and a destructor
b) create a function named justsold that show increments of the number of burgers the stand has sold by one. (This function will invoked each time the stand sells a burger so that you can track the total number of burgers sold by the stand. And returns the number of burgers sold.)
c) create a function that calculate the total number of burgers sold by all stands.
d) write in a data file, the list of burger stands and the total number of burgers sold by all stands.
class BurgerStand:
# create a constructor that allows user of the class to
# initialize values for id number and for how many burgers
# sold that day
def __init__(self,idNumber):
self.idNumber=idNumber
self.burgersSold=0
# destructor
def __del__(self):
self.idNumber=0
self.burgersSold=0
# create a function named justsold that show increments of
# the number of burgers the stand has sold by one.
def justsold(self):
self.burgersSold+=1
print("The number of burgers for burgerstand with id number "+str(self.idNumber)+" is: "+str(self.burgersSold))
return self.burgersSold
# The start point of the program
def main():
# create array of burger stands
burgerStands=[]
newBurgerStand1=BurgerStand(111111)
for i in range(5):
newBurgerStand1.justsold()
newBurgerStand2=BurgerStand(222222)
newBurgerStand2.justsold()
newBurgerStand3=BurgerStand(333333)
for i in range(10):
newBurgerStand3.justsold()
newBurgerStand4=BurgerStand(444444)
for i in range(3):
newBurgerStand4.justsold()
newBurgerStand5=BurgerStand(555555)
for i in range(8):
newBurgerStand5.justsold()
# add a new burgerStand to array
burgerStands.append(newBurgerStand1)
burgerStands.append(newBurgerStand2)
burgerStands.append(newBurgerStand3)
burgerStands.append(newBurgerStand4)
burgerStands.append(newBurgerStand5)
# write in a data file, the list of burger stands and the total number
# of burgers sold by all stands.
totalNumberBurgers=calculateTotalNumberBurgersSold(burgerStands)
fileBurgerStands = open("BurgerStands.txt", "w")
for bs in burgerStands:
fileBurgerStands.write("The number of burgers for burgerstand with id number "+str(bs.idNumber)+" is: "+str(bs.burgersSold)+"\r\n")
fileBurgerStands.write("The total number of burgers sold by all stands: " +str(totalNumberBurgers))
fileBurgerStands.close()
# a function that calculate the total number of burgers sold by all stands.
def calculateTotalNumberBurgersSold(burgerStands):
# Declare totalNumberBurgers variable
totalNumberBurgers=0
for bs in burgerStands:
totalNumberBurgers=totalNumberBurgers+bs.burgersSold
return totalNumberBurgers
# call main method
main()
Comments
Leave a comment