0

I am trying to make it so when an instance of Customers is created and the restaurant they are going to is specified the number_served instance attribute of that instance of the Restaurant class is incremented by the number of customers going to the restaurant.

I am not sure if the number_served should be a class or instance attribute (i think class) and I am not sure how I can inherit the attribute from the Restaurant class to the Customers class so I can increment it.

class Restaurant:



    def __init__(self,name,restaurant_type,cuisine_type):
        self.name = name 
        self.restaurant_type = restaurant_type
        self.cuisine_type = cuisine_type
        self.number_served = 0 


    def describe_restaurant(self):
        print(f"{self.name} is {self.restaurant_type} which serves {self.cuisine_type}.")

    def open_restaurant(self):
        print(f"{self.name} is open.")

    def get_number_served(self):
        print(f"{self.number_served} customers served at the {self.name} today.")

    def increment_number_served(self,num):
        self.number_served += num

class Customers(Restaurant):

    def __init__(self,where_eating,no_of_people):
        self.where_eating = where_eating
        self.no_of_people = no_of_people

    def go_eat(self):
        self.increment_number_served(self.no_of_people)

I basically want the number_served attribute of a restaurant instance to be incremented by the number of customers who went to the restaurant from an instance of the Customers class

3
  • 3
    Why is Customers inheriting from Restaurant in the first place? A customer is not a kind of restaurant. Commented Oct 30, 2019 at 17:02
  • 3
    Instead, Customers.go_eat should take a Restaurant instance as an argument, and the increment_number_served method of that instance should be invoked. Commented Oct 30, 2019 at 17:03
  • 1
    Python does not automatically call the parent class __init__ constructor. You need to use super().__init__(...) to correctly initialize instances Commented Oct 30, 2019 at 17:06

1 Answer 1

2

A customer is not a type of restaurant, so inheritance is not appropriate here. The go_eat method should take an instance of Restaurant as an argument instead.

class Restaurant:
    def __init__(self, name, restaurant_type, cuisine_type):
        self.name = name 
        self.restaurant_type = restaurant_type
        self.cuisine_type = cuisine_type
        self.number_served = 0 

    def describe_restaurant(self):
        print(f"{self.name} is {self.restaurant_type} which serves {self.cuisine_type}.")

    def open_restaurant(self):
        print(f"{self.name} is open.")

    def get_number_served(self):
        print(f"{self.number_served} customers served at the {self.name} today.")

    def increment_number_served(self,num):
        self.number_served += num


r = Restaurant("...", "...", "...")


class Customers:

    def __init__(self, no_of_people):
        self.no_of_people = no_of_people

    def go_eat(self, restaurant):
        restaurant.increment_number_served(self.no_of_people)


c = Customers(9)
c.go_eat(r)

If you want Customers to represent not just a group of diners, but rather a group of diners going to particular restaurant, then pass r to __init__:

class Customers:

    def __init__(self, where, no_of_people):
        self.where = where
        self.no_of_people = no_of_people

    def go_eat(self):
        self.where.increment_number_served(self.no_of_people)


c = Customers(r, 9)
c.go_eat()
Sign up to request clarification or add additional context in comments.

1 Comment

What's that :: after Customers @chepner

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.