Python Forum
Class/Method Confusion
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Class/Method Confusion
#1
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
Reply
#2
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'.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Please, how do I call the method inside this class Emekadavid 1 1,630 Jun-26-2020, 01:26 PM
Last Post: Yoriz
  class method on two object gabejohnsonny21 4 2,432 Apr-22-2020, 06:57 AM
Last Post: DeaD_EyE
  Class and calling a method Zatoichi 3 3,091 Mar-13-2018, 08:44 PM
Last Post: Zatoichi

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020