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 BurgerStands:
def __init__(self, id, burgerssold):
""" Constructor function initializing id and burgers sold.
"""
self.id = id
self.burgerssold = burgerssold
def justsold(self):
""" Function to increment the number of burgers sold by one.
"""
self.burgerssold += 1
return self.burgerssold
def __del__(self):
""" Destructor function for BurgerStands objects.
"""
print('BurgerStand<id: {}> destroyed'.format(self.id))
def totalburgerssold(stands):
""" Function to calculate total burgers sold in all stands.
"""
burgers = [stand.burgerssold for stand in stands]
totalsold = sum(burgers)
return totalsold
def writedatafile(burgerstands):
""" Function to enter burger stand details and total burgers sold in data file.
"""
f = open('burgerstands.dat', 'w')
f.write("Id\tBurgers sold\n")
for burgerstand in burgerstands:
f.write("{}\t{}\n".format(burgerstand.id, burgerstand.burgerssold))
f.write("\n")
f.write("Total burgers sold: {}\n".format(totalburgerssold(burgerstands)))
f.close()
# Test case
def updatesalesrecord(stand, burgerssold):
""" Function to update sales record for a burger stand.
"""
print("BurgerStand<id: {}>".format(stand.id))
print("Initial number of burgers: {}".format(stand.burgerssold))
for _ in range(burgerssold):
stand.justsold()
print("Final number of burgers after selling {}: {}\n".format(burgerssold, stand.burgerssold))
stands = [BurgerStands(1, 400), BurgerStands(2, 600)]
# Assuming a stand will sell exactly the initial number of burgers it had
for stand in stands:
updatesalesrecord(stand, stand.burgerssold)
writedatafile(stands)
Comments
Leave a comment