Answer to Question #158266 in Python for RajaShekhar

Question #158266

Can you do this question in Python code that can be run in Onlinegdb Site?

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-26T08:11:10-0500
class BurgerStands:

    # A class variable to hold the total number of burgers sold
    total_burgers_sold = 0

    def __init__(self, stand_id, burgers_sold):
        """Constructor for the class"""
        self.stand_id = stand_id
        self.burgers_sold = burgers_sold
        BurgerStands.total_burgers_sold += burgers_sold

    def __del__(self):
        """Destructor for the class"""
        self.stand_id = ''
        self.burgers_sold = 0

    def justsold(self):
        """This method increments the burgers sold for the stand its called on and increments the total burger count.
            Returns the burgers sold by the current stand."""
        self.burgers_sold += 1
        BurgerStands.total_burgers_sold += 1
        return self.burgers_sold

    @staticmethod
    def total_burgers():        # Static method is not dependent on objects, but on the class
        return BurgerStands.total_burgers_sold


# Driver code to test the above class
a = BurgerStands('Stand A', 2)
b = BurgerStands('Stand B', 3)
a.justsold()
b.justsold()
print(BurgerStands.total_burgers())

# Writing the details about burger stands in a datafile
f = open('output.txt', 'w') # Open a file in write mode
f.write("Stand\t\tBurgers Sold\n")
f.write("{}\t\t{}\n".format(a.stand_id, a.burgers_sold))
f.write("{}\t\t{}\n".format(b.stand_id, b.burgers_sold))
f.write("\nTotal burgers sold by all stands: {}".format(BurgerStands.total_burgers()))
f.close()

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