Python Forum

Full Version: Class/Method Confusion
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am required to write a method within a class which will refer to Employees as their job titles rather than as Employees.

##Homer order from <Employee: name = Pat salary = 40000>
##Bob makes pizza
##oven bakes
##Homer pays for item to <Employee: name = Pat salary = 40000>
##...
##Shaggy order from <Employee: name = Pat salary = 40000>
##Bob makes pizza
##oven bakes
##Shaggy pays for item to <Employee: name = Pat salary = 40000>

### We need to add one method to one of the following classes so
### after that the output of the code will look like the following

##Homer order from <Server: name = Pat salary = 40000>
##Bob makes pizza
##oven bakes
##Homer pays for item to <Server: name = Pat salary = 40000>
##...
##Shaggy order from <Server: name = Pat salary = 40000>
##Bob makes pizza
##oven bakes
##Shaggy pays for item to <Server: name = Pat salary = 40000>

I am required to add a method somewhere in here

class Employee:
    def __init__(self, name, salary=0):
        self.name = name
        self.salary = salary

    def giveraise(self, percent):
        self.salary += self.salary*percent

    def work(self):
        print(self.name, "does stuff")

    def __str__(self):
        return "<Employee: name = "+ str(self.name) + " salary = " +str(self.salary) +">"
    
class Chef(Employee):
    def __init__(self, name):
        Employee.__init__(self, name, 50000)

    def work(self):
        print(self.name, "makes food")

class Server(Employee):
    def __init__(self, name):
        Employee.__init__(self, name, 40000)

    def work(self):
        print(self.name, "interfaces with customer")


class PizzaRobot(Chef):
    def work(self):
        print(self.name, "makes pizza")

class Customer:
    def __init__(self, name):
        self.name = name

    def order(self, server):
        print(self.name, "order from", server)

    def pay(self, server):
        print(self.name, "pays for item to", server)

class Oven:
    def bake(self):
        print("oven bakes")

class PizzaShop:
    def __init__(self):
        self.server = Server("Pat")
        self.chef = PizzaRobot("Bob")
        self.oven = Oven()

    def order(self, name):
        customer = Customer(name)
        customer.order(self.server)
        self.chef.work()
        self.oven.bake()
        customer.pay(self.server)

scene = PizzaShop()
scene.order("Homer")
print("...")
scene.order("Shaggy")
I believe I am meant to add a method to the Employee class which will specify referring to employees as their job titles but i do not know how. Any ideas? thanks
You don't need a new method. You need to change Employee.__str__ to use the class name (self.__class__.__name__) instead of just the word 'Employee'.