Created
August 3, 2021 21:48
-
-
Save Louis95/9eeea25fba714ef79d91d7b67c18142f to your computer and use it in GitHub Desktop.
An employee management system following python good python coding practices.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Very advanced Employee management system. | |
""" | |
from dataclasses import dataclass | |
from typing import List | |
from enum import Enum, auto | |
from abc import ABC, abstractmethod | |
FIXED_VACATION_DAYS_PAYOUT = 5 # The fixed nr of vacation days that can be paid out. | |
class VacationDaysShortageError(Exception): | |
"""This a custom error that is raised when not enough vacation days are available""" | |
def __init__(self, requested_days: int, remaining_days: int, message: str) -> None: | |
self.requested_days = requested_days | |
self.remaining_days = remaining_days | |
self.message = message | |
super().__init__(message) | |
class Role(Enum): | |
"""Employee Roles""" | |
PRESIDENT = auto() | |
VICEPRESIDENT = auto() | |
MANAGER = auto() | |
LEAD = auto() | |
WORKER = auto() | |
INTERN = auto() | |
@dataclass | |
class Employee(ABC): | |
"""Basic representation of an employee at the company.""" | |
name: str | |
role: Role | |
vacation_days: int = 25 | |
def take_a_holiday(self) -> None: | |
"""Let the employee take a single holiday""" | |
if self.vacation_days < 1: | |
raise VacationDaysShortageError( | |
requested_days = 1, | |
remaining_days = self.vacation_days, | |
message="You don't have any holidays left. Now back to work, you!" | |
) | |
self.vacation_days -= 1 | |
print("Have fun on your holiday. Don't forget to check your emails!") | |
def pay_a_holiday(self) -> None: | |
"""Let the employee get paid for used holidays""" | |
# check that there are enough vacation days left for a payout | |
if self.vacation_days < FIXED_VACATION_DAYS_PAYOUT: | |
raise VacationDaysShortageError( | |
requested_days = FIXED_VACATION_DAYS_PAYOUT, | |
remaining_days = self.vacation_days, | |
message="You don't have enough holidays left over for a payout." | |
) | |
self.vacation_days -= FIXED_VACATION_DAYS_PAYOUT | |
print(f"Paying out a holiday. Holidays left: {self.vacation_days}") | |
@abstractmethod | |
def pay(self) -> None: | |
"""method to call when paying an employee""" | |
@dataclass | |
class HourlyEmployee(Employee): | |
"""Employee that's paid based on number of worked hours.""" | |
hourly_rate_dollars: float = 50 | |
hours_worked: int = 10 | |
def pay(self) -> None: | |
print( | |
f"Paying employee {self.name} a hourly rate of \ | |
${self.hourly_rate} for {self.amount} hours." | |
) | |
@dataclass | |
class SalariedEmployee(Employee): | |
"""Employee that's paid based on a fixed monthly salary.""" | |
monthly_salary: float = 5000 | |
def pay(self) -> None: | |
print( | |
f"Paying employee {self.name} a monthly salary of ${self.monthly_salary}." | |
) | |
class Company: | |
"""Represents a company with employees.""" | |
def __init__(self) -> None: | |
self.employees: List[Employee] = [] | |
def add_employee(self, employee: Employee) -> None: | |
"""Add an employee to the list of employees.""" | |
self.employees.append(employee) | |
def find_employees(self, role: Role) -> List[Employee]: | |
"""Find all employees with a particular role.""" | |
return [employee for employee in self.employees if employee.role is role] | |
def main() -> None: | |
"""Main function.""" | |
company = Company() | |
company.add_employee(SalariedEmployee(name="Louis", role=Role.MANAGER)) | |
company.add_employee(HourlyEmployee(name="Brenda", role=Role.PRESIDENT)) | |
company.add_employee(HourlyEmployee(name="Tim", role=Role.INTERN)) | |
print(company.find_employees(Role.MANAGER)) | |
print(company.find_employees(Role.PRESIDENT)) | |
print(company.find_employees(Role.INTERN)) | |
company.employees[0].pay() | |
company.employees[0].take_a_holiday() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment