In: Computer Science
Create automated Pizza Restaurant System which has the following details: a. A class Employee that has attributes like name, date of joining, salary and a job description. b. An Employee can be a Server or a Chef. A Server gets a salary of 4000 AED and has a job description “customer interface”. A Chef gets a salary of 5000 AED and has a job description “makes food”. The restaurant also has a Pizza Robot that is a chef and has a salary of 0 AED and has a job description “makes pizza”. The robot has other attributes like manufacturer, date of manufacture, and next service date. c. Create a separate test module where instances of the class are created, and the methods are tested with appropriate instances for each and print the job description and salary for each employee, including the robot chef!
Question 1 :
CODE :
import datetime #importing library for date objects
class Employee: #Employee class which is the super class
def __init__(self,name,doj,salary,job_description):
self.name=name
self.doj=doj
self.salary=salary
self.job_description=job_description
def __str__(self): #To print the employee details will be inherited
by all the subclasses
s="\nName : "+self.name+"\nDate of joining : "+str(self.doj)+"\nJob
Description : "+self.job_description
s=s+"\nSalary : "+str(self.salary)+" AED"
return s
class Server(Employee): #Server is a type of employee so its the
subclass of Employee
def __init__(self,name,doj): #only name and date of joining is
given by user whereas the salary and job description is fixed
super().__init__(name, doj, 4000, 'customer interface')
class Chef(Employee): #Chef is a type of employee so its the
subclass of Employee
def __init__(self,name,doj): #only name and date of joining is
given by user whereas the salary and job description is fixed
super().__init__(name, doj, 5000, 'makes food')
class PizzaRobot(Employee): #A type of chef but super class will be
employee because of the extra details
def __init__(self,name,doj,manufacturer,dom,service_date):
super().__init__(name, doj, 0, 'makes pizza') #Calling the Employee
class constructor using super()
self.manufacturer=manufacturer
self.dom=dom
self.service_date=service_date
def set_service_date(self,sdate): #This is a setter function
because the service dates will keep changing
self.service_date=sdate
def __str__(self): #to print details first print from the super
class and then the extra details of PizzaRobot class
print(super().__str__())
return "Manufacturer : "+self.manufacturer+"\nDate of manufacture :
"+str(self.dom)+"\nService Date : "+str(self.service_date)
#Test 1 where a Server is created and printed using __str__()
s1=Server("Mary Fonollonsa",datetime.date(2020,5,16))
print(s1.__str__())
#Test 2 where a Chef is created and printed using
__str__()
c1=Chef("Esther Romando",datetime.date(2020,6,12))
print(c1.__str__())
#Test 3 where a PizzaRobot is created and printed using
__str__()
p1=PizzaRobot("Robot 1.5X",datetime.date(2018,4,30),"TDX Robotics
Inc.",datetime.date(2017,4,30),datetime.date(2018,12,1))
print(p1.__str__())
##Test 4 where an Employee is created and printed using
__str__()
e1=Employee("Ronaldo
Vinci",datetime.date(2019,12,30),6000,"Manager")
print(e1.__str__())
CODE Screenshots :
OUTPUT :
Explanation of the code :
The code is about object oriented programming that is why there are 4 classes in the code and inheritance concept have been used.
The super class is the Employee with 4 fields. Now for Employee type Server and Chef the 2 fields are fixed i.e. salary and job description. So, these 2 classes are the subclasses of the Employee.
Then come the PizzaRobot class which is mentioned as the chef but also has 2 things fixed i.e. salary and job description. So instead of inheriting from the Chef class, it has been inherited from the Employee. IT also has some other fields which are taken care of by self variables.
All the information of the classes are printed by using __str__() functions. Since, Employee is the super class all its methods are inherited by its subclasses so, the __str__() function is present in the Employee class.
But PizzaRobot class has more new self variables and to print that it has its own __str__(). It first calls and prints the super() class i.e. Employee class __str__() because half information is already printable by using it and then rest of it is printed using its own __str__() function.