Answer to Question #159761 in Python for JOHN

Question #159761

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.



1
Expert's answer
2021-01-30T06:37:54-0500
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)


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS