In the line
In the line
It's already known what position they are in as there is a class of the same name.
Operator and Supervisor have the same method
The methods
At the moment it Is looking like you might not even need to use subclassing.
The following makes your code work
def __init__(self,first_name,last_name,id_number,hours_worked,total_wage = 0,Position position):
id_number
is not used
- A non-default argument follows default argument
total_wage
is not used
Position position
is invalid syntax
In the line
o = Operator("Kevin","ward",0,39,0,Position.SUPERVISOR)If they are an
Operator
why are you passing the enum SUPERVISOR
?It's already known what position they are in as there is a class of the same name.
Operator and Supervisor have the same method
calculate_pay
so it may as well go in the base class.The methods
def __str__(self):
are not required in the sub classes as they don't alter anything.At the moment it Is looking like you might not even need to use subclassing.
raise Exception("FIRSTNAME FIELD CANNOT BE EMPTY!")Raising the base Exception is not very useful, raise a more specific exception or make your own exception.
amount_per_hour
will change over time so it probably shouldn't be a default valueThe following makes your code work
from abc import ABC import itertools from enum import Enum class Employee(ABC): employee_id = itertools.count(1000) def __init__( self, first_name, last_name, hours_worked, position, amount_per_hour=0 ): self.set_first_name(first_name) self.set_last_name(last_name) self.id_number = next(self.employee_id) self.set_hours_worked(hours_worked) self.amount_per_hour = amount_per_hour self.total_wage = self.calculate_pay() self.position = position def get_first_name(self): return self.first_name def get_last_name(self): return self.last_name def get_id_number(self): return self.id_number def get_hours_worked(self): return self.hours_worked def set_first_name(self, first_name): if not first_name: raise Exception("FIRSTNAME FIELD CANNOT BE EMPTY!") self.first_name = first_name def set_last_name(self, last_name): if not last_name: raise Exception("LAST NAME FIELD CANNOT BE EMPTY!") self.last_name = last_name def set_id_number(self, id_number): self.id_number = id_number def set_hours_worked(self, hours_worked): if hours_worked < 0: raise Exception("HOURS CANNOT BE LESS THAN 0!") self.hours_worked = hours_worked def calculate_pay(self): return round(self.amount_per_hour * self.hours_worked) def __str__(self): return ( f"\nName: {self.first_name} \nLastName: {self.last_name} \n" f"Id Number: {self.id_number} \nHours Worked: {self.hours_worked} \n" f"Total Pay: {self.calculate_pay()} \nPosition: {self.position}" ) class Operator(Employee): def __init__(self, first_name, last_name, hours_worked, amount_per_hour=9.80): super().__init__( first_name, last_name, hours_worked, Position.OPERATOR, amount_per_hour ) class Supervisor(Employee): def __init__(self, first_name, last_name, hours_worked, amount_per_hour=12.10): super().__init__( first_name, last_name, hours_worked, Position.SUPERVISOR, amount_per_hour ) class Manager(Employee): def __init__(self, first_name, last_name, hours_worked): super().__init__(first_name, last_name, hours_worked, Position.MANAGER) def calculate_pay(self): return 4000 class Position(Enum): OPERATOR = "operator" SUPERVISOR = "supervisor" MANAGER = "manager" def main(): operator = Operator("Kevin", "ward", 39) print(operator) supervisor = Supervisor("Bob", "Jobs", 10) print(supervisor) manager = Manager("Almighty", "Dictator", 1) print(manager) if __name__ == "__main__": main()
Output:Name: Kevin
LastName: ward
Id Number: 1000
Hours Worked: 39
Total Pay: 382
Position: Position.OPERATOR
Name: Bob
LastName: Jobs
Id Number: 1001
Hours Worked: 10
Total Pay: 121
Position: Position.SUPERVISOR
Name: Almighty
LastName: Dictator
Id Number: 1002
Hours Worked: 1
Total Pay: 4000
Position: Position.MANAGER