Answer to Question #160054 in Python for elya

Question #160054

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-30T16:11:37-0500

Here the answer:

class BurgerStand:
   total = 0 # stores total number
   stands = list() # stores list of stands

   def __init__(self, id_, number):       
       self.id = id_
       self.number = number

       BurgerStand.stands.append(id_) 
       BurgerStand.total += number

   def justsold(self):
       self.number += 1
       BurgerStand.total += 1

       return self.number

   def get_all(self):
       return BurgerStand.total

   def write(self):
       path = 'Data.txt'

       result = ''
       for stand in BurgerStand.stands:
           result += str(stand) + '\n'
       result += 'Total: ' + str(BurgerStand.total)

       with open(path, 'w') as f:
           f.write(result)

   def __del__(self):
       BurgerStand.total -= self.number
       BurgerStand.stands.remove(self.id)

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